next_inactive up previous






A Motivational Introduction to Computational Logic
and (Constraint) Logic Programming





Computational Logic

\psfig{figure=/home/logalg/public_html/slides/Figs/fig1_english.eps,width=0.6\basewidth}

The Program Correctness Problem

\psfig{figure=/home/logalg/public_html/slides/Figs/logic_use_1.eps,width=0.40\basewidth}

A Simple Imperative Program

Natural Language

``Compute the squares of the natural numbers which are less or equal than 5.''

Ideal at first sight, but:

Philosophers and Mathematicians already pointed this out a long time ago...

Logic

Using Logic

\psfig{figure=/home/logalg/public_html/slides/Figs/logic_use_2a.eps,width=0.38\basewidth}

Generating Squares: A Specification (I)

Numbers --we will use ``Peano'' representation for simplicity:
0 $\rightarrow$ 0 1 $\rightarrow$ s(0) 2 $\rightarrow$ s(s(0)) 3 $\rightarrow$ s(s(s(0))) ...

Generating Squares: A Specification (II)


We can now write a specification of the (imperative) program, i.e., conditions that we want the program to meet:

Alternative Use of Logic?

From Representation/Specification to Computation


Computing With Our Previous Description / Specification

Query Answer
   
$nat(s(0))$ ? $(yes)$
   
$\exists X  add(s(0),s(s(0)),X)$ ? $X = s(s(s(0)))$
   
$\exists X  add(s(0),X,s(s(s(0))))$ ? $X = s(s(0))$
   
$\exists X  nat(X)$ ? $X = 0 \vee X = s(0) \vee X = s(s(0)) \vee \ldots$
   
$\exists X \exists Y  add(X,Y,s(0))$ ? $(X = 0 \wedge Y=s(0)) \vee (X = s(0) \wedge Y = 0)$
   
$\exists X  nat\_square(s(s(0)), X)$ ? $X = s(s(s(s(0))))$
   
$\exists X  nat\_square(X,s(s(s(s(0)))))$ ? $X = s(s(0))$
   
$\exists X \exists Y  nat\_square(X,Y)$ ? $ (X = 0 \wedge Y=0) \vee
(X = s(0) \wedge Y=s(0)) \vee
(X = s(s(0)) \wedge Y=s(s(s(s(0))))) \vee
\ldots $
   
$\exists X output(X)$ ? $ X = 0  \vee \
X = s(0)  \vee \
X = s(s(s(s(0))))  \vee \
X = s^9(0)  \vee \
X = s^{16}(0)  \vee \
X = s^{25}(0)
$
   

Which Logic?

Issues

Comparison of Logics (I)

Comparison of Logics (II)



Generating squares by SLD-Resolution - Logic Programming (I)

Generating squares by SLD-Resolution - Logic Programming (II)

$nat(0)$
$\neg nat(X) \vee nat(s(X))$
$\neg nat(X) \vee add(0, X, X))$
$\neg add(X,Y,Z) \vee add(s(X),Y,s(Z))$
$\neg nat(X) \vee mult(0, X, 0)$
$\neg mult(X,Y,W) \vee \neg add(W,Y,Z) \vee mult(s(X),Y,Z)$ $\neg nat(X) \vee \neg nat(Y) \vee \neg mult(X,X,Y)
\vee nat\_square(X,Y)$
 $$
Query: $\exists X \exists Y  add(X,Y,s(0))$ ?

 $$
In order to refute: $\neg add(X,Y,s(0))$
 $$
Resolution:
$\neg add(X,Y,s(0))$ with $\neg nat(X) \vee add(0, X, X))$ gives $\neg nat(s(0))$
$\neg nat(s(0))$ solved as before
 $$
Answer: $X=0, Y=s(0)$
 $$
Alternative:
$\neg add(X,Y,s(0))$ with $\neg add(X,Y,Z) \vee add(s(X),Y,s(Z))$ gives $\neg add(X,Y,0)$

Generating Squares in a Practical Logic Programming System (I)


:- module(_,_,['bf/af']).

nat(0) <- .
nat(s(X)) <- nat(X).

le(0,_X) <- .
le(s(X),s(Y)) <- le(X,Y).

add(0,Y,Y) <- nat(Y).
add(s(X),Y,s(Z)) <- add(X,Y,Z).

mult(0,Y,0) <- nat(Y).
mult(s(X),Y,Z) <- add(W,Y,Z), mult(X,Y,W).

nat_square(X,Y) <- nat(X), nat(Y), mult(X,X,Y).

output(X) <- nat(Y), le(Y,s(s(s(s(s(0)))))), nat_square(Y,X).

Generating Squares in a Practical Logic Programming System (II)

Query Answer
   
?- nat(s(0)). yes
   
?- add(s(0),s(s(0)),X). X = s(s(s(0)))
   
?- add(s(0),X,s(s(s(0)))). X = s(s(0))
   
?- nat(X). X = 0 ; X = s(0) ; X = s(s(0)) ; ...
   
?- add(X,Y,s(0)). (X = 0 , Y=s(0)) ; (X = s(0) , Y = 0)
   
?- nat_square(s(s(0)), X). X = s(s(s(s(0))))
   
?- nat_square(X,s(s(s(s(0))))). X = s(s(0))
   
?- nat_square(X,Y). (X = 0 , Y=0) ; (X = s(0) , Y=s(0)) ; (X = s(s(0)) , Y=s(s(s(s(0))))) ; ...
   
?- output(X). X = 0 ; X = s(0) ; X = s(s(s(s(0)))) ; ...
   

Introductory example (I) - Family relations

$father\_of(john, peter)$
$father\_of(john, mary)$
$father\_of(peter, michael)$
$mother\_of(mary, david)$
$\forall X\forall Y(\exists Z (father\_of(X,Z) \wedge father\_of(Z,Y))
\rightarrow grandfather\_of(X,Y))$
$\forall X\forall Y(\exists Z (father\_of(X,Z) \wedge mother\_of(Z,Y))
\rightarrow grandfather\_of(X,Y))$

father_of(john, peter).
father_of(john, mary).
father_of(peter, michael).
mother_of(mary, david).

grandfather_of(L,M) :- father_of(L,K),
                       father_of(K,M).

grandfather_of(X,Y) :- father_of(X,Z),
                       mother_of(Z,Y).
figure=/home/logalg/public_html/slides/Figs/family_1.eps,width=0.45

 $$
How can grandmother_of/2 be represented?
 $$
What does grandfather_of(X,david) mean? And grandfather_of(john,X)?

Introductory example (II) - Testing membership in lists


A (very brief) History of Logic Programming (I)

A (very brief) History of Logic Programming (II)

Currently


next_inactive up previous
Last modification: Wed Mar 21 12:47:35 CET 2007 <webmaster@clip.dia.fi.upm.es>[CLIP] [FIM] [UNM]