src/HOL/BNF_Least_Fixpoint.thy
author traytel
Thu Apr 07 17:56:22 2016 +0200 (2016-04-07)
changeset 62905 52c5a25e0c96
parent 60758 d8d85a8172b5
child 62906 75ca185db27f
permissions -rw-r--r--
derive (co)rec uniformly from (un)fold
     1 (*  Title:      HOL/BNF_Least_Fixpoint.thy
     2     Author:     Dmitriy Traytel, TU Muenchen
     3     Author:     Lorenz Panny, TU Muenchen
     4     Author:     Jasmin Blanchette, TU Muenchen
     5     Copyright   2012, 2013, 2014
     6 
     7 Least fixpoint (datatype) operation on bounded natural functors.
     8 *)
     9 
    10 section \<open>Least Fixpoint (Datatype) Operation on Bounded Natural Functors\<close>
    11 
    12 theory BNF_Least_Fixpoint
    13 imports BNF_Fixpoint_Base
    14 keywords
    15   "datatype" :: thy_decl and
    16   "datatype_compat" :: thy_decl
    17 begin
    18 
    19 lemma subset_emptyI: "(\<And>x. x \<in> A \<Longrightarrow> False) \<Longrightarrow> A \<subseteq> {}"
    20   by blast
    21 
    22 lemma image_Collect_subsetI: "(\<And>x. P x \<Longrightarrow> f x \<in> B) \<Longrightarrow> f ` {x. P x} \<subseteq> B"
    23   by blast
    24 
    25 lemma Collect_restrict: "{x. x \<in> X \<and> P x} \<subseteq> X"
    26   by auto
    27 
    28 lemma prop_restrict: "\<lbrakk>x \<in> Z; Z \<subseteq> {x. x \<in> X \<and> P x}\<rbrakk> \<Longrightarrow> P x"
    29   by auto
    30 
    31 lemma underS_I: "\<lbrakk>i \<noteq> j; (i, j) \<in> R\<rbrakk> \<Longrightarrow> i \<in> underS R j"
    32   unfolding underS_def by simp
    33 
    34 lemma underS_E: "i \<in> underS R j \<Longrightarrow> i \<noteq> j \<and> (i, j) \<in> R"
    35   unfolding underS_def by simp
    36 
    37 lemma underS_Field: "i \<in> underS R j \<Longrightarrow> i \<in> Field R"
    38   unfolding underS_def Field_def by auto
    39 
    40 lemma FieldI2: "(i, j) \<in> R \<Longrightarrow> j \<in> Field R"
    41   unfolding Field_def by auto
    42 
    43 lemma bij_betwE: "bij_betw f A B \<Longrightarrow> \<forall>a\<in>A. f a \<in> B"
    44   unfolding bij_betw_def by auto
    45 
    46 lemma bij_betw_imageE: "bij_betw f A B \<Longrightarrow> f ` A = B"
    47   unfolding bij_betw_def by auto
    48 
    49 lemma f_the_inv_into_f_bij_betw:
    50   "bij_betw f A B \<Longrightarrow> (bij_betw f A B \<Longrightarrow> x \<in> B) \<Longrightarrow> f (the_inv_into A f x) = x"
    51   unfolding bij_betw_def by (blast intro: f_the_inv_into_f)
    52 
    53 lemma ex_bij_betw: "|A| \<le>o (r :: 'b rel) \<Longrightarrow> \<exists>f B :: 'b set. bij_betw f B A"
    54   by (subst (asm) internalize_card_of_ordLeq) (auto dest!: iffD2[OF card_of_ordIso ordIso_symmetric])
    55 
    56 lemma bij_betwI':
    57   "\<lbrakk>\<And>x y. \<lbrakk>x \<in> X; y \<in> X\<rbrakk> \<Longrightarrow> (f x = f y) = (x = y);
    58     \<And>x. x \<in> X \<Longrightarrow> f x \<in> Y;
    59     \<And>y. y \<in> Y \<Longrightarrow> \<exists>x \<in> X. y = f x\<rbrakk> \<Longrightarrow> bij_betw f X Y"
    60   unfolding bij_betw_def inj_on_def by blast
    61 
    62 lemma surj_fun_eq:
    63   assumes surj_on: "f ` X = UNIV" and eq_on: "\<forall>x \<in> X. (g1 o f) x = (g2 o f) x"
    64   shows "g1 = g2"
    65 proof (rule ext)
    66   fix y
    67   from surj_on obtain x where "x \<in> X" and "y = f x" by blast
    68   thus "g1 y = g2 y" using eq_on by simp
    69 qed
    70 
    71 lemma Card_order_wo_rel: "Card_order r \<Longrightarrow> wo_rel r"
    72   unfolding wo_rel_def card_order_on_def by blast
    73 
    74 lemma Cinfinite_limit: "\<lbrakk>x \<in> Field r; Cinfinite r\<rbrakk> \<Longrightarrow> \<exists>y \<in> Field r. x \<noteq> y \<and> (x, y) \<in> r"
    75   unfolding cinfinite_def by (auto simp add: infinite_Card_order_limit)
    76 
    77 lemma Card_order_trans:
    78   "\<lbrakk>Card_order r; x \<noteq> y; (x, y) \<in> r; y \<noteq> z; (y, z) \<in> r\<rbrakk> \<Longrightarrow> x \<noteq> z \<and> (x, z) \<in> r"
    79   unfolding card_order_on_def well_order_on_def linear_order_on_def
    80     partial_order_on_def preorder_on_def trans_def antisym_def by blast
    81 
    82 lemma Cinfinite_limit2:
    83   assumes x1: "x1 \<in> Field r" and x2: "x2 \<in> Field r" and r: "Cinfinite r"
    84   shows "\<exists>y \<in> Field r. (x1 \<noteq> y \<and> (x1, y) \<in> r) \<and> (x2 \<noteq> y \<and> (x2, y) \<in> r)"
    85 proof -
    86   from r have trans: "trans r" and total: "Total r" and antisym: "antisym r"
    87     unfolding card_order_on_def well_order_on_def linear_order_on_def
    88       partial_order_on_def preorder_on_def by auto
    89   obtain y1 where y1: "y1 \<in> Field r" "x1 \<noteq> y1" "(x1, y1) \<in> r"
    90     using Cinfinite_limit[OF x1 r] by blast
    91   obtain y2 where y2: "y2 \<in> Field r" "x2 \<noteq> y2" "(x2, y2) \<in> r"
    92     using Cinfinite_limit[OF x2 r] by blast
    93   show ?thesis
    94   proof (cases "y1 = y2")
    95     case True with y1 y2 show ?thesis by blast
    96   next
    97     case False
    98     with y1(1) y2(1) total have "(y1, y2) \<in> r \<or> (y2, y1) \<in> r"
    99       unfolding total_on_def by auto
   100     thus ?thesis
   101     proof
   102       assume *: "(y1, y2) \<in> r"
   103       with trans y1(3) have "(x1, y2) \<in> r" unfolding trans_def by blast
   104       with False y1 y2 * antisym show ?thesis by (cases "x1 = y2") (auto simp: antisym_def)
   105     next
   106       assume *: "(y2, y1) \<in> r"
   107       with trans y2(3) have "(x2, y1) \<in> r" unfolding trans_def by blast
   108       with False y1 y2 * antisym show ?thesis by (cases "x2 = y1") (auto simp: antisym_def)
   109     qed
   110   qed
   111 qed
   112 
   113 lemma Cinfinite_limit_finite:
   114   "\<lbrakk>finite X; X \<subseteq> Field r; Cinfinite r\<rbrakk> \<Longrightarrow> \<exists>y \<in> Field r. \<forall>x \<in> X. (x \<noteq> y \<and> (x, y) \<in> r)"
   115 proof (induct X rule: finite_induct)
   116   case empty thus ?case unfolding cinfinite_def using ex_in_conv[of "Field r"] finite.emptyI by auto
   117 next
   118   case (insert x X)
   119   then obtain y where y: "y \<in> Field r" "\<forall>x \<in> X. (x \<noteq> y \<and> (x, y) \<in> r)" by blast
   120   then obtain z where z: "z \<in> Field r" "x \<noteq> z \<and> (x, z) \<in> r" "y \<noteq> z \<and> (y, z) \<in> r"
   121     using Cinfinite_limit2[OF _ y(1) insert(5), of x] insert(4) by blast
   122   show ?case
   123     apply (intro bexI ballI)
   124     apply (erule insertE)
   125     apply hypsubst
   126     apply (rule z(2))
   127     using Card_order_trans[OF insert(5)[THEN conjunct2]] y(2) z(3)
   128     apply blast
   129     apply (rule z(1))
   130     done
   131 qed
   132 
   133 lemma insert_subsetI: "\<lbrakk>x \<in> A; X \<subseteq> A\<rbrakk> \<Longrightarrow> insert x X \<subseteq> A"
   134   by auto
   135 
   136 lemmas well_order_induct_imp = wo_rel.well_order_induct[of r "\<lambda>x. x \<in> Field r \<longrightarrow> P x" for r P]
   137 
   138 lemma meta_spec2:
   139   assumes "(\<And>x y. PROP P x y)"
   140   shows "PROP P x y"
   141   by (rule assms)
   142 
   143 lemma nchotomy_relcomppE:
   144   assumes "\<And>y. \<exists>x. y = f x" "(r OO s) a c" "\<And>b. r a (f b) \<Longrightarrow> s (f b) c \<Longrightarrow> P"
   145   shows P
   146 proof (rule relcompp.cases[OF assms(2)], hypsubst)
   147   fix b assume "r a b" "s b c"
   148   moreover from assms(1) obtain b' where "b = f b'" by blast
   149   ultimately show P by (blast intro: assms(3))
   150 qed
   151 
   152 lemma predicate2D_vimage2p: "\<lbrakk>R \<le> vimage2p f g S; R x y\<rbrakk> \<Longrightarrow> S (f x) (g y)"
   153   unfolding vimage2p_def by auto
   154 
   155 lemma ssubst_Pair_rhs: "\<lbrakk>(r, s) \<in> R; s' = s\<rbrakk> \<Longrightarrow> (r, s') \<in> R"
   156   by (rule ssubst)
   157 
   158 lemma all_mem_range1:
   159   "(\<And>y. y \<in> range f \<Longrightarrow> P y) \<equiv> (\<And>x. P (f x)) "
   160   by (rule equal_intr_rule) fast+
   161 
   162 lemma all_mem_range2:
   163   "(\<And>fa y. fa \<in> range f \<Longrightarrow> y \<in> range fa \<Longrightarrow> P y) \<equiv> (\<And>x xa. P (f x xa))"
   164   by (rule equal_intr_rule) fast+
   165 
   166 lemma all_mem_range3:
   167   "(\<And>fa fb y. fa \<in> range f \<Longrightarrow> fb \<in> range fa \<Longrightarrow> y \<in> range fb \<Longrightarrow> P y) \<equiv> (\<And>x xa xb. P (f x xa xb))"
   168   by (rule equal_intr_rule) fast+
   169 
   170 lemma all_mem_range4:
   171   "(\<And>fa fb fc y. fa \<in> range f \<Longrightarrow> fb \<in> range fa \<Longrightarrow> fc \<in> range fb \<Longrightarrow> y \<in> range fc \<Longrightarrow> P y) \<equiv>
   172    (\<And>x xa xb xc. P (f x xa xb xc))"
   173   by (rule equal_intr_rule) fast+
   174 
   175 lemma all_mem_range5:
   176   "(\<And>fa fb fc fd y. fa \<in> range f \<Longrightarrow> fb \<in> range fa \<Longrightarrow> fc \<in> range fb \<Longrightarrow> fd \<in> range fc \<Longrightarrow>
   177      y \<in> range fd \<Longrightarrow> P y) \<equiv>
   178    (\<And>x xa xb xc xd. P (f x xa xb xc xd))"
   179   by (rule equal_intr_rule) fast+
   180 
   181 lemma all_mem_range6:
   182   "(\<And>fa fb fc fd fe ff y. fa \<in> range f \<Longrightarrow> fb \<in> range fa \<Longrightarrow> fc \<in> range fb \<Longrightarrow> fd \<in> range fc \<Longrightarrow>
   183      fe \<in> range fd \<Longrightarrow> ff \<in> range fe \<Longrightarrow> y \<in> range ff \<Longrightarrow> P y) \<equiv>
   184    (\<And>x xa xb xc xd xe xf. P (f x xa xb xc xd xe xf))"
   185   by (rule equal_intr_rule) (fastforce, fast)
   186 
   187 lemma all_mem_range7:
   188   "(\<And>fa fb fc fd fe ff fg y. fa \<in> range f \<Longrightarrow> fb \<in> range fa \<Longrightarrow> fc \<in> range fb \<Longrightarrow> fd \<in> range fc \<Longrightarrow>
   189      fe \<in> range fd \<Longrightarrow> ff \<in> range fe \<Longrightarrow> fg \<in> range ff \<Longrightarrow> y \<in> range fg \<Longrightarrow> P y) \<equiv>
   190    (\<And>x xa xb xc xd xe xf xg. P (f x xa xb xc xd xe xf xg))"
   191   by (rule equal_intr_rule) (fastforce, fast)
   192 
   193 lemma all_mem_range8:
   194   "(\<And>fa fb fc fd fe ff fg fh y. fa \<in> range f \<Longrightarrow> fb \<in> range fa \<Longrightarrow> fc \<in> range fb \<Longrightarrow> fd \<in> range fc \<Longrightarrow>
   195      fe \<in> range fd \<Longrightarrow> ff \<in> range fe \<Longrightarrow> fg \<in> range ff \<Longrightarrow> fh \<in> range fg \<Longrightarrow> y \<in> range fh \<Longrightarrow> P y) \<equiv>
   196    (\<And>x xa xb xc xd xe xf xg xh. P (f x xa xb xc xd xe xf xg xh))"
   197   by (rule equal_intr_rule) (fastforce, fast)
   198 
   199 lemmas all_mem_range = all_mem_range1 all_mem_range2 all_mem_range3 all_mem_range4 all_mem_range5
   200   all_mem_range6 all_mem_range7 all_mem_range8
   201 
   202 ML_file "Tools/BNF/bnf_lfp_util.ML"
   203 ML_file "Tools/BNF/bnf_lfp_tactics.ML"
   204 ML_file "Tools/BNF/bnf_lfp.ML"
   205 ML_file "Tools/BNF/bnf_lfp_compat.ML"
   206 ML_file "Tools/BNF/bnf_lfp_rec_sugar_more.ML"
   207 ML_file "Tools/BNF/bnf_lfp_size.ML"
   208 
   209 end