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