Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are...

51
PROLOG Programing in Logic Benoit Viguier @b_viguier Afup Lyon 13/12/2017

Transcript of Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are...

Page 1: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

PROLOGPrograming in Logic

Benoit Viguier@b_viguier

Afup Lyon 13/12/2017

Page 2: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

1972197219721972

Page 3: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

http://www.swi-prolog.org/

Page 4: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns
Page 5: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

?- length([a,b,c],3). true.

?- length([a,b,c],2). false.

Page 6: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

?- length([a,b,c],3). true.

?- length([a,b,c],2). false.

End of instruction

Result

Page 7: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

Unification

Page 8: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

?- length([a,b,c],S). S = 3.

?- length(L,3). L = [_642, _648, _654].

Page 9: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

?- length([a,b,c],S). S = 3.

?- length(L,3). L = [_642, _648, _654].

Variableaxiome

Singleton Variable

Page 10: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

?- length(L,S). L = [], S = 0 L = [_676], S = 1 ; L = [_676, _682], S = 2 ; L = [_676, _682, _688], S = 3 ; L = [_676, _682, _688, _694], S = 4 .

Page 11: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

Backtracking

Page 12: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

?- length(L,S). L = [], S = 0 ; L = [_676], S = 1 ; L = [_676, _682], S = 2 ; L = [_676, _682, _688], S = 3 ; L = [_676, _682, _688, _694], S = 4 .

Page 13: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

?- length(L,S). L = [], S = 0 ; L = [_676], S = 1 ; L = [_676, _682], S = 2 ; L = [_676, _682, _688], S = 3 ; L = [_676, _682, _688, _694], S = 4 .

Alternative

End

Page 14: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

Creating predicats

Page 15: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

swipl -f myfile.pl ? make.

Page 16: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

language(php). language(cplusplus). language(prolog).

?- language(php). true.

?- language(MyVar). MyVar = php ; MyVar = cplusplus ; MyVar = prolog.

Page 17: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

language(php). language(cplusplus). language(prolog).

?- language(js). false.

?- language(💩). false.

Page 18: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

fruit(🍎). fruit(🍌). fruit(🍉). fruit(🍍).

?- fruit(🍎). true.

?- fruit(🚗). false.

?- fruit(F). F = 🍎 ; F = 🍌 .

Page 19: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

Rules

Page 20: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

smoothie([A,B],2) :- fruit(A), fruit(B), A @> B.

?- smoothie(L,2). L = [🍎, 🍌] ; L = [🍎, 🍉] ; L = [🍎, 🍍] ; L = [🍌, 🍉] ; L = [🍍, 🍌] ; L = [🍍, 🍉] ; false.

Page 21: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

smoothie([A,B],2) :- fruit(A), fruit(B), A @> B.

?- smoothie(L,2). L = [🍎, 🍌] ; L = [🍎, 🍉] ; L = [🍎, 🍍] ; L = [🍌, 🍉] ; L = [🍍, 🍌] ; L = [🍍, 🍉] ; false.

« Is implied by… »

term comparison

Page 22: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

smoothie([A,B],2) :- fruit(A), fruit(B), A @> B.

?- smoothie(L,N). L = [🍎, 🍌], N = 2 ; L = [🍎, 🍉], N = 2 ; L = [🍎, 🍍], N = 2 ; L = [🍌, 🍉], N = 2 ; L = [🍍, 🍌], N = 2 ; L = [🍍, 🍉], N = 2 ; false.

Page 23: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

Recursion

Page 24: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

smoothie([F1|S],N):- fruit(F1), length([F1|S], N), Nm1 is N-1, smoothie(S, Nm1), [F2|_] = S, F1 @> F2.

?- smoothie(L,3). L = [🍎, 🍌, 🍉] ; L = [🍎, 🍍, 🍌] ; L = [🍎, 🍍, 🍉] ; L = [🍍, 🍌, 🍉] ; false.

Page 25: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

smoothie([F1|S],N):- fruit(F1), length([F1|S], N), Nm1 is N-1, smoothie(S, Nm1), [F2|_] = S, F1 @> F2.

?- smoothie(L,3). L = [🍎, 🍌, 🍉] ; L = [🍎, 🍍, 🍌] ; L = [🍎, 🍍, 🍉] ; L = [🍍, 🍌, 🍉] ; false.

Head | Tail

Anonymous variable

Page 26: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

smoothie([F1|S],N):- fruit(F1), length([F1|S], N), Nm1 is N-1, smoothie(S, Nm1), [F2|_] = S, F1 @> F2.

?- smoothie(L,3). L = [🍎, 🍌, 🍉] ; L = [🍎, 🍍, 🍌] ; L = [🍎, 🍍, 🍉] ; L = [🍍, 🍌, 🍉] ; false.

Page 27: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

smoothie([F1|S],N):- fruit(F1), length([F1|S], N), Nm1 is N-1, smoothie(S, Nm1), [F2|_] = S, F1 @> F2.

?- smoothie(L,4). L = [🍎, 🍍, 🍌, 🍉] ; false.

?- smoothie(L,5). false.

Page 28: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

Einstein's Puzzleor Zebra Puzzle (1962)

Page 29: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

