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

Propagators in Clojure

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

Propagators in Clojure

These are the slides from the Clojure/conj 2013 talk "Propagators in Clojure". The referenced "propaganda" library can be found at https://github.com/tgk/propaganda Questions can be directed at @tgkristensen

Avatar for Thomas G. Kristensen

Thomas G. Kristensen

November 16, 2013
Tweet

Other Decks in Programming

Transcript

  1. “A declarative language requires only the statement of relevant relationships,

    and the computational organization is specified separately or performed more or less automatically.” (what, not how) CONSTRAINTS - A Language for Expressing Almost-Hierarchical Descriptions Gerald Jay Sussman and Guy Lewis Steele Jr. AI Journal, 1981
  2. We Really Don’t Know How to Compute! Gerald Jay Sussman

    Massachusetts Institute of Technology CSAIL and EECS
  3. The Art of the Propagator Alexey Radul & Gerald Jay

    Sussman {axch,gjs}@mit.edu December 13, 2008 Abstract We develop a programming model built on the idea that the basic computational elements are autonomous machines interconnected by shared cells through which they communicate. Each machine con- tinuously examines the cells it is interested in, and adds information to some based on deductions it can make from information from the others. This model makes it easy to smoothly combine expression- oriented and constraint-based programming; it also easily accommo- dates implicit incremental distributed search in ordinary programs. This work builds on the original research of Guy Lewis Steele Jr. [18] and was developed more recently with the help of Chris Hanson.
  4. Propagation Networks: A Flexible and Expressive Substrate for Computation by

    Alexey Andreyevich Radul B.S., Massachusetts Institute of Technology (2003) M.Eng., Massachusetts Institute of Technology (2005) Submitted to the Department of Electrical Engineering and Computer Science in partial fulfillment of the requirements for the degree of Doctor of Philosophy in Computer Science and Engineering at the MASSACHVSETTS INSTITVTE OF TECHNOLOGY September 2009 c Massachusetts Institute of Technology 2009. All rights reserved. Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Department of Electrical Engineering and Computer Science September 4, 2009
  5. core.logic (prod a b p) (sum p c d) (add-value

    a 2) (add-value b 3) (add-value c 4) (fd/* a b p) (fd/+ p c d) (== a 2) (== b 3) (== c 4) a × b + c = d
  6. Propagators a × b + c = d (def plus

    (function->propagator-constructor +)) (def minus (function->propagator-constructor -)) (defn sum [x y z] (plus x y z) (minus z x y) (minus z y x))
  7. Propagators a × b + c = d (sum p

    c d) (prod a b p) (add-value a 2) (add-value b 3) (add-value c 4) (get-value d) ;; 10 (sum p c d) (prod a b p) (add-value a 2) (add-value b 3) (add-value c 4) (add-value d 9) ;; Exception
  8. Barometer puzzle (plop) h = ½ × g × t²

    (defn fall-duration [t h] (let [g (make-cell) one-half (make-cell) t-squared (make-cell) gt-squared (make-cell)] (quadratic t t-squared) (product g t-squared gt-squared) (product one-half gt-squared h) (add-value g (make-interval 9.789 9.832)) (add-value one-half 0.5)))
  9. Barometer puzzle (let [building-height (make-cell) fall-time (make-cell)] (fall-duration fall-time building-height)

    (add-value fall-time (make-interval 2.9 3.1)) {:building-height (get-value building-height)}) ;; {:building-height {:lo 41.162745, :hi 47.24276000000001}}
  10. Barometer puzzle (defn similar-triangles [s' h' s h] (let [ratio

    (make-cell)] (product s' ratio h') (product s ratio h))) h’ = s’ × r h = s × r
  11. Barometer puzzle (let [building-height (make-cell) barometer-height (make-cell) barometer-shadow (make-cell) building-shadow

    (make-cell)] (similar-triangles barometer-shadow barometer-height building-shadow building-height) (add-value building-shadow (make-interval 54.9 55.1)) (add-value barometer-height (make-interval 0.3 0.32)) (add-value barometer-shadow (make-interval 0.36 0.37)) {:building-height (get-value building-height)}) ;; {:building-height {:lo 44.51351351351351, ;; :hi 48.977777777777774}}
  12. Barometer puzzle (let [building-height (make-cell) fall-time (make-cell) barometer-height (make-cell) barometer-shadow

    (make-cell) building-shadow (make-cell)] (fall-duration fall-time building-height) (similar-triangles barometer-shadow barometer-height building-shadow building-height) (add-value fall-time (make-interval 2.9 3.1)) (add-value building-shadow (make-interval 54.9 55.1)) (add-value barometer-height (make-interval 0.3 0.32)) (add-value barometer-shadow (make-interval 0.36 0.37)) {:building-height (get-value building-height)}) ;; {:building-height {:lo 44.51351351351351, ;; :hi 47.24276000000001}} lo hi w. fall time 41.16 47.24 w. shadow length 44.51 48.98 fall time + shadow length 44.51 47.24
  13. Barometer puzzle (let [building-height (make-cell) fall-time (make-cell) barometer-height (make-cell) barometer-shadow

    (make-cell) building-shadow (make-cell)] (fall-duration fall-time building-height) (similar-triangles barometer-shadow barometer-height building-shadow building-height) (add-value fall-time (make-interval 2.9 3.1)) (add-value building-shadow (make-interval 54.9 55.1)) (add-value barometer-height (make-interval 0.3 0.32)) (add-value barometer-shadow (make-interval 0.36 0.37)) {:fall-time (get-value fall-time) :barometer-height (get-value barometer-height)}) ;; {:fall-time {:lo 3.0091234174691017, :hi 3.1}, ;; :barometer-height {:lo 0.3, :hi 0.3183938287795994}} entered entered calculated calculated lo hi lo hi fall-time 2.9 3.1 3.009 3.1 barometer-height 0.3 0.32 0.3 0.318
  14. Take one (binding [*merge* (default-merge)] (dosync (let [a (make-cell) b

    (make-cell) c (make-cell) d (make-cell)] (let [p (make-cell)] (prod a b p) (sum p c d) (add-value a 2) (add-value b 3) (add-value c 4) (get-value d))))) - Globally bound fns - dosync - Mutable - Where are the propagators? - Snapshots? - No ClojureScript support
  15. Take two (let [system (make-system)] (-> system (prod :a :b

    :p) (sum :p :c :d) (add-value :a 2) (add-value :b 3) (add-value :c 4) (get-value :d))) - No globally bound fns - No STM - Immutable - Propagators in system - Snapshots - ClojureScript support
  16. STM if you need it (def r (ref (make-system))) (dosync

    (doto r (alter prod :a :b :p) (alter sum :p :c :d) (alter add-value :a 2) (alter add-value :b 3) (alter add-value :c 4))) (get-value @r :d) ;; 10 (dosync (alter r add-value :d 9)) ;; Exception (get-value @r :d) ;; 10
  17. Differences core.logic propaganda Great at logic puzzles Great at arithmetics

    Efficient search algorithm Transparent and intuitive search algorithm
  18. Conclusion • Logic programming is great, but not the only

    approach in town • There is a lot of academic work out there, and you should explore it • Using Clojure gives you leverage