Top Banner
Institut Supérieur de l’Aéronautique et de l’Espace IN112 Mathematical Logic Lab session on Prolog Christophe Garion DMIA – ISAE Christophe Garion IN112 IN112 Mathematical Logic 1/ 31
70

IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

May 10, 2018

Download

Documents

lyxuyen
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Institut Supérieur de l’Aéronautique et de l’Espace

IN112 Mathematical LogicLab session on Prolog

Christophe GarionDMIA – ISAE

Christophe Garion IN112 IN112 Mathematical Logic 1/ 31

Page 2: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

License CC BY-NC-SA 3.0

This work is licensed under the CreativeCommonsAttribution-NonCommercial-ShareAlike 3.0Unported license (CC BY-NC-SA 3.0)

You are free to Share (copy, distribute and transmite) and to Remix (adapt) thiswork under the following conditions:

Attribution – You must attribute the work in the manner specifiedby the author or licensor (but not in any way that suggests thatthey endorse you or your use of the work).Noncommercial – You may not use this work for commercialpurposes.

Share Alike – If you alter, transform, or build upon this work,you may distribute the resulting work only under the same orsimilar license to this one.

See http://creativecommons.org/licenses/by-nc-sa/3.0/.

Christophe Garion IN112 IN112 Mathematical Logic 2/ 31

Page 3: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Outline

1 Prolog: getting started!

2 Interpreting and querying

3 Unification and assignement

4 Lists

5 Negation by failure

6 Misc.

Christophe Garion IN112 IN112 Mathematical Logic 3/ 31

Page 4: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Outline

1 Prolog: getting started!

2 Interpreting and querying

3 Unification and assignement

4 Lists

5 Negation by failure

6 Misc.

Christophe Garion IN112 IN112 Mathematical Logic 4/ 31

Page 5: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Prolog interpreter

Prolog is an interpreted language:

no executable is created (even if it is possible)a Prolog program is loaded into the interpreterqueries are executed on this program through the interpreter

We will use the GNU Prolog interpreter.

Diaz, D. (2013a).GNU Prolog.http://www.gprolog.org.

— (2013b).GNU Prolog Manual.http://www.gprolog.org/manual/html_node/index.html.

Christophe Garion IN112 IN112 Mathematical Logic 5/ 31

Page 6: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Prolog interpreter

Prolog is an interpreted language:

no executable is created (even if it is possible)a Prolog program is loaded into the interpreterqueries are executed on this program through the interpreter

We will use the GNU Prolog interpreter.

Diaz, D. (2013a).GNU Prolog.http://www.gprolog.org.

— (2013b).GNU Prolog Manual.http://www.gprolog.org/manual/html_node/index.html.

Christophe Garion IN112 IN112 Mathematical Logic 5/ 31

Page 7: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Starting GNU Prolog and queries

Starting the interpreter:

shell

c.garion@chabichou# gprologGNU Prolog 1.3.1By Daniel DiazCopyright (C) 1999-2009 Daniel Diaz| ?-

The | ?- prompt waits for a query.

To escape from the interpretor, the halt predicate is used:

|?- halt.

Christophe Garion IN112 IN112 Mathematical Logic 6/ 31

Page 8: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Starting GNU Prolog and queries

Starting the interpreter:

shell

c.garion@chabichou# gprologGNU Prolog 1.3.1By Daniel DiazCopyright (C) 1999-2009 Daniel Diaz| ?-

The | ?- prompt waits for a query.

To escape from the interpretor, the halt predicate is used:

|?- halt.

Christophe Garion IN112 IN112 Mathematical Logic 6/ 31

Page 9: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Prolog syntax: a (concise) overview

Syntax (case)Case is important in Prolog: identifiers beginning by an uppercaseletter are variables names.

Christophe Garion IN112 IN112 Mathematical Logic 7/ 31

Page 10: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Prolog syntax: a (concise) overview

Syntax (case)Case is important in Prolog: identifiers beginning by an uppercaseletter are variables names.

Examples: in p(X,f(Y),a)

p is a predicate nameX and Y are variablenamesf is a function namea is a constant name

