-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchurch.rkt
136 lines (106 loc) · 4.25 KB
/
church.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#lang racket
;; A church-compiler
(provide (all-defined-out))
;; for an input language:
;
; e ::= (letrec ([x (lambda (x ...) e)]) e)
; | (let ([x e] ...) e)
; | (lambda (x ...) e)
; | (e e ...)
; | x
; | (and e e) | (or e e)
; | (if e e e)
; | (prim e) | (prim e e)
; | datum
; datum ::= nat | (quote ()) | #t | #f
; nat ::= 0 | 1 | 2 | ...
; x is a symbol
; prim is a primitive operation in:
(define prims '(+ * - add1 sub1 cons car cdr null? not zero?))
;; To an output language:
;
; e ::= (lambda (x) e)
; | (e e)
; | x
;
(define id `(lambda (x) x))
(define (prim? prim)
(if (member prim prims) #t #f))
(define (churchify-prim prim)
(string->symbol (string-append "church:" (symbol->string prim))))
;; Take something in the input language and translate it to
;; a Racket lambda that represents the church-encoded version
(define (churchify e)
(match e
; Tagged expressions
[`(letrec ([,f (lambda (,args ...) ,e0)]) ,e1)
(lambda (x) x)]
[`(let ([,xs ,e0s] ...) ,e1)
(churchify `((lambda ,xs ,e1) . ,e0s))]
[`(lambda () ,e0)
(lambda (x) x)]
[`(lambda (,x) ,e0)
(lambda (,x) ,(churchify e0))]
[`(lambda (,x . ,rest) ,e0)
(lambda (x) x)]
[`(and ,e0 ,e1)
(lambda (x) x)]
[`(or ,e0 ,e1)
(lambda (x) x)]
[`(if ,e0 ,e1 ,e2)
(lambda (x) x)]
[`(,(? prim? prim) . ,args)
(lambda (x) x)]
; Variables
[(? symbol? x) x]
; Datums
[(? natural? nat)
(define (wrap nat)
(if (= 0 nat) 'x `(f ,(wrap (- nat 1)))))
(churchify `(lambda (f) (lambda (x) ,(wrap nat))))]
[''() (churchify '(lambda (when-cons when-null) (when-null)))]
[#t (churchify '(lambda (tt ft) (tt)))]
[#f (churchify '(lambda (tt ft) (ft)))]
; Untagged application
[`(,fun)
#f]
[`(,fun ,arg)
#f]
[`(,fun ,arg . ,rest)
#f]))
(define (church-encode e)
(define Y-comb `((lambda (u) (u u)) (lambda (y) (lambda (mk) (mk (lambda (x) (((y y) mk) x)))))))
(define church:null? `(lambda (p) (p (lambda (a b) #f) (lambda () #t))))
(define church:cons `(lambda (a b) (lambda (when-cons when-null) (when-cons a b))))
(define church:car `(lambda (p) (p (lambda (a b) a) (lambda () (lambda (x) x)))))
(define church:cdr `(lambda (p) (p (lambda (a b) b) (lambda () (lambda (x) x)))))
(define church:add1 `(lambda (n0) (lambda (f x) (f ((n0 f) x)))))
(define church:sub1 `(lambda (n0) (lambda (f) (lambda (y) (((n0
(lambda (g) (lambda (h) (h (g f)))))
; uses n0 to produce a chain of linked closures
; with |n0|-1 linked functions g -> (lambda (h) (h (g f)))
; The first g and last h are then (lambda (_) y) and id,
; so in a sense it's computing |n0|+1-2
(lambda (_) y))
(lambda (x) x))))))
(define church:zero? `(lambda (n0) ((n0 (lambda (b) #f)) #t)))
(define church:+ `(lambda (n0 n1) (lambda (f x) ((n1 f) ((n0 f) x)))))
(define church:- `(lambda (n0 n1) ((n1 ,church:sub1) n0)))
(define church:* `(lambda (n0 n1) (lambda (f) (lambda (x) ((n0 (n1 f)) x)))))
(define church:= `(lambda (n0 n1) (and (,church:zero? (,church:- n0 n1)) (,church:zero? (,church:- n1 n0)))))
(define church:not `(lambda (bool) (if bool #f #t)))
(churchify
`(let ([Y-comb ,Y-comb]
[church:null? ,church:null?]
[church:cons ,church:cons]
[church:car ,church:car]
[church:cdr ,church:cdr]
[church:add1 ,church:add1]
[church:sub1 ,church:sub1]
[church:+ ,church:+]
[church:- ,church:-]
[church:* ,church:*]
[church:zero? ,church:zero?]
[church:= ,church:=]
[church:not ,church:not])
,e)))