I have some dictionary e.g. of form:
JavaScript
2
1
my_dictionary = {'A': '¬(p)', 'B': '→(q, ∧(¬(p), ∨(y, z)))', 'Aim': ['p', '→(p, q)']}
2
Is their a simple way to replace all strings appearing in the dictionary values of form →(q, ∧(¬(p), ∨(y, z)))
or →(p, q)
by (q→(¬(p)∧(y∨z)))
or (p→q)
?
Advertisement
Answer
I found some solution by my own using some prolog:
Prolog-file (called “logic.pl“) has to contain the following code:
JavaScript
12
1
:-op(800, fx, ~).
2
:-op(801, xfy, ∧).
3
:-op(802, xfy, ∨).
4
:-op(803, xfy, →).
5
:-op(804, xfy, ↔).
6
7
8
m_Proposition_Binary_x_y(X ∨ Y, X, Y).
9
m_Proposition_Binary_x_y(X ∧ Y, X, Y).
10
m_Proposition_Binary_x_y(X → Y, X, Y).
11
m_Proposition_Binary_x_y(X ↔ Y, X, Y).
12
Now we can define some function:
JavaScript
25
1
from pyswip import Prolog
2
3
def normalize(collection):
4
interface = Prolog()
5
interface.consult("Prolog/logic.pl")
6
7
if type(collection) is str:
8
proposition = collection
9
try:
10
rest = list(interface.query(f"m_Proposition_Binary_x_y({proposition},A,B)".replace("'","")))[0]
11
return f"({normalize(rest['A'])}{proposition[0]}{normalize(rest['B'])})"
12
except:
13
return proposition
14
15
elif type(collection) is list:
16
return [normalize(x) for x in collection]
17
elif type(collection) is dict:
18
old_dict = collection
19
new_dict = {}
20
21
for key in old_dict:
22
new_dict[key] = normalize(old_dict[key])
23
24
return new_dict
25
With that background its possible to normalize the dictionary very easy:
JavaScript
4
1
my_dictionary = {'A': '~(p)', 'B': '→(q, ∧(~(p), ∨(y, z)))', 'Aim': ['p', '→(p, q)']}
2
3
print(m_str.normalize(my_dictionary))
4
Output:
JavaScript
2
1
{'A': '~(p)', 'B': '(q→(~(p)∧(y∨z)))', 'Aim': ['p', '(p→q)']}
2
Only problem, for some reason I cant use ¬ instead of ~ with pyswip library (Version: 0.2.11), cause with ¬ I get the curios error:
JavaScript
2
1
U+ffffffac is not in range [U+0000; U+10ffff]
2
whereby ¬ has unicode U+00AC ..