src/HOL/Inductive.thy
author wenzelm
Wed Aug 22 22:55:41 2012 +0200 (2012-08-22)
changeset 48891 c0eafbd55de3
parent 48357 828ace4f75ab
child 50302 9149a07a6c67
permissions -rw-r--r--
prefer ML_file over old uses;
wenzelm@7700
     1
(*  Title:      HOL/Inductive.thy
wenzelm@10402
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@11688
     3
*)
wenzelm@10727
     4
haftmann@24915
     5
header {* Knaster-Tarski Fixpoint Theorem and inductive definitions *}
lcp@1187
     6
nipkow@15131
     7
theory Inductive 
haftmann@44860
     8
imports Complete_Lattices
wenzelm@46950
     9
keywords
wenzelm@46950
    10
  "inductive" "coinductive" :: thy_decl and
wenzelm@46950
    11
  "inductive_cases" "inductive_simps" :: thy_script and "monos" and
wenzelm@46950
    12
  "rep_datatype" :: thy_goal and
wenzelm@46950
    13
  "primrec" :: thy_decl
nipkow@15131
    14
begin
wenzelm@10727
    15
haftmann@24915
    16
subsection {* Least and greatest fixed points *}
haftmann@24915
    17
haftmann@26013
    18
context complete_lattice
haftmann@26013
    19
begin
haftmann@26013
    20
haftmann@24915
    21
definition
haftmann@26013
    22
  lfp :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a" where
haftmann@24915
    23
  "lfp f = Inf {u. f u \<le> u}"    --{*least fixed point*}
haftmann@24915
    24
haftmann@24915
    25
definition
haftmann@26013
    26
  gfp :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a" where
haftmann@24915
    27
  "gfp f = Sup {u. u \<le> f u}"    --{*greatest fixed point*}
haftmann@24915
    28
haftmann@24915
    29
haftmann@24915
    30
subsection{* Proof of Knaster-Tarski Theorem using @{term lfp} *}
haftmann@24915
    31
haftmann@24915
    32
text{*@{term "lfp f"} is the least upper bound of 
haftmann@24915
    33
      the set @{term "{u. f(u) \<le> u}"} *}
haftmann@24915
    34
haftmann@24915
    35
lemma lfp_lowerbound: "f A \<le> A ==> lfp f \<le> A"
haftmann@24915
    36
  by (auto simp add: lfp_def intro: Inf_lower)
haftmann@24915
    37
haftmann@24915
    38
lemma lfp_greatest: "(!!u. f u \<le> u ==> A \<le> u) ==> A \<le> lfp f"
haftmann@24915
    39
  by (auto simp add: lfp_def intro: Inf_greatest)
haftmann@24915
    40
haftmann@26013
    41
end
haftmann@26013
    42
haftmann@24915
    43
lemma lfp_lemma2: "mono f ==> f (lfp f) \<le> lfp f"
haftmann@24915
    44
  by (iprover intro: lfp_greatest order_trans monoD lfp_lowerbound)
haftmann@24915
    45
haftmann@24915
    46
lemma lfp_lemma3: "mono f ==> lfp f \<le> f (lfp f)"
haftmann@24915
    47
  by (iprover intro: lfp_lemma2 monoD lfp_lowerbound)
haftmann@24915
    48
haftmann@24915
    49
lemma lfp_unfold: "mono f ==> lfp f = f (lfp f)"
haftmann@24915
    50
  by (iprover intro: order_antisym lfp_lemma2 lfp_lemma3)
haftmann@24915
    51
haftmann@24915
    52
lemma lfp_const: "lfp (\<lambda>x. t) = t"
haftmann@24915
    53
  by (rule lfp_unfold) (simp add:mono_def)
haftmann@24915
    54
haftmann@24915
    55
haftmann@24915
    56
subsection {* General induction rules for least fixed points *}
haftmann@24915
    57
haftmann@24915
    58
theorem lfp_induct:
haftmann@24915
    59
  assumes mono: "mono f" and ind: "f (inf (lfp f) P) <= P"
haftmann@24915
    60
  shows "lfp f <= P"
haftmann@24915
    61
proof -
haftmann@24915
    62
  have "inf (lfp f) P <= lfp f" by (rule inf_le1)
haftmann@24915
    63
  with mono have "f (inf (lfp f) P) <= f (lfp f)" ..
