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}. @return: C{bool} """ parsed = logic.Parser().parse(expr) variable = logic.Variable(var) return variable in parsed.free()
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 = logic.Parser(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, 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