Computational Logic
A Motivational Introduction

Note: slides with executable links. Follow the   run example \longmapsto  links to execute the example code.

The Program Correctness Problem

A Simple Imperative Program

Natural Language

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


Using Logic

Generating Squares: A Specification (I)

Numbers —we will use “Peano” representation for simplicity:
0 \equiv 0    1 \equiv s(0)    2 \equiv s(s(0))    3 \equiv 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)(yes)
?-X  add(s(0),s(s(0)),X) X=s(s(s(0)))X = s(s(s(0)))
?-X  add(s(0),X,s(s(s(0)))) X=s(s(0))X = s(s(0))
?-X  nat(X) X=0X=s(0)X=s(s(0))X = 0 \vee X = s(0) \vee X = s(s(0)) \vee \ldots
?-X Y  add(X,Y,s(0)) (X=0Y=s(0))(X=s(0)Y=0)(X = 0 \wedge Y=s(0)) \vee (X = s(0) \wedge Y = 0)
?-X  nat_square(s(s(0)), X) X=s(s(s(s(0))))X = s(s(s(s(0))))
?-X  nat_square(X,s(s(s(s(0))))) X=s(s(0))X = s(s(0))
?-X Y  nat_square(X,Y) (X=0Y=0)(X=s(0)Y=s(0))(X=s(s(0))Y=s(s(s(s(0)))))(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
?-X output(X) X=0X=s(0)X=s(s(s(s(0))))X=s9(0)X=s16(0)X=s25(0)X = 0 \ \vee \ X = s(0) \ \vee \ X = s(s(s(s(0)))) \ \vee \ X = s^9(0) \ \vee \ % X = s(s(s(s(s(s(s(s(s(0))))))))) \ \vee \ X = s^{16}(0) \ \vee \ % X = s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(0)))))))))))))))) \ \vee \ X = s^{25}(0) % X = % s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(0)))))))))))))))))))))))))

Which Logic?


Comparison of Logics (I)

Comparison of Logics (II)

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

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

Generating Squares in a Practical Logic Programming System (I)

  run example \longmapsto

:- module(_,_,['sr/bfall']).

nat(s(X)) :- nat(X).

le(0,X) :- nat(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)

 run example \longmapsto

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

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

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

Applications (I)

Applications (II)

The IBM Watson system (from WikipediA):

“Watson is a question-answering computer system capable of answering questions posed in natural language, developed in IBM’s DeepQA project... it competed on Jeopardy! against champions Brad Rutter and Ken Jennings, winning the first place prize of $1 million.”

Adam Lally, John M. Prager, Michael C. McCord, Branimir Boguraev, Siddharth Patwardhan, James Fan, Paul Fodor, Jennifer Chu-Carroll: Question analysis: How Watson reads a clue. IBM J. Res. Dev. 56(3): 2:

“Prior to our decision to use Prolog for this task, we had implemented custom pattern-matching frameworks over parses. These frameworks ended up replicating some of the features of Prolog but lacked the full feature set of Prolog or the efficiency of a good Prolog implementation. Using Prolog for this task has significantly improved our productivity in developing new pattern-matching rules and has delivered the execution efficiency necessary to be competitive in a Jeopardy! game.”