#include <stdio.h> main() { int number, square; number = 0; while (number <= 5) { square = number * number; printf("%d\n",square); number = number + 1; } }Semantically identical, except for a small detail:


Let us formalize our problem starting from scratch –a game, to specify things fully.
| "0 is a natural, | and | 1 is a natural, | and | 2 is a natural, | and | " |
| ∧ | ∧ | ∧ |
where " times is adding to itself times" ⟿ we need "times" and "add."
| … | |||
| … |
Generalizing the first line: ""
Generalizing the columns: "if then "
E.g.,
With this we express precisely our specification that " is an output for the program if there exists a natural number such that is the square of and is less than or equal to 5"

| If we ask: | We should obtain: |
|---|---|
| ? | |
| ? | |
| ? | |
| ? | |
| ? | |
| ? | |
| ? | |
| ? | |
| ? | |
(Not even the most advanced AI can escape from them!)
If we add constraints (in place of unification):
→ Constraint Logic Programming (CLP) – also this course!
| and | with unifier | giving | ||||
| and | with unifier | giving | □ |
In pure Logic Programming, using Prolog syntax, we define the naturals, arithmetic operations on them, and the squares of the naturals that are smaller than five:
:- module(_,_,[sr/bfall]). % :- module(_,_,[sr/afall]). % :- module(_,_,[sr/idall]). nat(0). 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) :- mult(X,Y,W), add(W,Y,Z). nat_square(X,Y) :- nat(X), nat(Y), mult(X,X,Y). output(X) :- le(Y,s(s(s(s(s(0)))))), nat(Y), nat_square(Y,X).Some examples of queries:
?- nat(s(0)).
?- nat(X).
?- add(s(0),s(s(0)),X).
?- add(s(0),X,s(s(s(0)))).
?- add(X,Y,s(0)).
?- nat_square(s(s(0)), X).
?- nat_square(X,s(s(s(s(0))))).
?- nat_square(X,Y).
?- output(X).Important observations:
| 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)))) ; ... |
"Prior to our decision to use Prolog for this task, we had implemented custom pattern-matching frameworks over parsers. 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."[ Watson is a question-answering system originally developed in IBM's DeepQA project. It won the first place prize of $1 million in Jeopardy! against the champions Brad Rutter and Ken Jennings. ]
"The short answer is that Prolog allows us to flexibly implement various specification formalisms as well as the analysis and verification tools that go along with it. At the same time, Prolog is very fast..."[ ProB is an animator, constraint solver, and model checker for the B-Method for program verification used by Alstom, ClearSy, Siemens, Thales and several other companies. ]