File: dsp-defuns.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,138,276 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,978; makefile: 3,840; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (397 lines) | stat: -rw-r--r-- 13,947 bytes parent folder | download | duplicates (5)
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
; Copyright (C) 2013, Regents of the University of Texas
; Written by: J Strother Moore and Qiang Zhang
;             Department of Computer Sciences
;             Univesity of Texas at Austin
;             October, 2004
; License: A 3-clause BSD license.  See the LICENSE file distributed with ACL2.

; (certify-book "script")

; A Proof of the Correctness of Dijkstra's Shortest Path Algorithm in
; ACL2

; See the paper ``Dijkstra's Shortest Path Algorithm Verified with ACL2''
; by the authors for a description of this script.

; Historical Notes: The first version of this proof was completed by
; Moore in September, 2003.  See /u/moore/shortest-path/dsp6.lisp.
; That file contained 92 defthms, 35 :hints, about 75 clause
; identifiers like "Goal" and "Subgoal" (some occur in comments), and
; 40 :use hints.  In order to learn ACL2, Zhang was given the task by
; Moore, first, to discover (independently) a proof (given the
; invariant), and, then, to clean it up.

; Zhang finished his first proof in December, 2003, using ACL2 Version
; 2.7.  In October, 2004, he cleaned up the proof, eliminating some of
; his hints.  This file is his second proof script.  In February,
; 2005, the authors wrote the paper above and in doing so renamed some
; of Zhang's functions and theorems.

; The current file contains 39 defuns, 126 defthms, 51 hints, 23 of
; which are :use hints mentioning 31 lemma instances.

(in-package "ACL2")

;All definitions
(defun del (x y)
  (if (endp y) nil
    (if (equal x (car y))
        (cdr y)
      (cons (car y) (del x (cdr y))))))

(defun memp (e x)
  (if (endp x) nil
    (or (equal e (car x))
        (memp e (cdr x)))))

(defun setp (s)
  (cond ((endp s) t)
        ((memp (car s) (cdr s)) nil)
        (t (setp (cdr s)))))

(defun my-union (s1 s2)
  (cond ((endp s1) s2)
        ((memp (car s1) s2)
         (my-union (cdr s1) s2))
        (t (cons (car s1)
                 (my-union (cdr s1) s2)))))

(defun my-subsetp (s1 s2)
  (cond ((endp s1) t)
        (t (and (memp (car s1) s2)
                (my-subsetp (cdr s1) s2)))))

(defun infinitep (x) (null x)) 

(defun lt (x y)
  (cond ((infinitep x) nil)
        ((infinitep y) t)
        (t (< x y))))

(defun plus (x y)
  (cond ((or (infinitep x)
             (infinitep y))
         nil)
        (t (+ x y))))

(defun edge-listp (lst)
  (cond ((endp lst) (equal lst nil))
        ((and (consp (car lst))
              (rationalp (cdar lst))
              (<= 0 (cdar lst))
              (not (assoc-equal (caar lst)
                          (cdr lst))))
         (edge-listp (cdr lst)))
        (t nil)))

(defun graphp (g)
  (cond ((endp g) (equal g nil))
        ((and (consp (car g))
              (edge-listp (cdar g)))
         (graphp (cdr g)))
        (t nil)))

(defun cons-set (e s)
  (if (memp e s) s
    (cons e s)))

(defun all-nodes (g)
  (cond ((endp g) nil)
        (t (cons-set (caar g)
                     (my-union (strip-cars (cdar g))
                               (all-nodes (cdr g)))))))

(defun nodep (n g) (memp n (all-nodes g)))

(defun neighbors (n g)
  (strip-cars (cdr (assoc-equal n g))))

(defun pathp-aux (path g)
  (cond ((endp path) nil)
        ((endp (cdr path))
         (nodep (car path) g))
        (t (and (memp (cadr path)
                     (neighbors (car path) g))
                (pathp-aux (cdr path) g)))))

(defun pathp (path g)
  (and (true-listp path)
       (pathp-aux path g)))

