CS 60, Spring 2016
Prolog!
Week 14, Class 25
Thursday will be the last CS 60 lecture.
Senior final: Thursday, May 5 at 1:15pmEveryone else: Wednesday, May 11 at 2pm
There will be no Prolog code on the final.
Models of Computation
So far we've seen two languages withtwo different models of what "computing" means
Racket ("functional")Computing means evaluating expressions to get the answer.
A program is a (big) expression.
Java ("imperative")Computing means changing values in memory.
A program is a sequence of statements, each having some effect.
Functional Programming in Java
Java is "naturally" imperative.That does not mean we can't think functionally.
int factorial(int n) { if (n == 0) return 1; return n * factorial(n-1); }
Functional Programming in Java 8
Java 8 (a.k.a. Java 1.8) might not be installed on your computer yet.
List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4);
numbers.stream() .filter(i -> i % 2 == 0) .distinct() .forEach(System.out::println);
numbers.parallelStream() .filter(i -> i % 2 == 0) .reduce(0, (a, b) -> a + b)
Lambdas in Java
Higher-order functions Functions as data
Another Model of Computation
Prolog: The Beautiful DreamWe type in a bunch of facts relevant to our problem.
pairs(apple, walnut). pairs(apple, honey). pairs(walnut, avocado). pairs(walnut, banana). pairs(apple, banana). pairs(banana, ginger). pairs(banana, cloves). pairs(banana, strawberry). pairs(banana, coriander).
pairs(strawberry, honey). pairs(strawberry, ginger). pairs(strawberry, tea). pairs(tea, walnut). pairs(tea, tomato). pairs(tea, milk).
pairs(X, X). pairs(X, coconut).
We explain how to recognize an answer.yummy_triple(X,Y,Z) :- pairs(X,Y), X \= Y, pairs(Y,Z), Y \= Z, pairs(X,Z), X \= Z.
Prolog automatically finds solutions for us!So get it now! http://www.swi-prolog.org
http://www.cs.hmc.edu/courses/2016/spring/cs60/code/facts.pl
Applications of Prolog
Applications of Prolog
Prolog Data is like Racket Data
Integers 42 60Floating Point 3.14 2.17Symbols tea milkStrings 'hi'Lists [ 42, 3.14, tea, 'hi', [ ] ]
Racket would say 'tea and 'milk
But Prolog has a different model of computation,so programs feel completely different!
Prolog: Just the facts
%% Here is a comment in Prolog /* this is also a comment */
Chocolate_is_tasty. %% A fact. A good one too!
good(spam). %% a debatable fact, but now prolog thinks so good(42). %% no one can deny this
better(tofu, spam). %% tofu is better than spam better(chocolate, tofu). %% chocolate is better than tofu better(money, chocolate). %% money is better than chocolate
%% Does prolog know that money is better than spam? %% Does prolog even know what spam is???
Hypothetical Family Tree
uggette!ug!
homericus! matilda!
marge!gomer!
maggie!bart! lisa!
jackie!john!
selma! patty! glum!homer!
millhouse! terpsichore!
helga!olf!
cher!
skug!skugerina!
gemini! esmerelda!
klotho! lachesis! atropos!
97 99 93 92
65 76 101 78
55 54
20 18 19
35 41 38
1 8 10
38 38 27 44
8 8
59 62
Oh, brother!
Excerpts from simpsons.pl%% Parent relation parent(homer, bart). parent(marge, bart). parent(homer, lisa). parent(marge, lisa). parent(homer, maggie). parent(marge, maggie).
%% Age relation age(marge, 35). age(homer, 38). age(lisa, 8). age(maggie, 1). age(bart, 10). age(gomer, 41).
%% Female predicate female(marge). female(jackie). female(selma). female(patty). female(cher). female(lisa).
%% Male predicate male(homer). male(gomer). male(gemini). male(glum). male(bart). male(millhouse).
%% Three rules about families
child(X, Y) :- parent(Y, X).
mother(X, Y) :- female(X), parent(X, Y).
anc(X, Y) :- parent(X, Y). anc(X, Y) :- parent(Z, Y), anc(X, Z).
Writing Queries in Prolog
Is Lisa a child of Marge?
Who has Marge as a parent?
Is Ug a parent of anyone?
Who are the ancestors of Lisa?
Who are the descendants of Uggette?
Who is a (known) person?
Who is Bart's age or younger?
child(lisa, marge).
parent(marge, X).
parent(ug, _).
anc(A, lisa).
anc(uggette, D).
male(X); female(X)
age(bart, N), age(P, M), M < N.
Defining Relationships in Prolog
sib(X,Y) — true when X and Y are siblings (or half-siblings)
sib(X,Y) :- parent(P,X), parent(P,Y), X \= Y.
rel(X,Y) — true when X and Y are related ("by blood")
rel(X,Y) :- anc(A,X), anc(A,Y).
Q
Unification: Prolog's main tool
Unification means to take two expressions and make them the same, by giving values to unknown variables.
Fact: pairs(X, coconut).
Query: pairs(apple, coconut).
Answer: yes, because we can make these match by setting X to apple.
Prolog's Secret Ingredient: DFS!
parent(P,N)
sibling(A,P)
female(A)
parent(homer,bart)
NO (fails)
other siblings?NO
other parents?
female(glum) ?NO SUCCESS
female(selma)
YES!
is there a parent?
YES N = bart
A = gomer
sibling(gomer,homer)is there a sibling?
YES N = bart
parent(marge,bart)YES N = bart
P = homer
P = homer
backtrack!
backtrack!
P = marge
A = glum
sibling(glum, marge)is there a sibling?
YES N = bart P = marge
female(gomer) ?
other siblings?sibling(selma, marge)
A = selma
YES N = bart P = marge
backtrack!
aunt(A,N) :- parent(P,N), sibling(A,P), female(A).
N = bart
Who are Bart’s aunts? aunt(A,bart)
What%space%is%Prolog%searching%through?%
Models of Computation
Racket ("functional programing")Computing means evaluating expressions to get the answer.
A program is a (big) expression.
Java ("imperative programming")Computing means changing values in memory.
A program is a sequence of statements, each having some effect.
Computing means search (logical inference).A program is a set of facts and rules, plus an initial question.
Prolog ("logic programming")
Prolog: The RealityWe type in a bunch of facts relevant to our problem.
pairs(apple, walnut). pairs(apple, honey). pairs(walnut, avocado). pairs(walnut, banana). %% etc.
pairs(X, X). pairs(X, coconut).
pairs(X,Y) :- pairs(Y,X).
We explain how to recognize an answer.yummy_triple(X,Y,Z) :- pairs(X,Y), X \= Y, pairs(Y,Z), Y \= Z, pairs(X,Z), X \= Z.
Prolog automatically finds solutions for us?
http://www.cs.hmc.edu/courses/2016/spring/cs60/code/facts1.pl
Prolog: The Reality (2)We type in a bunch of facts relevant to our problem.
pairs(X, X). pairs(X, coconut).
pairs(X,Y) :- pairs(Y,X).
pairs(apple, walnut). pairs(apple, honey). pairs(walnut, avocado). pairs(walnut, banana). %% etc.
We explain how to recognize an answer.yummy_triple(X,Y,Z) :- pairs(X,Y), X \= Y, pairs(Y,Z), Y \= Z, pairs(X,Z), X \= Z.
Prolog automatically finds solutions for us?
http://www.cs.hmc.edu/courses/2016/spring/cs60/code/facts2.pl
An aside on "Declarative" Languages
A language is said to be "declarative" if youspecify what you want, rather than how it's computed.
Some people say that Prolog is declarative.mother(X, Y) :- female(X), parent(X, Y).
But to use Prolog effectively, you have toworry about "how" the search will proceed.
But you can still do some very cool stuff!
More Fun with Prolog
append( [], L, L). append( [F|R], L, [F|M]) :- append(R,L,M).
append(A,B,C) — true if list C is the result of appending A and B.
permutation(A,B) — true if list A is a permutation of list B.
reverse(A,B) — true if list A is the reverse of list B.
Sorting in Prolog?
increasing( [] ). increasing( [X,Y | Z] ) :- X < Y, increasing([Y|Z]).
sort(L,M) :- permutation(L,M), increasing(M).
sort(L,M) — true if M is the sorted version of list L.