 ## Slide #1.

Prolog · Primarily for Symbolic (nonnumeric) Computation · Suited to Problems Involving Objects and Relationships · A Step Toward Declarative Programming.
More slides like this ## Slide #2.

Example: Family Tree pam tom bob liz ann pat parent(pam, parent(tom, parent(tom, parent(bob, parent(bob, parent(pat, Facts: Relations that always hold.  jim bob). bob). liz). ann). pat). jim).
More slides like this ## Slide #3.

Yes/No Queries · ?- parent(bob, pat). · yes · ?- parent(liz, pat). · no · ?-
More slides like this ## Slide #4.

Queries with Variables · · · · ?- parent(X, bob). X=pam; X=tom; no · · · · ?- parent(X, Y). X=pam Y=bob yes
More slides like this ## Slide #5.

Compound Queries · ?- parent(Y, pat), parent(X,Y). · X=pam · Y=bob; · X=tom · Y=bob · yes
More slides like this ## Slide #6.

Exercises · · · · 1. Who is Pat's parent? 2. Does Liz have a child? 3. Who is Pat’s grandparent? 4. Find a great-grandparent.
More slides like this ## Slide #7.

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. 
More slides like this ## Slide #9.

Exs: Sibling, Sister, Grandparent sibling(X,Y) :parent(P,X), parent(P,Y), X \= Y.
More slides like this ## Slide #10.

Exs: Sibling, Sister, Grandparent sister(X,Y) :sibling(X,Y), female(X).
More slides like this ## Slide #11.

Exs: Sibling, Sister, Grandparent grandparent(X,Y) :parent(P,Y), parent(X,P).
More slides like this ## Slide #12.

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.
More slides like this ## Slide #13.

How Prolog Works p1(foo). p1(X) :p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz). ?- p1(X).
More slides like this ## Slide #14.

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;
More slides like this ## Slide #15.

How Prolog Works 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; REDO
More slides like this ## Slide #16.

How Prolog Works 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)
More slides like this ## Slide #17.

How Prolog Works 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=foo, Y=bleen
More slides like this ## Slide #18.

How Prolog Works 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=foo, Y=bleen goal: p3(bleen)
More slides like this ## Slide #19.

How Prolog Works 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=foo, Y=bleen goal: p3(bleen) FAIL
More slides like this ## Slide #20.

How Prolog Works 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=foo, Y=bleen REDO
More slides like this ## Slide #21.

How Prolog Works 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
More slides like this ## Slide #22.

How Prolog Works 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)
More slides like this ## Slide #23.

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 ;
More slides like this ## Slide #24.

How Prolog Works 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) REDO
More slides like this ## Slide #25.

How Prolog Works 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) FAIL
More slides like this ## Slide #26.

How Prolog Works 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 REDO
More slides like this ## Slide #27.

How Prolog Works 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) FAIL
More slides like this ## Slide #28.

How Prolog Works p1(foo). p1(X) :p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz). goal: p1(X) REDO
More slides like this ## Slide #29.

How Prolog Works p1(foo). p1(X) :p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz). goal: p1(X) return: X=snork ;
More slides like this ## Slide #30.

How Prolog Works p1(foo). p1(X) :p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz). goal: p1(X) return: X=snork REDO
More slides like this ## Slide #31.

How Prolog Works p1(foo). p1(X) :p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz). goal: p1(X) FAIL
More slides like this ## Slide #32.

How Prolog Works p1(foo). p1(X) :p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz). goal: p1(X) return: no
More slides like this ## Slide #33.

How Prolog Works p1(foo). p1(X) :p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz). ?- p1(X). X=foo; X=bar; X=snork; no ?-
More slides like this ## Slide #34.

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
More slides like this ## Slide #35.

Ex: Temperature Conversion Conversion “Function” % convert.pl convert(Cel, Fahr) :Fahr is 9/5*Cel+32.
More slides like this ## Slide #36.

Interactive Conversion ?- [convert]. (Loads the code) % convert compiled ?- convert(100, X). X = 212 yes ?-
More slides like this ## Slide #37.

Adding Input/Output run :write('Enter a Celsius temp: '), read(C), convert(C, F), write('The temp is '), write(F), write(' degrees Fahrenheit.').
More slides like this ## Slide #38.

Running the Enhanced Version ?- run. Enter a Celsius temp: 100. The temp is 212 degrees Fahrenheit. Yes ?-
More slides like this ## Slide #39.

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.
More slides like this ## Slide #40.

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).
More slides like this ## Slide #41.

Version with Decision ?- run. Enter a Celsius temp: 100. The temp is 212 degrees Fahrenheit. It's really hot out! Yes ?-
More slides like this ## Slide #42.

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(_,'').
More slides like this ## Slide #43.

Adding a Loop Sentinel Loop Get an Input While input is valid number Convert to Fahrenheit Output temp and warnings Get next input As a Decision Get an Input 1if input is a number: 1 Do the conversion Get another input Do the loop again if input is 'quit' just do it.
More slides like this ## Slide #44.

Using Recusion to Loop run2 :getInput(C), convert_loop(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.
More slides like this ## Slide #45.

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).
More slides like this