src/HOL/Inductive.thy
author blanchet
Mon Feb 17 13:31:42 2014 +0100 (2014-02-17)
changeset 55534 b18bdcbda41b
parent 54615 62fb5af93fe2
child 55575 a5e33e18fb5c
permissions -rw-r--r--
renamed old 'primrec' to 'old_primrec' (until the new 'primrec' can be moved above 'Nat' in the theory dependencies)
     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" :: thy_decl and
    11   "inductive_cases" "inductive_simps" :: thy_script and "monos" and
    12   "print_inductives" :: diag and
    13   "rep_datatype" :: thy_goal and
    14   "old_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 (blast intro: weak_coinduct [OF _ coinduct_lemma])
   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 (erule gfp_upperbound [OF coinduct_lemma])
   186   apply assumption
   187   done
   188 
   189 lemma gfp_fun_UnI2: "[| mono(f);  a: gfp(f) |] ==> a: f(X Un gfp(f))"
   190   by (blast dest: gfp_lemma2 mono_Un)
   191 
   192 
   193 subsection {* Even Stronger Coinduction Rule, by Martin Coen *}
   194 
   195 text{* Weakens the condition @{term "X \<subseteq> f(X)"} to one expressed using both
   196   @{term lfp} and @{term gfp}*}
   197 
   198 lemma coinduct3_mono_lemma: "mono(f) ==> mono(%x. f(x) Un X Un B)"
   199 by (iprover intro: subset_refl monoI Un_mono monoD)
   200 
   201 lemma coinduct3_lemma:
   202      "[| X \<subseteq> f(lfp(%x. f(x) Un X Un gfp(f)));  mono(f) |]
   203       ==> lfp(%x. f(x) Un X Un gfp(f)) \<subseteq> f(lfp(%x. f(x) Un X Un gfp(f)))"
   204 apply (rule subset_trans)
   205 apply (erule coinduct3_mono_lemma [THEN lfp_lemma3])
   206 apply (rule Un_least [THEN Un_least])
   207 apply (rule subset_refl, assumption)
   208 apply (rule gfp_unfold [THEN equalityD1, THEN subset_trans], assumption)
   209 apply (rule monoD, assumption)
   210 apply (subst coinduct3_mono_lemma [THEN lfp_unfold], auto)
   211 done
   212 
   213 lemma coinduct3: 
   214   "[| mono(f);  a:X;  X \<subseteq> f(lfp(%x. f(x) Un X Un gfp(f))) |] ==> a : gfp(f)"
   215 apply (rule coinduct3_lemma [THEN [2] weak_coinduct])
   216 apply (rule coinduct3_mono_lemma [THEN lfp_unfold, THEN ssubst])
   217 apply (simp_all)
   218 done
   219 
   220 
   221 text{*Definition forms of @{text gfp_unfold} and @{text coinduct}, 
   222     to control unfolding*}
   223 
   224 lemma def_gfp_unfold: "[| A==gfp(f);  mono(f) |] ==> A = f(A)"
   225   by (auto intro!: gfp_unfold)
   226 
   227 lemma def_coinduct:
   228      "[| A==gfp(f);  mono(f);  X \<le> f(sup X A) |] ==> X \<le> A"
   229   by (iprover intro!: coinduct)
   230 
   231 lemma def_coinduct_set:
   232      "[| A==gfp(f);  mono(f);  a:X;  X \<subseteq> f(X Un A) |] ==> a: A"
   233   by (auto intro!: coinduct_set)
   234 
   235 (*The version used in the induction/coinduction package*)
   236 lemma def_Collect_coinduct:
   237     "[| A == gfp(%w. Collect(P(w)));  mono(%w. Collect(P(w)));   
   238         a: X;  !!z. z: X ==> P (X Un A) z |] ==>  
   239      a : A"
   240   by (erule def_coinduct_set) auto
   241 
   242 lemma def_coinduct3:
   243     "[| A==gfp(f); mono(f);  a:X;  X \<subseteq> f(lfp(%x. f(x) Un X Un A)) |] ==> a: A"
   244   by (auto intro!: coinduct3)
   245 
   246 text{*Monotonicity of @{term gfp}!*}
   247 lemma gfp_mono: "(!!Z. f Z \<le> g Z) ==> gfp f \<le> gfp g"
   248   by (rule gfp_upperbound [THEN gfp_least], blast intro: order_trans)
   249 
   250 
   251 subsection {* Inductive predicates and sets *}
   252 
   253 text {* Package setup. *}
   254 
   255 theorems basic_monos =
   256   subset_refl imp_refl disj_mono conj_mono ex_mono all_mono if_bool_eq_conj
   257   Collect_mono in_mono vimage_mono
   258 
   259 ML_file "Tools/inductive.ML"
   260 setup Inductive.setup
   261 
   262 theorems [mono] =
   263   imp_refl disj_mono conj_mono ex_mono all_mono if_bool_eq_conj
   264   imp_mono not_mono
   265   Ball_def Bex_def
   266   induct_rulify_fallback
   267 
   268 
   269 subsection {* Inductive datatypes and primitive recursion *}
   270 
   271 text {* Package setup. *}
   272 
   273 ML_file "Tools/Datatype/datatype_aux.ML"
   274 ML_file "Tools/Datatype/datatype_prop.ML"
   275 ML_file "Tools/Datatype/datatype_data.ML" setup Datatype_Data.setup
   276 ML_file "Tools/Datatype/rep_datatype.ML"
   277 ML_file "Tools/Datatype/datatype_codegen.ML"
   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 {*
   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 = Case_Translation.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