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