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 |
|
|
8 |
theory Fundefs
|
|
9 |
imports Main
|
|
10 |
begin
|
|
11 |
|
|
12 |
|
|
13 |
section {* Very basic *}
|
|
14 |
|
|
15 |
consts fib :: "nat \<Rightarrow> nat"
|
|
16 |
function
|
|
17 |
"fib 0 = 1"
|
|
18 |
"fib (Suc 0) = 1"
|
|
19 |
"fib (Suc (Suc n)) = fib n + fib (Suc n)"
|
|
20 |
by pat_completeness (* shows that the patterns are complete *)
|
|
21 |
auto (* shows that the patterns are compatible *)
|
|
22 |
|
|
23 |
(* we get partial simp and induction rules: *)
|
|
24 |
thm fib.psimps
|
|
25 |
thm fib.pinduct
|
|
26 |
|
|
27 |
(* There is also a cases rule to distinguish cases along the definition *)
|
|
28 |
thm fib.cases
|
|
29 |
|
|
30 |
(* Now termination: *)
|
|
31 |
termination fib
|
|
32 |
by (auto_term "less_than")
|
|
33 |
|
|
34 |
thm fib.simps
|
|
35 |
thm fib.induct
|
|
36 |
|
|
37 |
|
|
38 |
section {* Currying *}
|
|
39 |
|
|
40 |
consts add :: "nat \<Rightarrow> nat \<Rightarrow> nat"
|
|
41 |
function
|
|
42 |
"add 0 y = y"
|
|
43 |
"add (Suc x) y = Suc (add x y)"
|
|
44 |
by pat_completeness auto
|
|
45 |
termination
|
|
46 |
by (auto_term "measure fst")
|
|
47 |
|
|
48 |
thm add.induct (* Note the curried induction predicate *)
|
|
49 |
|
|
50 |
|
|
51 |
section {* Nested recursion *}
|
|
52 |
|
|
53 |
|
|
54 |
consts nz :: "nat \<Rightarrow> nat"
|
|
55 |
function
|
|
56 |
"nz 0 = 0"
|
|
57 |
"nz (Suc x) = nz (nz x)"
|
|
58 |
by pat_completeness auto
|
|
59 |
|
|
60 |
lemma nz_is_zero: (* A lemma we need to prove termination *)
|
|
61 |
assumes trm: "x \<in> nz.dom"
|
|
62 |
shows "nz x = 0"
|
|
63 |
using trm
|
|
64 |
by induct auto
|
|
65 |
|
|
66 |
termination nz
|
|
67 |
apply (auto_term "less_than") (* Oops, it left us something to prove *)
|
|
68 |
by (auto simp:nz_is_zero)
|
|
69 |
|
|
70 |
thm nz.simps
|
|
71 |
thm nz.induct
|
|
72 |
|
|
73 |
|
|
74 |
section {* More general patterns *}
|
|
75 |
|
|
76 |
(* Currently, patterns must always be compatible with each other, since
|
|
77 |
no automatich splitting takes place. But the following definition of
|
|
78 |
gcd is ok, although patterns overlap: *)
|
|
79 |
|
|
80 |
|
|
81 |
consts gcd2 :: "nat \<Rightarrow> nat \<Rightarrow> nat"
|
|
82 |
function
|
|
83 |
"gcd2 x 0 = x"
|
|
84 |
"gcd2 0 y = y"
|
|
85 |
"gcd2 (Suc x) (Suc y) = (if x < y then gcd2 (Suc x) (y - x)
|
|
86 |
else gcd2 (x - y) (Suc y))"
|
|
87 |
by pat_completeness auto
|
|
88 |
termination by (auto_term "measure (\<lambda>(x,y). x + y)")
|
|
89 |
|
|
90 |
thm gcd2.simps
|
|
91 |
thm gcd2.induct
|
|
92 |
|
|
93 |
|
|
94 |
(* General patterns allow even strange definitions: *)
|
|
95 |
consts ev :: "nat \<Rightarrow> bool"
|
|
96 |
function
|
|
97 |
"ev (2 * n) = True"
|
|
98 |
"ev (2 * n + 1) = False"
|
|
99 |
proof - (* completeness is more difficult here. . . *)
|
|
100 |
assume c1: "\<And>n. x = 2 * n \<Longrightarrow> P"
|
|
101 |
and c2: "\<And>n. x = 2 * n + 1 \<Longrightarrow> P"
|
|
102 |
have divmod: "x = 2 * (x div 2) + (x mod 2)" by auto
|
|
103 |
show "P"
|
|
104 |
proof cases
|
|
105 |
assume "x mod 2 = 0"
|
|
106 |
with divmod have "x = 2 * (x div 2)" by simp
|
|
107 |
with c1 show "P" .
|
|
108 |
next
|
|
109 |
assume "x mod 2 \<noteq> 0"
|
|
110 |
hence "x mod 2 = 1" by simp
|
|
111 |
with divmod have "x = 2 * (x div 2) + 1" by simp
|
|
112 |
with c2 show "P" .
|
|
113 |
qed
|
|
114 |
qed presburger+ (* solve compatibility with presburger *)
|
|
115 |
termination by (auto_term "{}")
|
|
116 |
|
|
117 |
thm ev.simps
|
|
118 |
thm ev.induct
|
|
119 |
thm ev.cases
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
end |