-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefapply.lisp
More file actions
382 lines (325 loc) · 12.3 KB
/
defapply.lisp
File metadata and controls
382 lines (325 loc) · 12.3 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
; FGL - A Symbolic Simulation Framework for ACL2
; Copyright (C) 2008-2013 Centaur Technology
;
; Contact:
; Centaur Technology Formal Verification Group
; 7600-C N. Capital of Texas Highway, Suite 300, Austin, TX 78731, USA.
; http://www.centtech.com/
;
; License: (An MIT/X11-style license)
;
; Permission is hereby granted, free of charge, to any person obtaining a
; copy of this software and associated documentation files (the "Software"),
; to deal in the Software without restriction, including without limitation
; the rights to use, copy, modify, merge, publish, distribute, sublicense,
; and/or sell copies of the Software, and to permit persons to whom the
; Software is furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
; DEALINGS IN THE SOFTWARE.
;
; Original author: Sol Swords <sswords@centtech.com>
(in-package "FGL")
(include-book "tools/rulesets" :dir :system)
(include-book "std/util/bstar" :dir :system)
(local (in-theory (disable mv-nth)))
(defun defeval-fns-to-calls (fns world)
(declare (xargs :guard (and (symbol-listp fns)
(plist-worldp world))))
(b* (((when (atom fns))
nil)
(formals (getprop (car fns) 'formals :missing
'current-acl2-world world))
((when (eq formals :missing))
(er hard? 'defeval-fns-to-calls
"Missing formals for function: ~x0~%" (car fns))
(defeval-fns-to-calls (cdr fns) world)))
(cons (cons (car fns) formals)
(defeval-fns-to-calls (cdr fns) world))))
(defmacro defeval-wrap (ev evlst fns)
`(make-event
`(acl2::defevaluator
,',ev ,',evlst
,(defeval-fns-to-calls ',fns (w state))
:namedp t)))
(defthmd len-open-for-defapply
(equal (len (cons a b))
(+ 1 (len b))))
(defthmd nth-open-for-defapply
(implies (syntaxp (quotep n))
(equal (nth n (cons a b))
(if (zp n)
a
(nth (1- n) b)))))
(defun my-return-last (x y z)
(declare (xargs :guard t))
(mbe :logic (return-last x y z)
:exec (if (or (not (eq x 'acl2::mbe1-raw))
(equal y z))
(return-last x y z)
z)))
(program)
(defun make-list-of-nths (sym start n)
(declare (xargs :guard (and (natp start)
(natp n))))
(if (zp n)
nil
(cons `(nth ,start ,sym)
(make-list-of-nths sym (1+ start) (1- n)))))
(defmacro ecc (call)
(declare (xargs :guard (consp call)))
(cond ((eq (car call) 'return-last)
(cons 'my-return-last (cdr call)))
((member-eq (car call) acl2::*ec-call-bad-ops*)
call)
(t `(ec-call ,call))))
(defun make-mv-call (f args world)
(let* ((stobjs-out (getprop f 'stobjs-out nil 'current-acl2-world world)))
(if (and stobjs-out (< 1 (length stobjs-out)))
`(mv-list ,(length stobjs-out)
(ecc (,f . ,args)))
`(ecc (,f . ,args)))))
(defun make-apply-entry (f world)
(let* ((formals (getprop f 'formals nil 'current-acl2-world world)))
`((and (eq f ',f)
(true-listp args)
(eql (len args) ,(length formals)))
,(make-mv-call f (make-list-of-nths 'args 0 (length formals)) world))))
(defun make-apply-clique-entries (clique world)
(if (atom clique)
nil
(cons (make-apply-entry (car clique) world)
(make-apply-clique-entries (cdr clique) world))))
(defun make-apply-entries (fns world acc)
(if (atom fns)
(prog2$ (flush-hons-get-hash-table-link acc)
nil)
(if (hons-get (car fns) acc)
(make-apply-entries (cdr fns) world acc)
(let* ((clique (or (getpropc (car fns) 'recursivep nil world)
(list (car fns))))
(acc (fast-alist-fork (pairlis$ clique (make-list (len clique) :initial-element t))
acc)))
(append (make-apply-clique-entries clique world)
(make-apply-entries (cdr fns) world acc))))))
;; (defun double-rewrite-formals (formals)
;; (if (atom formals)
;; nil
;; (cons `(double-rewrite ,(car formals))
;; (double-rewrite-formals (cdr formals)))))
;; (defun apply-rw-name (apply fn)
;; (intern-in-package-of-symbol
;; (concatenate 'string (symbol-name apply) "-" (symbol-name fn))
;; apply))
;; (defun apply-rw-thms (clique name world)
;; (if (atom clique)
;; nil
;; (let* ((fn (car clique))
;; (formals (wgetprop fn 'formals)))
;; (cons `(defthm ,(apply-rw-name name fn)
;; (equal (,name ',fn (list . ,formals))
;; (,fn . ,(double-rewrite-formals formals)))
;; :hints (("goal" :in-theory
;; (e/d** (minimal-theory
;; ;; (:executable-counterpart-theory :here)
;; (equal) (len) (nth) (binary-+) (not)
;; (zp)
;; (:definition ,name)
;; len-open-for-defapply
;; nth-open-for-defapply))
;; :do-not '(preprocess))
;; (and stable-under-simplificationp
;; ;; Special case for HIDE and functions that
;; ;; normalize to a constant.
;; '(:expand ((:free ,formals (,fn . ,formals)))))))
;; (apply-rw-thms (cdr clique) name world)))))
;; (defun make-apply-rewrites (name fns world)
;; (if (atom fns)
;; nil
;; (append (b* ((recursivep (getprop (car fns) 'recursivep nil
;; 'current-acl2-world world)))
;; (apply-rw-thms (or recursivep (list (car fns))) name world))
;; (make-apply-rewrites name (cdr fns) world))))
(def-ruleset! defapply-guards '((:executable-counterpart eqlablep)
(:executable-counterpart equal)
my-return-last))
(defun mk-arity-table (lst w)
(if (atom lst)
nil
(cons (cons (car lst)
(len (getprop (car lst) 'formals nil 'current-acl2-world w)))
(mk-arity-table (cdr lst) w))))
;; Test case:
(local (DEFUN MYAPP-ARITIES
NIL (DECLARE (XARGS :GUARD T))
'((MVFN . 2)
(IF . 3)
(EQUAL . 2)
(NOT . 1)
(LEN . 1)
(CONS . 2))))
(local (DEFEVAL-WRAP MYAPP-EV MYAPP-EV-LST
(IF EQUAL NOT LEN CONS)))
(defun make-apply (name evname fns world)
(declare (xargs :mode :program))
(b* ((ev evname)
(ev-lst (intern-in-package-of-symbol
(concatenate 'string (symbol-name evname) "-LIST")
evname)))
`(progn
(defun ,(intern-in-package-of-symbol
(concatenate 'string (symbol-name name) "-ARITIES")
name)
()
(declare (xargs :guard t))
',(mk-arity-table fns world))
(encapsulate nil
(local (in-theory (e/d** ((:ruleset defapply-guards)
eq eql car-cons cdr-cons
cadr-kwote-lst-meta-correct
(nfix)
;; make-apply-open-nth
;; (zp) (unary--) (binary-+)
(:rules-of-class :type-prescription :here)))))
(defeval-wrap ,ev ,ev-lst ,fns)
(defund ,name (f args)
(declare (xargs :guard t
:normalize nil))
(mbe :logic (,ev (cons f (kwote-lst args)) nil)
:exec
(cond
,@(make-apply-entries fns world nil)
(t (,ev (cons f (ec-call (kwote-lst args))) nil)))))
(table g-apply-table ',name ',fns)))))
(defmacro defapply (name evname fns)
`(make-event (make-apply ',name ',evname ',fns (w state))))
(logic)
(defthmd make-apply-open-nth
(equal (nth n args)
(if (zp n)
(car args)
(nth (1- n) (cdr args)))))
(defund mvfn (x y) (mv x y))
(defevaluator cadkw-ev cadkw-ev-lst
((car x) (cdr x) (len x) (kwote-lst x) (< x y) (nth n x) (cons x y) (nfix x)))
(defun cadr-kwote-lst-count-cdrs (x)
(declare (xargs :guard (pseudo-termp x)))
(b* (((when (or (atom x) (not (eq (car x) 'cdr))))
(mv 0 x))
((mv count final) (cadr-kwote-lst-count-cdrs (cadr x))))
(mv (+ 1 count) final)))
(Defthm natp-cadr-kwote-lst-count-cdrs
(natp (mv-nth 0 (cadr-kwote-lst-count-cdrs x)))
:rule-classes :type-prescription)
(local (include-book "arithmetic/top-with-meta" :dir :system))
(local (defthm cdr-of-nthcdr
(equal (cdr (nthcdr n x))
(nthcdr n (cdr x)))))
(defthm cadr-kwote-lst-count-cdrs-correct
(mv-let (n y)
(cadr-kwote-lst-count-cdrs x)
(equal (nthcdr n (cadkw-ev y a))
(cadkw-ev x a)))
:hints (("goal" :induct (cadr-kwote-lst-count-cdrs x))))
(defthm car-of-nthcdr
(equal (car (nthcdr n x))
(nth n x)))
(defthm cadr-kwote-lst-count-cdrs-correct-nth
(mv-let (n y)
(cadr-kwote-lst-count-cdrs x)
(equal (nth n (cadkw-ev y a))
(car (cadkw-ev x a))))
:hints (("goal" :use ((:instance car-of-nthcdr
(n (mv-nth 0 (cadr-kwote-lst-count-cdrs x)))
(x (cadkw-ev (mv-nth 1 (cadr-kwote-lst-count-cdrs x))
a))))
:in-theory (disable car-of-nthcdr))))
(defun cadr-kwote-lst-meta-res (x)
(declare (xargs :guard (pseudo-termp x)))
(if (and (consp x) (eq (car x) 'car))
(b* (((mv n kwote-lst-term) (cadr-kwote-lst-count-cdrs (cadr x)))
((unless (and (consp kwote-lst-term)
(eq (car kwote-lst-term) 'kwote-lst)))
x))
`(cons 'quote (cons (nth ',n ,(cadr kwote-lst-term)) 'nil)))
x))
(defun cadr-kwote-lst-meta-hyp (x)
(declare (xargs :guard (pseudo-termp x)))
(if (and (consp x) (eq (car x) 'car))
(b* (((mv n kwote-lst-term) (cadr-kwote-lst-count-cdrs (cadr x)))
((unless (and (consp kwote-lst-term)
(eq (car kwote-lst-term) 'kwote-lst)))
't))
`(< (nfix ',n) (len ,(cadr kwote-lst-term))))
't))
(in-theory (disable cadr-kwote-lst-count-cdrs
car-of-nthcdr))
(defthm nth-of-kwote-lst-when-len
(implies (< (nfix n) (len x))
(equal (nth n (kwote-lst x))
(list 'quote (nth n x)))))
(defthmd cadr-kwote-lst-meta-correct
(implies (cadkw-ev (cadr-kwote-lst-meta-hyp x) a)
(equal (cadkw-ev x a)
(cadkw-ev (cadr-kwote-lst-meta-res x) a)))
:hints (("goal" :use ((:instance cadr-kwote-lst-count-cdrs-correct-nth
(x (cadr x))))
:in-theory (disable cadr-kwote-lst-count-cdrs-correct-nth)))
:rule-classes ((:meta :trigger-fns (car))))
;; (defapply myapp (BINARY-*
;; BINARY-+
;; PKG-WITNESS
;; ; UNARY-/
;; UNARY--
;; COMPLEX-RATIONALP
;; ; BAD-ATOM<=
;; ACL2-NUMBERP
;; SYMBOL-PACKAGE-NAME
;; INTERN-IN-PACKAGE-OF-SYMBOL
;; CODE-CHAR
;; ; DENOMINATOR
;; CDR
;; ; COMPLEX
;; CAR
;; CONSP
;; SYMBOL-NAME
;; CHAR-CODE
;; IMAGPART
;; SYMBOLP
;; REALPART
;; ; NUMERATOR
;; EQUAL
;; STRINGP
;; RATIONALP
;; CONS
;; INTEGERP
;; CHARACTERP
;; <
;; COERCE
;; booleanp
;; logbitp
;; binary-logand
;; binary-logior
;; lognot
;; ash
;; integer-length
;; floor
;; mod
;; truncate
;; rem
;; ; acl2::boolfix
;; ;; these are from the constant *expandable-boot-strap-non-rec-fns*.
;; NOT IMPLIES
;; EQ ATOM EQL = /= NULL ENDP ZEROP ;; SYNP
;; PLUSP MINUSP LISTP ;; RETURN-LAST causes guard violation
;; ;; FORCE CASE-SPLIT
;; ;; DOUBLE-REWRITE
;; mvfn))