Presentation is loading. Please wait.

Presentation is loading. Please wait.

240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

Similar presentations


Presentation on theme: "240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research."— Presentation transcript:

1 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research Lab (Rm 101) ad@fivedots.coe.psu.ac.th Introduction to Prolog Please ask questions

2 240-202 Comp Eng Lab II: Intro. to Prolog 2 Outline z1. Language Elements z2. Executing Prolog z3. Rules z4. A Database in Prolog z5. Recursive Rules z6. Operators z7. Lists continued

3 240-202 Comp Eng Lab II: Intro. to Prolog 3 z8. The append/3 Predicate z9. Other List Predicates z10. Insertion Sort z11. The not/1 Predicate z12. Using Strawberry Prolog z13. More Information

4 240-202 Comp Eng Lab II: Intro. to Prolog 4 Acknowledgements zThe material for this presentation came from three main sources: A First Look at Prolog Adam Webber http://www.webber-labs.com/mpl/lectures/19.ppt yProlog in 90 minutes Ulf Nilsson, Linköping University yme!

5 240-202 Comp Eng Lab II: Intro. to Prolog 5 1. Language Elements zTerms zFacts and Rules ythey use terms as data structures zPredicates ymade up of facts and rules zThe (Logic) Program ymade up of predicates

6 240-202 Comp Eng Lab II: Intro. to Prolog 6 Terms Terms are data. zConstants yadam paris 5 3.14 [] ´Adam... zVariables yX Y List _12 _... zCompound terms (something like structs) yplus(2,3) foo(2,bar(a,'Jim'))... y2+3// infix notation

7 240-202 Comp Eng Lab II: Intro. to Prolog 7 Facts zA fact has the form p(t 1,...,t n ). yp is the name of the fact yt 1, …, t n are term arguments of the fact zExamples: edge(a, X). parent(adam, bill). note the '.'s

8 240-202 Comp Eng Lab II: Intro. to Prolog 8 Example zSix facts about families: ykim is the parent of holly Defines a predicate parent of arity 2 yparent/2 for short parent(kim,holly). parent(margaret,kim). parent(margaret,kent). parent(esther,margaret). parent(herbert,margaret). parent(herbert,jean). note the '.'s

9 240-202 Comp Eng Lab II: Intro. to Prolog 9 Facts are not Terms zA fact :parent(kim, holly). is part of the program. zA term:foo(1, 4) is a piece of data used inside a fact (or rule).

10 240-202 Comp Eng Lab II: Intro. to Prolog 10 Facts using Term Data zA staff/2 predicate: ystaff( name(andrew), room(101) ). staff( name(mondri), room(403) ). staff( name(chatchai), room(301) ). zEach staff/2 fact uses two terms.

11 240-202 Comp Eng Lab II: Intro. to Prolog 11 Logic Programs zA logic program is a set of predicates: ya predicate is made of facts and rules zThe program is used to answer user queries. zProlog is a logic programming language.

12 240-202 Comp Eng Lab II: Intro. to Prolog 12 2. Executing Prolog more details in section 12

13 240-202 Comp Eng Lab II: Intro. to Prolog 13 Use a query to execute Prolog code. zIn SB Prolog: yexecute query, print results with write()s yuse F5 (or Run|Run) to execute the query ythe output appears in the Output window.

14 240-202 Comp Eng Lab II: Intro. to Prolog 14 Simple Queries zThe queries: y?- parent(margaret, kent). yYes // printed in the Output window y?- parent(fred, pebbles). yNo

15 240-202 Comp Eng Lab II: Intro. to Prolog 15 Queries With Variables zQuery: y?- parent(P, jean), write(P), write("\n") yherbert Yes y?- parent(P, esther), write(P), write("\n") yNo zIn SB Prolog, the variable bindings must be printed by the query.

16 240-202 Comp Eng Lab II: Intro. to Prolog 16 Flexibility zVariables can appear anywhere in a query: ?- parent(Parent,jean). y?- parent(esther,Child). ?- parent(Parent,Child). y?- parent(Person,Person).

17 240-202 Comp Eng Lab II: Intro. to Prolog 17 Conjunctions zA conjunction is a series of queries. zThe Prolog system tries to prove them all by making variable bindings ye.g. bind X to kim ?- parent(margaret,X), parent(X,holly), write(X), write("\n"). kim Yes note the ','