haftmann@24915
    64
  also from mono have "f (lfp f) = lfp f" by (rule lfp_unfold [symmetric])
haftmann@24915
    65
  finally have "f (inf (lfp f) P) <= lfp f" .
haftmann@24915
    66
  from this and ind have "f (inf (lfp f) P) <= inf (lfp f) P" by (rule le_infI)
haftmann@24915
    67
  hence "lfp f <= inf (lfp f) P" by (rule lfp_lowerbound)
haftmann@24915
    68
  also have "inf (lfp f) P <= P" by (rule inf_le2)
haftmann@24915
    69
  finally show ?thesis .
haftmann@24915
    70
qed
haftmann@24915
    71
haftmann@24915
    72
lemma lfp_induct_set:
haftmann@24915
    73
  assumes lfp: "a: lfp(f)"
haftmann@24915
    74
      and mono: "mono(f)"
haftmann@24915
    75
      and indhyp: "!!x. [| x: f(lfp(f) Int {x. P(x)}) |] ==> P(x)"
haftmann@24915
    76
  shows "P(a)"
haftmann@24915
    77
  by (rule lfp_induct [THEN subsetD, THEN CollectD, OF mono _ lfp])
haftmann@32683
    78
    (auto simp: intro: indhyp)
haftmann@24915
    79
haftmann@26013
    80
lemma lfp_ordinal_induct:
haftmann@26013
    81
  fixes f :: "'a\<Colon>complete_lattice \<Rightarrow> 'a"
haftmann@26013
    82
  assumes mono: "mono f"
haftmann@26013
    83
  and P_f: "\<And>S. P S \<Longrightarrow> P (f S)"
haftmann@26013
    84
  and P_Union: "\<And>M. \<forall>S\<in>M. P S \<Longrightarrow> P (Sup M)"
haftmann@26013
    85
  shows "P (lfp f)"
haftmann@26013
    86
proof -
haftmann@26013
    87
  let ?M = "{S. S \<le> lfp f \<and> P S}"
haftmann@26013
    88
  have "P (Sup ?M)" using P_Union by simp
haftmann@26013
    89
  also have "Sup ?M = lfp f"
haftmann@26013
    90
  proof (rule antisym)
haftmann@26013
    91
    show "Sup ?M \<le> lfp f" by (blast intro: Sup_least)
haftmann@26013
    92
    hence "f (Sup ?M) \<le> f (lfp f)" by (rule mono [THEN monoD])
haftmann@26013
    93
    hence "f (Sup ?M) \<le> lfp f" using mono [THEN lfp_unfold] by simp
haftmann@26013
    94
    hence "f (Sup ?M) \<in> ?M" using P_f P_Union by simp
haftmann@26013
    95
    hence "f (Sup ?M) \<le> Sup ?M" by (rule Sup_upper)
haftmann@26013
    96
    thus "lfp f \<le> Sup ?M" by (rule lfp_lowerbound)
haftmann@26013
    97
  qed
haftmann@26013
    98
  finally show ?thesis .
haftmann@26013
    99
qed 
haftmann@26013
   100
haftmann@26013
   101
lemma lfp_ordinal_induct_set: 
haftmann@24915
   102
  assumes mono: "mono f"
haftmann@24915
   103
  and P_f: "!!S. P S ==> P(f S)"
haftmann@24915
   104
  and P_Union: "!!M. !S:M. P S ==> P(Union M)"
haftmann@24915
   105
  shows "P(lfp f)"
wenzelm@46008
   106
  using assms by (rule lfp_ordinal_induct)
haftmann@24915
   107
haftmann@24915
   108
haftmann@24915
   109
text{*Definition forms of @{text lfp_unfold} and @{text lfp_induct}, 
haftmann@24915
   110
    to control unfolding*}
haftmann@24915
   111
haftmann@24915
   112
lemma def_lfp_unfold: "[| h==lfp(f);  mono(f) |] ==> h = f(h)"
wenzelm@45899
   113
  by (auto intro!: lfp_unfold)
haftmann@24915
   114
haftmann@24915
   115
lemma def_lfp_induct: 
haftmann@24915
   116
    "[| A == lfp(f); mono(f);
haftmann@24915
   117
        f (inf A P) \<le> P
haftmann@24915
   118
     |] ==> A \<le> P"
haftmann@24915
   119
  by (blast intro: lfp_induct)
