src/HOL/BNF_Least_Fixpoint.thy
author blanchet
Thu Sep 11 19:32:36 2014 +0200 (2014-09-11)
changeset 58310 91ea607a34d8
parent 58305 57752a91eec4
child 58314 ee1be8b3032e
permissions -rw-r--r--
updated news
     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 fixed point operation on bounded natural functors.
     8 *)
     9 
    10 header {* Least Fixed Point Operation on Bounded Natural Functors *}
    11 
    12 theory BNF_Least_Fixpoint
    13 imports BNF_Fixpoint_Base
    14 keywords
    15   "datatype" :: thy_decl and
    16   "datatype" :: thy_decl and
    17   "datatype_compat" :: thy_decl
    18 begin
    19 
    20 lemma subset_emptyI: "(\<And>x. x \<in> A \<Longrightarrow> False) \<Longrightarrow> A \<subseteq> {}"
    21   by blast
    22 
    23 lemma image_Collect_subsetI: "(\<And>x. P x \<Longrightarrow> f x \<in> B) \<Longrightarrow> f ` {x. P x} \<subseteq> B"
    24   by blast
    25 
    26 lemma Collect_restrict: "{x. x \<in> X \<and> P x} \<subseteq> X"
    27   by auto
    28 
    29 lemma prop_restrict: "\<lbrakk>x \<in> Z; Z \<subseteq> {x. x \<in> X \<and> P x}\<rbrakk> \<Longrightarrow> P x"
    30   by auto
    31 
    32 lemma underS_I: "\<lbrakk>i \<noteq> j; (i, j) \<in> R\<rbrakk> \<Longrightarrow> i \<in> underS R j"
    33   unfolding underS_def by simp
    34 
    35 lemma underS_E: "i \<in> underS R j \<Longrightarrow> i \<noteq> j \<and> (i, j) \<in> R"
    36   unfolding underS_def by simp
    37 
    38 lemma underS_Field: "i \<in> underS R j \<Longrightarrow> i \<in> Field R"
    39   unfolding underS_def Field_def by auto
    40 
    41 lemma FieldI2: "(i, j) \<in> R \<Longrightarrow> j \<in> Field R"
    42   unfolding Field_def by auto
    43 
    44 lemma fst_convol': "fst (\<langle>f, g\<rangle> x) = f x"
    45   using fst_convol unfolding convol_def by simp
    46 
    47 lemma snd_convol': "snd (\<langle>f, g\<rangle> x) = g x"
    48   using snd_convol unfolding convol_def by simp
    49 
    50 lemma convol_expand_snd: "fst o f = g \<Longrightarrow> \<langle>g, snd o f\<rangle> = f"
    51   unfolding convol_def by auto
    52 
    53 lemma convol_expand_snd':
    54   assumes "(fst o f = g)"
    55   shows "h = snd o f \<longleftrightarrow> \<langle>g, h\<rangle> = f"
    56 proof -
    57   from assms have *: "\<langle>g, snd o f\<rangle> = f" by (rule convol_expand_snd)
    58   then have "h = snd o f \<longleftrightarrow> h = snd o \<langle>g, snd o f\<rangle>" by simp
    59   moreover have "\<dots> \<longleftrightarrow> h = snd o f" by (simp add: snd_convol)
    60   moreover have "\<dots> \<longleftrightarrow> \<langle>g, h\<rangle> = f" by (subst (2) *[symmetric]) (auto simp: convol_def fun_eq_iff)
    61   ultimately show ?thesis by simp
    62 qed
    63 
    64 lemma bij_betwE: "bij_betw f A B \<Longrightarrow> \<forall>a\<in>A. f a \<in> B"
    65   unfolding bij_betw_def by auto
    66 
    67 lemma bij_betw_imageE: "bij_betw f A B \<Longrightarrow> f ` A = B"
    68   unfolding bij_betw_def by auto
    69 
    70 lemma f_the_inv_into_f_bij_betw:
    71   "bij_betw f A B \<Longrightarrow> (bij_betw f A B \<Longrightarrow> x \<in> B) \<Longrightarrow> f (the_inv_into A f x) = x"
    72   unfolding bij_betw_def by (blast intro: f_the_inv_into_f)
    73 
    74 lemma ex_bij_betw: "|A| \<le>o (r :: 'b rel) \<Longrightarrow> \<exists>f B :: 'b set. bij_betw f B A"
    75   by (subst (asm) internalize_card_of_ordLeq) (auto dest!: iffD2[OF card_of_ordIso ordIso_symmetric])
    76 
    77 lemma bij_betwI':
    78   "\<lbrakk>\<And>x y. \<lbrakk>x \<in> X; y \<in> X\<rbrakk> \<Longrightarrow> (f x = f y) = (x = y);
    79     \<And>x. x \<in> X \<Longrightarrow> f x \<in> Y;
    80     \<And>y. y \<in> Y \<Longrightarrow> \<exists>x \<in> X. y = f x\<rbrakk> \<Longrightarrow> bij_betw f X Y"
    81   unfolding bij_betw_def inj_on_def by blast
    82 
    83 lemma surj_fun_eq:
    84   assumes surj_on: "f ` X = UNIV" and eq_on: "\<forall>x \<in> X. (g1 o f) x = (g2 o f) x"
    85   shows "g1 = g2"
    86 proof (rule ext)
    87   fix y
    88   from surj_on obtain x where "x \<in> X" and "y = f x" by blast
    89   thus "g1 y = g2 y" using eq_on by simp
    90 qed
    91 
    92 lemma Card_order_wo_rel: "Card_order r \<Longrightarrow> wo_rel r"
    93   unfolding wo_rel_def card_order_on_def by blast
    94 
    95 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"
    96   unfolding cinfinite_def by (auto simp add: infinite_Card_order_limit)
    97 
    98 lemma Card_order_trans:
    99   "\<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"
   100   unfolding card_order_on_def well_order_on_def linear_order_on_def
   101     partial_order_on_def preorder_on_def trans_def antisym_def by blast
   102 
   103 lemma Cinfinite_limit2:
   104   assumes x1: "x1 \<in> Field r" and x2: "x2 \<in> Field r" and r: "Cinfinite r"
   105   shows "\<exists>y \<in> Field r. (x1 \<noteq> y \<and> (x1, y) \<in> r) \<and> (x2 \<noteq> y \<and> (x2, y) \<in> r)"
   106 proof -
   107   from r have trans: "trans r" and total: "Total r" and antisym: "antisym r"
   108     unfolding card_order_on_def well_order_on_def linear_order_on_def
   109       partial_order_on_def preorder_on_def by auto
   110   obtain y1 where y1: "y1 \<in> Field r" "x1 \<noteq> y1" "(x1, y1) \<in> r"
   111     using Cinfinite_limit[OF x1 r] by blast
   112   obtain y2 where y2: "y2 \<in> Field r" "x2 \<noteq> y2" "(x2, y2) \<in> r"
   113     using Cinfinite_limit[OF x2 r] by blast
   114   show ?thesis
   115   proof (cases "y1 = y2")
   116     case True with y1 y2 show ?thesis by blast
   117   next
   118     case False
   119     with y1(1) y2(1) total have "(y1, y2) \<in> r \<or> (y2, y1) \<in> r"
   120       unfolding total_on_def by auto
   121     thus ?thesis
   122     proof
   123       assume *: "(y1, y2) \<in> r"
   124       with trans y1(3) have "(x1, y2) \<in> r" unfolding trans_def by blast
   125       with False y1 y2 * antisym show ?thesis by (cases "x1 = y2") (auto simp: antisym_def)
   126     next
   127       assume *: "(y2, y1) \<in> r"
   128       with trans y2(3) have "(x2, y1) \<in> r" unfolding trans_def by blast
   129       with False y1 y2 * antisym show ?thesis by (cases "x2 = y1") (auto simp: antisym_def)
   130     qed
   131   qed
   132 qed
   133 
   134 lemma Cinfinite_limit_finite:
   135   "\<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)"
   136 proof (induct X rule: finite_induct)
   137   case empty thus ?case unfolding cinfinite_def using ex_in_conv[of "Field r"] finite.emptyI by auto
   138 next
   139   case (insert x X)
   140   then obtain y where y: "y \<in> Field r" "\<forall>x \<in> X. (x \<noteq> y \<and> (x, y) \<in> r)" by blast
   141   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"
   142     using Cinfinite_limit2[OF _ y(1) insert(5), of x] insert(4) by blast
   143   show ?case
   144     apply (intro bexI ballI)
   145     apply (erule insertE)
   146     apply hypsubst
   147     apply (rule z(2))
   148     using Card_order_trans[OF insert(5)[THEN conjunct2]] y(2) z(3)
   149     apply blast
   150     apply (rule z(1))
   151     done
   152 qed
   153 
   154 lemma insert_subsetI: "\<lbrakk>x \<in> A; X \<subseteq> A\<rbrakk> \<Longrightarrow> insert x X \<subseteq> A"
   155   by auto
   156 
   157 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 
   159 lemma meta_spec2:
   160   assumes "(\<And>x y. PROP P x y)"
   161   shows "PROP P x y"
   162   by (rule assms)
   163 
   164 lemma nchotomy_relcomppE:
   165   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"
   166   shows P
   167 proof (rule relcompp.cases[OF assms(2)], hypsubst)
   168   fix b assume "r a b" "s b c"
   169   moreover from assms(1) obtain b' where "b = f b'" by blast
   170   ultimately show P by (blast intro: assms(3))
   171 qed
   172 
   173 lemma vimage2p_rel_fun: "rel_fun (vimage2p f g R) R f g"
   174   unfolding rel_fun_def vimage2p_def by auto
   175 
   176 lemma predicate2D_vimage2p: "\<lbrakk>R \<le> vimage2p f g S; R x y\<rbrakk> \<Longrightarrow> S (f x) (g y)"
   177   unfolding vimage2p_def by auto
   178 
   179 lemma id_transfer: "rel_fun A A id id"
   180   unfolding rel_fun_def by simp
   181 
   182 lemma ssubst_Pair_rhs: "\<lbrakk>(r, s) \<in> R; s' = s\<rbrakk> \<Longrightarrow> (r, s') \<in> R"
   183   by (rule ssubst)
   184 
   185 lemma all_mem_range1:
   186   "(\<And>y. y \<in> range f \<Longrightarrow> P y) \<equiv> (\<And>x. P (f x)) "
   187   by (rule equal_intr_rule) fast+
   188 
   189 lemma all_mem_range2:
   190   "(\<And>fa y. fa \<in> range f \<Longrightarrow> y \<in> range fa \<Longrightarrow> P y) \<equiv> (\<And>x xa. P (f x xa))"
   191   by (rule equal_intr_rule) fast+
   192 
   193 lemma all_mem_range3:
   194   "(\<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))"
   195   by (rule equal_intr_rule) fast+
   196 
   197 lemma all_mem_range4:
   198   "(\<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>
   199    (\<And>x xa xb xc. P (f x xa xb xc))"
   200   by (rule equal_intr_rule) fast+
   201 
   202 lemma all_mem_range5:
   203   "(\<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>
   204      y \<in> range fd \<Longrightarrow> P y) \<equiv>
   205    (\<And>x xa xb xc xd. P (f x xa xb xc xd))"
   206   by (rule equal_intr_rule) fast+
   207 
   208 lemma all_mem_range6:
   209   "(\<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>
   210      fe \<in> range fd \<Longrightarrow> ff \<in> range fe \<Longrightarrow> y \<in> range ff \<Longrightarrow> P y) \<equiv>
   211    (\<And>x xa xb xc xd xe xf. P (f x xa xb xc xd xe xf))"
   212   by (rule equal_intr_rule) (fastforce, fast)
   213 
   214 lemma all_mem_range7:
   215   "(\<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>
   216      fe \<in> range fd \<Longrightarrow> ff \<in> range fe \<Longrightarrow> fg \<in> range ff \<Longrightarrow> y \<in> range fg \<Longrightarrow> P y) \<equiv>
   217    (\<And>x xa xb xc xd xe xf xg. P (f x xa xb xc xd xe xf xg))"
   218   by (rule equal_intr_rule) (fastforce, fast)
   219 
   220 lemma all_mem_range8:
   221   "(\<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>
   222      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>
   223    (\<And>x xa xb xc xd xe xf xg xh. P (f x xa xb xc xd xe xf xg xh))"
   224   by (rule equal_intr_rule) (fastforce, fast)
   225 
   226 lemmas all_mem_range = all_mem_range1 all_mem_range2 all_mem_range3 all_mem_range4 all_mem_range5
   227   all_mem_range6 all_mem_range7 all_mem_range8
   228 
   229 ML_file "Tools/BNF/bnf_lfp_util.ML"
   230 ML_file "Tools/BNF/bnf_lfp_tactics.ML"
   231 ML_file "Tools/BNF/bnf_lfp.ML"
   232 ML_file "Tools/BNF/bnf_lfp_compat.ML"
   233 ML_file "Tools/BNF/bnf_lfp_rec_sugar_more.ML"
   234 ML_file "Tools/BNF/bnf_lfp_size.ML"
   235 ML_file "Tools/Function/old_size.ML"
   236 ML_file "Tools/datatype_realizer.ML"
   237 
   238 lemma size_bool[code]: "size (b\<Colon>bool) = 0"
   239   by (cases b) auto
   240 
   241 lemma size_nat[simp, code]: "size (n\<Colon>nat) = n"
   242   by (induct n) simp_all
   243 
   244 declare prod.size[no_atp]
   245 
   246 lemma size_sum_o_map: "size_sum g1 g2 \<circ> map_sum f1 f2 = size_sum (g1 \<circ> f1) (g2 \<circ> f2)"
   247   by (rule ext) (case_tac x, auto)
   248 
   249 lemma size_prod_o_map: "size_prod g1 g2 \<circ> map_prod f1 f2 = size_prod (g1 \<circ> f1) (g2 \<circ> f2)"
   250   by (rule ext) auto
   251 
   252 setup {*
   253 BNF_LFP_Size.register_size_global @{type_name sum} @{const_name size_sum} @{thms sum.size}
   254   @{thms size_sum_o_map}
   255 #> BNF_LFP_Size.register_size_global @{type_name prod} @{const_name size_prod} @{thms prod.size}
   256   @{thms size_prod_o_map}
   257 *}
   258 
   259 hide_fact (open) id_transfer
   260 
   261 end