src/HOL/BNF_Least_Fixpoint.thy
changeset 58128 43a1ba26a8cb
parent 58123 62765d39539f
child 58136 10f92532f128
equal deleted inserted replaced
58127:b7cab82f488e 58128:43a1ba26a8cb
       
     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 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 fst_convol': "fst (\<langle>f, g\<rangle> x) = f x"
       
    44   using fst_convol unfolding convol_def by simp
       
    45 
       
    46 lemma snd_convol': "snd (\<langle>f, g\<rangle> x) = g x"
       
    47   using snd_convol unfolding convol_def by simp
       
    48 
       
    49 lemma convol_expand_snd: "fst o f = g \<Longrightarrow> \<langle>g, snd o f\<rangle> = f"
       
    50   unfolding convol_def by auto
       
    51 
       
    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
       
    62 
       
    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
       
    65 
       
    66 lemma bij_betw_imageE: "bij_betw f A B \<Longrightarrow> f ` A = B"
       
    67   unfolding bij_betw_def by auto
       
    68 
       
    69 lemma f_the_inv_into_f_bij_betw: "bij_betw f A B \<Longrightarrow>
       
    70   (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)
       
    72 
       
    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)
       
    75     (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>
       
    96   \<exists>y \<in> Field r. x \<noteq> y \<and> (x, y) \<in> r"
       
    97 unfolding cinfinite_def by (auto simp add: infinite_Card_order_limit)
       
    98 
       
    99 lemma Card_order_trans:
       
   100   "\<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"
       
   101 unfolding card_order_on_def well_order_on_def linear_order_on_def
       
   102   partial_order_on_def preorder_on_def trans_def antisym_def by blast
       
   103 
       
   104 lemma Cinfinite_limit2:
       
   105  assumes x1: "x1 \<in> Field r" and x2: "x2 \<in> Field r" and r: "Cinfinite r"
       
   106  shows "\<exists>y \<in> Field r. (x1 \<noteq> y \<and> (x1, y) \<in> r) \<and> (x2 \<noteq> y \<and> (x2, y) \<in> r)"
       
   107 proof -
       
   108   from r have trans: "trans r" and total: "Total r" and antisym: "antisym r"
       
   109     unfolding card_order_on_def well_order_on_def linear_order_on_def
       
   110       partial_order_on_def preorder_on_def by auto
       
   111   obtain y1 where y1: "y1 \<in> Field r" "x1 \<noteq> y1" "(x1, y1) \<in> r"
       
   112     using Cinfinite_limit[OF x1 r] by blast
       
   113   obtain y2 where y2: "y2 \<in> Field r" "x2 \<noteq> y2" "(x2, y2) \<in> r"
       
   114     using Cinfinite_limit[OF x2 r] by blast
       
   115   show ?thesis
       
   116   proof (cases "y1 = y2")
       
   117     case True with y1 y2 show ?thesis by blast
       
   118   next
       
   119     case False
       
   120     with y1(1) y2(1) total have "(y1, y2) \<in> r \<or> (y2, y1) \<in> r"
       
   121       unfolding total_on_def by auto
       
   122     thus ?thesis
       
   123     proof
       
   124       assume *: "(y1, y2) \<in> r"
       
   125       with trans y1(3) have "(x1, y2) \<in> r" unfolding trans_def by blast
       
   126       with False y1 y2 * antisym show ?thesis by (cases "x1 = y2") (auto simp: antisym_def)
       
   127     next
       
   128       assume *: "(y2, y1) \<in> r"
       
   129       with trans y2(3) have "(x2, y1) \<in> r" unfolding trans_def by blast
       
   130       with False y1 y2 * antisym show ?thesis by (cases "x2 = y1") (auto simp: antisym_def)
       
   131     qed
       
   132   qed
       
   133 qed
       
   134 
       
   135 lemma Cinfinite_limit_finite: "\<lbrakk>finite X; X \<subseteq> Field r; Cinfinite r\<rbrakk>
       
   136  \<Longrightarrow> \<exists>y \<in> Field r. \<forall>x \<in> X. (x \<noteq> y \<and> (x, y) \<in> r)"
       
   137 proof (induct X rule: finite_induct)
       
   138   case empty thus ?case unfolding cinfinite_def using ex_in_conv[of "Field r"] finite.emptyI by auto
       
   139 next
       
   140   case (insert x X)
       
   141   then obtain y where y: "y \<in> Field r" "\<forall>x \<in> X. (x \<noteq> y \<and> (x, y) \<in> r)" by blast
       
   142   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"
       
   143     using Cinfinite_limit2[OF _ y(1) insert(5), of x] insert(4) by blast
       
   144   show ?case
       
   145     apply (intro bexI ballI)
       
   146     apply (erule insertE)
       
   147     apply hypsubst
       
   148     apply (rule z(2))
       
   149     using Card_order_trans[OF insert(5)[THEN conjunct2]] y(2) z(3)
       
   150     apply blast
       
   151     apply (rule z(1))
       
   152     done
       
   153 qed
       
   154 
       
   155 lemma insert_subsetI: "\<lbrakk>x \<in> A; X \<subseteq> A\<rbrakk> \<Longrightarrow> insert x X \<subseteq> A"
       
   156 by auto
       
   157 
       
   158 (*helps resolution*)
       
   159 lemma well_order_induct_imp:
       
   160   "wo_rel r \<Longrightarrow> (\<And>x. \<forall>y. y \<noteq> x \<and> (y, x) \<in> r \<longrightarrow> y \<in> Field r \<longrightarrow> P y \<Longrightarrow> x \<in> Field r \<longrightarrow> P x) \<Longrightarrow>
       
   161      x \<in> Field r \<longrightarrow> P x"
       
   162 by (erule wo_rel.well_order_induct)
       
   163 
       
   164 lemma meta_spec2:
       
   165   assumes "(\<And>x y. PROP P x y)"
       
   166   shows "PROP P x y"
       
   167 by (rule assms)
       
   168 
       
   169 lemma nchotomy_relcomppE:
       
   170   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"
       
   171   shows P
       
   172 proof (rule relcompp.cases[OF assms(2)], hypsubst)
       
   173   fix b assume "r a b" "s b c"
       
   174   moreover from assms(1) obtain b' where "b = f b'" by blast
       
   175   ultimately show P by (blast intro: assms(3))
       
   176 qed
       
   177 
       
   178 lemma vimage2p_rel_fun: "rel_fun (vimage2p f g R) R f g"
       
   179   unfolding rel_fun_def vimage2p_def by auto
       
   180 
       
   181 lemma predicate2D_vimage2p: "\<lbrakk>R \<le> vimage2p f g S; R x y\<rbrakk> \<Longrightarrow> S (f x) (g y)"
       
   182   unfolding vimage2p_def by auto
       
   183 
       
   184 lemma id_transfer: "rel_fun A A id id"
       
   185   unfolding rel_fun_def by simp
       
   186 
       
   187 lemma ssubst_Pair_rhs: "\<lbrakk>(r, s) \<in> R; s' = s\<rbrakk> \<Longrightarrow> (r, s') \<in> R"
       
   188   by (rule ssubst)
       
   189 
       
   190 ML_file "Tools/BNF/bnf_lfp_util.ML"
       
   191 ML_file "Tools/BNF/bnf_lfp_tactics.ML"
       
   192 ML_file "Tools/BNF/bnf_lfp.ML"
       
   193 ML_file "Tools/BNF/bnf_lfp_compat.ML"
       
   194 ML_file "Tools/BNF/bnf_lfp_rec_sugar_more.ML"
       
   195 
       
   196 hide_fact (open) id_transfer
       
   197 
       
   198 end