File: simple.lisp

package info (click to toggle)
acl2 3.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 36,712 kB
  • ctags: 38,396
  • sloc: lisp: 464,023; makefile: 5,470; sh: 86; csh: 47; cpp: 25; ansic: 22
file content (259 lines) | stat: -rwxr-xr-x 7,343 bytes parent folder | download | duplicates (2)
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
#|
This book verifies some simple properties about powerlists.  The examples all
come from Misra's wonderful "Powerlists: a structure for parallel recursion"
paper, published in ACM TOPLAS, Nov 1994.  The results are described in my own
paper "Defthms about zip and tie", UTCS tech report TR97-02.

To certify this book, I do the following:

    (ld "defpkg.lisp")
    (certify-book "simple" 4)
|#

(in-package "POWERLISTS")
(include-book "algebra")
(include-book "../arithmetic/top"
	      :load-compiled-file nil)

(set-verify-guards-eagerness 2)

;;; We begin with the reverse function.  We define this using the tie
;;; constructor, and we show that it is its own inverse.

(defun p-reverse (p)
  (if (powerlist-p p)
      (p-tie (p-reverse (p-untie-r p))
	     (p-reverse (p-untie-l p)))
    p))

(defthm reverse-reverse
  (equal (p-reverse (p-reverse x)) x))

;;; Next, we define reverse using the zip constructor.  We show that this is
;;; the same function as the one defined using tie.  Hence, it is its own
;;; inverse.

(defun p-reverse-zip (p)
  (if (powerlist-p p)
      (p-zip (p-reverse-zip (p-unzip-r p)) (p-reverse-zip (p-unzip-l p)))
    p))

(defthm reverse-zip
  (equal (p-zip (p-reverse x) (p-reverse y))
	 (p-reverse (p-zip y x))))

(defthm reverse-reverse-zip
  (equal (p-reverse-zip x) (p-reverse x))
  :rule-classes nil)

;;; Now, we define functions that rotate a powerlist to the left and right.
;;; Naturally, these functions are inverses of each other.

(defun p-rotate-right (x)
  "Rotate once to the right"
  (if (powerlist-p x)
      (p-zip (p-rotate-right (p-unzip-r x))
	     (p-unzip-l x))
    x))

(defun p-rotate-left (x)
  "Rotate once to the left"
  (if (powerlist-p x)
      (p-zip (p-unzip-r x) 
	     (p-rotate-left (p-unzip-l x)))
    x))

(defthm rotate-left-right
  (equal (p-rotate-left (p-rotate-right x)) x))

(defthm rotate-right-left
  (equal (p-rotate-right (p-rotate-left x)) x))

;;; Here are some amusing identities.  From Misra:

;;; On the amusing identity: rev, rr and rl are the kind of functions that
;;; semi-commute:
;;; 
;;;  rev * rr = rl * rev, where * denotes function composition
;;; 
;;; or, more interestingly, the function rev * rr is its own inverse. You
;;; may look into some of the identities along this line. The amusing
;;; identity follows trivially:
;;;  rev * rr * rev *rr = rev * rr * rl * rev 
;;; = { rr, rl are inverses of each other}
;;;   rev * rev 
;;; = identity

(defthm reverse-rotate
  (equal (p-reverse-zip (p-rotate-right x))
	 (p-rotate-left (p-reverse-zip x))))

(defthm reverse-rotate-reverse-rotate
  (equal (p-reverse-zip
	  (p-rotate-right 
	   (p-reverse-zip 
	    (p-rotate-right x))))
	 x))

;;; Now, we consider shifting a powerlist by more than a single position.
;;; A simple definition follows.

(defun p-rotate-right-k (x k)
  (declare (xargs :guard (and (integerp k) (>= k 0))))
  (if (zp k)
      x
    (p-rotate-right (p-rotate-right-k x (1- k)))))

;;; Before considering a faster definition, we must learn some basic facts
;;; about the naturals.  Perhaps we should include the kaufmann's arithmetic
;;; books instead?

(defun natural-induction (x)
  (declare (xargs :guard (and (integerp x) (>= x 0))))
  "Induct on naturals"
  (if (or (zp x) (equal x 1))
      x
    (natural-induction (1- x))))

(defthm even-odd
  (implies (and (integerp k)
		(>= k 0)
		(not (integerp (* 1/2 k))))
	   (integerp  (+ -1/2 (* 1/2 k))))
  :hints (("Goal" :induct (natural-induction k))))

(defthm even-odd-2
  (implies (and (integerp k)
		(>= k 0)
		(not (integerp (* 1/2 k))))
	   (and (integerp (* (+ -1 K) 1/2))
		(<= 0 (* (+ -1 K) 1/2))))
  :hints (("Goal" :induct (natural-induction k))))


