File: run-fpst.lisp

package info (click to toggle)
acl2 8.5dfsg-5
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 991,452 kB
  • sloc: lisp: 15,567,759; javascript: 22,820; cpp: 13,929; ansic: 12,092; perl: 7,150; java: 4,405; xml: 3,884; makefile: 3,507; sh: 3,187; ruby: 2,633; ml: 763; python: 746; yacc: 723; awk: 295; csh: 186; php: 171; lex: 154; tcl: 49; asm: 23; haskell: 17
file content (425 lines) | stat: -rw-r--r-- 10,739 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
(in-package "ACL2")

#|

We introduce functions for loading the datastructure of the
stobj-based pathfinding program and prove that the loading and
calculating of this optimized program works just like the original
implementation.

See also "fpst.lisp".

Matt Wilding and David Greve
Updated February, 2003

|#

;; Assumes the "find path - stobj" program is loaded
(include-book "fpst")

(set-verify-guards-eagerness 2)

;; We want to prove the guards of the functions we need from J's
;; proof.  However, some of the functions are not guard-provable
;; because the guards weren't in place.  In this case, we add a
;; function with the same body and the needed guards, and use that
;; instead.

(defun myall-nodes (g)
  (declare (xargs :guard (alistp g)))
  (cond ((endp g) nil)
	(t (cons (car (car g))
		 (myall-nodes (cdr g))))))

(defthm myall-nodes-is-all-nodes
  (equal
   (myall-nodes g)
   (all-nodes g)))

; mygraph1p is just like J's graph1p, except that the node and the
; children are in-range naturals.
(defun mygraph1p (g nodes)
  (declare (xargs :guard (and (true-listp nodes) (alistp g))))
  (cond ((endp g) t)
	(t (and (consp (car g))
		(true-listp (cdr (car g)))
		(numberlistp (car g) (maxnode))       ; needed for stobj version
		(subsetp (cdr (car g)) nodes)
		(no-duplicatesp (cdr (car g)))
		(mygraph1p (cdr g) nodes)))))

(defthm mygraph1p-is-graph1p
  (implies
   (mygraph1p g nodes)
   (graph1p g nodes)))

(defun mygraphp (g)
  (declare (xargs :guard (alistp g)))
  (and (alistp g)
       (eqlable-listp (myall-nodes g))
       (no-duplicatesp (myall-nodes g))
       (mygraph1p g (myall-nodes g))))

(defthm mygraphp-is-graphp
  (implies
   (mygraphp g)
   (graphp g)))

(defun myneighbors (node g)
  (declare (xargs :guard (alistp g)))
  (cond ((endp g) nil)
	((equal node (car (car g)))
	 (cdr (car g)))
	(t (myneighbors node (cdr g)))))

(defthm myneighbors-is-neighbors
  (equal
   (myneighbors n g)
   (neighbors n g)))

(defthm consp-neighbors
  (implies
   (mygraph1p g l)
   (iff
    (consp (neighbors i g))
    (neighbors i g))))

(defthm gp-update-nth
  (implies
   (and
    (gp g)
    (listp v)
    (bounded-natp i max))
   (gp (update-nth i v g)))
  :hints (("goal" :in-theory (enable update-nth))))


;; Now, some functions that allow us to load the stobj with
;; values in the datastructures used by J's implementation.

(defun load-graph1 (g i st)
  (declare (xargs :stobjs st
		  :guard (and (stp st) (alistp g)
			      (bounded-natp i (1+ (maxnode)))
			      (mygraphp g))
		  :measure (max 0 (nfix (- (maxnode) i)))))
  (if (or (not (integerp i)) (not (< i (maxnode))))
      st
      (let
	  ((st (update-gi i (myneighbors i g) st)))
	(load-graph1 g (1+ i) st))))

(defun load-graph (g st)
  (declare (xargs :stobjs st
		  :guard (and (stp st) (alistp g) (mygraphp g))))
  (load-graph1 g 0 st))

(defun init-marks1 (i st)
  (declare (xargs :stobjs st
		  :guard (and (stp st) (bounded-natp i (1+ (maxnode))))
		  :measure (max 0 (nfix (- (maxnode) i)))))
  (if (or (not (integerp i)) (not (< i (maxnode))))
      st
      (let
	  ((st (update-marksi i 0 st)))
	(init-marks1 (1+ i) st))))

;; Some rules about our loading functions
(defthm stp-load-graph1
  (implies
   (and
    (stp st)
    (mygraphp g)
    (integerp i)
    (<= 0 i))
   (stp (load-graph1 g i st))))

(defthm stp-init-marks1
  (implies
   (and
    (stp st)
    (integerp i)
    (<= 0 i))
   (stp (init-marks1 i st))))