. . . but ’P’(’My_Constant’)is also valid!

Christophe Garion IN112 IN112 Mathematical Logic 7/ 31

Page 11: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Prolog syntax: a (concise) overview

Syntax (case)Case is important in Prolog: identifiers beginning by an uppercaseletter are variables names.

Examples: in p(X,f(Y),a)

p is a predicate nameX and Y are variablenamesf is a function namea is a constant name

. . . but ’P’(’My_Constant’)is also valid!

Christophe Garion IN112 IN112 Mathematical Logic 7/ 31

Page 12: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Prolog syntax: a (concise) overview

Syntax (case)Case is important in Prolog: identifiers beginning by an uppercaseletter are variables names.

Examples: in p(X,f(Y),a)

p is a predicate nameX and Y are variablenamesf is a function namea is a constant name

. . . but ’P’(’My_Constant’)is also valid!

N.B.When speaking of a predicate, its arity is given (ancestor/2 for in-stance).

Christophe Garion IN112 IN112 Mathematical Logic 7/ 31

Page 13: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Prolog syntax: a (concise) overview

Syntax (clauses)A clause is written like this:

A :- B1,...,Bn.

where A is the head of the clause and B1,...,Bn its body.When a clause is written A :- true., it simply noted A. Such a clauseis called a fact.Do not forget the “.” at the end!

Christophe Garion IN112 IN112 Mathematical Logic 7/ 31

Page 14: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Prolog syntax: a (concise) overview

Syntax (clauses)A clause is written like this:

A :- B1,...,Bn.

where A is the head of the clause and B1,...,Bn its body.When a clause is written A :- true., it simply noted A. Such a clauseis called a fact.Do not forget the “.” at the end!

Examples:

jack is a parent of maryå parent(jack, mary).

for all X and Y, if there exists a Z such that X is an ancestor of Z andZ is a parent of Y, then X is an ancestor of Z

å ancestor(X, Y) :- ancestor(X, Z), parent(Z, Y).

Christophe Garion IN112 IN112 Mathematical Logic 7/ 31

Page 15: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Prolog syntax: a (concise) overview

Syntax (comments)Comments are written like this:

/* Comments */

Christophe Garion IN112 IN112 Mathematical Logic 7/ 31

Page 16: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Prolog syntax: a (concise) overview

Syntax (comments)Comments are written like this:

/* Comments */

N.B.Types cannot be declared explicitely.

Christophe Garion IN112 IN112 Mathematical Logic 7/ 31

Page 17: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

A Prolog program for ancestors. . .

We will consider the following program:

ancestors.pl

/**************************//* Definition of parent/2 *//**************************/parent(jack, mary).parent(louise, jack).parent(franck, john).

/***************************//* Definition of ancestor/2 *//***************************/ancestor(X, Y) :- parent(X, Y).ancestor(X, Y) :- ancestor(X, Z), parent(Z, Y).

Christophe Garion IN112 IN112 Mathematical Logic 8/ 31

Page 18: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Emacs mode for Prolog

There is a major mode for Prolog in Emacs.

Astrom, A., M. Zamazal, and S. Bruda.EMACS major mode for Prolog.http://turing.ubishops.ca/home/bruda/emacs-prolog/.

Add the following lines into your .emacs:

.emacs

(add-to-list ’load-path "/chemin/vers/repertoire/contenant/le/fichier/")(autoload ’run-prolog "prolog" "Start a Prolog sub-process." t)(autoload ’prolog-mode "prolog" "Major mode for Prolog programs." t)(autoload ’mercury-mode "prolog" "Major mode for Mercury programs." t)(setq prolog-system ’gnu)(setq auto-mode-alist (append ’(("\\.pl$" . prolog-mode)

("\\.m$" . mercury-mode))auto-mode-alist))

This mode provides syntax highlighting + interpreter access.

Christophe Garion IN112 IN112 Mathematical Logic 9/ 31

Page 19: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Emacs mode for Prolog

There is a major mode for Prolog in Emacs.

