def helper(f, result): if is_predicate(f) and len(f.args) == 0: result.add(f) else: for x in f.args: helper(x, result) return result
def evaluate_prop(f, assignment): """Evaluate a propositional formula with respect to the given assignment. # The assignment should be a dict with 2 keys: 'True' and 'False'. The values # for those keys should be a collection of strings corresponding to # the operator names - predicates with 0 arity (acting as propositions). The assignment maps propositions (0-ary predicates) to True or False. """ if f.op == OP_TRUE: return True elif f.op == OP_FALSE: return False elif f.op == OP_NOT: return not evaluate_prop(f.args[0], assignment) elif f.op == OP_AND: return all(evaluate_prop(x, assignment) == True for x in f.args) elif f.op == OP_OR: return any(evaluate_prop(x, assignment) == True for x in f.args) elif f.op == OP_IMPLIES: args = [evaluate_prop(x, assignment) for x in f.args] return not (args[0] == True and args[1] == False) elif f.op == OP_IMPLIED_BY: args = [evaluate_prop(x, assignment) for x in f.args] return not (args[0] == False and args[1] == True) elif f.op == OP_EQUIVALENT: args = [evaluate_prop(x, assignment) for x in f.args] return (args[0] == args[1]) elif is_predicate(f) and len(f.args) == 0: try: return assignment[f] except KeyError: raise EvaluationError('Predicate "{0}" is not in the assignment.'. format(str(f))) else: msg = ('The Quine-McCluskey algorithm is defined only for ' 'propositional logic. The operator "{0}" is not defined ' 'in propositional logic.'.format(f)) raise EvaluationError(msg)
def formula_to_rst(f): """ Convert a FOL formula to an RST tree. """ get_log().debug(str(f)) if f.op == OP_AND: msgs = [formula_to_rst(x) for x in f.args] m = Message("Conjunction", None, *msgs) # test = lambda x: (x.op == '&' or x.op == '|') # if any(map(test, f.args)): # m.marker = ', and' # else: m.marker = "and" return m if f.op == OP_OR: msgs = [formula_to_rst(x) for x in f.args] m = Message("Disjunction", None, *msgs) # test = lambda x: (x.op != 'forall' and # x.op != 'exists') # if any(map(test, f.args)): # m.marker = ', or' # else: m.marker = "or" return m if f.op == OP_IMPLIES: msgs = [formula_to_rst(x) for x in f.args] m = Message("Imply", msgs[0], msgs[1]) return m if f.op == OP_IMPLIED_BY: msgs = [formula_to_rst(x) for x in f.args] m = Message("Imply", msgs[1], msgs[0]) return m if f.op == OP_EQUIVALENT: msgs = [formula_to_rst(x) for x in f.args] m = Message("Equivalent", msgs[0], msgs[1]) return m if f.op == OP_EQUALS: msgs = [formula_to_rst(x) for x in f.args] m = Message("Equality", msgs[0], *msgs[1:]) return m if f.op == OP_NOTEQUALS: msgs = [formula_to_rst(x) for x in f.args] m = Message("Inequality", msgs[0], *msgs[1:]) return m if f.op == OP_FORALL: vars = [formula_to_rst(x) for x in f.vars] msgs = [formula_to_rst(x) for x in f.args] m = Message("Quantifier", vars, *msgs) m.marker = "for all" return m if f.op == OP_EXISTS: vars = [formula_to_rst(x) for x in f.vars] msgs = [formula_to_rst(x) for x in f.args] m = Message("Quantifier", vars, *msgs) if len(f.vars) == 1: m.marker = "there exists" else: m.marker = "there exist" return m if f.op == OP_NOT and is_predicate(f.args[0]): get_log().debug("negated predicate: " + str(f)) arg = f.args[0] m = PredicateMsg(arg, *[formula_to_rst(x) for x in arg.args]) m._features = {"NEGATED": "true"} return m if f.op == OP_NOT and is_variable(f.args[0]): get_log().debug("negated variable: " + str(f)) arg = f.args[0] m = NounPhrase(PlaceHolder(arg.op), Word("not", "DETERMINER")) return m if f.op == OP_NOT: get_log().debug("negated formula: " + str(f)) msgs = [formula_to_rst(x) for x in f.args] m = Message("Negation", msgs[0], *msgs[1:]) m.marker = "it is not the case that" return m if is_predicate(f): get_log().debug("predicate: " + str(f)) return PredicateMsg(f, *[formula_to_rst(x) for x in f.args]) if is_function(f): get_log().debug("function: " + str(f)) return PlaceHolder(f.op, PredicateMsg(f, *[formula_to_rst(x) for x in f.args])) else: get_log().debug("None: " + repr(f)) return PredicateMsg(f)