src/HOL/BNF_Def.thy
author wenzelm
Tue Sep 26 20:54:40 2017 +0200 (21 months ago)
changeset 66695 91500c024c7f
parent 66198 4a5589dd8e1a
child 67091 1393c2340eec
permissions -rw-r--r--
tuned;
     1 (*  Title:      HOL/BNF_Def.thy
     2     Author:     Dmitriy Traytel, TU Muenchen
     3     Author:     Jasmin Blanchette, TU Muenchen
     4     Copyright   2012, 2013, 2014
     5 
     6 Definition of bounded natural functors.
     7 *)
     8 
     9 section \<open>Definition of Bounded Natural Functors\<close>
    10 
    11 theory BNF_Def
    12 imports BNF_Cardinal_Arithmetic Fun_Def_Base
    13 keywords
    14   "print_bnfs" :: diag and
    15   "bnf" :: thy_goal
    16 begin
    17 
    18 lemma Collect_case_prodD: "x \<in> Collect (case_prod A) \<Longrightarrow> A (fst x) (snd x)"
    19   by auto
    20 
    21 inductive
    22    rel_sum :: "('a \<Rightarrow> 'c \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> 'd \<Rightarrow> bool) \<Rightarrow> 'a + 'b \<Rightarrow> 'c + 'd \<Rightarrow> bool" for R1 R2
    23 where
    24   "R1 a c \<Longrightarrow> rel_sum R1 R2 (Inl a) (Inl c)"
    25 | "R2 b d \<Longrightarrow> rel_sum R1 R2 (Inr b) (Inr d)"
    26 
    27 definition
    28   rel_fun :: "('a \<Rightarrow> 'c \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> 'd \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('c \<Rightarrow> 'd) \<Rightarrow> bool"
    29 where
    30   "rel_fun A B = (\<lambda>f g. \<forall>x y. A x y \<longrightarrow> B (f x) (g y))"
    31 
    32 lemma rel_funI [intro]:
    33   assumes "\<And>x y. A x y \<Longrightarrow> B (f x) (g y)"
    34   shows "rel_fun A B f g"
    35   using assms by (simp add: rel_fun_def)
    36 
    37 lemma rel_funD:
    38   assumes "rel_fun A B f g" and "A x y"
    39   shows "B (f x) (g y)"
    40   using assms by (simp add: rel_fun_def)
    41 
    42 lemma rel_fun_mono:
    43   "\<lbrakk> rel_fun X A f g; \<And>x y. Y x y \<longrightarrow> X x y; \<And>x y. A x y \<Longrightarrow> B x y \<rbrakk> \<Longrightarrow> rel_fun Y B f g"
    44 by(simp add: rel_fun_def)
    45 
    46 lemma rel_fun_mono' [mono]:
    47   "\<lbrakk> \<And>x y. Y x y \<longrightarrow> X x y; \<And>x y. A x y \<longrightarrow> B x y \<rbrakk> \<Longrightarrow> rel_fun X A f g \<longrightarrow> rel_fun Y B f g"
    48 by(simp add: rel_fun_def)
    49 
    50 definition rel_set :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> bool"
    51   where "rel_set R = (\<lambda>A B. (\<forall>x\<in>A. \<exists>y\<in>B. R x y) \<and> (\<forall>y\<in>B. \<exists>x\<in>A. R x y))"
    52 
    53 lemma rel_setI:
    54   assumes "\<And>x. x \<in> A \<Longrightarrow> \<exists>y\<in>B. R x y"
    55   assumes "\<And>y. y \<in> B \<Longrightarrow> \<exists>x\<in>A. R x y"
    56   shows "rel_set R A B"
    57   using assms unfolding rel_set_def by simp
    58 
    59 lemma predicate2_transferD:
    60    "\<lbrakk>rel_fun R1 (rel_fun R2 (op =)) P Q; a \<in> A; b \<in> B; A \<subseteq> {(x, y). R1 x y}; B \<subseteq> {(x, y). R2 x y}\<rbrakk> \<Longrightarrow>
    61    P (fst a) (fst b) \<longleftrightarrow> Q (snd a) (snd b)"
    62   unfolding rel_fun_def by (blast dest!: Collect_case_prodD)
    63 
    64 definition collect where
    65   "collect F x = (\<Union>f \<in> F. f x)"
    66 
    67 lemma fstI: "x = (y, z) \<Longrightarrow> fst x = y"
    68   by simp
    69 
    70 lemma sndI: "x = (y, z) \<Longrightarrow> snd x = z"
    71   by simp
    72 
    73 lemma bijI': "\<lbrakk>\<And>x y. (f x = f y) = (x = y); \<And>y. \<exists>x. y = f x\<rbrakk> \<Longrightarrow> bij f"
    74   unfolding bij_def inj_on_def by auto blast
    75 
    76 (* Operator: *)
    77 definition "Gr A f = {(a, f a) | a. a \<in> A}"
    78 
    79 definition "Grp A f = (\<lambda>a b. b = f a \<and> a \<in> A)"
    80 
    81 definition vimage2p where
    82   "vimage2p f g R = (\<lambda>x y. R (f x) (g y))"
    83 
    84 lemma collect_comp: "collect F \<circ> g = collect ((\<lambda>f. f \<circ> g) ` F)"
    85   by (rule ext) (simp add: collect_def)
    86 
    87 definition convol ("\<langle>(_,/ _)\<rangle>") where
    88   "\<langle>f, g\<rangle> \<equiv> \<lambda>a. (f a, g a)"
    89 
    90 lemma fst_convol: "fst \<circ> \<langle>f, g\<rangle> = f"
    91   apply(rule ext)
    92   unfolding convol_def by simp
    93 
    94 lemma snd_convol: "snd \<circ> \<langle>f, g\<rangle> = g"
    95   apply(rule ext)
    96   unfolding convol_def by simp
    97 
    98 lemma convol_mem_GrpI:
    99   "x \<in> A \<Longrightarrow> \<langle>id, g\<rangle> x \<in> (Collect (case_prod (Grp A g)))"
   100   unfolding convol_def Grp_def by auto
   101 
   102 definition csquare where
   103   "csquare A f1 f2 p1 p2 \<longleftrightarrow> (\<forall> a \<in> A. f1 (p1 a) = f2 (p2 a))"
   104 
   105 lemma eq_alt: "op = = Grp UNIV id"
   106   unfolding Grp_def by auto
   107 
   108 lemma leq_conversepI: "R = op = \<Longrightarrow> R \<le> R^--1"
   109   by auto
   110 
   111 lemma leq_OOI: "R = op = \<Longrightarrow> R \<le> R OO R"
   112   by auto
   113 
   114 lemma OO_Grp_alt: "(Grp A f)^--1 OO Grp A g = (\<lambda>x y. \<exists>z. z \<in> A \<and> f z = x \<and> g z = y)"
   115   unfolding Grp_def by auto
   116 
   117 lemma Grp_UNIV_id: "f = id \<Longrightarrow> (Grp UNIV f)^--1 OO Grp UNIV f = Grp UNIV f"
   118   unfolding Grp_def by auto
   119 
   120 lemma Grp_UNIV_idI: "x = y \<Longrightarrow> Grp UNIV id x y"
   121   unfolding Grp_def by auto
   122 
   123 lemma Grp_mono: "A \<le> B \<Longrightarrow> Grp A f \<le> Grp B f"
   124   unfolding Grp_def by auto
   125 
   126 lemma GrpI: "\<lbrakk>f x = y; x \<in> A\<rbrakk> \<Longrightarrow> Grp A f x y"
   127   unfolding Grp_def by auto
   128 
   129 lemma GrpE: "Grp A f x y \<Longrightarrow> (\<lbrakk>f x = y; x \<in> A\<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R"
   130   unfolding Grp_def by auto
   131 
   132 lemma Collect_case_prod_Grp_eqD: "z \<in> Collect (case_prod (Grp A f)) \<Longrightarrow> (f \<circ> fst) z = snd z"
   133   unfolding Grp_def comp_def by auto
   134 
   135 lemma Collect_case_prod_Grp_in: "z \<in> Collect (case_prod (Grp A f)) \<Longrightarrow> fst z \<in> A"
   136   unfolding Grp_def comp_def by auto
   137 
   138 definition "pick_middlep P Q a c = (SOME b. P a b \<and> Q b c)"
   139 
   140 lemma pick_middlep:
   141   "(P OO Q) a c \<Longrightarrow> P a (pick_middlep P Q a c) \<and> Q (pick_middlep P Q a c) c"
   142   unfolding pick_middlep_def apply(rule someI_ex) by auto
   143 
   144 definition fstOp where
   145   "fstOp P Q ac = (fst ac, pick_middlep P Q (fst ac) (snd ac))"
   146 
   147 definition sndOp where
   148   "sndOp P Q ac = (pick_middlep P Q (fst ac) (snd ac), (snd ac))"
   149 
   150 lemma fstOp_in: "ac \<in> Collect (case_prod (P OO Q)) \<Longrightarrow> fstOp P Q ac \<in> Collect (case_prod P)"
   151   unfolding fstOp_def mem_Collect_eq
   152   by (subst (asm) surjective_pairing, unfold prod.case) (erule pick_middlep[THEN conjunct1])
   153 
   154 lemma fst_fstOp: "fst bc = (fst \<circ> fstOp P Q) bc"
   155   unfolding comp_def fstOp_def by simp
   156 
   157 lemma snd_sndOp: "snd bc = (snd \<circ> sndOp P Q) bc"
   158   unfolding comp_def sndOp_def by simp
   159 
   160 lemma sndOp_in: "ac \<in> Collect (case_prod (P OO Q)) \<Longrightarrow> sndOp P Q ac \<in> Collect (case_prod Q)"
   161   unfolding sndOp_def mem_Collect_eq
   162   by (subst (asm) surjective_pairing, unfold prod.case) (erule pick_middlep[THEN conjunct2])
   163 
   164 lemma csquare_fstOp_sndOp:
   165   "csquare (Collect (f (P OO Q))) snd fst (fstOp P Q) (sndOp P Q)"
   166   unfolding csquare_def fstOp_def sndOp_def using pick_middlep by simp
   167 
   168 lemma snd_fst_flip: "snd xy = (fst \<circ> (%(x, y). (y, x))) xy"
   169   by (simp split: prod.split)
   170 
   171 lemma fst_snd_flip: "fst xy = (snd \<circ> (%(x, y). (y, x))) xy"
   172   by (simp split: prod.split)
   173 
   174 lemma flip_pred: "A \<subseteq> Collect (case_prod (R ^--1)) \<Longrightarrow> (%(x, y). (y, x)) ` A \<subseteq> Collect (case_prod R)"
   175   by auto
   176 
   177 lemma predicate2_eqD: "A = B \<Longrightarrow> A a b \<longleftrightarrow> B a b"
   178   by simp
   179 
   180 lemma case_sum_o_inj: "case_sum f g \<circ> Inl = f" "case_sum f g \<circ> Inr = g"
   181   by auto
   182 
   183 lemma map_sum_o_inj: "map_sum f g o Inl = Inl o f" "map_sum f g o Inr = Inr o g"
   184   by auto
   185 
   186 lemma card_order_csum_cone_cexp_def:
   187   "card_order r \<Longrightarrow> ( |A1| +c cone) ^c r = |Func UNIV (Inl ` A1 \<union> {Inr ()})|"
   188   unfolding cexp_def cone_def Field_csum Field_card_of by (auto dest: Field_card_order)
   189 
   190 lemma If_the_inv_into_in_Func:
   191   "\<lbrakk>inj_on g C; C \<subseteq> B \<union> {x}\<rbrakk> \<Longrightarrow>
   192    (\<lambda>i. if i \<in> g ` C then the_inv_into C g i else x) \<in> Func UNIV (B \<union> {x})"
   193   unfolding Func_def by (auto dest: the_inv_into_into)
   194 
   195 lemma If_the_inv_into_f_f:
   196   "\<lbrakk>i \<in> C; inj_on g C\<rbrakk> \<Longrightarrow> ((\<lambda>i. if i \<in> g ` C then the_inv_into C g i else x) \<circ> g) i = id i"
   197   unfolding Func_def by (auto elim: the_inv_into_f_f)
   198 
   199 lemma the_inv_f_o_f_id: "inj f \<Longrightarrow> (the_inv f \<circ> f) z = id z"
   200   by (simp add: the_inv_f_f)
   201 
   202 lemma vimage2pI: "R (f x) (g y) \<Longrightarrow> vimage2p f g R x y"
   203   unfolding vimage2p_def .
   204 
   205 lemma rel_fun_iff_leq_vimage2p: "(rel_fun R S) f g = (R \<le> vimage2p f g S)"
   206   unfolding rel_fun_def vimage2p_def by auto
   207 
   208 lemma convol_image_vimage2p: "\<langle>f \<circ> fst, g \<circ> snd\<rangle> ` Collect (case_prod (vimage2p f g R)) \<subseteq> Collect (case_prod R)"
   209   unfolding vimage2p_def convol_def by auto
   210 
   211 lemma vimage2p_Grp: "vimage2p f g P = Grp UNIV f OO P OO (Grp UNIV g)\<inverse>\<inverse>"
   212   unfolding vimage2p_def Grp_def by auto
   213 
   214 lemma subst_Pair: "P x y \<Longrightarrow> a = (x, y) \<Longrightarrow> P (fst a) (snd a)"
   215   by simp
   216 
   217 lemma comp_apply_eq: "f (g x) = h (k x) \<Longrightarrow> (f \<circ> g) x = (h \<circ> k) x"
   218   unfolding comp_apply by assumption
   219 
   220 lemma refl_ge_eq: "(\<And>x. R x x) \<Longrightarrow> op = \<le> R"
   221   by auto
   222 
   223 lemma ge_eq_refl: "op = \<le> R \<Longrightarrow> R x x"
   224   by auto
   225 
   226 lemma reflp_eq: "reflp R = (op = \<le> R)"
   227   by (auto simp: reflp_def fun_eq_iff)
   228 
   229 lemma transp_relcompp: "transp r \<longleftrightarrow> r OO r \<le> r"
   230   by (auto simp: transp_def)
   231 
   232 lemma symp_conversep: "symp R = (R\<inverse>\<inverse> \<le> R)"
   233   by (auto simp: symp_def fun_eq_iff)
   234 
   235 lemma diag_imp_eq_le: "(\<And>x. x \<in> A \<Longrightarrow> R x x) \<Longrightarrow> \<forall>x y. x \<in> A \<longrightarrow> y \<in> A \<longrightarrow> x = y \<longrightarrow> R x y"
   236   by blast
   237 
   238 definition eq_onp :: "('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"
   239   where "eq_onp R = (\<lambda>x y. R x \<and> x = y)"
   240 
   241 lemma eq_onp_Grp: "eq_onp P = BNF_Def.Grp (Collect P) id"
   242   unfolding eq_onp_def Grp_def by auto
   243 
   244 lemma eq_onp_to_eq: "eq_onp P x y \<Longrightarrow> x = y"
   245   by (simp add: eq_onp_def)
   246 
   247 lemma eq_onp_top_eq_eq: "eq_onp top = op ="
   248   by (simp add: eq_onp_def)
   249 
   250 lemma eq_onp_same_args: "eq_onp P x x = P x"
   251   by (auto simp add: eq_onp_def)
   252 
   253 lemma eq_onp_eqD: "eq_onp P = Q \<Longrightarrow> P x = Q x x"
   254   unfolding eq_onp_def by blast
   255 
   256 lemma Ball_Collect: "Ball A P = (A \<subseteq> (Collect P))"
   257   by auto
   258 
   259 lemma eq_onp_mono0: "\<forall>x\<in>A. P x \<longrightarrow> Q x \<Longrightarrow> \<forall>x\<in>A. \<forall>y\<in>A. eq_onp P x y \<longrightarrow> eq_onp Q x y"
   260   unfolding eq_onp_def by auto
   261 
   262 lemma eq_onp_True: "eq_onp (\<lambda>_. True) = (op =)"
   263   unfolding eq_onp_def by simp
   264 
   265 lemma Ball_image_comp: "Ball (f ` A) g = Ball A (g o f)"
   266   by auto
   267 
   268 lemma rel_fun_Collect_case_prodD:
   269   "rel_fun A B f g \<Longrightarrow> X \<subseteq> Collect (case_prod A) \<Longrightarrow> x \<in> X \<Longrightarrow> B ((f o fst) x) ((g o snd) x)"
   270   unfolding rel_fun_def by auto
   271 
   272 lemma eq_onp_mono_iff: "eq_onp P \<le> eq_onp Q \<longleftrightarrow> P \<le> Q"
   273   unfolding eq_onp_def by auto
   274 
   275 ML_file "Tools/BNF/bnf_util.ML"
   276 ML_file "Tools/BNF/bnf_tactics.ML"
   277 ML_file "Tools/BNF/bnf_def_tactics.ML"
   278 ML_file "Tools/BNF/bnf_def.ML"
   279 
   280 end