Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prolog Primarily for Symbolic (nonnumeric) Computation

Similar presentations


Presentation on theme: "Prolog Primarily for Symbolic (nonnumeric) Computation"— Presentation transcript:

1 Prolog Primarily for Symbolic (nonnumeric) Computation
Suited to Problems Involving Objects and Relationships A Step Toward Declarative Programming.

2 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.

3 Yes/No Queries ?- parent(bob, pat). yes ?- parent(liz, pat). no ?-

4 Queries with Variables
?- parent(X, bob). X=pam; X=tom; no ?- parent(X, Y). X=pam Y=bob yes

5 Compound Queries ?- parent(Y, pat), parent(X,Y). X=pam Y=bob; X=tom
yes

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.

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.

8 Exs: Sibling, Sister, Grandparent

9 Exs: Sibling, Sister, Grandparent
sibling(X,Y) :- parent(P,X), parent(P,Y), X \= Y.

10 Exs: Sibling, Sister, Grandparent
sister(X,Y) :- sibling(X,Y), female(X).

11 Exs: Sibling, Sister, Grandparent
grandparent(X,Y) :- parent(P,Y), parent(X,P).

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.

13 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).

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;

15 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

16 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).

17 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).

18 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).

19 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).

20 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).

21 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).

22 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).

23 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).

24 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).

25 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).

26 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).

27 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).

28 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).

29 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).

30 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).

31 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).

32 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).

33 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).

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

35 Ex: Temperature Conversion
Conversion “Function” % convert.pl convert(Cel, Fahr) :- Fahr is 9/5*Cel+32.

36 Interactive Conversion
?- [convert]. (Loads the code) % convert compiled ?- convert(100, X). X = 212 yes ?-

37 Adding Input/Output run :- write('Enter a Celsius temp: '), read(C),
convert(C, F), write('The temp is '), write(F), write(' degrees Fahrenheit.').

38 Running the Enhanced Version
Enter a Celsius temp: 100. The temp is 212 degrees Fahrenheit. Yes ?-

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.

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).

41 Version with Decision ?- run. Enter a Celsius temp: 100.
The temp is 212 degrees Fahrenheit. It's really hot out! Yes ?-

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(_,'').

43 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

44 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.

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).

46 Structured Data


Download ppt "Prolog Primarily for Symbolic (nonnumeric) Computation"

Similar presentations


Ads by Google