SlideShare a Scribd company logo
1 of 19
27/09/04 AIPP Lecture 2: Prolog Fundamentals 1
Prolog Fundamentals
Artificial Intelligence Programming
in Prolog
Lecture 2
27/09/04
27/09/04 AIPP Lecture 2: Prolog Fundamentals 2
Anatomy of a Program
• Last week I told you that Prolog programs are made
up of facts and rules.
• A fact asserts some property of an object, or relation
between two or more objects.
e.g. parent(jane,alan).
Can be read as “Jane is the parent of Alan.”
• Rules allow us to infer that a property or relationship
holds based on preconditions.
e.g. parent(X,Y) :- mother(X,Y).
= “Person X is the parent of person Y if X is Y’s
mother.”
27/09/04 AIPP Lecture 2: Prolog Fundamentals 3
• Both facts and rules are predicate definitions.
• ‘Predicate’ is the name given to the word
occurring before the bracket in a fact or rule:
parent(jane,alan).
• By defining a predicate you are specifying
which information needs to be known for the
property denoted by the predicate to be true.
Predicate Definitions
Predicate name
27/09/04 AIPP Lecture 2: Prolog Fundamentals 4
Clauses
• Predicate definitions consist of clauses.
= An individual definition (whether it be a fact or rule).
e.g. mother(jane,alan). = Fact
parent(P1,P2):- mother(P1,P2). = Rule
• A clause consists of a head
• and sometimes a body.
– Facts don’t have a body because they are always
true.
head body
27/09/04 AIPP Lecture 2: Prolog Fundamentals 5
Arguments
• A predicate head consists of a predicate name and
sometimes some arguments contained within
brackets and separated by commas.
mother(jane,alan).
• A body can be made up of any number of subgoals
(calls to other predicates) and terms.
• Arguments also consist of terms, which can be:
– Constants e.g. jane,
– Variables e.g. Person1, or
– Compound terms (explained in later lectures).
Predicate name Arguments
27/09/04 AIPP Lecture 2: Prolog Fundamentals 6
Terms: Constants
Constants can either be:
• Numbers:
– integers are the usual form (e.g. 1, 0, -1, etc), but
– floating-point numbers can also be used (e.g. 3.0E7)
• Symbolic (non-numeric) constants:
– always start with a lower case alphabetic character and
contain any mixture of letters, digits, and underscores
(but no spaces, punctuation, or an initial capital).
• e.g. abc, big_long_constant, x4_3t).
• String constants:
– are anything between single quotes e.g. ‘Like this’.
27/09/04 AIPP Lecture 2: Prolog Fundamentals 7
Terms: Variables
• Variables always start with an upper case
alphabetic character or an underscore.
• Other than the first character they can be made up
of any mixture of letters, digits, and underscores.
e.g. X, ABC, _89two5, _very_long_variable
• There are no “types” for variables (or constants) – a
variable can take any value.
• All Prolog variables have a “local” scope:
– they only keep the same value within a clause; the same
variable used outside of a clause does not inherit the value
(this would be a “global” scope).
27/09/04 AIPP Lecture 2: Prolog Fundamentals 8
Naming tips
• Use real English when naming predicates,
constants, and variables.
e.g. “John wants to help Somebody.”
Could be: wants(john,to_help,Somebody).
Not: x87g(j,_789).
• Use a Verb Subject Object structure:
wants(john,to_help).
• BUT do not assume Prolog Understands the
meaning of your chosen names!
– You create meaning by specifying the body (i.e.
preconditions) of a clause.
27/09/04 AIPP Lecture 2: Prolog Fundamentals 9
Using predicate definitions
• Command line programming is tedious
e.g. | ?- write(‘What is your name?’), nl, read(X),
write(‘Hello ‘), write(X).
• We can define predicates to automate
commands:
greetings:-
write(‘What is your name?’),
nl,
read(X),
write(‘Hello ‘),
write(X).
| ?- greetings.
What is your name?
|: tim.
Hello tim
X = tim ?
yes
Prolog Code Terminal
27/09/04 AIPP Lecture 2: Prolog Fundamentals 10
Arity
• greetings is a predicate with no arguments.
• The number of arguments a predicate has is
called its arity.
– The arity of greetings is zero = greetings/0
• The behaviour of predicates can be made
more specific by including more arguments.
– greetings(hamish) = greetings/1
• The predicate can then behave differently
depending on the arguments passed to it.
27/09/04 AIPP Lecture 2: Prolog Fundamentals 11
Using multiple clauses
• Different clauses can be used to deal with
different arguments.
greet(hamish):-
write(‘How are you doin, pal?’).
greet(amelia):-
write(‘Awfully nice to see you!’).
= “Greet Hamish or Amelia” = a disjunction of goals.
| ?- greet(hamish). | ?- greet(amelia).
How are you doin, pal? Awfully nice to see you!
yes yes
• Clauses are tried in order from the top of the file.
• The first clause to match succeeds (= yes).
27/09/04 AIPP Lecture 2: Prolog Fundamentals 12
Variables in Questions
• We can call greet/1 with a variable in the question.
• A variable will match any head of greet/1.
| ?- greet(Anybody).
How are you doin, pal?
Anybody = hamish?
yes
• The question first matches the clause closest to the
top of the file.
• The variable is instantiated (i.e. bound) to the value
‘hamish’.
• As the variable was in the question it is passed back
to the terminal (Anybody = hamish?).
27/09/04 AIPP Lecture 2: Prolog Fundamentals 13
Re-trying Goals
• When a question is asked with a variable as an
argument (e.g. greet(Anybody).) we can ask the
Prolog interpreter for multiple answers using: ;
| ?- greet(Anybody).
How are you doin, pal?
Anybody = hamish? ;  “Redo that match.”
Anybody = amelia? ;  “Redo that match.”
no  “Fail as no more matches.”
• This fails the last clause used and searches down
the program for another that matches.
• RETURN = accept the match
• ; = reject that match
27/09/04 AIPP Lecture 2: Prolog Fundamentals 14
Variable clause head.
• If greet/1 is called with a constant other than
hamish or amelia it will fail (return no).
• We can create a default case that always succeeds by
writing a clause with a variable as the head argument.
greet(Anybody):-
write(‘Hullo ‘),
write(Anybody).
• Any call to greet/1 will unify (i.e. match)
greet(Anybody).
• Once the terms unify the variable is instantiated to the
value of the argument (e.g. bob).
|?- greet(bob).
Hullo bob.
yes
27/09/04 AIPP Lecture 2: Prolog Fundamentals 15
Ordering of clauses
• The order of multiple clauses is important.
greet(hamish):-
write('How are you doin, pal?').
greet(amelia):-
write('Awfully nice to see you!').
• The most specific clause should always be at the top.
• General clauses (containing variables) at the bottom.
| ?- greet(hamish).
Hullo hamish?
yes
greet(Anybody):-
write('Hullo '), write(Anybody).
27/09/04 AIPP Lecture 2: Prolog Fundamentals 16
Ordering of clauses
• The order of multiple clauses is important.
greet(hamish):-
write('How are you doin, pal?').
greet(amelia):-
write('Awfully nice to see you!').
• The most specific clause should always be at the top.
• General clauses (containing variables) at the bottom.
| ?- greet(hamish).
How are you doin,
pal?.
yes
greet(Anybody):-
write('Hullo '), write(Anybody).
27/09/04 AIPP Lecture 2: Prolog Fundamentals 17
Unification
• When two terms match we say that they unify.
– Their structures and arguments are compatible.
• This can be checked using =/2
|?- loves(john,X) = loves(Y,mary).
X = mary,  unification leads to instantiation
Y = john? 
yes Terms that unify Outcome
fred = fred. yes.
‘Hey you’ = ‘Hey you’. yes
fred=X. X=fred.
X=Y. Y = X.
foo(X) = foo(bar). X=bar.
foo(N,N) = foo(bar,X). N=bar, X=bar.
foo(foo(bar)) = foo(X) X = foo(bar)
Terms that don’t unify
fred = jim.
‘Hey you’ = ‘Hey me’.
frou(frou) = f(frou).
foo(bar) = foo(bar,bar).
foo(N,N) = foo(bar,rab).
27/09/04 AIPP Lecture 2: Prolog Fundamentals 18
Asking questions of the database
We can ask about facts directly:
|?- mother(X,alan).
X = jane?
Yes
Or we can define rules that prove
if a property or relationship holds
given the facts currently in the
database.
|?- parent(jane,X).
X = alan?
yes
mother(jane,alan).
father(john,alan).
parent(Mum,Child):-
mother(Mum,Child).
parent(Dad,Child):-
father(Dad,Child).
27/09/04 AIPP Lecture 2: Prolog Fundamentals 19
Summary
• A Prolog program consists of predicate definitions.
• A predicate denotes a property or relationship between objects.
• Definitions consist of clauses.
• A clause has a head and a body (Rule) or just a head (Fact).
• A head consists of a predicate name and arguments.
• A clause body consists of a conjunction of terms.
• Terms can be constants, variables, or compound terms.
• We can set our program goals by typing a command that unifies
with a clause head.
• A goal unifies with clause heads in order (top down).
• Unification leads to the instantiation of variables to values.
• If any variables in the initial goal become instantiated this is
reported back to the user.

