2112
|
1 |
fun cread thy s = read_cterm (sign_of thy) (s, (TVar(("DUMMY",0),[])));
|
|
2 |
fun read thy = term_of o cread thy;
|
|
3 |
fun Term s = read WF1.thy s;
|
|
4 |
|
|
5 |
fun Rfunc thy R eqs =
|
|
6 |
let val {induction,rules,theory,tcs} =
|
|
7 |
timeit(fn () => Tfl.Rfunction thy (read thy R) (read thy eqs))
|
|
8 |
in {induction=induction, rules=rules, theory=theory,
|
|
9 |
tcs = map (cterm_of (sign_of theory)) tcs}
|
|
10 |
end;
|
|
11 |
|
|
12 |
val Rfunction = Rfunc WF1.thy;
|
|
13 |
|
|
14 |
fun function tm = timeit (fn () => Tfl.function WF1.thy (Term tm));
|
|
15 |
|
|
16 |
|
|
17 |
(*---------------------------------------------------------------------------
|
|
18 |
* Factorial. Notice how functions without pattern matching are often harder
|
|
19 |
* to deal with than those with! Unfortunately, not all functions can be
|
|
20 |
* described purely by pattern matching, e.g., "variant" as below.
|
|
21 |
*---------------------------------------------------------------------------*)
|
|
22 |
function "fact x = (if (x = 0) then Suc 0 else x * fact (x - Suc 0))";
|
|
23 |
|
|
24 |
Rfunction"pred_nat"
|
|
25 |
"fact x = (if (x = 0) then Suc 0 else x * fact (x - Suc 0))";
|
|
26 |
|
|
27 |
function "(Fact 0 = (Suc 0)) & \
|
|
28 |
\ (Fact (Suc x) = (Fact x * Suc x))";
|
|
29 |
|
|
30 |
Rfunction "pred_nat"
|
|
31 |
"(Fact 0 = (Suc 0)) & \
|
|
32 |
\ (Fact (Suc x) = (Fact x * Suc x))";
|
|
33 |
|
|
34 |
(*---------------------------------------------------------------------------
|
|
35 |
* Fibonacci.
|
|
36 |
*---------------------------------------------------------------------------*)
|
|
37 |
function "(Fib 0 = (Suc 0)) & \
|
|
38 |
\ (Fib (Suc 0) = (Suc 0)) & \
|
|
39 |
\ (Fib (Suc(Suc x)) = (Fib x + Fib (Suc x)))";
|
|
40 |
|
|
41 |
(* "<" doesn't currently work smoothly *)
|
|
42 |
Rfunction"{p::(nat*nat). fst p < snd p}"
|
|
43 |
"(Fib 0 = (Suc 0)) & \
|
|
44 |
\ (Fib (Suc 0) = (Suc 0)) & \
|
|
45 |
\ (Fib (Suc(Suc x)) = (Fib x + Fib (Suc x)))";
|
|
46 |
|
|
47 |
|
|
48 |
(* "trancl pred" means "<" and works better *)
|
|
49 |
Rfunction"trancl pred_nat"
|
|
50 |
"(Fib 0 = (Suc 0)) & \
|
|
51 |
\ (Fib (Suc 0) = (Suc 0)) & \
|
|
52 |
\ (Fib (Suc(Suc x)) = (Fib x + Fib (Suc x)))";
|
|
53 |
|
|
54 |
(*---------------------------------------------------------------------------
|
|
55 |
* Ackermann.
|
|
56 |
*---------------------------------------------------------------------------*)
|
|
57 |
Rfunction"pred_nat ** pred_nat"
|
|
58 |
"(Ack (0,n) = (n + Suc 0)) & \
|
|
59 |
\ (Ack (Suc m,0) = (Ack (m, Suc 0))) & \
|
|
60 |
\ (Ack (Suc m, Suc n) = Ack (m, Ack (Suc m, n)))";
|
|
61 |
|
|
62 |
(*---------------------------------------------------------------------------
|
|
63 |
* Almost primitive recursion.
|
|
64 |
*---------------------------------------------------------------------------*)
|
|
65 |
function"(map2(f, [], L) = []) & \
|
|
66 |
\ (map2(f, h#t, []) = []) & \
|
|
67 |
\ (map2(f, h1#t1, h2#t2) = f h1 h2 # map2 (f, t1, t2))";
|
|
68 |
|
|
69 |
(* Swap arguments *)
|
|
70 |
function"(map2(([],L), f) = []) & \
|
|
71 |
\ (map2((h#t, []), f) = []) & \
|
|
72 |
\ (map2((h1#t1, h2#t2), f) = f h1 h2 # map2((t1,t2),f))";
|
|
73 |
|
|
74 |
Rfunction
|
|
75 |
"measure((length o fst o snd)::('a=>'b=>'c)*'a list*'b list => nat)"
|
|
76 |
"(map2((f::'a=>'b=>'c), ([]::'a list), (L::'b list)) = []) & \
|
|
77 |
\ (map2(f, h#t, []) = []) & \
|
|
78 |
\ (map2(f, h1#t1, h2#t2) = f h1 h2 # map2 (f, t1, t2))";
|
|
79 |
|
|
80 |
(*---------------------------------------------------------------------------
|
|
81 |
* Relation "R" holds stepwise in a list
|
|
82 |
*---------------------------------------------------------------------------*)
|
|
83 |
function"(finiteRchain ((R::'a=>'a=>bool), ([]::'a list)) = True) & \
|
|
84 |
\ (finiteRchain (R, [x]) = True) & \
|
|
85 |
\ (finiteRchain (R, x#y#rst) = (R x y & finiteRchain(R, y#rst)))";
|
|
86 |
|
|
87 |
|
|
88 |
Rfunction"measure ((length o snd)::('a=>'a=>bool) * 'a list => nat)"
|
|
89 |
"(finiteRchain((R::'a=>'a=>bool), ([]::'a list)) = True) & \
|
|
90 |
\ (finiteRchain(R, [x]) = True) & \
|
|
91 |
\ (finiteRchain(R, x#y#rst) = (R x y & finiteRchain(R, y#rst)))";
|
|
92 |
|
|
93 |
(*---------------------------------------------------------------------------
|
|
94 |
* Quicksort.
|
|
95 |
*---------------------------------------------------------------------------*)
|
|
96 |
function"(qsort(ord, []) = []) & \
|
|
97 |
\ (qsort(ord, x#rst) = \
|
|
98 |
\ qsort(ord,filter(not o ord x) rst) \
|
|
99 |
\ @[x]@ \
|
|
100 |
\ qsort(ord,filter(ord x) rst))";
|
|
101 |
|
|
102 |
Rfunction"measure ((length o snd)::('a=>'a=>bool) * 'a list => nat)"
|
|
103 |
"(qsort((ord::'a=>'a=>bool), ([]::'a list)) = []) & \
|
|
104 |
\ (qsort(ord, x#rst) = \
|
|
105 |
\ qsort(ord,filter(not o ord x) rst) \
|
|
106 |
\ @[x]@ \
|
|
107 |
\ qsort(ord,filter(ord x) rst))";
|
|
108 |
|
|
109 |
(*---------------------------------------------------------------------------
|
|
110 |
* Variant.
|
|
111 |
*---------------------------------------------------------------------------*)
|
|
112 |
function"variant(x, L) = (if (x mem L) then variant(Suc x, L) else x)";
|
|
113 |
|
|
114 |
Rfunction
|
|
115 |
"measure(%(p::nat*nat list). length(filter(%y. fst(p) <= y) (snd p)))"
|
|
116 |
"variant(x, L) = (if (x mem L) then variant(Suc x, L) else x)";
|
|
117 |
|
|
118 |
(*---------------------------------------------------------------------------
|
|
119 |
* Euclid's algorithm
|
|
120 |
*---------------------------------------------------------------------------*)
|
|
121 |
function"(gcd ((0::nat),(y::nat)) = y) & \
|
|
122 |
\ (gcd (Suc x, 0) = Suc x) & \
|
|
123 |
\ (gcd (Suc x, Suc y) = \
|
|
124 |
\ (if (y <= x) then gcd(x - y, Suc y) \
|
|
125 |
\ else gcd(Suc x, y - x)))";
|
|
126 |
|
|
127 |
|
|
128 |
(*---------------------------------------------------------------------------
|
|
129 |
* Wrong answer because Isabelle rewriter (going bottom-up) attempts to
|
|
130 |
* apply congruence rule for split to "split" but can't because split is only
|
|
131 |
* partly applied. It then fails, instead of just not doing the rewrite.
|
|
132 |
* Tobias has said he'll fix it.
|
|
133 |
*
|
|
134 |
* ... July 96 ... seems to have been fixed.
|
|
135 |
*---------------------------------------------------------------------------*)
|
|
136 |
|
|
137 |
Rfunction"measure (split (op+) ::nat*nat=>nat)"
|
|
138 |
"(gcd ((0::nat),(y::nat)) = y) & \
|
|
139 |
\ (gcd (Suc x, 0) = Suc x) & \
|
|
140 |
\ (gcd (Suc x, Suc y) = \
|
|
141 |
\ (if (y <= x) then gcd(x - y, Suc y) \
|
|
142 |
\ else gcd(Suc x, y - x)))";
|
|
143 |
|
|
144 |
(*---------------------------------------------------------------------------
|
|
145 |
* A simple nested function.
|
|
146 |
*---------------------------------------------------------------------------*)
|
|
147 |
Rfunction"trancl pred_nat"
|
|
148 |
"(g 0 = 0) & \
|
|
149 |
\ (g(Suc x) = g(g x))";
|
|
150 |
|
|
151 |
(*---------------------------------------------------------------------------
|
|
152 |
* A clever division algorithm. Primitive recursive.
|
|
153 |
*---------------------------------------------------------------------------*)
|
|
154 |
function"(Div(0,x) = (0,0)) & \
|
|
155 |
\ (Div(Suc x, y) = \
|
|
156 |
\ (let (q,r) = Div(x,y) \
|
|
157 |
\ in if (y <= Suc r) then (Suc q,0) else (q, Suc r)))";
|
|
158 |
|
|
159 |
Rfunction"inv_image pred_nat (fst::nat*nat=>nat)"
|
|
160 |
"(Div(0,x) = (0,0)) & \
|
|
161 |
\ (Div(Suc x, y) = \
|
|
162 |
\ (let q = fst(Div(x,y)); \
|
|
163 |
\ r = snd(Div(x,y)) \
|
|
164 |
\ in \
|
|
165 |
\ if (y <= Suc r) then (Suc q,0) else (q, Suc r)))";
|
|
166 |
|
|
167 |
(*---------------------------------------------------------------------------
|
|
168 |
* Testing nested contexts.
|
|
169 |
*---------------------------------------------------------------------------*)
|
|
170 |
function"(f(0,x) = (0,0)) & \
|
|
171 |
\ (f(Suc x, y) = \
|
|
172 |
\ (let z = x \
|
|
173 |
\ in \
|
|
174 |
\ if (0<y) then (0,0) else f(z,y)))";
|
|
175 |
|
|
176 |
|
|
177 |
function"(f(0,x) = (0,0)) & \
|
|
178 |
\ (f(Suc x, y) = \
|
|
179 |
\ (if y = x \
|
|
180 |
\ then (if (0<y) then (0,0) else f(x,y)) \
|
|
181 |
\ else (x,y)))";
|
|
182 |
|
|
183 |
|
|
184 |
(*---------------------------------------------------------------------------
|
|
185 |
* Naming trickery in lets.
|
|
186 |
*---------------------------------------------------------------------------*)
|
|
187 |
|
|
188 |
(* No trick *)
|
|
189 |
function "(test(x, []) = x) & \
|
|
190 |
\ (test(x,h#t) = (let y = Suc x in test(y,t)))";
|
|
191 |
|
|
192 |
(* Trick *)
|
|
193 |
function"(test(x, []) = x) & \
|
|
194 |
\ (test(x,h#t) = \
|
|
195 |
\ (let x = Suc x \
|
|
196 |
\ in \
|
|
197 |
\ test(x,t)))";
|
|
198 |
|
|
199 |
(* Tricky naming, plus nested contexts *)
|
|
200 |
function "vary(x, L) = \
|
|
201 |
\ (if (x mem L) \
|
|
202 |
\ then (let x = Suc x \
|
|
203 |
\ in vary(x,L)) \
|
|
204 |
\ else x)";
|
|
205 |
|
|
206 |
|
|
207 |
(*---------------------------------------------------------------------------
|
|
208 |
* Handling paired lets of various kinds
|
|
209 |
*---------------------------------------------------------------------------*)
|
|
210 |
function
|
|
211 |
"(Fib(0) = Suc 0) & \
|
|
212 |
\ (Fib (Suc 0) = Suc 0) & \
|
|
213 |
\ (Fib (Suc (Suc n)) = \
|
|
214 |
\ (let (x,y) = (Fib (Suc n), Fib n) in x+y))";
|
|
215 |
|
|
216 |
|
|
217 |
function
|
|
218 |
"(qsort((ord::'a=>'a=>bool), ([]::'a list)) = []) & \
|
|
219 |
\ (qsort(ord, x#rst) = \
|
|
220 |
\ (let (L1,L2) = (filter(not o ord x) rst, \
|
|
221 |
\ filter (ord x) rst) \
|
|
222 |
\ in \
|
|
223 |
\ qsort(ord,L1)@[x]@qsort(ord,L2)))";
|
|
224 |
|
|
225 |
function"(qsort((ord::'a=>'a=>bool), ([]::'a list)) = []) & \
|
|
226 |
\ (qsort(ord, x#rst) = \
|
|
227 |
\ (let (L1,L2,P) = (filter(not o ord x) rst, \
|
|
228 |
\ filter (ord x) rst, x) \
|
|
229 |
\ in \
|
|
230 |
\ qsort(ord,L1)@[x]@qsort(ord,L2)))";
|
|
231 |
|
|
232 |
function"(qsort((ord::'a=>'a=>bool), ([]::'a list)) = []) & \
|
|
233 |
\ (qsort(ord, x#rst) = \
|
|
234 |
\ (let (L1,L2) = (filter(not o ord x) rst, \
|
|
235 |
\ filter (ord x) rst); \
|
|
236 |
\ (p,q) = (x,rst) \
|
|
237 |
\ in \
|
|
238 |
\ qsort(ord,L1)@[x]@qsort(ord,L2)))";
|
|
239 |
|
|
240 |
|
|
241 |
(*---------------------------------------------------------------------------
|
|
242 |
* A biggish function
|
|
243 |
*---------------------------------------------------------------------------*)
|
|
244 |
|
|
245 |
function"(acc1(A,[],s,xss,zs,xs) = \
|
|
246 |
\ (if xs=[] then (xss, zs) \
|
|
247 |
\ else acc1(A, zs,s,(xss @ [xs]),[],[]))) & \
|
|
248 |
\ (acc1(A,(y#ys), s, xss, zs, xs) = \
|
|
249 |
\ (let s' = s; \
|
|
250 |
\ zs' = (if fst A s' then [] else zs@[y]); \
|
|
251 |
\ xs' = (if fst A s' then xs@zs@[y] else xs) \
|
|
252 |
\ in \
|
|
253 |
\ acc1(A, ys, s', xss, zs', xs')))";
|
|
254 |
|
|
255 |
|
|
256 |
(*---------------------------------------------------------------------------
|
|
257 |
* Nested, with context.
|
|
258 |
*---------------------------------------------------------------------------*)
|
|
259 |
Rfunction"pred_nat"
|
|
260 |
"(k 0 = 0) & \
|
|
261 |
\ (k (Suc n) = (let x = k (Suc 0) \
|
|
262 |
\ in if (0=Suc 0) then k (Suc(Suc 0)) else n))";
|
|
263 |
|
|
264 |
|
|
265 |
(*---------------------------------------------------------------------------
|
|
266 |
* A function that partitions a list into two around a predicate "P".
|
|
267 |
*---------------------------------------------------------------------------*)
|
|
268 |
val {theory,induction,rules,tcs} =
|
|
269 |
Rfunction
|
|
270 |
"inv_image pred_list \
|
|
271 |
\ ((fst o snd)::('a=>bool)*'a list*'a list*'a list => 'a list)"
|
|
272 |
|
|
273 |
"(part(P::'a=>bool, [], l1,l2) = (l1,l2)) & \
|
|
274 |
\ (part(P, h#rst, l1,l2) = \
|
|
275 |
\ (if P h then part(P,rst, h#l1, l2) \
|
|
276 |
\ else part(P,rst, l1, h#l2)))";
|
|
277 |
|
|
278 |
|
|
279 |
(*---------------------------------------------------------------------------
|
|
280 |
* Another quicksort.
|
|
281 |
*---------------------------------------------------------------------------*)
|
|
282 |
Rfunc theory "measure ((length o snd)::('a=>'a=>bool) * 'a list => nat)"
|
|
283 |
"(fqsort(ord,[]) = []) & \
|
|
284 |
\ (fqsort(ord, x#rst) = \
|
|
285 |
\ (let less = fst(part((%y. ord y x), rst,([],[]))); \
|
|
286 |
\ more = snd(part((%y. ord y x), rst,([],[]))) \
|
|
287 |
\ in \
|
|
288 |
\ fqsort(ord,less)@[x]@fqsort(ord,more)))";
|
|
289 |
|
|
290 |
Rfunc theory "measure ((length o snd)::('a=>'a=>bool) * 'a list => nat)"
|
|
291 |
"(fqsort(ord,[]) = []) & \
|
|
292 |
\ (fqsort(ord, x#rst) = \
|
|
293 |
\ (let (less,more) = part((%y. ord y x), rst,([],[])) \
|
|
294 |
\ in \
|
|
295 |
\ fqsort(ord,less)@[x]@fqsort(ord,more)))";
|
|
296 |
|
|
297 |
|
|
298 |
(* Should fail on repeated variables. *)
|
|
299 |
function"(And(x,[]) = x) & \
|
|
300 |
\ (And(y, y#t) = And(y, t))";
|
|
301 |
|