(defun edge-len (a b g)
  (cdr (assoc-equal b (cdr (assoc-equal a g)))))

(defun path-len (path g)
  (cond ((endp path) nil)
        ((endp (cdr path))
         (if (nodep (car path) g) 0 nil))
        (t (plus (edge-len (car path) (cadr path) g)
                 (path-len (cdr path) g)))))

(defun path (n pt) (cdr (assoc-equal n pt)))

(defun d (n pt g)
  (path-len (path n pt) g))

#|
(defun choose-next (ts pt g)
  (cond ((endp ts) '(non-node))
        ((endp (cdr ts)) (car ts))
        ((lt (d (car ts) pt g)
             (d (choose-next (cdr ts) pt g) pt g))
         (car ts))
        (t (choose-next (cdr ts) pt g))))
|#

(defun choose-next (ts pt g)
  (cond ((endp ts) '(non-node))
        ((endp (cdr ts)) (car ts))
        (t (let ((u (choose-next (cdr ts) pt g)))
             (if (lt (d (car ts) pt g) (d u pt g))
                 (car ts)
               u)))))

(defun reassign (u v-lst pt g)
  (cond ((endp v-lst) pt)
        (t (let* ((v (car v-lst))
                  (w (edge-len u v g)))
             (cond ((lt (plus (d u pt g) w)
                        (d v pt g))
                    (cons (cons v (append (path u pt) (list v)))
                          (reassign u (cdr v-lst) pt g)))
                   (t (reassign u (cdr v-lst) pt g)))))))

(defun dsp (ts pt g)
  (cond ((endp ts) pt)
        (t (let ((u (choose-next ts pt g)))
             (dsp (del u ts)
                  (reassign u (neighbors u g) pt g)
                  g)))))

(defun dijkstra-shortest-path (a b g)
  (let ((p (dsp (all-nodes g)
                (list (cons a (list a))) g)))
    (path b p)))

;===================================================================
(defun pathp-from-to (p a b g)
  (and (pathp p g)
       (equal (car p) a)
       (equal (car (last p)) b)))



(defun pt-propertyp (a pt g)
  (if (endp pt) t
    (and (or (null (cdar pt))
             (pathp-from-to (cdar pt) a (caar pt) g))
         (pt-propertyp a (cdr pt) g))))


(defun lte (n1 n2)
  (not (lt n2 n1)))

(defun shorterp (p1 p2 g)
  (lte (path-len p1 g)
       (path-len p2 g)))

(defun confinedp (p fs)
  (if (endp p) t
    (if (endp (cdr p)) t
      (and (memp (car p) fs)
           (confinedp (cdr p) fs)))))
#|
(defun-sk shortest-confined-pathp (a b p fs g)
  (forall path (implies (and (pathp-from-to path a b g)
                             (confinedp path fs))
                        (shorterp p path g))))
|#

(ENCAPSULATE
      NIL (LOGIC)
      (SET-MATCH-FREE-DEFAULT :ALL)
      (SET-INHIBIT-WARNINGS "Theory"
                            "Use" "Free" "Non-rec" "Infected")
      (ENCAPSULATE
           ((SHORTEST-CONFINED-PATHP-WITNESS (A B P FS G) PATH)
            (SHORTEST-CONFINED-PATHP (A B P FS G) T))
           (LOCAL (IN-THEORY '(IMPLIES)))
           (LOCAL (DEFCHOOSE SHORTEST-CONFINED-PATHP-WITNESS (PATH)
                             (A B P FS G)
                             (NOT (IMPLIES (AND (PATHP-FROM-TO PATH A B G)
                                                (CONFINEDP PATH FS))
                                           (SHORTERP P PATH G)))))
           (LOCAL (DEFUN SHORTEST-CONFINED-PATHP (A B P FS G)
                     (LET ((PATH (SHORTEST-CONFINED-PATHP-WITNESS A B P FS G)))
                          (IMPLIES (AND (PATHP-FROM-TO PATH A B G)
                                        (CONFINEDP PATH FS))
                                   (SHORTERP P PATH G)))))
;(IN-THEORY (DISABLE (SHORTEST-CONFINED-PATHP)))
           (DEFTHM SHORTEST-CONFINED-PATHP-DEF
             (EQUAL (SHORTEST-CONFINED-PATHP A B P FS G)
                    (LET ((PATH (SHORTEST-CONFINED-PATHP-WITNESS A B P FS G)))
                         (IMPLIES (AND (PATHP-FROM-TO PATH A B G)
                                       (CONFINEDP PATH FS))
                                  (SHORTERP P PATH G)))))
           (DEFTHM SHORTEST-CONFINED-PATHP-NECC
                   (IMPLIES (NOT (IMPLIES (AND (PATHP-FROM-TO PATH A B G)
                                               (CONFINEDP PATH FS))
                                          (SHORTERP P PATH G)))
                            (NOT (SHORTEST-CONFINED-PATHP A B P FS G)))
                   :HINTS (("Goal" :USE (SHORTEST-CONFINED-PATHP-WITNESS SHORTEST-CONFINED-PATHP)
                            :IN-THEORY (THEORY 'MINIMAL-THEORY))))))
(include-book "data-structures/utilities" :dir :system)
(include-book "tools/bstar" :dir :system)
(include-book "ordinals/e0-ordinal" :dir :system)
(encapsulate ()
;Auxiliary functions for fixers and for instantiating defun-sk
  (defun count-non-members (x y)
   (cond ((endp x) 0)
         ((member (car x) y) (count-non-members (cdr x) y))
         (t (+ 1 (count-non-members (cdr x) y)))))

(defun measure (c stack g)
   (cons (+ 1 (count-non-members (strip-cars g) stack))
         (len c)))

(defun neighbors2 (node g)
  (cond ((endp g) nil)
        ((equal node (car (car g))) (cdr (car g)))
        (t (neighbors2 node (cdr g)))))


(set-well-founded-relation e0-ord-<)
(local (in-theory (enable strip-cars)))

(defun find-all-next-steps (c stack b g)
    (declare (xargs :measure (measure c stack g)))
    (cond
     ((endp c) nil)
     ((member (car c) stack)
      (find-all-next-steps (cdr c) stack b g))
     ((equal (car c) b)
      (cons (reverse (cons b stack))
            (find-all-next-steps (cdr c) stack b g)))
     (t (append (find-all-next-steps (neighbors2 (car c) g)
                                     (cons (car c) stack)
                                     b g)
                (find-all-next-steps (cdr c) stack b g)))))

(defun change-rep (vs g)
  ;remove weights and also add empty entries for non-neighbour vertices
  (if (endp vs)
    g
    (b* ((entry (assoc-equal (car vs) g))
         ((when (null entry)) (change-rep (cdr vs) (put-assoc-equal (car vs) nil g)))
         (edges-with-weights (cdr entry))
         (edges (strip-cars edges-with-weights)))
        (change-rep (cdr vs) (put-assoc-equal (car vs) edges g)))))
  
(defun find-all-simple-paths (a b g)
  (if (and (equal a b) (nodep a g)) ; added for type-safety and less-restrictive fixer rules
    (list (list a))
    (b* ((g1 (change-rep (all-nodes g) g)))
        (find-all-next-steps (neighbors2 a g1) ;(neighbours a g1) bug 
                             (list a) b g1))))
)

(u::defloop shortest-confined-pathp/exec1 (paths a b p fs g)
  (for ((path in paths)) (always (implies (and (pathp-from-to path a b g)
                                               (confinedp path fs))
                                          (shorterp p path g)))))

(defun shortest-confined-pathp/exec (a b p fs g)
  (shortest-confined-pathp/exec1 (find-all-simple-paths a b g) a b p fs g))

(defttag t)
;(defattach (shortest-confined-pathp shortest-confined-pathp/exec)) guards not verified
(defattach (shortest-confined-pathp shortest-confined-pathp/exec) :skip-checks t)
(defttag nil)

#|  
(defun-sk shortest-pathp (a b p g)
  (forall path (implies (pathp-from-to path a b g)
                        (shorterp p path g))))
|#

(ENCAPSULATE NIL (LOGIC)
              (SET-MATCH-FREE-DEFAULT :ALL)
              (SET-INHIBIT-WARNINGS "Theory"
                                    "Use" "Free" "Non-rec" "Infected")
              (ENCAPSULATE ((SHORTEST-PATHP-WITNESS (A B P G) PATH)
                             (SHORTEST-PATHP (A B P G) T))
                           (LOCAL (IN-THEORY '(IMPLIES)))
                           (LOCAL (DEFCHOOSE SHORTEST-PATHP-WITNESS (PATH)
                                             (A B P G)
                                             (NOT (IMPLIES (PATHP-FROM-TO PATH A B G)
                                                           (SHORTERP P PATH G)))))
                           (LOCAL (DEFUN SHORTEST-PATHP (A B P G)
                                    (LET ((PATH (SHORTEST-PATHP-WITNESS A B P G)))
                                      (IMPLIES (PATHP-FROM-TO PATH A B G)
                                               (SHORTERP P PATH G)))))
;(IN-THEORY (DISABLE (SHORTEST-PATHP)))
                           (DEFTHM SHORTEST-PATHP-DEF
                             (EQUAL (SHORTEST-PATHP A B P G)
                                    (LET ((PATH (SHORTEST-PATHP-WITNESS A B P G)))
                                         (IMPLIES (PATHP-FROM-TO PATH A B G)
                                                  (SHORTERP P PATH G)))))
                           (DEFTHM SHORTEST-PATHP-NECC
                                   (IMPLIES (NOT (IMPLIES (PATHP-FROM-TO PATH A B G)
                                                          (SHORTERP P PATH G)))
                                            (NOT (SHORTEST-PATHP A B P G)))
                                   :HINTS (("Goal" :USE (SHORTEST-PATHP-WITNESS SHORTEST-PATHP)
                                            :IN-THEORY (THEORY 'MINIMAL-THEORY))))))

(u::defloop shortest-pathp/exec1 (paths a b p g)
            (for ((path in paths)) (always (implies (pathp-from-to path a b g)
                                                    (shorterp p path g)))))

(defun shortest-pathp/exec (a b p g)
  (shortest-pathp/exec1 (find-all-simple-paths a b g) a b p g))

(defttag t)
(defattach (shortest-pathp shortest-pathp/exec) :skip-checks t)
(defttag nil)

(defun ts-propertyp (a ts fs pt g)
  (if (endp ts) t
    (and (shortest-confined-pathp a (car ts) (path (car ts) pt) fs g)
         (confinedp (path (car ts) pt) fs)
         (ts-propertyp a (cdr ts) fs pt g))))

(defun fs-propertyp (a fs fs0 pt g)
  (if (endp fs) t
    (and (shortest-pathp a (car fs) (path (car fs) pt) g)
         (confinedp (path (car fs) pt) fs0)
         (fs-propertyp a (cdr fs) fs0 pt g))))

(defun comp-set (ts s)
  (if (endp s) nil
    (if (memp (car s) ts)
        (comp-set ts (cdr s))
      (cons (car s) (comp-set ts (cdr s))))))

(defun invp (ts pt g a)
  (let ((fs (comp-set ts (all-nodes g))))
    (and (ts-propertyp a ts fs pt g)
         (fs-propertyp a fs fs pt g)
         (pt-propertyp a pt g))))

(defun find-partial-path (p s)
  (if (endp p) nil
    (if (memp (car p) s)
        (cons (car p) (find-partial-path (cdr p) s))
      (list (car p)))))

(defun find-last-next-path (p)
  (if (or (endp p) (endp (cdr p))) nil
    (cons (car p) (find-last-next-path (cdr p)))))

(defun last-node (p)
  (car (last (find-last-next-path p))))

(defun find-partial-path-to-u (p u)
  (cond ((not (memp u p)) nil)
        ((equal (car p) u) (list u))
        (t (cons (car p)
                 (find-partial-path-to-u (cdr p) u)))))