0

I have some dictionary e.g. of form:

my_dictionary = {'A': '¬(p)', 'B': '→(q, ∧(¬(p), ∨(y, z)))', 'Aim': ['p', '→(p, q)']}

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)?

Martin Kunze
  • 995
  • 6
  • 16
  • Feel free to mark the answer as accepted, if it helped you out. – blurryroots Jan 09 '22 at 18:59
  • Thanks, I appreciate your post (gives some very helpful hints and links), but its not exactly the answer I searched ... I found now some more or less complicated way (see below). – Martin Kunze Jan 09 '22 at 21:10

2 Answers2

1

The simplest way would probably be using any of the available string interpolation methods or regular expressions. Judging from your background in prolog, to handle first-order logic properly, you might want to look into using a FOL parse or full on solving engines, like pyprover, pyPL, PyLog or pythogic.

blurryroots
  • 402
  • 5
  • 12
0

I found some solution by my own using some prolog:

Prolog-file (called "logic.pl") has to contain the following code:

:-op(800, fx, ~).
:-op(801, xfy, ∧).
:-op(802, xfy, ∨).
:-op(803, xfy, →).
:-op(804, xfy, ↔).


m_Proposition_Binary_x_y(X ∨ Y, X, Y).
m_Proposition_Binary_x_y(X ∧ Y, X, Y).
m_Proposition_Binary_x_y(X → Y, X, Y).
m_Proposition_Binary_x_y(X ↔ Y, X, Y).

Now we can define some function:

from pyswip import Prolog

def normalize(collection):
    interface = Prolog()
    interface.consult("Prolog/logic.pl")

    if type(collection) is str:
        proposition = collection
        try:
            rest = list(interface.query(f"m_Proposition_Binary_x_y({proposition},A,B)".replace("'","")))[0]
            return f"({normalize(rest['A'])}{proposition[0]}{normalize(rest['B'])})"
        except:
            return proposition
    
    elif type(collection) is list:
        return [normalize(x) for x in collection]
    elif type(collection) is dict:
        old_dict = collection
        new_dict = {}

        for key in old_dict:
            new_dict[key] = normalize(old_dict[key])

        return new_dict 

With that background its possible to normalize the dictionary very easy:

my_dictionary = {'A': '~(p)', 'B': '→(q, ∧(~(p), ∨(y, z)))', 'Aim': ['p', '→(p, q)']}

print(m_str.normalize(my_dictionary))

Output:

{'A': '~(p)', 'B': '(q→(~(p)∧(y∨z)))', 'Aim': ['p', '(p→q)']}

Only problem, for some reason I cant use ¬ instead of ~ with pyswip library (Version: 0.2.11), cause with ¬ I get the curios error:

U+ffffffac is not in range [U+0000; U+10ffff]

whereby ¬ has unicode U+00AC ..

Martin Kunze
  • 995
  • 6
  • 16