Title: Logic Programming
1Logic Programming
2Announcements
3Logic Programming
- the use of mathematical logic for computer
programming. - In this view of logic programming, which can be
traced at least as far back as John McCarthy's
1958 advice-taker proposal - It was probably the first proposal to use logic
to represent information in a computer and the
first paper to propose common sense reasoning
ability as the key to artificial intelligence. - In his paper, McCarthy advocated
- "programs to manipulate in a suitable formal
language common instrumental statements. The
basic program will draw immediate conclusions
from a list of premises. These conclusions will
be either declarative or imperative sentences.
When an imperative sentence is deduced the
program takes a corresponding action." - Logic is used as a purely declarative
representation language, and a or
model-generator is used as the problem-solver.
4Logic Programming
- The problem-solving task is split between
- the programmer, who is responsible only for
ensuring the truth of programs expressed in
logical form, and - the theorem-prover or model-generator, which is
responsible for solving problems efficiently.
5Prolog
- The programming language Prolog was developed in
1972 by Alain Colmerauer (Marseille) and Robert
Kowalski (Edinburgh) - Colmerauer was working on natural language
understanding, using logic to represent semantics
and using resolution for question-answering. - During the summer of 1971, they discovered that
the clausal form of logic could be used to
represent formal grammars and that resolution
theorem provers could be used for parsing. - It was in the following summer of 1972, that
Kowalski, again working with Colmerauer,
developed the procedural interpretation of
implications which later became formalised in the
Prolog notation
6Learn Prolog Now!
7GNU Prolog
- Freely available Prolog interpreter
- Works with Windows,
- There are many more Prolog interpreters
- Not all are ISO compliant
8Lecture
- Theory
- Introduction to Prolog
- Facts, Rules and Queries
- Prolog Syntax
- Exercises
- Exercises of LPN chapter 1
- Practical work
9Aim of this lecture
- Give some simple examples of Prolog programs
- Discuss the three basic constructs in Prolog
- Facts
- Rules
- Queries
- Introduce other concepts, such as
- the role of logic
- unification with the help of variables
- Begin the systematic study of Prolog by defining
terms, atoms, and variables
10Prolog
- "Programming with Logic"
- Declarative
- Very different from other (procedural)
programming languages - Good for knowledge-rich tasks
11History of Prolog
first Prolog interpreter by Colmerauer and Roussel
1972
1980s/1990s
1977
1980
2005
12History of Prolog
implementation of DEC10 compiler by Warren
1972
1980s/1990s
1977
1980
2005
13History of Prolog
Definite Clause Grammars implementation by
Pereira and Warren
1972
1980s/1990s
1977
1980
2005
14History of Prolog
Prolog grows in popularity especially in Europe
and Japan
1972
1980s/1990s
1977
1980
2005
15History of Prolog
Prolog used to program natural language interface
in International Space Station by NASA
1972
1980s/1990s
1977
1980
2005
16Basic idea of Prolog
- Describe the situation of interest
- Ask a question
- Prolog logically deduces new facts about the
situation we described - Prolog gives us its deductions back as answers
17Consequences
- Think declaratively, not procedurally
- Challenging
- Requires a different mindset
- High-level language
- Not as efficient as, say, C
- Good for rapid prototyping
- Useful in many AI applications
18Knowledge Base 1
- woman(mia).
- woman(jody).
- woman(yolanda).
- playsAirGuitar(jody).
- party.
19Knowledge Base 1
- woman(mia).
- woman(jody).
- woman(yolanda).
- playsAirGuitar(jody).
- party.
?-
20Knowledge Base 1
- woman(mia).
- woman(jody).
- woman(yolanda).
- playsAirGuitar(jody).
- party.
?- woman(mia).
21Knowledge Base 1
- woman(mia).
- woman(jody).
- woman(yolanda).
- playsAirGuitar(jody).
- party.
?- woman(mia). yes ?-
22Knowledge Base 1
- woman(mia).
- woman(jody).
- woman(yolanda).
- playsAirGuitar(jody).
- party.
?- woman(mia). yes ?- playsAirGuitar(jody).
23Knowledge Base 1
- woman(mia).
- woman(jody).
- woman(yolanda).
- playsAirGuitar(jody).
- party.
?- woman(mia). yes ?- playsAirGuitar(jody). yes ?-
24Knowledge Base 1
- woman(mia).
- woman(jody).
- woman(yolanda).
- playsAirGuitar(jody).
- party.
?- woman(mia). yes ?- playsAirGuitar(jody). yes ?-
playsAirGuitar(mia). no
25Knowledge Base 1
- woman(mia).
- woman(jody).
- woman(yolanda).
- playsAirGuitar(jody).
- party.
?- tattoed(jody).
26Knowledge Base 1
- woman(mia).
- woman(jody).
- woman(yolanda).
- playsAirGuitar(jody).
- party.
?- tattoed(jody). no ?-
27Knowledge Base 1
- woman(mia).
- woman(jody).
- woman(yolanda).
- playsAirGuitar(jody).
- party.
?- tattoed(jody). ERROR predicate tattoed/1 not
defined. ?-
28Knowledge Base 1
- woman(mia).
- woman(jody).
- woman(yolanda).
- playsAirGuitar(jody).
- party.
?- party.
29Knowledge Base 1
- woman(mia).
- woman(jody).
- woman(yolanda).
- playsAirGuitar(jody).
- party.
?- party. yes ?-
30Knowledge Base 1
- woman(mia).
- woman(jody).
- woman(yolanda).
- playsAirGuitar(jody).
- party.
?- rockConcert.
31Knowledge Base 1
- woman(mia).
- woman(jody).
- woman(yolanda).
- playsAirGuitar(jody).
- party.
?- rockConcert. no ?-
32Knowledge Base 2
- happy(yolanda).
- listens2music(mia).
- listens2music(yolanda)- happy(yolanda).
- playsAirGuitar(mia)- listens2music(mia).
- playsAirGuitar(yolanda)- listens2music(yolanda).
33Knowledge Base 2
fact
- happy(yolanda).
- listens2music(mia).
- listens2music(yolanda)- happy(yolanda).
- playsAirGuitar(mia)- listens2music(mia).
- playsAirGuitar(yolanda)- listens2music(yolanda).
34Knowledge Base 2
fact
- happy(yolanda).
- listens2music(mia).
- listens2music(yolanda)- happy(yolanda).
- playsAirGuitar(mia)- listens2music(mia).
- playsAirGuitar(yolanda)- listens2music(yolanda).
fact
35Knowledge Base 2
fact
- happy(yolanda).
- listens2music(mia).
- listens2music(yolanda)- happy(yolanda).
- playsAirGuitar(mia)- listens2music(mia).
- playsAirGuitar(yolanda)- listens2music(yolanda).
fact
rule
36Knowledge Base 2
fact
- happy(yolanda).
- listens2music(mia).
- listens2music(yolanda)- happy(yolanda).
- playsAirGuitar(mia)- listens2music(mia).
- playsAirGuitar(yolanda)- listens2music(yolanda).
fact
rule
rule
37Knowledge Base 2
fact
- happy(yolanda).
- listens2music(mia).
- listens2music(yolanda)- happy(yolanda).
- playsAirGuitar(mia)- listens2music(mia).
- playsAirGuitar(yolanda)- listens2music(yolanda).
fact
rule
rule
rule
38Knowledge Base 2
- happy(yolanda).
- listens2music(mia).
- listens2music(yolanda)- happy(yolanda).
- playsAirGuitar(mia)- listens2music(mia).
- playsAirGuitar(yolanda)- listens2music(yolanda).
head
body
39Knowledge Base 2
- happy(yolanda).
- listens2music(mia).
- listens2music(yolanda)- happy(yolanda).
- playsAirGuitar(mia)- listens2music(mia).
- playsAirGuitar(yolanda)- listens2music(yolanda).
?-
40Knowledge Base 2
- happy(yolanda).
- listens2music(mia).
- listens2music(yolanda)- happy(yolanda).
- playsAirGuitar(mia)- listens2music(mia).
- playsAirGuitar(yolanda)- listens2music(yolanda).
?- playsAirGuitar(mia). yes ?-
41Knowledge Base 2
- happy(yolanda).
- listens2music(mia).
- listens2music(yolanda)- happy(yolanda).
- playsAirGuitar(mia)- listens2music(mia).
- playsAirGuitar(yolanda)- listens2music(yolanda).
?- playsAirGuitar(mia). yes ?- playsAirGuitar(yola
nda). yes
42Clauses
- happy(yolanda).
- listens2music(mia).
- listens2music(yolanda)- happy(yolanda).
- playsAirGuitar(mia)- listens2music(mia).
- playsAirGuitar(yolanda)- listens2music(yolanda).
There are five clauses in this knowledge
base two facts, and three rules. The end of a
clause is marked with a full stop.
43Predicates
- happy(yolanda).
- listens2music(mia).
- listens2music(yolanda)- happy(yolanda).
- playsAirGuitar(mia)- listens2music(mia).
- playsAirGuitar(yolanda)- listens2music(yolanda).
There are three predicates in this knowledge
base happy, listens2music, and playsAirGuitar
44Knowledge Base 3
- happy(vincent).
- listens2music(butch).
- playsAirGuitar(vincent)- listens2music(vincent),
happy(vincent). - playsAirGuitar(butch)- happy(butch).
- playsAirGuitar(butch)- listens2music(butch).
45Expressing Conjunction
- happy(vincent).
- listens2music(butch).
- playsAirGuitar(vincent)- listens2music(vincent),
happy(vincent). - playsAirGuitar(butch)- happy(butch).
- playsAirGuitar(butch)- listens2music(butch).
The comma ," expresses conjunction in Prolog
46Knowledge Base 3
- happy(vincent).
- listens2music(butch).
- playsAirGuitar(vincent)- listens2music(vincent),
happy(vincent). - playsAirGuitar(butch)- happy(butch).
- playsAirGuitar(butch)- listens2music(butch).
?- playsAirGuitar(vincent). no ?-
47Knowledge Base 3
- happy(vincent).
- listens2music(butch).
- playsAirGuitar(vincent)- listens2music(vincent),
happy(vincent). - playsAirGuitar(butch)- happy(butch).
- playsAirGuitar(butch)- listens2music(butch).
?- playsAirGuitar(butch). yes ?-
48Expressing Disjunction
- happy(vincent).
- listens2music(butch).
- playsAirGuitar(vincent)- listens2music(vincent),
happy(vincent). - playsAirGuitar(butch)- happy(butch).
- playsAirGuitar(butch)- listens2music(butch).
happy(vincent). listens2music(butch). playsAirGuit
ar(vincent)- listens2music(vincent),
happy(vincent). playsAirGuitar(butch)-
happy(butch) listens2music(butch).
49Prolog and Logic
- Clearly Prolog has something to do with logic
- Operators
- Implication -
- Conjunction ,
- Disjunction
- Use of modus ponens
- Negation
50Knowledge Base 4
- woman(mia).
- woman(jody).
- woman(yolanda).
- loves(vincent, mia).
- loves(marsellus, mia).
- loves(pumpkin, honey_bunny).
- loves(honey_bunny, pumpkin).
51Prolog Variables
- woman(mia).
- woman(jody).
- woman(yolanda).
- loves(vincent, mia).
- loves(marsellus, mia).
- loves(pumpkin, honey_bunny).
- loves(honey_bunny, pumpkin).
?- woman(X).
52Variable Instantiation
- woman(mia).
- woman(jody).
- woman(yolanda).
- loves(vincent, mia).
- loves(marsellus, mia).
- loves(pumpkin, honey_bunny).
- loves(honey_bunny, pumpkin).
?- woman(X). Xmia
53Asking Alternatives
- woman(mia).
- woman(jody).
- woman(yolanda).
- loves(vincent, mia).
- loves(marsellus, mia).
- loves(pumpkin, honey_bunny).
- loves(honey_bunny, pumpkin).
?- woman(X). Xmia
54Asking Alternatives
- woman(mia).
- woman(jody).
- woman(yolanda).
- loves(vincent, mia).
- loves(marsellus, mia).
- loves(pumpkin, honey_bunny).
- loves(honey_bunny, pumpkin).
?- woman(X). Xmia Xjody
55Asking Alternatives
- woman(mia).
- woman(jody).
- woman(yolanda).
- loves(vincent, mia).
- loves(marsellus, mia).
- loves(pumpkin, honey_bunny).
- loves(honey_bunny, pumpkin).
?- woman(X). Xmia Xjody Xyolanda
56Asking Alternatives
- woman(mia).
- woman(jody).
- woman(yolanda).
- loves(vincent, mia).
- loves(marsellus, mia).
- loves(pumpkin, honey_bunny).
- loves(honey_bunny, pumpkin).
?- woman(X). Xmia Xjody Xyolanda no
57Knowledge Base 4
- woman(mia).
- woman(jody).
- woman(yolanda).
- loves(vincent, mia).
- loves(marsellus, mia).
- loves(pumpkin, honey_bunny).
- loves(honey_bunny, pumpkin).
?- loves(marsellus,X), woman(X).
58Knowledge Base 4
- woman(mia).
- woman(jody).
- woman(yolanda).
- loves(vincent, mia).
- loves(marsellus, mia).
- loves(pumpkin, honey_bunny).
- loves(honey_bunny, pumpkin).
?- loves(marsellus,X), woman(X). Xmia yes ?-
59Knowledge Base 4
- woman(mia).
- woman(jody).
- woman(yolanda).
- loves(vincent, mia).
- loves(marsellus, mia).
- loves(pumpkin, honey_bunny).
- loves(honey_bunny, pumpkin).
?- loves(pumpkin,X), woman(X).
60Knowledge Base 4
- woman(mia).
- woman(jody).
- woman(yolanda).
- loves(vincent, mia).
- loves(marsellus, mia).
- loves(pumpkin, honey_bunny).
- loves(honey_bunny, pumpkin).
?- loves(pumpkin,X), woman(X). no ?-
61Knowledge Base 5
- loves(vincent,mia).
- loves(marsellus,mia).
- loves(pumpkin, honey_bunny).
- loves(honey_bunny, pumpkin).
- jealous(X,Y)- loves(X,Z), loves(Y,Z).
62Knowledge Base 5
- loves(vincent,mia).
- loves(marsellus,mia).
- loves(pumpkin, honey_bunny).
- loves(honey_bunny, pumpkin).
- jealous(X,Y)- loves(X,Z), loves(Y,Z).
?- jealous(marsellus,W).
63Knowledge Base 5
- loves(vincent,mia).
- loves(marsellus,mia).
- loves(pumpkin, honey_bunny).
- loves(honey_bunny, pumpkin).
- jealous(X,Y)- loves(X,Z), loves(Y,Z).
?- jealous(marsellus,W). Wvincent ?-
64Prolog Syntax
- What exactly are facts, rules and queries built
out of?
Terms
Terms
Simple Terms
Complex Terms
Simple Terms
Complex Terms
Constants
Variables
Constants
Variables
Atoms
Numbers
Atoms
Numbers
65Atoms
- A sequence of characters of upper-case letters,
lower-case letters, digits, or underscore,
starting with a lowercase letter - Examples butch, big_kahuna_burger, playGuitar
- An arbitrary sequence of characters enclosed in
single quotes - Examples 'Vincent', 'Five dollar shake',
'_at_' - A sequence of special characters
- Examples , . -
66Numbers
- Integers 12, -34, 22342
- Floats 34573.3234
67Variables
- A sequence of characters of upper-case letters,
lower-case letters, digits, or underscore,
starting with either an uppercase letter or an
underscore - Examples X, Y, Variable, Vincent, _tag
68Complex Terms
- Atoms, numbers and variables are building blocks
for complex terms - Complex terms are built out of a functor directly
followed by a sequence of arguments - Arguments are put in round brackets, separated by
commas - The functor must be an atom
69Examples of complex terms
- Examples we have seen before
- playsAirGuitar(jody)
- loves(vincent, mia)
- jealous(marsellus, W)
- Complex terms inside complex terms
- hide(X,father(father(father(butch))))
70Arity
- The number of arguments a complex term has is
called its arity - Examples woman(mia) is a term with arity
1 loves(vincent,mia) has arity 2
father(father(butch)) arity 1
71Arity is important
- In Prolog you can define two predicates with the
same functor but with different arity - Prolog would treat this as two different
predicates - In Prolog documentation arity of a predicate is
usually indicated with the suffix "/" followed by
a number to indicate the arity
72Example of Arity
happy(yolanda). listens2music(mia). listens2music(
yolanda)- happy(yolanda). playsAirGuitar(mia)-
listens2music(mia). playsAirGuitar(yolanda)-
listens2music(yolanda).
- This knowledge base defines
- happy/1
- listens2music/1
- playsAirGuitar/1
73Summary of this lecture
- Simple examples of Prolog programs
- Introduced three basic constructs in Prolog
- Facts
- Rules
- Queries
- Discussed other concepts, such as
- the role of logic
- unification with the help of variables
- Definition of Prolog constructs terms, atoms,
and variables
74Next Lecture?
- Discuss unification in Prolog
- Prolog's search strategy
75Exercise Witches
76Example Family Tree
77Example Sentence
78Example Crossword
- abalone
- abandon
- enhance
- anagram
- connect
- elegant
79Example Sudoku