matmod/R.py

128 lines
3 KiB
Python
Raw Normal View History

2024-04-08 23:59:21 -04:00
"""
Modeling the logic R
"""
from logic import (
PropositionalVariable,
Rule,
Logic,
Implication,
Conjunction,
Negation,
Disjunction,
Rule,
)
2024-05-03 13:06:52 -04:00
from model import Model, ModelFunction, ModelValue, violates_vsp
2024-04-08 23:59:21 -04:00
from generate_model import generate_model
# ===================================================
# Defining the logic of R
x = PropositionalVariable("x")
y = PropositionalVariable("y")
z = PropositionalVariable("z")
implication_rules = {
2024-04-15 00:08:00 -04:00
Rule(set(), Implication(x, x)),
2024-04-08 23:59:21 -04:00
Rule({Implication(x, y), Implication(y, z)}, Implication(x, z)),
2024-04-15 00:08:00 -04:00
Rule({Implication(x, Implication(x, y)),}, Implication(x, y)),
Rule({Implication(x, Implication(y, z)),}, Implication(y, Implication(x, z))),
Rule({Implication(x, y),}, Implication(Implication(z, x), Implication(z, y))),
Rule({Implication(x, y),}, Implication(Implication(y, z), Implication(x, z))),
2024-04-08 23:59:21 -04:00
Rule({Implication(x, y), x}, y)
}
negation_rules = {
2024-04-15 00:08:00 -04:00
Rule({Negation(Negation(x)),}, x),
Rule({x,}, Negation(Negation(x))),
2024-04-08 23:59:21 -04:00
Rule({Implication(x, y)}, Implication(Negation(y), Negation(x))),
2024-04-15 00:08:00 -04:00
Rule({Implication(x, y),}, Implication(Negation(y), Negation(x)))
2024-04-08 23:59:21 -04:00
}
conjunction_rules = {
Rule({y, z}, Conjunction(y, z)),
2024-04-15 00:08:00 -04:00
Rule({Conjunction(x, y),}, x),
Rule({Conjunction(x, y),}, y),
Rule({Conjunction(Implication(x, y), Implication(x, z)),}, Implication(x, Conjunction(y, z)))
2024-04-08 23:59:21 -04:00
}
disjunction_rules = {
2024-04-15 00:08:00 -04:00
Rule({x,}, Disjunction(x, y)),
Rule({y,}, Disjunction(x, y)),
Rule({Conjunction(Implication(x, z), Implication(y, z)),}, Implication(Disjunction(x, y), z)),
Rule({Conjunction(x, Disjunction(y, z)),}, Disjunction(Conjunction(x, y), Conjunction(x, z)))
2024-04-08 23:59:21 -04:00
}
2024-04-15 00:08:00 -04:00
2024-04-08 23:59:21 -04:00
logic_rules = implication_rules | negation_rules | conjunction_rules | disjunction_rules
operations = {Negation, Conjunction, Disjunction, Implication}
R_logic = Logic(operations, logic_rules)
# ===============================
# Example Model of R
a0 = ModelValue("a0")
a1 = ModelValue("a1")
carrier_set = {a0, a1}
2024-05-03 13:06:52 -04:00
mnegation = ModelFunction(1, {
2024-04-08 23:59:21 -04:00
a0: a1,
a1: a0
})
2024-05-03 13:06:52 -04:00
mimplication = ModelFunction(2, {
2024-04-08 23:59:21 -04:00
(a0, a0): a1,
(a0, a1): a1,
(a1, a0): a0,
(a1, a1): a1
})
2024-05-03 13:06:52 -04:00
mconjunction = ModelFunction(2, {
2024-04-08 23:59:21 -04:00
(a0, a0): a0,
(a0, a1): a0,
(a1, a0): a0,
(a1, a1): a1
})
2024-05-03 13:06:52 -04:00
mdisjunction = ModelFunction(2, {
2024-04-08 23:59:21 -04:00
(a0, a0): a0,
(a0, a1): a1,
(a1, a0): a1,
(a1, a1): a1
})
designated_values = {a1}
logical_operations = {
mnegation, mimplication, mconjunction, mdisjunction
}
R_model_2 = Model(carrier_set, logical_operations, designated_values)
interpretation = {
Negation: mnegation,
Conjunction: mconjunction,
Disjunction: mdisjunction,
Implication: mimplication
}
# =================================
# Generate models of R of a given size
model_size = 2
2024-04-15 00:08:00 -04:00
satisfiable_models = generate_model(R_logic, model_size, print_model=True)
2024-04-08 23:59:21 -04:00
print(f"There are {len(satisfiable_models)} satisfiable models of element length {model_size}")
2024-05-03 13:06:52 -04:00
for smodel in satisfiable_models:
print(violates_vsp(smodel[0], smodel[1]))