Astrom, A., M. Zamazal, and S. Bruda.EMACS major mode for Prolog.http://turing.ubishops.ca/home/bruda/emacs-prolog/.

Add the following lines into your .emacs:

.emacs

(add-to-list ’load-path "/chemin/vers/repertoire/contenant/le/fichier/")(autoload ’run-prolog "prolog" "Start a Prolog sub-process." t)(autoload ’prolog-mode "prolog" "Major mode for Prolog programs." t)(autoload ’mercury-mode "prolog" "Major mode for Mercury programs." t)(setq prolog-system ’gnu)(setq auto-mode-alist (append ’(("\\.pl$" . prolog-mode)

("\\.m$" . mercury-mode))auto-mode-alist))

This mode provides syntax highlighting + interpreter access.Christophe Garion IN112 IN112 Mathematical Logic 9/ 31

Page 20: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Easy to use

The command line interpreter allows to:

navigate into queries history with ↑ and ↓dynamically complete a predicate name with −→

From Emacs:

history access with M-p and M-p

a predicate, a region or a buffer can be evaluatedpredicate definitions can be retrievedpredicate template insertion with C-c C-n

. . .

Christophe Garion IN112 IN112 Mathematical Logic 10/ 31

Page 21: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Easy to use

The command line interpreter allows to:

navigate into queries history with ↑ and ↓dynamically complete a predicate name with −→

From Emacs:

history access with M-p and M-p

a predicate, a region or a buffer can be evaluatedpredicate definitions can be retrievedpredicate template insertion with C-c C-n

. . .

Christophe Garion IN112 IN112 Mathematical Logic 10/ 31

Page 22: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Outline

1 Prolog: getting started!

2 Interpreting and querying

3 Unification and assignement

4 Lists

5 Negation by failure

6 Misc.

Christophe Garion IN112 IN112 Mathematical Logic 11/ 31

Page 23: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Interpret a program

From interpreter:

| ?- [’/home/tof/Cours/IN112/exempleProlog/ancestors.pl’].compiling /home/tof/Cours/IN112/exempleProlog/ancetres.pl for byte code.../home/tof/Cours/IN112/exempleProlog/ancetres.pl compiled,12 lines read - 898 bytes written, 62 ms

(2 ms) yes

do not forget the final “.”filename protection with “’’”Prolog answers yes: the predicate consult/1 is evaluated!beware when reloading programs, verify with listing the predicatesdefinitions

From Emacs, open the file and C-c C-b.

Christophe Garion IN112 IN112 Mathematical Logic 12/ 31

Page 24: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Interpret a program

From interpreter:

| ?- [’/home/tof/Cours/IN112/exempleProlog/ancestors.pl’].compiling /home/tof/Cours/IN112/exempleProlog/ancetres.pl for byte code.../home/tof/Cours/IN112/exempleProlog/ancetres.pl compiled,12 lines read - 898 bytes written, 62 ms

(2 ms) yes

do not forget the final “.”filename protection with “’’”Prolog answers yes: the predicate consult/1 is evaluated!beware when reloading programs, verify with listing the predicatesdefinitions

From Emacs, open the file and C-c C-b.

Christophe Garion IN112 IN112 Mathematical Logic 12/ 31

Page 25: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Query evaluation

Query evaluation:

| ?- ancestor(jack,mary).

true ?

The “?” symbol signifies that Prolog waits for a user command:

; to ask for the next solution (backtracking)a to ask for all solutions

RETURN to stop

In our case, after “;”:

Fatal Error: local stack overflow (size: 8192 Kb,environment variable used: LOCALSZ)

Christophe Garion IN112 IN112 Mathematical Logic 13/ 31

Page 26: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Query evaluation

Query evaluation:

| ?- ancestor(jack,mary).

true ?

The “?” symbol signifies that Prolog waits for a user command:

; to ask for the next solution (backtracking)a to ask for all solutions

RETURN to stop

In our case, after “;”:

Fatal Error: local stack overflow (size: 8192 Kb,environment variable used: LOCALSZ)

Christophe Garion IN112 IN112 Mathematical Logic 13/ 31

Page 27: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Query evaluation

