summaryrefslogtreecommitdiff
path: root/books/workshops/2000/cowles/books/tarai2.lisp
blob: d5424cf938b650cc6720b117a89373931d47b952 (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
; An ACL2 Tarai Function book.
; Copyright (C) 2000  John R. Cowles, University of Wyoming

; This book is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.

; This book is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.

; You should have received a copy of the GNU General Public License
; along with this book; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

; Written by:
; John Cowles
; Department of Computer Science
; University of Wyoming
; Laramie, WY 82071-3682 U.S.A.

;; The function Fb satisfies the tarai recursion and the
;;  restricted tarai recursion for lists of length 6.

;; (certify-book "C:/acl2/tak/tarai2")

(in-package "ACL2")

(defmacro
    last-el (lst)
    "Return the last element in the list lst."
    (list 'car (list 'last lst)))

(defun
    Gb (lst)
    "Bailey's g function for Knuth's Theorem 4.
     The input lst is intended to be a nonempty
     list of integers."
    (declare (xargs :guard (and (integer-listp lst)
				(consp lst))))
    (cond ((consp (nthcdr 3 lst))    ;; (len lst) > 3
	   (if (or (equal (first lst)
			  (+ (second lst) 1))
		   (> (second lst)
		      (+ (third lst) 1)))
	       (Gb (rest lst))
	       (max (third lst)
		    (last-el lst))))
	  (t (last-el lst))))       ;; (len lst) <= 3
(defun
    decreasing-p (lst)
    "Determine if the elements in lst are strictly decreasing."
    (declare (xargs :guard (rational-listp lst)))
    (if (consp (rest lst))         ;; (len lst) > 1
	(and (> (first lst)(second lst))
	     (decreasing-p (rest lst)))
        t))

(defun
    first-non-decrease (lst)
    "Return the front of lst up to and including the first
     element that does not strictly decrease."
    (declare (xargs :guard (and (rational-listp lst)
				(not (decreasing-p lst)))))
    (if (consp (rest lst))        ;; (len lst) > 1
	(if (<= (first lst)(second lst))
	    (list (first lst)(second lst))
 	    (cons (first lst)
		  (first-non-decrease (rest lst))))
        lst))

(defun
    Fb (lst)
    "Bailey's f function for Knuth's theorem 4.
     The input lst is intended to be a nonempty
     list of integers."
    (declare (xargs :guard (and (integer-listp lst)
				(consp lst))))
    (if (decreasing-p lst)
	(first lst)
        (Gb (first-non-decrease lst))))
(defun
    rotate (lst)
    "Return the result of shifting the first element
     of the list lst onto the tail end of lst."
    (declare (xargs :guard (true-listp lst)))
    (if (consp lst)
	(append (rest lst)(list (first lst)))
        nil))

(defun
    minus-1 (lst)
    "Return the result of replacing (first lst)
     with (first lst) - 1."
    (declare (xargs :guard (and (rational-listp lst)
				(consp lst))))
    (if (consp lst)
	(cons (- (first lst) 1)
	      (rest lst))
        lst))

(defun
    lst-rotates-with-minus-1 (n lst)
    "Return the list of n+1 successive rotates of the
     list lst with the first of each rotate replaced
     by the first minus 1."
    (declare (xargs :guard (and (rational-listp lst)
				(consp lst)
				(integerp n)
				(>= n 0))))
    (if (zp n)
	(list (minus-1 lst))
        (cons (minus-1 lst)
	      (lst-rotates-with-minus-1 (- n 1)
					(rotate lst)))))

(defun
    Fb-lst (lst)
    (if (consp lst)
	(cons (Fb (first lst))
	      (Fb-lst (rest lst)))
        nil))

(defun
    dec-front-len (lst)
    "Return the number of strictly decreasing elements
     at the front of the list lst."
    (declare (xargs :guard (rational-listp lst)))
    (cond ((consp (rest lst))  ;; (len lst) > 1
	   (if (<= (first lst)(second lst))
	       1
	       (+ 1 (dec-front-len (rest lst)))))
	  ((consp lst) 1)      ;; (len lst) = 1
	  (t 0)))              ;; (len lst) = 0