(defthm nth-init-marks1-other
  (implies
   (not (equal i (marksindex)))
   (equal (nth i (init-marks1 j st))
	  (nth i st))))

(defun init-marks (st)
  (declare (xargs :stobjs st :guard (stp st)))
  (init-marks1 0 st))

(defun load-st (g st)
  (declare (xargs :stobjs st
		  :guard (and (stp st) (alistp g) (mygraphp g))))
  (let ((st (load-graph g st)))
    (let ((st (init-marks st)))
      (let ((st (update-status 1 st)))
	(let ((st (update-stack nil st)))
	  st)))))

(defthm graph-equivp-only-on-g
  (implies
   (equal (nth (gindex) st1) (nth (gindex) st2))
   (equal
    (graph-equivp1 g st1 i)
    (graph-equivp1 g st2 i)))
  :rule-classes nil)

(defthm graph-equivp1-init-marks
  (equal
   (graph-equivp1 g (init-marks1 i st) i)
   (graph-equivp1 g st i))
  :hints (("goal" :use (:instance graph-equivp-only-on-g
				  (st1 (init-marks1 i st)) (st2 st)))))
(defthm graphp1-equal-graphs
  (implies
   (and
    (equal (nth (gindex) st1) (nth (gindex) st2))
    (stp st1)
    (stp st2))
   (iff
    (graphp1-st st1 i)
    (graphp1-st st2 i)))
  :hints (("goal" :in-theory (enable graphp1-st)))
  :rule-classes nil)

(defthm graphp1-st-lesser
  (implies
   (and
    (graphp1-st st i)
    (bounded-natp i j))
   (graphp1-st st j))
  :hints (("goal" :in-theory (enable graphp1-st))))

(defthm graphp1-st-init-marks
  (implies
   (and
    (stp st)
    (integerp i)
    (<= 0 i))
   (equal
    (graphp1-st (init-marks1 i st) i)
    (graphp1-st st i)))
   :hints (("goal" :use (:instance graphp1-equal-graphs
				   (st1 (init-marks1 i st)) (st2 st))
	    :in-theory (disable stp))))

(defthm nth-0-load-graph1-above
  (implies
   (and
    (bounded-natp i j)
    (integerp j))
  (equal
   (nth i (nth (gindex) (load-graph1 g j st)))
   (nth i (nth (gindex) st)))))

(defthm graph-equivp1-load-graph1
  (implies
   (bounded-natp i (maxnode))
   (graph-equivp1 g (load-graph1 g i st) i))
  :hints (("Subgoal *1/3'" :expand (:free (x) (LOAD-GRAPH1 G x ST)))
	  ("Subgoal *1/3''" :expand (:free (x n) (GRAPH-EQUIVP1 G x n)))))

(defthm nth-1-init-marks-above
  (implies
   (and
    (bounded-natp i j)
    (integerp j))
  (equal
   (nth i (nth (marksindex) (init-marks1 j st)))
   (nth i (nth (marksindex) st)))))

(defthm mark-equivp1-init-marks1
  (implies
   (and
    (integerp i)
    (<= 0 i))
   (mark-equivp1 nil (init-marks1 i st) i)))