• There are five houses.• The Englishman lives in the red house.• The Spaniard owns the dog.• Coffee is drunk in the green house.• The Ukrainian drinks tea.• The green house is immediately to the right of the ivory house.• The Old Gold smoker owns snails.• Kools are smoked in the yellow house.• Milk is drunk in the middle house.• The Norwegian lives in the first house.• The man who smokes Chesterfields lives in the house next to the

man with the fox.• Kools are smoked in the house next to the house where the horse is

kept.• The Lucky Strike smoker drinks orange juice.• The Japanese smokes Parliaments.• The Norwegian lives next to the blue house.

Now, who drinks water? Who owns the zebra?

Page 30: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

It is often claimed that only 2% of the population can solve the

puzzle.

Page 31: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

Solutionwith Emojis 😁

Page 32: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

[(, 🐍, 🍓, 🍼, 🚗],

House = List

Page 33: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

einstein(X):- length(X,5), member([(,_,_,_,🚗], X), member([,,🐶,_,_,_], X), member([_,_,_,🍺,🚲], X), member([0,_,_,🍵,_], X), nextto([_,_,_,_,🚲], [_,_,_,_,🚐], X), member([_,🐍,🍓,_,_], X), member([_,_,🍋,_,🚕], X), X = [_,_,[_,_,_,🍼,_],_,_], X = [[5,_,_,_,_],_,_,_,_], next([_,_,🍍,_,_], [_,🐺,_,_,_], X), next([_,_,🍋,_,_], [_,🐴,_,_,_], X), member([_,_,🍇,🍹,_], X), member([:,_,🍒,_,_], X), nextto([5,_,_,_,_], [_,_,_,_,🚙], X), member([_,_,_,🍷,_], X), member([_,🐘,_,_,_], X).

Page 34: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

length(X,5)

Five houses.

Page 35: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

member([(,_,_,_,🚗],X)

The Englishman owns the red car.

Page 36: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

member([(,_,_,_,🚗],X) member([,,🐶,_,_,_],X) member([_,_,_,🍺,🚲],X) member([0,_,_,🍵,_],X) member([_,🐍,🍓,_,_],X) member([_,_,🍋,_,🚕],X) member([_,_,🍇,🍹,_],X) member([:,_,🍒,_,_],X)

Page 37: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

nextto([_,_,_,_,🚲],[_,_,_,_,🚐], X)

The bicycle owner lives immediately to the right of the white car owner.

Page 38: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

nextto([5,_,_,_,_], [_,_,_,_,🚙], X)

The Norwegian lives next to the blue car.

Page 39: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

next([_,_,🍍,_,_], [_,🐺,_,_,_], X)

The man who eats pineapples lives in the house next to the man with the fox.

Page 40: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

next(X,Y,L) :- nextto(X,Y,L), !.

next(X,Y,L) :- nextto(Y,X,L).

?- next(b,c,[a,b,c]). true.

?- next(c,b,[a,b,c]). true ; false.

Page 41: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

next([_,_,🍋,_,_], [_,🐴,_,_,_], X)

Lemons are eaten in the house next to the house where the horse is kept.

Page 42: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

X = [_,_,[_,_,_,🍼,_],_,_]

Milk is drunk in the middle house.

Page 43: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

X = [[5,_,_,_,_],_,_,_,_]

The Norwegian lives in the first house.

Page 44: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

member([_,_,_,🍷,_], X)

Who drinks wine?

Page 45: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

member([_,🐘,_,_,_], X)

Who owns the elephant?

Page 46: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

einstein(X):- length(X,5), member([(,_,_,_,🚗], X), member([,,🐶,_,_,_], X), member([_,_,_,🍺,🚲], X), member([0,_,_,🍵,_], X), nextto([_,_,_,_,🚲], [_,_,_,_,🚐], X), member([_,🐍,🍓,_,_], X), member([_,_,🍋,_,🚕], X), X = [_,_,[_,_,_,🍼,_],_,_], X = [[5,_,_,_,_],_,_,_,_], next([_,_,🍍,_,_], [_,🐺,_,_,_], X), next([_,_,🍋,_,_], [_,🐴,_,_,_], X), member([_,_,🍇,🍹,_], X), member([:,_,🍒,_,_], X), nextto([5,_,_,_,_], [_,_,_,_,🚙], X), member([_,_,_,🍷,_], X), member([_,🐘,_,_,_], X).

Page 47: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

?- einstein(X). X = [ [5, 🐺, 🍋, 🍷, 🚕], [0, 🐴, 🍍, 🍵, 🚙], [(, 🐍, 🍓, 🍼, 🚗], [:, 🐘, 🍒, 🍺, 🚲], [,, 🐶, 🍇, 🍹, 🚐]

] ; false.

Page 48: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

Try it!http://www.swi-prolog.org/

Page 49: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

Thanks@b_viguier

Page 50: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

?- [X|L] = [a,b,c]. X = a, L = [b, c].

?- [X|L] = [a]. X = a, L = [].

?- [X|L] = []. false.

Page 51: Prolog - b-viguier.github.iob-viguier.github.io/downloads/talks/AfupLyon-Prolog.pdf · •There are five houses. • The Englishman lives in the red house. • The Spaniard owns

next(X,Y,L) :- nextto(X,Y,L), !.

next(X,Y,L) :- nextto(Y,X,L).

?- next(b,c,[a,b,c]). true.

?- next(c,b,[a,b,c]). true ; false.

Cut