Programming and Computational Logic
A Motivational Introduction

Course General Topic

Computational Logic

figure=/home/logalg/public_html/slides/Figs/fig1_english.eps,width=0.6

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)
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:

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:

Use of 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, ...).

• Semantics:
• A semantics associates a meaning (a mathematical object) to a program or program sentence.

• 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.

Styles of Semantics

• 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.

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 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!

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).

mult(0,Y,0) <- nat(Y).