Transcript Chapter 1

Introduction
• Logic programming languages, sometimes
called declarative programming languages
• Express programs in a form of symbolic
logic
• Use a logical inferencing process to
produce results
• Declarative rather that procedural:
– Only specification of results are stated (not
detailed procedures for producing them)
1-1
Proposition
• A logical statement that may or may not be
true
– Consists of objects and relationships of objects
to each other
1-2
Symbolic Logic
• Logic which can be used for the basic needs
of formal logic:
– Express propositions
– Express relationships between propositions
– Describe how new propositions can be inferred
from other propositions
• Particular form of symbolic logic used for
logic programming called predicate calculus
1-3
Forms of a Proposition
• Propositions can be stated in two forms:
– Fact: proposition is assumed to be true
– Query: truth of proposition is to be determined
• Compound proposition:
– Have two or more atomic propositions
– Propositions are connected by operators
1-4
Logical Operators
Name
Symbol
Example
Meaning
negation

a
not a
conjunction

ab
a and b
disjunction

ab
a or b
equivalence

ab
implication


ab
ab
a is equivalent
to b
a implies b
b implies a
1-5
Quantifiers
Name
Example
Meaning
universal
X.P
For all X, P is true
existential
X.P
There exists a value of X
such that P is true
1-6
Overview of Logic Programming
• Declarative semantics
– There is a simple way to determine the meaning
of each statement
– Simpler than the semantics of imperative
languages
• Programming is nonprocedural
– Programs do not state now a result is to be
computed, but rather the form of the result
1-7
The Origins of Prolog
• University of Aix-Marseille
– Natural language processing
• University of Edinburgh
– Automated theorem proving
1-8
Example Problem
We are given three colors – red, yellow and
blue – with which to color all the countries on
a map. The colors must be chosen so that no
two countries that share a border have the
same color. The shapes of the countries are
arbitrary, but a country must consist of a
single piece. Countries that meet a corner
are not considered a border.
Is it possible?
1-9
Three Color Mapping Problem
Not always possible to solve
1-10
Three Color Mapping Problem
• From the viewpoint of logic, it is easy to set down
the parameters of the problem, that is, to assert
what properties a solution, if it exist, must have.
• Take the map on the right, our solution should be
a list of colors, A, B, C, D, E, F such that
–
–
–
–
–
A is different from B, C, D and F
B is different from C, E
C is different from D and E
D is different from E
E is different from F
1-11
Three Color Mapping Problem
• To render the problem in Prolog, we must first
state the fact that the colors red, yellow and blue
are different, and assert the conditions just stated
about this particular map.
different(yellow, red)
different(yellow, blue)
different(blue, yellow)
different(blue, red)
different(red, yellow)
different(red, blue)
1-12
Three Color Mapping Problem
->map-coloring(A, B, C, D, E, F):different(A, B), different(A, C),
different(A, D), different(A, F),
different(B, C), different(B, E),
different(C, E), different(C, D),
different(D, E), different(E, F)
yellow blue red blue yellow blue
Satisfied
1-13
Execution of Prolog Programs
•
•
•
•
Prove that goal is satisfiable
Search of facts/rules is top-down
Execution of sub-goals is left to right
Closed-world assumption:
– Anything not in database is false
1-14
Applications of Prolog
•
•
•
•
•
Relational database queries
Expert systems
Parsing of context-free languages
Natural language processing
Teaching programming
1-15
Other Examples
ancestor(mary,shelley):mother(mary,shelley).
• Can use variables (universal objects) to
generalize meaning:
parent(X,Y):- mother(X,Y).
parent(X,Y):- father(X,Y).
grandparent(X,Z):- parent(X,Y),
parent(Y,Z).
sibling(X,Y):- mother(M,X), mother(M,Y),
father(F,X), father(F,Y).
1-16
Goal Statements
• Can you prove or disprove the following
statements?
Fact:
father(fred,Mike)
Query:
parent(fred,Mike)
man(fred)
1-17
Backtracking
• With a goal with multiple subgoals, if fail to
show truth of one of subgoals, reconsider
previous subgoal to find an alternative
solution: backtracking
• Begin search where previous search left off
• Can take lots of time and space because
may find all possible proofs to every
subgoal
1-18
List Structures
• Other basic data structure (besides atomic
propositions we have already seen): list
• List is a sequence of any number of elements
• Elements can be atoms, atomic propositions,
or other terms (including other lists)
[apple, prune, grape, kumquat]
[]
(empty list)
[X | Y]
(head X and tail Y)
1-19
Append Example
append([], List, List).
append([Head | List_1], List_2,
[Head | List_3]) :append (List_1, List_2, List_3).
1-20
Reverse Example
reverse([], []).
reverse([Head | Tail], List) :reverse (Tail, Result),
append (Result, [Head], List).
1-21