aboutsummaryrefslogtreecommitdiff
path: root/L-99.lisp
blob: 26bb663674f3b04a2ca643fd25d752753d16a977 (plain)
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
;; P01 (*) Find the last box of a list.
(defun my-last (x)
  (declare (xargs :guard (and (true-listp x)
                              (consp x))))
  (if (endp (cdr x))
      x
      (my-last (cdr x))))

(defthm theorem-of-my-last
  (implies (and (true-listp x)
                (consp x))
           (equal (list (nth (1- (len x)) x))
                  (my-last x))))

;; P02 (*) Find the last but one box of a list.
(defun my-butlast (x)
  (declare (xargs :guard (and (true-listp x)
                              (consp x))))
  (if (endp (cdr x))
      nil
      (cons (car x)
            (my-butlast (cdr x)))))

(defthm my-butlast-my-last
  (implies (and (true-listp x)
                (consp x))
           (equal (append (my-butlast x) (my-last x))
                  x)))

;; P03 (*) Find the K'th element of a list.
(defun element-at (x i)
  (declare (xargs :guard (and (true-listp x)
                              (natp i)
                              (< i (len x)))))
  (if (zp i)
      (car x)
      (element-at (cdr x) (1- i))))

(defthm element-at-nil
  (equal (element-at nil i)
         nil))

(defthm elment-at-equal-nth
  (equal (element-at x i)
         (nth i x)))

;; P04 (*) Find the number of elements of a list.
(defun my-len (x)
  (declare (xargs :guard (true-listp x)))
  (if (endp x)
      0
      (+ 1 (my-len (cdr x)))))

(defun rev-iota (n)
  (if (zp n)
      nil
      (cons (1- n) (rev-iota (1- n)))))

(defthm my-len-rev-iota
  (implies (natp n)
           (equal (my-len (rev-iota n))
                  n)))

;; P05 (*) Reverse a list.
(defun app (x y)
  (declare (xargs :guard (and (true-listp x)
                              (true-listp y))))
  (if (endp x)
      y
      (cons (car x)
            (app (cdr x) y))))

(defthm true-listp-app
  (implies (true-listp y)
           (true-listp (app x y))))

(defun rev (x)
  (declare (xargs :guard (true-listp x)))
  (if (endp x)
      nil
      (app (rev (cdr x))
           (list (car x)))))

(defun revapp (x y)
  (declare (xargs :guard (and (true-listp x)
                              (true-listp y))))
  (if (endp x)
      y
      (revapp (cdr x)
              (cons (car x) y))))

(defmacro my-reverse (x)
  `(revapp ,x nil))

(defthm associativity-of-app
  (equal (app (app x y) z)
         (app x (app y z))))

(defthm app-rev-equal-revapp
  (equal (revapp x y)
         (app (rev x) y)))

(defthm my-reverse-equal-rev
  (equal (my-reverse x)
         (rev x)))

;; P06 (*) Find out whether a list is a palindrome.
(defmacro palindromep (x)
  `(equal (my-reverse ,x) ,x))

(defthm app-nil
  (implies (true-listp x)
           (equal (app x nil)
                  x)))

(defthm true-listp-rev
  (implies (true-listp x)
           (true-listp (rev x))))

(defthm rev-app
  (implies (true-listp y)
           (equal (rev (app x y))
                  (app (rev y) (rev x)))))

(defthm rev-rev
  (implies (true-listp x)
           (equal (rev (rev x))
                  x)))

(defthm sandwich-palindrome
  (implies (and (true-listp y)
                (palindromep x))
           (palindromep (app y (app x (rev y))))))

;; P07 (**) Flatten a nested list structure.
(defun flatten (x)
  (cond
    ((endp x) x)
    ((consp (car x))
     (app (flatten (car x))
          (flatten (cdr x))))
    (t (cons (car x)
             (flatten (cdr x))))))

(defthm flatten-flatten
  (equal (flatten (flatten x))
         (flatten x)))

(defthm flatten-app
  (equal (flatten (app x y))
         (app (flatten x)
              (flatten y))))

;; P08 (**) Eliminate consecutive duplicates of list elements.
(defun compress (x)
  (cond
    ((endp x) nil)
    ((endp (cdr x)) (list (car x)))
    ((equal (car x) (cadr x)) (compress (cdr x)))
    (t (cons (car x) (compress (cdr x))))))

(defthm car-compress
  (implies (consp x)
           (equal (car (compress x))
                  (car x))))

(defthm compress-compress
  (equal (compress (compress x))
         (compress x)))

;; P09 (**) Pack consecutive duplicates of list elements into sublists.
(defun pack-1 (x acc)
  (cond
    ((endp x)
     (if (endp acc)
         nil
         (list acc)))
    ((or (endp acc)
         (equal (car acc) (car x)))
     (pack-1 (cdr x) (cons (car x) acc)))
    (t (cons acc (pack-1 (cdr x) (list (car x)))))))

