a second life for prolog … · prolog declarative programming jan wielemaker [email protected]...
TRANSCRIPT
![Page 1: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/1.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
1
A second life for Prolog
Declarative programming
This research was partially supported by the VRE4EIC project, a project that has received funding from the European Union's Horizon 2020 research and innovation program under grant agreement No 676247.
![Page 2: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/2.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
2
Sessions
1) Introducing declarative programming and Prolog– Programming paradigms– Basics of Prolog
2) Algorithm = Logic + Control– Advanced control: tabling, constraints, continuations– Data and aggregation
3) Prolog as unifying framework– Accessing the outside world
![Page 3: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/3.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
3
Programming paradigms
![Page 4: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/4.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
4
The Turing Machine
By Wvbailey (talk) (Uploads) - Own work, CC BY-SA 3.0, https://en.wikipedia.org/w/index.php?curid=6917306
![Page 5: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/5.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
5
The universal computing device!
● "The evidence seems to indicate that every algorithm for any computing device has an equivalent Turing machine algorithm ... if [Church's thesis] is true, it is certainly remarkable that Turing machines, with their extremely primitive operations, are capable of performing any computation that any other device can perform, regardless of how complex a device we choose."
(Stone (1972), p. 13)
![Page 6: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/6.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
6
Von Neumann architecture
By Kapooht - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=25789639
![Page 7: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/7.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
7
Imperative Programming
● Imperative: giving an authoritative command● X = Y + Z
– Take the values stored in the memory locations of Y and Z, add them and store the result in the memory location of X.
● X = X + 1– To a mathematician this simply false– A programmer doesn't even see the problem!
● With state everywhere, it gets hard to– Understand the computation– Reorder it computation (exploit concurrency)
![Page 8: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/8.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
8
Just a test ... do { double r = floor(e0/e1);
double e00 = e0, p00 = p0, q00 = q0;volatile double p1_q1;
e0 = e1;p0 = p1;q0 = q1;e1 = e00 - r*e1;p1 = p00 - r*p1;q1 = q00 - r*q1;
p1_q1 = p1/q1;d = p1_q1 - n1->value.f;
} while(fabs(d) > DBL_EPSILON);
![Page 9: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/9.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
9
Functional programming
● A function specifies its (return) value as an expression over its inputs
– There are no more variables storing state– Easier to reason about and execute concurrently
● Functions as primary objects (Lambda expressions)● Lisp (1958), Scheme (1970), Clojure (2007)● Most are hybrid language: they do provide for traditional variables
but discourage their use● JavaScript, Python. Even Java and to some extend C.
![Page 10: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/10.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
10
Logic programming
● Use (predicate) logic to describe the relation between values:
parent(bob, jane).
parent(jan, jane).
mother(X,Y) :- parent(X,Y), female(Y).
father(X,Y) :- parent(X,Y), male(Y).
brother(X,Y) :- parent(X,P), parent(Y,P), male(Y).
![Page 11: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/11.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
11
Twists wrt. logic
● P → Q (P implies Q) is written as Q :- P (Q is true if P is true).
● Q (the head) is only a single atom, i.e., we cannot write– A, B :- P.
![Page 12: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/12.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
12
Some logic based languages
● Prolog (Alain Colmerauer, 1972)– Goal-directed backward chaining (top-down)
● Datalog (~1977)– Forward chaining (bottom-up)
● Mercury (1995)– Avoid need for extra-logical constructs using more declarations
● Picat (2015)– Aim at combinatorial problems
● Answer set programming (1993)– Pure
![Page 13: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/13.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
13
Prolog data
● Constants– bob, 'Bob', 'Nice wheather'– =, ==, <, ... (sequences of glueing punctuation characters)– !, |, … (non-glueing punctuation characters)
● Numbers– 0, 42, 67.3– 7‘012345, 0‘a (97), 0b010101, 0xa0, 0o77
● Strings (SWI, LPA, ECLiPSe)– “abc“ (SWI, ECLiPSe), `abc` (LPA)
● Compounds– age('Bob', 42)
● Variables– X, Myvar, _var
![Page 14: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/14.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
14
Syntactic sugar
● Operators (user extensible):– X = Y ↔ =(X,Y)– Type: fx, fy, xfx, xfy, yfx, xf, yf– Priority (0..1200)
● Lists (In SWI-Prolog . is [|]):– [a,b,c] ↔ .(a, .(b, .(c, [])))– [a,b|C] ↔ .(a, .(b, C))– “ab“ or `ab` (SWI) ↔ .(97,.(98, []))
● Others– { a,b } ↔ {}((a,b)).
![Page 15: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/15.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
15
Logical variables: unification
● X = a● a = X● X = Y● X = Y, Y = a● f(1) = f(X)● 1 = 2● f(X,1) = f(Y,2)
● a = a● a = a● XY = XY● a = a● f(1) = f(1) ● false● false
● T1 = T2, is true if there is a substitution of variables with values that makes the two terms identical (T1 == T2)
![Page 16: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/16.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
16
Logical variables
● After unifying with a non-variable, the variable is gone!– ?- T = f(X), X = 1.– T = f(1).
● Unify-once: – X = 1, X = 2 → false!
![Page 17: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/17.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
17
Observations
● Objects (terms) that do not contain variables are ground.● There is no new. Writing an object (term) creates it. As
terms are immutable one instance suffices.● The program is represented as a term
– We can reason about programs– We can do dynamic programming
![Page 18: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/18.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
18
databases and a queries
● The data– mother(bob, jane).
mother(jan, jane).● Queries
– ?- mother(bob, jane).– true.– ?- mother(bob, jennifer).– false.
![Page 19: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/19.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
19
databases and a queries (cont)
● The data– mother(bob, jane).
mother(jan, jane).● Queries
– ?- mother(X, jane).– X = bob ;– X = jan ;– false.
![Page 20: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/20.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
20
databases and a queries (cont)
● The data– parent(jane, bob). female(jane). male(bob).
parent(jane, jan). male(jan).– parent(jane, mary). female(mary).
● Queries– ?- parent(jane, X), female(X).– X = mary.
![Page 21: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/21.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
21
databases, rules and a queries
● The data– parent(jane, bob). female(jane). male(bob).
parent(jane, jan). male(jan).– parent(jane, mary). female(mary).
● Rule– daughter(Mother, Daughter) :-
parent(Mother, Daughter),
female(Daughter).
● Queries– ?- daughter(X, jane).– X = mary.
![Page 22: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/22.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
22
Exercises
● Please play around with the exercise below:– https://swish.swi-prolog.org/p/ltc_family.swinb –
● All material is linked from– https://swish.swi-prolog.org/p/LTC2017.swinb
![Page 23: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/23.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
23
Einstein‘s Riddle
● Also known as „The zebra puzzle“– https://swish.swi-prolog.org/example/houses_puzzle.pl
1. Five colored houses in a row, each with an owner, a pet, cigarettes, and a drink.
2. The English lives in the red house.
3. The Spanish has a dog.
4. They drink coffee in the green house.
5. The Ukrainian drinks tea.
6. The green house is next to the white house.
7. The Winston smoker has a serpent.
8. In the yellow house they smoke Kool.
9. In the middle house they drink milk.
10. The Norwegian lives in the first house from the left.
11. The Chesterfield smoker lives near the man with the fox.
12. In the house near the house with the horse they smoke Kool.
13. The Lucky Strike smoker drinks juice.
14. The Japanese smokes Kent.
15. The Norwegian lives near the blue house.
![Page 24: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/24.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
24
Five colored houses in a row, each with an owner, a pet, cigarettes, and a
drink.
Houses = [ house(Owner,Pet,Cigarettes,Drink,Color), house(_,_,_,_,_), house(_,_,_,_,_),
house(_,_,_,_,_), house(_,_,_,_,_)
].
![Page 25: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/25.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
25
The English lives in the red house
member(house(english,_,_,_,red), Houses)
member(X, [X|_]).member(X, [_|T]) :- member(X, T).
![Page 26: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/26.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
26
In the middle house they drink milk.
Houses = [_,_,house(_,_,_,milk,_),_,_].
![Page 27: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/27.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
27
The Chesterfield smoker lives near the man with the fox
next(house(_,fox,_,_,_), house(_,_,chesterfield,_,_), Houses)
next(A, B, Ls) :- append(_, [A,B|_], Ls).next(A, B, Ls) :- append(_, [B,A|_], Ls).
![Page 28: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/28.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
28
Exercises
● Map coloring– https://swish.swi-prolog.org/p/ltc_mapcolor.swinb
● Learn Prolog Now! (2.2 Proof Search)– http://lpn.swi-prolog.org/lpnpage.php?pagetype=html&pageid=
lpn-htmlse6
![Page 29: A second life for Prolog … · Prolog Declarative programming Jan Wielemaker J.Wielemaker@cwi.nl This research was partially supported by the VRE4EIC project, a project that has](https://reader030.vdocuments.mx/reader030/viewer/2022041008/5eaf6c69f5a29d613948f19b/html5/thumbnails/29.jpg)
8th
Lang
uage
& T
echn
olog
y C
onfe
renc
eN
ovem
ber
17-1
9, 2
017,
Poz
nań
, P
ola
nd
29