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

Programmation en Clojique

Lars Hupel
February 29, 2020

Programmation en Clojique

Prolog is this weird old language that established the paradigm of logic programming. Once heralded as the solution to all AI problems, it is now considered a niche language. Luckily, many languages have adopted ideas from Prolog, such as Clojure with core.logic. In this talk, I would like to introduce Prolog’s programming model and showcase some programming domains in which it allows for very concise, elegant programs.

Lars Hupel

February 29, 2020
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 C l o g i q u e
    L a r s H u p e l
    : c l o j u r e D
    2 0 2 0 - 0 2 - 2 9

    View Slide

  2. (defne presentation [orateur]
    ([:lars]
    (fresh [questions]
    (blague :drole) #_(rire)
    (introduction :corelogic)
    (fonctions :cool)
    (audience questions)
    (repondre questions))))

    View Slide

  3. P r o g r a m m a t i o n
    e n C l o g i q u e
    L a r s H u p e l
    : c l o j u r e D
    2 0 2 0 - 0 2 - 2 9

    View Slide

  4. (defne presentation [speaker]
    ([:lars]
    (fresh [questions]
    (joke :funny) #_(laugh)
    (introduction :corelogic)
    (features :cool)
    (audience questions)
    (answer questions))))

    View Slide

  5. (defne presentation [speaker]
    ([:lars]
    (fresh [questions]
    (joke :funny) #_(laugh)
    (introduction :corelogic)
    (features :cool)
    (audience questions)
    (answer questions))))

    View Slide

  6. W h o i n v e n t e d l o g i c
    p r o g r a m m i n 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 .

    View Slide

  7. W h o i n v e n t e d l o g i c
    p r o g r a m m i n 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

    View Slide

  8. (defne presentation [speaker]
    ([:lars]
    (fresh [questions]
    (joke :funny) #_(laugh)
    (introduction :corelogic)
    (features :cool)
    (audience questions)
    (answer questions))))

    View Slide

  9. 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
    • t h i s t a l k : u s i n g c o r e . l o g i c s y n t a x

    View Slide

  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 ) .

    View Slide

  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 .

    View Slide

  12. 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 .

    View Slide

  13. 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 .

    View Slide

  14. 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 .

    View Slide

  15. 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

    View Slide

  16. H e l l o W o r l d !
    P r o g r a m
    (defn hi [])

    View Slide

  17. H e l l o W o r l d !
    P r o g r a m
    (defn hi [])
    I n t e r p r e t e r
    => (run* (hi))
    (_0)

    View Slide

  18. H e l l o W o r l d !
    P r o g r a m
    (defn hi [])
    I n t e r p r e t e r
    => (run* (hi))
    (_0)
    (defn hello [x]
    (== x :world))

    View Slide

  19. H e l l o W o r l d !
    P r o g r a m
    (defn hi [])
    I n t e r p r e t e r
    => (run* (hi))
    (_0)
    (defn hello [x]
    (== x :world))
    => (run* [_] (hello :world))
    (_0)

    View Slide

  20. H e l l o W o r l d !
    P r o g r a m
    (defn hi [])
    I n t e r p r e t e r
    => (run* (hi))
    (_0)
    (defn hello [x]
    (== x :world))
    => (run* [_] (hello :world))
    (_0)
    => (run* [_] (hello :coworld))
    ()

    View Slide

  21. H e l l o W o r l d !
    P r o g r a m
    (defn hi [])
    I n t e r p r e t e r
    => (run* (hi))
    (_0)
    (defn hello [x]
    (== x :world))
    => (run* [_] (hello :world))
    (_0)
    => (run* [_] (hello :coworld))
    ()

    View Slide

  22. H e l l o W o r l d !
    P r o g r a m
    (defn hi [])
    I n t e r p r e t e r
    => (run* (hi))
    (_0)
    (defn hello [x]
    (== x :world))
    => (run* [_] (hello :world))
    (_0)
    => (run* [_] (hello :coworld))
    ()
    => (run* [x] (hello x))
    (:world)

    View Slide

  23. A s m a l l p r o g r a m
    F a c t s
    (defne location [place in]
    ([:munich :germany])
    ([:augsburg :germany])
    ([:germany :europe])
    ([:london :unitedkingdom])
    ([:unitedkingdom :europe]))

    View Slide

  24. A s m a l l p r o g r a m
    F a c t s
    (defne location [place in]
    ([:munich :germany])
    ([:augsburg :germany])
    ([:germany :europe])
    ([:london :unitedkingdom])
    ([: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

    View Slide

  25. A s m a l l p r o g r a m
    F a c t s
    (defne location [place in]
    ([:munich :germany])
    ([:augsburg :germany])
    ([:germany :europe])
    ([:london :unitedkingdom])
    ([:unitedkingdom :europe]))

    View Slide

  26. A s m a l l p r o g r a m
    F a c t s
    (defne location [place in]
    ([:munich :germany])
    ([:augsburg :germany])
    ([:germany :europe])
    ([:london :unitedkingdom])
    ([:unitedkingdom :europe]))
    R u l e s
    (defn neighbour [x y]
    (fresh [z]
    (location x z)
    (location y z)))

    View Slide

  27. A s m a l l p r o g r a m
    F a c t s
    (defne location [place in]
    ([:munich :germany])
    ([:augsburg :germany])
    ([:germany :europe])
    ([:london :unitedkingdom])
    ([:unitedkingdom :europe]))
    R u l e s
    (defn is-in [x y]
    (conde
    [(location x y)]
    [(fresh (z)
    (location x z)
    (is-in z y))]))

    View Slide

  28. (defne presentation [speaker]
    ([:lars]
    (fresh [questions]
    (joke :funny) #_(laugh)
    (introduction :corelogic)
    (features :cool)
    (audience questions)
    (answer questions))))

    View Slide

  29. B a c k t r a c k i n g
    (defn best-boy [x]
    (fresh [y]
    (dog :good x)
    (colour :dark_brown x)
    (behind x y)
    (colour :light_brown y)))

    View Slide

  30. B a c k t r a c k i n g
    (defn best-boy [x]
    (fresh [y]
    (dog :good x)
    (colour :dark_brown x)
    (behind x y)
    (colour :light_brown y)))

    View Slide

  31. B a c k t r a c k i n g
    (defn best-boy [x]
    (fresh [y]
    (dog :good x)
    (colour :dark_brown x)
    (behind x y)
    (colour :light_brown y)))

    View Slide

  32. B a c k t r a c k i n g
    (defn best-boy [x]
    (fresh [y]
    (dog :good x)
    (colour :dark_brown x)
    (behind x y)
    (colour :light_brown y)))

    View Slide

  33. B a c k t r a c k i n g
    (defn best-boy [x]
    (fresh [y]
    (dog :good x)
    (colour :dark_brown x)
    (behind x y)
    (colour :light_brown y)))

    View Slide

  34. B a c k t r a c k i n g
    (defn best-boy [x]
    (fresh [y]
    (dog :good x)
    (colour :dark_brown x)
    (behind x y)
    (colour :light_brown y)))

    View Slide

  35. B a c k t r a c k i n g
    (defn best-boy [x]
    (fresh [y]
    (dog :good x)
    (colour :dark_brown x)
    (behind x y)
    (colour :light_brown y)))

    View Slide

  36. B a c k t r a c k i n g
    (defn best-boy [x]
    (fresh [y]
    (dog :good x)
    (colour :dark_brown x)
    (behind x y)
    (colour :light_brown y)))

    View Slide

  37. B a c k t r a c k i n g
    (defn best-boy [x]
    (fresh [y]
    (dog :good x)
    (colour :dark_brown x)
    (behind x y)
    (colour :light_brown y)))

    View Slide

  38. B a c k t r a c k i n g
    (defn best-boy [x]
    (fresh [y]
    (dog :good x)
    (colour :dark_brown x)
    (behind x y)
    (colour :light_brown y)))

    View Slide

  39. 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

    View Slide

  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

    View Slide

  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
    R
    : (I
    × O
    ) → {0
    , 1
    }

    View Slide

  42. B i - d i r e c t i o n a l c o m p u t i n g
    C l o j u r e
    (concat xs ys)

    View Slide

  43. B i - d i r e c t i o n a l c o m p u t i n g
    C l o j u r e
    (concat xs ys)
    c o r e . l o g i c
    (appendo xs ys zs)

    View Slide

  44. N o t a s i l v e r b u l l e t . . .
    H o w t o r e v e r s e flatten?

    View Slide

  45. W h a t a b o u t n u m b e r s ?
    => (run* [x y] (membero x [1 2 3]) (== y 2) (> x y))

    View Slide

  46. W h a t a b o u t n u m b e r s ?
    => (run* [x y] (membero x [1 2 3]) (== y 2) (> x y))

    View Slide

  47. W h a t a b o u t n u m b e r s ?
    => (run* [x y] (membero x [1 2 3]) (== y 2) (> x y))
    => (require '[clojure.core.logic.fd :as fd])
    => (run* [x y] (membero x [1 2 3]) (== y 2) (fd/> x y))
    ([3 2])

    View Slide

  48. F a c t d a t a b a s e s
    H o w t o m o d e l f a c t s e v o l v i n g o v e r t i m e ?

    View Slide

  49. F a c t d a t a b a s e s
    H o w t o m o d e l f a c t s e v o l v i n g o v e r t i m e ?
    S e l f - m o d i f y i n g c o d e ?

    View Slide

  50. F a c t d a t a b a s e s
    H o w t o m o d e l f a c t s e v o l v i n g o v e r t i m e ?
    S e l f - m o d i f y i n g c o d e ?
    J u s t l i k e i n S Q L !

    View Slide

  51. P L D B
    (pldb/db-rel location p q)
    (def facts
    (pldb/db
    [location :munich :germany]
    [location :augsburg :germany]
    [location :germany :europe]
    [location :london :unitedkingdom]
    [location :unitedkingdom :europe]))

    View Slide

  52. P L D B a s r e a l
    d a t a b a s e
    • d a t a m o d e l i s s i m i l a r t o S Q L :
    s e t s o f t u p l e s ( “ r e l a t i o n s ” )
    • P L D B i s s t a t i c & i m m u t a b l e

    View Slide

  53. 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 . . . .

    View Slide

  54. G r a m m a r s

    View Slide

  55. G r a m m a r s

    View Slide

  56. G r a m m a r s

    View Slide

  57. G r a m m a r s
    (def-->e det [d]
    ([[:d 'the]] '[the])
    ([[:d 'a]] '[a]))
    (def-->e noun-phrase [n]
    ([[:np ?d ?n]]
    (det ?d) (noun ?n)))

    View Slide

  58. 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

    View Slide

  59. 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

    View Slide

  60. (defne presentation [speaker]
    ([:lars]
    (fresh [questions]
    (joke :funny) #_(laugh)
    (introduction :corelogic)
    (features :cool)
    (audience questions)
    (answer questions))))

    View Slide

  61. 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
    c / o W e W o r k
    H e r m a n n s t r a s s e 1 3
    2 0 0 9 5 H a m b u r g
    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

    View Slide

  62. 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 .

    View Slide

  63. I m a g e s o u r c e s
    • T i t l e ( m a p ) : https://pixabay.com/photos/world-europe-map-connections-1264062/
    • 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/
    • C l o c k : https://pixabay.com/photos/clock-alarm-alarm-clock-dial-time-1031503/
    • F i l e s : https://pixabay.com/photos/files-ddr-archive-1633406/

    View Slide