| author | wenzelm | 
| Mon, 10 Aug 2015 11:20:16 +0200 | |
| changeset 60871 | 9b26f3118e40 | 
| parent 58889 | 5b7a9633cfa8 | 
| child 61343 | 5b5656a63bd6 | 
| permissions | -rw-r--r-- | 
| 19568 | 1 | (* Title: HOL/ex/Fundefs.thy | 
| 2 | Author: Alexander Krauss, TU Muenchen | |
| 22726 | 3 | *) | 
| 19568 | 4 | |
| 58889 | 5 | section {* Examples of function definitions *}
 | 
| 19568 | 6 | |
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 7 | theory Fundefs | 
| 58770 | 8 | imports Main "~~/src/HOL/Library/Monad_Syntax" | 
| 19568 | 9 | begin | 
| 10 | ||
| 22726 | 11 | subsection {* Very basic *}
 | 
| 19568 | 12 | |
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 13 | fun fib :: "nat \<Rightarrow> nat" | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 14 | where | 
| 19568 | 15 | "fib 0 = 1" | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 16 | | "fib (Suc 0) = 1" | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 17 | | "fib (Suc (Suc n)) = fib n + fib (Suc n)" | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 18 | |
| 21319 
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
 krauss parents: 
21240diff
changeset | 19 | text {* partial simp and induction rules: *}
 | 
| 19568 | 20 | thm fib.psimps | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 21 | thm fib.pinduct | 
| 19568 | 22 | |
| 19736 | 23 | text {* There is also a cases rule to distinguish cases along the definition *}
 | 
| 19568 | 24 | thm fib.cases | 
| 25 | ||
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 26 | |
| 21319 
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
 krauss parents: 
21240diff
changeset | 27 | text {* total simp and induction rules: *}
 | 
| 19568 | 28 | thm fib.simps | 
| 29 | thm fib.induct | |
| 30 | ||
| 53611 | 31 | text {* elimination rules *}
 | 
| 32 | thm fib.elims | |
| 33 | ||
| 22726 | 34 | subsection {* Currying *}
 | 
| 19568 | 35 | |
| 25170 | 36 | fun add | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 37 | where | 
| 19568 | 38 | "add 0 y = y" | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 39 | | "add (Suc x) y = Suc (add x y)" | 
| 19568 | 40 | |
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 41 | thm add.simps | 
| 19736 | 42 | thm add.induct -- {* Note the curried induction predicate *}
 | 
| 19568 | 43 | |
| 44 | ||
| 22726 | 45 | subsection {* Nested recursion *}
 | 
| 19568 | 46 | |
| 25170 | 47 | function nz | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 48 | where | 
| 19568 | 49 | "nz 0 = 0" | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 50 | | "nz (Suc x) = nz (nz x)" | 
| 21240 
8e75fb38522c
Made "termination by lexicographic_order" the default for "fun" definitions.
 krauss parents: 
21051diff
changeset | 51 | by pat_completeness auto | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 52 | |
| 19736 | 53 | 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: 
20536diff
changeset | 54 | assumes trm: "nz_dom x" | 
| 19568 | 55 | shows "nz x = 0" | 
| 56 | using trm | |
| 39754 | 57 | by induct (auto simp: nz.psimps) | 
| 19568 | 58 | |
| 59 | termination nz | |
| 21319 
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
 krauss parents: 