18 240-202 Comp Eng Lab II: Intro. to Prolog 18 Multiple Solutions the user types 'F8' to make SB Prolog look for another answer

19 240-202 Comp Eng Lab II: Intro. to Prolog 19 3. Rules zTo prove the head, prove the conditions. To prove greatgrandparent(GGP,GGC), find some GP and P for which you can prove parent(GGP,GP), then parent(GP,P), and finally parent(P,GGC). greatgrandparent(GGP,GGC) :- parent(GGP,GP), parent(GP,P), parent(P,GGC). conditions head

20 240-202 Comp Eng Lab II: Intro. to Prolog 20 A Program With a Rule zparent/2 and greatgrandparent/2 predicates. parent(kim,holly). parent(margaret,kim). parent(margaret,kent). parent(esther,margaret). parent(herbert,margaret). parent(herbert,jean). greatgrandparent(GGP,GGC) :- parent(GGP,GP), parent(GP,P), parent(P,GGC).

21 240-202 Comp Eng Lab II: Intro. to Prolog 21 parent/2 as a Graph esther herbert margaret kimkent holly parent jean

22 240-202 Comp Eng Lab II: Intro. to Prolog 22 Example zThis shows the initial query and final result. zThere are intermediate goals inside the system: ?- greatgrandparen(esther,GreatGrandchild), write(GreatGrandchild), write("\n"). holly Yes continued

23 240-202 Comp Eng Lab II: Intro. to Prolog 23 greatgrandparent(esther,GreatGrandchild) 1. parent(kim,holly). 2. parent(margaret,kim). 3. parent(margaret,kent). 4. parent(esther,margaret). 5. parent(herbert,margaret). 6. parent(herbert,jean). 7. greatgrandparent(GGP,GGC) :- parent(GGP,GP), parent(GP,P), parent(P,GGC). parent(esther,GP), parent(GP,P), parent(P,GreatGrandchild) parent(margaret,P), parent(P,GreatGrandchild) parent(kim,GreatGrandchild) Clause 7, binding GGP to esther and GGC to GreatGrandChild Clause 4, binding GP to margaret Clause 2, binding P to kim Clause 1, binding GreatGrandchild to holly

24 240-202 Comp Eng Lab II: Intro. to Prolog 24 Rules Using Other Rules Note that both rules use a variable P. zThe scope of the definition of a variable is the fact/rule that contains it. greatgrandparent(GGP,GGC) :- grandparent(GGP,P), parent(P,GGC). grandparent(GP,GC) :- parent(GP,P), parent(P,GC).

25 240-202 Comp Eng Lab II: Intro. to Prolog 25 4. A Database in Prolog lecturer(Lecturer,Course) :- course(Course,_,Lecturer,_). duration(Course,Length) :- course(Course,time(_,S,F),_,_), Length is F-S. teaches(Lect,Day) :- course(_, time(Day,_,_), Lect, _). occupied(Room,Day,Time) :- course(_,time(Day,S,F),_,Room), S =< Time, Time =< F. % Database course(logic, time(monday, 8, 10), dave, a12). course(java, time(tuesday, 9, 11), ad, r204). :

26 240-202 Comp Eng Lab II: Intro. to Prolog 26 Some Queries z?- lecturer(L, logic). ywho teaches logic? z?- duration(C, 2). ywhat course is 2 hours long? z?- occupied(r204, wednesday, Time). ywhen is r204 occupied on Wednesday? continued

27 240-202 Comp Eng Lab II: Intro. to Prolog 27 zSB Prolog Queries: y?- lecturer(L, logic), write(L), write("\n"). dave yes y?- duration(C,2), write(C), write("\n"). logic java // I typed F8 no // I typed F8 y?- occupied(r204, wednesday, Time). no

28 240-202 Comp Eng Lab II: Intro. to Prolog 28 5. Recursive Rules X is an ancestor of Y if: Base case: X is a parent of Y Recursive case: there is some Z such that Z is a parent of Y, and X is an ancestor of Z zProlog tries rules in the order you give them, so put base-case rules and facts first. ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(Z,Y), ancestor(X,Z).

29 240-202 Comp Eng Lab II: Intro. to Prolog 29 parent/2 again parent(kim,holly). parent(margaret,kim). parent(margaret,kent). parent(esther,margaret). parent(herbert,margaret). parent(herbert,jean). esther herbert margaret kimkent holly jean

