Upload
mavis-campbell
View
222
Download
0
Embed Size (px)
Citation preview
CT214 – Logical Foundations of ComputingCT214 – Logical Foundations of Computing
Lecture 8Lecture 8
Introduction to PrologIntroduction to Prolog
PROLOG -> PROgramming in LOGic
Prolog is a logic language that is suitable for symbolic or non-numeric computation.
Prolog is frequently used in Artificial Intelligence where manipulation of symbols and inference about them is common.
Prolog consists of a series of rules and facts.
A program is run by presenting some query and seeing if it can be proved against the set of known rules and facts.
Topics in Prolog:
◦ Simple facts
◦ Facts with arguments
◦ Variables and unification
◦ Rules
◦ Search
◦ Recursion
◦ Lists
Topics in Prolog:
◦ Simple facts
◦ Facts with arguments
◦ Variables and unification
◦ Rules
◦ Search
◦ Recursion
◦ Lists
Represent a statement that is true
Can consist of a particular item or a relationship between items
Must begin with a lowercase letter!
Examples of simple facts:
raining. /*it is raining*/
mary_has_a_coat. /*mary has a coat*/
jackLikesDogs. /*jack likes dogs*/
Represent more complicated facts
Consist of a relation and the items it refers to
Facts can have an arbitrary number of arguments (>0)
Relation names must begin with a lowercase letter!
Examples of facts with arguments:
hungry( paul ). /*paul is hungry*/
likes( john, mary ). /*john likes mary*/
plays( john, football ). /*john plays football*/
Variables are used to query the database
Variable names must begin with an uppercase letter!
For example:
the letter ‘X’ is a variable
the word ‘Who’ is a variable
and the string ‘Abcd’ is a variable
To query database must replace unknown data in our query with a variable
For example:
Assuming the facts stated earlier are in our database
If we want to find out who john likes, the query:
?- likes( john, X ).
returns X = mary
The process of matching items with variables is known as unification
So far we have looked at how to represent facts and how to query a database of facts
Rules allow us to make conditional statements
i.e. “the main statement is true if its sub-statement(s) are true”
Rules are written in the form:
main_statement(<arguments> ) :-
sub_statement( <arguments> ).
Consider the following statement:
“All footballers are well paid”
In other words:
“If a person is a footballer then they are well paid”
or “A person is well paid if they are a footballer”
This statement can be written in Prolog as a rule:
well_paid( X ) :- footballer( X ).
More complex conditional statements can also be written in Prolog using rules
Consider the following statement:
“A house is energy efficient if it is well insulated and has double glazed windows”
This can be written in Prolog as the following rule:
energy_efficient( X ) :-
house( X ), well_insulated( X ),
double_glazed_windows( X ).
Consider the following statement:
“A car has good fuel economy if it is a hybrid or it is a diesel”
Statements with a disjunction can be written in Prolog as two separate rules:
good_fuel_economy( X ) :- car( X ), hybrid( X ).
good_fuel_economy( X ) :- car( X ), diesel( X ).
Introduces backtracking in Prolog
Consider the following database of facts:
eats( jack, curry ).
eats( jack, pizza ).
eats( jack, tuna ).
Earlier we queried a database of facts to find out a single item of information
?- eats( jack, What ).
?- eats( jack, What ).
This query will return: What = curry
Suppose we wish to answer the question:
“What are all the things that jack eats?”
?- eats( jack, What ). returns What = curry
Can continue to ask Prolog for more results using ;
What = pizza; What = tuna; No
Backtracking in rules
Consider the following database:
hold_party( X ) :- birthday( X ), happy( X ).birthday( tom ).birthday( jack ).birthday( helen ).happy( mary ).happy( helen ).
If we want to use the information contained in this database to decide who to hold a party for we can submit the following query:
?- hold_party( Who ).
Prolog begins by finding a clause for ‘birthday’ and then binds Who to the argument of clause
Prolog will then attempt to find a clause for ‘happy’ that matches the argument bound to Who
If a match is not found, the search fails and Prolog backtracks to attempt to find a new birthday clause
hold_party( X ) :- birthday( X ), happy( X ).birthday( tom ).birthday( jack ).birthday( helen ).happy( mary ).happy( helen ).
1. Who binds to tom, search for happy( tom ), fail
2. Who binds to jack, search for happy( jack ), fail
3. Who binds to helen, search for happy( helen ), pass