Lecture1 LP
Lecture1 LP
An Introduction to Prolog
I Symbolic computation:
I relational databases,
I mathematical logic,
I abstract problem solving,
I natural language understanding,
I symbolic equation solving,
I design automation,
I artificial intelligence,
I biochemical structure analysis, etc.
Applications of logic programming (cont’d)
I Industrial applications:
I aviation:
I SCORE - a longterm aiport capacity management system for
coordinated airports (20% of air traffic worldwide, according
to www.pdc-aviation.com)
I FleetWatch - operational control, used by 21 international air
companies.
I personnel planning: StaffPlan (airports in Barcelona, Madrid;
Hovedstaden region in Denmark).
I information management for disasters: ARGOS - crisis
management in CBRN (chemical, biological, radiological and
nuclear) incidents – used by Australia, Brasil, Canada, Ireland,
Denmark, Sweden, Norway, Poland, Estonia, Lithuania and
Montenegro.
Problem solving with Prolog
I Programming in Prolog:
I rather than prescribe a sequence of steps to solve a problem,
I describe known facts and relations, then ask questions.
I Use Prolog to solve problems that involve objects and
relations between objects.
I Examples:
I Objects: “John”, “book”, “jewel”, etc.
I Relations: “John owns the book”, “The jewel is valuable”.
I Rules: “Two people are sisters if they are both females and
they have the same parents”.
Problem solving with Prolog (cont’d)
I A query in Prolog:
?− owns ( mary , book ) .
Prolog searches in the knowledge base for facts that match
the question:
Queries
I A query in Prolog:
?− owns ( mary , book ) .
Prolog searches in the knowledge base for facts that match
the question:
I Prolog answers true if :
Queries
I A query in Prolog:
?− owns ( mary , book ) .
Prolog searches in the knowledge base for facts that match
the question:
I Prolog answers true if :
I the predicates are the same,
Queries
I A query in Prolog:
?− owns ( mary , book ) .
Prolog searches in the knowledge base for facts that match
the question:
I Prolog answers true if :
I the predicates are the same,
I arguments are the same,
Queries
I A query in Prolog:
?− owns ( mary , book ) .
Prolog searches in the knowledge base for facts that match
the question:
I Prolog answers true if :
I the predicates are the same,
I arguments are the same,
I Ortherwise the answer is false :
Queries
I A query in Prolog:
?− owns ( mary , book ) .
Prolog searches in the knowledge base for facts that match
the question:
I Prolog answers true if :
I the predicates are the same,
I arguments are the same,
I Ortherwise the answer is false :
I only what is known is true (“closed world assumption”),
Queries
I A query in Prolog:
?− owns ( mary , book ) .
Prolog searches in the knowledge base for facts that match
the question:
I Prolog answers true if :
I the predicates are the same,
I arguments are the same,
I Ortherwise the answer is false :
I only what is known is true (“closed world assumption”),
I Attention: false may not mean that the answer is false (but
more like “not derivable from the knowledge”).
Variables
I Think variables in predicate logic.
Variables
I Think variables in predicate logic.
I Instead of:
?− l i k e s ( j o h n , mary ) .
?− l i k e s ( j o h n , a p p l e s ) .
?− l i k e s ( j o h n , candy ) .
ask something like “What does John like?” (i.e. give
everything that John likes).
Variables
I Think variables in predicate logic.
I Instead of:
?− l i k e s ( j o h n , mary ) .
?− l i k e s ( j o h n , a p p l e s ) .
?− l i k e s ( j o h n , candy ) .
ask something like “What does John like?” (i.e. give
everything that John likes).
I Variables stand for objects to be determined by Prolog.
Variables
I Think variables in predicate logic.
I Instead of:
?− l i k e s ( j o h n , mary ) .
?− l i k e s ( j o h n , a p p l e s ) .
?− l i k e s ( j o h n , candy ) .
ask something like “What does John like?” (i.e. give
everything that John likes).
I Variables stand for objects to be determined by Prolog.
I Variables can be:
Variables
I Think variables in predicate logic.
I Instead of:
?− l i k e s ( j o h n , mary ) .
?− l i k e s ( j o h n , a p p l e s ) .
?− l i k e s ( j o h n , candy ) .
ask something like “What does John like?” (i.e. give
everything that John likes).
I Variables stand for objects to be determined by Prolog.
I Variables can be:
I instantiated - there is an object the variable stands for,
Variables
I Think variables in predicate logic.
I Instead of:
?− l i k e s ( j o h n , mary ) .
?− l i k e s ( j o h n , a p p l e s ) .
?− l i k e s ( j o h n , candy ) .
ask something like “What does John like?” (i.e. give
everything that John likes).
I Variables stand for objects to be determined by Prolog.
I Variables can be:
I instantiated - there is an object the variable stands for,
I uninstantiated - it is not (yet) known what the variable stands
for.
Variables
I Think variables in predicate logic.
I Instead of:
?− l i k e s ( j o h n , mary ) .
?− l i k e s ( j o h n , a p p l e s ) .
?− l i k e s ( j o h n , candy ) .
ask something like “What does John like?” (i.e. give
everything that John likes).
I Variables stand for objects to be determined by Prolog.
I Variables can be:
I instantiated - there is an object the variable stands for,
I uninstantiated - it is not (yet) known what the variable stands
for.
I In Prolog variables start with CAPITAL LETTERS:
?− l i k e s ( j o h n , X ) .
Prolog computation: example
/∗1∗/ t h i e f ( j o h n ) .
/∗2∗/ l i k e s ( mary , f o o d ) .
/∗3∗/ l i k e s ( mary , w i n e ) .
/∗4∗/ l i k e s ( j o h n , X): − l i k e s (X , w i n e ) .
/∗5∗/ m a y s t e a l (X , Y) :−
t h i e f (X ) , l i k e s (X , Y ) .
I Explain how the query
?− m a y s t e a l ( j o h n , X ) .
is executed by Prolog.
I Prolog programs are built from terms (written as strings of
characters).
I The following are terms:
I constants,
I variables,
I structures.
Constants
I atoms:
I likes ,
I a (lowercase letters),
I =,
I −−>,
I ’Void’ (anything between single quotes),
I george smith (constants may contain underscore),
I not atoms:
I 314a5 (cannot start with a number),
I george−smith (cannot contain a hyphen),
I George (cannot start with a capital letter),
I something (cannot start with underscore).
Variables
I Characters:
I A-Z
I a-z
I 0-9
I +-*/\ ˜ˆ<>: .
I ? @#$&
I Characters are ASCII (printing) characters with codes greater
than 32.
I Remark: ’ ’ allows the use of any character.
Arithmetic operators
I Arithmetic operators:
I +,
I −,
I ∗,
I /,
I +(x, ∗(y , z)) is equivalent with x + (y · z)
I Operators do not cause evaluation in Prolog.
I Example: 3+4 (structure) does not have the same meaning
with 7 (term).
I X is 3+4 causes evaluation ( is represents the evaluator in
Prolog).
I The result of the evaluation is that X is assigned the value 7.
Parsing arithmetic expressions
X = f (X ) .
Occurs check
X = f (X ) .
?− p r i n c e ( c a d w a l l o n , 9 8 6 ) .
true
?− p r i n c e (X , 9 7 9 ) .
X = lago ap idwal ;
X = hywel ap ieuaf
Example (with arithmetic(2))
pop ( p l a c e 1 , 203).
pop ( p l a c e 2 , 548).
pop ( p l a c e 3 , 800).
pop ( p l a c e 4 , 108).
area ( place1 , 3) .
area ( place2 , 1) .
area ( place3 , 4) .
area ( place4 , 3) .
d e n s i t y (X , Y): −
pop (X , P ) ,
a r e a (X , A ) ,
Y i s P/A .
?− d e n s i t y ( p l a c e 3 , X ) .
X = 200
true
I In this lecture the following were discussed:
I asserting facts about objects,
I asking questions about facts,
I using variables, scopes of variables,
I conjunctions,
I an introduction to backtracking (in examples),
I Prolog syntax: terms (constants, variables, structures),
I Arithmetic in Prolog,
I Unification procedure,
I Subtle point: occurs check.
I All things SWIProlog can be found at
http://www.swi-prolog.org.
I Install SWI-Prolog and try out the examples in the lecture.
I Read: Chapter 1 and Chapter 2 (including exercises section)
of [Clocksin and Mellish, 2003].
Ben-Ari, M. (2001).
Mathematical Logic for Computer Science.
Springer Verlag, London, 2nd edition.
Clocksin, W. F. and Mellish, C. S. (2003).
Programming in Prolog.
Springer, Berlin, 5th edition.
Nilsson, U. and Maluszynski, J. (2000).
Logic, Programming and Prolog.
copyright Ulf Nilsson and Jan Maluszynski, 2nd edition.