File: file-system-1.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 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,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (427 lines) | stat: -rw-r--r-- 14,621 bytes parent folder | download | duplicates (3)
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
(in-package "ACL2")

;  file-system-1.lisp                                  Mihir Mehta

; Here we define the rudiments of a file system.  We first start with
; a file-system recognizer, and then we define various file-system
; operations.

(include-book "../file-system-lemmas")
(include-book "../utilities/insert-text")

(defun l1-fs-p (fs)
  (declare (xargs :guard t))
  (if (atom fs)
      (null fs)
    (and (let ((directory-or-file-entry (car fs)))
           (if (atom directory-or-file-entry)
               nil
             (let ((name (car directory-or-file-entry))
                   (entry (cdr directory-or-file-entry)))
               (and (symbolp name)
                    (or (stringp entry) (l1-fs-p entry))))))
         (l1-fs-p (cdr fs)))))

(defthm alistp-l1-fs-p
  (implies (l1-fs-p fs)
           (alistp fs)))

(defthm l1-fs-p-assoc
  (implies (and (l1-fs-p fs)
                (consp (assoc-equal name fs))
                (consp (cdr (assoc-equal name fs))))
           (l1-fs-p (cdr (assoc-equal name fs)))))

(defun l1-stat (hns fs)
  (declare (xargs :guard (and (symbol-listp hns)
                              (l1-fs-p fs))))
  (if (atom hns)
      fs
    (if (atom fs)
        nil
      (let ((sd (assoc (car hns) fs)))
        (if (atom sd)
            nil
          (let ((contents (cdr sd)))
            (if (stringp contents)
                (and (null (cdr hns))
                     contents)
              (l1-stat (cdr hns) contents))))))))

(defthm l1-stat-of-l1-stat-lemma-1
  (implies (and (l1-fs-p fs)
                (consp (assoc-equal name fs))
                (not (stringp (cdr (assoc-equal name fs)))))
           (l1-fs-p (cdr (assoc-equal name fs)))))

(defthm l1-stat-of-l1-stat
  (implies (and (symbol-listp inside)
                (symbol-listp outside)
                (l1-stat outside fs)
                (l1-fs-p fs))
           (equal (l1-stat inside (l1-stat outside fs))
                  (l1-stat (append outside inside) fs)))
  :hints
  (("Goal"
    :induct (l1-stat outside fs))))

(defun l1-rdchs (hns fs start n)
  (declare (xargs :guard (and (symbol-listp hns)
                              (l1-fs-p fs)
                              (natp start)
                              (natp n))))
  (let ((file (l1-stat hns fs)))
    (if (not (stringp file))
        nil
      (let ((file-length (length file))
            (end (+ start n)))
        (if (< file-length end)
            nil
          (subseq file start (+ start n)))))))

; Delete file
(defun
    l1-unlink (hns fs)
  (declare (xargs :guard (and (symbol-listp hns) (l1-fs-p fs))))
  (if
      (atom hns)
      fs ;;error case, basically
    (if
        (atom (cdr hns))
        (remove1-assoc (car hns) fs)
      (if
          (atom fs)
          nil
        (let
            ((sd (assoc (car hns) fs)))
          (if (atom sd)
              fs
            (let ((contents (cdr sd)))
              (if (atom contents)
                  (and (null (cdr hns)) contents)
                (cons (cons (car sd)
                            (l1-unlink (cdr hns) contents))
                      (remove1-assoc (car hns) fs))))))))))