haftmann@24915
   120
haftmann@24915
   121
lemma def_lfp_induct_set: 
haftmann@24915
   122
    "[| A == lfp(f);  mono(f);   a:A;                    
haftmann@24915
   123
        !!x. [| x: f(A Int {x. P(x)}) |] ==> P(x)         
haftmann@24915
   124
     |] ==> P(a)"
haftmann@24915
   125
  by (blast intro: lfp_induct_set)
haftmann@24915
   126
haftmann@24915
   127
(*Monotonicity of lfp!*)
haftmann@24915
   128
lemma lfp_mono: "(!!Z. f Z \<le> g Z) ==> lfp f \<le> lfp g"
haftmann@24915
   129
  by (rule lfp_lowerbound [THEN lfp_greatest], blast intro: order_trans)
haftmann@24915
   130
haftmann@24915
   131
haftmann@24915
   132
subsection {* Proof of Knaster-Tarski Theorem using @{term gfp} *}
haftmann@24915
   133
haftmann@24915
   134
text{*@{term "gfp f"} is the greatest lower bound of 
haftmann@24915
   135
      the set @{term "{u. u \<le> f(u)}"} *}
haftmann@24915
   136
haftmann@24915
   137
lemma gfp_upperbound: "X \<le> f X ==> X \<le> gfp f"
haftmann@24915
   138
  by (auto simp add: gfp_def intro: Sup_upper)
haftmann@24915
   139
haftmann@24915
   140
lemma gfp_least: "(!!u. u \<le> f u ==> u \<le> X) ==> gfp f \<le> X"
haftmann@24915
   141
  by (auto simp add: gfp_def intro: Sup_least)
haftmann@24915
   142
haftmann@24915
   143
lemma gfp_lemma2: "mono f ==> gfp f \<le> f (gfp f)"
haftmann@24915
   144
  by (iprover intro: gfp_least order_trans monoD gfp_upperbound)
haftmann@24915
   145
haftmann@24915
   146
lemma gfp_lemma3: "mono f ==> f (gfp f) \<le> gfp f"
haftmann@24915
   147
  by (iprover intro: gfp_lemma2 monoD gfp_upperbound)
haftmann@24915
   148
haftmann@24915
   149
lemma gfp_unfold: "mono f ==> gfp f = f (gfp f)"
haftmann@24915
   150
  by (iprover intro: order_antisym gfp_lemma2 gfp_lemma3)
haftmann@24915
   151
haftmann@24915
   152
haftmann@24915
   153
subsection {* Coinduction rules for greatest fixed points *}
haftmann@24915
   154
haftmann@24915
   155
text{*weak version*}
haftmann@24915
   156
lemma weak_coinduct: "[| a: X;  X \<subseteq> f(X) |] ==> a : gfp(f)"
wenzelm@45899
   157
  by (rule gfp_upperbound [THEN subsetD]) auto
haftmann@24915
   158
haftmann@24915
   159
lemma weak_coinduct_image: "!!X. [| a : X; g`X \<subseteq> f (g`X) |] ==> g a : gfp f"
wenzelm@45899
   160
  apply (erule gfp_upperbound [THEN subsetD])
wenzelm@45899
   161
  apply (erule imageI)
wenzelm@45899
   162
  done
haftmann@24915
   163
haftmann@24915
   164
lemma coinduct_lemma:
haftmann@24915
   165
     "[| X \<le> f (sup X (gfp f));  mono f |] ==> sup X (gfp f) \<le> f (sup X (gfp f))"
haftmann@24915
   166
  apply (frule gfp_lemma2)
haftmann@24915
   167
  apply (drule mono_sup)
haftmann@24915
   168
  apply (rule le_supI)
haftmann@24915
   169
  apply assumption
haftmann@24915
   170
  apply (rule order_trans)
haftmann@24915
   171
  apply (rule order_trans)
haftmann@24915
   172
  apply assumption
haftmann@24915
   173
  apply (rule sup_ge2)
haftmann@24915
   174
  apply assumption
haftmann@24915
   175
  done
haftmann@24915
   176
haftmann@24915
   177
text{*strong version, thanks to Coen and Frost*}
haftmann@24915
   178
lemma coinduct_set: "[| mono(f);  a: X;  X \<subseteq> f(X Un gfp(f)) |] ==> a : gfp(f)"
wenzelm@45899
   179
  by (blast intro: weak_coinduct [OF _ coinduct_lemma])
