-
Notifications
You must be signed in to change notification settings - Fork 1
/
geta.scm
77 lines (65 loc) · 2.04 KB
/
geta.scm
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
(define (delete1 x xs)
(cond ((null? xs)
(error "not found" x))
((equal? x (car xs))
(cdr xs))
(else
(cons (car xs)
(delete1 x (cdr xs))))))
;; (delete1 2 '(0 1 2 3 4 5 2 6))
;; (delete1 9 '(0 1 2 3 4 5 2 6))
(define (consume bag1 bag2)
(if (null? bag2)
bag1
(consume (delete1 (car bag2) bag1) (cdr bag2))))
;; (consume '(a b c p p d) '(p c d))
(define (equiv-expr? x)
(and (pair? x) (eq? (car x) '=)))
(define (flatten-equiv x)
(if (equiv-expr? x)
(let ((r (append-map
(lambda (y)
(let ((z (flatten-equiv y)))
(if (equiv-expr? z)
(cdr z)
(list z))))
(cdr x))))
(if (null? (cdr r))
(car r)
(cons '= r)))
x))
;; (flatten-equiv '(= (= a b) c))
;; (flatten-equiv '(= p))
;; (flatten-equiv '(or p (not p)))
;;; theorem ==> theorem[var := expr]
(define (substitution theorem var expr)
(flatten-equiv
(cond ((eq? theorem var) expr)
((pair? theorem)
(cons (substitution (car theorem) var expr)
(substitution (cdr theorem) var expr)))
(else theorem))))
;; (substitution '(= (not (= p q)) (not p) q) 'q 'p)
;;; P, P = Q ==> Q
(define (equanimity P theorem)
(if (equiv-expr? theorem)
(flatten-equiv
(cons '=
(consume
(cdr theorem)
(if (and (pair? P) (eq? (car P) '=))
(cdr P)
(list P)))))
(error "theorem must be an equation" theorem)))
;; (equanimity 'p '(= p true))
;; (equanimity '(= q r) '(= p q r s))
;;; P = Q ==> expr[var:=P] = expr[var:=Q]
(define (leibniz theorem expr var P)
(let ((Q (equanimity P theorem)))
(flatten-equiv
(list '=
(substitution expr var P)
(substitution expr var Q)))))
;; (leibniz '(= a b c d) '(or p (not p)) 'p 'b)
;; (leibniz '(= a b c d) '(or p (not p)) 'p '(= c a))
;; (leibniz '(= a b c d) '(or p (not p)) 'p '(= b d c))