(defthm equiv-load-st
  (equiv nil g nil (load-st g st))
  :hints (("goal" :in-theory (set-difference-theories
			      (enable graph-equivp mark-equivp)
			      '(graph-equivp1 mark-equivp1 init-marks1)))))

(defthm numberlistp-neighbors
  (implies
   (mygraph1p g n)
   (numberlistp (neighbors i g) (maxnode))))

(defthm graph1p-st-load-graph1
  (implies
   (and
    (mygraphp g)
    (integerp i)
    (<= 0 i))
  (graphp1-st (load-graph1 g i st) i))
  :hints (("goal" :in-theory (enable load-graph1 graphp1-st))))

(defun linear-find-st (a b g st)
  (declare (xargs :stobjs st
		  :guard (and (stp st)
			      (bounded-natp a (maxnode))
			      (bounded-natp b (maxnode))
			      (alistp g)
			      (mygraphp g))))
  (let ((st (load-st g st)))
    (let ((st (linear-find-next-step-st-mbe (list a) b st)))
      (if (not (equal (status st) 0))
	  (mv 'failure st)
	(mv (stack st) st)))))

(defthm nth-init-marks1
  (implies
   (and
    (integerp j)
    (integerp i)
    (<= 0 i)
    (<= j i)
    (< i (maxnode)))
  (equal (nth i (nth (marksindex) (init-marks1 j st)))
	 0))
  :hints (("goal" :expand ((INIT-MARKS1 I ST)))))

(defthm linear-find-next-step-st-mbe-base
  (implies
   (and
    (equal (nth i (nth (marksindex) st)) 0)
    (bounded-natp i (maxnode))
    (equal (stack st) nil))
   (equal (nth (stackindex) (linear-find-next-step-st-mbe (list i) i st)) (list i)))
  :hints (("goal" :expand (linear-find-next-step-st-mbe (list i) i st))))

(defthm nth-load-graph1
  (implies
   (and
    (integerp i)
    (<= 0 i)
    (not (equal i (gindex))))
   (equal (nth i (load-graph1 g j st))
	  (nth i st))))

;; ****************
;; Main lemma
;; ****************
;; Our implementation returns the same value as the original
;; list-based one when we load the stobj using the functions
;; of this file.
(defthm linear-find-st-linear-find-path
  (implies
   (and
    (bounded-natp a (maxnode))
    (bounded-natp b (maxnode))
    (mygraphp g)
    (stp st))
  (equal
   (car (linear-find-st a b g st))
   (linear-find-path a b g)))
  :hints (("goal" :use ((:instance implementations-same (stack nil) (mt nil) (c (list a))
				   (st (load-st g st)))
			equiv-load-st)
	   :in-theory (disable linear-find-path-is-find-path equiv-load-st stp))))

#|
ACL2 !>(assign g '((0 1 2) (1) (2 0 1 2 3) (3 1)))
 ((0 1 2) (1) (2 0 1 2 3) (3 1))
ACL2 !>(mygraphp (@ g))
T
ACL2 !>(stp st)
T
ACL2 !>(linear-find-st 0 3 (@ g) st)
((0 2 3) <st>)
ACL2 !>(linear-find-path 0 3 (@ g))
(0 2 3)

|#

;; Some functions for building graphs

;; Generate a graph with nodes numbered curr though last and edges
;; from each node to the list all
(defun completeg-helper (curr last all)
  (declare (xargs :verify-guards t))
  (declare (xargs :measure (nfix (- (1+ (nfix last)) (nfix curr)))))
  (if (<= (nfix curr) (nfix last))
      (cons
       (cons curr all)
       (completeg-helper (1+ (nfix curr)) last all))
    nil))

;; Generate a list of naturals from curr to last
(defun listofnats (curr last)
  (declare (xargs :verify-guards t
		  :measure (nfix (- (1+ (nfix last)) (nfix curr)))))
  (if (<= (nfix curr) (nfix last))
      (cons
       curr
       (listofnats (1+ (nfix curr)) last))
    nil))

;; Generate a complete graph with nodes 0 to size-1
(defun completeg (size)
  (declare (xargs :verify-guards t))
  (completeg-helper 0 (1- (nfix size)) (listofnats 0 (1- (nfix size)))))

;; Generate a "bad" graph with n nodes.  Nodes 0..n-1 are a complete
;; graph, and node n is disconnected
(defun badg (size)
  (declare (xargs :verify-guards t))
  (cons
   (list size)
   (completeg size)))

#|

The pathfinder can be run from the ACL2 read-eval-print loop.

First, load this book

   ACL2 !>(include-book
           "run-fpst")
   Loading /accts/dagreve/local/src/acl2-2.8a/books/arithmetic/equalities.o
   start address -T 1827ecc Finished loading /accts/dagreve/local/src/acl2-2.8a/books/arithmetic/equalities.o
   Loading /accts/dagreve/local/src/acl2-2.8a/books/arithmetic/rational-listp.o

   ...

   Summary
   Form:  ( INCLUDE-BOOK ; manual editing by Matt K. to avoid Makefile-deps dependency
            "run-fpst" ...)
   Rules: NIL
   Warnings:  None
   Time:  1.49 seconds (prove: 0.00, print: 0.00, other: 1.49)
    "/accts/dagreve/ACL/challenges/graph/run-fpst.lisp"

Next, load the datastructure with a graph.  In this example, we load a
graph with 1,000 nodes and 1,000,000 edges, which takes about a minute.

   ACL2 !>(load-st (badg 1000) st)
   <st>

We try to find a non-existent path so that the program traverses all
the edges.  This requires less than a second.

   ACL2 !>(linear-find-next-step-st-mbe (list 0) 1000 st)
   <st>
   ACL2 !>(status st)
   1

We reset the marks array and search for an existing path.  Note that
it's not guaranteed to be the shortest one, only a valid one.

   ACL2 !>(init-marks st)
   <st>
   ACL2 !>(linear-find-next-step-st-mbe (list 0) 6 st)
   <st>
   ACL2 !>(status st)
   0
   ACL2 !>(stack st)
   (0 1 2 3 4 5 6)

|#