21240diff
changeset | 60 | by (relation "less_than") (auto simp:nz_is_zero) | 
| 19568 | 61 | |
| 62 | thm nz.simps | |
| 63 | thm nz.induct | |
| 64 | ||
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 65 | text {* Here comes McCarthy's 91-function *}
 | 
| 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 66 | |
| 21051 
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
 krauss parents: 
20536diff
changeset | 67 | |
| 21240 
8e75fb38522c
Made "termination by lexicographic_order" the default for "fun" definitions.
 krauss parents: 
21051diff
changeset | 68 | function f91 :: "nat => nat" | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 69 | where | 
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 70 | "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: 
21051diff
changeset | 71 | by pat_completeness auto | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 72 | |
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 73 | (* Prove a lemma before attempting a termination proof *) | 
| 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 74 | lemma f91_estimate: | 
| 24585 | 75 | assumes trm: "f91_dom n" | 
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 76 | shows "n < f91 n + 11" | 
| 39754 | 77 | using trm by induct (auto simp: f91.psimps) | 
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 78 | |
| 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 79 | termination | 
| 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 80 | proof | 
| 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 81 | let ?R = "measure (%x. 101 - x)" | 
| 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 82 | show "wf ?R" .. | 
| 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 83 | |
| 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 84 | fix n::nat assume "~ 100 < n" (* Inner call *) | 
| 24585 | 85 | thus "(n + 11, n) : ?R" by simp | 
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 86 | |
| 21051 
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
 krauss parents: 
20536diff
changeset | 87 | assume inner_trm: "f91_dom (n + 11)" (* Outer call *) | 
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 88 | 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: 
19922diff
changeset | 89 | 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: 
19736diff
changeset | 90 | qed | 
| 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 91 | |
| 28584 | 92 | text{* Now trivial (even though it does not belong here): *}
 | 
| 93 | lemma "f91 n = (if 100 < n then n - 10 else 91)" | |
| 94 | by (induct n rule:f91.induct) auto | |
| 19568 | 95 | |
| 24585 | 96 | |
| 22726 | 97 | subsection {* More general patterns *}
 | 
| 19568 | 98 | |
| 22726 | 99 | subsubsection {* Overlapping patterns *}
 | 
| 19782 
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
 krauss parents: 
19770diff
changeset | 100 | |
| 19736 | 101 | 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: 
19922diff
changeset | 102 | no automatic splitting takes place. But the following definition of | 
| 19736 | 103 | gcd is ok, although patterns overlap: *} | 
| 19568 | 104 | |
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 105 | fun gcd2 :: "nat \<Rightarrow> nat \<Rightarrow> nat" | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 106 | where | 
| 19568 | 107 | "gcd2 x 0 = x" | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 108 | | "gcd2 0 y = y" | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 109 | | "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: 
20270diff
changeset | 110 | else gcd2 (x - y) (Suc y))" | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 111 | |
| 19568 | 112 | thm gcd2.simps | 
| 113 | thm gcd2.induct | |
| 114 | ||
| 22726 | 115 | subsubsection {* Guards *}
 | 
| 19782 
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
 krauss parents: 
19770diff
changeset | 116 | |
| 
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
 krauss parents: 
19770diff
changeset | 117 | text {* We can reformulate the above example using guarded patterns *}
 | 
| 
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
 krauss parents: 
19770diff
changeset | 118 | |
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 119 | function gcd3 :: "nat \<Rightarrow> nat \<Rightarrow> nat" | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 120 | where | 
| 19782 
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
 krauss parents: 
19770diff
changeset | 121 | "gcd3 x 0 = x" | 
| 22492 | 122 | | "gcd3 0 y = y" | 
| 123 | | "x < y \<Longrightarrow> gcd3 (Suc x) (Suc y) = gcd3 (Suc x) (y - x)" | |
| 124 | | "\<not> x < y \<Longrightarrow> gcd3 (Suc x) (Suc y) = gcd3 (x - y) (Suc y)" | |
| 19922 | 125 | apply (case_tac x, case_tac a, auto) | 
| 126 | apply (case_tac ba, auto) | |
| 19782 
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
 krauss parents: 
19770diff
changeset | 127 | done | 
| 21240 
8e75fb38522c
Made "termination by lexicographic_order" the default for "fun" definitions.
 krauss parents: 
21051diff
changeset | 128 | termination by lexicographic_order | 
| 19782 
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
 krauss parents: 
