File: user-thms.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 (313 lines) | stat: -rw-r--r-- 10,626 bytes parent folder | download | duplicates (4)
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
; Copyright (C) 2019, ForrestHunt, Inc.
; Written by Matt Kaufmann and J Moore
; License: A 3-clause BSD license.  See the LICENSE file distributed with ACL2.

(in-package "MODAPP")
(include-book "user-defs")

; -----------------------------------------------------------------
; Some Sample Theorems about Mapping Functions

; Theorems just showing some evaluations:

(defthm collect-example
  (equal (collect '(1 2 3) '(LAMBDA (X) (CONS 'A X)))
         '((A . 1) (A . 2) (A . 3)))
  :rule-classes nil)

(defthm collect-collect-example
  (implies (warrant collect)
           (equal (collect '((1 2 3) (4 5 6))
                           '(LAMBDA (X)
                                    (COLLECT X '(LAMBDA (X) (CONS 'A X)))))
                  '(((A . 1) (A . 2) (A . 3))
                    ((A . 4) (A . 5) (A . 6)))))
  :rule-classes nil
  :hints (("Goal" :expand ((:free (x) (hide x))))))

(defthm recur-by-collect-example
  (equal (recur-by-collect '(1 1 1)
                           '(LAMBDA (X) (BINARY-+ '1 X)))
         '(1 2 3))
  :rule-classes nil)

; A theorem showing that functions can be data, i.e., we can apply mapping functions
; to mapping functions, as long as the data functions are tame.

(defthm collect*-collect-example
  (implies (warrant square collect)
           (equal (collect* '(((1 2 3) (LAMBDA (X) (CONS 'A X)))
                              ((4 5 6 7) SQUARE)
                              (((20 21 22)
                                (30 31 32)
                                (40 41 42))
                               (LAMBDA (Y)
                                       (COLLECT Y '(LAMBDA (Z) (CONS 'C Z)))))
                              )
                            'COLLECT)
                  '(((A . 1)(A . 2)(A . 3))
                    (16 25 36 49)
                    (((C . 20) (C . 21) (C . 22))
                     ((C . 30) (C . 31) (C . 32))
                     ((C . 40) (C . 41) (C . 42)))
                    )))
  :rule-classes nil
  :hints
  (("Goal"
    :in-theory
    (disable (:executable-counterpart collect)
             (:executable-counterpart collect*)))))

; Theorems about general relationships

(defthm collect-my-append1
  (equal (collect (my-append1 a b) fn)
         (my-append1 (collect a fn)
                     (collect b fn))))

(defthm sumlist-my-append1
  (equal (sumlist (my-append1 a b) u)
         (+ (sumlist a u)
            (sumlist b u))))

(defthm all-my-append1
  (equal (all (my-append1 a b) fn)
         (and (all a fn) (all b fn))))

(defthm xists-my-append1
  (equal (xists (my-append1 a b) fn)
         (if (xists a fn)
             t
             (xists b fn))))

(defthm len-filter
  (<= (len (filter lst fn)) (len lst))
  :rule-classes :linear)

(defthm filter-v-all
  (implies (true-listp lst)
           (iff (equal (filter lst fn) lst)
                (all lst fn)))
  :hints (("Subgoal *1/3.2'" :in-theory (disable len-filter)
           :use ((:instance len-filter (lst (cdr lst)) (fn fn))))))

(defthm filter-v-xists
  (iff (filter lst fn)
       (xists lst fn))
  :rule-classes nil)

; We show some theorems about FOLDR further below.

; Theorems about concrete uses of mapping functions.

(encapsulate nil
  (local (include-book "arithmetic-5/top" :dir :system))
  (defthm sumlist-nats
    (implies (and (warrant square)
                  (natp n))
             (equal (sumlist (nats n) 'SQUARE)
                    (/ (* n (+ n 1) (+ (* 2 n) 1))
                       6)))
    :hints (("Goal" :expand ((:free (x) (HIDE x)))))))

; If lst is a true-list and every element of lst is also, then reversing each element twice
; leaves lst unchanged:

(defthm rev-rev-version-1
  (implies (and (warrant my-rev)
                (true-listp lst)
                (all lst 'true-listp))
           (equal (collect (collect lst 'my-rev) 'my-rev) lst)))

; We can actually accomplish an append and a reverse via FOLDRs.  So after we
; establish that, we repeat the above theorem but this time without referring
; to my-rev.

(defthm foldr-as-my-append1
  (equal (foldr x 'CONS y)
         (my-append1 x y)))

(defthm foldr-as-my-rev
  (implies (warrant foldr)
           (equal (foldr x
                         '(LAMBDA (X Y)
                            (FOLDR Y 'CONS (CONS X 'NIL)))
                         nil)
                  (my-rev x))))

(defthm rev-rev-version-2
  (implies (and (warrant foldr)
                (true-listp lst)
                (all lst 'true-listp))
           (equal (collect (collect lst
                                    '(lambda (x)
                                       (foldr x
                                              '(LAMBDA (X Y)
                                                       (FOLDR Y 'CONS (CONS X 'NIL)))
                                              nil)))
                           '(lambda (x)
                              (foldr x
                                     '(LAMBDA (X Y)
                                              (FOLDR Y 'CONS (CONS X 'NIL)))
                                     nil)))
                  lst)))

