File: utils.lisp

package info (click to toggle)
acl2 8.5dfsg-5
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 991,452 kB
  • sloc: lisp: 15,567,759; javascript: 22,820; cpp: 13,929; ansic: 12,092; perl: 7,150; java: 4,405; xml: 3,884; makefile: 3,507; sh: 3,187; ruby: 2,633; ml: 763; python: 746; yacc: 723; awk: 295; csh: 186; php: 171; lex: 154; tcl: 49; asm: 23; haskell: 17
file content (252 lines) | stat: -rw-r--r-- 6,193 bytes parent folder | download | duplicates (4)
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
;; Copyright (C) 2017, Regents of the University of Texas
;; Written by Cuong Chau
;; License: A 3-clause BSD license.  See the LICENSE file distributed with
;; ACL2.

;; Cuong Chau <ckcuong@cs.utexas.edu>
;; September 2018

(in-package "ADE")

(include-book "std/lists/take" :dir :system)
(include-book "std/util/bstar" :dir :system)

;; ======================================================================

;; BINARY-AND*

(defun binary-and* (x y)
  (declare (xargs :guard t))
  (if x y nil))

(defthm booleanp-binary-and*
  (implies (booleanp y)
           (booleanp (binary-and* x y)))
  :rule-classes :type-prescription)

(in-theory (disable binary-and*))

(defund and*-macro (x)
  (declare (xargs :guard t))
  (cond ((atom x)
         t)
        ((atom (cdr x))
         (car x))
        (t
         `(binary-and* ,(car x)
                       ,(and*-macro (cdr x))))))

(defmacro and* (&rest args)
  (and*-macro args))

;; BINARY-OR*

(defun binary-or* (x y)
  (declare (xargs :guard t))
  (if x x y))

(defthm booleanp-binary-or*
  (implies (and (booleanp x)
                (booleanp y))
           (booleanp (binary-or* x y)))
  :rule-classes :type-prescription)

(in-theory (disable binary-or*))

(defund or*-macro (x)
  (declare (xargs :guard t))
  (cond ((atom x)
         nil)
        ((atom (cdr x))
         (car x))
        (t
         `(binary-or* ,(car x)
                      ,(or*-macro (cdr x))))))

(defmacro or* (&rest args)
  (or*-macro args))

;; NOT*

(defun not* (x)
  (declare (xargs :guard t))
  (not x))

(defthm booleanp-not*
  (booleanp (not* x))
  :rule-classes :type-prescription)

(in-theory (disable not*))

;; ======================================================================

;; Shuffle two lists

(defun insert (x i l)
  (declare (xargs :guard (natp i)))
  (cond
   ((atom l)
    (list x))
   ((zp i)
    (cons x l))
   (t (cons (car l)
            (insert x (1- i) (cdr l))))))

(defthm true-listp-insert
  (implies (true-listp l)
           (true-listp (insert x i l)))
  :rule-classes :type-prescription)

(in-theory (disable insert))

(defun insert-up-to-i (x i l)
  (declare (xargs :guard (natp i)))
  (if (zp i)
      (list (insert x i l))
    (cons (insert x i l)
          (insert-up-to-i x (1- i) l))))

(defthm true-list-listp-insert-up-to-i
  (implies (true-listp l)
           (true-list-listp (insert-up-to-i x i l)))
  :rule-classes :type-prescription)

(in-theory (disable insert-up-to-i))

(defund cons-or-append (x y)
  (declare (xargs :guard t))
  (if (atom x)
      (if (atom y)
          (cons x (list y))
        (cons x y))
    (if (atom y)
        (append (list-fix x) (list y))
      (append (list-fix x) y))))

(defun cons-or-append-at-i (x i l)
  (declare (xargs :guard (integerp i)))
  (cond
   ((atom l)
    (list x))
   ((= i 0)
    (cons (cons-or-append x (car l))
          (cdr l)))
   ((not (natp i)) nil)
   (t (cons (car l)
            (cons-or-append-at-i x (1- i) (cdr l))))))

(defthm true-listp-cons-or-append-at-i
  (implies (true-listp l)
           (true-listp (cons-or-append-at-i x i l)))
  :rule-classes :type-prescription)

(in-theory (disable cons-or-append-at-i))

(defun cons-or-append-up-to-i (x i l)
  (declare (xargs :measure (acl2-count (1+ i))
                  :guard (integerp i)))
  (if (not (natp i))
      nil
    (cons (cons-or-append-at-i x i l)
          (cons-or-append-up-to-i x (1- i) l))))

(defthm true-list-listp-cons-or-append-up-to-i
  (implies (true-listp l)
           (true-list-listp (cons-or-append-up-to-i x i l)))
  :rule-classes :type-prescription)

(in-theory (disable cons-or-append-up-to-i))

(defund index-of-nested (k x)
  (declare (xargs :guard t))
  (cond ((atom x) nil)
        ((equal k (car x)) 0)
        ((atom (car x))
         (b* ((res (index-of-nested k (cdr x))))
           (and res (1+ res))))
        (t (b* ((res1 (index-of-nested k (car x))))
             (if res1
                 0
               (b* ((res (index-of-nested k (cdr x))))
                 (and res (1+ res))))))))

(defun insert-up-to-rec (x y l)
  (declare (xargs :guard (true-list-listp l)))
  (if (atom l)
      nil
    (b* ((i (index-of-nested y (car l)))
         (i (if (natp i) i (len (car l)))))
      (append (append (insert-up-to-i x i (car l))
                      (cons-or-append-up-to-i x (1- i) (car l)))
              (insert-up-to-rec x y (cdr l))))))

(defthm true-list-listp-of-append
  (implies (and (true-list-listp x)
                (true-list-listp y))
           (true-list-listp (append x y)))
  :rule-classes :type-prescription)

(defthm true-list-listp-insert-up-to-rec
  (implies (true-list-listp l)
           (true-list-listp (insert-up-to-rec x y l)))
  :rule-classes :type-prescription)

(in-theory (disable insert-up-to-rec))

(defun shuffle (l1 l2)
  (declare (xargs :guard (and (true-listp l1)
                              (true-listp l2))))
  (if (atom l1)
      (list l2)
    (insert-up-to-rec (car l1) (cadr l1)
                      (shuffle (cdr l1) l2))))

(defthm true-list-listp-shuffle
  (implies (and (true-listp l1)
                (true-listp l2))
           (true-list-listp (shuffle l1 l2)))
  :rule-classes :type-prescription)

(in-theory (disable shuffle))

(defun shuffle-rec1 (x y)
  (declare (xargs :guard (and (true-list-listp x)
                              (true-listp y))))
  (if (atom x)
      nil
    (append (shuffle (car x) y)
            (shuffle-rec1 (cdr x) y))))

(defun shuffle-rec2 (x y)
  (declare (xargs :guard (and (true-listp x)
                              (true-list-listp y))))
  (if (atom y)
      nil
    (append (shuffle x (car y))
            (shuffle-rec2 x (cdr y)))))

;; Compute a powerset

(defund combine (x y)
  (declare (xargs :guard t))
  (list y (cons x y)))

(defund combine-rec (x y)
  (declare (xargs :guard (true-listp y)))
  (if (atom y)
      (list (list x))
    (append (combine x (car y))
            (combine-rec x (cdr y)))))

(defund no-empty-powerset (x)
  (declare (xargs :guard t))
  (if (atom x)
      nil
    (combine-rec (car x)
                 (no-empty-powerset (cdr x)))))

(defund powerset (x)
  (declare (xargs :guard t))
  (cons nil (no-empty-powerset x)))