author | haftmann |
Wed, 30 Jan 2008 10:57:44 +0100 | |
changeset 26013 | 8764a1f1253b |
parent 25680 | 909bfa21acc2 |
child 28584 | 58ac551ce1ce |
permissions | -rw-r--r-- |
19568 | 1 |
(* Title: HOL/ex/Fundefs.thy |
2 |
ID: $Id$ |
|
3 |
Author: Alexander Krauss, TU Muenchen |
|
22726 | 4 |
*) |
19568 | 5 |
|
22726 | 6 |
header {* Examples of function definitions *} |
19568 | 7 |
|
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
8 |
theory Fundefs |
20528 | 9 |
imports Main |
19568 | 10 |
begin |
11 |
||
22726 | 12 |
subsection {* Very basic *} |
19568 | 13 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
14 |
fun fib :: "nat \<Rightarrow> nat" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
15 |
where |
19568 | 16 |
"fib 0 = 1" |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
17 |
| "fib (Suc 0) = 1" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
18 |
| "fib (Suc (Suc n)) = fib n + fib (Suc n)" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
19 |
|
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21240
diff
changeset
|
20 |
text {* partial simp and induction rules: *} |
19568 | 21 |
thm fib.psimps |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
22 |
thm fib.pinduct |
19568 | 23 |
|
19736 | 24 |
text {* There is also a cases rule to distinguish cases along the definition *} |
19568 | 25 |
thm fib.cases |
26 |
||
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
27 |
|
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21240
diff
changeset
|
28 |
text {* total simp and induction rules: *} |
19568 | 29 |
thm fib.simps |
30 |
thm fib.induct |
|
31 |
||
22726 | 32 |
subsection {* Currying *} |
19568 | 33 |
|
25170 | 34 |
fun add |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
35 |
where |
19568 | 36 |
"add 0 y = y" |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
37 |
| "add (Suc x) y = Suc (add x y)" |
19568 | 38 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
39 |
thm add.simps |
19736 | 40 |
thm add.induct -- {* Note the curried induction predicate *} |
19568 | 41 |
|
42 |
||
22726 | 43 |
subsection {* Nested recursion *} |
19568 | 44 |
|
25170 | 45 |
function nz |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
46 |
where |
19568 | 47 |
"nz 0 = 0" |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
48 |
| "nz (Suc x) = nz (nz x)" |
21240
8e75fb38522c
Made "termination by lexicographic_order" the default for "fun" definitions.
krauss
parents:
21051
diff
changeset
|
49 |
by pat_completeness auto |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
50 |
|
19736 | 51 |
lemma nz_is_zero: -- {* A lemma we need to prove termination *} |
21051
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
krauss
parents:
20536
diff
changeset
|
52 |
assumes trm: "nz_dom x" |
19568 | 53 |
shows "nz x = 0" |
54 |
using trm |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
55 |
by induct auto |
19568 | 56 |
|
57 |
termination nz |
|
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21240
diff
changeset
|
58 |
by (relation "less_than") (auto simp:nz_is_zero) |
19568 | 59 |
|
60 |
thm nz.simps |
|
61 |
thm nz.induct |
|
62 |
||
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
63 |
text {* Here comes McCarthy's 91-function *} |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
64 |
|
21051
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
krauss
parents:
20536
diff
changeset
|
65 |
|
21240
8e75fb38522c
Made "termination by lexicographic_order" the default for "fun" definitions.
krauss
parents:
21051
diff
changeset
|
66 |
function f91 :: "nat => nat" |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
67 |
where |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
68 |
"f91 n = (if 100 < n then n - 10 else f91 (f91 (n + 11)))" |
21240
8e75fb38522c
Made "termination by lexicographic_order" the default for "fun" definitions.
krauss
parents:
21051
diff
changeset
|
69 |
by pat_completeness auto |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
70 |
|
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
71 |
(* Prove a lemma before attempting a termination proof *) |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
72 |
lemma f91_estimate: |
24585 | 73 |
assumes trm: "f91_dom n" |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
74 |
shows "n < f91 n + 11" |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
75 |
using trm by induct auto |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
76 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
77 |
termination |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
78 |
proof |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
79 |
let ?R = "measure (%x. 101 - x)" |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
80 |
show "wf ?R" .. |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
81 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
82 |
fix n::nat assume "~ 100 < n" (* Inner call *) |
24585 | 83 |
thus "(n + 11, n) : ?R" by simp |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
84 |
|
21051
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
krauss
parents:
20536
diff
changeset
|
85 |
assume inner_trm: "f91_dom (n + 11)" (* Outer call *) |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
86 |
with f91_estimate have "n + 11 < f91 (n + 11) + 11" . |
20270
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
19922
diff
changeset
|
87 |
with `~ 100 < n` show "(f91 (n + 11), n) : ?R" by simp |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
88 |
qed |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
89 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
90 |
|
19568 | 91 |
|
24585 | 92 |
|
22726 | 93 |
subsection {* More general patterns *} |
19568 | 94 |
|
22726 | 95 |
subsubsection {* Overlapping patterns *} |
19782
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
96 |
|
19736 | 97 |
text {* Currently, patterns must always be compatible with each other, since |
20270
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
19922
diff
changeset
|
98 |
no automatic splitting takes place. But the following definition of |
19736 | 99 |
gcd is ok, although patterns overlap: *} |
19568 | 100 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
101 |
fun gcd2 :: "nat \<Rightarrow> nat \<Rightarrow> nat" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
102 |
where |
19568 | 103 |
"gcd2 x 0 = x" |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
104 |
| "gcd2 0 y = y" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
105 |
| "gcd2 (Suc x) (Suc y) = (if x < y then gcd2 (Suc x) (y - x) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
106 |
else gcd2 (x - y) (Suc y))" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
107 |
|
19568 | 108 |
thm gcd2.simps |
109 |
thm gcd2.induct |
|
110 |
||
22726 | 111 |
subsubsection {* Guards *} |
19782
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
112 |
|
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
113 |
text {* We can reformulate the above example using guarded patterns *} |
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
114 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
115 |
function gcd3 :: "nat \<Rightarrow> nat \<Rightarrow> nat" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
116 |
where |
19782
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
117 |
"gcd3 x 0 = x" |
22492 | 118 |
| "gcd3 0 y = y" |
119 |
| "x < y \<Longrightarrow> gcd3 (Suc x) (Suc y) = gcd3 (Suc x) (y - x)" |
|
120 |
| "\<not> x < y \<Longrightarrow> gcd3 (Suc x) (Suc y) = gcd3 (x - y) (Suc y)" |
|
19922 | 121 |
apply (case_tac x, case_tac a, auto) |
122 |
apply (case_tac ba, auto) |
|
19782
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
123 |
done |
21240
8e75fb38522c
Made "termination by lexicographic_order" the default for "fun" definitions.
krauss
parents:
21051
diff
changeset
|
124 |
termination by lexicographic_order |
19782
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
125 |
|
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
126 |
thm gcd3.simps |
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
127 |
thm gcd3.induct |
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
128 |
|
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
129 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
130 |
text {* General patterns allow even strange definitions: *} |
19782
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
131 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
132 |
function ev :: "nat \<Rightarrow> bool" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
133 |
where |
19568 | 134 |
"ev (2 * n) = True" |
22492 | 135 |
| "ev (2 * n + 1) = False" |
19736 | 136 |
proof - -- {* completeness is more difficult here \dots *} |
19922 | 137 |
fix P :: bool |
138 |
and x :: nat |
|
19568 | 139 |
assume c1: "\<And>n. x = 2 * n \<Longrightarrow> P" |
140 |
and c2: "\<And>n. x = 2 * n + 1 \<Longrightarrow> P" |
|
141 |
have divmod: "x = 2 * (x div 2) + (x mod 2)" by auto |
|
142 |
show "P" |
|
19736 | 143 |
proof cases |
19568 | 144 |
assume "x mod 2 = 0" |
145 |
with divmod have "x = 2 * (x div 2)" by simp |
|
146 |
with c1 show "P" . |
|
147 |
next |
|
148 |
assume "x mod 2 \<noteq> 0" |
|
149 |
hence "x mod 2 = 1" by simp |
|
150 |
with divmod have "x = 2 * (x div 2) + 1" by simp |
|
151 |
with c2 show "P" . |
|
152 |
qed |
|
23315 | 153 |
qed presburger+ -- {* solve compatibility with presburger *} |
21240
8e75fb38522c
Made "termination by lexicographic_order" the default for "fun" definitions.
krauss
parents:
21051
diff
changeset
|
154 |
termination by lexicographic_order |
19568 | 155 |
|
156 |
thm ev.simps |
|
157 |
thm ev.induct |
|
158 |
thm ev.cases |
|
159 |
||
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
160 |
|
22726 | 161 |
subsection {* Mutual Recursion *} |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
162 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
163 |
fun evn od :: "nat \<Rightarrow> bool" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
164 |
where |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
165 |
"evn 0 = True" |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
166 |
| "od 0 = False" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
167 |
| "evn (Suc n) = od n" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
168 |
| "od (Suc n) = evn n" |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
169 |
|
21240
8e75fb38522c
Made "termination by lexicographic_order" the default for "fun" definitions.
krauss
parents:
21051
diff
changeset
|
170 |
thm evn.simps |
8e75fb38522c
Made "termination by lexicographic_order" the default for "fun" definitions.
krauss
parents:
21051
diff
changeset
|
171 |
thm od.simps |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
172 |
|
23817 | 173 |
thm evn_od.induct |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
174 |
thm evn_od.termination |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
175 |
|
21240
8e75fb38522c
Made "termination by lexicographic_order" the default for "fun" definitions.
krauss
parents:
21051
diff
changeset
|
176 |
|
22726 | 177 |
subsection {* Definitions in local contexts *} |
22618 | 178 |
|
179 |
locale my_monoid = |
|
180 |
fixes opr :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" |
|
181 |
and un :: "'a" |
|
182 |
assumes assoc: "opr (opr x y) z = opr x (opr y z)" |
|
183 |
and lunit: "opr un x = x" |
|
184 |
and runit: "opr x un = x" |
|
185 |
begin |
|
186 |
||
187 |
fun foldR :: "'a list \<Rightarrow> 'a" |
|
188 |
where |
|
189 |
"foldR [] = un" |
|
190 |
| "foldR (x#xs) = opr x (foldR xs)" |
|
191 |
||
192 |
fun foldL :: "'a list \<Rightarrow> 'a" |
|
193 |
where |
|
194 |
"foldL [] = un" |
|
195 |
| "foldL [x] = x" |
|
196 |
| "foldL (x#y#ys) = foldL (opr x y # ys)" |
|
197 |
||
198 |
thm foldL.simps |
|
199 |
||
200 |
lemma foldR_foldL: "foldR xs = foldL xs" |
|
201 |
by (induct xs rule: foldL.induct) (auto simp:lunit runit assoc) |
|
202 |
||
203 |
thm foldR_foldL |
|
204 |
||
205 |
end |
|
206 |
||
207 |
thm my_monoid.foldL.simps |
|
208 |
thm my_monoid.foldR_foldL |
|
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
209 |
|
22726 | 210 |
subsection {* Regression tests *} |
211 |
||
212 |
text {* The following examples mainly serve as tests for the |
|
213 |
function package *} |
|
214 |
||
215 |
fun listlen :: "'a list \<Rightarrow> nat" |
|
216 |
where |
|
217 |
"listlen [] = 0" |
|
218 |
| "listlen (x#xs) = Suc (listlen xs)" |
|
219 |
||
220 |
(* Context recursion *) |
|
221 |
||
222 |
fun f :: "nat \<Rightarrow> nat" |
|
223 |
where |
|
224 |
zero: "f 0 = 0" |
|
225 |
| succ: "f (Suc n) = (if f n = 0 then 0 else f n)" |
|
226 |
||
227 |
||
228 |
(* A combination of context and nested recursion *) |
|
229 |
function h :: "nat \<Rightarrow> nat" |
|
230 |
where |
|
231 |
"h 0 = 0" |
|
232 |
| "h (Suc n) = (if h n = 0 then h (h n) else h n)" |
|
233 |
by pat_completeness auto |
|
234 |
||
235 |
||
236 |
(* Context, but no recursive call: *) |
|
237 |
fun i :: "nat \<Rightarrow> nat" |
|
238 |
where |
|
239 |
"i 0 = 0" |
|
240 |
| "i (Suc n) = (if n = 0 then 0 else i n)" |
|
241 |
||
242 |
||
243 |
(* Tupled nested recursion *) |
|
244 |
fun fa :: "nat \<Rightarrow> nat \<Rightarrow> nat" |
|
245 |
where |
|
246 |
"fa 0 y = 0" |
|
247 |
| "fa (Suc n) y = (if fa n y = 0 then 0 else fa n y)" |
|
248 |
||
249 |
(* Let *) |
|
250 |
fun j :: "nat \<Rightarrow> nat" |
|
251 |
where |
|
252 |
"j 0 = 0" |
|
253 |
| "j (Suc n) = (let u = n in Suc (j u))" |
|
254 |
||
255 |
||
256 |
(* There were some problems with fresh names\<dots> *) |
|
257 |
(* FIXME: tailrec? *) |
|
258 |
function k :: "nat \<Rightarrow> nat" |
|
259 |
where |
|
260 |
"k x = (let a = x; b = x in k x)" |
|
261 |
by pat_completeness auto |
|
262 |
||
263 |
||
264 |
(* FIXME: tailrec? *) |
|
265 |
function f2 :: "(nat \<times> nat) \<Rightarrow> (nat \<times> nat)" |
|
266 |
where |
|
267 |
"f2 p = (let (x,y) = p in f2 (y,x))" |
|
268 |
by pat_completeness auto |
|
269 |
||
270 |
||
271 |
(* abbreviations *) |
|
272 |
fun f3 :: "'a set \<Rightarrow> bool" |
|
273 |
where |
|
274 |
"f3 x = finite x" |
|
275 |
||
276 |
||
277 |
(* Simple Higher-Order Recursion *) |
|
278 |
datatype 'a tree = |
|
279 |
Leaf 'a |
|
280 |
| Branch "'a tree list" |
|
23117
e2744f32641e
updated examples to include an instance of (lexicographic_order simp:...)
krauss
parents:
22726
diff
changeset
|
281 |
|
25680 | 282 |
lemma lem:"x \<in> set l \<Longrightarrow> size x < Suc (list_size size l)" |
22726 | 283 |
by (induct l, auto) |
284 |
||
23117
e2744f32641e
updated examples to include an instance of (lexicographic_order simp:...)
krauss
parents:
22726
diff
changeset
|
285 |
function treemap :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a tree \<Rightarrow> 'a tree" |
22726 | 286 |
where |
287 |
"treemap fn (Leaf n) = (Leaf (fn n))" |
|
288 |
| "treemap fn (Branch l) = (Branch (map (treemap fn) l))" |
|
23117
e2744f32641e
updated examples to include an instance of (lexicographic_order simp:...)
krauss
parents:
22726
diff
changeset
|
289 |
by pat_completeness auto |
e2744f32641e
updated examples to include an instance of (lexicographic_order simp:...)
krauss
parents:
22726
diff
changeset
|
290 |
termination by (lexicographic_order simp:lem) |
e2744f32641e
updated examples to include an instance of (lexicographic_order simp:...)
krauss
parents:
22726
diff
changeset
|
291 |
|
e2744f32641e
updated examples to include an instance of (lexicographic_order simp:...)
krauss
parents:
22726
diff
changeset
|
292 |
declare lem[simp] |
22726 | 293 |
|
294 |
fun tinc :: "nat tree \<Rightarrow> nat tree" |
|
295 |
where |
|
296 |
"tinc (Leaf n) = Leaf (Suc n)" |
|
297 |
| "tinc (Branch l) = Branch (map tinc l)" |
|
298 |
||
299 |
||
300 |
(* Pattern matching on records *) |
|
301 |
record point = |
|
302 |
Xcoord :: int |
|
303 |
Ycoord :: int |
|
304 |
||
305 |
function swp :: "point \<Rightarrow> point" |
|
306 |
where |
|
307 |
"swp \<lparr> Xcoord = x, Ycoord = y \<rparr> = \<lparr> Xcoord = y, Ycoord = x \<rparr>" |
|
308 |
proof - |
|
309 |
fix P x |
|
310 |
assume "\<And>xa y. x = \<lparr>Xcoord = xa, Ycoord = y\<rparr> \<Longrightarrow> P" |
|
311 |
thus "P" |
|
312 |
by (cases x) |
|
313 |
qed auto |
|
314 |
termination by rule auto |
|
315 |
||
316 |
||
317 |
(* The diagonal function *) |
|
318 |
fun diag :: "bool \<Rightarrow> bool \<Rightarrow> bool \<Rightarrow> nat" |
|
319 |
where |
|
320 |
"diag x True False = 1" |
|
321 |
| "diag False y True = 2" |
|
322 |
| "diag True False z = 3" |
|
323 |
| "diag True True True = 4" |
|
324 |
| "diag False False False = 5" |
|
325 |
||
326 |
||
327 |
(* Many equations (quadratic blowup) *) |
|
328 |
datatype DT = |
|
329 |
A | B | C | D | E | F | G | H | I | J | K | L | M | N | P |
|
330 |
| Q | R | S | T | U | V |
|
331 |
||
332 |
fun big :: "DT \<Rightarrow> nat" |
|
333 |
where |
|
334 |
"big A = 0" |
|
335 |
| "big B = 0" |
|
336 |
| "big C = 0" |
|
337 |
| "big D = 0" |
|
338 |
| "big E = 0" |
|
339 |
| "big F = 0" |
|
340 |
| "big G = 0" |
|
341 |
| "big H = 0" |
|
342 |
| "big I = 0" |
|
343 |
| "big J = 0" |
|
344 |
| "big K = 0" |
|
345 |
| "big L = 0" |
|
346 |
| "big M = 0" |
|
347 |
| "big N = 0" |
|
348 |
| "big P = 0" |
|
349 |
| "big Q = 0" |
|
350 |
| "big R = 0" |
|
351 |
| "big S = 0" |
|
352 |
| "big T = 0" |
|
353 |
| "big U = 0" |
|
354 |
| "big V = 0" |
|
355 |
||
356 |
||
357 |
(* automatic pattern splitting *) |
|
358 |
fun |
|
359 |
f4 :: "nat \<Rightarrow> nat \<Rightarrow> bool" |
|
360 |
where |
|
361 |
"f4 0 0 = True" |
|
25170 | 362 |
| "f4 _ _ = False" |
22726 | 363 |
|
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
364 |
|
19736 | 365 |
end |