Query evaluation:

| ?- ancestor(jack,mary).

true ?

The “?” symbol signifies that Prolog waits for a user command:

; to ask for the next solution (backtracking)a to ask for all solutions

RETURN to stop

In our case, after “;”:

Fatal Error: local stack overflow (size: 8192 Kb,environment variable used: LOCALSZ)

Christophe Garion IN112 IN112 Mathematical Logic 13/ 31

Page 28: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Query evaluation

After having corrected the program:

| ?- ancestor(W,mary).

W = jack ? a

W = louise

no

Evaluation with failure:

| ?- ancestor(john,jack).

no

Christophe Garion IN112 IN112 Mathematical Logic 14/ 31

Page 29: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Query evaluation

After having corrected the program:

| ?- ancestor(W,mary).

W = jack ? a

W = louise

no

Evaluation with failure:

| ?- ancestor(john,jack).

no

Christophe Garion IN112 IN112 Mathematical Logic 14/ 31

Page 30: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Outline

1 Prolog: getting started!

2 Interpreting and querying

3 Unification and assignement

4 Lists

5 Negation by failure

6 Misc.

Christophe Garion IN112 IN112 Mathematical Logic 15/ 31

Page 31: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Arithmetics

Arithmetics is available in GNU Prolog.

+, *, -, /min, max=:=, =/= (cf. next slides)<, >, =<, >=. . .

All those operators are infix.

N.B. (important)The operands of those operators must be evaluable.

Christophe Garion IN112 IN112 Mathematical Logic 16/ 31

Page 32: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Arithmetics

Arithmetics is available in GNU Prolog.

+, *, -, /min, max=:=, =/= (cf. next slides)<, >, =<, >=. . .

All those operators are infix.

N.B. (important)The operands of those operators must be evaluable.

Christophe Garion IN112 IN112 Mathematical Logic 16/ 31

Page 33: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignment, equality: try it!

Unification operator: =

X = Y.X = Y, f(Y) = Z.f(X) = g(Y).f(X) \= g(a).

Assignment operator: is

X is 2.X is (2 + 3).X is Y.Y = 2, X is Y.

Christophe Garion IN112 IN112 Mathematical Logic 17/ 31

Page 34: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignment, equality: try it!

Unification operator: =

X = Y.X = Y, f(Y) = Z.f(X) = g(Y).f(X) \= g(a).

Assignment operator: is

X is 2.X is (2 + 3).X is Y.Y = 2, X is Y.

Christophe Garion IN112 IN112 Mathematical Logic 17/ 31

Page 35: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignment, equality: try it!

Unification operator: =

X = Y.X = Y, f(Y) = Z.f(X) = g(Y).f(X) \= g(a).

Terms equality operator: ==

X == X.X == Y.X \== Y.2 == (1 + 1).

Assignment operator: is

X is 2.X is (2 + 3).X is Y.Y = 2, X is Y.

Christophe Garion IN112 IN112 Mathematical Logic 17/ 31

Page 36: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignment, equality: try it!

Unification operator: =

X = Y.X = Y, f(Y) = Z.f(X) = g(Y).f(X) \= g(a).

Terms equality operator: ==

X == X.X == Y.X \== Y.2 == (1 + 1).

Assignment operator: is

X is 2.X is (2 + 3).X is Y.Y = 2, X is Y.

Arithmetic equality: =:=

2 =:= 2.2 =:= (1 + 1).2 =:= 3.2 =\= 3.

Christophe Garion IN112 IN112 Mathematical Logic 17/ 31

Page 37: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

QuestionHow to define a fact/2 predicate which represents the factorial func-tion?

Using logic:

Factorial definition

fact(0, 1)∧∀x∀y fact(x − 1, y)→ fact(x , y ∗ x)

Christophe Garion IN112 IN112 Mathematical Logic 18/ 31

Page 38: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

QuestionHow to define a fact/2 predicate which represents the factorial func-tion?

Using logic:

Factorial definition

fact(0, 1)∧∀x∀y fact(x − 1, y)→ fact(x , y ∗ x)

