-
Notifications
You must be signed in to change notification settings - Fork 0
/
hons.lisp
314 lines (256 loc) · 9.99 KB
/
hons.lisp
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
; ACL2 Version 8.3 -- A Computational Logic for Applicative Common Lisp
; Copyright (C) 2020, Regents of the University of Texas
; This version of ACL2 is a descendent of ACL2 Version 1.9, Copyright
; (C) 1997 Computational Logic, Inc. See the documentation topic NOTE-2-0.
; This program is free software; you can redistribute it and/or modify
; it under the terms of the LICENSE file distributed with ACL2.
; This program 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
; LICENSE for more details.
; Written by: Matt Kaufmann and J Strother Moore
; email: [email protected] and [email protected]
; Department of Computer Science
; University of Texas at Austin
; Austin, TX 78712 U.S.A.
; hons.lisp -- Logical definitions for hash cons and fast alists. Note that
; the memoization and watch functionality previously provided by this file have
; been moved into "memoize.lisp". A closely related file is "hons-raw.lisp"
; that provides the Common Lisp implementation of many of the concepts
; introduced below.
; The original version of this file was contributed by Bob Boyer and Warren
; A. Hunt, Jr. The design of this system of hash cons, function memoization,
; and fast association lists (applicative hash tables) was initially
; implemented by Boyer and Hunt. The code has since been improved by Boyer and
; Hunt, and also by Sol Swords, Jared Davis, and Matt Kaufmann.
(in-package "ACL2")
(defmacro defn (f a &rest r)
`(defun ,f ,a (declare (xargs :guard t)) ,@r))
(defmacro defnd (f a &rest r)
`(defund ,f ,a (declare (xargs :guard t)) ,@r))
#+(or acl2-loop-only (not hons))
(defn hons-equal (x y)
(declare (xargs :mode :logic))
;; Has an under-the-hood implementation
(equal x y))
(defn hons-assoc-equal (key alist)
(declare (xargs :mode :logic))
(cond ((atom alist)
nil)
((and (consp (car alist))
(hons-equal key (caar alist)))
(car alist))
(t
(hons-assoc-equal key (cdr alist)))))
#+(or acl2-loop-only (not hons))
(defn hons-get (key alist)
(declare (xargs :mode :logic))
;; Has an under-the-hood implementation
(hons-assoc-equal key alist))
#+(or acl2-loop-only (not hons))
(defn hons-acons (key val alist)
(declare (xargs :mode :logic))
;; Has an under-the-hood implementation
(cons (cons key val) alist))
#+(or acl2-loop-only (not hons))
(defmacro fast-alist-free-on-exit-raw (alist form)
;; Has an under-the-hood implementation
(declare (ignore alist))
form)
#+(or acl2-loop-only (not hons))
(defn fast-alist-free (alist)
(declare (xargs :mode :logic))
;; Has an under-the-hood implementation
alist)
(defmacro fast-alist-free-on-exit (alist form)
`(return-last 'fast-alist-free-on-exit-raw ,alist ,form))
#+(or acl2-loop-only (not hons))
(defn hons-copy (x)
;; Has an under-the-hood implementation
(declare (xargs :mode :logic)) ; for attaching early to acl2x-expansion-alist
x)
#+(or acl2-loop-only (not hons))
(defn hons-copy-persistent (x)
;; Has an under-the-hood implementation
x)
#+(or acl2-loop-only (not hons))
(defn hons (x y)
;; Has an under-the-hood implementation
(cons x y))
#+(or acl2-loop-only (not hons))
(defn hons-equal-lite (x y)
;; Has an under-the-hood implementation
(equal x y))
#+(or acl2-loop-only (not hons))
(defn hons-clear (gc)
;; Has an under-the-hood implementation
(declare (ignore gc))
nil)
#+(or acl2-loop-only (not hons))
(defn hons-clear! (gc)
;; Has an under-the-hood implementation
(declare (ignore gc))
nil)
#+(or acl2-loop-only (not hons))
(defn hons-wash ()
;; Has an under-the-hood implementation
nil)
#+(or acl2-loop-only (not hons))
(defn hons-wash! ()
;; Has an under-the-hood implementation
nil)
#+(or acl2-loop-only (not hons))
(defn hons-summary ()
;; Has an under-the-hood implementation
nil)
(defmacro hons-resize (&key str-ht nil-ht cdr-ht cdr-ht-eql
addr-ht other-ht sbits
fal-ht persist-ht)
`(hons-resize-fn ,str-ht ,nil-ht ,cdr-ht ,cdr-ht-eql
,addr-ht ,other-ht ,sbits
,fal-ht ,persist-ht))
#+(or acl2-loop-only (not hons))
(defn hons-resize-fn (str-ht nil-ht cdr-ht cdr-ht-eql
addr-ht other-ht sbits
fal-ht persist-ht)
(declare (ignore str-ht nil-ht cdr-ht cdr-ht-eql
addr-ht other-ht sbits
fal-ht persist-ht))
;; Has an under-the-hood implementation
nil)
(table hons 'slow-alist-warning
; By default, ensure that the ACL2 user sees any violation of fast alist
; discipline.
#-acl2-par :break
; Except: In ACL2(p), when waterfall-parallelism is enabled, hons-get will fail
; the fast alist discipline when it is called from any any thread other than
; the top-level thread. We avoid breaking in that case.
#+acl2-par :warning)
(defmacro set-slow-alist-action (action)
(declare (xargs :guard (or (eq action :warning)
(eq action :break)
(not action))))
`(table hons 'slow-alist-warning ,action))
(defn get-slow-alist-action (state)
(declare (xargs :stobjs state))
(let* ((alist (table-alist 'hons (w state)))
(warning (hons-assoc-equal 'slow-alist-warning alist)))
(and (consp warning)
(cdr warning))))
#+(or acl2-loop-only (not hons))
(defn hons-acons! (key val alist)
;; Has an under-the-hood implementation
(cons (cons key val) alist))
#+(or acl2-loop-only (not hons))
(defn make-fast-alist (alist)
;; Has an under-the-hood implementation
alist)
#+(or acl2-loop-only (not hons))
(defn fast-alist-fork (alist ans)
;; Has an under-the-hood implementation
(cond ((atom alist)
ans)
((atom (car alist))
(fast-alist-fork (cdr alist) ans))
((hons-assoc-equal (car (car alist)) ans)
(fast-alist-fork (cdr alist) ans))
(t
(fast-alist-fork (cdr alist) (cons (car alist) ans)))))
#+(or acl2-loop-only (not hons))
(defn fast-alist-fork! (alist ans)
;; Has an under-the-hood implementation
(fast-alist-fork alist ans))
; Deprecated:
(defmacro hons-shrink-alist (alist ans)
`(fast-alist-fork ,alist ,ans))
(defmacro hons-shrink-alist! (alist ans)
`(fast-alist-fork! ,alist ,ans))
(add-macro-alias hons-shrink-alist fast-alist-fork)
(add-macro-alias hons-shrink-alist! fast-alist-fork!)
#+(or acl2-loop-only (not hons))
(defn fast-alist-clean (alist)
;; Has an under-the-hood implementation
(fast-alist-fork alist
(if (consp alist)
(cdr (last alist))
alist)))
#+(or acl2-loop-only (not hons))
(defn fast-alist-clean! (alist)
;; Has an under-the-hood implementation
(fast-alist-clean alist))
#+(or acl2-loop-only (not hons))
(defn fast-alist-len (alist)
;; Has an under-the-hood implementation
(len (fast-alist-fork alist nil)))
#+(or acl2-loop-only (not hons))
(defn fast-alist-summary ()
;; Has an under-the-hood implementation
nil)
#+(or acl2-loop-only (not hons))
(defmacro with-fast-alist-raw (alist form)
;; Has an under-the-hood implementation
(declare (ignore alist))
form)
(defmacro with-fast-alist (alist form)
`(return-last 'with-fast-alist-raw ,alist ,form))
#+(or acl2-loop-only (not hons))
(defmacro with-stolen-alist-raw (alist form)
;; Has an under-the-hood implementation
(declare (ignore alist))
form)
(defmacro with-stolen-alist (alist form)
`(return-last 'with-stolen-alist-raw ,alist ,form))
(defn cons-subtrees (x al)
(cond ((atom x)
al)
((hons-get x al)
al)
(t
(cons-subtrees
(car x)
(cons-subtrees (cdr x) (hons-acons x t al))))))
#+(or acl2-loop-only (not hons))
(defn number-subtrees (x)
;; Has an under-the-hood implementation
(len (cons-subtrees x 'number-subtrees)))
(defn flush-hons-get-hash-table-link (alist)
(fast-alist-free alist))
(in-theory (disable
; The execution of honsing and fast alist functions during theorem proving
; could be very subtle. It is easy to imagine discipline failures, inadvertent
; norming, inadvertent clearing of hash tables, etc. We try to prevent this at
; least somewhat by disabling the executable-counterparts of many of the above
; functions. This is not a total solution, but seems like a good idea anyway.
;; These would probably be pretty harmless
(:executable-counterpart hons)
(:executable-counterpart hons-copy)
(:executable-counterpart hons-copy-persistent)
(:executable-counterpart hons-summary)
(:executable-counterpart fast-alist-summary)
;; These could be particularly bad to call by mistake
(:executable-counterpart hons-clear)
(:executable-counterpart hons-clear!)
(:executable-counterpart hons-wash)
(:executable-counterpart hons-wash!)
(:executable-counterpart hons-resize-fn)
;; These could lead to discipline failures
(:executable-counterpart hons-get)
(:executable-counterpart hons-acons)
(:executable-counterpart hons-acons!)
(:executable-counterpart fast-alist-fork)
(:executable-counterpart fast-alist-fork!)
(:executable-counterpart fast-alist-clean)
(:executable-counterpart fast-alist-clean!)
(:executable-counterpart fast-alist-len)
(:executable-counterpart fast-alist-free)
(:executable-counterpart flush-hons-get-hash-table-link)
))
(defun remove-keyword (word l)
(declare (xargs :guard (and (keywordp word)
(keyword-value-listp l))))
(cond ((endp l) nil)
((eq word (car l))
(remove-keyword word (cddr l)))
(t (list* (car l) (cadr l) (remove-keyword word (cddr l))))))
; For some additional helper functions and lemmas, see the community books
; books/misc/hons-help.lisp and books/misc/hons-help2.lisp.