Prolog Primarily for Symbolic (nonnumeric) Computation Suited to Problems Involving Objects and Relationships A Step Toward Declarative Programming.
Example: Family Tree always hold. parent(pam, bob). parent(tom, bob). ann jim liz pat parent(pam, bob). parent(tom, bob). parent(tom, liz). parent(bob, ann). parent(bob, pat). parent(pat, jim). Facts: Relations that always hold.
Yes/No Queries ?- parent(bob, pat). yes ?- parent(liz, pat). no ?-
Queries with Variables ?- parent(X, bob). X=pam; X=tom; no ?- parent(X, Y). X=pam Y=bob yes
Compound Queries ?- parent(Y, pat), parent(X,Y). X=pam Y=bob; X=tom yes
Exercises 1. Who is Pat's parent? 2. Does Liz have a child? 3. Who is Pat’s grandparent? 4. Find a great-grandparent.
Adding Inference Rules For all X and Y Y is an offspring of X if X is a parent of Y offspring(Y,X):- parent(X,Y). Rules: Conclusion is True if Conditions Satisfied. All Rules Have Two Parts: Conclusion, Consequent or Head (Left-hand side). Antecedents, Condtions or Body (Right-hand side). A Fact is a Rule with no Conditions.
Exs: Sibling, Sister, Grandparent
Exs: Sibling, Sister, Grandparent sibling(X,Y) :- parent(P,X), parent(P,Y), X \= Y.
Exs: Sibling, Sister, Grandparent sister(X,Y) :- sibling(X,Y), female(X).
Exs: Sibling, Sister, Grandparent grandparent(X,Y) :- parent(P,Y), parent(X,P).
Summary So Far Prolog Program is a Set of Clauses. Prolog Clauses: Facts, Rules, Queries. Facts declare unconditional truths. Rules declare truths based on conditions. Queries are used to ask Prolog what is true. Prolog Clauses have a head and body.
How Prolog Works p1(foo). ?- p1(X). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz). ?- p1(X).
p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz). goal: p1(X) return: X=foo;
How Prolog Works p1(foo). goal: p1(X). p1(X) :- return: X=foo; p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz). goal: p1(X). return: X=foo; REDO
How Prolog Works p1(foo). p1(X) :- goal: p1(X) goal: p2(X,Y) p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works p1(foo). goal: p1(X) p1(X) :- goal: p2(X,Y) p2(X,Y), X=foo, Y=bleen p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works p1(foo). goal: p1(X) p1(X) :- goal: p2(X,Y) p2(X,Y), X=foo, Y=bleen goal: p3(bleen) p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works p1(foo). goal: p1(X) p1(X) :- goal: p2(X,Y) p2(X,Y), X=foo, Y=bleen goal: p3(bleen) FAIL p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works p1(foo). goal: p1(X) p1(X) :- goal: p2(X,Y) p2(X,Y), X=foo, Y=bleen REDO p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works p1(foo). goal: p1(X) p1(X) :- goal: p2(X,Y) p2(X,Y), X=bar, Y=baz p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works p1(foo). goal: p1(X) p1(X) :- goal: p2(X,Y) p2(X,Y), X=bar, Y=baz goal: p3(baz) p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
goal: p1(X) goal: p2(X,Y) X=bar, Y=baz goal: p3(baz) yes return: X=bar ; p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works goal: p1(X) p1(foo). goal: p2(X,Y) p1(X) :- X=bar, Y=baz goal: p3(baz) REDO p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works goal: p1(X) p1(foo). goal: p2(X,Y) p1(X) :- X=bar, Y=baz goal: p3(baz) FAIL p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works goal: p1(X) p1(foo). goal: p2(X,Y) p1(X) :- X=bar, Y=baz REDO p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works goal: p1(X) p1(foo). goal: p2(X,Y) p1(X) :- FAIL p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works goal: p1(X) p1(foo). REDO p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works goal: p1(X) p1(foo). return: X=snork ; p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works goal: p1(X) p1(foo). return: X=snork p1(X) :- REDO p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works goal: p1(X) p1(foo). FAIL p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works goal: p1(X) p1(foo). return: no p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
How Prolog Works ?- p1(X). p1(foo). X=foo; p1(X) :- p2(X,Y), X=bar; X=snork; no ?- p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).
Is That All There is to It? Yes. But there is more to know: Built-in predicates Designing Algorithms (programming) Predicates are sub-programs Decisions (ifs) are Predicates with multiple clauses Loops via recursion or Repeat/Fail Variables don't vary. Data Structures: Complex terms
Ex: Temperature Conversion Conversion “Function” % convert.pl convert(Cel, Fahr) :- Fahr is 9/5*Cel+32.
Interactive Conversion ?- [convert]. (Loads the code) % convert compiled ?- convert(100, X). X = 212 yes ?-
Adding Input/Output run :- write('Enter a Celsius temp: '), read(C), convert(C, F), write('The temp is '), write(F), write(' degrees Fahrenheit.').
Running the Enhanced Version Enter a Celsius temp: 100. The temp is 212 degrees Fahrenheit. Yes ?-
Adding a Decision If temp is greater than 90: print a heat warning if temp is less than 30: print a cold warning warn(T, 'It''s really hot') :- T > 90. warn(T,'Brass monkey danger') :- T < 30. warn(T, '') :- T >= 30, T <= 90.
Enhanced main program run :- write('Enter a Celsius temp: '), read(C), convert(C, F), write('The temp is '), write(F), write(' degrees Fahrenheit.'), nl, warn(F, Warning), write(Warning).
Version with Decision ?- run. Enter a Celsius temp: 100. The temp is 212 degrees Fahrenheit. It's really hot out! Yes ?-
Retricting Backtracking warn(T, 'It''s really hot') :- T > 90. warn(T,'Brass monkey danger') :- T < 30. warn(T, '') :- T >= 30, T <= 90. ! (called "cut") causes Prolog to commit to choices. warn(T, 'It''s really hot) :- T > 90, !. warn(T, 'Brass monkey danger') :- T < 30, !. warn(_,'').
Adding a Loop Sentinel Loop As a Decision Get an Input While input is valid number Convert to Fahrenheit Output temp and warnings Get next input Get an Input if input is a number: Do the conversion Get another input Do the loop again if input is 'quit' just do it. 11
Using Recusion to Loop run2 :- getInput(C), convert_loop(C). number(C), convert(C,F), ... getInput(C1), convert_loop(C1). convert_loop(quit). If input is a number: Do the conversion Get another input Do the loop again if input is 'quit' just do it.
Repeat-Fail Loop run3 :- repeat, write('Enter a Celsius Temp: '), read(C), process(C), C = quit. process(C) :- number(C), convert(C,F), write('The temp is '), write(F), warn(F, W), write(W), nl. process(quit).
Structured Data