19770diff
changeset | 129 | |
| 
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
 krauss parents: 
19770diff
changeset | 130 | thm gcd3.simps | 
| 
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
 krauss parents: 
19770diff
changeset | 131 | thm gcd3.induct | 
| 
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
 krauss parents: 
19770diff
changeset | 132 | |
| 
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
 krauss parents: 
19770diff
changeset | 133 | |
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 134 | text {* General patterns allow even strange definitions: *}
 | 
| 19782 
48c4632e2c28
HOL/Tools/function_package: imporoved handling of guards, added an example
 krauss parents: 
19770diff
changeset | 135 | |
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 136 | function ev :: "nat \<Rightarrow> bool" | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 137 | where | 
| 19568 | 138 | "ev (2 * n) = True" | 
| 22492 | 139 | | "ev (2 * n + 1) = False" | 
| 19736 | 140 | proof -  -- {* completeness is more difficult here \dots *}
 | 
| 19922 | 141 | fix P :: bool | 
| 142 | and x :: nat | |
| 19568 | 143 | assume c1: "\<And>n. x = 2 * n \<Longrightarrow> P" | 
| 144 | and c2: "\<And>n. x = 2 * n + 1 \<Longrightarrow> P" | |
| 145 | have divmod: "x = 2 * (x div 2) + (x mod 2)" by auto | |
| 146 | show "P" | |
| 19736 | 147 | proof cases | 
| 19568 | 148 | assume "x mod 2 = 0" | 
| 149 | with divmod have "x = 2 * (x div 2)" by simp | |
| 150 | with c1 show "P" . | |
| 151 | next | |
| 152 | assume "x mod 2 \<noteq> 0" | |
| 153 | hence "x mod 2 = 1" by simp | |
| 154 | with divmod have "x = 2 * (x div 2) + 1" by simp | |
| 155 | with c2 show "P" . | |
| 156 | qed | |
| 23315 | 157 | qed presburger+ -- {* solve compatibility with presburger *} 
 | 
| 21240 
8e75fb38522c
Made "termination by lexicographic_order" the default for "fun" definitions.
 krauss parents: 
21051diff
changeset | 158 | termination by lexicographic_order | 
| 19568 | 159 | |
| 160 | thm ev.simps | |
| 161 | thm ev.induct | |
| 162 | thm ev.cases | |
| 163 | ||
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 164 | |
| 22726 | 165 | subsection {* Mutual Recursion *}
 | 
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 166 | |
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 167 | fun evn od :: "nat \<Rightarrow> bool" | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 168 | where | 
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 169 | "evn 0 = True" | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 170 | | "od 0 = False" | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 171 | | "evn (Suc n) = od n" | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20270diff
changeset | 172 | | "od (Suc n) = evn n" | 
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 173 | |
| 21240 
8e75fb38522c
Made "termination by lexicographic_order" the default for "fun" definitions.
 krauss parents: 
21051diff
changeset | 174 | thm evn.simps | 
| 
8e75fb38522c
Made "termination by lexicographic_order" the default for "fun" definitions.
 krauss parents: 
21051diff
changeset | 175 | thm od.simps | 
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 176 | |
| 23817 | 177 | thm evn_od.induct | 
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 178 | thm evn_od.termination | 
| 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 179 | |
| 53611 | 180 | thm evn.elims | 
| 181 | thm od.elims | |
| 21240 
8e75fb38522c
Made "termination by lexicographic_order" the default for "fun" definitions.
 krauss parents: 
21051diff
changeset | 182 | |
| 22726 | 183 | subsection {* Definitions in local contexts *}
 | 
