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

interdisciplinary computing (domcode)

Igor Wiedler
November 14, 2015

interdisciplinary computing (domcode)

Igor Wiedler

November 14, 2015
Tweet

More Decks by Igor Wiedler

Other Decks in Programming

Transcript

  1. • 1600: leibniz devises binary numbers • 1800: boole +

    de morgan supply the laws • 1937: 1-bit binary adder • 1947: transistors • 1947: error correcting codes • 1948: information theory • 1965: cosmic radiowave background • 1969: unix • 1972: c programming language
  2. • 1600: leibniz devises binary numbers • 1800: boole +

    de morgan supply the laws • 1937: 1-bit binary adder • 1947: transistors • 1947: error correcting codes • 1948: information theory • 1965: cosmic radiowave background • 1969: unix • 1972: c programming language
  3. • 1600: leibniz devises binary numbers • 1800: boole +

    de morgan supply the laws • 1937: 1-bit binary adder • 1947: transistors • 1947: error correcting codes • 1948: information theory • 1965: cosmic radiowave background • 1969: unix • 1972: c programming language
  4. data = %q{ program = "data = %q{#{data}}" + data

    puts program } program = "data = %q{#{data}}" + data puts program
  5. µKanren (define (var c) (vector c)) (define (var? x) (vector?

    x)) (define (var=? x1 x2) (= (vector-ref x1 0) (vector-ref x2 0))) (define (walk u s) (let ((pr (and (var? u) (assp (lambda (v) (var=? u v)) s)))) (if pr (walk (cdr pr) s) u))) (define (ext-s x v s) `((,x . ,v) . ,s)) (define (== u v) (lambda (s/c) (let ((s (unify u v (car s/c)))) (if s (unit `(,s . ,(cdr s/c))) mzero)))) (define (unit s/c) (cons s/c mzero)) (define mzero '()) (define (unify u v s) (let ((u (walk u s)) (v (walk v s))) (cond ((and (var? u) (var? v) (var=? u v)) s) ((var? u) (ext-s u v s)) ((var? v) (ext-s v u s)) ((and (pair? u) (pair? v)) (let ((s (unify (car u) (car v) s))) (and s (unify (cdr u) (cdr v) s)))) (else (and (eqv? u v) s))))) (define (call/fresh f) (lambda (s/c) (let ((c (cdr s/c))) ((f (var c)) `(,(car s/c) . ,(+ c 1)))))) (define (disj g1 g2) (lambda (s/c) (mplus (g1 s/c) (g2 s/c)))) (define (conj g1 g2) (lambda (s/c) (bind (g1 s/c) g2))) (define (mplus $1 $2) (cond ((null? $1) $2) ((procedure? $1) (lambda () (mplus $2 ($1)))) (else (cons (car $1) (mplus (cdr $1) $2))))) (define (bind $ g) (cond ((null? $) mzero) ((procedure? $) (lambda () (bind ($) g))) (else (mplus (g (car $)) (bind (cdr $) g)))))
  6. (== #t #f) run nevero (== #t #f) X nevero

    (define nevero (conde [(== #t #f)] [nevero]))
  7. (== #t #f) run nevero (== #t #f) X nevero

    X (define nevero (conde [(== #t #f)] [nevero]))
  8. (== #t #f) run nevero (== #t #f) X nevero

    X ... (define nevero (conde [(== #t #f)] [nevero]))
  9. (define bit-xoro (lambda (x y r) (conde ((== 0 x)

    (== 0 y) (== 0 r)) ((== 1 x) (== 0 y) (== 1 r)) ((== 0 x) (== 1 y) (== 1 r)) ((== 1 x) (== 1 y) (== 0 r))))) (define bit-ando (lambda (x y r) (conde ((== 0 x) (== 0 y) (== 0 r)) ((== 1 x) (== 0 y) (== 0 r)) ((== 0 x) (== 1 y) (== 0 r)) ((== 1 x) (== 1 y) (== 1 r)))))
  10. (define full-addero (lambda (b x y r c) (fresh (w

    xy wz) (half-addero x y w xy) (half-addero w b r wz) (bit-xoro xy wz c))))
  11. (define +o (lambda (n m k) (addero 0 n m

    k))) (define -o (lambda (n m k) (+o m k n)))
  12. (run* (x y) (+o x y '(1 0 1))) ;=>

    (((1 0 1) ()) ; (() (1 0 1)) ; ((1) (0 0 1)) ; ((0 0 1) (1)) ; ((1 1) (0 1)) ; ((0 1) (1 1)))
  13. exp := var | abs | app abs := "λ"

    var "." exp app := "(" exp " " exp ")"
  14. exp := var | abs | app abs := "λ"

    var "." exp app := "(" exp " " exp ")" x λx.x λx.x x (λx.x) (λx.x) ...
  15. (define exp (lambda (e) (conde [(symbolo e)] [(fresh (v e2)

    (symbolo v) (exp e2) (== e `(λ ,v . ,e2)))] [(fresh (e1 e2) (exp e1) (exp e2) (== e `(,e1 ,e2)))])))
  16. (define eval-expo (lambda (exp env val) (conde ((fresh (v) (==

    `(quote ,v) exp) (not-in-envo 'quote env) (noo 'closure v) (== v val))) ((fresh (a*) (== `(list . ,a*) exp) (not-in-envo 'list env) (noo 'closure a*) (proper-listo a* env val))) ((symbolo exp) (lookupo exp env val)) ((fresh (rator rand x body env^ a) (== `(,rator ,rand) exp) (eval-expo rator env `(closure ,x ,body ,env^)) (eval-expo rand env a) (eval-expo body `((,x . ,a) . ,env^) val))) ((fresh (x body) (== `(lambda (,x) ,body) exp) (symbolo x) (not-in-envo 'lambda env) (== `(closure ,x ,body ,env) val)))))) (define not-in-envo (lambda (x env) (conde ((fresh (y v rest) (== `((,y . ,v) . ,rest) env) (=/= y x) (not-in-envo x rest))) ((== '() env))))) (define proper-listo (lambda (exp env val) (conde ((== '() exp) (== '() val)) ((fresh (a d t-a t-d) (== `(,a . ,d) exp) (== `(,t-a . ,t-d) val) (eval-expo a env t-a) (proper-listo d env t-d)))))) (define lookupo (lambda (x env t) (fresh (rest y v) (== `((,y . ,v) . ,rest) env) (conde ((== y x) (== v t)) ((=/= y x) (lookupo x rest t)))))) github.com/webyrd/quines
  17. (run 3 (q) (eval-expo q '() 'me!)) ;=> ('me! ;

    ((lambda (_.0) 'me!) '_.1) ; ((lambda (_.0) _.0) 'me!))
  18. (run 1 (q) (eval-expo q '() q)) ;=> ((lambda (_.0)

    (list _.0 (list 'quote _.0))) ; '(lambda (_.0) (list _.0 (list 'quote _.0))))
  19. • ∧ conjunction (implicit) • ∨ disjunction (conde) • =

    unification (==) • ∃ variables (fresh)
  20. (21) The Reasoned Schemer
 Daniel Friedman, William Byrd, Oleg Kiselyov

    (22) Quine Generation via Relational Interpreters
 William Byrd, Eric Holk, Daniel Friedman (23) µKanren
 Jason Hemann, Daniel Friedman (4) How to Replace Failure by a List of Successes
 Philip Wadler (7) Unspeakable Things
 Laurie Penny
  21. (1) Knitting for Fun: A Recursive Sweater
 Anna Bernasconi, Chiara

    Bodei, Linda Pagli (2) Weaving and Programming: More Related Than You (Probably) Realize!
 Allie Jones (3) From Text to Textiles!
 Lea Albaugh (4) How to Replace Failure by a List of Successes
 Philip Wadler (5) A Mathematical Theory of Communication
 Claude Shannon
  22. (6) Harassed by Algorithms
 Joanne McNeil (7) Unspeakable Things
 Laurie

    Penny (8) Cognitive Psychology and Programming Language Design
 Ben Shneiderman (9) Gödel, Escher, Bach
 Douglas Hofstadter (10) The Annotated Turing
 Charles Petzold
  23. (11) Clause and Effect
 William Clocksin (12) Code
 Charles Petzold

    (13) Bitsquatting
 Artem Dinaburg (14) Redis Crashes - a small rant about software reliability
 Salvatore Sanfilippo (15) The Thrilling Adventures of Lovelace and Babbage
 Sydney Padua
  24. (16) Logicomix: An Epic Search for Truth
 Apostolos Doxiadis, Christos

    Papadimitriou (17) Pioneer Programmer
 Jean Jennings Bartik (18) The C Programming Language
 Brian Kernighan, Dennis Ritchie (19) Understanding Computation
 Tom Stuart (20) Quantum Electrodynamics
 Richard Feynman
  25. (21) The Reasoned Schemer
 Daniel Friedman, William Byrd, Oleg Kiselyov

    (22) Quine Generation via Relational Interpreters
 William Byrd, Eric Holk, Daniel Friedman (23) µKanren
 Jason Hemann, Daniel Friedman (24) Propositions as Types
 Philip Wadler (25) Unflattening
 Nick Sousanis