author | krauss |
Thu, 14 Sep 2006 15:27:08 +0200 | |
changeset 20536 | f088edff8af8 |
parent 20528 | 4ade644022dd |
child 21051 | c49467a9c1e1 |
permissions | -rw-r--r-- |
19568 | 1 |
(* Title: HOL/ex/Fundefs.thy |
2 |
ID: $Id$ |
|
3 |
Author: Alexander Krauss, TU Muenchen |
|
4 |
||
5 |
Examples of function definitions using the new "function" package. |
|
6 |
*) |
|
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 |
||
12 |
section {* Very basic *} |
|
13 |
||
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
14 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
15 |
fun fib :: "nat \<Rightarrow> nat" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
16 |
where |
19568 | 17 |
"fib 0 = 1" |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
18 |
| "fib (Suc 0) = 1" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
19 |
| "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
|
20 |
|
19568 | 21 |
|
19736 | 22 |
text {* we get partial simp and induction rules: *} |
19568 | 23 |
thm fib.psimps |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
24 |
thm fib.pinduct |
19568 | 25 |
|
19736 | 26 |
text {* There is also a cases rule to distinguish cases along the definition *} |
19568 | 27 |
thm fib.cases |
28 |
||
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
29 |
thm fib.domintros |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
30 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
31 |
|
19736 | 32 |
text {* Now termination: *} |
19568 | 33 |
termination fib |
19736 | 34 |
by (auto_term "less_than") |
19568 | 35 |
|
36 |
thm fib.simps |
|
37 |
thm fib.induct |
|
38 |
||
39 |
||
40 |
section {* Currying *} |
|
41 |
||
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
42 |
fun add :: "nat \<Rightarrow> nat \<Rightarrow> nat" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
43 |
where |
19568 | 44 |
"add 0 y = y" |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
45 |
| "add (Suc x) y = Suc (add x y)" |
19736 | 46 |
termination |
19568 | 47 |
by (auto_term "measure fst") |
48 |
||
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
49 |
thm add.simps |
19736 | 50 |
thm add.induct -- {* Note the curried induction predicate *} |
19568 | 51 |
|
52 |
||
53 |
section {* Nested recursion *} |
|
54 |
||
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
55 |
fun nz :: "nat \<Rightarrow> nat" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
56 |
where |
19568 | 57 |
"nz 0 = 0" |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
58 |
| "nz (Suc x) = nz (nz x)" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
59 |
|
19736 | 60 |
lemma nz_is_zero: -- {* A lemma we need to prove termination *} |
19583
c5fa77b03442
function-package: Changed record usage to make sml/nj happy...
krauss
parents:
19568
diff
changeset
|
61 |
assumes trm: "x \<in> nz_dom" |
19568 | 62 |
shows "nz x = 0" |
63 |
using trm |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
64 |
by induct auto |
19568 | 65 |
|
66 |
termination nz |
|
19736 | 67 |
apply (auto_term "less_than") -- {* Oops, it left us something to prove *} |
19568 | 68 |
by (auto simp:nz_is_zero) |
69 |
||
70 |
thm nz.simps |
|
71 |
thm nz.induct |
|
72 |
||
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
73 |
text {* Here comes McCarthy's 91-function *} |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
74 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
75 |
fun f91 :: "nat => nat" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
76 |
where |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
77 |
"f91 n = (if 100 < n then n - 10 else f91 (f91 (n + 11)))" |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
78 |
|
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
79 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
80 |
(* Prove a lemma before attempting a termination proof *) |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
81 |
lemma f91_estimate: |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
82 |
assumes trm: "n : f91_dom" |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
83 |
shows "n < f91 n + 11" |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
84 |
using trm by induct auto |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
85 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
86 |
termination |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
87 |
proof |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
88 |
let ?R = "measure (%x. 101 - x)" |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
89 |
show "wf ?R" .. |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
90 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
91 |
fix n::nat assume "~ 100 < n" (* Inner call *) |
20270
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
19922
diff
changeset
|
92 |
thus "(n + 11, n) : ?R" by simp |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
93 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
94 |
assume inner_trm: "n + 11 : f91_dom" (* Outer call *) |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
95 |
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
|
96 |
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
|
97 |
qed |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
98 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
99 |
|
19568 | 100 |
|
101 |
section {* More general patterns *} |
|
102 |
||
19782
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
103 |
subsection {* Overlapping patterns *} |
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
104 |
|
19736 | 105 |
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
|
106 |
no automatic splitting takes place. But the following definition of |
19736 | 107 |
gcd is ok, although patterns overlap: *} |
19568 | 108 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
109 |
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
|
110 |
where |
19568 | 111 |
"gcd2 x 0 = x" |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
112 |
| "gcd2 0 y = y" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
113 |
| "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
|
114 |
else gcd2 (x - y) (Suc y))" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
115 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
116 |
|
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
117 |
termination |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
118 |
by (auto_term "(measure (%x. x)) <*lex*> (measure (%x. x))") |
19568 | 119 |
|
120 |
thm gcd2.simps |
|
121 |
thm gcd2.induct |
|
122 |
||
19782
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
123 |
subsection {* Guards *} |
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
124 |
|
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
125 |
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
|
126 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
127 |
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
|
128 |
where |
19782
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
129 |
"gcd3 x 0 = x" |
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
130 |
"gcd3 0 y = y" |
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
131 |
"x < y \<Longrightarrow> gcd3 (Suc x) (Suc y) = gcd3 (Suc x) (y - x)" |
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
132 |
"\<not> x < y \<Longrightarrow> gcd3 (Suc x) (Suc y) = gcd3 (x - y) (Suc y)" |
19922 | 133 |
apply (case_tac x, case_tac a, auto) |
134 |
apply (case_tac ba, auto) |
|
19782
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
135 |
done |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
136 |
|
19782
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
137 |
termination |
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
138 |
by (auto_term "measure (\<lambda>(x,y). x + y)") |
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
139 |
|
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
140 |
thm gcd3.simps |
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
141 |
thm gcd3.induct |
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
142 |
|
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
krauss
parents:
19770
diff
changeset
|
143 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
144 |
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
|
145 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
146 |
function ev :: "nat \<Rightarrow> bool" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
147 |
where |
19568 | 148 |
"ev (2 * n) = True" |
149 |
"ev (2 * n + 1) = False" |
|
19736 | 150 |
proof - -- {* completeness is more difficult here \dots *} |
19922 | 151 |
fix P :: bool |
152 |
and x :: nat |
|
19568 | 153 |
assume c1: "\<And>n. x = 2 * n \<Longrightarrow> P" |
154 |
and c2: "\<And>n. x = 2 * n + 1 \<Longrightarrow> P" |
|
155 |
have divmod: "x = 2 * (x div 2) + (x mod 2)" by auto |
|
156 |
show "P" |
|
19736 | 157 |
proof cases |
19568 | 158 |
assume "x mod 2 = 0" |
159 |
with divmod have "x = 2 * (x div 2)" by simp |
|
160 |
with c1 show "P" . |
|
161 |
next |
|
162 |
assume "x mod 2 \<noteq> 0" |
|
163 |
hence "x mod 2 = 1" by simp |
|
164 |
with divmod have "x = 2 * (x div 2) + 1" by simp |
|
165 |
with c2 show "P" . |
|
166 |
qed |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
167 |
qed presburger+ -- {* solve compatibility with presburger *} |
19568 | 168 |
termination by (auto_term "{}") |
169 |
||
170 |
thm ev.simps |
|
171 |
thm ev.induct |
|
172 |
thm ev.cases |
|
173 |
||
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
174 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
175 |
section {* Mutual Recursion *} |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
176 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
177 |
fun evn od :: "nat \<Rightarrow> bool" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
178 |
where |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
179 |
"evn 0 = True" |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
180 |
| "od 0 = False" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
181 |
| "evn (Suc n) = od n" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20270
diff
changeset
|
182 |
| "od (Suc n) = evn n" |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
183 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
184 |
thm evn.psimps |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
185 |
thm od.psimps |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
186 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
187 |
thm evn_od.pinduct |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
188 |
thm evn_od.termination |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
189 |
thm evn_od.domintros |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
190 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
191 |
termination |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
192 |
by (auto_term "measure (sum_case (%n. n) (%n. n))") |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
193 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
194 |
thm evn.simps |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
195 |
thm od.simps |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
196 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19736
diff
changeset
|
197 |
|
19736 | 198 |
end |