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