-
Notifications
You must be signed in to change notification settings - Fork 0
/
translations.lisp
347 lines (259 loc) · 12.1 KB
/
translations.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
;;; translations.lisp Some formula translations
(in-package :dialogues)
(defclass formula-translation ()
((transformer :type function
:initarg :transformer
:initform #'identity
:accessor transformer)
(description :type string
:initarg :description
:initform "(no description was supplied)"
:accessor description)))
(defun apply-translation (translation formula)
(funcall (transformer translation) formula))
;;; Gödel-Gentzen translation
(defgeneric goedel-gentzen (formula)
(:documentation "The Goedel-Gentzen negative translation."))
(defmethod goedel-gentzen ((formula atomic-formula))
(negate (negate formula)))
(defmethod goedel-gentzen ((negation negation))
(negate (goedel-gentzen (unnegate negation))))
(defmethod goedel-gentzen ((conjunction binary-conjunction))
(make-binary-conjunction (goedel-gentzen (lhs conjunction))
(goedel-gentzen (rhs conjunction))))
(defmethod goedel-gentzen ((disjunction binary-disjunction))
(negate (make-binary-conjunction (negate (goedel-gentzen (lhs disjunction)))
(negate (goedel-gentzen (rhs disjunction))))))
(defmethod goedel-gentzen ((implication implication))
(make-implication (goedel-gentzen (antecedent implication))
(goedel-gentzen (consequent implication))))
(defmethod goedel-gentzen ((universal universal-generalization))
(make-instance 'universal-generalization
:bindings (bindings universal)
:matrix (goedel-gentzen (matrix universal))))
(defmethod goedel-gentzen ((existential existential-generalization))
(negate (make-instance 'universal-generalization
:bindings (bindings existential)
:matrix (negate (goedel-gentzen (matrix existential))))))
(defparameter goedel-gentzen-translation
(make-instance 'formula-translation
:description "The Goedel-Gentzen negative translation"
:transformer #'goedel-gentzen))
;;; Simple double negation
(defun double-negate (formula)
"Double negate the whole formula"
(negate (negate formula)))
(defparameter double-negate-translation
(make-instance 'formula-translation
:description "Double negate the whole formula"
:transformer #'double-negate))
;;; All-subformulas translation
(defgeneric dn-all-subformulas (formula)
(:documentation "Double negate all subformulas"))
(defmethod dn-all-subformulas ((formula atomic-formula))
(negate (negate formula)))
(defmethod dn-all-subformulas ((formula unary-connective-formula))
(negate (negate (negate (dn-all-subformulas (argument formula))))))
(defmethod dn-all-subformulas ((formula binary-connective-formula))
(negate (negate (make-instance (class-of formula)
:lhs (dn-all-subformulas (lhs formula))
:rhs (dn-all-subformulas (rhs formula))))))
(defmethod dn-all-subformulas ((gen generalization))
(make-instance (class-of gen)
:bindings (bindings gen)
:matrix (negate (negate (dn-all-subformulas (matrix gen))))))
(defparameter double-negate-all-subformulas-translation
(make-instance 'formula-translation
:description "Double negate all subformulas"
:transformer #'dn-all-subformulas))
;;; Kuroda translation
(defun kuroda (formula)
(labels ((kuroda-helper (form)
(cond ((atomic-formula-p form) form)
((negation? form)
(negate (kuroda-helper (unnegate form))))
((binary-connective-formula? form)
(make-instance (class-of form)
:lhs (kuroda-helper (lhs form))
:rhs (kuroda-helper (rhs form))))
((existential-generalization? form)
(make-instance 'existential-generalization?
:bindings (bindings form)
:matrix (kuroda-helper (matrix form))))
((universal-generalization? form)
(make-instance 'universal-generalization?
:bindings (bindings form)
:matrix (negate
(negate
(kuroda-helper (matrix form)))))))))
(negate (negate (kuroda-helper formula)))))
(defparameter kuroda-translation
(make-instance 'formula-translation
:description "The Kuroda translation"
:transformer #'kuroda))
;;; Replace all atomic formulas by their negations
(defgeneric negate-atomic-subformulas (formula)
(:documentation "Negate atomic subformulas"))
(defmethod negate-atomic-subformulas ((formula atomic-formula))
(negate formula))
(defmethod negate-atomic-subformulas ((formula unary-connective-formula))
(make-instance (class-of formula)
:argument (negate-atomic-subformulas (argument formula))))
(defmethod negate-atomic-subformulas ((formula binary-connective-formula))
(make-instance (class-of formula)
:lhs (negate-atomic-subformulas (lhs formula))
:rhs (negate-atomic-subformulas (rhs formula))))
(defmethod negate-atomic-subformulas ((gen generalization))
(make-instance (class-of gen)
:bindings (bindings gen)
:matrix (negate-atomic-subformulas (matrix gen))))
(defparameter negate-atomic-subformulas-translation
(make-instance 'formula-translation
:description "Negate atomic subformulas"
:transformer #'negate-atomic-subformulas))
;; Replace all atomic subformulas by their double negations
(defgeneric double-negate-atomic-subformulas (formula)
(:documentation "Double negate atomic subformulas"))
(defmethod double-negate-atomic-subformulas ((formula atomic-formula))
(negate (negate formula)))
(defmethod double-negate-atomic-subformulas ((formula unary-connective-formula))
(make-instance (class-of formula)
:argument (double-negate-atomic-subformulas (argument formula))))
(defmethod double-negate-atomic-subformulas ((formula binary-connective-formula))
(make-instance (class-of formula)
:lhs (double-negate-atomic-subformulas (lhs formula))
:rhs (double-negate-atomic-subformulas (rhs formula))))
(defmethod double-negate-atomic-subformulas ((gen generalization))
(make-instance (class-of gen)
:bindings (bindings gen)
:matrix (double-negate-atomic-subformulas (matrix gen))))
(defparameter double-negate-atomic-subformulas-translation
(make-instance 'formula-translation
:description "Double negate atomic subformulas"
:transformer #'double-negate-atomic-subformulas))
;; Replace all atomic subformulas by their "self-conjunctions"
(defgeneric self-conjoin-atomic-subformulas (formula)
(:documentation "Replace all atomic subformulas p by (p ∧ p)"))
(defmethod self-conjoin-atomic-subformulas ((formula atomic-formula))
(make-binary-conjunction formula formula))
(defmethod self-conjoin-atomic-subformulas ((formula unary-connective-formula))
(make-instance (class-of formula)
:argument (self-conjoin-atomic-subformulas (argument formula))))
(defmethod self-conjoin-atomic-subformulas ((formula binary-connective-formula))
(make-instance (class-of formula)
:lhs (self-conjoin-atomic-subformulas (lhs formula))
:rhs (self-conjoin-atomic-subformulas (rhs formula))))
(defmethod self-conjoin-atomic-subformulas ((gen generalization))
(make-instance (class-of gen)
:bindings (bindings gen)
:matrix (self-conjoin-atomic-subformulas (matrix gen))))
(defparameter self-conjoin-atomic-subformulas-translation
(make-instance 'formula-translation
:description "Replace all atomic subformulas p by (p ∧ p)"
:transformer #'self-conjoin-atomic-subformulas))
;; Replace all atomic subformulas by their "self-disjunctions"
(defgeneric self-disjoin-atomic-subformulas (formula)
(:documentation "Replace all atomic subformulas p by (p ∨ p)"))
(defmethod self-disjoin-atomic-subformulas ((formula atomic-formula))
(make-binary-disjunction formula formula))
(defmethod self-disjoin-atomic-subformulas ((formula unary-connective-formula))
(make-instance (class-of formula)
:argument (self-disjoin-atomic-subformulas (argument formula))))
(defmethod self-disjoin-atomic-subformulas ((formula binary-connective-formula))
(make-instance (class-of formula)
:lhs (self-disjoin-atomic-subformulas (lhs formula))
:rhs (self-disjoin-atomic-subformulas (rhs formula))))
(defmethod self-disjoin-atomic-subformulas ((gen generalization))
(make-instance (class-of gen)
:bindings (bindings gen)
:matrix (self-disjoin-atomic-subformulas (matrix gen))))
(defparameter self-disjoin-atomic-subformulas-translation
(make-instance 'formula-translation
:description "Replace all atomic subformulas p by (p ∨ p)"
:transformer #'self-disjoin-atomic-subformulas))
;; Contrapositive of all implications
(defgeneric contrapositivify (formula)
(:documentation "Take the contrapositive of all implications"))
(defmethod contrapositivify ((formula atomic-formula))
formula)
(defmethod contrapositivify ((formula binary-conjunction))
(make-binary-conjunction (contrapositivify (lhs formula))
(contrapositivify (rhs formula))))
(defmethod contrapositivify ((formula binary-disjunction))
(make-binary-disjunction (contrapositivify (lhs formula))
(contrapositivify (rhs formula))))
(defmethod contrapositivify ((neg negation))
(negate (contrapositivify (argument neg))))
(defmethod contrapositivify ((equiv equivalence))
(make-equivalence (contrapositivify (lhs equiv))
(contrapositivify (rhs equiv))))
(defmethod contrapositivify ((imp implication))
(make-implication (negate (contrapositivify (consequent imp)))
(negate (contrapositivify (antecedent imp)))))
(defmethod contrapositivify ((gen generalization))
(make-instance (class-of gen)
:bindings (bindings gen)
:matrix (contrapositivify (matrix gen))))
(defparameter contrapositivify-translation
(make-instance 'formula-translation
:description "Take the contrapositive of all implications"
:transformer #'contrapositivify))
;; Contrapositive of an implication (but don't contrapositivify any
;; inner implications)
(defgeneric contrapositive (formula)
(:documentation "Take the contrapositive of an implications (but do nothing inside the formula)"))
(defmethod contrapositive ((formula atomic-formula))
formula)
(defmethod contrapositive ((formula binary-conjunction))
formula)
(defmethod contrapositive ((formula binary-disjunction))
formula)
(defmethod contrapositive ((neg negation))
neg)
(defmethod contrapositive ((equiv equivalence))
equiv)
(defmethod contrapositive ((imp implication))
(make-implication (negate (consequent imp))
(negate (antecedent imp))))
(defmethod contrapositive ((gen generalization))
gen)
(defparameter contrapositive-translation
(make-instance 'formula-translation
:description "Take the contrapositive of all implications (but do nothing inside the formula)"
:transformer #'contrapositive))
;; Identity translation
(defparameter identity-translation
(make-instance 'formula-translation
:description "Identity function"
:transformer #'identity))
;; atomics to instances of excluded middle
(defgeneric atomic->excluded-middle (formula))
(defmethod atomic->excluded-middle ((form atomic-formula))
(make-binary-disjunction form
(negate form)))
(defmethod atomic->excluded-middle ((formula unary-connective-formula))
(make-instance (class-of formula)
:argument (atomic->excluded-middle (argument formula))))
(defmethod atomic->excluded-middle ((formula binary-connective-formula))
(make-instance (class-of formula)
:lhs (atomic->excluded-middle (lhs formula))
:rhs (atomic->excluded-middle (rhs formula))))
(defmethod atomic->excluded-middle ((gen generalization))
(make-instance (class-of gen)
:bindings (bindings gen)
:matrix (atomic->excluded-middle (matrix gen))))
(defparameter atomic->excluded-middle-translation
(make-instance 'formula-translation
:description "Replace all atomic subformulas p by (p ∨ ¬p)"
:transformer #'atomic->excluded-middle))
;;; converse
(defun converse (formula)
(if (implication? formula)
(make-implication (consequent formula)
(antecedent formula))
formula))
(defparameter converse-translation
(make-instance 'formula-translation
:description "Take the converse of an implication (but do nothing to non-implications)"
:transformer #'converse))
;;; translations.lisp ends here