haftmann@24915
   180
haftmann@24915
   181
lemma coinduct: "[| mono(f); X \<le> f (sup X (gfp f)) |] ==> X \<le> gfp(f)"
haftmann@24915
   182
  apply (rule order_trans)
haftmann@24915
   183
  apply (rule sup_ge1)
haftmann@24915
   184
  apply (erule gfp_upperbound [OF coinduct_lemma])
haftmann@24915
   185
  apply assumption
haftmann@24915
   186
  done
haftmann@24915
   187
haftmann@24915
   188
lemma gfp_fun_UnI2: "[| mono(f);  a: gfp(f) |] ==> a: f(X Un gfp(f))"
wenzelm@45899
   189
  by (blast dest: gfp_lemma2 mono_Un)
haftmann@24915
   190
haftmann@24915
   191
haftmann@24915
   192
subsection {* Even Stronger Coinduction Rule, by Martin Coen *}
haftmann@24915
   193
haftmann@24915
   194
text{* Weakens the condition @{term "X \<subseteq> f(X)"} to one expressed using both
haftmann@24915
   195
  @{term lfp} and @{term gfp}*}
haftmann@24915
   196
haftmann@24915
   197
lemma coinduct3_mono_lemma: "mono(f) ==> mono(%x. f(x) Un X Un B)"
haftmann@24915
   198
by (iprover intro: subset_refl monoI Un_mono monoD)
haftmann@24915
   199
haftmann@24915
   200
lemma coinduct3_lemma:
haftmann@24915
   201
     "[| X \<subseteq> f(lfp(%x. f(x) Un X Un gfp(f)));  mono(f) |]
haftmann@24915
   202
      ==> lfp(%x. f(x) Un X Un gfp(f)) \<subseteq> f(lfp(%x. f(x) Un X Un gfp(f)))"
haftmann@24915
   203
apply (rule subset_trans)
haftmann@24915
   204
apply (erule coinduct3_mono_lemma [THEN lfp_lemma3])
haftmann@24915
   205
apply (rule Un_least [THEN Un_least])
haftmann@24915
   206
apply (rule subset_refl, assumption)
haftmann@24915
   207
apply (rule gfp_unfold [THEN equalityD1, THEN subset_trans], assumption)
wenzelm@46008
   208
apply (rule monoD, assumption)
haftmann@24915
   209
apply (subst coinduct3_mono_lemma [THEN lfp_unfold], auto)
haftmann@24915
   210
done
haftmann@24915
   211
haftmann@24915
   212
lemma coinduct3: 
haftmann@24915
   213
  "[| mono(f);  a:X;  X \<subseteq> f(lfp(%x. f(x) Un X Un gfp(f))) |] ==> a : gfp(f)"
haftmann@24915
   214
apply (rule coinduct3_lemma [THEN [2] weak_coinduct])
haftmann@41081
   215
apply (rule coinduct3_mono_lemma [THEN lfp_unfold, THEN ssubst])
haftmann@41081
   216
apply (simp_all)
haftmann@24915
   217
done
haftmann@24915
   218
haftmann@24915
   219
haftmann@24915
   220
text{*Definition forms of @{text gfp_unfold} and @{text coinduct}, 
haftmann@24915
   221
    to control unfolding*}
haftmann@24915
   222
haftmann@24915
   223
lemma def_gfp_unfold: "[| A==gfp(f);  mono(f) |] ==> A = f(A)"
wenzelm@45899
   224
  by (auto intro!: gfp_unfold)
haftmann@24915
   225
haftmann@24915
   226
lemma def_coinduct:
haftmann@24915
   227
     "[| A==gfp(f);  mono(f);  X \<le> f(sup X A) |] ==> X \<le> A"
wenzelm@45899
   228
  by (iprover intro!: coinduct)
haftmann@24915
   229
haftmann@24915
   230
lemma def_coinduct_set:
haftmann@24915
   231
     "[| A==gfp(f);  mono(f);  a:X;  X \<subseteq> f(X Un A) |] ==> a: A"
wenzelm@45899
   232
  by (auto intro!: coinduct_set)
haftmann@24915
   233
haftmann@24915
   234
(*The version used in the induction/coinduction package*)
haftmann@24915
   235
