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