Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Programmation en Logique (LambdaDays edition)

Lars Hupel
February 21, 2019

Programmation en Logique (LambdaDays edition)

Prolog is one of the most underrated programming languages around; possibly because of its strange syntax and the unusual paradigm. But it is a very nice special-purpose programming language. In this talk, I would like to introduce Prolog’s programming model, showcase some programming domains in which Prolog allows for very concise, elegant programs, and finally describe how it can also be used as a general-purpose tool.

Lars Hupel

February 21, 2019
Tweet

More Decks by Lars Hupel

Other Decks in Programming

Transcript

  1. P r o g r a m m a t

    i o n e n L o g i q u e L a r s H u p e l L a m b d a D a y s 2 0 1 9 - 0 2 - 2 1
  2. W h o i n v e n t e

    d P r o l o g ? “ 1 . T h e w o r l d i s e v e r y t h i n g t h a t i s t h e c a s e . 1 .1 T h e w o r l d i s t h e t o t a l i t y o f f a c t s , n o t o f t h i n g s . 1 .1 1 T h e w o r l d i s d e t e r m i n e d b y t h e f a c t s , a n d b y t h e s e b e i n g a l l t h e f a c t s . ”
  3. W h o i n v e n t e

    d P r o l o g ? “ 1 . T h e w o r l d i s e v e r y t h i n g t h a t i s t h e c a s e . 1 .1 T h e w o r l d i s t h e t o t a l i t y o f f a c t s , n o t o f t h i n g s . 1 .1 1 T h e w o r l d i s d e t e r m i n e d b y t h e f a c t s , a n d b y t h e s e b e i n g a l l t h e f a c t s . ” – L u d w i g W i t t g e n s t e i n , 1 9 1 8
  4. W h o i n v e n t e

    d P r o l o g ? • a p p e a r e d i n t h e e a r l y 7 0 s i n F r a n c e • o r i g i n a l d e v e l o p e r s : A l a i n C o l m e r a u e r a n d P h i l i p p e R o u s s e l • u s e d t h e .pl e x t e n s i o n b e f o r e P e r l • r a d i c a l l y d i f f e r e n t p r o g r a m m i n g p a r a d i g m
  5. A b r i e f p r i m

    e r o n P r o l o g 1 . P r o l o g p r o g r a m s a r e s e q u e n c e s o f r u l e s ( o r c l a u s e s ) .
  6. A b r i e f p r i m

    e r o n P r o l o g 1 . P r o l o g p r o g r a m s a r e s e q u e n c e s o f r u l e s ( o r c l a u s e s ) . 2 . R u l e s c a n h a v e a r g u m e n t s .
  7. A b r i e f p r i m

    e r o n P r o l o g 1 . P r o l o g p r o g r a m s a r e s e q u e n c e s o f r u l e s ( o r c l a u s e s ) . 2 . R u l e s c a n h a v e a r g u m e n t s . 3 . R u l e s c a n h a v e c o n d i t i o n s .
  8. A b r i e f p r i m

    e r o n P r o l o g 1 . P r o l o g p r o g r a m s a r e s e q u e n c e s o f r u l e s ( o r c l a u s e s ) . 2 . R u l e s c a n h a v e a r g u m e n t s . 3 . R u l e s c a n h a v e c o n d i t i o n s . 4 . P r o g r a m s c a n b e q u e r i e d .
  9. A b r i e f p r i m

    e r o n P r o l o g 1 . P r o l o g p r o g r a m s a r e s e q u e n c e s o f r u l e s ( o r c l a u s e s ) . 2 . R u l e s c a n h a v e a r g u m e n t s . 3 . R u l e s c a n h a v e c o n d i t i o n s . 4 . P r o g r a m s c a n b e q u e r i e d . 5 . A n y t h i n g t h a t i s n o t i n t h e p r o g r a m i s n o t t r u e .
  10. A b r i e f p r i m

    e r o n P r o l o g 1 . P r o l o g p r o g r a m s a r e s e q u e n c e s o f r u l e s ( o r c l a u s e s ) . 2 . R u l e s c a n h a v e a r g u m e n t s . 3 . R u l e s c a n h a v e c o n d i t i o n s . 4 . P r o g r a m s c a n b e q u e r i e d . 5 . A n y t h i n g t h a t i s n o t i n t h e p r o g r a m i s n o t t r u e . 6 . Q u e r i e s m a y a l t e r t h e p r o g r a m
  11. A b r i e f p r i m

    e r o n P r o l o g 1 . P r o l o g p r o g r a m s a r e s e q u e n c e s o f r u l e s ( o r c l a u s e s ) . 2 . R u l e s c a n h a v e a r g u m e n t s . 3 . R u l e s c a n h a v e c o n d i t i o n s . 4 . P r o g r a m s c a n b e q u e r i e d . 5 . A n y t h i n g t h a t i s n o t i n t h e p r o g r a m i s n o t t r u e . 6 . Q u e r i e s m a y a l t e r t h e p r o g r a m J u s t l i k e i n S Q L !
  12. H e l l o W o r l d

    ! P r o g r a m hi.
  13. H e l l o W o r l d

    ! P r o g r a m hi. I n t e r p r e t e r ?- hi. true.
  14. H e l l o W o r l d

    ! P r o g r a m hi. I n t e r p r e t e r ?- hi. true. hello(world).
  15. H e l l o W o r l d

    ! P r o g r a m hi. I n t e r p r e t e r ?- hi. true. hello(world). ?- hello(world). true.
  16. H e l l o W o r l d

    ! P r o g r a m hi. I n t e r p r e t e r ?- hi. true. hello(world). ?- hello(world). true. ?- hello(coworld). false.
  17. H e l l o W o r l d

    ! P r o g r a m hi. I n t e r p r e t e r ?- hi. true. hello(world). ?- hello(world). true. ?- hello(coworld). false. T h i s u s e d t o b e y e s / n o , f o r 1 0 0 % t o d d l e r c o m - p a t i b i l i t y
  18. H e l l o W o r l d

    ! P r o g r a m hi. I n t e r p r e t e r ?- hi. true. hello(world). ?- hello(world). true. ?- hello(coworld). false. ?- hello(X). X = world.
  19. H e l l o W o r l d

    ! P r o g r a m hi. I n t e r p r e t e r ?- hi. true. hello(world). ?- hello(world). true. ?- hello(coworld). false. ?- hello(X). X = world. V a r i a b l e s : u p p e r - c a s e R e s t : l o w e r - c a s e
  20. A s m a l l p r o g

    r a m F a c t s location(munich, germany). location(augsburg, germany). location(germany, europe). location(london, unitedkingdom). location(unitedkingdom, europe).
  21. A s m a l l p r o g

    r a m F a c t s location(munich, germany). location(augsburg, germany). location(germany, europe). location(london, unitedkingdom). location(unitedkingdom, europe). EU Customs Union Council of Europe European Union EEA BSEC Eurozone Benelux Baltic Assembly EFTA GUAM CEFTA Schengen Area Union State Nordic Council Visegrád Group Common Travel Area Monetary agreement with the EU
  22. A s m a l l p r o g

    r a m F a c t s location(munich, germany). location(augsburg, germany). location(germany, europe). location(london, unitedkingdom). location(unitedkingdom, europe).
  23. A s m a l l p r o g

    r a m F a c t s location(munich, germany). location(augsburg, germany). location(germany, europe). location(london, unitedkingdom). location(unitedkingdom, europe). R u l e s neighbour(X, Y) :- location(X, Z), location(Y, Z).
  24. A s m a l l p r o g

    r a m F a c t s location(munich, germany). location(augsburg, germany). location(germany, europe). location(london, unitedkingdom). location(unitedkingdom, europe). R u l e s is_in(X, Y) :- location(X, Y). is_in(X, Y) :- location(X, Z), is_in(Z, Y).
  25. P r o l o g s y n t

    a x W h a t ’s w i t h t h e w e i r d s y n t a x ?
  26. P r o l o g s y n t

    a x W h a t ’s w i t h t h e w e i r d s y n t a x ? I s i t s t o l e n f r o m E r l a n g ?
  27. P r o l o g s y n t

    a x W h a t ’s w i t h t h e w e i r d s y n t a x ? I s i t s t o l e n f r o m E r l a n g ? H e l l o , A l a i n !
  28. P r o l o g s y n t

    a x W h a t ’s w i t h t h e w e i r d s y n t a x ? I s i t s t o l e n f r o m E r l a n g ? H e l l o , J o e !
  29. E r l a n g , i n s

    p i r e d b y P r o l o g “ T h e f i r s t i n t e r p r e t e r w a s a s i m p l e P r o l o g m e t a i n t e r p r e t e r w h i c h a d d e d t h e n o t i o n o f a s u s p e n d a b l e p r o c e s s t o P r o l o g . . . [ i t ] w a s r a p i d l y m o d i f i e d ( a n d r e - w r i t t e n ) . . . ” – A r m s t r o n g , V i r d i n g , W i l l i a m s : U s e o f P r o l o g f o r d e v e l o p i n g a n e w p r o g r a m m i n g l a n g u a g e
  30. B a c k t r a c k i

    n g best_boy(X) :- dog(good, X), colour(dark_brown, X), behind(X, Y), colour(light_brown, Y).
  31. B a c k t r a c k i

    n g best_boy(X) :- dog(good, X), colour(dark_brown, X), behind(X, Y), colour(light_brown, Y).
  32. B a c k t r a c k i

    n g best_boy(X) :- dog(good, X), colour(dark_brown, X), behind(X, Y), colour(light_brown, Y).
  33. B a c k t r a c k i

    n g best_boy(X) :- dog(good, X), colour(dark_brown, X), behind(X, Y), colour(light_brown, Y).
  34. B a c k t r a c k i

    n g best_boy(X) :- dog(good, X), colour(dark_brown, X), behind(X, Y), colour(light_brown, Y).
  35. B a c k t r a c k i

    n g best_boy(X) :- dog(good, X), colour(dark_brown, X), behind(X, Y), colour(light_brown, Y).
  36. B a c k t r a c k i

    n g best_boy(X) :- dog(good, X), colour(dark_brown, X), behind(X, Y), colour(light_brown, Y).
  37. B a c k t r a c k i

    n g best_boy(X) :- dog(good, X), colour(dark_brown, X), behind(X, Y), colour(light_brown, Y).
  38. B a c k t r a c k i

    n g best_boy(X) :- dog(good, X), colour(dark_brown, X), behind(X, Y), colour(light_brown, Y).
  39. B a c k t r a c k i

    n g best_boy(X) :- dog(good, X), colour(dark_brown, X), behind(X, Y), colour(light_brown, Y).
  40. B i - d i r e c t i

    o n a l c o m p u t i n g f : I → O
  41. B i - d i r e c t i

    o n a l c o m p u t i n g f : I → O
  42. B i - d i r e c t i

    o n a l c o m p u t i n g f : I → O R : (I × O ) → {0 , 1 }
  43. B i - d i r e c t i

    o n a l c o m p u t i n g S c a l a def append[A](xs: List[A], ys: List[A]): List[A]
  44. B i - d i r e c t i

    o n a l c o m p u t i n g S c a l a def append[A](xs: List[A], ys: List[A]): List[A] P r o l o g append(?List1, ?List2, ?List1AndList2)
  45. B i - d i r e c t i

    o n a l c o m p u t i n g S c a l a def appendAll[A](xss: List[List[A]]): List[A]
  46. B i - d i r e c t i

    o n a l c o m p u t i n g S c a l a def appendAll[A](xss: List[List[A]]): List[A] P r o l o g append_all(+ListOfLists, ?List)
  47. N o t a s i l v e r

    b u l l e t . . . ?- member(X, [1, 2, 3]), Y = 2, X > Y. X = 3, Y = 2.
  48. N o t a s i l v e r

    b u l l e t . . . ?- member(X, [1, 2, 3]), Y = 2, X > Y. X = 3, Y = 2. ?- X > Y, member(X, [1, 2, 3]), Y = 2.
  49. N o t a s i l v e r

    b u l l e t . . . ?- member(X, [1, 2, 3]), Y = 2, X > Y. X = 3, Y = 2. ?- X > Y, member(X, [1, 2, 3]), Y = 2.
  50. N o t a s i l v e r

    b u l l e t . . . ?- member(X, [1, 2, 3]), Y = 2, X > Y. X = 3, Y = 2. ?- use_module(library(clpfd)). ?- X #> Y, X in 1..3, Y = 2.
  51. C o n s t r a i n t

    s o l v i n g P u z z l e T h e r e a r e f i v e h o u s e s . 1 . T h e E n g l i s h p e r s o n l i v e s i n t h e r e d h o u s e . 2 . T h e S w e d i s h p e r s o n o w n s a d o g . 3 . T h e D a n i s h p e r s o n l i k e s t o d r i n k t e a . 4 . T h e g r e e n h o u s e i s l e f t t o t h e w h i t e h o u s e . 5 . T h e o w n e r o f t h e g r e e n h o u s e d r i n k s c o f f e e . 6 . . . .
  52. G r a m m a r s s -->

    np, vp. np --> d, n. d --> [the]. d --> [a]. vp --> v, np. n --> [dog]. n --> [bone].
  53. P r o l o g i s f o

    r p a r s i n g ? “ T h e p r o g r a m m i n g l a n g u a g e . . . w a s b o r n o f a p r o j e c t a i m e d n o t a t p r o d u c i n g a p r o g r a m m i n g l a n g u a g e b u t a t p r o c e s s i n g n a t u r a l l a n - g u a g e s ; i n t h i s c a s e , F r e n c h . ” – C o l m e r a u e r , R o u s s e l : T h e B i r t h o f P r o l o g
  54. P r o l o g i s f o

    r p a r s i n g ? “ T h e p r o g r a m m i n g l a n g u a g e . . . w a s b o r n o f a p r o j e c t a i m e d n o t a t p r o d u c i n g a p r o g r a m m i n g l a n g u a g e b u t a t p r o c e s s i n g n a t u r a l l a n - g u a g e s ; i n t h i s c a s e , F r e n c h . ” – C o l m e r a u e r , R o u s s e l : T h e B i r t h o f P r o l o g
  55. P a r s i n g S c a

    l a type Parser[A] = String => List[(A, String)]
  56. P a r s i n g S c a

    l a type Parser[A] = String => List[(A, String)] P r o l o g parse(?A, ?ListIn, ?ListOut)
  57. P a r s i n g S c a

    l a type Parser[A] = String => List[(A, String)] + m o n a d s y n t a x P r o l o g parse(?A, ?ListIn, ?ListOut)
  58. P a r s i n g S c a

    l a type Parser[A] = String => List[(A, String)] + m o n a d s y n t a x P r o l o g parse(?A, ?ListIn, ?ListOut) + D C G s y n t a x
  59. Q & A L a r s H u p

    e l � l a r s . h u p e l @ i n n o q . c o m � @ l a r s r _ h w w w . i n n o q . c o m i n n o Q D e u t s c h l a n d G m b H K r i s c h e r s t r . 1 0 0 4 0 7 8 9 M o n h e i m a . R h . G e r m a n y + 4 9 2 1 7 3 3 3 6 6 - 0 O h l a u e r S t r . 4 3 1 0 9 9 9 B e r l i n G e r m a n y L u d w i g s t r . 1 8 0 E 6 3 0 6 7 O f f e n b a c h G e r m a n y K r e u z s t r . 1 6 8 0 3 3 1 M ü n c h e n G e r m a n y i n n o Q S c h w e i z G m b H G e w e r b e s t r . 1 1 C H - 6 3 3 0 C h a m S w i t z e r l a n d + 4 1 4 1 7 4 3 0 1 1 1 A l b u l a s t r . 5 5 8 0 4 8 Z ü r i c h S w i t z e r l a n d
  60. L A R S H U P E L C

    o n s u l t a n t i n n o Q D e u t s c h l a n d G m b H L a r s e n j o y s p r o g r a m m i n g i n a v a r i e t y o f l a n - g u a g e s , i n c l u d i n g S c a l a , H a s k e l l , a n d R u s t . H e i s k n o w n a s a f r e q u e n t c o n f e r e n c e s p e a k e r a n d o n e o f t h e f o u n d e r s o f t h e T y p e l e v e l i n i t i a t i v e w h i c h i s d e d i c a t e d t o p r o v i d i n g p r i n c i p l e d , t y p e - d r i v e n S c a l a l i b r a r i e s .
  61. I m a g e s o u r c

    e s • P r o l o g c o f f e e : M a r e k K u b i c a • S h i b a r o w : https://www.pinterest.de/pin/424112489894679416/ • S h i b a w i t h m l e m : https://www.reddit.com/r/mlem/comments/6tc1of/shibe_doing_a_mlem/ • H a p p y d o g : https://www.rover.com/blog/is-my-dog-happy/ • K i d w i t h c r o s s e d a r m s : https: //www.psychologytoday.com/us/blog/spycatcher/201410/9-truths-exposing-myth-about-body-language • N o a m C h o m s k y : https://en.wikipedia.org/wiki/File:Noam_Chomsky_Toronto_2011.jpg • A l a i n C o l m e r a u e r : https://de.wikipedia.org/wiki/Datei:A-Colmerauer_web-800x423.jpg • J o e A r m s t r o n g : E r l a n g , t h e M o v i e • S i g n a t u r e s : http://www.swi-prolog.org/pldoc/man?section=preddesc • Z e b r a p u z z l e : S t a c k O v e r f l o w c o n t r i b u t o r s ( https://stackoverflow.com/q/11122814/4776939) • O w l : https://www.theloop.ca/angry-owl-terrorizes-oregon-joggers/