intelligent architectures for electronic commerce a (brief) prolog tutorial
TRANSCRIPT
Intelligent Architecture
s for Electronic Commerce
A (Brief) Prolog Tutorial
Intelligent Architectures for Electronic CommerceTimothy J Norman and Wamberto Vasconcelos
2
Overview
1. Introduction2. Syntax (the shape of Prolog programs)3. Semantics (the meaning of Prolog
programs)4. Pragmatics (how to write Prolog
programs)5. Practicalities6. Reading List
Intelligent Architectures for Electronic CommerceTimothy 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– won’t be enough to write very clever agents
in Prolog, but hopefully will help!– is idiosyncratic…
Intelligent Architectures for Electronic CommerceTimothy 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 CommerceTimothy J Norman and Wamberto Vasconcelos
5
2.1 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 CommerceTimothy J Norman and Wamberto Vasconcelos
6
2.2 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 CommerceTimothy J Norman and Wamberto Vasconcelos
7
2.3 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 CommerceTimothy J Norman and Wamberto Vasconcelos
8
2.4 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 CommerceTimothy J Norman and Wamberto Vasconcelos
9
2.5 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 CommerceTimothy J Norman and Wamberto Vasconcelos
10
2.5 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 CommerceTimothy J Norman and Wamberto Vasconcelos
11
3. 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:
⊢ P1 , P2 , …, Pn
• Essential concepts:– unification– resolution
Intelligent Architectures for Electronic CommerceTimothy J Norman and Wamberto Vasconcelos
12
3.1 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)doesn’t unify with rich(abe)
• Not always possible!• Unification problem:find values for variables to make terms equal
Intelligent Architectures for Electronic CommerceTimothy J Norman and Wamberto Vasconcelos
13
3.1 Unification (Cont’d)
• 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 CommerceTimothy J Norman and Wamberto Vasconcelos
14
3.1 Unification (Cont’d)
• 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 CommerceTimothy J Norman and Wamberto Vasconcelos
15
3.2 Resolution
• process to prove if ⊢ P • resolution(,P ,) iff ⊢ P :
1. resolution(,P ,) if Q and unify(P,Q,)
2. resolution(,P ,) if (Q :- R1,…, Rn) and unify(P,Q,’) and
resolution(, (R1,…, Rn) ’,)
3. resolution(, (R1,…, Rn) ,) if
resolution(,R1,1) and
resolution(,(R2,…, Rn)1,)
Intelligent Architectures for Electronic CommerceTimothy J Norman and Wamberto Vasconcelos
16
3.2 Resolution (Cont’d)
• 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 CommerceTimothy J Norman and Wamberto Vasconcelos
17
4. 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 CommerceTimothy J Norman and Wamberto Vasconcelos
18
4. Pragmatics (Cont’d)
• example: creation of list
create([X|Xs]):- read(X), \+ X = stop, create(Xs).create([]).
Intelligent Architectures for Electronic CommerceTimothy J Norman and Wamberto Vasconcelos
19
4. Pragmatics (Cont’d)
• generic agent
loop(MntlSt):- final_condition(MntlSt).loop(MntlSt):- senseEnvironment(Percepts), react(Percepts,MntlSt,NewMntlSt), loop(MntlSt).
Intelligent Architectures for Electronic CommerceTimothy J Norman and Wamberto Vasconcelos
20
5. 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 CommerceTimothy J Norman and Wamberto Vasconcelos
21
6. Reading List
• Bratko, I (2001). Prolog Programming for Artificial Intelligence, 3rd Edition, Addison-Wesley.
• Stirling, L & Shapiro, E (1997). The Art of Prolog, 2nd Edition, MIT Press.