Ejemplo n.º 1
0
    def freevar(self, var, expr):
        """
        Is C{var} one of the free variables in C{expr}?

        @type var: an C{Indvar} of L{logic}
        @param var: the variable to test for.
        @param expr: an C{Expression} of L{logic}.
        @rtype: C{bool}
        """
        parsed = LogicParser().parse(expr)
        variable = Variable(var)
        return variable in parsed.free()
Ejemplo n.º 2
0
    def freevar(self, var, expr):
        """
        Is C{var} one of the free variables in C{expr}?

        @type var: an C{Indvar} of L{logic}
        @param var: the variable to test for.
        @param expr: an C{Expression} of L{logic}.
        @rtype: C{bool}
        """
        parsed = LogicParser().parse(expr)
        variable = Variable(var)
        return variable in parsed.free()
Ejemplo n.º 3
0
def parse_fol(s):
    """
    Convert a  file of First Order Formulas into a list of {Expression}s.
    
    @parameter s: the contents of the file
    @type s: C{str}
    @return: a list of parsed formulas.
    @rtype: C{list} of L{Expression}
    """
    statements = []
    lp = LogicParser()
    for linenum, line in enumerate(s.splitlines()):
        line = line.strip()
        if line.startswith('#') or line=='': continue
        try:
            statements.append(lp.parse(line))
        except Error:
            raise ValueError, 'Unable to parse line %s: %s' % (linenum, line)
    return statements
Ejemplo n.º 4
0
    def decompose(self, expr):
        """
        Function to communicate with a first-order functional language.

        This function tries to make weak assumptions about the parse structure
        provided by the logic module. It makes the assumption that an expression
        can be broken down into a pair of subexpressions:

          - The C{(binder, body)} pair is for decomposing quantified formulae.
          - The C{(op, args)} pair is for decomposing formulae with a boolean operator.
          - The C{(fun, args)} pair should catch other relevant cases.

        @param expr: A string representation of a first-order formula.
        """

        try:
            parsed = LogicParser(constants=self.valuation.symbols).parse(expr)
        except TypeError:
            print "Cannot parse %s" % expr

        try:
            first, second = parsed.binder, parsed.body
            #print 'first is %s, second is %s' % (first, second)
            return (first, second)
        except AttributeError:
            pass
        try:
            first, second = parsed.op, map(str, parsed.args)
            #print 'first is %s, second is %s' % (first, second)
            return (first, second)
        except AttributeError:
            pass
        try:
            first, second = str(parsed.first), str(parsed.second)
            #print 'first is %s, second is %s' % (first, second)
            return (first, second)
        except (AttributeError, TypeError):
            return expr
Ejemplo n.º 5
0
 def __init__(self):
     LogicParser.__init__(self)
     
     self.operator_precedence = {APP: 1, Tokens.IMP: 2, None: 3}
     self.right_associated_operations += [Tokens.IMP]
Ejemplo n.º 6
0
 def __init__(self):
     LogicParser.__init__(self)
Ejemplo n.º 7
0
def process(line):
    tokens = LogicParser.fromstring(line)
    LogicParser.parse(tokens)
Ejemplo n.º 8
0
    def __init__(self):
        LogicParser.__init__(self)

        self.order_of_operations = {'APP': 1, Tokens.IMP: 2, None: 3}
        self.right_associated_operations += [Tokens.IMP]
Ejemplo n.º 9
0
 def __init__(self):
     LogicParser.__init__(self)