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