Programming and Computational Logic
A Motivational Introduction
Computational Logic
figure=/home/logalg/public_html/slides/Figs/fig1_english.eps,width=0.6
 Conventional models of using computers  not easy to determine
correctness!
 Has become a very important issue, not just in safetycritical
apps.
 Components with assured quality, being able to give a warranty, ...
 Being able to run untrusted code, certificate carrying code, ...
``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...
 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?
 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, ...).
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:
We can now write a specification of the (imperative) program,
i.e., conditions that we want the program to meet:
 Precondition:
empty.
 Postcondition:
 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, ...).
 Semantics:
 A semantics associates a meaning (a mathematical object)
to a program or program sentence.
 Semantic tasks:
 Verification: proving that a program meets its specification.
 Static debugging: finding where a program does not meet
specifications.
 Program equivalence: proving that two programs have the same
semantics.
 etc.
 Operational:
The meaning of program sentences is
defined in terms of the steps (transformations from state to state)
that computations may take during execution (derivations).
Proofs by induction on derivations.
 Axiomatic:
The meaning of program sentences
is defined indirectly in terms some axioms and rules of some logic
of program properties.
 Denotational (fixpoint):
The meaning of program
sentences is given abstractly as elements of some suitable
mathematical structure (domain).
 Model (declarative) semantics:
The
meaning of programs is given as a minimal model (``logical
meaning'') of the logic that the program is written in.
 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!
Query 
Answer 


? 



? 



? 



? 



? 



? 



? 



? 



? 



 We have already argued the convenience of representing the
problem in logic, but
 which logic?
 propositional
 predicate calculus (first order)
 higherorder logics
 modal logics
 calculus, ...
 which reasoning procedure?
 natural deduction, classical methods
 resolution
 Prawitz/Bibel, tableaux
 bottomup fixpoint
 rewriting
 narrowing, ...
 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. firstorder formulas.
 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., SLDresolution)
classical logic programming!
 Higherorder 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, functionallogic
programming, ...
 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!
 We code the problem as definite (Horn) clauses:
 Query: ?
 In order to refute:
 Resolution:
with
gives
with gives
 Answer:

 $$
 Query:
?
 $$
 In order to refute:
 $$
 Resolution:
with
gives
solved as before
 $$
 Answer:
 $$
 Alternative:
with
gives
: 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).
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)))) ;
...



Last modification: Wed Nov 22 22:58:25 CET 2006 <webmaster@clip.dia.fi.upm.es>