-
Notifications
You must be signed in to change notification settings - Fork 0
/
expressions.lisp
1393 lines (1070 loc) · 43.5 KB
/
expressions.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; terms.lisp A representation of simple terms
(in-package :dialogues)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Terms
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass expression ()
nil)
(defclass atomic-expression (expression)
((head
:initarg :head
:accessor head
:initform (error "An atomic expression needs a head.")
:type string)
(arguments
:initarg :arguments
:accessor arguments
:initform nil
:type list)))
(defmethod print-object ((term atomic-expression) stream)
(with-slots (head arguments)
term
(if (null arguments)
(format stream "~a" head)
(format stream "~a(~{~a~^,~})" head arguments))))
(defclass term () nil)
(defun term? (thing)
(typep thing 'term))
(defclass function-term (atomic-expression term)
nil)
(defgeneric function-symbol (x)
(:documentation "The function symbol of a function term."))
(defmethod function-symbol ((x t))
(error "How to extract the function symbol of an object~%~% ~a~%~%of class~%~~% ~a~%~%?" x (class-of x)))
(defmethod function-symbol ((x function-term))
(head x))
(defun make-function-term (function &rest args)
(make-instance 'function-term
:function function
:args args))
(defclass variable-term (atomic-expression term)
nil)
(defun variable-term-p (x)
(typep x 'variable-term))
(defgeneric equal-variables? (var-1 var-2))
(defmethod equal-variables? ((var-1 variable-term) (var-2 variable-term))
(string= (head var-1) (head var-2)))
(defgeneric equal-terms? (term-1 term-2))
(defmethod equal-terms? ((v-1 variable-term) (v-2 variable-term))
(equal-variables? v-1 v-2))
(defmethod equal-terms? ((v variable-term) (f function-term))
nil)
(defmethod equal-terms? ((f function-term) (v variable-term))
nil)
(defmethod equal-terms? ((f-1 function-term) (f-2 function-term))
(when (string= (head f-1) (head f-2))
(let ((args-1 (arguments f-1))
(args-2 (arguments f-2)))
(when (length= args-1 args-2)
(every #'equal-terms? args-1 args-2)))))
(defgeneric make-variable (x)
(:documentation "Make a variable named 'X'."))
(defmethod make-variable ((x string))
(make-instance 'variable-term
:head x))
(defmethod make-variable ((x symbol))
(make-variable (symbol-name x)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Formulas
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass formula (expression)
nil)
(defun formula-p (x)
(typep x 'formula))
(defclass atomic-formula (formula atomic-expression)
nil)
(defclass equation ()
((lhs
:accessor lhs
:initarg :lhs
:initform (error "An equation needs a left-hand side."))
(rhs
:accessor rhs
:initarg :rhs
:initform (error "An equation needs a right-hand side."))))
(defmethod print-object ((x equation) stream)
(format stream "~a = ~a" (lhs x) (rhs x)))
(defclass disequation ()
((lhs
:accessor lhs
:initarg :lhs
:initform (error "A disequation needs a left-hand side."))
(rhs
:accessor rhs
:initarg :rhs
:initform (error "A disquation needs a right-hand side."))))
(defmethod print-object ((x disequation) stream)
(format stream "~a != ~a" (lhs x) (rhs x)))
(defclass verum ()
nil)
(defmethod print-object ((x verum) stream)
(format stream "$true"))
(defclass falsum ()
nil)
(defmethod print-object ((x falsum) stream)
(format stream "$false"))
(defgeneric atomic-formula-p (x)
(:documentation "Is X an atomic formula?"))
(defmethod atomic-formula-p ((x t))
nil)
(defun non-atomic-formula-p (x)
(when (formula-p x)
(not (atomic-formula-p x))))
(defmethod atomic-formula-p ((thing formula))
(or (typep thing 'atomic-formula)
(typep thing 'equation)
(typep thing 'disequation)
(typep thing 'verum)
(typep thing 'falsum)))
(defmethod print-object ((x disequation) stream)
(format stream "~a != ~a" (lhs x) (rhs x)))
(defclass composite-formula (formula)
nil)
(defclass unary-connective-formula (composite-formula)
((argument :initarg :argument
:accessor argument)))
(defclass negation (unary-connective-formula)
nil)
(defmethod print-object ((x negation) stream)
(format stream "~~~a" (argument x)))
(defgeneric negation-p (x)
(:documentation "Is X a negation?"))
(defmethod negation-p ((x t))
(typep x 'negation))
(defgeneric literal-p (x)
(:documentation "Is X a literal (atomic formula or negation of an atomic formula)?"))
(defmethod literal-p ((x t))
(or (atomic-formula-p x)
(and (negation-p x)
(atomic-formula-p (argument x)))))
(defclass binary-connective-formula (composite-formula)
((lhs :initarg :lhs
:accessor lhs
:type formula)
(rhs :initarg :rhs
:accessor rhs
:type formula)))
(defclass binary-conjunction (binary-connective-formula)
nil)
(defmethod print-object ((x binary-conjunction) stream)
(format stream "(~a & ~a)" (lhs x) (rhs x)))
(defclass binary-disjunction (binary-connective-formula)
nil)
(defmethod print-object ((x binary-disjunction) stream)
(format stream "(~a | ~a)" (lhs x) (rhs x)))
(defun binary-disjunction-p (x)
(typep x 'binary-disjunction))
(defclass implication (binary-connective-formula)
nil)
(defun implication-p (x)
(typep x 'implication))
(defmethod print-object ((x implication) stream)
(format stream "(~a => ~a)" (lhs x) (rhs x)))
(defclass reverse-implication (binary-connective-formula)
nil)
(defmethod print-object ((x reverse-implication) stream)
(format stream "(~a <= ~a)" (lhs x) (rhs x)))
(defclass equivalence (binary-connective-formula)
nil)
(defmethod print-object ((x equivalence) stream)
(format stream "(~a <=> ~a)" (lhs x) (rhs x)))
;; quantifiers
(defclass generalization (composite-formula)
((bindings :initarg :bindings
:accessor bindings
:type list)
(matrix :initarg :matrix
:accessor matrix
:type formula)))
(defgeneric generalization-p (thing)
(:documentation "Is THING a generalization?"))
(defmethod generalization-p ((thing t))
nil)
(defmethod generalization-p ((thing expression))
(typep thing 'generalization))
(defclass universal-generalization (generalization)
nil)
(defmethod print-object ((uni-gen universal-generalization) stream)
(format stream "(! [~{~a~^,~}] : ~a)" (bindings uni-gen) (matrix uni-gen)))
(defgeneric universal-generalization-p (x)
(:documentation "Is X a universal generalization (forall)?"))
(defmethod universal-generalization-p ((x t))
nil)
(defmethod universal-generalization-p ((x expression))
(typep x 'universal-generalization))
(defclass existential-generalization (generalization)
nil)
(defmethod print-object ((exi-gen existential-generalization) stream)
(format stream "(? [~{~a~^,~}] : ~a)" (bindings exi-gen) (matrix exi-gen)))
(defgeneric existential-generalization-p (x)
(:documentation "Is X an existential generalization (there exists)?"))
(defmethod existential-generalization-p ((x t))
nil)
(defmethod existential-generalization-p ((x expression))
(typep x 'existential-generalization))
(defgeneric make-atomic-formula (predicate &rest arguments))
(defmethod make-atomic-formula ((predicate symbol) &rest arguments)
(make-instance 'atomic-formula
:head (symbol-name predicate)
:arguments arguments))
(defmethod make-atomic-formula ((predicate string) &rest arguments)
(make-instance 'atomic-formula
:head predicate
:arguments arguments))
(defun make-equation (lhs rhs)
(make-atomic-formula '= lhs rhs))
(defclass composite-formula (formula)
nil)
(defun composite-formula-p (x)
"Determine whether X is non-atomic.
Unlike other predicates such as BINARY-DISJUNCTION? and
UNIVERSAL-GENERALIZATION?, this predicate does not merely test whether
the direct class of its argument is COMPOSITE-FORMULA. The class
COMPOSITE-FORMULA is defined only to provide a common superclass for
further subclasses, such as BINARY-DISJUNCTION and
UNIVERSAL-GENERALIZATION, that is intended to be disjoint from the
class ATOMIC-FORMULA. This function expresses that disjointedness."
(and (formula-p x)
(not (atomic-formula-p x))))
(defun binary-connective-formula? (thing)
(typep thing 'binary-connective-formula))
(defgeneric unnegate (formula))
(defmethod unnegate ((negation negation))
(argument negation))
(defun negation? (thing)
(typep thing 'negation))
(defgeneric negate (thing))
(defmethod negate ((formula formula))
(make-instance 'negation :argument formula))
(defclass multiple-arity-connective-formula (composite-formula)
((arguments :initarg :arguments
:accessor arguments
:type list)))
(defun implication? (thing)
(typep thing 'implication))
(defgeneric make-implication (antecedent consequent))
(defmethod make-implication ((antecedent formula) (consequent formula))
(make-instance 'implication
:lhs antecedent
:rhs consequent))
(defgeneric antecedent (formula))
(defgeneric consequent (formula))
(defmethod antecedent ((implication implication))
(lhs implication))
(defmethod consequent ((implication implication))
(rhs implication))
(defun equivalence? (thing)
(typep thing 'equivalence))
(defun make-equivalence (lhs rhs)
(make-instance 'equivalence
:lhs lhs
:rhs rhs))
;;; disjunctions
(defun binary-disjunction? (thing)
(typep thing 'binary-disjunction))
(defgeneric make-binary-disjunction (lhs rhs))
(defclass multiple-arity-disjunction (multiple-arity-connective-formula)
nil)
(defmethod print-object ((x multiple-arity-disjunction) stream)
(let ((args (arguments x)))
(cond ((null args)
(error "A multiple arity disjunction has zero arguments."))
((length= args 1)
(format stream "~a" (first args)))
(t
(loop
:initially (format stream "(~a" (first args))
:for arg :in (rest args)
:do (format stream " | ~a" arg)
:finally (format stream ")"))))))
(defun multiple-arity-disjunction? (thing)
(eql (class-of thing) 'multiple-arity-disjunction))
(defmethod make-binary-disjunction ((lhs formula) (rhs formula))
(make-instance 'binary-disjunction
:lhs lhs
:rhs rhs))
(defgeneric make-multiple-arity-disjunction (&rest disjuncts))
(defmethod make-multiple-arity-disjunction (&rest disjuncts)
(if disjuncts
(if (cdr disjuncts)
(if (cddr disjuncts)
(make-instance 'multiple-arity-disjunction
:arguments disjuncts)
(car disjuncts)))
(make-instance 'verum)))
(defun binary-disjunction->multiple-arity-disjunction (binary-disjunction)
(make-instance 'multiple-arity-disjunction
:arguments (list (lhs binary-disjunction)
(rhs binary-disjunction))))
(defun multiple-arity-disjunction->binary-disjunction (multiple-arity-disjunction)
(let ((disjuncts (arguments multiple-arity-disjunction)))
(if (null disjuncts)
(make-instance 'binary-disjunction
:lhs (make-instance 'verum)
:rhs (make-instance 'verum))
(if (null (cdr disjuncts))
(make-instance 'binary-disjunction
:lhs (first disjuncts)
:rhs (make-instance 'contradiction))
(labels ((make-disjunction (ds)
(if (null (cddr ds))
(make-binary-disjunction (first ds)
(second ds))
(make-binary-disjunction (first ds)
(make-disjunction (cdr ds))))))
(make-disjunction disjuncts))))))
;; conjunctions
(defun binary-conjunction-p (thing)
(typep thing 'binary-conjunction))
(defclass multiple-arity-conjunction (multiple-arity-connective-formula)
nil)
(defun multiple-arity-conjunction? (thing)
(eql (class-of thing) 'multiple-arity-conjunction))
(defmethod print-object ((x multiple-arity-conjunction) stream)
(let ((args (arguments x)))
(cond ((null args)
(error "A multiple arity conjunction has zero arguments."))
((length= args 1)
(format stream "~a" (first args)))
(t
(loop
:initially (format stream "(~a" (first args))
:for arg :in (rest args)
:do (format stream " & ~a" arg)
:finally (format stream ")"))))))
(defun make-binary-conjunction (lhs rhs)
(make-instance 'binary-conjunction
:lhs lhs
:rhs rhs))
(defun make-multiple-arity-conjunction (&rest conjuncts)
(if conjuncts
(if (rest conjuncts)
(if (rest (rest conjuncts))
(make-instance 'multiple-arity-conjunction
:arguments conjuncts))
(first conjuncts))
(make-instance 'falsum)))
(defun binary-conjunction->multiple-arity-conjunction (binary-conjunction)
(make-instance 'multiple-arity-conjunction
:arguments (list (lhs binary-conjunction)
(rhs binary-conjunction))))
(defun multiple-arity-conjunction->binary-conjunction (multiple-arity-conjunction)
(let ((conjuncts (arguments multiple-arity-conjunction)))
(if (null conjuncts)
(make-binary-conjunction (make-instance 'falsum)
(make-instance 'falsum))
(if (null (cdr conjuncts))
(make-instance 'binary-conjunction
:lhs (first conjuncts)
:rhs (make-instance 'verum))
(labels ((make-conjunction (ds)
(if (null (cddr ds))
(make-binary-conjunction (first ds)
(second ds))
(make-binary-conjunction (first ds)
(make-conjunction (cdr ds))))))
(make-conjunction conjuncts))))))
(defun universal-generalization? (thing)
(eql (class-of thing) 'universal-generalization))
(defun existential-generalization? (thing)
(eql (class-of thing) 'existential-generalization))
(defun make-universal (var formula)
(make-instance 'universal-generalization
:bindings (list var)
:matrix formula))
(defun make-existential (var formula)
(make-instance 'existential-generalization
:bindings (list var)
:matrix formula))
(defun account-for-extension (constants predicate)
"Make a formula saying that the extension of PREDICATE is exhausted
by the list CONSTANTS of constant symbols. E.g,
\(ACCOUNT-FOR-EXTENSION '(A B C) 'VERTEX\)
should return the formula
\(ALL ?X (IMPLIES (VERTEX ?X) (OR (= ?X A) (= ?X B) (= ?X C)))\)"
(let ((var (make-variable "x")))
(make-universal var
(make-implication
(make-atomic-formula predicate var)
(apply #'make-multiple-arity-disjunction
(mapcar #'(lambda (constant)
(make-equation var constant))
constants))))))
(defgeneric proper-subformulas-1 (formula))
(defmethod proper-subformulas-1 ((formula atomic-formula))
nil)
(defmethod proper-subformulas-1 ((negation negation))
(let ((inside (unnegate negation)))
(cons inside (proper-subformulas-1 inside))))
(defmethod proper-subformulas-1 ((formula binary-connective-formula))
(let ((lhs (lhs formula))
(rhs (rhs formula)))
(append (list lhs rhs)
(proper-subformulas-1 lhs)
(proper-subformulas-1 rhs))))
(defmethod proper-subformulas-1 ((formula multiple-arity-connective-formula))
(let ((arguments (arguments formula)))
(append arguments
(mapcar #'proper-subformulas-1 arguments))))
(defmethod proper-subformulas-1 ((formula generalization))
(let ((matrix (matrix formula)))
(cons matrix (proper-subformulas-1 matrix))))
(defun proper-subformulas (formula)
(remove-duplicates (proper-subformulas-1 formula) :test #'equal-formulas?))
(defun proper-subformula-occurrences (formula)
(proper-subformulas-1 formula))
(defgeneric subst-term-for-var-in-term (term var target-term))
(defmethod subst-term-for-var-in-term ((term term)
(var variable-term)
(target-term variable-term))
(if (equal-variables? var target-term)
term
target-term))
(defmethod subst-term-for-var-in-term ((term term)
(var variable-term)
(target-term function-term))
(let ((f (function-symbol target-term))
(args (arguments target-term)))
(apply #'make-function-term
f
(mapcar #'(lambda (x) (subst-term-for-var-in-term term var x))
args))))
(defgeneric instantiate (term variable formula)
(:documentation "Substitute TERM for free occurances of VARIBLE in FORMULA.
WARNING: No regard is given to variables appearing in TERM that may become
bound once the substitution is carried out: no renaming is done either
in TERM or FORMULA."))
(defmethod instantiate (term variable (formula atomic-formula))
(let ((pred (head formula))
(args (arguments formula)))
(apply #'make-atomic-formula
pred
(mapcar #'(lambda (arg)
(subst-term-for-var-in-term term variable arg))
args))))
(defmethod instantiate (term variable (formula binary-connective-formula))
(make-instance (class-of formula)
:lhs (instantiate term variable (lhs formula))
:rhs (instantiate term variable (rhs formula))))
(defmethod instantiate (term variable (formula multiple-arity-connective-formula))
(make-instance (class-of formula)
:arguments (mapcar #'(lambda (item)
(instantiate term variable item))
(arguments formula))))
(defmethod instantiate (term variable (formula generalization))
(let ((bindings (bindings formula))
(matrix (matrix formula)))
(if (member variable bindings :test #'equal-variables?)
(let ((other-bindings (remove-if-not #'(lambda (x)
(equal-variables? x variable))
bindings))
(subst (subst-term-for-var-in-term term variable matrix)))
(if (null other-bindings)
subst
(make-instance (class-of formula)
:bindings other-bindings
:matrix subst)))
(make-instance (class-of formula)
:bindings bindings
:matrix (instantiate term variable matrix)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Sundry formula-related utilities
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric equal-formulas? (formula-1 formula-2))
(defmethod equal-formulas? ((formula-1 t) (formula-2 t))
"By default, if no other generic function applies, the answer is no."
nil)
(defmethod equal-formulas? ((form-1 atomic-formula) (form-2 atomic-formula))
(and (string= (head form-1)
(head form-2))
(every-pair #'(lambda (term-1 term-2)
(equal-terms? term-1 term-2))
(arguments form-1)
(arguments form-2))))
(defmethod equal-formulas? ((form-1 negation) (form-2 negation))
(equal-formulas? (unnegate form-1)
(unnegate form-2)))
(defmethod equal-formulas? ((form-1 binary-connective-formula)
(form-2 binary-connective-formula))
(and (eql (class-of form-1) (class-of form-2))
(equal-formulas? (lhs form-1)
(lhs form-2))
(equal-formulas? (rhs form-1)
(rhs form-2))))
(defmethod equal-formulas? ((form-1 multiple-arity-connective-formula)
(form-2 multiple-arity-connective-formula))
(when (eql (class-of form-1) (class-of form-2))
(let ((args-1 (arguments form-1))
(args-2 (arguments form-2)))
(when (length= args-1 args-2)
(every-pair #'equal-formulas? args-1 args-2)))))
(defmethod equal-formulas? ((form-1 generalization)
(form-2 generalization))
(let ((bindings-1 (bindings form-1))
(bindings-2 (bindings form-2)))
(when (subsetp bindings-1 bindings-2 :test #'equal-variables?)
(when (subsetp bindings-2 bindings-1 :test #'equal-variables?)
(equal-formulas? (matrix form-1) (matrix form-2))))))
(defun contains-formula? (lst formula)
(member formula lst :test #'equal-formulas?))
(defun equal-atomic-formulas? (formula-1 formula-2)
(and (atomic-formula-p formula-1)
(atomic-formula-p formula-2)
(equal-formulas? formula-1 formula-2)))
(defgeneric contains-contradiction-p (x)
(:documentation "Is a contradiction (bottom) found anywhere inside X?"))
(defmethod contains-contradiction-p ((x verum))
nil)
(defmethod contains-contradiction-p ((x falsum))
t)
(defmethod contains-contradiction-p ((x atomic-formula))
nil)
(defmethod contains-contradiction-p ((x negation))
(contains-contradiction-p (argument x)))
(defmethod contains-contradiction-p ((x binary-conjunction))
(or (contains-contradiction-p (lhs x))
(contains-contradiction-p (rhs x))))
(defmethod contains-contradiction-p ((x binary-disjunction))
(or (contains-contradiction-p (lhs x))
(contains-contradiction-p (rhs x))))
(defmethod contains-contradiction-p ((x implication))
(or (contains-contradiction-p (antecedent x))
(contains-contradiction-p (consequent x))))
(defmethod contains-contradiction-p ((x equivalence))
(or (contains-contradiction-p (lhs x))
(contains-contradiction-p (rhs x))))
(defmethod contains-contradiction-p ((x multiple-arity-conjunction))
(some #'contains-contradiction-p (arguments x)))
(defmethod contains-contradiction-p ((x multiple-arity-disjunction))
(some #'contains-contradiction-p (arguments x)))
(defmethod contains-contradiction-p ((x generalization))
(contains-contradiction-p (matrix x)))
(defgeneric contains-verum-p (x)
(:documentation "Is a verum (top) found anywhere inside X?"))
(defmethod contains-verum-p ((x atomic-formula))
nil)
(defmethod contains-verum-p ((x negation))
(contains-verum-p (argument x)))
(defmethod contains-verum-p ((x binary-conjunction))
(or (contains-verum-p (lhs x))
(contains-verum-p (rhs x))))
(defmethod contains-verum-p ((x binary-disjunction))
(or (contains-verum-p (lhs x))
(contains-verum-p (rhs x))))
(defmethod contains-verum-p ((x implication))
(or (contains-verum-p (antecedent x))
(contains-verum-p (consequent x))))
(defmethod contains-verum-p ((x equivalence))
(or (contains-verum-p (lhs x))
(contains-verum-p (rhs x))))
(defmethod contains-verum-p ((x generalization))
(contains-verum-p (matrix x)))
(defmethod contains-verum-p ((x multiple-arity-conjunction))
(some #'contains-verum-p (arguments x)))
(defmethod contains-verum-p ((x multiple-arity-disjunction))
(some #'contains-verum-p (arguments x)))
(defgeneric contains-quantifier-p (x)
(:documentation "Is a quantifier found anywhere inside X?"))
(defmethod contains-quantifier-p ((x atomic-formula))
nil)
(defmethod contains-quantifier-p ((x negation))
(contains-quantifier-p (argument x)))
(defmethod contains-quantifier-p ((x binary-conjunction))
(or (contains-quantifier-p (lhs x))
(contains-quantifier-p (rhs x))))
(defmethod contains-quantifier-p ((x binary-disjunction))
(or (contains-quantifier-p (lhs x))
(contains-quantifier-p (rhs x))))
(defmethod contains-quantifier-p ((x implication))
(or (contains-quantifier-p (antecedent x))
(contains-quantifier-p (consequent x))))
(defmethod contains-quantifier-p ((x equivalence))
(or (contains-quantifier-p (lhs x))
(contains-quantifier-p (rhs x))))
(defmethod contains-quantifier-p ((x multiple-arity-conjunction))
(some #'contains-quantifier-p (arguments x)))
(defmethod contains-quantifier-p ((x multiple-arity-disjunction))
(some #'contains-quantifier-p (arguments x)))
(defmethod contains-quantifier-p ((x generalization))
t)
(defgeneric contains-equation-p (x)
(:documentation "Is an equation found anywhere inside X?"))
(defmethod contains-equation-p ((x equation))
t)
(defmethod contains-equation-p ((x disequation))
t)
(defmethod contains-equation-p ((x atomic-formula))
nil)
(defmethod contains-equation-p ((x unary-connective-formula))
(contains-equation-p (argument x)))
(defmethod contains-equation-p ((x binary-connective-formula))
(or (contains-equation-p (lhs x))
(contains-equation-p (rhs x))))
(defmethod contains-equation-p ((x generalization))
(contains-equation-p (matrix x)))
(defmethod contains-equation-p ((x multiple-arity-connective-formula))
(some #'contains-equation-p (arguments x)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Reading formulas
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-condition malformed-formula-error (error)
((text :initarg :text
:reader malformed-formula-error-text))
(:report (lambda (condition stream)
(let ((text (malformed-formula-error-text condition)))
(if (null text)
(format stream
"Weird: no text was given.~%")
(format stream
"The given text,~%~% ~A,~%~%is not a formula."
text))))))
(define-condition parse-form-empty-argument-list-error (error)
((op :initarg :op
:reader operator))
(:report (lambda (condition stream)
(let ((op (operator condition)))
(format stream
"The operator ~A expects at least one argument, but none were supplied."
op)))))
(define-condition parse-form-at-least-two-args-expected-but-only-one-supplied-error (error)
((op :initarg :op
:reader operator)
(first-arg :initarg :first-arg
:reader first-argument))
(:report (lambda (condition stream)
(let ((op (operator condition))
(first-arg (first-argument condition)))
(format stream
"The operator ~A requires at least two arguments, but only one,~%~% ~A,~%~%was supplied."
op first-arg)))))
(define-condition parse-form-unary-operator-multiple-arguments-error (error)
((op :initarg :op
:reader operator)
(args :initarg :args
:reader arguments))
(:report (lambda (condition stream)
(let* ((op (operator condition))
(args (arguments condition))
(num-args (length args)))
(format stream
"The unary operator ~A expects exactly one argument, but multiple arguments were supplied:~%~%"
op)
(loop
for i from 1 upto num-args
for arg in args
do
(format stream "[~d] ~A" i arg))))))
(define-condition parse-form-exactly-two-args-expected-but-at-least-three-supplied-error (error)
((op :initarg op
:reader operator)
(args :initarg :args
:reader arguments))
(:report (lambda (condition stream)
(let* ((op (operator condition))
(args (arguments condition))
(num-args (length args)))
(format stream "The binary operator ~A expects exactly two arguments, but at least three arguments were supplied:~%~%" op)
(loop
for i from 1 upto num-args
for arg in args
do
(format stream "[~d] ~A" i arg))))))
(define-condition parse-form-formula-expected-error (error)
((op :initarg :op
:reader operator)
(form :initarg :form
:reader form))
(:report (lambda (condition stream)
(let ((op (operator condition))
(form (form condition)))
(format stream "The operator ~A expects an formula for one of its arguments, but a non-formula was given in that argument position.~%" op)
(format stream "The given argument was~%~% ~A~%~%This form cannot be understood as a formula." form)))))
(define-condition parse-form-variable-expected (error)
((op :initarg :op
:reader operator)
(form :initarg :form
:reader form))
(:report (lambda (condition stream)
(let ((op (operator condition))
(form (form condition)))
(format stream "The operator ~A expects an variable for one of its arguments, but a non-variable was given in that argument position.~%" op)
(format stream "The given argument was~%~% ~A~%~%This form cannot be understood as a variable." form)))))
(define-condition parse-form-empty-list-supplied-error (error)
()
(:report (lambda (condition stream)
(declare (ignore condition))
(format stream "The empty list cannot be understood as either a formula or a term."))))
(defun try-another-formula (c)
(declare (ignore c))
(let ((restart (find-restart 'try-another-formula)))
(when restart
(invoke-restart 'try-another-formula))))
(defun read-formula (&optional (stream *standard-input*))
(declare (ignore stream))
(error "READ-FORMULA is dead."))
(defun parse-formula (str)
(with-input-from-string (s str)
(read-formula s)))
(defun read-new-formula ()
(format t "Enter a new formula: ")
(multiple-value-list (read-formula)))
(defun read-atomic-formula ()
(let (response)
(until (atomic-formula-p response)
(read-formula))
response))
(defun read-composite-formula (&optional (stream *standard-input*))
(declare (ignore stream))
(error "READ-COMPOSITE-FORMULA is dead."))
(defgeneric uniquify-atoms (formula)
(:documentation "Ensure that all the atoms of FORMULA are distinct objects, even if they have the same print name. (We treat only the propositional case.)"))
(defmethod uniquify-atoms ((atom atomic-formula))
(make-instance 'atomic-formula
:head (head atom)
:arguments nil))
(defmethod uniquify-atoms ((formula unary-connective-formula))
(make-instance (class-of formula)
:argument (uniquify-atoms (argument formula))))
(defmethod uniquify-atoms ((formula binary-connective-formula))
(make-instance (class-of formula)
:lhs (uniquify-atoms (lhs formula))
:rhs (uniquify-atoms (rhs formula))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Sorting formulas
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric formula-< (formula-1 formula-2))
(defmethod formula-< ((formula-1 atomic-formula) (formula-2 atomic-formula))
(let ((pred-1 (head formula-1))
(pred-2 (head formula-2)))
(lex< pred-1 pred-2)))
(defmethod formula-< ((formula-1 atomic-formula) (formula-2 t))
t)
(defmethod formula-< ((formula-1 unary-connective-formula) (formula-2 atomic-formula))
nil)
(defmethod formula-< ((formula-1 unary-connective-formula) (formula-2 unary-connective-formula))
(formula-< (argument formula-1)
(argument formula-2)))
(defmethod formula-< ((formula-1 unary-connective-formula) (formula-2 binary-connective-formula))
t)
(defmethod formula-< ((formula-1 binary-connective-formula) (formula-2 atomic-formula))
nil)
(defmethod formula-< ((formula-1 binary-connective-formula) (formula-2 unary-connective-formula))
nil)
(defmethod formula-< ((formula-1 binary-connective-formula) (formula-2 binary-connective-formula))
(or (formula-< (lhs formula-1)
(lhs formula-2))
(formula-< (rhs formula-1)
(rhs formula-2))))
(defgeneric appears-in (term thing)
(:documentation "Does TERM appear in THING?"))
(defmethod appears-in ((term term) (thing atomic-formula))
(some #'(lambda (x)
(appears-in term x))
(arguments thing)))
(defmethod appears-in ((term term) (thing term))
(or (equal-terms? term thing)
(some #'(lambda (x)
(appears-in term x))
(arguments term))))
(defmethod appears-in ((term term) (thing negation))
(appears-in term (argument thing)))