; And ALL and COLLECT are also FOLDRs, so we can make this an all FOLDR show:

(defthm rev-rev-version-3
  (implies (and (warrant foldr)
                (true-listp lst)
                (foldr lst
                       '(LAMBDA (X Y)
                                (IF (TRUE-LISTP X) Y 'NIL))
                       t))
           (equal (foldr
                   (foldr lst
                          '(LAMBDA (X Y)
                                   (CONS (foldr x
                                                '(LAMBDA (X Y)
                                                         (FOLDR Y 'CONS (CONS X 'NIL)))
                                                nil)
                                         Y))
                          nil)
                   '(LAMBDA (X Y)
                            (CONS (foldr x
                                         '(LAMBDA (X Y)
                                                  (FOLDR Y 'CONS (CONS X 'NIL)))
                                         nil)
                                  Y))
                   nil)
                  lst)))

; A few theorems manipulating mapping functions and the functions they
; apply.

; Here is a way to move a constant factor out of a sumlist regardless of the
; names of the variables.

(defthm sumlist-binary-*-constant
  (implies (tamep body)
           (equal (sumlist lst (lamb (list v) (list 'binary-* (list 'quote const) body)))
                  (* const (sumlist lst (lamb (list v) body))))))

(defthm lamb-x-x-is-identity
  (implies (symbolp x)
           (fn-equal (lamb (list x) x) 'identity))
  :hints (("Goal" :in-theory (enable fn-equal))))

; The hint below is only provided to show that the theorem is proved by
; rewriting, not induction.

(defthm example-of-loop$-rewriting
  (equal (sumlist (my-append1 aaa bbb) (lamb '(x) '(binary-* '2 x)))
         (+ (* 2 (sumlist aaa 'identity))
            (* 2 (sumlist bbb 'identity))))
  :hints (("Goal" :do-not-induct t))
  :rule-classes nil)

; A couple of nice foldr theorems.  The first two are redundant; they were
; critical as rewrite rules in our rev-rev versions above.  We repeat them here
; just to summarize the relations with foldr. The rest These are really just
; designed to illustrate the relationships, not to be used as rewrites.

(defthm foldr-as-my-append1
  (equal (foldr x 'CONS y)
         (my-append1 x y)))

(defthm foldr-as-my-rev
  (implies (warrant foldr)
           (equal (foldr x
                         '(LAMBDA (X Y)
                            (FOLDR Y 'CONS (CONS X 'NIL)))
                         nil)
                  (my-rev x))))

; The theorem below, which is not being stored as a rule, illustrates the most
; general fact we can think of relating COLLECT and FOLDR:

(defthm general-collect-is-a-foldr
  (implies (and (not (equal fn 'QUOTE))
                (not (equal fn 'IF))
                (symbolp x)
                (symbolp y)
                (not (eq x y))
                (tamep `(,fn ,x)))
           (equal (collect lst fn)
                  (foldr lst
                         `(lambda (,x ,y)
                            (cons (,fn ,x) ,y))
                         nil)))
  :rule-classes nil)

; But this rule is not useful as a rewrite rule because x and y are free
; variables.  Furthermore, if the goal is to rewrite COLLECTs into FOLDRs, it's
; not necessary to be so general: we could just choose the x and y we want to
; use.  But we're not interested in rewriting in either direction here so we
; make this (and subsequent theorems) :rule-classes nil.

; We use a convenient abbreviation for the hypotheses we'll see over and over
; here.

(defthm foldr-as-collect
  (implies (force (ok-fnp fn))
           (equal (foldr lst
                         `(LAMBDA (X Y)
                                  (CONS (,fn X) Y))
                         nil)
                  (collect lst fn)))
  :rule-classes nil)

(defthm foldr-as-sumlist
  (implies (ok-fnp fn)
           (equal (foldr lst
                         `(LAMBDA (X Y)
                                  (BINARY-+ (,fn X) Y))
                         0)
                  (sumlist lst fn)))
  :rule-classes nil)

(defthm foldr-as-filter
  (implies (ok-fnp fn)
           (equal (foldr lst
                         `(LAMBDA (X Y)
                            (IF (,fn X) (CONS X Y) Y))
                         nil)
                  (filter lst fn)))
  :rule-classes nil)

(defthm foldr-as-all
  (implies (ok-fnp fn)
           (equal (foldr lst
                         `(LAMBDA (X Y)
                                  (IF (,fn X) Y 'NIL))
                         t)
                  (all lst fn)))
  :rule-classes nil)

(defthm foldr-as-xists
  (implies (ok-fnp fn)
           (equal (foldr lst
                         `(LAMBDA (X Y)
                                  (IF (,fn X) 'T Y))
                         nil)
                  (xists lst fn)))
  :rule-classes nil)

(defthm maxlist-non-nil
  (implies (and (ok-fnp fn)
                (all (collect lst fn) 'ACL2-NUMBERP))
           (iff (maxlist lst fn)
                (consp lst)))
  :rule-classes nil)

(defthm foldr-as-maxlist
  (implies (and (ok-fnp fn)
                (all (collect lst fn) 'ACL2-NUMBERP))
           (equal (foldr lst `(LAMBDA (X Y)
                                (IF (EQUAL Y 'NIL)
                                    (,fn X)
                                    (MAX (,fn X) Y)))
                         nil)
                  (maxlist lst fn)))
  :rule-classes nil)