More Related Content

Similar to Prolog fundamentals for beeginers in windows.ppt

Inductive definitions
Inductive definitionsInductive definitions
Inductive definitions
lorcap
 
UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptx
UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptxUOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptx
UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptx
qasim ali
 
introduction to Genifer -- Deduction
introduction to Genifer -- Deductionintroduction to Genifer -- Deduction
introduction to Genifer -- Deduction
Yan Yin
 
Propositional logic is a good vehicle to introduce basic properties of logic
Propositional logic is a good vehicle to introduce basic properties of logicPropositional logic is a good vehicle to introduce basic properties of logic
Propositional logic is a good vehicle to introduce basic properties of logic
pendragon6626
 
Special Topics on Functions, Sequences, and Series MAT117 .docx
 Special Topics on Functions, Sequences, and Series MAT117 .docx Special Topics on Functions, Sequences, and Series MAT117 .docx
Special Topics on Functions, Sequences, and Series MAT117 .docx
MARRY7
 
Theory of first order logic
Theory of first order logicTheory of first order logic
Theory of first order logic
Devaddd
 

Similar to Prolog fundamentals for beeginers in windows.ppt (20)

Technical Style Workshop Part 1
Technical Style Workshop Part 1Technical Style Workshop Part 1
Technical Style Workshop Part 1
 
