def test_ex_3_1(self): """ Exercise 3.1 Give M_2 code for L_2 facts q(a, b) and r(b, c) and L_2 query ?- p(U, V), then trace the code shown in Figure 3.1 and verify that the solution produced is U = a, V = c. """ fact1, fact2, query, scope = parse(PROLOG_OPS, 'q(a, b)', 'r(b, c)', 'p(U, V)') compiler = Compiler() fact1_instrs = compiler.compile_rule(fact1, []) fact2_instrs = compiler.compile_rule(fact2, []) reg_allocation = RegisterAllocation() query_instrs = compiler.compile_query_m1(query, reg_allocation) wam = WAM() wam.load(('p', 2), self.fig_3_1_instrs) wam.load(('q', 2), fact1_instrs) wam.load(('r', 2), fact2_instrs) query_offset = wam.load(None, query_instrs) wam.p = query_offset wam.run() aU = wam.deref_reg(reg_allocation[scope.var('U')]) self.assertEqual(wam.get_term_repr(aU), 'a') aV = wam.deref_reg(reg_allocation[scope.var('V')]) self.assertEqual(wam.get_term_repr(aV), 'c')
def test_fig_4_4(self): rules_and_scopes = [parse(PROLOG_OPS, x) for x in ('p(X, a)', 'p(b, X)', 'p(X, Y) :- p(X, a), p(b, Y)')] compiler = Compiler() instrs = compiler.compile_predicate([x[0] for x in rules_and_scopes]) self.assertEqual(self.fig_4_4_instrs, instrs)
def test_fig_3_1(self): rule, scope = parse(PROLOG_OPS, 'p(X, Y) :- q(X, Z), r(Z, Y)') head, body = rule.subterms subgoals = find_subgoals(body) compiler = Compiler() instrs = compiler.compile_rule(head, subgoals) self.assertEqual(self.fig_3_1_instrs, instrs)
def main(): query_str = ' '.join(sys.argv[1:]) if query_str == '': print 'Please enter a query on the command line (you might need quotes around it)' print 'E.g. qolog.py assert(prime(3)), assert(prime(5)), assert(prime(7)), findall(S, (prime(P), prime(Q), S is P*Q), B)' return db = Database() db.add_rules(LIST_RULES) db.add_rules(ARITHMETIC_RULES) goal, scope = parse(db.operators, query_str) print 'Proving:', unparse(db.operators, goal, scope) t0 = time.time() prove_interactively(goal, scope, db) print 'Ran for %0.2f seconds' % (time.time() - t0)
def test_ex_4_1(self): """ Exercise 4.1 Trace the execution of L_3 query ?- p(c, d) with code in Figure 4.4, giving all the successive states of the stack, the heap, and the trail. """ compiler = Compiler() query, query_scope = parse(PROLOG_OPS, 'p(c, d)') query_reg_allocation = RegisterAllocation() query_instrs = compiler.compile_query_m2(query, query_reg_allocation) wam = WAM() wam.load(('p', 2), self.fig_4_4_instrs) wam.p = wam.load(None, query_instrs) wam.push_frame(EnvironmentFrame(None, None, size=len(query_reg_allocation.permanent_allocation))) wam.run()
def check_and_compile_rule(self, rule): if type(rule) is str: rule, _ = parse(self.operators, rule) else: rule = rule.copy_to_new_scope(Scope()) if not rule.is_functor(':-', 2): rule = Compound(':-', rule, Atom('true')) subgoals = [] else: subgoals = find_subgoals(rule.subterms[1]) head = rule.subterms[0] functor = head.get_functor() if functor is None: raise Exception('Cannot make a rule with head %s' % (head, )) compiled_rule = compile_rule(rule) compiled_rule.subgoals = subgoals return functor, head, compiled_rule
def prove_str(goal_str, database): goal, scope = parse(database.operators, goal_str) for bindings in prove(goal, database): print bound_vars_str(database, bindings, scope)
def unify_strs(database, str1, str2): t1, t2, scope = parse(database.operators, str1, str2) unifications = unify(t1, t2) if unifications is None: return None return sorted(scope.var_mappings(unifications).keys())