summaryrefslogtreecommitdiff
path: root/huffman-encode.lisp
blob: febf07bf7cb327cc9ba7f29d34ba1ae314d16c00 (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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
;;; Provide Huffman encoding
(in-package "ACL2")

(local (include-book "tools/mv-nth" :dir :system))

;;; Deine huffman tree
(defun nodep (x)
  (declare (xargs :guard t))
  (and (consp x)
       (eq (car x) 'node)))

(defun huffman-treep (x)
  (declare (xargs :guard t))
  (flet ((symbol-list (x)
           (if (nodep x)
               (second x)
               (list (second x)))))
    (if (nodep x)
        (and (true-listp x)
             (equal (len x) 5)
             (true-listp (second x))
             (rationalp (third x))
             (huffman-treep (fourth x))
             (huffman-treep (fifth x))
             (equal (second x)
                    (append (symbol-list (fourth x))
                            (symbol-list (fifth x))))
             (equal (third x)
                    (+ (third (fourth x))
                       (third (fifth x)))))
        (and (true-listp x)
             (equal (len x) 3)
             (equal (first x) 'leaf)
             (rationalp (third x))))))

(defun symbol-list (x)
  (declare (xargs :guard (huffman-treep x)))
  (if (nodep x)
      (second x)
      (list (second x))))

(defun weight (x)
  (declare (xargs :guard (huffman-treep x)))
  (third x))

(defun huffman-leaf (s w)
  (declare (xargs :guard (rationalp w)))
  (list 'leaf s w))

(defun leaf-symbol (x)
  (declare (xargs :guard (and (huffman-treep x)
                              (not (nodep x)))))
  (second x))

(defun huffman-node (l r)
  (declare (xargs :guard (and (huffman-treep l)
                              (huffman-treep r))))
  (list 'node
        (append (symbol-list l)
                (symbol-list r))
        (+ (weight l) (weight r))
        l
        r))

(defun left (x)
  (declare (xargs :guard (and (huffman-treep x)
                              (nodep x))))
  (fourth x))

(defun right (x)
  (declare (xargs :guard (and (huffman-treep x)
                              (nodep x))))
  (fifth x))

(defthm leaf-symbol-weight-elim
  (implies (and (not (nodep x))
                (huffman-treep x))
           (equal (huffman-leaf (leaf-symbol x)
                                (weight x))
                  x))
  :rule-classes :elim)

(defthm len=5
  (implies (true-listp x)
           (equal (equal (len x) 5)
                  (and (consp x)
                       (consp (cdr x))
                       (consp (cddr x))
                       (consp (cdddr x))
                       (consp (cddddr x))
                       (null (cdr (cddddr x)))))))

(defthm left-right-elim
  (implies (and (nodep x)
                (huffman-treep x))
           (equal (huffman-node (left x) (right x))
                  x))
  :hints (("Goal" :use len=5
                  :in-theory (disable len=5)))
  :rule-classes :elim)

(defthm huffman-treep-huffman-node
  (implies (and (huffman-treep l)
                (huffman-treep r))
           (huffman-treep (huffman-node l r))))

(defthm nodep-huffman-node
  (implies (and (huffman-treep l)
                (huffman-treep r))
           (nodep (huffman-node l r))))

(defthm symbol-list-left-right
  (implies (and (nodep x)
                (huffman-treep x))
           (equal (symbol-list x)
                  (append (symbol-list (left x))
                          (symbol-list (right x))))))

(defthm weight-left-right
  (implies (and (nodep x)
                (huffman-treep x))
           (equal (weight x)
                  (+ (weight (left x))
                     (weight (right x))))))

(defthm huffman-treep-left
  (implies (and (nodep x)
                (huffman-treep x))
           (huffman-treep (left x))))

(defthm huffman-treep-right
  (implies (and (nodep x)
                (huffman-treep x))
           (huffman-treep (right x))))

(defthm true-listp-symbol-list
  (implies (huffman-treep x)
           (true-listp (symbol-list x))))

(defthm symbol-list-leaf-symbol
  (implies (and (huffman-treep x)
                (not (nodep x)))
           (equal (symbol-list x)
                  (list (leaf-symbol x)))))

(defthm rationalp-weight
  (implies (huffman-treep x)
           (rationalp (weight x))))

(defthm rationalp-weight/leaf
  (implies (huffman-treep (huffman-leaf s w))
           (rationalp w)))

(defthm huffman-treep-huffman-leaf
  (implies (rationalp w)
           (huffman-treep (huffman-leaf s w))))

(defun node-count (x)
  (declare (xargs :guard (huffman-treep x)))
  (if (nodep x)
      (+ 1
         (node-count (left x))
         (node-count (right x)))
      0))

(defthm node-count-left
  (implies (nodep x)
           (< (node-count (left x))
              (node-count x)))
  :rule-classes :linear)

(defthm node-count-right
  (implies (nodep x)
           (< (node-count (right x))
              (node-count x)))
  :rule-classes :linear)

(in-theory (disable nodep huffman-treep
                    leaf-symbol
                    huffman-leaf huffman-node
                    symbol-list weight left right
                    node-count))

;;; Generate huffman trees
(defun symbol-weight-pair-listp (x)
  (declare (xargs :guard t))
  (if (atom x)
      (null x)
      (and (consp (car x))
           (consp (cdar x))
           (rationalp (cadar x))
           (null (cddar x))
           (symbol-weight-pair-listp (cdr x)))))

(defun huffman-tree-listp (x)
  (declare (xargs :guard t))
  (if (atom x)
      (null x)
      (and (huffman-treep (car x))
           (huffman-tree-listp (cdr x)))))

(defun orderdp (x)
  (declare (xargs :guard (huffman-tree-listp x)))
  (cond ((atom x) t)
        ((<= (weight (car x)) (weight (car x)))
         (orderdp (cdr x)))
        (t nil)))

(defun insert (e x)
  (declare (xargs :guard (and (huffman-treep e)
                              (huffman-tree-listp x))))
  (cond ((endp x) (list e))
        ((<= (weight e) (weight (car x)))
         (cons e x))
        (t
         (cons (car x)
               (insert e (cdr x))))))

(defun pairs-to-leaves (x)
  (declare (xargs :guard (symbol-weight-pair-listp x)))
  (if (endp x)
      nil
      (insert (huffman-leaf (caar x) (cadar x))
              (pairs-to-leaves (cdr x)))))

(defun generate (x a)
  (declare (xargs :measure (len x)
                  :guard (and (huffman-tree-listp x)
                              (orderdp x)
                              (huffman-treep a))))
  (cond ((endp x) a)
        ((<= (weight a) (weight (car x)))
         (generate (cdr x) (huffman-node a (car x))))
        ((endp (cdr x))
         (huffman-node (car x) a))
        ((<= (weight a) (weight (cadr x)))
         (generate (cdr x)
                   (huffman-node (car x) a)))
        (t
         (generate (insert a (cddr x))
                   (huffman-node (car x) (cadr x))))))

(defun generate-huffman-tree (x)
  (declare (xargs :guard (and (symbol-weight-pair-listp x)
                              (< 1 (len x)))
                  :verify-guards nil))
  (let ((leaves (pairs-to-leaves x)))
    (generate (cdr leaves) (car leaves))))

(defthm huffman-tree-listp-insert
  (implies (and (huffman-tree-listp x)
                (huffman-treep e))
           (huffman-tree-listp (insert e x))))

(defthm orderdp-insert
  (implies (orderdp x)
           (orderdp (insert e x))))

(defthm huffman-tree-listp-pairs-to-leaves
  (implies (symbol-weight-pair-listp x)
           (huffman-tree-listp (pairs-to-leaves x))))

(defthm orderdp-pairs-to-leaves
  (orderdp (pairs-to-leaves x)))

(defthm huffman-treep-generate
  (implies (and (huffman-treep a)
                (huffman-tree-listp x)
                (consp x))
           (huffman-treep (generate x a))))

(defthm nodep-generate
  (implies (and (huffman-treep a)
                (huffman-tree-listp x)
                (consp x))
           (nodep (generate x a))))

(defthm huffman-tree-listp-cdr
  (implies (huffman-tree-listp x)
           (huffman-tree-listp (cdr x))))

(defthm huffman-treep-car
  (implies (and (consp x) (huffman-tree-listp x))
           (huffman-treep (car x))))

(defthm len>1-consp
  (equal (< 1 (len x))
         (and (consp x)
              (consp (cdr x)))))

(defthm nodep-generate-huffman-tree
  (implies (and (symbol-weight-pair-listp x)
                (< 1 (len x)))
           (nodep (generate-huffman-tree x))))

(defthm huffman-treep-generate-huffman-tree
  (implies (and (symbol-weight-pair-listp x)
                (< 1 (len x)))
           (huffman-treep (generate-huffman-tree x))))

(verify-guards generate-huffman-tree)

(defun count-and-remove (e x)
  (declare (xargs :guard (true-listp x)))
  (cond ((endp x) (mv 0 nil))
        ((equal e (car x))
         (mv-let (c rest)
             (count-and-remove e (cdr x))
           (mv (+ 1 c) rest)))
        (t
         (mv-let (c rest)
             (count-and-remove e (cdr x))
           (mv c (cons (car x) rest))))))


(defun message-to-pairs (x)
  (declare (xargs :guard (true-listp x)))
  (if (endp x)
      nil
      (mv-let (c rest)
          (count-and-remove (car x) x)
        (cons (list (car x) c)
              (message-to-pairs rest)))))

(defthm rationalp-0-count-and-remove
  (rationalp (mv-nth 0 (count-and-remove e x))))

(defthm symbol-weight-pair-listp-message-to-pairs
  (symbol-weight-pair-listp (message-to-pairs x)))

;;; Huffman encodeing and decoding.
(defun encode-1 (x tree)
  (declare (xargs :guard (huffman-treep tree)
                  :measure (node-count tree)))
  (if (nodep tree)
      (cond ((member-equal x (symbol-list (left tree)))
             (cons 0 (encode-1 x (left tree))))
            (t
             (cons 1 (encode-1 x (right tree)))))
      nil))

(defun encode (x tree)
  (declare (xargs :guard (and (true-listp x)
                              (nodep tree)
                              (huffman-treep tree))))
  (if (endp x)
      nil
      (append (encode-1 (car x) tree)
              (encode (cdr x) tree))))

(defun bit-listp (x)
  (declare (xargs :guard t))
  (if (atom x)
      (null x)
      (and (bitp (car x))
           (bit-listp (cdr x)))))

(defun decode-1 (x tree)
  (declare (xargs :guard (and (bit-listp x)
                              (huffman-treep tree))
                  :measure (node-count tree)))
  (cond ((and (consp x) (nodep tree))
         (if (equal (car x) 0)
             (decode-1 (cdr x) (left tree))
             (decode-1 (cdr x) (right tree))))
        ((nodep tree) (mv nil nil))
        (t
         (mv (leaf-symbol tree) x))))

(defun decode (x tree)
  (declare (xargs :measure (len x)
                  :guard (and (bit-listp x)
                              (nodep tree)
                              (huffman-treep tree))))
  (if (or (endp x)
          (not (nodep tree)))
      nil
      (mv-let (symbol rest)
          (decode-1 x tree)
        (cons symbol (decode rest tree)))))

(defthm bit-listp-encode-1
  (bit-listp (encode-1 x tree)))

(defthm bit-listp-encode
  (bit-listp (encode x tree)))

(defthm consp-append
  (equal (consp (append x y))
         (or (consp x)
             (consp y))))

(defthm consp-encode-1
  (implies (and (huffman-treep tree)
                (nodep tree))
           (consp (encode-1 x tree))))

(defthm consp-encode
  (implies (and (consp x)
                (true-listp x)
                (huffman-treep tree)
                (nodep tree))
           (consp (encode x tree))))


(defthm len-decode-1
  (implies (and (consp x)
                (nodep tree))
           (< (len (mv-nth 1 (decode-1 x tree)))
              (len x))))

(defthm member-append
  (iff (member-equal e (append x y))
       (or (member-equal e x)
           (member-equal e y))))

(defthm decode-1-encode-1
  (implies (and (huffman-treep tree)
                (member-equal x (symbol-list tree)))
           (and (equal (mv-nth 0 (decode-1 (encode-1 x tree) tree))
                       x)
                (equal (mv-nth 1 (decode-1 (encode-1 x tree) tree))
                       nil))))

(defthm decode1-append-encode-1
  (implies (and (huffman-treep tree)
                (member-equal x (symbol-list tree)))
           (and (equal (mv-nth 0 (decode-1 (append (encode-1 x tree)
                                                   y)
                                           tree))
                       x)
                (equal (mv-nth 1 (decode-1 (append (encode-1 x tree)
                                                   y)
                                           tree))
                       y))))

(defthm decode-encode-to-decode1-encode1
  (implies (and (huffman-treep tree)
                (nodep tree)
                (member-equal x (symbol-list tree))
                (subsetp y (symbol-list tree)))
           (equal (decode (append (encode-1 x tree)
                                  (encode y tree))
                          tree)
                  (cons (mv-nth 0 (decode-1 (encode-1 x tree) tree))
                        (decode (encode y tree)
                                tree))))
  :hints (("Goal" :induct (decode x tree))))

(defthm decode-encode
  (implies (and (true-listp x)
                (huffman-treep tree)
                (nodep tree)
                (subsetp x (symbol-list tree)))
           (equal (decode (encode x tree)
                          tree)
                  x)))