;;----------------------------------------------------------
;; Fb satisfies the tarai recursion equation when lst is a
;;  true list of integers of length 5.

(local
 (defthm
     lst-rotates-with-minus-1-6a
     (let ((lst (list first second third forth fifth sixth)))
       (equal (lst-rotates-with-minus-1 1 lst)
	      (list (list (- first 1) second third forth fifth sixth)
		    (list (- second 1) third forth fifth sixth first))))
     :hints (("Goal"
	      :expand ((lst-rotates-with-minus-1
			1
			(list first second third forth fifth sixth))))))
 )

(local
 (defthm
     lst-rotates-with-minus-1-6b
     (let ((lst (list first second third forth fifth sixth)))
       (equal (lst-rotates-with-minus-1 2 lst)
	      (list (list (- first 1) second third forth fifth sixth)
		    (list (- second 1) third forth fifth sixth first)
		    (list (- third 1) forth fifth sixth first second)))))
 )

(local
 (defthm
     lst-rotates-with-minus-1-6c
     (let ((lst (list first second third forth fifth sixth)))
       (equal (lst-rotates-with-minus-1 3 lst)
	      (list (list (- first 1) second third forth fifth sixth)
		    (list (- second 1) third forth fifth sixth first)
		    (list (- third 1) forth fifth sixth first second)
		    (list (- forth 1) fifth sixth first second third)))))
 )

(local
 (defthm
     lst-rotates-with-minus-1-6d
     (let ((lst (list first second third forth fifth sixth)))
       (equal (lst-rotates-with-minus-1 4 lst)
	      (list (list (- first 1) second third forth fifth sixth)
		    (list (- second 1) third forth fifth sixth first)
		    (list (- third 1) forth fifth sixth first second)
		    (list (- forth 1) fifth sixth first second third)
		    (list (- fifth 1) sixth first second third forth)
		    ))))
 )

(local
 (defthm
     lst-rotates-with-minus-1-6e
     (let ((lst (list first second third forth fifth sixth)))
       (equal
	(lst-rotates-with-minus-1 5 lst)
	(list (list (- first 1) second third forth fifth sixth)
	      (list (- second 1) third forth fifth sixth first)
	      (list (- third 1) forth fifth sixth first second)
	      (list (- forth 1) fifth sixth first second third)
	      (list (- fifth 1) sixth first second third forth)
	      (list (- sixth 1) first second third forth fifth)
	      ))))
 )

(defthm
    ;;Time:  703.87 seconds (prove: 327.38, print: 376.44, other: 0.05)
    Fb-sat-tarai-def-6
    (implies (and (integerp first)
		  (integerp second)
		  (integerp third)
		  (integerp forth)
		  (integerp fifth)
		  (integerp sixth))
	     (let ((lst (list first second third forth fifth sixth)))
	       (equal (Fb lst)
		      (if (<= (first lst)
			      (second lst))
			  (second lst)
			(Fb (Fb-lst (lst-rotates-with-minus-1
				     (- (len lst) 1)
				     lst)))))))
    :rule-classes nil)

(defthm
   ;;Time:  253.60 seconds (prove: 122.80, print: 130.74, other: 0.06)
    Fb-sat-tarai-def-6a
    (implies (and (integerp first)
		  (integerp second)
		  (integerp third)
		  (integerp forth)
		  (integerp fifth)
		  (integerp sixth))
	     (let ((lst (list first second third forth fifth sixth)))
	       (equal (Fb lst)
		      (if (<= (first lst)
			      (second lst))
			  (second lst)
			(Fb (Fb-lst (lst-rotates-with-minus-1
				     (- (dec-front-len lst) 1)
				     lst)))))))
    :rule-classes nil)