180 likes | 328 Views
Implementing Prolog with Coroutines. Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010. HW3 post mortem. Q2 Part 2: “What do coroutines buy us?”
E N D
Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010
HW3 post mortem Q2 Part 2: “What do coroutines buy us?” Problem: Implement function concat2 that concatenates its arguments but avoids creating an explicit list. Example use: for i in concat2(range(3,100000), lst) { print i if (i>5) { null } }
concat2 with closure-based iterators defconcatBasedOnIterators(l1,l2) { # if the argument is a list, get its iterator if (type(l1) == "obj") { l1 = _getIterator_(l1) } if (type(l2) == "obj") { l2 = _getIterator_(l2) } def e1 = l1() lambda() { if (e1 != null) { def old_e1 = e1 e1 = l1(); old_e1 } else { l2() } } }
concat2 with coroutine iterators defconcatCoroutines(l1,l2) { wrap(lambda(_) { for e in l1 { yield(e) } for e in l2 { yield(e) } null }) }
Prolog Basics Program: eat(thibaud, vegetables). eat(thibaud, fruits). eat(lion, thibaud). Queries: eat(thibaud, lion)? eat(thibaud, X)?
Structure of Programs works(ras). Fact (Axiom) works(thibaud) :- works(ras). Rule works(X)? Query In a rule: a(X, Y) :- b(X,Z), c(Z,Y) Clause Constant Variable Free Variable Body Head
Execution Function process: Goal -> Solution Initial goal: works(X) 1. works(X) | works(ras) compatible? X=ras 2. works(X) | works(thibaud) compatible? X=thibaudcreates subgoal: works(ras) 2.1 works(ras) | works(ras) compatible? {} works(ras). works(thibaud) :- works(ras). works(X)? <-- query: our goal
Solutions Function process: Goal -> Solution Operations: unify: Are two terms compatible? If yes, give a unifier a(X, Y) | a(1, 2) --> {X -> 1, Y -> 2} subst: Apply Substitution on clauses subst[a(X, Y), {X -> ras, Y -> Z}] --> a(ras, Z) Clause Ex: eat(X, thibaud) List of substitutions Ex: {X -> ras, Y -> Z}
Unification What does it means to be compatible? a(1,Y) | a(X,2) a(X) | b(X) a(1,Y) | a(2,X) a(1, Y) | a(1, X) The result of unify(term1, term2) is Most General Unifier (mgu)
More on Unification Got it? What about these two… a(X,Y) | a(b(Y),c(Z)) a([1|X]) | a(X) Robinson 1965
Interpreter Structure (1) process(goal) { for rule in program { mgu= unify(rule.head, goal) if (mgu) { if (rule has no body) print mgu else process(body) } } } works(ras). works(thibaud) :- works(ras). works(X)? this interpreter works for rules with one clause in their bodies Note that we are not handling unification and substitution yet. We’ll this at the very end. For now, let’s focus on control transfer.
So far we are handling disjunction for rule in alternative rules head(A,B) :- …. head(X,Y) :- a(X), b(X,Z), c(Y, Z, D) head(X,Y) :- ….
Conjunctions parent(john, ed). parent(bob, john). gp(X,Y) :- parent(X,Z), parent(Z, Y). gp(W, ed)? Goal: gp(W, ed) gp(W, ed) | gp(X, Y) {Y = ed, X = W} gp(W, ed) :- parent(W, Z), parent(Z, ed). subgoal 1: parent(W, Z) parent(W, Z) | parent(john, ed) {W = john, Z=ed} gp(john, ed) :- parent(john, ed), parent(ed, ed). subgoal 2: parent(ed, ed) Fail! No Unifier
Attempt at interpreter with conjunction process(goal) { for (rule in program) { mgu = unify(rule.head, goal) if (mgu) { if (rule has no body) return mgu else conjunction(rule.body, 0) } } } conjunction(clauses, depth) { goal = clauses[depth] mgu = process(body) if (mgu) { if (depth reached the last clause) return mgu else return conjunction(clauses, depth+1) } else { no solution! must backtrack to previous clause and ask for its next solution (how?) } }
Recursion in conjunction() needs to backtrack and try alternative goals for each clause for rule in alternative rules head(A,B)…. head(X,Y) :- a(X), b(X,Z), c(Y, Z, D) head(X,Y)…. Recursive function conjunction()
Give each clause in a conjunction a coroutine to enumerate its solutions process(goal) { for (rule in program) { mgu = unify(rule.head, goal) if (mgu) { if (rule has no body) yield(mgu) else conjunction(rule.body, 0) } } } conjunction(clauses, mgus, depth) { goal = clauses[depth] solutions = coroutine.wrap(process, body) for (mgu in solutions) { if (depth reached the last clause) yield(mgu) else conjunction(clauses, depth+1) } }
The complete view of control transfer for rule in alternative rules head(A,B)…. head(X,Y) :- a(X), b(X,Z), c(Y, Z, D) head(X,Y)…. recursive function concjunction() coroutine process() coroutine process() coroutine process()
Finally, we add substitution of variables in subgoals and mergingof unifiers from subgoals into the final solution process(goal) { for (rule in program) { mgu = unify(rule.head, goal) if (mgu) { if (rule has no body) yield(mgu) else conjunction(subst(rule.body,mgu), {}, 0) } } } conjunction(clauses, mgus, depth) { goal = clauses[depth] solutions = coroutine.wrap(process, body) for (mgu in solutions) { if (depth reached the last clause) yield(merge(mgu,mgus)) else conjunction(subst(clauses, mgu), merge(mgu,mgus), depth+1) } }