480 likes | 563 Views
Computer Engineering Lab II. 242-203 , Semester 2, 20 13 -20 14. Please ask questions. Introduction to Prolog. Who I am: Andrew Davison WiG Lab Office ad@fivedots.coe.psu.ac.th. Outline. 7 . The append/3 Predicate 8 . Other List Predicates 9 . The not/1 Predicate
E N D
Computer Engineering Lab II 242-203, Semester 2, 2013-2014 Please ask questions Introduction to Prolog Who I am: Andrew DavisonWiG Lab Officead@fivedots.coe.psu.ac.th 242-203 Comp. Eng. II: Intro. to Prolog
Outline • 7. The append/3 Predicate • 8. Other List Predicates • 9. The not/1 Predicate • 10. Using Strawberry Prolog • 11. More Information • 1. Language Elements • 2. Executing Prolog • 3. Rules • 4. Recursive Rules • 5. Operators • 6. Lists 241-203 Software Lab II: Intro. to Prolog
0. Why Prolog? • Very popular in Artificial Intelligence (AI) • Unique features include: • unification (more powerful assignment) • backtracking • close links to predicate logic • Very different from C, Java • an engineer must know more than 1 tool • Prolog is a logic programming language 241-203 Comp Eng Lab II: Intro. to Prolog
C Program Compared to Prolog • void foo(...){ ... }int bar(...){ ... }void main(){ ... } • Prolog predicate-- made up of facts and rulesProlog predicate -- more facts and rules?- Prolog query. Data types: term, list Lots of data types: int, char, struct, pointer, ... 241-203 Comp Eng Lab II: Intro. to Prolog
1. Language Elements • Terms • Facts and Rules • they use terms as data structures • Predicates • made up of facts and rules • The (Logic) Program • made up of predicates 241-203 Software Lab II: Intro. to Prolog
Terms Terms are data. • Constants adam paris 5 3.14 [] ´Adam’ ... • Variables X Y List _12 _ ... • Compound terms (something like structs) plus(2,3) foo(2,bar(a,'Jim')) ... 2+3 // infix notation 241-203 Software Lab II: Intro. to Prolog
Facts • A fact has the form p(t1,...,tn). • p is the name of the fact • t1, …, tn are term arguments of the fact • Examples: edge(a, X).parent(adam, bill). note the '.'s 241-203 Software Lab II: Intro. to Prolog
Example parent(kim,holly).parent(margaret,kim).parent(margaret,kent).parent(esther,margaret).parent(herbert,margaret).parent(herbert,jean). • Six facts about families: • kim is the parent of holly • Defines a predicateparent of arity 2 • parent/2 for short note the '.'s 241-203 Software Lab II: Intro. to Prolog
Facts are not Terms • A fact : parent(kim, holly).is part of the program. • A term: foo(1, 4)is a piece of data used inside a fact (or rule). 241-203 Software Lab II: Intro. to Prolog
Facts using Term Data • A staff/2 predicate: staff( name(andrew), room(101) ).staff( name(mondri), room(403) ).staff( name(chatchai), room(301) ). • Each staff/2 fact uses two terms. 241-203 Software Lab II: Intro. to Prolog
2. Executing Prolog more details in section 10
Use a query to execute Prolog code. • In SB Prolog: • execute query, print results with write()s • use F5 (or Run|Run) to execute the query • the output appears in the Output window. 241-203 Software Lab II: Intro. to Prolog
Simple Queries • The queries: ?- parent(margaret, kent). Yes // printed in the Output window ?- parent(fred, pebbles). No 241-203 Software Lab II: Intro. to Prolog
Queries With Variables • Query: ?- parent(P, jean), write(P), write("\n") herbertYes ?- parent(P, esther), write(P), write("\n") No • In SB Prolog, the variable bindings must be printed by the query. 241-203 Software Lab II: Intro. to Prolog
Flexibility • Variables can appear anywhere in a query: ?- parent(Parent,jean). ?- parent(esther,Child). ?- parent(Parent,Child). ?- parent(Person,Person). 241-203 Software Lab II: Intro. to Prolog
Conjunctions note the ',' ?- parent(margaret,X), parent(X,holly), write(X), write("\n"). kim Yes • A conjunction is a series of queries. • The Prolog system tries to prove them all by making variable bindings • e.g. bind X to kim 241-203 Software Lab II: Intro. to Prolog
Multiple Solutions the user types 'F8' to make SB Prolog look for another answer
3. Rules head greatgrandparent(GGP,GGC) :- parent(GGP,GP), parent(GP,P), parent(P,GGC). • To 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). conditions 241-203 Software Lab II: Intro. to Prolog
A Program With a Rule 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). • parent/2 and greatgrandparent/2 predicates. 241-203 Software Lab II: Intro. to Prolog
parent/2 as a Graph herbert esther parent jean margaret kim kent holly 241-203 Software Lab II: Intro. to Prolog
Example • This shows the initial query and final result. • There are intermediate goals inside the system: ?- greatgrandparent(esther,GreatGrandchild), write(GreatGrandchild), write("\n"). hollyYes 241-203 Software Lab II: Intro. to Prolog continued
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). greatgrandparent(esther,GreatGrandchild) Clause 7, binding GGP to esther and GGC to GreatGrandChild parent(esther,GP), parent(GP,P), parent(P,GreatGrandchild) Clause 4, binding GP to margaret parent(margaret,P), parent(P,GreatGrandchild) Clause 2, binding P to kim parent(kim,GreatGrandchild) Clause 1, binding GreatGrandchild to holly 241-203 Software Lab II: Intro. to Prolog
Rules Using Other Rules • Note that both rules use a variable P. • The 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). 241-203 Software Lab II: Intro. to Prolog
4. Recursive Rules ancestor(X,Y) :- parent(X,Y).ancestor(X,Y) :- parent(Z,Y), ancestor(X,Z). • 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 • Prolog tries rules in the order you give them, so put base-case rules and facts first. 241-203 Software Lab II: Intro. to Prolog
parent/2 again herbert esther parent(kim,holly).parent(margaret,kim).parent(margaret,kent).parent(esther,margaret).parent(herbert,margaret).parent(herbert,jean). jean margaret kim kent holly 241-203 Software Lab II: Intro. to Prolog
ancestor/2 Queries ?- ancestor(kim,holly).Yes ?- ancestor(A,holly), write(A), write("\n").kimmargaret // I typed F8esther // typed F8herbert // typed F8No // typed F8 241-203 Software Lab II: Intro. to Prolog
5. Operators • Prolog systems have many built-in operators. e.g. =/2 is/2 and arithmetic 241-203 Software Lab II: Intro. to Prolog
The =/2 Predicate • The goal =(X,Y) succeeds if Xand Y can be unified • "unify" means create variable bindings • Usually written as X = Y ?- name(adam,seth) = name(adam,X), write(X), write("\n").seth Yes 241-203 Software Lab II: Intro. to Prolog
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. 241-203 Software Lab II: Intro. to Prolog
Evaluating Arithmetic • The general format is: Variable is operation_term ?- X is 1+2*3, write(X), write("\n").7 yes 241-203 Software Lab II: Intro. to Prolog
List notation Meaning [] an empty list [1] list with 1 value [1,2,3] 3 value list [1,name(X,Y)] 2 value list 6. Lists 241-203 Software Lab II: Intro. to Prolog
Examples ?- X = [1, 2, 3].X = [1, 2, 3] ?- [X, Y, Z] = [1, 2, 3].X = 1Y = 2Z = 3 241-203 Software Lab II: Intro. to Prolog
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] 241-203 Software Lab II: Intro. to Prolog
7. 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([], B, B).append([Head|TailA], B, [Head|TailC]) :- append(TailA, B, TailC). ?- append([1,2],[3,4],Z), write(Z), write("\n").[1, 2, 3, 4] Yes 241-203 Software Lab II: Intro. to Prolog
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 241-203 Software Lab II: Intro. to Prolog
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 241-203 Software Lab II: Intro. to Prolog
In SB Prolog: writeBind(Str, Val) :- write(Str), write(" = "), write(Val), write("\n"). ?- append(X, Y,[1,2,3]), writeBind("X", X), writeBind("Y", Y). 241-203 Software Lab II: Intro. to Prolog
Predicate Description member(X,Y) Succeeds if X is an element in the list Y. length(X,Y) Succeeds if the list X is length Y. 8. Other List Predicates • Flexible, like append/3 • queries can contain variables anywhere. 241-203 Software Lab II: Intro. to Prolog
member/2 member(X, [X|_]). member(X, [Y|Rest]) :- member(X, Rest). • member(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").ji // F8m // F8no // F8 241-203 Software Lab II: Intro. to Prolog
length/2 length([], 0). length([X|Rest], Len) :- length(Rest, LenRest), Len is LenRest + 1. • length(X,Y) succeeds if Y is the length of the list X. ?- length([a,b,c,d], L), write(L), write("\n").4?- length([1,2,3], 4).no 241-203 Software Lab II: Intro. to Prolog
9. The not/1 Predicate ?- not( member(4,[1,2,3]) ).Yes ?- not( member(1,[1,2,3]) ).No • Built-in not(X) succeeds when X fails. • Only use not/1 when its goal contains no variables. (Use not/1 only as a test.) 241-203 Software Lab II: Intro. to Prolog
10. Using Strawberry Prolog • Download the installation program for the SB Prolog system from: http://fivedots.coe.psu.ac.th/ Software.coe/LAB/Prolog/ • The filename: Strawberry Prolog 2.92 for Vista/XP • Read the readme.txt file in the same directory. It's for Win 7 too. 241-203 Software Lab II: Intro. to Prolog
Start SB Prolog from C:\Program Files\Strawberry Prolog\Prolog.exe:
Create a Prolog Program using any text editor saved in c:/windows/desktop/parents.pro 241-203 Software Lab II: Intro. to Prolog
Alternatively, you can just type into a new Prolog file window, then save at the end. • in SB Prolog select File|New, and choose "Prolog File" • at the end, select File|Save 241-203 Software Lab II: Intro. to Prolog
Running parents.pro • Load the file using the menu item File|Open. • Type in a query into the parents.pro window: • ?- parent(X, Y), write(X), write(" and "), write(Y). • Select Run|Run from the menu • Check the Output window 241-203 Software Lab II: Intro. to Prolog
11. More Information • The Help menu in the SB Prolog system leads to: • a tutorial • a Prolog language guide • examples 241-203 Software Lab II: Intro. to Prolog continued
SB Prolog’s Web Site: • http://www.dobrev.com/light.html • v.2.92, and other versions • A complete Prolog book (in PDF format): • http://www.ida.liu.se/~ulfni/lpp/ • Online Prolog tutorials list at • http://www.thefreecountry.com/documentation/ onlineprolog.shtml 241-203 Software Lab II: Intro. to Prolog