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