| 22618 | 184 | |
| 185 | locale my_monoid = | |
| 186 | fixes opr :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" | |
| 187 | and un :: "'a" | |
| 188 | assumes assoc: "opr (opr x y) z = opr x (opr y z)" | |
| 189 | and lunit: "opr un x = x" | |
| 190 | and runit: "opr x un = x" | |
| 191 | begin | |
| 192 | ||
| 193 | fun foldR :: "'a list \<Rightarrow> 'a" | |
| 194 | where | |
| 195 | "foldR [] = un" | |
| 196 | | "foldR (x#xs) = opr x (foldR xs)" | |
| 197 | ||
| 198 | fun foldL :: "'a list \<Rightarrow> 'a" | |
| 199 | where | |
| 200 | "foldL [] = un" | |
| 201 | | "foldL [x] = x" | |
| 202 | | "foldL (x#y#ys) = foldL (opr x y # ys)" | |
| 203 | ||
| 204 | thm foldL.simps | |
| 205 | ||
| 206 | lemma foldR_foldL: "foldR xs = foldL xs" | |
| 207 | by (induct xs rule: foldL.induct) (auto simp:lunit runit assoc) | |
| 208 | ||
| 209 | thm foldR_foldL | |
| 210 | ||
| 211 | end | |
| 212 | ||
| 213 | thm my_monoid.foldL.simps | |
| 214 | thm my_monoid.foldR_foldL | |
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 215 | |
| 53611 | 216 | subsection {* @{text fun_cases} *}
 | 
| 217 | ||
| 218 | subsubsection {* Predecessor *}
 | |
| 219 | ||
| 220 | fun pred :: "nat \<Rightarrow> nat" where | |
| 221 | "pred 0 = 0" | | |
| 222 | "pred (Suc n) = n" | |
| 223 | ||
| 224 | thm pred.elims | |
| 225 | ||
| 226 | lemma assumes "pred x = y" | |
| 227 | obtains "x = 0" "y = 0" | "n" where "x = Suc n" "y = n" | |
| 228 | by (fact pred.elims[OF assms]) | |
| 229 | ||
| 230 | text {* If the predecessor of a number is 0, that number must be 0 or 1. *}
 | |
| 231 | ||
| 232 | fun_cases pred0E[elim]: "pred n = 0" | |
| 233 | ||
| 234 | lemma "pred n = 0 \<Longrightarrow> n = 0 \<or> n = Suc 0" | |
| 235 | by (erule pred0E) metis+ | |
| 236 | ||
| 237 | ||
| 238 | text {* Other expressions on the right-hand side also work, but whether the
 | |
| 239 | generated rule is useful depends on how well the simplifier can | |
| 240 | simplify it. This example works well: *} | |
| 241 | ||
| 242 | fun_cases pred42E[elim]: "pred n = 42" | |
| 243 | ||
| 244 | lemma "pred n = 42 \<Longrightarrow> n = 43" | |
| 245 | by (erule pred42E) | |
| 246 | ||
| 247 | subsubsection {* List to option *}
 | |
| 248 | ||
| 249 | fun list_to_option :: "'a list \<Rightarrow> 'a option" where | |
| 250 | "list_to_option [x] = Some x" | | |
| 251 | "list_to_option _ = None" | |
| 252 | ||
| 253 | fun_cases list_to_option_NoneE: "list_to_option xs = None" | |
| 53991 
d8f7f3d998de
provide regular ML interface and use plain Syntax.read_prop/Syntax.check_prop (update by Manuel Eberl);
 wenzelm parents: 
53611diff
changeset | 254 | and list_to_option_SomeE: "list_to_option xs = Some x" | 
| 53611 | 255 | |
| 256 | lemma "list_to_option xs = Some y \<Longrightarrow> xs = [y]" | |
| 257 | by (erule list_to_option_SomeE) | |
| 258 | ||
| 259 | subsubsection {* Boolean Functions *}
 | |