Christophe Garion IN112 IN112 Mathematical Logic 18/ 31

Page 39: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

fact-1.pl

fact(0,1).fact(N,Y * N) :- fact(N - 1,Y).

Christophe Garion IN112 IN112 Mathematical Logic 19/ 31

Page 40: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

fact-1.pl

fact(0,1).fact(N,Y * N) :- fact(N - 1,Y).

Trying with 0!:

| ?- fact(0,X).

X = 1 ? ;

Fatal Error: global stack overflow (size: 32768 Kb,environment variable used: GLOBALSZ)

Christophe Garion IN112 IN112 Mathematical Logic 19/ 31

Page 41: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

fact-1.pl

fact(0,1).fact(N,Y * N) :- fact(N - 1,Y).

Explanation (with trace., remove trace mode with notrace.):

1 1 Redo: fact(0,1) ?2 2 Call: fact(0-1,_48) ?3 3 Call: fact(0-1-1,_78) ?4 4 Call: fact(0-1-1-1,_108) ?5 5 Call: fact(0-1-1-1-1,_138) ?...

Christophe Garion IN112 IN112 Mathematical Logic 19/ 31

Page 42: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

fact-2.pl

fact(0,1).fact(N,Y * N) :- N > 0, fact(N - 1, Y).

Christophe Garion IN112 IN112 Mathematical Logic 20/ 31

Page 43: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

fact-2.pl

fact(0,1).fact(N,Y * N) :- N > 0, fact(N - 1, Y).

Trying 0!:

| ?- fact(0,X).

X = 1 ? ;

no

Christophe Garion IN112 IN112 Mathematical Logic 20/ 31

Page 44: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

fact-2.pl

fact(0,1).fact(N,Y * N) :- N > 0, fact(N - 1, Y).

Trying 3!:

| ?- fact(3,X).

no

Christophe Garion IN112 IN112 Mathematical Logic 20/ 31

Page 45: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

fact-2.pl

fact(0,1).fact(N,Y * N) :- N > 0, fact(N - 1, Y).

Explanation (with trace.):

1 1 Call: fact(3,_16) ?2 2 Call: 3>0 ?2 2 Exit: 3>0 ?3 2 Call: fact(3-1,_48) ?4 3 Call: 3-1>0 ?4 3 Exit: 3-1>0 ?...8 5 Call: 3-1-1-1>0 ?8 5 Fail: 3-1-1-1>0 ?7 4 Fail: fact(3-1-1-1,_158) ?5 3 Fail: fact(3-1-1,_103) ?3 2 Fail: fact(3-1,_48) ?1 1 Fail: fact(3,_16) ?

Christophe Garion IN112 IN112 Mathematical Logic 20/ 31

Page 46: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

fact-2.pl

fact(0,1).fact(N,Y * N) :- N > 0, fact(N - 1, Y).

Explanation (why fact(0,1) is not used):

| ?- 3-1-1-1 = 0.

no

Christophe Garion IN112 IN112 Mathematical Logic 20/ 31

Page 47: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

fact-3.pl

fact(0,1).fact(N, Y * N) :- N > 0, M = N - 1, fact(M, Y).

Christophe Garion IN112 IN112 Mathematical Logic 21/ 31

Page 48: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

fact-3.pl

fact(0,1).fact(N, Y * N) :- N > 0, M = N - 1, fact(M, Y).

Trying 3!:

| ?- fact(3,X).fact(3,X).

no

Christophe Garion IN112 IN112 Mathematical Logic 21/ 31

Page 49: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

fact-3.pl

fact(0,1).fact(N, Y * N) :- N > 0, M = N - 1, fact(M, Y).

Explanation (with trace.):

1 1 Call: fact(3,_16) ?2 2 Call: 3>0 ?2 2 Exit: 3>0 ?3 2 Call: fact(3-1,_48) ?4 3 Call: 3-1>0 ?4 3 Exit: 3-1>0 ?...8 5 Fail: 3-1-1-1>0 ?7 4 Fail: fact(3-1-1-1,_158) ?5 3 Fail: fact(3-1-1,_103) ?3 2 Fail: fact(3-1,_48) ?1 1 Fail: fact(3,_16) ?