30 240-202 Comp Eng Lab II: Intro. to Prolog 30 ?- ancestor(kim,holly). Yes ?- ancestor(A,holly), write(A), write("\n"). kim margaret // I typed F8 esther// typed F8 herbert// typed F8 No// typed F8 ancestor/2 Queries

31 240-202 Comp Eng Lab II: Intro. to Prolog 31 Path Searching path(Node,Node). path(Node1,Node3) :- edge(Node1,Node2), path(Node2,Node3). edge(a,b). edge(a,c). edge(b,d). edge(c,d). edge(d,e). edge(f,g). a b c d e f g

32 240-202 Comp Eng Lab II: Intro. to Prolog 32 Some Queries z?- path(a, e). yis there a path from a to e? z?- path(a, Y). ywhat node can be reached from a? z?- path(X, d). ywhat node has a path to d? continued

33 240-202 Comp Eng Lab II: Intro. to Prolog 33 Note the multiple answers, by using F8 zSB Prolog queries: y? - path(a, e). yes y?- path(a,Y), write(Y), write("\n"). a b d e c d e no y?- path(X,d), write(X), write("\n"). d a a b c no

34 240-202 Comp Eng Lab II: Intro. to Prolog 34 6. Operators zProlog systems have many built-in operators. e.g. y=/2 yis/2 and arithmetic

35 240-202 Comp Eng Lab II: Intro. to Prolog 35 The =/2 Predicate The goal =(X,Y) succeeds if X and Y can be unified y"unify" means create variable bindings zUsually written as X = Y ?- name(adam,seth) = name(adam,X), write(X), write("\n"). seth Yes

36 240-202 Comp Eng Lab II: Intro. to Prolog 36 Arithmetic Operators Terms +, -, * and / are operators, with the usual precedence and associativity ?- X = +(1,*(2,3)). X = 1+2*3 ?- X = 1+2*3. X = 1+2*3 No evaluation yet.

37 240-202 Comp Eng Lab II: Intro. to Prolog 37 Evaluating Arithmetic zThe general format is: Variable is operation_term ?- X is 1+2*3, write(X), write("\n"). 7 yes

38 240-202 Comp Eng Lab II: Intro. to Prolog 38 7. Lists The atom [] represents the empty list. List notationActual term [] [1].(1,[]) [1,2,3].(1,.(2,.(3,[]))) [1,name(X,Y)].(1,.(name(X,Y),[]))

39 240-202 Comp Eng Lab II: Intro. to Prolog 39 Examples ?- X =.(1,.(2,.(3,[]))). X = [1, 2, 3] ?- [X, Y, Z] = [1, 2, 3]. X = 1 Y = 2 Z = 3

40 240-202 Comp Eng Lab II: Intro. to Prolog 40 List Notation With Tail [1,2|X] unifies with a list that starts with 1,2 and binds X to the tail. ?- [1,2|X] = [1,2,3,4,5]. X = [3, 4, 5]

41 240-202 Comp Eng Lab II: Intro. to Prolog 41 8. The append/3 Predicate append(X,Y,Z) succeeds when Z is the same as the list Y appended onto the end of the list X. ?- append([1,2],[3,4],Z), write(Z), write("\n"). [1, 2, 3, 4] Yes append([], B, B). append([Head|TailA], B, [Head|TailC]) :- append(TailA, B, TailC).

42 240-202 Comp Eng Lab II: Intro. to Prolog 42 Other Uses of append/3 append/3 can be called with variables in any of its argument positions. ?- append(X,[3,4],[1,2,3,4]), write(X), write("\n"). [1, 2] Yes

43 240-202 Comp Eng Lab II: Intro. to Prolog 43 Multiple Answers ?- append(X,Y,[1,2,3]). X = [] Y = [1, 2, 3] X = [1] Y = [2, 3] X = [1, 2] Y = [3] X = [1, 2, 3] Y = [] No By using F8

44 240-202 Comp Eng Lab II: Intro. to Prolog 44 zIn SB Prolog: ywriteBind(Str, Val) :- write(Str), write(" = "), write(Val), write("\n"). y?- append(X, Y,[1,2,3]), writeBind("X", X), writeBind("Y", Y).

