author wenzelm
Sat Jul 18 22:58:50 2015 +0200 (2015-07-18)
changeset 60758 d8d85a8172b5
parent 58916 229765cc3414
child 62905 52c5a25e0c96
permissions -rw-r--r--
isabelle update_cartouches;
     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
     7 Least fixpoint (datatype) operation on bounded natural functors.
     8 *)
    10 section \<open>Least Fixpoint (Datatype) Operation on Bounded Natural Functors\<close>
    12 theory BNF_Least_Fixpoint
    13 imports BNF_Fixpoint_Base
    14 keywords
    15   "datatype" :: thy_decl and
    16   "datatype_compat" :: thy_decl
    17 begin
    19 lemma subset_emptyI: "(\<And>x. x \<in> A \<Longrightarrow> False) \<Longrightarrow> A \<subseteq> {}"
    20   by blast
    22 lemma image_Collect_subsetI: "(\<And>x. P x \<Longrightarrow> f x \<in> B) \<Longrightarrow> f ` {x. P x} \<subseteq> B"
    23   by blast
    25 lemma Collect_restrict: "{x. x \<in> X \<and> P x} \<subseteq> X"
    26   by auto
    28 lemma prop_restrict: "\<lbrakk>x \<in> Z; Z \<subseteq> {x. x \<in> X \<and> P x}\<rbrakk> \<Longrightarrow> P x"
    29   by auto
    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
    34 lemma underS_E: "i \<in> underS R j \<Longrightarrow> i \<noteq> j \<and> (i, j) \<in> R"
    35   unfolding underS_def by simp
    37 lemma underS_Field: "i \<in> underS R j \<Longrightarrow> i \<in> Field R"
    38   unfolding underS_def Field_def by auto
    40 lemma FieldI2: "(i, j) \<in> R \<Longrightarrow> j \<in> Field R"
    41   unfolding Field_def by auto
    43 lemma fst_convol': "fst (\<langle>f, g\<rangle> x) = f x"
    44   using fst_convol unfolding convol_def by simp
    46 lemma snd_convol': "snd (\<langle>f, g\<rangle> x) = g x"
    47   using snd_convol unfolding convol_def by simp
    49 lemma convol_expand_snd: "fst o f = g \<Longrightarrow> \<langle>g, snd o f\<rangle> = f"
    50   unfolding convol_def by auto
    52 lemma convol_expand_snd':
    53   assumes "(fst o f = g)"
    54   shows "h = snd o f \<longleftrightarrow> \<langle>g, h\<rangle> = f"
    55 proof -
    56   from assms have *: "\<langle>g, snd o f\<rangle> = f" by (rule convol_expand_snd)
    57   then have "h = snd o f \<longleftrightarrow> h = snd o \<langle>g, snd o f\<rangle>" by simp
    58   moreover have "\<dots> \<longleftrightarrow> h = snd o f" by (simp add: snd_convol)
    59   moreover have "\<dots> \<longleftrightarrow> \<langle>g, h\<rangle> = f" by (subst (2) *[symmetric]) (auto simp: convol_def fun_eq_iff)
    60   ultimately show ?thesis by simp
    61 qed
    63 lemma bij_betwE: "bij_betw f A B \<Longrightarrow> \<forall>a\<in>A. f a \<in> B"
    64   unfolding bij_betw_def by auto
    66 lemma bij_betw_imageE: "bij_betw f A B \<Longrightarrow> f ` A = B"
    67   unfolding bij_betw_def by auto
    69 lemma f_the_inv_into_f_bij_betw:
    70   "bij_betw f A B \<Longrightarrow> (bij_betw f A B \<Longrightarrow> x \<in> B) \<Longrightarrow> f (the_inv_into A f x) = x"
    71   unfolding bij_betw_def by (blast intro: f_the_inv_into_f)
    73 lemma ex_bij_betw: "|A| \<le>o (r :: 'b rel) \<Longrightarrow> \<exists>f B :: 'b set. bij_betw f B A"
    74   by (subst (asm) internalize_card_of_ordLeq) (auto dest!: iffD2[OF card_of_ordIso ordIso_symmetric])
    76 lemma bij_betwI':
    77   "\<lbrakk>\<And>x y. \<lbrakk>x \<in> X; y \<in> X\<rbrakk> \<Longrightarrow> (f x = f y) = (x = y);
    78     \<And>x. x \<in> X \<Longrightarrow> f x \<in> Y;
    79     \<And>y. y \<in> Y \<Longrightarrow> \<exists>x \<in> X. y = f x\<rbrakk> \<Longrightarrow> bij_betw f X Y"
    80   unfolding bij_betw_def inj_on_def by blast
    82 lemma surj_fun_eq:
    83   assumes surj_on: "f ` X = UNIV" and eq_on: "\<forall>x \<in> X. (g1 o f) x = (g2 o f) x"
    84   shows "g1 = g2"
    85 proof (rule ext)
    86   fix y
    87   from surj_on obtain x where "x \<in> X" and "y = f x" by blast
    88   thus "g1 y = g2 y" using eq_on by simp
    89 qed
    91 lemma Card_order_wo_rel: "Card_order r \<Longrightarrow> wo_rel r"
    92   unfolding wo_rel_def card_order_on_def by blast
    94 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"
    95   unfolding cinfinite_def by (auto simp add: infinite_Card_order_limit)
    97 lemma Card_order_trans:
    98   "\<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"
    99   unfolding card_order_on_def well_order_on_def linear_order_on_def
   100     partial_order_on_def preorder_on_def trans_def antisym_def by blast
   102 lemma Cinfinite_limit2:
   103   assumes x1: "x1 \<in> Field r" and x2: "x2 \<in> Field r" and r: "Cinfinite r"
   104   shows "\<exists>y \<in> Field r. (x1 \<noteq> y \<and> (x1, y) \<in> r) \<and> (x2 \<noteq> y \<and> (x2, y) \<in> r)"
   105 proof -
   106   from r have trans: "trans r" and total: "Total r" and antisym: "antisym r"
   107     unfolding card_order_on_def well_order_on_def linear_order_on_def
   108       partial_order_on_def preorder_on_def by auto
   109   obtain y1 where y1: "y1 \<in> Field r" "x1 \<noteq> y1" "(x1, y1) \<in> r"
   110     using Cinfinite_limit[OF x1 r] by blast
   111   obtain y2 where y2: "y2 \<in> Field r" "x2 \<noteq> y2" "(x2, y2) \<in> r"
   112     using Cinfinite_limit[OF x2 r] by blast
   113   show ?thesis
   114   proof (cases "y1 = y2")
   115     case True with y1 y2 show ?thesis by blast
   116   next
   117     case False
   118     with y1(1) y2(1) total have "(y1, y2) \<in> r \<or> (y2, y1) \<in> r"
   119       unfolding total_on_def by auto
   120     thus ?thesis
   121     proof
   122       assume *: "(y1, y2) \<in> r"
   123       with trans y1(3) have "(x1, y2) \<in> r" unfolding trans_def by blast
   124       with False y1 y2 * antisym show ?thesis by (cases "x1 = y2") (auto simp: antisym_def)
   125     next
   126       assume *: "(y2, y1) \<in> r"
   127       with trans y2(3) have "(x2, y1) \<in> r" unfolding trans_def by blast
   128       with False y1 y2 * antisym show ?thesis by (cases "x2 = y1") (auto simp: antisym_def)
   129     qed
   130   qed
   131 qed
   133 lemma Cinfinite_limit_finite:
   134   "\<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)"
   135 proof (induct X rule: finite_induct)
   136   case empty thus ?case unfolding cinfinite_def using ex_in_conv[of "Field r"] finite.emptyI by auto
   137 next
   138   case (insert x X)
   139   then obtain y where y: "y \<in> Field r" "\<forall>x \<in> X. (x \<noteq> y \<and> (x, y) \<in> r)" by blast
   140   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"
   141     using Cinfinite_limit2[OF _ y(1) insert(5), of x] insert(4) by blast
   142   show ?case
   143     apply (intro bexI ballI)
   144     apply (erule insertE)
   145     apply hypsubst
   146     apply (rule z(2))
   147     using Card_order_trans[OF insert(5)[THEN conjunct2]] y(2) z(3)
   148     apply blast
   149     apply (rule z(1))
   150     done
   151 qed
   153 lemma insert_subsetI: "\<lbrakk>x \<in> A; X \<subseteq> A\<rbrakk> \<Longrightarrow> insert x X \<subseteq> A"
   154   by auto
   156 lemmas well_order_induct_imp = wo_rel.well_order_induct[of r "\<lambda>x. x \<in> Field r \<longrightarrow> P x" for r P]
   158 lemma meta_spec2:
   159   assumes "(\<And>x y. PROP P x y)"
   160   shows "PROP P x y"
   161   by (rule assms)
   163 lemma nchotomy_relcomppE:
   164   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"
   165   shows P
   166 proof (rule relcompp.cases[OF assms(2)], hypsubst)
   167   fix b assume "r a b" "s b c"
   168   moreover from assms(1) obtain b' where "b = f b'" by blast
   169   ultimately show P by (blast intro: assms(3))
   170 qed
   172 lemma predicate2D_vimage2p: "\<lbrakk>R \<le> vimage2p f g S; R x y\<rbrakk> \<Longrightarrow> S (f x) (g y)"
   173   unfolding vimage2p_def by auto
   175 lemma id_transfer: "rel_fun A A id id"
   176   unfolding rel_fun_def by simp
   178 lemma fst_transfer: "rel_fun (rel_prod A B) A fst fst"
   179   unfolding rel_fun_def by simp
   181 lemma snd_transfer: "rel_fun (rel_prod A B) B snd snd"
   182   unfolding rel_fun_def by simp
   184 lemma convol_transfer:
   185   "rel_fun (rel_fun R S) (rel_fun (rel_fun R T) (rel_fun R (rel_prod S T))) BNF_Def.convol BNF_Def.convol"
   186   unfolding rel_fun_def convol_def by auto
   188 lemma ssubst_Pair_rhs: "\<lbrakk>(r, s) \<in> R; s' = s\<rbrakk> \<Longrightarrow> (r, s') \<in> R"
   189   by (rule ssubst)
   191 lemma all_mem_range1:
   192   "(\<And>y. y \<in> range f \<Longrightarrow> P y) \<equiv> (\<And>x. P (f x)) "
   193   by (rule equal_intr_rule) fast+
   195 lemma all_mem_range2:
   196   "(\<And>fa y. fa \<in> range f \<Longrightarrow> y \<in> range fa \<Longrightarrow> P y) \<equiv> (\<And>x xa. P (f x xa))"
   197   by (rule equal_intr_rule) fast+
   199 lemma all_mem_range3:
   200   "(\<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))"
   201   by (rule equal_intr_rule) fast+
   203 lemma all_mem_range4:
   204   "(\<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>
   205    (\<And>x xa xb xc. P (f x xa xb xc))"
   206   by (rule equal_intr_rule) fast+
   208 lemma all_mem_range5:
   209   "(\<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>
   210      y \<in> range fd \<Longrightarrow> P y) \<equiv>
   211    (\<And>x xa xb xc xd. P (f x xa xb xc xd))"
   212   by (rule equal_intr_rule) fast+
   214 lemma all_mem_range6:
   215   "(\<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>
   216      fe \<in> range fd \<Longrightarrow> ff \<in> range fe \<Longrightarrow> y \<in> range ff \<Longrightarrow> P y) \<equiv>
   217    (\<And>x xa xb xc xd xe xf. P (f x xa xb xc xd xe xf))"
   218   by (rule equal_intr_rule) (fastforce, fast)
   220 lemma all_mem_range7:
   221   "(\<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>
   222      fe \<in> range fd \<Longrightarrow> ff \<in> range fe \<Longrightarrow> fg \<in> range ff \<Longrightarrow> y \<in> range fg \<Longrightarrow> P y) \<equiv>
   223    (\<And>x xa xb xc xd xe xf xg. P (f x xa xb xc xd xe xf xg))"
   224   by (rule equal_intr_rule) (fastforce, fast)
   226 lemma all_mem_range8:
   227   "(\<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>
   228      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>
   229    (\<And>x xa xb xc xd xe xf xg xh. P (f x xa xb xc xd xe xf xg xh))"
   230   by (rule equal_intr_rule) (fastforce, fast)
   232 lemmas all_mem_range = all_mem_range1 all_mem_range2 all_mem_range3 all_mem_range4 all_mem_range5
   233   all_mem_range6 all_mem_range7 all_mem_range8
   235 ML_file "Tools/BNF/bnf_lfp_util.ML"
   236 ML_file "Tools/BNF/bnf_lfp_tactics.ML"
   237 ML_file "Tools/BNF/bnf_lfp.ML"
   238 ML_file "Tools/BNF/bnf_lfp_compat.ML"
   239 ML_file "Tools/BNF/bnf_lfp_rec_sugar_more.ML"
   240 ML_file "Tools/BNF/bnf_lfp_size.ML"
   242 end