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