Christophe Garion IN112 IN112 Mathematical Logic 21/ 31

Page 50: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

fact-3.pl

fact(0,1).fact(N, Y * N) :- N > 0, M = N - 1, fact(M, Y).

Explanation (why fact(0,1) is not used):

| ?- M = 3 - 1.M = 3-1

yes

Christophe Garion IN112 IN112 Mathematical Logic 21/ 31

Page 51: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

fact-4.pl

fact(0,1).fact(N, Y * N) :- N > 0, M is N - 1, fact(M, Y).

Trying 3!:

| ?- fact(3,X).

X = 1*1*2*3 ? ;

no

fact-5.pl (finally!)

fact(0,1).fact(N, Y) :- N > 0, M is N - 1, fact(M, X), Y is X * N.

Christophe Garion IN112 IN112 Mathematical Logic 22/ 31

Page 52: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

fact-4.pl

fact(0,1).fact(N, Y * N) :- N > 0, M is N - 1, fact(M, Y).

Trying 3!:

| ?- fact(3,X).

X = 1*1*2*3 ? ;

no

fact-5.pl (finally!)

fact(0,1).fact(N, Y) :- N > 0, M is N - 1, fact(M, X), Y is X * N.

Christophe Garion IN112 IN112 Mathematical Logic 22/ 31

Page 53: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Unification, assignement: example

fact-4.pl

fact(0,1).fact(N, Y * N) :- N > 0, M is N - 1, fact(M, Y).

Trying 3!:

| ?- fact(3,X).

X = 1*1*2*3 ? ;

no

fact-5.pl (finally!)

fact(0,1).fact(N, Y) :- N > 0, M is N - 1, fact(M, X), Y is X * N.

Christophe Garion IN112 IN112 Mathematical Logic 22/ 31

Page 54: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Outline

1 Prolog: getting started!

2 Interpreting and querying

3 Unification and assignement

4 Lists

5 Negation by failure

6 Misc.

Christophe Garion IN112 IN112 Mathematical Logic 23/ 31

Page 55: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Lists representation

Lists are basic data structures.

Building a new list by induction:

Syntax (list)

empty list: []

or the list is composed of:an element, the head of the lista list, the tail of the list

The list is then represented by [head | tail].

Christophe Garion IN112 IN112 Mathematical Logic 24/ 31

Page 56: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Lists representation

Lists are basic data structures.

Building a new list by induction:

Syntax (list)

empty list: []

or the list is composed of:an element, the head of the lista list, the tail of the list

The list is then represented by [head | tail].

A list containing known elements is represented with the “,”: [a, b, c].

Christophe Garion IN112 IN112 Mathematical Logic 24/ 31

Page 57: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Lists representation

Lists are basic data structures.

Building a new list by induction:

Syntax (list)

empty list: []

or the list is composed of:an element, the head of the lista list, the tail of the list

The list is then represented by [head | tail].

Examples to evaluate:

[T | Q] = [a, b, c].[T, Q] = [a, b, c].[T, Q, R] = [a, b, c].[T | Q] = [a].[T] = [a].

Christophe Garion IN112 IN112 Mathematical Logic 24/ 31

Page 58: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Operations on lists

Avalaible operations on lists:

member/2 (e.g. member(X, [a,b,c]).)append/3 (e.g. append([d, e], [a,b,c], X).)reverse/2

last/2

length/2

sort/2

. . .

Christophe Garion IN112 IN112 Mathematical Logic 25/ 31

Page 59: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Outline

1 Prolog: getting started!

2 Interpreting and querying

3 Unification and assignement

4 Lists

5 Negation by failure

6 Misc.

Christophe Garion IN112 IN112 Mathematical Logic 26/ 31

Page 60: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Negation by failure

Negation does not exist in Prolog: only Horn clauses are used!

Syntax (negation by failure)\+ (goal)

This operator represents the fact that Prolog cannot prove goal.

Try the following examples:

\+(X == Y).\+(member(a, [b,c])).

