matmod/R.py

316 lines
6.1 KiB
Python
Raw Normal View History

2024-04-08 23:59:21 -04:00
"""
Modeling the logic R
"""
from logic import (
Conjunction,
Disjunction,
2024-05-28 14:50:31 -04:00
Implication,
Logic,
Negation,
PropositionalVariable,
2024-04-08 23:59:21 -04:00
Rule,
)
2024-05-28 14:50:31 -04:00
from model import Model, ModelFunction, ModelValue, satisfiable
2024-04-08 23:59:21 -04:00
from generate_model import generate_model
2024-05-28 14:50:31 -04:00
from vsp import has_vsp
2024-04-08 23:59:21 -04:00
# ===================================================
2024-05-29 13:50:20 -04:00
"""
Defining the Logic of R
"""
2024-04-08 23:59:21 -04:00
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}
2024-05-29 13:50:20 -04:00
R_logic = Logic(operations, logic_rules, "R")
2024-04-08 23:59:21 -04:00
# ===============================
2024-05-29 13:50:20 -04:00
"""
Example 2-Element Model of R
"""
2024-04-08 23:59:21 -04:00
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,
2024-05-28 14:50:31 -04:00
(a1, a1): a1
2024-04-08 23:59:21 -04:00
})
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,
2024-05-28 14:50:31 -04:00
(a1, a1): a1
2024-04-08 23:59:21 -04:00
})
designated_values = {a1}
logical_operations = {
mnegation, mimplication, mconjunction, mdisjunction
}
2024-05-29 13:50:20 -04:00
R_model_2 = Model(carrier_set, logical_operations, designated_values, "R2")
2024-04-08 23:59:21 -04:00
interpretation = {
Negation: mnegation,
Conjunction: mconjunction,
Disjunction: mdisjunction,
Implication: mimplication
}
2024-05-29 13:50:20 -04:00
print(R_model_2)
2024-04-08 23:59:21 -04:00
# =================================
2024-05-29 13:50:20 -04:00
"""
Generate models of R of a specified size
"""
print("*" * 30)
2024-04-08 23:59:21 -04:00
2024-05-04 16:51:49 -04:00
model_size = 2
2024-05-29 13:50:20 -04:00
print("Generating models of Logic", R_logic.name, "of size", model_size)
solutions = generate_model(R_logic, model_size, print_model=False)
2024-04-08 23:59:21 -04:00
2024-05-29 13:50:20 -04:00
print(f"Found {len(solutions)} satisfiable models")
2024-04-08 23:59:21 -04:00
2024-05-04 16:51:49 -04:00
for model, interpretation in solutions:
2024-05-28 14:50:31 -04:00
print(has_vsp(model, interpretation))
2024-05-04 16:51:49 -04:00
2024-05-29 13:50:20 -04:00
print("*" * 30)
2024-05-03 13:06:52 -04:00
2024-05-03 17:04:03 -04:00
######
2024-05-29 13:50:20 -04:00
"""
Showing the smallest model for R that has the
variable sharing property.
This model has 6 elements.
"""
2024-05-03 17:04:03 -04:00
a0 = ModelValue("a0")
a1 = ModelValue("a1")
a2 = ModelValue("a2")
a3 = ModelValue("a3")
a4 = ModelValue("a4")
a5 = ModelValue("a5")
carrier_set = { a0, a1, a2, a3, a4, a5 }
designated_values = {a1, a2, a3, a4, a5 }
mnegation = ModelFunction(1, {
a5: a0,
a4: a1,
a3: a3,
a2: a2,
a1: a4,
a0: a5
2024-05-29 13:50:20 -04:00
}, "¬")
2024-05-03 17:04:03 -04:00
mimplication = ModelFunction(2, {
(a5, a5): a5,
(a5, a4): a0,
(a5, a3): a0,
(a5, a2): a0,
(a5, a1): a0,
(a5, a0): a0,
(a4, a5): a5,
(a4, a4): a1,
(a4, a3): a0,
(a4, a2): a0,
(a4, a1): a0,
(a4, a0): a0,
(a3, a5): a5,
(a3, a4): a3,
(a3, a3): a3,
(a3, a2): a0,
(a3, a1): a0,
(a3, a0): a0,
(a2, a5): a5,
(a2, a4): a2,
(a2, a3): a0,
(a2, a2): a2,
(a2, a1): a0,
(a2, a0): a0,
(a1, a5): a5,
(a1, a4): a4,
(a1, a3): a3,
(a1, a2): a2,
(a1, a1): a1,
(a1, a0): a0,
(a0, a5): a5,
(a0, a4): a5,
(a0, a3): a5,
(a0, a2): a5,
(a0, a1): a5,
(a0, a0): a5
2024-05-29 13:50:20 -04:00
}, "")
2024-05-03 17:04:03 -04:00
mconjunction = ModelFunction(2, {
(a5, a5): a5,
(a5, a4): a4,
(a5, a3): a3,
(a5, a2): a2,
(a5, a1): a1,
(a5, a0): a0,
(a4, a5): a4,
(a4, a4): a4,
(a4, a3): a3,
(a4, a2): a2,
(a4, a1): a1,
(a4, a0): a0,
(a3, a5): a3,
(a3, a4): a3,
(a3, a3): a3,
(a3, a2): a1,
(a3, a1): a1,
(a3, a0): a0,
(a2, a5): a2,
(a2, a4): a2,
(a2, a3): a1,
(a2, a2): a2,
(a2, a1): a1,
(a2, a0): a0,
(a1, a5): a1,
(a1, a4): a1,
(a1, a3): a1,
(a1, a2): a1,
(a1, a1): a1,
(a1, a0): a0,
(a0, a5): a0,
(a0, a4): a0,
(a0, a3): a0,
(a0, a2): a0,
(a0, a1): a0,
(a0, a0): a0
2024-05-29 13:50:20 -04:00
}, "")
2024-05-03 17:04:03 -04:00
mdisjunction = ModelFunction(2, {
(a5, a5): a5,
(a5, a4): a5,
(a5, a3): a5,
(a5, a2): a5,
(a5, a1): a5,
(a5, a0): a5,
(a4, a5): a5,
(a4, a4): a4,
(a4, a3): a4,
(a4, a2): a4,
(a4, a1): a4,
(a4, a0): a4,
(a3, a5): a5,
(a3, a4): a4,
(a3, a3): a3,
(a3, a2): a4,
(a3, a1): a3,
(a3, a0): a3,
(a2, a5): a5,
(a2, a4): a4,
(a2, a3): a4,
(a2, a2): a2,
(a2, a1): a2,
(a2, a0): a2,
(a1, a5): a5,
(a1, a4): a4,
(a1, a3): a3,
(a1, a2): a2,
(a1, a1): a1,
(a1, a0): a1,
(a0, a5): a5,
(a0, a4): a4,
(a0, a3): a3,
(a0, a2): a2,
(a0, a1): a1,
(a0, a0): a0
2024-05-29 13:50:20 -04:00
}, "")
2024-05-03 17:04:03 -04:00
logical_operations = {
mnegation, mimplication, mconjunction, mdisjunction
}
2024-05-29 13:50:20 -04:00
R_model_6 = Model(carrier_set, logical_operations, designated_values, "R6")
2024-05-03 17:04:03 -04:00
interpretation = {
Negation: mnegation,
Conjunction: mconjunction,
Disjunction: mdisjunction,
Implication: mimplication
}
2024-05-04 16:51:49 -04:00
print(R_model_6)
2024-05-29 13:50:20 -04:00
print(f"Model {R_model_6.name} satisfies logic {R_logic.name}?", satisfiable(R_logic, R_model_6, interpretation))
2024-05-28 14:50:31 -04:00
print(has_vsp(R_model_6, interpretation))