Prolog,Prolog Programming IN AI.pdf
Prolog,Prolog Programming IN AI.pdfProlog,Prolog Programming IN AI.pdf
Prolog,Prolog Programming IN AI.pdf
 
ICS1019.pdf
ICS1019.pdfICS1019.pdf
ICS1019.pdf
 
prolog ppt
prolog pptprolog ppt
prolog ppt
 
predicateLogic.ppt
predicateLogic.pptpredicateLogic.ppt
predicateLogic.ppt
 
Prolog basics
Prolog basicsProlog basics
Prolog basics
 
Inductive definitions
Inductive definitionsInductive definitions
Inductive definitions
 
UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptx
UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptxUOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptx
UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptx
 
01bkb04p.ppt
01bkb04p.ppt01bkb04p.ppt
01bkb04p.ppt
 
introduction to Genifer -- Deduction
introduction to Genifer -- Deductionintroduction to Genifer -- Deduction
introduction to Genifer -- Deduction
 
Lecture 1.pdf
Lecture 1.pdfLecture 1.pdf
Lecture 1.pdf
 
Majorfinal
MajorfinalMajorfinal
Majorfinal
 
lect14-semantics.ppt
lect14-semantics.pptlect14-semantics.ppt
lect14-semantics.ppt
 
Propositional logic is a good vehicle to introduce basic properties of logic
Propositional logic is a good vehicle to introduce basic properties of logicPropositional logic is a good vehicle to introduce basic properties of logic
Propositional logic is a good vehicle to introduce basic properties of logic
 
continuity of module 2.pptx
continuity of module 2.pptxcontinuity of module 2.pptx
continuity of module 2.pptx
 
Logic in Predicate and Propositional Logic
Logic in Predicate and Propositional LogicLogic in Predicate and Propositional Logic
Logic in Predicate and Propositional Logic
 
Ai lab manual
Ai lab manualAi lab manual
Ai lab manual
 