45 240-202 Comp Eng Lab II: Intro. to Prolog 45 9. Other List Predicates Flexible, like append/3 yqueries can contain variables anywhere. PredicateDescription member(X,Y) Succeeds if X is an element in the list Y. length(X,Y) Succeeds if the list X is length Y.

46 240-202 Comp Eng Lab II: Intro. to Prolog 46 member/2 member(X, [X|_]). member(X, [Y|Rest]) :- member(X, Rest). zmember(X,L) succeeds if X is an element in the list L ?- member(1,[1,2,3]). yes ?- member(a,[a,n,d,y]). yes ?- member(X,[j,i,m]), write(X), write("\n"). j i // F8 m // F8 no // F8

47 240-202 Comp Eng Lab II: Intro. to Prolog 47 length/2 length(X,Y) succeeds if Y is the length of the list X. length([], 0). length([X|Rest], Len) :- length(Rest, LenRest), Len is LenRest + 1. ?- length([a,b,c,d], L), write(L), write("\n"). 4 ?- length([1,2,3], 4). no

48 240-202 Comp Eng Lab II: Intro. to Prolog 48 10. Insertion Sort % isort(A,B): B is a sorted version of A isort([X|Xs],Ys) :- isort(Xs,Zs), insert(X,Zs,Ys). isort([ ],[ ]). % insert(A,B,C) % if B is a sorted list, then C is sorted % and contains all elements in B plus A insert(X,[ ],[X]). insert(X,[Y|Ys],[Y|Zs]) :- X > Y, insert(X,Ys,Zs). insert(X,[Y|Ys],[X,Y|Ys]) :- X =< Y. ?- isort([4, 3, 1, 5], S) write(S), write("\n"). S = [1, 3, 4, 5] yes

49 240-202 Comp Eng Lab II: Intro. to Prolog 49 11. The not/1 Predicate Built-in not(X) succeeds when X fails. zOnly use not/1 when its goal contains no variables. (Use not/1 only as a test.) ?- not( member(4,[1,2,3]) ). Yes ?- not( member(1,[1,2,3]) ). No

50 240-202 Comp Eng Lab II: Intro. to Prolog 50 Example sibling(X,Y) :- not(X=Y), parent(P,X), parent(P,Y). ?- sibling(kim,kent). Yes ?- sibling(kim,kim). No

51 240-202 Comp Eng Lab II: Intro. to Prolog 51 12. Using Strawberry Prolog zDownload the installation program for the SB Prolog system from: http://fivedots.coe.psu.ac.th/ Software.coe/LAB/Prolog/ zThe filename: StrawberryProlog2_5_for_98.exe Read the readme.txt file in the same directory.

52 240-202 Comp Eng Lab II: Intro. to Prolog 52 zStart SB Prolog from C:\Program Files\Strawberry Prolog\Prolog.exe :

53 240-202 Comp Eng Lab II: Intro. to Prolog 53 Create a Prolog Program saved in c:/windows/desktop/parents.pro using any text editor

54 240-202 Comp Eng Lab II: Intro. to Prolog 54 zAlternatively, you can just type into a new Prolog file window, then save at the end. yin SB Prolog select File|New, and choose "Prolog File" yat the end, select File|Save

55 240-202 Comp Eng Lab II: Intro. to Prolog 55 Running parents.pro zLoad the file using the menu item File|Open. zType in a query into the parents.pro window: ?- parent(X, Y), write(X), write(" and "), write(Y). zSelect Run|Run from the menu zCheck the Output window

56 240-202 Comp Eng Lab II: Intro. to Prolog 56 13. More Information zThe Help menu in the SB Prolog system leads to: ya tutorial ya Prolog language guide yexamples continued

57 240-202 Comp Eng Lab II: Intro. to Prolog 57 zSB Prologs Web Site: yhttp://www.dobrev.com/light.html yother versions zA complete Prolog book (in PDF format): yhttp://www.ida.liu.se/~ulfni/lpp/ continued

58 240-202 Comp Eng Lab II: Intro. to Prolog 58 zOnline Prolog tutorials: yhttp://cs.wwc.edu/~cs_dept/KU/PR/ Prolog.html http://cbl.leeds.ac.uk/~tamsin/ prologtutorial/ xlinks to another online Prolog book http://www.compapp.dcu.ie/~jhayes/Logic/ yhttp://www.csupomona.edu/~jrfisher/www/ prolog_tutorial/pt_framer.html


Download ppt "240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research."

Similar presentations


Ads by Google