| 260 | ||
| 261 | fun xor :: "bool \<Rightarrow> bool \<Rightarrow> bool" where | |
| 262 | "xor False False = False" | | |
| 263 | "xor True True = False" | | |
| 264 | "xor _ _ = True" | |
| 265 | ||
| 266 | thm xor.elims | |
| 267 | ||
| 268 | text {* @{text fun_cases} does not only recognise function equations, but also works with
 | |
| 269 | functions that return a boolean, e.g.: *} | |
| 270 | ||
| 271 | fun_cases xor_TrueE: "xor a b" and xor_FalseE: "\<not>xor a b" | |
| 272 | print_theorems | |
| 273 | ||
| 274 | subsubsection {* Many parameters *}
 | |
| 275 | ||
| 276 | fun sum4 :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat" where | |
| 277 | "sum4 a b c d = a + b + c + d" | |
| 278 | ||
| 279 | fun_cases sum40E: "sum4 a b c d = 0" | |
| 280 | ||
| 281 | lemma "sum4 a b c d = 0 \<Longrightarrow> a = 0" | |
| 282 | by (erule sum40E) | |
| 283 | ||
| 40111 | 284 | |
| 285 | subsection {* Partial Function Definitions *}
 | |
| 286 | ||
| 287 | text {* Partial functions in the option monad: *}
 | |
