1
Implementing Prolog with Coroutines
Ras Bodik, Thibaud Hottelier, James IdeUC 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
def concatBasedOnIterators(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
def concatCoroutines(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). Ruleworks(X)? Query
In a rule:a(X, Y) :- b(X,Z), c(Z,Y)
VariableConstant
Head Body
Free Variable
Clause
Execution
Function process: Goal -> Solution
Initial goal: works(X)1. works(X) | works(ras) compatible? X=ras
2. works(X) | works(thibaud) compatible? X=thibaud creates 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 clausessubst[a(X, Y), {X -> ras, Y -> Z}] --> a(ras, Z)
ClauseEx: 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
head(A,B) :- ….
head(X,Y) :- a(X), b(X,Z), c(Y, Z, D)
head(X,Y) :- ….
for rule in alternative rules
Conjunctions
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
parent(john, ed).parent(bob, john).gp(X,Y) :- parent(X,Z), parent(Z, Y).gp(W, ed)?
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
head(A,B)….
head(X,Y) :- a(X), b(X,Z), c(Y, Z, D)
head(X,Y)….
for rule in alternative rulesRecursive 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
head(A,B)….
head(X,Y) :- a(X), b(X,Z), c(Y, Z, D)
head(X,Y)….
for rule in alternative rulesrecursive function concjunction()
coroutine process()coroutine process()
coroutine process()
Finally, we add substitution of variables in subgoals and merging of unifiers from subgoals into the final solutionprocess(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)
} }