-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformations.lisp
220 lines (194 loc) · 7.91 KB
/
transformations.lisp
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
(in-package #:ski)
(defun combinator->lambda (combinator)
"Convert COMBINATOR to its representation as a lambda abstraction."
(labels ((term->lambda (term)
(etypecase term
(combinator-application
(make-lambda-application
(term->lambda (left term))
(term->lambda (right term))))
(combinator
(combinator->lambda term))
(combinator-variable
(make-lambda-variable (name term))))))
(let* ((variables (loop with g = (make-variable-name-generator)
with arity = (arity combinator)
repeat arity
collect (make-combinator-variable (generate-name g))))
(term (reduce-term (reduce #'make-combinator-application
variables
:initial-value combinator))))
(reduce #'make-lambda-abstraction
(mapcar (lambda (v) (make-lambda-variable (name v))) variables)
:from-end t
:initial-value (term->lambda term)))))
(defgeneric lambda->ski (term)
(:documentation "Convert a lambda calculus TERM to its corresponding SKI calculus term."))
(defmethod lambda->ski ((term (eql (get-combinator 'S))))
term)
(defmethod lambda->ski ((term (eql (get-combinator 'K))))
term)
(defmethod lambda->ski ((term (eql (get-combinator 'I))))
term)
(defmethod lambda->ski ((term combinator-variable))
term)
(defmethod lambda->ski ((term lambda-variable))
(make-combinator-variable (name term)))
(defmethod lambda->ski ((term application))
(with-accessors ((left left) (right right)) term
(make-combinator-application (lambda->ski left) (lambda->ski right))))
(defmethod lambda->ski ((term lambda-abstraction))
(with-accessors ((variable variable) (body body)) term
(cond ((not (occurs-free-p variable body))
(make-combinator-application
(get-combinator 'K)
(lambda->ski body)))
((and (variable-p body) (same-variable-p variable body))
(get-combinator 'I))
((lambda-abstraction-p body)
(lambda->ski
(make-lambda-abstraction variable (lambda->ski body))))
((application-p body)
(with-accessors ((left left) (right right)) body
(if (and (variable-p right)
(same-variable-p right variable)
(not (occurs-free-p variable left)))
(lambda->ski left)
(make-combinator-application
(make-combinator-application
(get-combinator 'S)
(lambda->ski (make-lambda-abstraction variable left)))
(lambda->ski (make-lambda-abstraction variable right)))))))))
(defgeneric lambda->sk (term)
(:documentation "Convert a lambda calculus TERM to its corresponding SK calculus term."))
(defmethod lambda->sk ((term (eql (get-combinator 'S))))
term)
(defmethod lambda->sk ((term (eql (get-combinator 'K))))
term)
(defmethod lambda->sk ((term combinator-variable))
term)
(defmethod lambda->sk ((term lambda-variable))
(make-combinator-variable (name term)))
(defmethod lambda->sk ((term application))
(with-accessors ((left left) (right right)) term
(make-combinator-application (lambda->sk left) (lambda->sk right))))
(defmethod lambda->sk ((term lambda-abstraction))
(with-accessors ((variable variable) (body body)) term
(cond ((not (occurs-free-p variable body))
(make-combinator-application
(get-combinator 'K)
(lambda->sk body)))
((and (variable-p body) (same-variable-p variable body))
(make-combinator-application
(make-combinator-application
(get-combinator 'S)
(get-combinator 'K))
(get-combinator 'K)))
((lambda-abstraction-p body)
(lambda->sk
(make-lambda-abstraction variable (lambda->sk body))))
((application-p body)
(with-accessors ((left left) (right right)) body
(if (and (variable-p right)
(same-variable-p right variable)
(not (occurs-free-p variable left)))
(lambda->sk left)
(make-combinator-application
(make-combinator-application
(get-combinator 'S)
(lambda->sk (make-lambda-abstraction variable left)))
(lambda->sk (make-lambda-abstraction variable right)))))))))
(defun combinator->ski (combinator)
"Return a SKI calculus term equivalent to COMBINATOR."
(labels ((eliminate (term var)
(cond ((term-equal var term)
(get-combinator 'I))
((not (occurs-free-p var term))
(make-combinator-application (get-combinator 'K) term))
((and (combinator-application-p term)
(term-equal var (right term))
(not (occurs-free-p var (left term))))
(left term))
(t
(make-combinator-application
(make-combinator-application
(get-combinator 'S)
(eliminate (left term) var))
(eliminate (right term) var))))))
(let ((vars (loop with g = (make-variable-name-generator)
with arity = (arity combinator)
repeat arity
collect (make-combinator-variable (generate-name g)))))
(let ((term (reduce-term
(reduce #'make-combinator-application vars :initial-value combinator))))
(loop for var in (nreverse vars)
do (setf term (eliminate term var))
finally (return term))))))
(defgeneric sk->goedel (term)
(:documentation "Return the Gödel number of TERM."))
(defmethod sk->goedel ((term (eql (get-combinator 'S))))
1)
(defmethod sk->goedel ((term (eql (get-combinator 'K))))
2)
(defmethod sk->goedel ((term combinator-application))
(nth-value
0
(parse-integer
(concatenate
'string
"3"
(write-to-string (sk->goedel (left term)))
(write-to-string (sk->goedel (right term)))
"4"))))
(defun goedel->sk (n)
"Return the SK term denoted by the Gödel number N."
(parse-combinator-term
(nsubstitute
#\S #\1
(nsubstitute
#\K #\2
(nsubstitute
#\( #\3
(nsubstitute
#\) #\4
(write-to-string n)))))))
(defun natural->church (n)
"Convert the natural number N to its representation as a Church
numeral."
(declare (type (integer 0) n))
(let ((f (make-lambda-variable #\f))
(x (make-lambda-variable #\x)))
(make-lambda-abstraction
f
(make-lambda-abstraction
x
(let ((acc x))
(dotimes (i n acc)
(setf acc (make-lambda-application f acc))))))))
(defun church->natural (term)
"Convert the Church numeral TERM to its corresponding natural number."
(do ((result 0)
(body (body (body term))))
((lambda-variable-p body) result)
(incf result)
(setf body (right body))))
(defun natural->barendregt (n)
"Convert the natural number N to its representation in the scheme used
in the book To Mock a Mockingbird by Raymond Smullyan."
(declare (type (integer 0) n))
(let* ((v (get-combinator 'V))
(z (get-combinator 'I))
(f (make-combinator-application (get-combinator 'K) z))
(succ (make-combinator-application v f))
(result z))
(dotimes (i n result)
(setf result (make-combinator-application succ result)))))
(defun barendregt->natural (term)
"Convert a numeral from the scheme used in the book To Mock a
Mockingbird by Raymond Smullyan to its corresponding natural number."
(do ((i (get-combinator 'I))
(result 0)
(acc term))
((term-equal i acc) result)
(incf result)
(setf acc (right acc))))