compiling to calculus and church encodings e lambda x e e
play

Compiling to -calculus and church encodings e ::= (lambda (x) e) - PowerPoint PPT Presentation

Compiling to -calculus and church encodings e ::= (lambda (x) e) | (e e) | x Alonzo Church = ( U U ) = (( (u) (u u)) ( (u) (u u))) (u u)[u U ] = (( (u) (u u)) ( (u) (u u))) Church encoding e ::= (letrec ([x


  1. λ Compiling to λ -calculus and church encodings

  2. e ::= (lambda (x) e) | (e e) | x

  3. Alonzo Church

  4. Ω = ( U U ) = (( λ (u) (u u)) Ω ( λ (u) (u u))) β (u u)[u ← U ] = (( λ (u) (u u)) ( λ (u) (u u)))

  5. Church encoding

  6. e ::= (letrec ([x (lambda (x …) e)])) 
 | (let ([x e] …) e) | (lambda (x …) e) | (e e …) | x | (if e e e) | (+ e e) | (* e e) | (cons e e) | (car e) | (cdr e) | d d ::= ℕ | #t | #f | ‘() x ::= <vars>

  7. e ::= (letrec ([x (lambda (x …) e)])) 
 | (let ([x e] …) e) | (lambda (x …) e) e ::= ) e) | (e e …) ) | x | (if e e e) | (+ e e) | (* e e) | (cons e e) | (car e) | (cdr e) | d d ::= ℕ | #t | #f | ‘() x ::= <vars>

  8. Desugaring Let

  9. (let ([x e] …) ebody)

  10. (let ([x e] …) ebody) (( λ (x …) ebody) e …)

  11. Currying

  12. Gottlob Frege Moses Schönfinkel

  13. ( λ (x) ( λ (x y z) e) ( λ (y) ( λ (z) e))) ( λ (x) e) ( λ (x) e) ( λ () e) ( λ (_) e)

  14. (f a b c d) ((((f a) b) c) d) (f a) (f a) (f) (f f)

  15. e ::= (letrec ([x (lambda (x) e)])) | (lambda (x) e) | (e e) | x | (if e e e) | ((+ e) e) | ((* e) e) | ((cons e) e) | (car e) | (cdr e) | d d ::= ℕ | #t | #f | ‘() x ::= <vars>

  16. Conditionals & Booleans

  17. (if #t e T e F ) (if #f e T e F ) e T e F

  18. (( λ (t f) t) (if #t e T e F ) (( λ (t f) f) (if #f e T e F ) (( λ (t f) t) v T v F ) (( λ (t f) f) v T v F ) v T v F

  19. (( λ (t f) t) e T Ω ) .........

  20. (( λ (t f) (t)) ( λ () e T ) ( λ () Ω )) (( λ () e T )) e T v T

  21. Turn all values into verbs! (Focus on the behaviors that are implicit in values.)

  22. e ::= (letrec ([x (lambda (x) e)])) | (lambda (x) e) | (e e) | x | ((+ e) e) | ((* e) e) | ((cons e) e) | (car e) | (cdr e) | d d ::= ℕ | ‘() x ::= <vars>

  23. Natural Numbers

  24. ( λ (f) ( λ (x) (f N x))) 0: ( λ (f) ( λ (x) x)) 1: ( λ (f) ( λ (x) (f x))) 2: ( λ (f) ( λ (x) (f (f x)))) 3: ( λ (f) ( λ (x) (f (f (f x)))))

  25. church+ = ( λ (n) ( λ (m) ( λ (f) ( λ (x) …))))

  26. church+ = ( λ (n) ( λ (m) ( λ (f) ( λ (x) ((n f) ((m f) x))))))

  27. church* = ( λ (n) ( λ (m) ( λ (f) ( λ (x) …))))

  28. church* = ( λ (n) ( λ (m) ( λ (f) ( λ (x) ((n (m f)) x)))))

  29. M f N = f N*M

  30. e ::= (letrec ([x (lambda (x) e)])) | (lambda (x) e) | (e e) | x | ((cons e) e) | (car e) | (cdr e) | d d ::= ‘() x ::= <vars>

  31. Lists

  32. The fundamental problem: We need to be able to case-split. The solution: We take two callbacks as with #t , #f !

  33. ‘() = ( λ (when-cons) ( λ (when-null) (when-null))) (cons a b) = ( λ (when-cons) ( λ (when-null) (when-cons a b)))

  34. e ::= (letrec ([x (lambda (x) e)])) | (lambda (x) e) | (e e) | x x ::= <vars>

  35. Y combinator

  36. (letrec ([fact ( λ (n) (if (= n 0) 1 (* n (fact (- n 1)))))]) (fact 5))

  37. (letrec ([fact ( λ (fact) ( λ (n) (if (= n 0) 1 (* n (fact (- n 1)))))]) (fact 5))

  38. (letrec ([mk ( λ (fact) ( λ (n) (if (= n 0) 1 (* n (fact (- n 1)))))]) ((mk mk) 5))

  39. (letrec ([mk ( λ (mk) ( λ (n) (if (= n 0) 1 (* n ((mk mk) (- n 1)))))]) ((mk mk) 5))

  40. Y

  41. (Y f) = f (Y f) (It’s a fixed-point combinator!)

  42. Y = ( U ( λ (y) ( λ (f) (f ( λ (x) (((y y) f) x)))))

  43. (letrec ([fact (Y ( λ (fact) ( λ (n) (if (= n 0) 1 (* n (fact (- n 1)))))]) (fact 5))

  44. e ::= (lambda (x) e) | (e e) | x

  45. De-churching (define (church->nat cv) ) (define (church->list cv) ) (define (church->bool cv) )

  46. De-churching (define (church->nat cv) ((cv add1) 0)) (define (church->list cv) ) (define (church->bool cv) )

  47. De-churching (define (church->nat cv) ((cv add1) 0)) (define (church->list cv) ((cv ( λ (car) ( λ (cdr) (cons car (church->list cdr))))) ( λ (na) ‘()))) (define (church->bool cv) )

  48. De-churching (define (church->nat cv) ((cv add1) 0)) (define (church->list cv) ((cv ( λ (car) ( λ (cdr) (cons car (church->list cdr))))) ( λ (na) ‘()))) (define (church->bool cv) ((cv ( λ () #t)) ( λ () #f)))

  49. (letrec ([map ( λ (f lst) (if (null? lst) '() (cons (f (car lst)) (map f (cdr lst)))))]) (map ( λ (x) (+ 1 x)) '(0 5 3)))

  50. (define lst ((((((((( λ (Y-comb) ( λ (church:null?) ( λ (church:cons) ( λ (church:car) ( λ (church:cdr) ( λ (church:+) ( λ (church:*) ( λ (church:not) (( λ (map) ((map ( λ (x) ((church:+ ( λ (f) ( λ (x) (f x)))) x))) ((church:cons ( λ (f) ( λ (x) x))) ((church:cons ( λ (f) ( λ (x) (f (f (f (f (f x)))))))) ((church:cons ( λ (f) ( λ (x) (f (f (f x)))))) > (map church->nat (church->list lst)) ( λ (when-cons) ( λ (when-null) '(1 6 4) (when-null ( λ (x) x))))))))) (Y-comb ( λ (map)

  51. Now we can write a church encoder!

Recommend


More recommend