; The problem with this definition of l1-wrchs is that it deletes a directory if
; it's found where a text file is expected
(defun
    l1-wrchs (hns fs start text)
  (declare (xargs :guard (and (symbol-listp hns)
                              (or (stringp fs) (l1-fs-p fs))
                              (natp start)
                              (stringp text))))
  (if
      (atom hns)
      (let ((file fs))
        (if (not (stringp file))
            file ;; error, so leave fs unchanged
          (coerce (insert-text (coerce file 'list)
                               start text)
                  'string)))
    (if (atom fs)
        fs ;; error, so leave fs unchanged
      (let ((sd (assoc (car hns) fs)))
        (if (atom sd)
            fs ;; error, so leave fs unchanged
          (let ((contents (cdr sd)))
            (cons (cons (car sd)
                        (l1-wrchs (cdr hns)
                                  contents start text))
                  (remove1-assoc (car hns) fs))))))))

(defthm l1-wrchs-returns-fs-lemma-1
  (implies (and (consp (assoc-equal s fs))
                (l1-fs-p fs))
           (symbolp (car (assoc-equal s fs)))))

(defthm l1-wrchs-returns-fs-lemma-2
  (implies (l1-fs-p fs)
           (l1-fs-p (remove1-assoc-equal s fs))))

(defthm
  l1-wrchs-returns-fs-lemma-3
  (implies (and (not (l1-fs-p (cdr (assoc-equal (car hns) fs))))
                (l1-fs-p fs)
                (not (stringp (l1-wrchs (cdr hns)
                                        (cdr (assoc-equal (car hns) fs))
                                        start text))))
           (l1-fs-p (l1-wrchs (cdr hns)
                              (cdr (assoc-equal (car hns) fs))
                              start text)))
  :hints (("goal" :in-theory (disable l1-wrchs-returns-fs-lemma-1)
           :use (:instance l1-wrchs-returns-fs-lemma-1
                           (s (car hns))))))

(defthm l1-wrchs-returns-fs
  (implies (l1-fs-p fs)
           (l1-fs-p (l1-wrchs hns fs start text))))

(defthm l1-unlink-returns-fs
  (implies (and (l1-fs-p fs))
           (l1-fs-p (l1-unlink hns fs))))

(defun l1-create (hns fs text)
  (declare (xargs :guard (and (symbol-listp hns)
                              (l1-fs-p fs)
                              (stringp text))))
  (if (atom hns)
      fs ;; error - showed up at fs with no name  - so leave fs unchanged
    (let ((sd (assoc (car hns) fs)))
      (if (atom sd)
          (cons
           (cons
            (car hns)
            (if (atom (cdr hns))
                text
              (l1-create (cdr hns) nil text)))
           fs)
        (let ((contents (cdr sd)))
          (cons (cons (car sd)
                      (if (stringp (cdr sd))
                          contents ;; file already exists, so leave fs unchanged
                        (l1-create (cdr hns) contents text)))
                (remove1-assoc (car hns) fs))
          )))))

(defthm l1-create-returns-fs
  (implies (and (symbol-listp hns)
                (l1-fs-p fs)
                (stringp text))
           (l1-fs-p (l1-create hns fs text)))
  :rule-classes (:rewrite :type-prescription))

(defthm l1-read-after-write-1-lemma-1
  (implies (consp (assoc-equal name alist))
           (equal (car (assoc-equal name alist)) name)))

(defthm l1-read-after-write-2-lemma-2
  (implies (and (consp hns1)
                (consp fs)
                (consp (assoc-equal (car hns1) fs))
                (l1-fs-p fs)
                (symbolp (car hns1))
                (not (cdr hns1))
                (stringp (l1-stat hns1 fs)))
           (equal (l1-stat hns1 fs) (cdr (assoc (car hns1) fs)))))

(encapsulate
  ()

  (local
   (defun
       induction-scheme (hns1 hns2 fs)
     (if
         (atom hns1)
         fs
       (if
           (atom fs)
           nil
         (let
             ((sd (assoc (car hns2) fs)))
           (if
               (atom sd)
               fs
             (if
                 (atom hns2)
                 fs
               (if (not (equal (car hns1) (car hns2)))
                   fs
                 (let ((contents (cdr sd)))
                   (if (atom (cdr hns1))
                       (cons (cons (car sd) contents)
                             (remove1-assoc (car hns2) fs))
                     (cons (cons (car sd)
                                 (induction-scheme (cdr hns1)
                                                   (cdr hns2)
                                                   contents))
                           (remove1-assoc (car hns2) fs))))))))))))

  (defthm
    l1-read-after-write-2-lemma-3
    (implies
     (l1-fs-p fs)
     (equal
      (stringp (l1-stat hns1 (l1-wrchs hns2 fs start2 text2)))
      (stringp (l1-stat hns1 fs))))
    :hints (("goal" :induct (induction-scheme hns1 hns2 fs))))

  (defthm
    l1-stat-after-write
    (implies (and (l1-fs-p fs)
                  (stringp text2)
                  (symbol-listp hns1)
                  (symbol-listp hns2)
                  (natp start2)
                  (stringp (l1-stat hns1 fs)))
             (equal (l1-stat hns1 (l1-wrchs hns2 fs start2 text2))
                    (if (not (equal hns1 hns2))
                        (l1-stat hns1 fs)
                        (coerce (insert-text (coerce (l1-stat hns1 fs) 'list)
                                             start2 text2)
                                'string))))
    :hints (("goal" :induct (induction-scheme hns1 hns2 fs)))))

(defthm l1-read-after-write-1
  (implies (and (l1-fs-p fs)
                (stringp text)
                (symbol-listp hns)
                (natp start)
                (equal n (length text))
                (stringp (l1-stat hns fs)))
           (equal (l1-rdchs hns (l1-wrchs hns fs start text) start n) text)))

(defthm l1-read-after-write-2
  (implies (and (l1-fs-p fs)
                (stringp text2)
                (symbol-listp hns1)
                (symbol-listp hns2)
                (not (equal hns1 hns2))
                (natp start1)
                (natp start2)
                (natp n1))
           (equal (l1-rdchs hns1 (l1-wrchs hns2 fs start2 text2) start1 n1)
                  (l1-rdchs hns1 fs start1 n1))))

;; Induction argument for the proof of
;; (implies
;;  (and (l1-fs-p fs)
;;       (stringp text2)
;;       (symbol-listp hns1)
;;       (symbol-listp hns2)
;;       (not (equal hns1 hns2))
;;       (not (l1-stat hns2 fs))
;;       (stringp (l1-stat hns2 (l1-create hns2 fs text2)))
;;       (stringp (l1-stat hns1 fs)))
;;  (equal (l1-stat hns1 (l1-create hns2 fs text2)) (l1-stat hns1 fs)))
;; now, let's semi-formally write the cases we want.
;; case 1: (atom hns1) - this will violate the hypothesis
;; (stringp (l1-stat hns1 fs))
;; case 2: (and (consp hns1) (atom fs)) - this will violate the hypothesis
;; (stringp (l1-stat hns1 fs))
;; case 3: (and (consp hns1) (consp fs) (atom (assoc (car hns1) fs))) - this
;; will violate the hypothesis (stringp (l1-stat hns1 fs))
;; case 4: (and (consp hns1) (consp fs) (consp (assoc (car hns1) fs))
;;              (atom hns2)) - this will violate the hypothesis
;; (not (l1-stat hns2 fs))
;; case 5: (and (consp hns1) (consp fs) (consp (assoc (car hns1) fs))
;;              (consp hns2) (equal (car hns1) (car hns2))
;;              (stringp (cdr (assoc (car hns1) fs)))) - this will violate the
;; hypothesis (not (l1-stat hns2 fs))
;; case 6: (and (consp hns1) (consp fs) (consp (assoc (car hns1) fs))
;;              (consp hns2) (equal (car hns1) (car hns2))
;;              (not (stringp (cdr (assoc (car hns1) fs))))) - in this case,
;; (l1-stat hns1 (l1-create hns2 fs text2)) =
;; (l1-stat (cdr hns1) (cdr (assoc (car hns1) (l1-create hns2 fs text2)))) =
;; (l1-stat (cdr hns1) (l1-create (cdr hns2) (cdr (assoc (car hns1) fs)) text2)) =
;; (l1-stat (cdr hns1) (cdr (assoc (car hns1) fs))) = (induction hypothesis)
;; (l1-stat hns1 fs)
;; case 7: (and (consp hns1) (consp fs) (consp (assoc (car hns1) fs))
;;              (consp hns2) (not (equal (car hns1) (car hns2)))) - in this
;; case,
;; (l1-stat hns1 (l1-create hns2 fs text2)) =
;; (l1-stat (cdr hns1) (cdr (assoc (car hns1) fs))) =
;; (l1-stat hns1 fs)

(encapsulate
  ()

  (local
   (defun
       induction-scheme (hns1 hns2 fs)
     (if
         (atom hns1)
         fs
       (if
           (atom fs)
           nil
         (let
             ((sd (assoc (car hns1) fs)))
           (if
               (atom sd)
               fs
             (if
                 (atom hns2)
                 fs
               (if (not (equal (car hns1) (car hns2)))
                   fs
                 (let ((contents (cdr sd)))
                   (if (stringp (cdr sd))
                       (cons (cons (car sd) contents)
                             (remove1-assoc (car hns2) fs))
                     (cons (cons (car sd)
                                 (induction-scheme (cdr hns1)
                                                   (cdr hns2)
                                                   contents))
                           (remove1-assoc (car hns2) fs))))))))))))

  (defthm
    l1-stat-after-create
    (implies
     (and (l1-fs-p fs)
          (stringp text2)
          (symbol-listp hns1)
          (symbol-listp hns2)
          (not (l1-stat hns2 fs))
          (stringp (l1-stat hns2 (l1-create hns2 fs text2)))
          (stringp (l1-stat hns1 fs)))
     (equal (l1-stat hns1 (l1-create hns2 fs text2))
            (if (equal hns1 hns2)
                text2 (l1-stat hns1 fs))))
    :hints (("goal" :induct (induction-scheme hns1 hns2 fs)))))

(defthm l1-read-after-create-1
  (implies (and (l1-fs-p fs)
                (stringp text)
                (symbol-listp hns)
                (equal n (length text))
                (not (l1-stat hns fs))
                (stringp (l1-stat hns (l1-create hns fs text))))
           (equal (l1-rdchs hns (l1-create hns fs text) 0 n) text)))

(defthm l1-read-after-create-2
  (implies (and (l1-fs-p fs)
                (stringp text2)
                (symbol-listp hns1)
                (symbol-listp hns2)
                (not (equal hns1 hns2))
                (natp start1)
                (natp n1)
                (not (l1-stat hns2 fs))
                (stringp (l1-stat hns2 (l1-create hns2 fs text2)))
                (stringp (l1-stat hns1 fs)))
           (equal (l1-rdchs hns1 (l1-create hns2 fs text2) start1 n1)
                  (l1-rdchs hns1 fs start1 n1))))

; Find length of file
(defun l1-wc-len (hns fs)
  (declare (xargs :guard (and (symbol-listp hns)
                              (l1-fs-p fs))))
  (let ((file (l1-stat hns fs)))
    (if (not (stringp file))
        nil
      (length file))))

; Prove (list-of-chars-to-string (string-to-chars str))
;       (string-to-chars (list-of-chars-to-string char-list))
; and then, you will be positioned to use either form.
#||From :doc STR::STD/STRINGS/COERCE
  Theorem: <coerce-inverse-1-better>

    (defthm coerce-inverse-1-better
            (equal (coerce (coerce x 'string) 'list)
                   (if (stringp x)
                       nil (make-character-list x))))

  Theorem: <coerce-inverse-2-better>

    (defthm coerce-inverse-2-better
            (equal (coerce (coerce x 'list) 'string)
                   (if (stringp x) x "")))
That takes care of that
||#
; Correspond (or not) with Linux system calls -- the low-level stuff...

; Add file -- or, if you will, create a file with some initial contents

; and so on...