Special Topics on Functions, Sequences, and Series MAT117 .docx
 Special Topics on Functions, Sequences, and Series MAT117 .docx Special Topics on Functions, Sequences, and Series MAT117 .docx
Special Topics on Functions, Sequences, and Series MAT117 .docx
 
Prolog2 (1)
Prolog2 (1)Prolog2 (1)
Prolog2 (1)
 
Theory of first order logic
Theory of first order logicTheory of first order logic
Theory of first order logic
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 

Prolog fundamentals for beeginers in windows.ppt

  • 1. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 1 Prolog Fundamentals Artificial Intelligence Programming in Prolog Lecture 2 27/09/04
  • 2. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 2 Anatomy of a Program • Last week I told you that Prolog programs are made up of facts and rules. • A fact asserts some property of an object, or relation between two or more objects. e.g. parent(jane,alan). Can be read as “Jane is the parent of Alan.” • Rules allow us to infer that a property or relationship holds based on preconditions. e.g. parent(X,Y) :- mother(X,Y). = “Person X is the parent of person Y if X is Y’s mother.”
  • 3. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 3 • Both facts and rules are predicate definitions. • ‘Predicate’ is the name given to the word occurring before the bracket in a fact or rule: parent(jane,alan). • By defining a predicate you are specifying which information needs to be known for the property denoted by the predicate to be true. Predicate Definitions Predicate name
  • 4. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 4 Clauses • Predicate definitions consist of clauses. = An individual definition (whether it be a fact or rule). e.g. mother(jane,alan). = Fact parent(P1,P2):- mother(P1,P2). = Rule • A clause consists of a head • and sometimes a body. – Facts don’t have a body because they are always true. head body
  • 5. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 5 Arguments • A predicate head consists of a predicate name and sometimes some arguments contained within brackets and separated by commas. mother(jane,alan). • A body can be made up of any number of subgoals (calls to other predicates) and terms. • Arguments also consist of terms, which can be: – Constants e.g. jane, – Variables e.g. Person1, or – Compound terms (explained in later lectures). Predicate name Arguments
  • 6. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 6 Terms: Constants Constants can either be: • Numbers: – integers are the usual form (e.g. 1, 0, -1, etc), but – floating-point numbers can also be used (e.g. 3.0E7) • Symbolic (non-numeric) constants: – always start with a lower case alphabetic character and contain any mixture of letters, digits, and underscores (but no spaces, punctuation, or an initial capital). • e.g. abc, big_long_constant, x4_3t). • String constants: – are anything between single quotes e.g. ‘Like this’.
  • 7. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 7 Terms: Variables • Variables always start with an upper case alphabetic character or an underscore. • Other than the first character they can be made up of any mixture of letters, digits, and underscores. e.g. X, ABC, _89two5, _very_long_variable • There are no “types” for variables (or constants) – a variable can take any value. • All Prolog variables have a “local” scope: – they only keep the same value within a clause; the same variable used outside of a clause does not inherit the value (this would be a “global” scope).
  • 8. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 8 Naming tips • Use real English when naming predicates, constants, and variables. e.g. “John wants to help Somebody.” Could be: wants(john,to_help,Somebody). Not: x87g(j,_789). • Use a Verb Subject Object structure: wants(john,to_help). • BUT do not assume Prolog Understands the meaning of your chosen names! – You create meaning by specifying the body (i.e. preconditions) of a clause.
  • 9. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 9 Using predicate definitions • Command line programming is tedious e.g. | ?- write(‘What is your name?’), nl, read(X), write(‘Hello ‘), write(X). • We can define predicates to automate commands: greetings:- write(‘What is your name?’), nl, read(X), write(‘Hello ‘), write(X). | ?- greetings. What is your name? |: tim. Hello tim X = tim ? yes Prolog Code Terminal
  • 10. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 10 Arity • greetings is a predicate with no arguments. • The number of arguments a predicate has is called its arity. – The arity of greetings is zero = greetings/0 • The behaviour of predicates can be made more specific by including more arguments. – greetings(hamish) = greetings/1 • The predicate can then behave differently depending on the arguments passed to it.
  • 11. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 11 Using multiple clauses • Different clauses can be used to deal with different arguments. greet(hamish):- write(‘How are you doin, pal?’). greet(amelia):- write(‘Awfully nice to see you!’). = “Greet Hamish or Amelia” = a disjunction of goals. | ?- greet(hamish). | ?- greet(amelia). How are you doin, pal? Awfully nice to see you! yes yes • Clauses are tried in order from the top of the file. • The first clause to match succeeds (= yes).
  • 12. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 12 Variables in Questions • We can call greet/1 with a variable in the question. • A variable will match any head of greet/1. | ?- greet(Anybody). How are you doin, pal? Anybody = hamish? yes • The question first matches the clause closest to the top of the file. • The variable is instantiated (i.e. bound) to the value ‘hamish’. • As the variable was in the question it is passed back to the terminal (Anybody = hamish?).
  • 13. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 13 Re-trying Goals • When a question is asked with a variable as an argument (e.g. greet(Anybody).) we can ask the Prolog interpreter for multiple answers using: ; | ?- greet(Anybody). How are you doin, pal? Anybody = hamish? ;  “Redo that match.” Anybody = amelia? ;  “Redo that match.” no  “Fail as no more matches.” • This fails the last clause used and searches down the program for another that matches. • RETURN = accept the match • ; = reject that match
  • 14. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 14 Variable clause head. • If greet/1 is called with a constant other than hamish or amelia it will fail (return no). • We can create a default case that always succeeds by writing a clause with a variable as the head argument. greet(Anybody):- write(‘Hullo ‘), write(Anybody). • Any call to greet/1 will unify (i.e. match) greet(Anybody). • Once the terms unify the variable is instantiated to the value of the argument (e.g. bob). |?- greet(bob). Hullo bob. yes
  • 15. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 15 Ordering of clauses • The order of multiple clauses is important. greet(hamish):- write('How are you doin, pal?'). greet(amelia):- write('Awfully nice to see you!'). • The most specific clause should always be at the top. • General clauses (containing variables) at the bottom. | ?- greet(hamish). Hullo hamish? yes greet(Anybody):- write('Hullo '), write(Anybody).
  • 16. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 16 Ordering of clauses • The order of multiple clauses is important. greet(hamish):- write('How are you doin, pal?'). greet(amelia):- write('Awfully nice to see you!'). • The most specific clause should always be at the top. • General clauses (containing variables) at the bottom. | ?- greet(hamish). How are you doin, pal?. yes greet(Anybody):- write('Hullo '), write(Anybody).
  • 17. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 17 Unification • When two terms match we say that they unify. – Their structures and arguments are compatible. • This can be checked using =/2 |?- loves(john,X) = loves(Y,mary). X = mary,  unification leads to instantiation Y = john?  yes Terms that unify Outcome fred = fred. yes. ‘Hey you’ = ‘Hey you’. yes fred=X. X=fred. X=Y. Y = X. foo(X) = foo(bar). X=bar. foo(N,N) = foo(bar,X). N=bar, X=bar. foo(foo(bar)) = foo(X) X = foo(bar) Terms that don’t unify fred = jim. ‘Hey you’ = ‘Hey me’. frou(frou) = f(frou). foo(bar) = foo(bar,bar). foo(N,N) = foo(bar,rab).
  • 18. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 18 Asking questions of the database We can ask about facts directly: |?- mother(X,alan). X = jane? Yes Or we can define rules that prove if a property or relationship holds given the facts currently in the database. |?- parent(jane,X). X = alan? yes mother(jane,alan). father(john,alan). parent(Mum,Child):- mother(Mum,Child). parent(Dad,Child):- father(Dad,Child).
  • 19. 27/09/04 AIPP Lecture 2: Prolog Fundamentals 19 Summary • A Prolog program consists of predicate definitions. • A predicate denotes a property or relationship between objects. • Definitions consist of clauses. • A clause has a head and a body (Rule) or just a head (Fact). • A head consists of a predicate name and arguments. • A clause body consists of a conjunction of terms. • Terms can be constants, variables, or compound terms. • We can set our program goals by typing a command that unifies with a clause head. • A goal unifies with clause heads in order (top down). • Unification leads to the instantiation of variables to values. • If any variables in the initial goal become instantiated this is reported back to the user.