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

## The Program Correctness Problem

• Conventional models of using computers - not easy to determine correctness!
• Has become a very important issue, not just in safety-critical apps.

• Components with assured quality, being able to give a warranty, ...

• Being able to run untrusted code, certificate carrying code, ...

## A Simple Imperative Program

• Example:
#include <stdio.h>
main() {
int Number, Square;
Number = 0;
while(Number <= 5)
{ Square = Number * Number;
printf("%d\n",Square);
Number = Number + 1; } }


• Is it correct? With respect to what?
• A suitable formalism:
• to provide specifications (describe problems), and
• to reason about the correctness of programs (their implementation).
is needed.

## Natural Language

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

Ideal at first sight, but:

• verbose
• vague
• ambiguous
• needs context (assumed information)
• ...

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

## Logic

• A means of clarifying / formalizing the human thought process

• Logic for example tells us that (classical logic)
Aristotle likes cookies, and
Plato is a friend of anyone who likes cookies

imply that
Plato is a friend of Aristotle
• Symbolic logic:
A shorthand for classical logic - plus many useful results:

• But, can logic be used:
• To represent the problem (specifications)?
• Even perhaps to solve the problem?

## Using Logic

• For expressing specifications and reasoning about the correctness of programs we need:
• Specification languages (assertions), modeling, ...
• Program semantics (models, axiomatic, fixpoint, ...).
• Proofs: program verification (and debugging, equivalence, ...).

## Generating Squares: A Specification (I)

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

• Defining the natural numbers:

• A better solution:

• Order on the naturals:

• Addition of naturals:

## Generating Squares: A Specification (II)

• Multiplication of naturals:

• Squares of the naturals:

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

• Precondition:
empty.
• Postcondition:

## Alternative Use of Logic?

• So, logic allows us to represent problems (program specifications).

i.e., the process of implementing solutions to problems.

• The importance of Programming Languages (and tools).
• Interesting question: can logic help here too?

## From Representation/Specification to Computation

• Assuming the existence of a mechanical proof method (deduction procedure)
a new view of problem solving and computing is possible [Greene]:
• program once and for all the deduction procedure in the computer,
• find a suitable representation for the problem (i.e., the specification),
• then, to obtain solutions, ask questions and let deduction procedure do rest:

• No correctness proofs needed!

## Computing With Our Previous Description / Specification

 Query Answer ? ? ? ? ? ? ? ? ?

## Which Logic?

• We have already argued the convenience of representing the problem in logic, but

• which logic?
• propositional
• predicate calculus (first order)
• higher-order logics
• modal logics
• -calculus, ...
• which reasoning procedure?
• natural deduction, classical methods
• resolution
• Prawitz/Bibel, tableaux
• bottom-up fixpoint
• rewriting
• narrowing, ...

## Issues

• We try to maximize expressive power.

• But one of the main issues is whether we have an effective reasoning procedure.

• It is important to understand the underlying properties and the theoretical limits!

• Example: propositions vs. first-order formulas.
• Propositional logic:

spot is a dog'' 		 p
dogs have tail'' 		 q

but how can we conclude that Spot has a tail?

• Predicate logic extends the expressive power of propositional logic:



now, using deduction we can conclude:




## Comparison of Logics (I)

• Propositional logic:

SPMgt;spot is a dog''		 p
+ decidability/completeness
- limited expressive power
+ practical deduction mechanism

circuit design, answer set'' programming, ...
• Predicate logic: (first order)

SPMgt;spot is a dog''		 dog(spot)
+/- decidability/completeness
+/- good expressive power
+ practical deduction mechanism (e.g., SLD-resolution)

classical logic programming!

## Comparison of Logics (II)

• Higher-order predicate logic:

SPMgt;There is a relationship for spot''		 X(spot)
- decidability/completeness
+ good expressive power
- practical deduction mechanism

But interesting subsets HO logic programming, functional-logic prog., ...
• Other logics: decidability? Expressive power? Practical deduction mechanism?
Often (very useful) variants of previous ones:
• Predicate logic + constraints (in place of unification)
constraint programming!
• Propositional temporal logic, etc.
• Interesting case: -calculus

+ similar to predicate logic in results, allows higher order
- does not support predicates (relations), only functions

functional programming!

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

• We code the problem as definite (Horn) clauses:

• Query: ?

• In order to refute:
• Resolution:
with gives
with gives

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

$$Query: ?$$
In order to refute:
$$Resolution: with gives solved as before$$
$$Alternative: with gives ## 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) ?- nat(s(0)). ?- add(s(0),s(s(0)),X). Query Answer yes X = s(s(s(0))) X = s(s(0)) X = 0 ; X = s(0) ; X = s(s(0)) ; ... (X = 0 , Y=s(0)) ; (X = s(0) , Y = 0) X = s(s(s(s(0)))) X = s(s(0)) (X = 0 , Y=0) ; (X = s(0) , Y=s(0)) ; (X = s(s(0)) , Y=s(s(s(s(0))))) ; ... 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). 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

• Declarative view:
• Suppose there is a functor such that represents a list with head and tail .
• Membership definition:
• Using logic:

• Using Prolog:
member(X, f(X, T)).
member(X, f(Z, T)) :- member(X,T).

• Procedural view (but for checking membership only!):
• Traverse the list comparing each element until is found or list is finished
/* Testing array membership in C */
int member(int x,int list[LISTSIZE]) {
for (int i = 0; i < LISTSIZE; i++)
if (x == list[i]) return TRUE;
return FALSE;
}


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

• 60's
• Greene: problem solving.
• Robinson: linear resolution.

• 70's
• (early) Kowalski: procedural interpretation of Horn clause logic. Read:
if and and and as:
to solve (execute) , solve (execute) and and,...,

• (early) Colmerauer: specialized theorem prover (Fortran) embedding the procedural interpretation: Prolog (Programmation et Logique).

• In the U.S.: next-generation AI languages'' of the time (i.e. planner) seen as inefficient and difficult to control.

• (late) D.H.D. Warren develops DEC-10 Prolog compiler, almost completely written in Prolog. Very efficient (same as LISP). Very useful control builtins.

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

• Late 80's, 90's
• Major research in the basic paradigms and advanced implementation techniques: Japan (Fifth Generation Project), US (MCC), Europe (ECRC, ESPRIT projects).
• Numerous commercial Prolog implementations, programming books, and a de facto standard, the Edinburgh Prolog family.
• First parallel and concurrent logic programming systems.
• CLP - Constraint Logic Programming: Major extension - many new applications areas.
• 1995: ISO Prolog standard.

## Currently

• Many commercial CLP systems with fielded applications.
• Extensions to full higher order, inclusion of functional programming, ...
• Highly optimizing compilers, automatic parallelism, automatic debugging.
• Concurrent constraint programming systems.
• Distributed systems.
• Object oriented dialects.
• Applications
• Natural language processing
• Scheduling/Optimization problems
• AI related problems
• (Multi) agent systems programming.
• Program analyzers
• ...

Last modification: Wed Mar 21 12:47:35 CET 2007 <webmaster@clip.dia.fi.upm.es>