-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbvar-db-equivs.lisp
More file actions
307 lines (264 loc) · 12.9 KB
/
bvar-db-equivs.lisp
File metadata and controls
307 lines (264 loc) · 12.9 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
; 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 "bvar-db-bfrlist")
(include-book "pathcond")
(include-book "contexts")
(defsection bvar-db-boundedp
(defun-sk bvar-db-boundedp (bvar-db logicman)
(forall var
(implies (and (natp var)
(<= (base-bvar$a bvar-db) var)
(< var (next-bvar$a bvar-db)))
(bfrlist-boundedp (fgl-object-bfrlist (get-bvar->term$a var bvar-db))
var logicman)))
:rewrite :direct)
(defthm bvar-db-boundedp-of-empty
(bvar-db-boundedp (init-bvar-db$a base bvar-db) logicman))
(in-theory (disable bvar-db-boundedp
bvar-db-boundedp-necc))
(defthm bvar-db-boundedp-of-add
(implies (and (equal (next-bvar$a bvar-db) (bfr-nvars logicman))
(bvar-db-boundedp bvar-db logicman))
(bvar-db-boundedp (add-term-bvar$a x bvar-db) logicman))
:hints (("goal" :expand ((bvar-db-boundedp (add-term-bvar$a x bvar-db) logicman))
:use ((:instance bvar-db-boundedp-necc
(var (bvar-db-boundedp-witness (add-term-bvar$a x bvar-db) logicman)))))))
(defthm bvar-db-boundedp-of-update-term-equivs
(implies (bvar-db-boundedp bvar-db logicman)
(bvar-db-boundedp (update-term-equivs$a equivs bvar-db) logicman))
:hints (("goal" :expand ((bvar-db-boundedp (update-term-equivs$a equivs bvar-db) logicman))
:use ((:instance bvar-db-boundedp-necc
(var (bvar-db-boundedp-witness (update-term-equivs$a equivs bvar-db) logicman)))))))
(defthm bvar-db-boundedp-of-logicman-extension
(implies (and (bind-logicman-extension new old)
(bvar-db-boundedp bvar-db old)
(lbfr-listp (bvar-db-bfrlist bvar-db) old))
(bvar-db-boundedp bvar-db new))
:hints (("goal" :expand ((bvar-db-boundedp bvar-db new))
:use ((:instance bvar-db-boundedp-necc
(var (bvar-db-boundedp-witness bvar-db new))
(logicman old)))))))
;; (local (include-book "std/lists/take" :dir :system))
;; (local (in-theory (disable acl2::take-redefinition acl2::take-of-1)))
;; (local (defthm take-of-symbol-list
;; (implies (symbol-listp x)
;; (symbol-listp (take n x)))
;; :hints(("Goal" :in-theory (enable acl2::take-redefinition)))))
;; (define context-p (x)
;; (or (pseudo-fnsym-p x)
;; (and (pseudo-lambda-p x)
;; (eql (len (acl2::pseudo-lambda->formals x)) 1)))
;; ///
;; (define context-fix ((x context-p))
;; :Returns (new-x context-p)
;; (mbe :logic (if (pseudo-fnsym-p x)
;; x
;; (b* (((pseudo-lambda x) (pseudo-lambda-fix x)))
;; (pseudo-lambda (take 1 x.formals) x.body)))
;; :exec x)
;; ///
;; (defthm context-fix-when-context-p
;; (implies (context-p x)
;; (equal (context-fix x) x)))
;; (fty::deffixtype context :pred context-p :fix context-fix :equiv context-equiv
;; :define t)))
(define check-equiv-replacement ((x fgl-object-p)
(equiv-term fgl-object-p)
(contexts equiv-contextsp)
state)
:returns (mv ok
(equiv fgl-object-p)
negp
iff-equiv-p)
(declare (ignorable state))
;; BOZO fix these to work with context fixing terms, refinements, negated equivs, etc
(b* (((when (hons-equal (fgl-object-fix x)
(fgl-object-fix equiv-term)))
(mv t nil t (member-eq 'iff (equiv-contexts-fix contexts))))
((unless (fgl-object-case equiv-term :g-apply))
(mv nil nil nil nil))
(equiv (g-apply->fn equiv-term))
((unless (or (eq equiv 'equal)
(member-eq equiv (equiv-contexts-fix contexts))))
(mv nil nil nil nil))
(args (g-apply->args equiv-term))
((unless (equal (len args) 2))
(mv nil nil nil nil))
((when (hons-equal (car args) (fgl-object-fix x)))
(mv t (cadr args) nil nil))
((when (hons-equal (cadr args) (fgl-object-fix x)))
(mv t (car args) nil nil)))
(mv nil nil nil nil))
///
(defret fgl-object-bfrlist-of-<fn>
(implies (not (member v (fgl-object-bfrlist equiv-term)))
(not (member v (fgl-object-bfrlist equiv))))))
(define try-equivalences ((x fgl-object-p)
(bvars nat-listp)
(contexts equiv-contextsp)
pathcond
bvar-db
logicman
state)
:guard (and (bvar-list-okp bvars bvar-db)
(equal (next-bvar bvar-db) (bfr-nvars)))
:returns (mv ok
(new-x fgl-object-p)
(new-pathcond (equal new-pathcond (pathcond-fix pathcond))))
:guard-hints ((and stable-under-simplificationp
'(:expand ((bvar-list-okp$a bvars bvar-db))
:in-theory (enable bfr-varname-p))))
(b* ((pathcond (pathcond-fix pathcond))
((when (atom bvars)) (mv nil nil pathcond))
(bvar (lnfix (car bvars)))
(bfr-var (bfr-var bvar logicman))
(equiv-term (get-bvar->term bvar bvar-db))
((mv check-ok repl negp iff-equiv-p)
(check-equiv-replacement x equiv-term contexts state))
((unless check-ok)
(try-equivalences x (cdr bvars) contexts pathcond bvar-db logicman state))
((mv ans pathcond) (logicman-pathcond-implies bfr-var pathcond logicman))
((when (if negp (eql ans 0) (eql ans 1)))
(mv t repl pathcond))
((when (and iff-equiv-p ans))
(mv t (eql ans 1) pathcond)))
(try-equivalences x (cdr bvars) contexts pathcond bvar-db logicman state))
///
(local (defthm fgl-object-bfrlist-of-boolean
(implies (booleanp x)
(equal (fgl-object-bfrlist x) nil))
:hints(("Goal" :in-theory (enable booleanp)))))
(defret fgl-object-bfrlist-of-<fn>
(implies (and (not (member v (bvar-db-bfrlist bvar-db)))
(bvar-list-okp$a bvars bvar-db))
(not (member v (fgl-object-bfrlist new-x))))))
(define try-equivalences-loop ((x fgl-object-p)
(contexts equiv-contextsp)
(clk natp)
pathcond
bvar-db
logicman
state)
:guard (equal (next-bvar bvar-db) (bfr-nvars))
:measure (nfix clk)
:returns (mv error
(replacement fgl-object-p)
(new-pathcond (equal new-pathcond (pathcond-fix pathcond))))
(b* ((pathcond (pathcond-fix pathcond))
((when (zp clk)) (mv "try-equivalences ran out of clock -- equiv loop?"
(fgl-object-fix x)
pathcond))
(equivs (get-term->equivs x bvar-db))
((mv ok repl pathcond)
(try-equivalences x equivs contexts pathcond bvar-db logicman state))
((when ok)
(try-equivalences-loop repl contexts (1- clk) pathcond bvar-db logicman state)))
(mv nil (fgl-object-fix x) pathcond))
///
(defret fgl-object-bfrlist-of-<fn>
(implies (and (not (member v (bvar-db-bfrlist bvar-db)))
(not (member v (fgl-object-bfrlist x))))
(not (member v (fgl-object-bfrlist replacement))))))
(define maybe-add-equiv-term ((test-obj fgl-object-p)
(bvar natp)
bvar-db
state)
:guard (and (<= (base-bvar bvar-db) bvar)
(< bvar (next-bvar bvar-db)))
:returns (new-bvar-db)
;; We (maybe) add an association between some term and the generated Boolean
;; variable, saying that if the Boolean variable is assumed true or false,
;; that may imply some value of the test-obj.
;; In some cases we associate test-obj itself to bvar. In this case the
;; association means if bvar is NIL, then normalize test-obj is NIL.
;; Otherwise test-obj is (equal x y) and we associate either x or y to bvar;
;; in this case, if bvar is true, normalize (respectively) x to y or y to x.
(declare (ignorable state))
(fgl-object-case test-obj
:g-var (add-term-equiv test-obj bvar bvar-db)
:g-apply (b* ((fn test-obj.fn)
(args test-obj.args)
((unless (and (eq fn 'equal)
(equal (len args) 2)))
(add-term-equiv test-obj bvar bvar-db))
((list a b) args)
;; The rest is just a heuristic determination of which should rewrite to
;; the other. Note: in many cases we don't rewrite either way!
;; Heuristic 1: when a variable is equated with anything else, normalize var -> other.
;; (Note this could loop, up to the user to prevent this)
(a-varp (fgl-object-case a :g-var))
(b-varp (fgl-object-case b :g-var))
((when a-varp)
(if b-varp
(add-term-equiv test-obj bvar bvar-db)
(add-term-equiv a bvar bvar-db)))
((when b-varp)
(add-term-equiv b bvar bvar-db))
;; Heuristic 2: when one object has no free variables,
;; normalize other -> good obj.
(a-goodp (fgl-object-variable-free-p a))
((when a-goodp)
(add-term-equiv b bvar bvar-db))
(b-goodp (fgl-object-variable-free-p b))
((when b-goodp)
(add-term-equiv a bvar bvar-db)))
;; Neither heuristic applied -- don't normalize either way.
(add-term-equiv test-obj bvar bvar-db))
:otherwise (add-term-equiv test-obj bvar bvar-db))
///
(defthm bvar-db-bfrlist-of-maybe-add-equiv-term
(equal (bvar-db-bfrlist (maybe-add-equiv-term obj bvar bvar-db state))
(bvar-db-bfrlist bvar-db))
:hints(("Goal" :in-theory (enable maybe-add-equiv-term
bvar-db-bfrlist
add-term-equiv))))
(defthm bvar-db-boundedp-of-maybe-add-equiv-term
(implies (bvar-db-boundedp bvar-db logicman)
(bvar-db-boundedp (maybe-add-equiv-term obj bvar bvar-db state) logicman))
:hints (("goal" :expand ((bvar-db-boundedp (maybe-add-equiv-term obj bvar bvar-db state) logicman))
:use ((:instance bvar-db-boundedp-necc
(var (bvar-db-boundedp-witness
(maybe-add-equiv-term obj bvar bvar-db state) logicman))))
:in-theory (enable add-term-equiv))))
(defthm next-bvar$a-of-maybe-add-equiv-term
(equal (next-bvar$a (maybe-add-equiv-term x bvar bvar-db state))
(next-bvar$a bvar-db))
:hints(("Goal" :in-theory (enable maybe-add-equiv-term))))
(defthm base-bvar$a-of-maybe-add-equiv-term
(equal (base-bvar$a (maybe-add-equiv-term x bvar bvar-db state))
(base-bvar$a bvar-db))
:hints(("Goal" :in-theory (enable maybe-add-equiv-term))))
(defthm get-bvar->term$a-of-maybe-add-equiv-term
(equal (get-bvar->term$a n (maybe-add-equiv-term x bvar bvar-db state))
(get-bvar->term$a n bvar-db)))
(defthm get-term->bvar$a-of-maybe-add-equiv-term
(equal (get-term->bvar$a obj (maybe-add-equiv-term x bvar bvar-db state))
(get-term->bvar$a obj bvar-db))))