1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164:...
-
Upload
gabriel-ford -
Category
Documents
-
view
217 -
download
0
Transcript of 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164:...
![Page 1: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/1.jpg)
1
Implementing Prolog with Coroutines
Ras Bodik, Thibaud Hottelier, James IdeUC Berkeley
CS164: Introduction to Programming Languages and Compilers Fall 2010
![Page 2: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/2.jpg)
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 }}
![Page 3: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/3.jpg)
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()} } }
![Page 4: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/4.jpg)
concat2 with coroutine iterators
def concatCoroutines(l1,l2) { wrap(lambda(_) { for e in l1 { yield(e) } for e in l2 { yield(e) } null })}
![Page 5: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/5.jpg)
Prolog Basics
Program:eat(thibaud, vegetables).eat(thibaud, fruits).eat(lion, thibaud).
Queries:eat(thibaud, lion)?eat(thibaud, X)?
![Page 6: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/6.jpg)
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
![Page 7: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/7.jpg)
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
![Page 8: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/8.jpg)
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}
![Page 9: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/9.jpg)
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)
![Page 10: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/10.jpg)
More on Unification
Got it? What about these two…
a(X,Y) | a(b(Y),c(Z))
a([1|X]) | a(X)
Robinson 1965
![Page 11: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/11.jpg)
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.
![Page 12: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/12.jpg)
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
![Page 13: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/13.jpg)
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)?
![Page 14: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/14.jpg)
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?)} }
![Page 15: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/15.jpg)
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()
![Page 16: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/16.jpg)
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)
} }
![Page 17: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/17.jpg)
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()
![Page 18: 1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf851a28abf838c87494/html5/thumbnails/18.jpg)
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)
} }