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