This file is indexed.

/usr/share/acl2-6.5/books/cgen/simple-graph-array.lisp is in acl2-books-source 6.5-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
426
427
428
429
430
431
432
433
434
435
436
437
#|$ACL2s-Preamble$;
(ld ;; Newline to fool ACL2/cert.pl dependency scanner
 "portcullis.lsp")
(acl2::begin-book t :ttags :all);$ACL2s-Preamble$|#

;Author: Harsh Raju Chamarthi (harshrc)

(in-package "DEFDATA")
(include-book "utilities")
(include-book "ordinals/lexicographic-ordering-without-arithmetic" :dir :system)

(defun make-n-upto-list (size ans)
;make a list of natural numbers upto (size-1)
;(make-n-upto-list 3 nil) ==> (0 1 2)
  (declare (xargs :guard (and (natp size)
                              (nat-listp ans))))
  (if (zp size)
    ans
    (make-n-upto-list  (1- size) (cons (1- size) ans))))  

(set-verify-guards-eagerness 0)


(defmacro in (a X) 
  `(member-equal ,a ,X))

(defun vertex-indexes (vs sym-lst)
;returns a natural number which is associated with
;v in mapping. vs is the original symbol-list
;used to create mapping
 (declare (xargs :guard (and (symbol-listp vs)
                             (symbol-listp sym-lst)
                             (subsetp vs sym-lst))))
 (if (endp vs)
     nil
   (cons (position (car vs) sym-lst)
         (vertex-indexes (cdr vs) sym-lst))))

(defstobj g$
  (adj-list-array :type (array T (0))
                  :initially nil
                  :resizable t)
  :renaming ((adj-list-arrayi ai)
             (update-adj-list-arrayi ui))
  :doc "Graph represented as an adjacency list array.
        Key is vertex-index.
        Value is a record with the following fields:
        :name - (symbol) name of the vertex
        :adj - (nat-list) list of indexes of adjacent vertices
        :seen - boolean (bit) indicating wether this vertex has been visited
        :cc - (nat) indicating the connected component this vertex belongs to"
  :inline t)

(defrec vinfo%
  (name adj seen cc)
  NIL)


(defmacro make-g$-array-value (name &key adj seen cc)
 `(acl2::make vinfo%
     :name ,name
     :adj ,adj
     :seen ,seen
     :cc ,(or cc 0)))

(defun symbol-alst->g$1 (alst vs g$)
 (declare (xargs :stobjs (g$)
                 :guard (and (symbol-alistp alst)
                            (symbol-listp vs)
                            (g$p g$)
                            )))
;transforms a symbol-alist graph adjacency list representation
;to an g$ adjacency list representation.
 (if (endp alst)
     g$
   (b* (((cons v adj-vs) (car alst))
        ((list i) (vertex-indexes (list v) vs))
        (adj-is (vertex-indexes adj-vs vs))
        (g$ (ui i (make-g$-array-value v :adj adj-is) g$)))
     (symbol-alst->g$1 (cdr alst) vs g$))))


(defun init-g$1 (i vs g$)
 (declare (xargs :stobjs (g$)
                 :guard (and (symbol-listp vs)
                             (g$p g$)
                             )))
;intializes a g$ to a graph with no edges
 (if (endp vs)
     g$
     (let ((g$ (ui i (make-g$-array-value (car vs)) g$)))
       (init-g$1 (1+ i) (cdr vs) g$))))

(defun init-g$ (vs size g$)
  ";intializes/resets a g$ to a graph with no edges"
  (declare (xargs :stobjs (g$)
                  :guard (and (symbol-listp vs)
                              (= (len vs) size)
                              (g$p g$)
                              )))
  (let ((g$ (resize-adj-list-array size g$)))
    (init-g$1 0 vs g$)))
#||
(defun reset-g$-aux (l g$)
  (if (zp l)
      g$
    (reset-g$-aux (1- l)
                  ;;index corresponding to l is (1- l) which is the last
                  ;;element initially
                  (ui (1- l) nil g$))))