lemma def_Collect_coinduct:
haftmann@24915
   236
    "[| A == gfp(%w. Collect(P(w)));  mono(%w. Collect(P(w)));   
haftmann@24915
   237
        a: X;  !!z. z: X ==> P (X Un A) z |] ==>  
haftmann@24915
   238
     a : A"
wenzelm@45899
   239
  by (erule def_coinduct_set) auto
haftmann@24915
   240
haftmann@24915
   241
lemma def_coinduct3:
haftmann@24915
   242
    "[| A==gfp(f); mono(f);  a:X;  X \<subseteq> f(lfp(%x. f(x) Un X Un A)) |] ==> a: A"
wenzelm@45899
   243
  by (auto intro!: coinduct3)
haftmann@24915
   244
haftmann@24915
   245
text{*Monotonicity of @{term gfp}!*}
haftmann@24915
   246
lemma gfp_mono: "(!!Z. f Z \<le> g Z) ==> gfp f \<le> gfp g"
haftmann@24915
   247
  by (rule gfp_upperbound [THEN gfp_least], blast intro: order_trans)
haftmann@24915
   248
haftmann@24915
   249
berghofe@23734
   250
subsection {* Inductive predicates and sets *}
wenzelm@11688
   251
wenzelm@11688
   252
text {* Package setup. *}
wenzelm@10402
   253
berghofe@23734
   254
theorems basic_monos =
haftmann@22218
   255
  subset_refl imp_refl disj_mono conj_mono ex_mono all_mono if_bool_eq_conj
wenzelm@11688
   256
  Collect_mono in_mono vimage_mono
wenzelm@11688
   257
wenzelm@48891
   258
ML_file "Tools/inductive.ML"
haftmann@31723
   259
setup Inductive.setup
berghofe@21018
   260
berghofe@23734
   261
theorems [mono] =
haftmann@22218
   262
  imp_refl disj_mono conj_mono ex_mono all_mono if_bool_eq_conj
berghofe@33934
   263
  imp_mono not_mono
berghofe@21018
   264
  Ball_def Bex_def
berghofe@21018
   265
  induct_rulify_fallback
berghofe@21018
   266
wenzelm@11688
   267
wenzelm@12023
   268
subsection {* Inductive datatypes and primitive recursion *}
wenzelm@11688
   269
wenzelm@11825
   270
text {* Package setup. *}
wenzelm@11825
   271
wenzelm@48891
   272
ML_file "Tools/Datatype/datatype_aux.ML"
wenzelm@48891
   273
ML_file "Tools/Datatype/datatype_prop.ML"
wenzelm@48891
   274
ML_file "Tools/Datatype/datatype_data.ML" setup Datatype_Data.setup
wenzelm@48891
   275
ML_file "Tools/Datatype/datatype_case.ML" setup Datatype_Case.setup
wenzelm@48891
   276
ML_file "Tools/Datatype/rep_datatype.ML"
wenzelm@48891
   277
ML_file "Tools/Datatype/datatype_codegen.ML" setup Datatype_Codegen.setup
wenzelm@48891
   278
ML_file "Tools/Datatype/primrec.ML"
berghofe@12437
   279
nipkow@23526
   280
text{* Lambda-abstractions with pattern matching: *}
nipkow@23526
   281
nipkow@23526
   282
syntax
nipkow@23529
   283
  "_lam_pats_syntax" :: "cases_syn => 'a => 'b"               ("(%_)" 10)
nipkow@23526
   284
syntax (xsymbols)
nipkow@23529
   285
  "_lam_pats_syntax" :: "cases_syn => 'a => 'b"               ("(\<lambda>_)" 10)
nipkow@23526
   286
nipkow@23529
   287
parse_translation (advanced) {*
nipkow@23529
   288
let
nipkow@23529
   289
  fun fun_tr ctxt [cs] =
nipkow@23529
   290
    let
wenzelm@45899
   291
      val x = Syntax.free (fst (Name.variant "x" (Term.declare_term_frees cs Name.context)));
wenzelm@45891
   292
      val ft = Datatype_Case.case_tr true ctxt [x, cs];
nipkow@23529
   293
    in lambda x ft end
wenzelm@35115
   294
in [(@{syntax_const "_lam_pats_syntax"}, fun_tr)] end
nipkow@23526
   295
*}
nipkow@23526
   296
nipkow@23526
   297
end