Skip to content

ModaLogic Functional/Technical Design Documentation

Expression Rewriter Module

It all starts with the ExpressionRewriter. This component will take a possibly in ascii writtern argument and converts it into a consistent format based on on UTF8 "symbols". This is to support the parser by providing a consistent foundation to lex and parse.

The ExpressionRewriter handles the conversion of the expressions of a modal logic argument and converts them into a consistent format that the parser can handle. The parser expects all operators to be single symbols --not ascii representations that use possilbe multiple characters for a single operator.

For instance a user might want to enter the iff operator with an ascii input string which would look like '<-->' but the parser want a single logic symbol like: '↔'. The ExpressionRewriter will handle such conversions.

ExpressionRewriter

toSymbols(str) classmethod

This function takes an ascii string representing the expression and replaces the ascii-symbols into UTF symbols.

1
str = re.sub(r"^\((.*)\)$", r"\g<1>", str)
Source code in modalogic\textio\ascii_io.py
@classmethod
def toSymbols(cls, str):
    """ This function takes an ascii string representing the expression
    and replaces the ascii-symbols into UTF symbols.

        str = re.sub(r"^\((.*)\)$", r"\g<1>", str)
    """
    str = str.replace('&', '∧')
    str = str.replace('^', '∧')
    str = str.replace('<->', '↔')
    str = str.replace('->', '→')
    str = str.replace('~', '¬')
    str = str.replace(' v ', ' ∨ ') # 'v' letter => or symbol
    str = str.replace('[]', '□')
    str = str.replace('<>', '◇')

    str = re.sub(r"\(A([s-z])\)", r"∀\g<1>", str)  # (Ax) => ∀x
    str = re.sub(r"\(E([s-z])\)", r"∃\g<1>", str)  # (Ex) => ∃x
    str = re.sub(r"\forall[\{ ]?\}?/g", '∀', str)

    str =re.sub(r"\\[Bb]ox[\{]?[\} ]?", r"□", str)
    str = re.sub(r"\\[Dd]iamond[\{ ]?\}?", '◇', str)
    str = re.sub(r"\\exists[\{ ]?\}?", '∃', str)
    str = re.sub(r"(\\neg|\\lnot)[\{ ]?\}?", '¬', str) 
    str = re.sub(r"(\\vee|\\lor)[\{ ]?\}?", '∨', str)
    str = re.sub(r"(\\wedge|\\land)[\{ ]?\}?", '∧', str)
    str = re.sub(r"(\\to|\\rightarrow)[\{ ]?\}?", '→', str)
    str = re.sub(r"\\leftrightarrow[\{ ]?\}?", '↔', str)

    str = re.sub(r"(?:^|\W)\(([s-z])\)", r"∀\g<1>", str)

    # str =re.sub(r"(?:^|\W)\(([s-z])\)", '∀$1', str) # (x) => ∀x, but not f(x) => f∀x
    return str

Last update: 2021-07-02