File: bv-add.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (289 lines) | stat: -rw-r--r-- 8,856 bytes parent folder | download | duplicates (6)
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
(in-package "ACL2")

(include-book "textbook/chap11/perm-append" :dir :system)

;; --------------------------------------------------------------

;; bv-add definition and some theorems about it

(include-book "arithmetic-3/floor-mod/floor-mod" :dir :system)

(defund bv-add (x y)
  (mod (+ (nfix x) (nfix y)) 32))

(defun bv-add-lex-< (x y)
  (lexorder x y))

(defthm bv-commute
  (implies (syntaxp (not (bv-add-lex-< x y)))
           (equal (bv-add x y)
                  (bv-add y x)))
  :hints (("Goal" :in-theory (enable bv-add))))

(defthm bv-commute-2
  (equal (bv-add x (bv-add y z))
         (bv-add y (bv-add x z)))
  :hints (("Goal" :in-theory (enable bv-add)))
  :rule-classes nil)

(defun make-bv-add (sym-list)
  (cond
   ((atom sym-list)
    nil)
   ((atom (cdr sym-list))
    (car sym-list))
   (t
    `(bv-add ,(car sym-list)
             ,(make-bv-add (cdr sym-list))))))

(defevaluator evl evl-list
  ((if a b c) (bv-add x y)))

(defthm not-consp-cddr-perm
  (implies
   (and (perm x y)
        (not (consp (cddr x))))
   (not (consp (cddr y))))
  :rule-classes nil)

(defthm perm-cdr
  (implies
   (and (perm x y)
        (not (consp (cdr x))))
   (not (consp (cdr y))))
  :rule-classes nil)

(defthm del-implies-bv-add-equality-2-0
  (IMPLIES (AND (CONSP X)
                (CONSP (CDR X))
                (IN A (CDR X))
                (NOT (EQUAL A (CAR X)))
                (NOT (CONSP (DEL A (CDR X)))))
           (EQUAL (BV-ADD (EVL (CAR X) ENV)
                          (EVL (MAKE-BV-ADD (CDR X)) ENV))
                  (BV-ADD (EVL A ENV)
                          (EVL (CAR X) ENV))))
  :hints (("Goal" :expand (in a (cdr x)))))

(defthm del-implies-bv-add-equality-2
  (implies (and (in a x)
                (consp x)
                (consp (cdr x)))
           (equal (evl (make-bv-add x) env)
                  (bv-add (evl a env) (evl (make-bv-add (del a x)) env))))
  :hints (("Subgoal *1/4.2'" :use ((:instance bv-commute-2 (x (evl a env))
                                                (y (evl (car x) env))
                                                (z (evl (make-bv-add (del a
                                                                          (cdr
                                                                           x)))
                                                        env)))))
          ("Subgoal *1/4.1" :in-theory (disable make-bv-add evl-constraint-1)))
  :rule-classes nil)

(defthm perm-implies-bv-add-equality
  (implies
   (perm sym-list perm-list)
   (equal (evl (make-bv-add sym-list) env)
          (evl (make-bv-add perm-list) env)))
  :hints (("Goal" :induct (perm sym-list perm-list))
          ("Subgoal *1/2"
           :use ((:instance del-implies-bv-add-equality-2
                            (a (car sym-list))
                            (x perm-list)
                            (env env))
                 (:instance perm-cdr
                            (x sym-list)
                            (y perm-list))))))

;; --------------------------------------------------------------

;; Definition and proof of Clause Processor

;; For now, lexorder will do.
(defun bv-add-lex-< (x y)
  (lexorder x y))

(defun split-list (x)
  (cond ((atom x) (mv nil nil))
	((atom (cdr x)) (mv x nil))
	(t (mv-let (a b)
		   (split-list (cddr x))
		   (mv (cons (car x) a) (cons (cadr x) b))))))

(defun merge2 (x y)
  (declare (xargs :measure (+ (acl2-count x) (acl2-count y))))
  (cond ((atom x) y)
	((atom y) x)
	((bv-add-lex-< (car x) (car y))
	 (cons (car x) (merge2 (cdr x) y)))
	(t (cons (car y) (merge2 x (cdr y))))))

(defthm split-list-smaller
  (implies (and (consp x) (consp (cdr x)))
           (and (< (acl2-count (car (split-list x)))
                   (acl2-count x))
                ;; Originally we used (cadr ..) instead of (mv-nth 1 ..) below,
                ;; but the mv-nth didn't open up to cadr in the termination
                ;; proof of mergesort, so we are going with mv-nth below.
                (< (acl2-count (mv-nth 1 (split-list x)))
                   (acl2-count x)))))

(defun mergesort (x)
  (cond ((atom x) nil)
	((atom (cdr x)) x)
	(t (mv-let (a b)
		   (split-list x)
		   (merge2 (mergesort a) (mergesort b))))))

(defthm perm-append
  (perm (append x y) (append y x)))

(defthm perm-append-del
  (implies (and (consp y)
                (in (car y) x))
           (perm (append (del (car y) x) y)
                 (append x (cdr y)))))

(defthm merge2-is-append
  (perm (merge2 x y)
        (append x y))
  :hints (("Goal" :induct (merge2 x y))))

(defthm perm-append-cons-2
  (perm (append x (cons a y))
        (cons a (append x y))))

(defthm perm-append-split-list
  (perm (append (car (split-list lst)) (mv-nth 1 (split-list lst)))
        lst))

(defthm perm-mergesort
  (perm (mergesort lst) lst))

(defun simplify-bv-adds-in-flg-num (flg)
  (cond
   ((equal 'expr-list flg)
    0)
   ((equal 'expr flg)
    1)
   (t ;;(equal 'bv-add flg)
    0)))

(defun simplify-bv-adds-in (flg x)
  (declare (xargs :measure
                  (list* (cons 1 (1+ (acl2-count x)))
                         (simplify-bv-adds-in-flg-num flg))))
  (cond
   ((equal 'expr-list flg)
    (cond
     ((atom x)
      nil)
     (t
      (cons (simplify-bv-adds-in 'expr (car x))
            (simplify-bv-adds-in 'expr-list (cdr x))))))

   ((equal 'expr flg)
    (cond
     ((atom x)
      x)
     ((quotep x)
      x)
     ((equal (car x) 'bv-add)
      (let* ((add-lst (simplify-bv-adds-in 'bv-add x))
             (add-lst (mergesort add-lst)))
        (make-bv-add add-lst)))

     (t
      (cons (car x)
            (simplify-bv-adds-in 'expr-list (cdr x))))))

   (t ;;(equal 'bv-add flg)
    ;; x=(bv-add a0 a1)
    (cond
     ((or (atom x) (not (equal (car x) 'bv-add)))
      ;; This case makes no sense but I added
      ;; it to make the termination proof easy.
      nil)

     (t
      (let ((a0 (cadr x))
            (a1 (caddr x)))
        (cond
         ((and (consp a1)
               (equal 'bv-add (car a1)))
          (cons (simplify-bv-adds-in 'expr a0)
                (simplify-bv-adds-in 'bv-add a1)))
         (t
          (list (simplify-bv-adds-in 'expr a0)
                (simplify-bv-adds-in 'expr a1))))))))))

(defun bv-add-sort-cp (clause)
  (list (simplify-bv-adds-in 'expr-list clause)))

(in-theory (disable mergesort perm))

(defthm bv-add-merge-sort-ok
  (implies
   (and (consp sym-lst)
        (consp (cdr sym-lst)))
   (equal (evl (make-bv-add (mergesort sym-lst)) env)
          (evl (make-bv-add sym-lst) env)))
  :hints (("Goal" :use ((:instance perm-implies-bv-add-equality
                                   (sym-list sym-lst)
                                   (perm-list (mergesort sym-lst))
                                   (env env))))))

(defthm lemma0
  (implies (and (consp x1)
                (equal fn (car x1))
                (not (equal fn 'quote))
                (equal (evl-list args0 env) (evl-list (cdr x1) env)))
           (equal (equal (evl (cons fn args0) env) (evl x1 env))
                  t))
  :hints (("Goal" :in-theory (enable evl-constraint-0))))

(defthm correctness-of-bv-adds-in
  (and (implies
        (equal flg 'expr)
        (equal (evl (simplify-bv-adds-in flg x) env)
               (evl x env)))

       (implies
        (equal flg 'expr-list)
        (equal (evl-list (simplify-bv-adds-in flg x) env)
               (evl-list x env)))

       (implies
        (and (not (equal flg 'expr))
             (not (equal flg 'expr-list))
             (consp x)
             (equal (car x) 'bv-add))
        (equal (evl (make-bv-add (simplify-bv-adds-in flg x)) env)
               (evl x env))))
  :hints (;;("Goal" :in-theory (enable evl-constraint-0)))
          ("Subgoal *1/6" :use ((:instance evl-constraint-0
                                           (x x) (a env)))))
  :rule-classes nil)

(defthm correctness-of-bv-adds-in-expr
  (equal (evl (simplify-bv-adds-in 'expr x) env)
         (evl x env))
  :hints (("Goal" :use ((:instance correctness-of-bv-adds-in
                                   (x x) (env env) (flg 'expr)))))
  :rule-classes nil)

(defthm correctness-of-bv-add-sort-cp
  (implies (and (pseudo-term-listp clause)
                (alistp env)
                (evl (conjoin-clauses (bv-add-sort-cp clause)) env))
           (evl (disjoin clause) env))
  :hints (("Goal" :induct (disjoin clause))
          ("Subgoal *1/3" :use ((:instance correctness-of-bv-adds-in-expr
                                           (x (car clause))
                                           (env env))))
          ("Subgoal *1/2" :use ((:instance correctness-of-bv-adds-in-expr
                                           (x (car clause))
                                           (env env)))))
  :rule-classes :clause-processor)

(in-theory (disable bv-commute))