summaryrefslogtreecommitdiff
path: root/vikalpa/prelude.scm
blob: 264773d86574588f75d1858f38745cdf145c791c (plain)
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
;;; Vikalpa --- Proof Assistant
;;; Copyright © 2020 Masaya Tojo <masaya@tojo.tokyo>
;;;
;;; This file is part of Vikalpa.
;;;
;;; Vikalpa is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Vikalpa is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Vikalpa.  If not, see <http://www.gnu.org/licenses/>.

(define-module (vikalpa prelude)
  #:export (atom? size implies natural? prelude)
  #:use-module (vikalpa))

(define (atom? x)
  (not (pair? x)))

(define (natural? x)
  (and (integer? x)
       (not (negative? x))))

(define (size x)
  (if (pair? x)
      (+ 1
         (size (car x))
         (size (cdr x)))
      0))

(define-syntax implies
  (syntax-rules ()
    ((_ x y) (if x y #t))
    ((_ x y z rest ...)
     (if x (implies y z rest ...) #t))))

(define-system prelude ()
  (define-function atom? (x)
    (not (pair? x)))
  (define-syntax-rules list ()
    ((list) '())
    ((list x . y) (cons x (list . y))))
  (define-syntax-rules and ()
    ((and) '#t)
    ((and x) x)
    ((and x . xs) (if x (and . xs) #f)))
  (define-syntax-rules or ()
    ((or) '#f)
    ((or x) x)
    ((or x . xs) (if x x (or . xs))))
  (define-syntax-rules cond (else)
    ((cond (else e)) e)
    ((cond (test then) . rest)
     (if test then (cond . rest))))
  (define-syntax-rules implies ()
    ((implies x y) (if x y #t))
    ((implies x y z . rest)
     (if x (implies y z . rest) #t)))

  (define-axiom equal-same (x)
    (equal? (equal? x x) '#t))
  (define-axiom equal-swap (x y)
    (equal? (equal? x y) (equal? y x)))
  (define-axiom equal-if (x y)
    (implies (equal? x y) (equal? x y)))
  (define-axiom atom/cons (x y)
    (equal? (atom? (cons x y)) '#f))
  (define-axiom car/cons (x y)
    (equal? (car (cons x y)) x))
  (define-axiom cdr/cons (x y)
    (equal? (cdr (cons x y)) y))
  (define-axiom cons/car+cdr (x)
    (if (atom? x)
        '#t
        (equal? (cons (car x) (cdr x)) x)))
  (define-axiom if-nest (x y z)
    (if x
        (equal? (if x y z) y)
        (equal? (if x y z) z)))
  (define-axiom if-true (x y)
    (equal? (if '#t x y) x))
  (define-axiom if-false (x y)
    (equal? (if '#f x y) y))
  (define-axiom if-same (x y)
    (equal? (if x y y) y))
  (define-axiom natural?/size (x)
    (equal? (natural? (size x))
            '#t))
  (define-axiom </size/car (x)
    (if (atom? x)
        '#t
        (equal? (< (size (car x)) (size x))
                '#t)))
  (define-axiom </size/cdr (x)
    (if (atom? x)
        '#t
        (equal? (< (size (cdr x)) (size x))
                '#t)))
  (define-axiom if-not (x y z)
    (equal? (if (not x) y z)
            (if x z y)))
  (define-axiom sub1/add1 (x)
    (implies (natural? x)
             (equal? (sub1 (add1 x)) x)))
  (define-axiom natural?/0 (x)
    (equal? (natural? '0) '#t))
  (define-axiom natural?/add1 (x)
    (implies (natural? x)
             (equal? (natural? (add1 x)) '#t)))
  (define-axiom sub1/add1 (x)
    (implies (natural? x)
             (equal? (sub1 (add1 x)) x)))
  (define-axiom </sub1 (x)
    (implies (natural? x)
             (equal? (< (sub1 x) x) '#t)))
  (define-axiom common-add1 (x y)
    (implies (natural? x)
             (natural? y)
             (equal? (equal? (add1 x) (add1 y))
                     (equal? x y))))
  (define-axiom false-if (x)
    (if x '#t (equal? x '#f)))
  (define-axiom equal?/01 (x)
    (equal? (equal? '0 '1) #f))
  (define-axiom natural?/sub1 (x)
    (if (natural? x)
        (if (equal? '0 x)
            '#t
            (equal? (natural? (sub1 x)) '#t))
        '#t))
  (define-axiom add1/sub1 (x)
    (if (natural? x)
        (if (equal? '0 x)
            '#t
            (equal? (add1 (sub1 x)) x))
        '#t))
  
  (define-primitive-function + (x y)
    (if (natural? x)
        (if (equal? '0 x)
            y
            (add1 (+ (sub1 x) y)))
        'undefined))

  (define-proof +
    (total/natural? x)
    (((2) if-nest)
     ((2 3) </sub1)
     ((2) if-same)
     (() if-same)))

  (define-theorem thm-1+1=2 ()
    (equal? (+ 1 1) 2))
  
  (define-function natural-induction (n)
    (if (natural? n)
        (if (equal? '0 n)
            '0
            (add1 (natural-induction (sub1 n))))
        'undefined))

  (define-proof natural-induction
    (total/natural? n)
    (((2) if-nest)
     ((2 3) </sub1)
     ((2) if-same)
     (() if-same)))

  (define-theorem equal?/add1 (n)
    (if (natural? n)
        (equal? (equal? n (add1 n)) #f)
        #t))
  
  (define-proof equal?/add1
    (induction (natural-induction n))
    (((2 2 2 1 1) equal-if)
     ((2 2 2 1 2 1) equal-if)
     ((2 2 2 1) equal?/01)
     ((2 2 2) equal-same)
     ((2 2) if-same)
     ((2 3 1 1) natural?/sub1)
     ((2 3 1) if-true)
     ((2 3 2 2 1 1) add1/sub1)
     ((2 3 2 2 1 2 1) add1/sub1)
     ((2 3 2 2) if-same (set x (natural? (add1 (sub1 n)))))
     ((2 3 2 2) if-same (set x (natural? (sub1 n))))
     ((2 3 2 2 2 2 1) common-add1
      ;; ルール探索のアルゴリズムにバグがある
      (set x (sub1 n))
      (set y (add1 (sub1 n))))
     ((2 3 2 2 2 2 1) equal-if)
     ((2 3 2 2 2 2) equal-same)
     ((2 3 2 2 2 1) natural?/add1)
     ((2 3 2 2 2) if-true)
     ((2 3 2 2 1) natural?/sub1)
     ((2 3 2 2) if-true)
     ((2 3 2) if-same)
     ((2 3) if-same)
     ((2) if-same)
     ((3) if-nest)
     (() if-same)))
  
  (define-proof thm-1+1=2
    ()
    ((() if-same (set x (natural? '0)))
     ((2 1) +)
     ((2 1 2 2 1) equal-if)
     ((2 1 2 3 1 1) sub1/add1)
     ((2 1 2 3 1) +)
     ((2 1 2 3 1 2 1) equal-same)
     ((2 1 2 3 1 2) if-true)
     ((2 1 2 3 1 1) natural?/0)
     ((2 1 2 3 1) if-true)
     ((2 1 2) if-same)
     ((2 1 1) natural?/add1)
     ((2 1) if-true)
     ((2) equal-same)
     ((1) natural?/0)
     (() if-true))))