matmod/parse_magic.py

497 lines
16 KiB
Python
Raw Normal View History

"""
Parses the Magic Ugly Data File Format
Assumes the base logic is R with no extra connectives
"""
import argparse
import sys
from typing import TextIO, List, Optional, Tuple, Set, Dict
from model import Model, ModelValue, ModelFunction
from logic import (
Implication,
Conjunction,
Negation,
2024-10-04 15:51:05 -04:00
Necessitation,
Disjunction
)
from vsp import has_vsp
2024-06-23 23:02:53 -04:00
class SourceFile:
def __init__(self, fileobj: TextIO):
self.fileobj = fileobj
self.current_line = 0
def __next__(self):
contents = next(self.fileobj)
self.current_line += 1
return contents
2024-10-03 23:34:59 -04:00
class UglyHeader:
2024-10-24 21:18:56 -04:00
def __init__(self, negation: bool, necessitation: bool, custom_model_functions: List[Tuple[int, str]]):
2024-10-03 23:34:59 -04:00
self.negation = negation
self.necessitation = necessitation
2024-10-24 21:18:56 -04:00
self.custom_model_functions = custom_model_functions
2024-06-23 23:02:53 -04:00
2024-10-04 14:09:18 -04:00
class ModelBuilder:
def __init__(self):
self.size : int = 0
self.carrier_set : Set[ModelValue] = set()
self.num_negation: int = 0
self.mnegation: Optional[ModelFunction] = None
self.num_order: int = 0
self.mconjunction: Optional[ModelFunction] = None
self.mdisjunction: Optional[ModelFunction] = None
self.num_designated: int = 0
self.designated_values: Set[ModelValue] = set()
self.num_implication: int = 0
self.mimplication: Optional[ModelFunction] = None
2024-10-04 15:51:05 -04:00
self.num_necessitation: int = 0
self.mnecessitation: Optional[ModelFunction] = None
2024-10-03 23:34:59 -04:00
def parse_matrices(infile: SourceFile) -> List[Tuple[Model, Dict]]:
solutions = [] # Reset
header = parse_header(infile)
2024-10-04 14:09:18 -04:00
current_model_parts = ModelBuilder()
process_sizes(infile, header, current_model_parts, solutions)
return solutions
2024-10-04 14:09:18 -04:00
def process_sizes(infile: SourceFile, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
2024-10-03 23:34:59 -04:00
"""Stage 1"""
2024-10-04 15:51:05 -04:00
2024-10-15 10:28:16 -04:00
first_run = True
2024-10-04 15:51:05 -04:00
while True:
2024-10-04 15:51:05 -04:00
print("Processing next size")
try:
2024-10-15 10:28:16 -04:00
size = parse_size(infile, first_run)
first_run = False
2024-10-04 15:51:05 -04:00
except StopIteration:
# For some reason, when necessitation is enabled this doesn't
# have a -1 on the last line
break
if size is None:
break
2024-10-04 15:51:05 -04:00
carrier_set = carrier_set_from_size(size)
2024-10-04 14:09:18 -04:00
current_model_parts.size = size
current_model_parts.carrier_set = carrier_set
process_negations(infile, header, current_model_parts, solutions)
2024-10-04 14:09:18 -04:00
def process_negations(infile: SourceFile, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
2024-10-03 23:34:59 -04:00
"""Stage 2 (Optional)"""
num_negation = 0
while True:
2024-10-04 15:51:05 -04:00
print("Processing next negation")
2024-10-03 23:34:59 -04:00
mnegation = None
if header.negation:
2024-10-04 14:09:18 -04:00
mnegation = parse_single_negation(infile, current_model_parts.size)
if mnegation is None:
break
num_negation += 1
2024-10-04 14:09:18 -04:00
current_model_parts.num_negation = num_negation
current_model_parts.mnegation = mnegation
process_orders(infile, header, current_model_parts, solutions)
2024-10-03 23:34:59 -04:00
if not header.negation:
break
2024-10-04 14:09:18 -04:00
def process_orders(infile: SourceFile, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
2024-10-03 23:34:59 -04:00
"""Stage 3"""
num_order = 0
while True:
2024-10-04 15:51:05 -04:00
print("Processing next order")
2024-10-04 14:09:18 -04:00
result = parse_single_order(infile, current_model_parts.size)
2024-10-03 23:34:59 -04:00
if result is None:
break
num_order += 1
2024-10-04 14:09:18 -04:00
mconjunction, mdisjunction = result
current_model_parts.num_order = num_order
current_model_parts.mconjunction = mconjunction
current_model_parts.mdisjunction = mdisjunction
process_designateds(infile, header, current_model_parts, solutions)
2024-10-03 23:34:59 -04:00
2024-10-04 14:09:18 -04:00
def process_designateds(infile: SourceFile, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
2024-10-03 23:34:59 -04:00
"""Stage 4"""
num_designated = 0
while True:
2024-10-04 15:51:05 -04:00
print("Processing next designated")
2024-10-04 14:09:18 -04:00
designated_values = parse_single_designated(infile, current_model_parts.size)
2024-10-03 23:34:59 -04:00
if designated_values is None:
break
num_designated += 1
2024-10-04 14:09:18 -04:00
current_model_parts.num_designated = num_designated
current_model_parts.designated_values = designated_values
process_implications(infile, header, current_model_parts, solutions)
2024-10-03 23:34:59 -04:00
def process_implications(
2024-10-04 14:09:18 -04:00
infile: SourceFile, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
2024-10-03 23:34:59 -04:00
"""Stage 5"""
2024-10-04 15:51:05 -04:00
if header.necessitation:
num_implication = 0
while True:
print("Processing next implication")
instr = next(infile).strip()
mimplication, reststr = parse_single_implication(instr, infile.current_line, current_model_parts.size)
if mimplication is None:
break
num_implication += 1
current_model_parts.num_implication = num_implication
current_model_parts.mimplication = mimplication
process_necessitations(infile, reststr, header, current_model_parts, solutions)
else:
results = parse_implications(infile, current_model_parts.size)
for num_implication, mimplication in enumerate(results, 1):
current_model_parts.num_implication = num_implication
current_model_parts.mimplication = mimplication
process_model(current_model_parts, solutions)
def process_necessitations(infile: SourceFile, instr: str, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
# NOTE: For some reason, one necessitation table will be on the same line as the implication table
mnecessitation = parse_single_necessitation_from_str(instr, infile.current_line, current_model_parts.size)
assert mnecessitation is not None, f"Expected Necessitation Table at line {infile.current_line}"
num_necessitation = 1
current_model_parts.num_necessitation = num_necessitation
current_model_parts.mnecessitation = mnecessitation
process_model(current_model_parts, solutions)
while True:
print("Processing next necessitation")
mnecessitation = parse_single_necessitation(infile, current_model_parts.size)
if mnecessitation is None:
break
num_necessitation += 1
current_model_parts.num_necessitation = num_necessitation
current_model_parts.mnecessitation = mnecessitation
2024-10-04 14:09:18 -04:00
process_model(current_model_parts, solutions)
2024-10-03 23:34:59 -04:00
2024-10-04 14:09:18 -04:00
def process_model(mp: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
2024-10-03 23:34:59 -04:00
"""Create Model"""
2024-10-04 14:09:18 -04:00
assert mp.mimplication is not None
assert len(mp.carrier_set) > 0
2024-10-03 23:34:59 -04:00
2024-10-04 14:09:18 -04:00
logical_operations = { mp.mimplication }
2024-10-04 15:51:05 -04:00
model_name = f"{mp.size}{'.' + str(mp.num_negation) if mp.num_negation != 0 else ''}.{mp.num_order}.{mp.num_designated}.{mp.num_implication}{'.' + str(mp.num_necessitation) if mp.num_necessitation != 0 else ''}"
2024-10-04 14:09:18 -04:00
model = Model(mp.carrier_set, logical_operations, mp.designated_values, name=model_name)
2024-10-03 23:34:59 -04:00
interpretation = {
2024-10-04 14:09:18 -04:00
Implication: mp.mimplication
2024-10-03 23:34:59 -04:00
}
2024-10-04 14:09:18 -04:00
if mp.mnegation is not None:
logical_operations.add(mp.mnegation)
interpretation[Negation] = mp.mnegation
if mp.mconjunction is not None:
logical_operations.add(mp.mconjunction)
interpretation[Conjunction] = mp.mconjunction
if mp.mdisjunction is not None:
logical_operations.add(mp.mdisjunction)
interpretation[Disjunction] = mp.mdisjunction
2024-10-04 15:51:05 -04:00
if mp.mnecessitation is not None:
logical_operations.add(mp.mnecessitation)
interpretation[Necessitation] = mp.mnecessitation
2024-10-03 23:34:59 -04:00
solutions.append((model, interpretation))
print(f"Parsed Matrix {model.name}")
def parse_header(infile: SourceFile) -> UglyHeader:
"""
Parse the header line from the ugly data format.
"""
header_line = next(infile).strip()
header_tokens = header_line.split(" ")
assert header_tokens[0] in ["0", "1"]
assert header_tokens[6] in ["0", "1"]
2024-10-24 21:18:56 -04:00
assert len(header_tokens) >= 7
2024-10-03 23:34:59 -04:00
negation_defined = bool(int(header_tokens[0]))
necessitation_defined = bool(int(header_tokens[6]))
2024-10-24 21:18:56 -04:00
num_custom_connectives = int(header_tokens[7])
custom_model_functions: List[Tuple[int, str]] = []
for i in range(num_custom_connectives):
arity = int(header_tokens[7 + (2 * i) + 1])
symbol = header_tokens[7 + (2 * i) + 2]
custom_model_functions.append((arity, symbol))
return UglyHeader(negation_defined, necessitation_defined, custom_model_functions)
def carrier_set_from_size(size: int):
"""
Construct a carrier set of model values
based on the desired size.
"""
return {
mvalue_from_index(i) for i in range(size + 1)
}
2024-10-15 10:28:16 -04:00
def parse_size(infile: SourceFile, first_run: bool) -> Optional[int]:
"""
Parse the line representing the matrix size.
"""
size = int(next(infile))
2024-10-15 10:28:16 -04:00
# HACK: The first size line may be -1 due to a bug. Skip it
if size == -1 and first_run:
size = int(next(infile))
if size == -1:
return None
2024-06-23 23:02:53 -04:00
assert size > 0, f"Unexpected size at line {infile.current_line}"
return size
2024-10-03 23:34:59 -04:00
def parse_single_negation(infile: SourceFile, size: int) -> Optional[ModelFunction]:
"""
Parse the line representing the negation table.
"""
line = next(infile).strip()
if line == '-1':
return None
row = line.split(" ")
2024-06-23 23:02:53 -04:00
assert len(row) == size + 1, f"Negation table doesn't match size at line {infile.current_line}"
mapping = {}
for i, j in zip(range(size + 1), row):
x = mvalue_from_index(i)
y = parse_mvalue(j)
mapping[(x, )] = y
return ModelFunction(1, mapping, "¬")
def mvalue_from_index(i: int):
"""
Given an index, return the
representation of the model value.
"""
return ModelValue(f"a{i}")
def parse_mvalue(x: str) -> ModelValue:
"""
Parse an element and return the model value.
"""
return mvalue_from_index(int(x))
def determine_cresult(size: int, ordering: Dict[ModelValue, ModelValue], a: ModelValue, b: ModelValue) -> ModelValue:
"""
Determine what a b should be given the ordering table.
"""
for i in range(size + 1):
c = mvalue_from_index(i)
2024-06-23 23:02:53 -04:00
if not ordering[(c, a)]:
continue
if not ordering[(c, b)]:
continue
invalid = False
for j in range(size + 1):
d = mvalue_from_index(j)
if c == d:
continue
if ordering[(c, d)]:
if ordering[(d, a)] and ordering [(d, b)]:
invalid = True
if not invalid:
return c
def determine_dresult(size: int, ordering: Dict[ModelValue, ModelValue], a: ModelValue, b: ModelValue) -> ModelValue:
"""
Determine what a b should be given the ordering table.
"""
for i in range(size + 1):
c = mvalue_from_index(i)
if not ordering[(a, c)]:
continue
if not ordering[(b, c)]:
continue
invalid = False
for j in range(size + 1):
d = mvalue_from_index(j)
if d == c:
continue
if ordering[(d, c)]:
if ordering[(a, d)] and ordering[(b, d)]:
invalid = True
if not invalid:
return c
2024-10-04 15:51:05 -04:00
def parse_single_order(infile: SourceFile, size: int) -> Optional[Tuple[ModelFunction, ModelFunction]]:
"""
Parse the line representing the ordering table
"""
line = next(infile).strip()
if line == '-1':
return None
table = line.split(" ")
2024-06-23 23:02:53 -04:00
assert len(table) == (size + 1)**2, f"Order table doesn't match expected size at line {infile.current_line}"
omapping = {}
table_i = 0
for i in range(size + 1):
x = mvalue_from_index(i)
for j in range(size + 1):
y = mvalue_from_index(j)
omapping[(x, y)] = table[table_i] == '1'
table_i += 1
cmapping = {}
dmapping = {}
for i in range(size + 1):
x = mvalue_from_index(i)
for j in range(size + 1):
y = mvalue_from_index(j)
cresult = determine_cresult(size, omapping, x, y)
if cresult is None:
print("[Warning] Conjunction and Disjunction are not well-defined")
print(f"{x}{y} = ??")
return None, None
cmapping[(x, y)] = cresult
dresult = determine_dresult(size, omapping, x, y)
if dresult is None:
print("[Warning] Conjunction and Disjunction are not well-defined")
print(f"{x} {y} = ??")
return None, None
dmapping[(x, y)] = dresult
mconjunction = ModelFunction(2, cmapping, "")
mdisjunction = ModelFunction(2, dmapping, "")
return mconjunction, mdisjunction
2024-10-04 15:51:05 -04:00
def parse_single_designated(infile: SourceFile, size: int) -> Optional[Set[ModelValue]]:
"""
Parse the line representing which model values are designated.
"""
line = next(infile).strip()
if line == '-1':
return None
row = line.split(" ")
2024-06-23 23:02:53 -04:00
assert len(row) == size + 1, f"Designated table doesn't match expected size at line {infile.current_line}"
designated_values = set()
for i, j in zip(range(size + 1), row):
if j == '1':
x = mvalue_from_index(i)
designated_values.add(x)
return designated_values
2024-10-04 15:51:05 -04:00
def parse_single_implication(instr: str, line: int, size: int) -> Tuple[ModelFunction, str]:
"""
Take the current string, parse an implication table from it,
and return along with it the remainder of the string
"""
if instr == "-1":
return None, ""
table = instr.split(" ")
assert len(table) >= (size + 1)**2, f"Implication table does not match expected size at line {line}"
mapping = {}
table_i = 0
for i in range(size + 1):
x = mvalue_from_index(i)
for j in range(size + 1):
y = mvalue_from_index(j)
r = parse_mvalue(table[table_i])
table_i += 1
mapping[(x, y)] = r
mimplication = ModelFunction(2, mapping, "")
reststr = " ".join(table[(size + 1)**2:])
return mimplication, reststr
def parse_implications(infile: SourceFile, size: int) -> List[ModelFunction]:
"""
Parse the line representing the list of implication
tables.
"""
line = next(infile).strip()
# Split and remove the last '-1' character
table = line.split(" ")[:-1]
2024-06-23 23:02:53 -04:00
assert len(table) % (size + 1)**2 == 0, f"Implication table does not match expected size at line {infile.current_line}"
table_i = 0
mimplications: List[ModelFunction] = []
for _ in range(len(table) // (size + 1)**2):
mapping = {}
for i in range(size + 1):
x = mvalue_from_index(i)
for j in range(size + 1):
y = mvalue_from_index(j)
r = parse_mvalue(table[table_i])
table_i += 1
mapping[(x, y)] = r
mimplication = ModelFunction(2, mapping, "")
mimplications.append(mimplication)
return mimplications
2024-10-04 15:51:05 -04:00
def parse_single_necessitation_from_str(instr: str, line: int, size: int) -> Optional[ModelFunction]:
"""
Parse the line representing the necessitation table.
"""
if instr == "-1":
return None
row = instr.split(" ")
assert len(row) == size + 1, f"Necessitation table doesn't match size at line {line}"
mapping = {}
for i, j in zip(range(size + 1), row):
x = mvalue_from_index(i)
y = parse_mvalue(j)
mapping[(x, )] = y
return ModelFunction(1, mapping, "!")
def parse_single_necessitation(infile: SourceFile, size: int) -> Optional[ModelFunction]:
line = next(infile).strip()
return parse_single_necessitation_from_str(line, infile.current_line, size)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="VSP Checker")
parser.add_argument("--verbose", action='store_true', help="Print out all parsed matrices")
args = vars(parser.parse_args())
2024-10-04 14:09:18 -04:00
solutions = parse_matrices(SourceFile(sys.stdin))
print(f"Parsed {len(solutions)} matrices")
2024-10-04 13:22:40 -04:00
num_has_vsp = 0
for i, (model, interpretation) in enumerate(solutions):
2024-10-03 21:47:12 -04:00
vsp_result = has_vsp(model, interpretation)
print(vsp_result)
2024-10-04 13:22:40 -04:00
2024-10-03 21:47:12 -04:00
if args['verbose'] or vsp_result.has_vsp:
print(model)
2024-10-04 13:22:40 -04:00
if vsp_result.has_vsp:
num_has_vsp += 1
print(f"Tested {len(solutions)} models, {num_has_vsp} of which satisfy VSP")