Christophe Garion IN112 IN112 Mathematical Logic 27/ 31

Page 61: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Negation by failure

Negation does not exist in Prolog: only Horn clauses are used!

Syntax (negation by failure)\+ (goal)

This operator represents the fact that Prolog cannot prove goal.

Try the following examples:

\+(X == Y).\+(member(a, [b,c])).

Christophe Garion IN112 IN112 Mathematical Logic 27/ 31

Page 62: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Negation by failure

Negation does not exist in Prolog: only Horn clauses are used!

Syntax (negation by failure)\+ (goal)

This operator represents the fact that Prolog cannot prove goal.

Try the following examples:

\+(X == Y).\+(member(a, [b,c])).

Christophe Garion IN112 IN112 Mathematical Logic 27/ 31

Page 63: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

NBF: the ancestors example

Add the following predicate: X is not a direct ancestor of Y if X is anancestor of Y and X is not a parent of Y.

ancestor.pl

not_direct_ancestor(X, Y) :- ancestor(X, Y), \+(parent(X, Y)).

Try it!

Christophe Garion IN112 IN112 Mathematical Logic 28/ 31

Page 64: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

NBF: the ancestors example

Add the following predicate: X is not a direct ancestor of Y if X is anancestor of Y and X is not a parent of Y.

ancestor.pl

not_direct_ancestor(X, Y) :- ancestor(X, Y), \+(parent(X, Y)).

Try it!

Christophe Garion IN112 IN112 Mathematical Logic 28/ 31

Page 65: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

NBF is not logical negation!

In classical logic:

|= A ∧ ¬B ↔ ¬B ∧ A

. . . but not with NBF!

Try the following definition for not_direct_ancestor:

ancestor.pl

not_direct_ancestor(X, Y) :- \+(parent(X, Y)), ancestor(X, Y).

Christophe Garion IN112 IN112 Mathematical Logic 29/ 31

Page 66: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

NBF is not logical negation!

In classical logic:

|= A ∧ ¬B ↔ ¬B ∧ A

. . . but not with NBF!

Try the following definition for not_direct_ancestor:

ancestor.pl

not_direct_ancestor(X, Y) :- \+(parent(X, Y)), ancestor(X, Y).

Christophe Garion IN112 IN112 Mathematical Logic 29/ 31

Page 67: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Outline

1 Prolog: getting started!

2 Interpreting and querying

3 Unification and assignement

4 Lists

5 Negation by failure

6 Misc.

Christophe Garion IN112 IN112 Mathematical Logic 30/ 31

Page 68: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Misc.

listing is used to obtain a predicate definition or all program predicatesdefinition.

ex: listing(ancestor).

Normally, a predicate should be defined contiguously. You can add thefollowing line at the beginning of the file:

:- discontiguous(pred/n).

where pred is the predicate.

If a predicate is defined in more than one file, you should add the followingin all files preambles:

:- multifile(pred/n).

Christophe Garion IN112 IN112 Mathematical Logic 31/ 31

Page 69: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Misc.

listing is used to obtain a predicate definition or all program predicatesdefinition.

ex: listing(ancestor).

Normally, a predicate should be defined contiguously. You can add thefollowing line at the beginning of the file:

:- discontiguous(pred/n).

where pred is the predicate.

If a predicate is defined in more than one file, you should add the followingin all files preambles:

:- multifile(pred/n).

Christophe Garion IN112 IN112 Mathematical Logic 31/ 31

Page 70: IN112 Mathematical Logic Lab session on Prolog · Lab session on Prolog Christophe Garion DMIA – ISAE ... . Christophe Garion IN112 IN112 Mathematical Logic 5/ 31.

Misc.

listing is used to obtain a predicate definition or all program predicatesdefinition.

ex: listing(ancestor).

Normally, a predicate should be defined contiguously. You can add thefollowing line at the beginning of the file:

:- discontiguous(pred/n).

where pred is the predicate.

If a predicate is defined in more than one file, you should add the followingin all files preambles:

:- multifile(pred/n).

Christophe Garion IN112 IN112 Mathematical Logic 31/ 31