(defthm int-int+1
  (implies (integerp x)
	   (integerp (1+ x))))

(defthm simplify-1-1/2
  (equal (+ 1 -1/2 x) (+ 1/2 x)))

(defthm even-odd-3
  (implies (and (integerp k)
		(>= k 0)
		(not (integerp (* 1/2 k))))
	   (integerp (+ 1/2 (* 1/2 k))))
  :hints (("Goal"
	   :use ((:instance even-odd)
		 (:instance int-int+1 (x (+ -1/2 (* 1/2 k)))))
	   :in-theory (disable even-odd int-int+1))))

(defthm even-odd-4
  (implies (and (integerp k)
		(>= k 0)
		(not (integerp (* 1/2 k))))
	   (and (integerp (+ 1 (* (+ -1 K) 1/2)))
		(<= 0 (+ 1 (* (+ -1 K) 1/2)))))
  :hints (("Goal"
	   :use (:instance even-odd-2)
	   :in-theory (disable even-odd-2))))

;;; Now, we can define a faster rotation function.  Intuitively, this function
;;; shifts a powerlist k times by considering the odd- and even-indexed
;;; elements of the powerlist separately and shifting these by k/2.

(defun p-rotate-right-k-fast (x k)
  (declare (xargs :guard (and (integerp k) (>= k 0))))
  (if (powerlist-p x)
      (if (integerp (/ k 2))
	  (p-zip (p-rotate-right-k-fast (p-unzip-l x)
					(/ k 2))
		 (p-rotate-right-k-fast (p-unzip-r x)
					(/ k 2)))
	(p-zip (p-rotate-right-k-fast (p-unzip-r x)
				      (1+ (/ (1- k) 2)))
	       (p-rotate-right-k-fast (p-unzip-l x)
				      (/ (1- k) 2))))
    x))

;;; We now show that our "fast" rotation returns the same result as our earlier
;;; "slow" rotation.

(defthm rotate-right-k-base
  (implies (and (not (powerlist-p x))
		(integerp k)
		(<= 0 k))
	   (equal (p-rotate-right-k x k) x)))

(defthm rotate-right-k-lemma
  (implies (and (integerp k)
		(>= k 0))
	   (equal (p-rotate-right-k x k)
		  (if (not (powerlist-p x))
		      x
		    (if (integerp (/ k 2))
			  (p-zip (p-rotate-right-k (p-unzip-l x) (/ k 2))
				 (p-rotate-right-k (p-unzip-r x) (/ k 2)))
			(p-rotate-right (p-zip (p-rotate-right-k (p-unzip-l x)
								 (/ (1- k) 2))
					       (p-rotate-right-k (p-unzip-r x)
								 (/ (1- k)
								    2))))))))
  :hints (("Goal" :induct (natural-induction k)))
  :rule-classes nil)

(defthm rotate-right-k
  (implies (and (integerp k)
		(>= k 0))
	   (equal (p-rotate-right-k-fast x k)
		  (p-rotate-right-k x k)))
  :hints (("Goal" :induct (p-rotate-right-k-fast x k))
	  ("Subgoal *1/2" :use (:instance rotate-right-k-lemma))
	  ("Subgoal *1/1" :use (:instance rotate-right-k-lemma))))

;;; We now define the shuffle routines which shift not the elements of a
;;; powerlist, but the index of each element (in binary).

(defun p-right-shuffle (x)
  (if (powerlist-p x)
      (p-tie (p-unzip-l x) (p-unzip-r x))
    x))

(defun p-left-shuffle (x)
  (if (powerlist-p x)
      (p-zip (p-untie-l x) (p-untie-r x))
    x))

(defthm left-right-shuffle
  (equal (p-left-shuffle (p-right-shuffle x)) x))

(defthm right-left-shuffle
  (equal (p-right-shuffle (p-left-shuffle x)) x))

;;; We now define the invert routine which permutes a powerlist by taking
;;; the index of each element, reversing the index (011 -> 110), and placing
;;; the element at that new position index.  This is useful, for example, in
;;; the FFT computation.

(defun p-invert (x)
  (if (powerlist-p x)
      (p-zip (p-invert (p-untie-l x))
	     (p-invert (p-untie-r x)))
    x))

(defthm invert-zip
  (equal (p-invert (p-zip x y))
	 (p-tie (p-invert x) (p-invert y))))

(defthm invert-invert
      (equal (p-invert (p-invert x)) x))

(defthm invert-reverse
      (equal (p-invert (p-reverse x))
             (p-reverse (p-invert x))))

(defthm invert-zip-fn2
      (implies (p-similar-p x y)
               (equal (p-invert (a-zip-fn2 x y))
                      (a-zip-fn2 (p-invert x)
                                 (p-invert y)))))