(defun reset-g$ (g$)
  "reset the information stored for each vertice"
  (reset-g$-aux (adj-list-array-length g$) g$))
||#

(defun symbol-alist->g$ (alst g$)
"top-level call to populate g$ with adj-list information obtained
from alst. Assumption: (len alst) = number of vertices in graph and
[strip-cars alst] has distinct vertices"
 (declare (xargs :stobjs (g$)
                 :guard (and (symbol-alistp alst)
                             (g$p g$))))
                 
 (b* ((vs (strip-cars alst))
      (size (len alst))
      (g$ (init-g$ vs size g$)))
   (symbol-alst->g$1 alst 
                     vs ;find position (index)
                     g$
                     )))

(set-well-founded-relation acl2::l<)



;Dasgupta Algo
;Vertices are natural numbers.
(defun dfs-visit1 (g$ v n fin flag)
"explore the graph g$ (adj-list array) starting at v.
n is the number of vertices of g$ not seen,
initially it is just the total number of vertices.
fin is the list of finished vertices, with the
(car fin) being the last finished vertice, i.e
the vertice with the maximum post time."
  (declare (xargs :stobjs (g$)
                  :guard (and (g$p g$)
                              (or (natp v);vertice
                                  (nat-listp v));vertices
                              (nat-listp fin)
                              (natp n))
                  :measure (list (nfix n) (acl2-count v))))
  (if (zp n);visited all vertices
      (mv g$ fin)
    (if (equal 'dfs-visit flag)
;DFS-VISIT
        (b* ((v-entry (ai v g$))
             (adj-vs (acl2::access vinfo% v-entry :adj))
             (g$ (ui v (acl2::change vinfo% v-entry :seen t) g$));update/change seen
             ((mv g$ fin!)
              (dfs-visit1 g$ adj-vs (1- n) fin 'dfs-visit-lst)))
          ;;update finished vertices
          (mv g$ (cons v fin!)))
;DFS-VISIT-LST
      (if (endp v);visited all neighbours
          (mv g$ fin)
        (b* ((v-entry (ai (car v) g$))
             ;(- (cw "dfs-visit-lst: v-entry for ~x0 is ~x1~%" (car v) v-entry))
             )
          (if (acl2::access vinfo% v-entry :seen);already seen
              (dfs-visit1 g$ (cdr v) n fin 'dfs-visit-lst)
            (b* (((mv g$ fin!)
                  (dfs-visit1 g$ (car v) n fin 'dfs-visit)))
              (dfs-visit1 g$ (cdr v) n fin! 'dfs-visit-lst))))))))
        

(defun dfs-all-vertices (g$ vs n fin cnum) 
  "Do DFS over all vertices in vs"
  (declare (xargs :stobjs (g$)
                  :guard (and (g$p g$)
                              (nat-listp vs);vertices
                              (nat-listp fin)
                              (natp cnum)
                              (natp n))))
 
  (if (endp vs);visited all neighbours
      (mv g$ fin)
      (b* ((v-entry (ai (car vs) g$)))
           ;(- (cw "dfs-all: v-entry for ~x0 is ~x1~%" (car vs) v-entry)))
        (if (acl2::access vinfo% v-entry :seen);already seen
            (dfs-all-vertices g$ (cdr vs) n fin cnum)
        (b* ((g$ (ui (car vs) 
                     (acl2::change vinfo% v-entry :cc cnum)
                     g$))
             ((mv g$ fin!) 
              (dfs-visit1 g$
                          ;;update current component as part of pre 
                          (car vs) n fin 
                          'dfs-visit)))
          (dfs-all-vertices g$ (cdr vs) n fin! (1+ cnum)))))))


(defun dfs1 (g$ vs)
;Depth First Search on adj list array g$ iterating
;over the vertices in vs.
  (declare (xargs :stobjs (g$)
                  :guard (and (nat-listp vs)
                              (g$p g$))))
  (dfs-all-vertices g$ vs (adj-list-array-length g$) nil 0))

;(defdata adjacency-list (map symbol symbol-list))
(defun adjacency-list1p (v)
  (if (null v)
      t
    (if (atom v)
        nil
      (let ((entry (car v)))
      (and (symbolp (car entry))
           (symbol-listp (cdr entry))
           (no-duplicatesp (cdr entry))
           (adjacency-list1p (cdr v)))))))

(defun adjacency-listp (v)
  (and (adjacency-list1p v)
       (no-duplicatesp (strip-cars v))))

(defun make-empty-adj-list (vars)
  (declare (xargs :guard (and (symbol-listp vars)
                              (no-duplicatesp vars))))
  ;order important 
  ;order of keys alst created is the same as order of vars
  (if (endp vars)
    nil
    (cons (cons (car vars) nil)
          (make-empty-adj-list (cdr vars)))))



;fs means Functionaly dependent vars
;ASSUMPTION: alst has all the variables as keys already
;this function just updates the entries, doesnt insert
;new entries.
(defun union-entry-in-adj-list (var fvars alst)
  (declare (xargs :guard (and (adjacency-listp alst)
                              (true-listp fvars))))
  (if (endp alst)
      nil
    (if (eq var (caar alst))
      (cons (cons var (union-equal fvars 
                                   (cdar alst)))
            (cdr alst))
      (cons (car alst)
            (union-entry-in-adj-list var fvars (cdr alst))))))
  
  
;recurse above fun over list of indices
(defun union-entries-in-adj-list (is fis alst)
 (declare (xargs :guard (and (adjacency-listp alst)
                             (true-listp is)
                             (true-listp fis))))
 (if (endp is)
    alst
    (union-entries-in-adj-list 
     (cdr is) fis (union-entry-in-adj-list (car is) fis alst))))

  
(defun transpose-alst1 (alst ans)
;Scan G at index i and transpose the result corresponding to i in ans
  (declare (xargs :guard (and (adjacency-listp alst)
                              (adjacency-listp ans)
                              )))
  (if (endp alst)
      ans
    (b* (((cons v vs) (car alst)))
      (transpose-alst1 (cdr alst)
                       (union-entries-in-adj-list vs (list v) ans)))))
  

(defun transpose-alst (alst)
;Return transpose/reverse of alst
;INVARIANT: Order is very important
  (declare (xargs :guard (adjacency-listp alst)))
  (transpose-alst1 alst (make-empty-adj-list (strip-cars alst))))
#|
(defthm transpose-idempotent
  (implies (adjacency-list1p x)
           (equal (transpose-alst (transpose-alst x))
                  x)))

(defthm transpose-doesnt-change-order
  (implies (adjacency-list1p x)
           (equal (strip-cars (transpose-alst x))
                  (strip-cars x))))
|#




(defun scc1 (alst g$)
;Strongly Connected Components of adj list array G,
;alst is the same adj-list, but in form of an alist
  (declare (xargs :stobjs (g$)
                  :guard (and (symbol-alistp alst)
                              (adjacency-listp alst)
                              (g$p g$))))
  (b* ((r-alst (transpose-alst alst))
       (g$ (symbol-alist->g$ r-alst g$))
       (N (adj-list-array-length g$))
       ((mv g$ fin) (dfs1 g$ (make-n-upto-list N nil)))
       (g$ (symbol-alist->g$ alst g$))
       ((mv g$ fin!) (dfs1 g$ fin)))
    (mv g$ fin!)))


(defun g$->var-quotient-alst1 (g$ i size ans)
  "Given graph g$, where g$[v]=(record name adj-is seenBit ccnum), we will
return, symbol alist, which maps each vertex (name), to its component
number (ccnum). This is used in simple-var-hyp? for finding cycles."
  (declare (xargs  :stobjs (g$)
                   :measure (nfix (- size i))
                   :guard (and (natp i) (natp size)
                               (<= i size))))
  (if (zp (- size i))
      ans
    (let ((v-entry (ai i g$)))
      (g$->var-quotient-alst1 g$ (1+ i) size
                             (acons (acl2::access vinfo% v-entry :name) 
                                    (acl2::access vinfo% v-entry :cc)
                                    ans)))))

(defun g$->var-quotient-alst (g$)
  (declare (xargs :stobjs (g$)))
  (g$->var-quotient-alst1 g$ 0 (adj-list-array-length g$) nil))

(defun vertex-names (is g$)
  (declare (xargs :stobjs (g$)
                  :guard (nat-listp is)))
  (if (endp is)
      nil
    (cons (acl2::access vinfo% (ai (car is) g$) :name)
          (vertex-names (cdr is) g$))))

(defun g$->alst1 (g$ i size ans)
  (declare (xargs  :stobjs (g$)
                   :measure (nfix (- size i))
                   :guard (and (natp i) (natp size)
                               (<= i size))))
  (if (zp (- size i))
      ans
    (let ((v-entry (ai i g$)))
      (g$->alst1 g$ (1+ i) size
                    (acons (acl2::access vinfo% v-entry :name) 
                           (vertex-names (acl2::access vinfo% v-entry :adj) g$)
                           ans)))))

(defun g$->symbol-alist (g$)
  (declare (xargs  :stobjs (g$)))
  (g$->alst1 g$ 0 (adj-list-array-length g$) nil))


(defun scc0 (alst g$)
  (declare (xargs :stobjs (g$)
                  :guard (symbol-alistp alst)))
  (mv-let (g$ fin)
          (scc1 alst g$)
          (mv (g$->var-quotient-alst g$) 
              (vertex-names fin g$)
              (g$->symbol-alist g$)
              g$)))

(defun fix-adjacency-list (alst)
  (declare (xargs :guard (adjacency-listp alst)))
  "Fix an adjacency list to have in it keys all the vertices."
  (b* ((adj-v-lst-lst (strip-cdrs alst))
       (vs (strip-cars alst))
       (adj-vs (union-lsts adj-v-lst-lst))
       (missing-vs (set-difference-eq adj-vs vs))
       (missing-alst (pairlis$ missing-vs nil)))
    (append alst missing-alst)))

(defun strongly-connected-components (alst debug-flag)
   "Strongly Connected Components of adj list graph alst.
Gives (mv map-ccnum finished-vertex-list) as result, where
map-ccnum, maps each vertex to its component number. 
finished-vertex-list gives the list of vertexes in decreasing
post times."
   (declare (xargs :guard (adjacency-listp alst)))
    (b* ((alst! (fix-adjacency-list alst))
         (- (cw? (and (not (equal alst alst!))
                               debug-flag)
            "CEgen/Note: SCC: Got Adjacency list : ~x0 Fixed to : ~x1~%" alst alst!)))
      (acl2::with-local-stobj 
       g$
       
       (mv-let (var-ccnum-alst decreasing-post-times-vertex-lst adj-alst g$)
               (scc0 alst! g$)
               (mv var-ccnum-alst 
                   decreasing-post-times-vertex-lst
                   adj-alst)))))
   
;to check simple soundness (g$->symbol-alist g$) = alst!
  
(defun approximate-topological-sort (alst debug-flag)
;return vertices following the order ->, but
;since alst might not be a dag, the order 
;inside a component might be skewed, but we
;are okay with it, since we choose arbitrarily
;from within a component 
  (declare (xargs :guard (adjacency-listp alst)))
  (b* (((mv & fin-vs &) 
        (strongly-connected-components alst debug-flag)))
    fin-vs))
       



#|

;example:
;(untrace$ dfs dfs-visit dfs-all-vertices)
(let* ((A '((a b)
            (b e c d)
            (c f)
            (d)
            (e b g f)
            (f c h)
            (g j h )
            (h k)
            (i g)
            (j i)
            (k l)
            (l j))))
  (approximate-topological-sort A))
;ans:(A B E C F D G H K L J I)
;ans by memories graph.lisp: (A B E C F D G H K L J I)
|#  
;What correctness theorems can we prove?