(defmacro pack (x)
  `(pack-1 ,x nil))

(defun collect-cars (x)
  (if (endp x)
      nil
      (cons (caar x)
            (collect-cars (cdr x)))))

(defthm collect-cars-pack-lemma-1
  (implies (endp (collect-cars (pack-1 x y)))
           (endp x)))

(defthm collect-cars-pack-lemma-2
  (implies (consp y)
           (equal (collect-cars (pack-1 nil y))
                  (list (car y)))))

(defun seqp (x)
  (cond
    ((endp x) t)
    ((endp (cdr x)) t)
    ((equal (car x) (cadr x))
     (seqp (cdr x)))
    (t nil)))

(defthm collect-cars-pack-1-lemma-1
  (implies (and (seqp y)
                (consp x)
                (consp y)
                (equal (car x) (car y)))
           (equal (collect-cars (pack-1 x y))
                  (collect-cars (pack-1 x (cdr y))))))

(defthm collect-cars-pack-1-lemma-2
  (implies (and (consp y)
                (endp (cdr y))
                (not (equal (car x) (car y))))
           (equal (collect-cars (pack-1 x y))
                  (cons (car y) (collect-cars (pack-1 x nil))))))

(defthm collect-cars-pack-1-lemma-3
  (implies (and (consp y)
                (consp (cdr y))
                (equal (car y) (cadr y)))
           (equal (collect-cars (pack-1 x y))
                  (collect-cars (pack-1 x (cdr y))))))

(defthm collect-cars-pack-1
  (implies (and (seqp y)
                (true-listp y))
           (equal (collect-cars (pack-1 x y))
                  (compress (app y x)))))

(defthm collect-cars-pack
  (equal (collect-cars (pack x))
         (compress x)))

;; P10 (*) Run-length encoding of a list.
(defun encode-1 (x)
  (if (endp x)
      nil
      (cons (list (len (car x)) (caar x))
            (encode-1 (cdr x)))))

(defmacro encode (x)
  `(encode-1 (pack ,x)))

;; P11 (*) Modified run-length encoding.
(defun encode-modified-1 (x)
  (cond
    ((endp x) nil)
    ((equal (caar x) 1)
     (cons (cadar x)
           (encode-modified-1 (cdr x))))
    (t
     (cons (car x)
           (encode-modified-1 (cdr x))))))

(defmacro encode-modified (x)
  `(encode-modified-1 (encode ,x)))

;; P12 (**) Decode a run-length encoded list.
(defun repeat (x n)
  (if (zp n)
      nil
      (cons x (repeat x (1- n)))))

(defthm len-repeat
  (implies (natp n)
           (equal (len (repeat x n))
                  n)))

(defthm car-repeat
  (equal (car (repeat x n))
         (if (zp n)
             nil
             x))
  :hints (("Goal" :expand (REPEAT X 1))))

(defthm seqp-repeat
  (implies (natp n)
           (seqp (repeat x n)))
  :hints (("Goal" :expand (repeat x (+ -1 n)))))

(defun decode (x)
  (cond
    ((endp x) nil)
    ((atom (car x))
     (cons (car x)
           (decode (cdr x))))
    (t (app (repeat (cadar x) (caar x))
            (decode (cdr x))))))

(defthm repeat-1-list
  (equal (repeat y 1) (list y))
  :hints (("Goal" :expand ((repeat y 1)))))


(defthm decode-encode-1-pack-1-lemma-1
  (implies (and (consp y)
                (true-listp x)
                (true-listp y)
                (consp y)
                (seqp y))
           (equal (decode (encode-1 (pack-1 x y)))
                  (cons (car y) (decode (encode-1 (pack-1 x (cdr y))))))))

(defthm decode-encode-1
  (implies (and (true-listp x)
                (true-listp y)
                (seqp y))
           (equal (decode (encode-1 (pack-1 x y)))
                  (app y x))))

(defthm decode-encode
  (implies (and (true-listp x)
                (true-listp y))
           (equal (decode (encode x))
                  x)))

(defthm decode-encode-modified-1-lemma-1
  (implies (and (atom-listp x)
                (atom-listp y)
                (consp y)
                (seqp y))
           (equal (decode (encode-modified-1 (encode-1 (pack-1 x y))))
                  (cons (car y)
                        (decode (encode-modified-1 (encode-1 (pack-1 x (cdr
                                                                        y)))))))))

(defthm decode-encode-modified-1
  (implies (and (atom-listp x)
                (atom-listp y)
                (seqp y))
           (equal (decode (encode-modified-1 (encode-1 (pack-1 x y))))
                  (app y x))))

(defthm decode-encode-modified
  (implies (atom-listp x)
           (equal (decode (encode-modified x))
                  x)))

;; P13 (**) Run-length encoding of a list (direct solution).
(defun encode-direct-1 (x prev n)
  (cond
    ((endp x)
     (cond
       ((zp n) nil)
       ((equal n 1) (list prev))
       (t (list (list n prev)))))
    ((or (zp n)
         (equal (car x) prev))
     (encode-direct-1 (cdr x) (car x) (1+ n)))
    ((equal n 1)
     (cons prev (encode-direct-1 (cdr x) (car x) 1)))
    (t  (cons (list n prev)
              (encode-direct-1 (cdr x) (car x) 1)))))

(defmacro encode-direct (x)
  `(encode-direct-1 ,x nil 0))

(defthm app-repeat-1
  (implies (and (natp n)
                (consp x))
           (equal (app (repeat (car x) n) x)
                  (cons (car x)
                        (app (repeat (car x) n) (cdr x))))))

(defthm decode-encode-direct-1
  (implies (and (atom-listp x)
                (atom sym)
                (natp n))
           (equal (decode (encode-direct-1 x sym n))
                  (app (repeat sym n) x))))

(defthm decode-encode-direct
  (implies (atom-listp x)
           (equal (decode (encode-direct x))
                  x)))

(defthm not-zp-car-repeat
  (implies (not (zp n))
           (equal (car (repeat sym n)) sym)))

(defthm consp-repeat
  (implies (posp n)
           (consp (repeat x n))))

(defthm encode-direct-equal-encode-modified-1
  (implies (and (atom-listp x)
                (atom sym)
                (natp n))
           (equal (encode-direct-1 x sym n)
                  (encode-modified-1 (encode-1 (pack-1 x (repeat sym n)))))))

(defthm encode-direct-equal-encode-modified
  (implies (atom-listp x)
           (equal (encode-direct x)
                  (encode-modified x))))