Intelligent Architectures for Electronic Commerce A (Brief) Prolog Tutorial
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos 2 Overview 1.Introduction 2.Syntax (the shape of Prolog programs) 3.Semantics (the meaning of Prolog programs) 4.Pragmatics (how to write Prolog programs) 5.Practicalities 6.Reading List
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos 3 1. Introduction Prolog: –PROgramming (in) LOGic –logic + control = program –results/behaviour obtained as a proof This tutorial: –quick look over essential issues –not exhaustive, not authoritative –wont be enough to write very clever agents in Prolog, but hopefully will help! –is idiosyncratic…
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos 4 2. Syntax Prolog programs are made up of –facts –clauses And these are made up of –atoms –variables –terms
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Atoms Atoms are the most essential components. Numbers are atoms. Strings starting with small letter are atoms. Strings within single quotes are atoms. Examples: freddie 123 car sam umbrella 123
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Variables Variables are: –any string starting with capital letter, or –any string starting with _ (underscore) Variables are untyped. Variables are defined at run-time (no need to declare). Examples A Var1 Freddie Xs
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Terms Structure with atoms and variables. General format: atomNotNumber ( AtomOrVar, …, AtomOrVar ) Examples: p(23,rover) owns(sam,X,23) Terms can be nested: odd(p(23,rover),owns(sam,X,23)) Terms are used to build data structures.
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Facts Facts are terms asserted as true. Examples owns(freddie,car). owns(sam,X). Period. indicates end of fact. No intrinsic meaning.
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Clauses General format: Term :- Term,…, Term. Examples rich(X):- owns(X,house),owns(X,car). siblings(X,Y):- mother(X,Z),mother(Y,Z). Period. indicates end of clause. Clauses are also known as rules. Terms of clauses also called goals or literals. head body
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Editing Programs Create a file with extension.pl Type in your program (facts and rules). Do not forget the. !! Save your file. Load your file in a Prolog session. Always make sure you save and re-load files.
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Semantics Meaning of program defined by its inferences Scenario: –given a program, try to prove if goal P is true P –goal P is also called a query –queries may also be a conjunction of goals: P 1, P 2, …, P n Essential concepts: –unification –resolution
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Unification Process of making two or more terms equal. Variables are assigned values (computation!) Examples: –rich(X) unifies with rich(tom) if X = tom –rich(tom) doesnt unify with rich(abe) Not always possible! Unification problem: find values for variables to make terms equal
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Unification (Contd) Unification also obtains a substitution –set of pairs Variable /Term = { A / a, B / f(1) } –apply it by replacing Variable by Term: p(A,B) = p(a,f(1)) unify(TermLeft,TermRight, ) iff TermLeft = TermRight
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Unification (Contd) More examples: –unify( p(a,q(X,X),t),p(Y,q(a,a),B), ) ? –unify( q(b,c(d)), X, ) ? –unify( s(f(1,2),X),s(f(X,C),1), ) ?
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Resolution process to prove if P resolution(,P, ) iff P : 1.resolution(,P, ) if Q and unify(P,Q, ) 2.resolution(,P, ) if (Q :- R 1,…, R n ) and unify(P,Q, ) and resolution(, (R 1,…, R n ), ) 3.resolution(, (R 1,…, R n ), ) if resolution(,R 1, 1 ) and resolution(,(R 2,…, R n ) 1, )
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Resolution (Contd) Prolog: clauses/facts are chosen top-down. Prolog tries different clauses and facts. example: mother(john,barbara). father(john,philip). mother(mary,barbara). father(mary,philip). parent(X,Y):- mother(X,Y). parent(X,Y):- father(X,Y). siblings(X,Y):- parent(X,P),parent(Y,P). ?- siblings(john,mary).
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Pragmatics recursive data structures lists: dynamic allocation sequence of elements within [ and ] examples: [1,2,rtu] [hello,(p(X):-q(X)),[4]] generic way to break list: head and tail –[1,2,rtu] = [Head|Tail] Head = 1 Tail = [2,rtu]
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Pragmatics (Contd) example: creation of list create([X|Xs]):- read(X), \+ X = stop, create(Xs). create([]).
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Pragmatics (Contd) generic agent loop(MntlSt):- final_condition(MntlSt). loop(MntlSt):- senseEnvironment(Percepts), react(Percepts,MntlSt,NewMntlSt), loop(MntlSt).
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Practicalities CIAO Prolog: free, efficient. Emacs: integrated editor. Dowload and install them. Loop: edit-load-run (be careful!) Debugging is possible and useful CIAO Prolog talks to JAVA (and hence JADE)
Intelligent Architectures for Electronic Commerce Timothy J Norman and Wamberto Vasconcelos Reading List Bratko, I (2001). Prolog Programming for Artificial Intelligence, 3 rd Edition, Addison- Wesley. Stirling, L & Shapiro, E (1997). The Art of Prolog, 2 nd Edition, MIT Press.