| 288 | ||
| 289 | partial_function (option) | |
| 290 | collatz :: "nat \<Rightarrow> nat list option" | |
| 291 | where | |
| 292 | "collatz n = | |
| 293 | (if n \<le> 1 then Some [n] | |
| 294 | else if even n | |
| 295 |      then do { ns \<leftarrow> collatz (n div 2); Some (n # ns) }
 | |
| 296 |      else do { ns \<leftarrow> collatz (3 * n + 1);  Some (n # ns)})"
 | |
| 297 | ||
| 40169 
11ea439d947f
declare recursive equation as ".simps", in accordance with other packages
 krauss parents: 
40111diff
changeset | 298 | declare collatz.simps[code] | 
| 40111 | 299 | value "collatz 23" | 
| 300 | ||
| 301 | ||
| 302 | text {* Tail-recursive functions: *}
 | |
| 303 | ||
| 304 | partial_function (tailrec) fixpoint :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"
 | |
| 305 | where | |
| 306 | "fixpoint f x = (if f x = x then x else fixpoint f (f x))" | |
| 307 | ||
| 308 | ||
| 22726 | 309 | subsection {* Regression tests *}
 | 
| 310 | ||
| 311 | text {* The following examples mainly serve as tests for the 
 | |
| 312 | function package *} | |
| 313 | ||
| 314 | fun listlen :: "'a list \<Rightarrow> nat" | |
| 315 | where | |
| 316 | "listlen [] = 0" | |
| 317 | | "listlen (x#xs) = Suc (listlen xs)" | |
| 318 | ||
| 319 | (* Context recursion *) | |
| 320 | ||
| 321 | fun f :: "nat \<Rightarrow> nat" | |
| 322 | where | |
| 323 | zero: "f 0 = 0" | |
| 324 | | succ: "f (Suc n) = (if f n = 0 then 0 else f n)" | |
| 325 | ||
| 326 | ||
| 327 | (* A combination of context and nested recursion *) | |
| 328 | function h :: "nat \<Rightarrow> nat" | |
| 329 | where | |
| 330 | "h 0 = 0" | |
| 331 | | "h (Suc n) = (if h n = 0 then h (h n) else h n)" | |
| 332 | by pat_completeness auto | |
| 333 | ||
| 334 | ||
| 335 | (* Context, but no recursive call: *) | |
| 336 | fun i :: "nat \<Rightarrow> nat" | |
| 337 | where | |
| 338 | "i 0 = 0" | |
| 339 | | "i (Suc n) = (if n = 0 then 0 else i n)" | |
| 340 | ||
| 341 | ||
| 342 | (* Tupled nested recursion *) | |
| 343 | fun fa :: "nat \<Rightarrow> nat \<Rightarrow> nat" | |
| 344 | where | |
| 345 | "fa 0 y = 0" | |
| 346 | | "fa (Suc n) y = (if fa n y = 0 then 0 else fa n y)" | |
| 347 | ||
| 348 | (* Let *) | |
| 349 | fun j :: "nat \<Rightarrow> nat" | |
| 350 | where | |
| 351 | "j 0 = 0" | |
| 352 | | "j (Suc n) = (let u = n in Suc (j u))" | |
| 353 | ||
| 354 | ||
| 355 | (* There were some problems with fresh names\<dots> *) | |
| 356 | function k :: "nat \<Rightarrow> nat" | |
| 357 | where | |
| 358 | "k x = (let a = x; b = x in k x)" | |
| 359 | by pat_completeness auto | |
| 360 | ||
| 361 | ||
| 362 | function f2 :: "(nat \<times> nat) \<Rightarrow> (nat \<times> nat)" | |
| 363 | where | |
| 364 | "f2 p = (let (x,y) = p in f2 (y,x))" | |
| 365 | by pat_completeness auto | |
| 366 | ||
| 367 | ||
| 368 | (* abbreviations *) | |
| 369 | fun f3 :: "'a set \<Rightarrow> bool" | |
| 370 | where | |
| 371 | "f3 x = finite x" | |
| 372 | ||
| 373 | ||
| 374 | (* Simple Higher-Order Recursion *) | |
| 58310 | 375 | datatype 'a tree = | 
| 22726 | 376 | Leaf 'a | 
| 377 | | Branch "'a tree list" | |
| 23117 
e2744f32641e
updated examples to include an instance of (lexicographic_order simp:...)
 krauss parents: 
22726diff
changeset | 378 | |
| 36269 | 379 | fun treemap :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a tree \<Rightarrow> 'a tree"
 | 
| 22726 | 380 | where | 
| 381 | "treemap fn (Leaf n) = (Leaf (fn n))" | |
| 382 | | "treemap fn (Branch l) = (Branch (map (treemap fn) l))" | |
| 383 | ||
| 384 | fun tinc :: "nat tree \<Rightarrow> nat tree" | |
| 385 | where | |
| 386 | "tinc (Leaf n) = Leaf (Suc n)" | |
| 387 | | "tinc (Branch l) = Branch (map tinc l)" | |
| 388 | ||
| 36270 
fd95c0514623
tolerate eta-variants in f_graph.cases (from inductive package); added test case;
 krauss parents: 
36269diff
changeset | 389 | fun testcase :: "'a tree \<Rightarrow> 'a list" | 
| 
fd95c0514623
tolerate eta-variants in f_graph.cases (from inductive package); added test case;
 krauss parents: 
36269diff
changeset | 390 | where | 
| 
fd95c0514623
tolerate eta-variants in f_graph.cases (from inductive package); added test case;
 krauss parents: 
36269diff
changeset | 391 | "testcase (Leaf a) = [a]" | 
| 
fd95c0514623
tolerate eta-variants in f_graph.cases (from inductive package); added test case;
 krauss parents: 
36269diff
changeset | 392 | | "testcase (Branch x) = | 
| 
fd95c0514623
tolerate eta-variants in f_graph.cases (from inductive package); added test case;
 krauss parents: 
36269diff
changeset | 393 | (let xs = concat (map testcase x); | 
| 
fd95c0514623
tolerate eta-variants in f_graph.cases (from inductive package); added test case;
 krauss parents: 
36269diff
changeset | 394 | ys = concat (map testcase x) in | 
| 
fd95c0514623
tolerate eta-variants in f_graph.cases (from inductive package); added test case;
 krauss parents: 
36269diff
changeset | 395 | xs @ ys)" | 
| 
fd95c0514623
tolerate eta-variants in f_graph.cases (from inductive package); added test case;
 krauss parents: 
36269diff
changeset | 396 | |
| 22726 | 397 | |
| 398 | (* Pattern matching on records *) | |
| 399 | record point = | |
| 400 | Xcoord :: int | |
| 401 | Ycoord :: int | |
| 402 | ||
| 403 | function swp :: "point \<Rightarrow> point" | |
| 404 | where | |
| 405 | "swp \<lparr> Xcoord = x, Ycoord = y \<rparr> = \<lparr> Xcoord = y, Ycoord = x \<rparr>" | |
| 406 | proof - | |
| 407 | fix P x | |
| 408 | assume "\<And>xa y. x = \<lparr>Xcoord = xa, Ycoord = y\<rparr> \<Longrightarrow> P" | |
| 409 | thus "P" | |
| 410 | by (cases x) | |
| 411 | qed auto | |
| 412 | termination by rule auto | |
| 413 | ||
| 414 | ||
| 415 | (* The diagonal function *) | |
| 416 | fun diag :: "bool \<Rightarrow> bool \<Rightarrow> bool \<Rightarrow> nat" | |
| 417 | where | |
| 418 | "diag x True False = 1" | |
| 419 | | "diag False y True = 2" | |
| 420 | | "diag True False z = 3" | |
| 421 | | "diag True True True = 4" | |
| 422 | | "diag False False False = 5" | |
| 423 | ||
| 424 | ||
| 425 | (* Many equations (quadratic blowup) *) | |
| 58310 | 426 | datatype DT = | 
| 22726 | 427 | A | B | C | D | E | F | G | H | I | J | K | L | M | N | P | 
| 428 | | Q | R | S | T | U | V | |
| 429 | ||
| 430 | fun big :: "DT \<Rightarrow> nat" | |
| 431 | where | |
| 432 | "big A = 0" | |
| 433 | | "big B = 0" | |
| 434 | | "big C = 0" | |
| 435 | | "big D = 0" | |
| 436 | | "big E = 0" | |
| 437 | | "big F = 0" | |
| 438 | | "big G = 0" | |
| 439 | | "big H = 0" | |
| 440 | | "big I = 0" | |
| 441 | | "big J = 0" | |
| 442 | | "big K = 0" | |
| 443 | | "big L = 0" | |
| 444 | | "big M = 0" | |
| 445 | | "big N = 0" | |
| 446 | | "big P = 0" | |
| 447 | | "big Q = 0" | |
| 448 | | "big R = 0" | |
| 449 | | "big S = 0" | |
| 450 | | "big T = 0" | |
| 451 | | "big U = 0" | |
| 452 | | "big V = 0" | |
| 453 | ||
| 454 | ||
| 455 | (* automatic pattern splitting *) | |
| 456 | fun | |
| 457 | f4 :: "nat \<Rightarrow> nat \<Rightarrow> bool" | |
| 458 | where | |
| 459 | "f4 0 0 = True" | |
| 25170 | 460 | | "f4 _ _ = False" | 
| 22726 | 461 | |
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19736diff
changeset | 462 | |
| 45008 
8b74cfea913a
match types when applying mono_thm -- previous export generalizes type variables;
 krauss parents: 
41817diff
changeset | 463 | (* polymorphic partial_function *) | 
| 
8b74cfea913a
match types when applying mono_thm -- previous export generalizes type variables;
 krauss parents: 
41817diff
changeset | 464 | partial_function (option) f5 :: "'a list \<Rightarrow> 'a option" | 
| 
8b74cfea913a
match types when applying mono_thm -- previous export generalizes type variables;
 krauss parents: 
41817diff
changeset | 465 | where | 
| 
8b74cfea913a
match types when applying mono_thm -- previous export generalizes type variables;
 krauss parents: 
41817diff
changeset | 466 | "f5 x = f5 x" | 
| 
8b74cfea913a
match types when applying mono_thm -- previous export generalizes type variables;
 krauss parents: 
41817diff
changeset | 467 | |
| 19736 | 468 | end |