summaryrefslogtreecommitdiff
path: root/vikalpa/the-little-prover.scm
blob: fe7735ece1bab94e3e4fed5011f12a92b4e210e8 (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
;;; 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 the-little-prover)
  #:export (atom? nat? the-little-prover)
  #:use-module (vikalpa))

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

(define (nat? x)
  (and (integer? x)
       (<= 0 x)))

(define-system the-little-prover ()
  (define-core-function atom? (x) atom?)
  (define-core-function nat? (x) nat?)
  (define-core-function < (x y)
    (lambda (x y)
      (if (number? x)
          (if (number? y)
              (< x y)
              (< x 0))
          (if (number? y)
              (< 0 y)
              #f))))
  (set-measure-predicate nat?)
  (set-measure-less-than <)
  (define-core-function + (x y)
    (lambda (x y)
      (if (number? x)
          (if (number? y)
              (+ x y)
              x)
          (if (number? y)
              y
              0))))
  (define-core-function cons (x y) cons)
  (define-core-function car (x)
    (lambda (x)
      (if (atom? x) '() (car x))))
  (define-core-function cdr (x)
    (lambda (x)
      (if (atom? x) '() (cdr x))))
  (define-trivial-total-function size (x)
    (if (atom? x)
        0
        (+ 1
           (+ (size (car x))
              (size (cdr x))))))

  ;; Axioms of Equal
  (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)
    (if (equal? x y) (equal? x y) #t))

  ;; Axioms of Cons
  (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)))

  ;; Axioms of If
  (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 if-nest-A (x y z)
    (if x (equal? (if x y z) y) #t))
  (define-axiom if-nest-E (x y z)
    (if x #t (equal? (if x y z) z)))

  ;; Axioms of Size
  (define-axiom nat/size (x)
    (equal? (nat? (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)))

  ;; Axioms of `+` and `<`
  (define-axiom identity-+ (x)
    (if (nat? x)
        (equal? (+ 0 x) x)
        #t))
  (define-axiom commute-+ (x y)
    (equal? (+ x y) (+ y x)))
  (define-axiom associate-+ (x y z)
    (equal? (+ (+ x y) z) (+ x (+ y z))))
  (define-axiom positive-+ (x y)
    (if (< '0 x)
        (if (< '0 y)
            (equal? (< '0 (+ x y)) #t)
            #t)
        #t))
  (define-axiom nat/+ (x y)
    (if (nat? x)
        (if (nat? y)
            (equal? (nat? (+ x y)) #t)
            #t)
        #t))
  (define-axiom common-addends-< (x y z)
    (equal? (< (+ x z) (+ y z))
            (< x y)))
  
  ;; Prelude
  (define-function list-induction (x)
    (if (atom? x)
        x
        (cons (car x)
              (list-induction (cdr x)))))

  (define-function star-induction (x)
    (if (atom? x)
        x
        (cons (star-induction (car x))
              (star-induction (cdr x)))))

  (define-proof list-induction
    (size x)
    (((2 3) size/cdr)
     ((2) if-same)
     ((1) nat/size)
     (() if-true)))

  (define-proof star-induction
    (size x)
    (((2 3 1) size/car)
     ((2 3 2) size/cdr)
     ((2 3) if-true)
     ((2) if-same)
     ((1) nat/size)
     (() if-true))))