src/HOL/BNF_Def.thy
author blanchet
Tue Sep 16 19:23:37 2014 +0200 (2014-09-16)
changeset 58352 37745650a3f4
parent 58106 c8cba801c483
child 58446 e89f57d1e46c
permissions -rw-r--r--
register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet@55059
     1
(*  Title:      HOL/BNF_Def.thy
blanchet@48975
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@57398
     3
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@57698
     4
    Copyright   2012, 2013, 2014
blanchet@48975
     5
blanchet@48975
     6
Definition of bounded natural functors.
blanchet@48975
     7
*)
blanchet@48975
     8
blanchet@48975
     9
header {* Definition of Bounded Natural Functors *}
blanchet@48975
    10
blanchet@48975
    11
theory BNF_Def
blanchet@57398
    12
imports BNF_Cardinal_Arithmetic Fun_Def_Base
blanchet@48975
    13
keywords
blanchet@49286
    14
  "print_bnfs" :: diag and
blanchet@51836
    15
  "bnf" :: thy_goal
blanchet@48975
    16
begin
blanchet@48975
    17
desharna@58104
    18
lemma Collect_splitD: "x \<in> Collect (split A) \<Longrightarrow> A (fst x) (snd x)"
desharna@58104
    19
  by auto
desharna@58104
    20
blanchet@57398
    21
definition
blanchet@57398
    22
  rel_fun :: "('a \<Rightarrow> 'c \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> 'd \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('c \<Rightarrow> 'd) \<Rightarrow> bool"
blanchet@57398
    23
where
blanchet@57398
    24
  "rel_fun A B = (\<lambda>f g. \<forall>x y. A x y \<longrightarrow> B (f x) (g y))"
blanchet@57398
    25
blanchet@57398
    26
lemma rel_funI [intro]:
blanchet@57398
    27
  assumes "\<And>x y. A x y \<Longrightarrow> B (f x) (g y)"
blanchet@57398
    28
  shows "rel_fun A B f g"
blanchet@57398
    29
  using assms by (simp add: rel_fun_def)
blanchet@57398
    30
blanchet@57398
    31
lemma rel_funD:
blanchet@57398
    32
  assumes "rel_fun A B f g" and "A x y"
blanchet@57398
    33
  shows "B (f x) (g y)"
blanchet@57398
    34
  using assms by (simp add: rel_fun_def)
blanchet@57398
    35
desharna@58104
    36
definition rel_set :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> bool"
desharna@58104
    37
  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))"
desharna@58104
    38
desharna@58104
    39
lemma rel_setI:
desharna@58104
    40
  assumes "\<And>x. x \<in> A \<Longrightarrow> \<exists>y\<in>B. R x y"
desharna@58104
    41
  assumes "\<And>y. y \<in> B \<Longrightarrow> \<exists>x\<in>A. R x y"
desharna@58104
    42
  shows "rel_set R A B"
desharna@58104
    43
  using assms unfolding rel_set_def by simp
desharna@58104
    44
desharna@58104
    45
lemma predicate2_transferD:
desharna@58104
    46
   "\<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>
desharna@58104
    47
   P (fst a) (fst b) \<longleftrightarrow> Q (snd a) (snd b)"
desharna@58104
    48
  unfolding rel_fun_def by (blast dest!: Collect_splitD)
desharna@58104
    49
blanchet@57398
    50
definition collect where
blanchet@58352
    51
  "collect F x = (\<Union>f \<in> F. f x)"
blanchet@57398
    52
blanchet@57398
    53
lemma fstI: "x = (y, z) \<Longrightarrow> fst x = y"
blanchet@58352
    54
  by simp
blanchet@57398
    55
blanchet@57398
    56
lemma sndI: "x = (y, z) \<Longrightarrow> snd x = z"
blanchet@58352
    57
  by simp
blanchet@57398
    58
blanchet@57398
    59
lemma bijI': "\<lbrakk>\<And>x y. (f x = f y) = (x = y); \<And>y. \<exists>x. y = f x\<rbrakk> \<Longrightarrow> bij f"
blanchet@58352
    60
  unfolding bij_def inj_on_def by auto blast
blanchet@57398
    61
blanchet@57398
    62
(* Operator: *)
blanchet@57398
    63
definition "Gr A f = {(a, f a) | a. a \<in> A}"
blanchet@57398
    64
blanchet@57398
    65
definition "Grp A f = (\<lambda>a b. b = f a \<and> a \<in> A)"
blanchet@57398
    66
blanchet@57398
    67
definition vimage2p where
blanchet@57398
    68
  "vimage2p f g R = (\<lambda>x y. R (f x) (g y))"
blanchet@57398
    69
blanchet@56635
    70
lemma collect_comp: "collect F \<circ> g = collect ((\<lambda>f. f \<circ> g) ` F)"
blanchet@55066
    71
  by (rule ext) (auto simp only: comp_apply collect_def)
traytel@51893
    72
wenzelm@57641
    73
definition convol ("\<langle>(_,/ _)\<rangle>") where
blanchet@58352
    74
  "\<langle>f, g\<rangle> \<equiv> \<lambda>a. (f a, g a)"
traytel@49495
    75
blanchet@58352
    76
lemma fst_convol: "fst \<circ> \<langle>f, g\<rangle> = f"
blanchet@58352
    77
  apply(rule ext)
blanchet@58352
    78
  unfolding convol_def by simp
traytel@49495
    79
blanchet@58352
    80
lemma snd_convol: "snd \<circ> \<langle>f, g\<rangle> = g"
blanchet@58352
    81
  apply(rule ext)
blanchet@58352
    82
  unfolding convol_def by simp
traytel@49495
    83
traytel@51893
    84
lemma convol_mem_GrpI:
blanchet@58352
    85
  "x \<in> A \<Longrightarrow> \<langle>id, g\<rangle> x \<in> (Collect (split (Grp A g)))"
blanchet@58352
    86
  unfolding convol_def Grp_def by auto
traytel@51893
    87
blanchet@49312
    88
definition csquare where
blanchet@58352
    89
  "csquare A f1 f2 p1 p2 \<longleftrightarrow> (\<forall> a \<in> A. f1 (p1 a) = f2 (p2 a))"
blanchet@49312
    90
traytel@51893
    91
lemma eq_alt: "op = = Grp UNIV id"
blanchet@58352
    92
  unfolding Grp_def by auto
traytel@51893
    93
traytel@51893
    94
lemma leq_conversepI: "R = op = \<Longrightarrow> R \<le> R^--1"
traytel@51893
    95
  by auto
traytel@51893
    96
traytel@54841
    97
lemma leq_OOI: "R = op = \<Longrightarrow> R \<le> R OO R"
traytel@51893
    98
  by auto
traytel@51893
    99
traytel@53561
   100
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)"
traytel@53561
   101
  unfolding Grp_def by auto
traytel@53561
   102
traytel@51893
   103
lemma Grp_UNIV_id: "f = id \<Longrightarrow> (Grp UNIV f)^--1 OO Grp UNIV f = Grp UNIV f"
blanchet@58352
   104
  unfolding Grp_def by auto
traytel@51893
   105
traytel@51893
   106
lemma Grp_UNIV_idI: "x = y \<Longrightarrow> Grp UNIV id x y"
blanchet@58352
   107
  unfolding Grp_def by auto
traytel@51893
   108
traytel@51893
   109
lemma Grp_mono: "A \<le> B \<Longrightarrow> Grp A f \<le> Grp B f"
blanchet@58352
   110
  unfolding Grp_def by auto
traytel@51893
   111
traytel@51893
   112
lemma GrpI: "\<lbrakk>f x = y; x \<in> A\<rbrakk> \<Longrightarrow> Grp A f x y"
blanchet@58352
   113
  unfolding Grp_def by auto
traytel@51893
   114
traytel@51893
   115
lemma GrpE: "Grp A f x y \<Longrightarrow> (\<lbrakk>f x = y; x \<in> A\<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R"
blanchet@58352
   116
  unfolding Grp_def by auto
traytel@51893
   117
traytel@51893
   118
lemma Collect_split_Grp_eqD: "z \<in> Collect (split (Grp A f)) \<Longrightarrow> (f \<circ> fst) z = snd z"
blanchet@58352
   119
  unfolding Grp_def comp_def by auto
traytel@51893
   120
traytel@51893
   121
lemma Collect_split_Grp_inD: "z \<in> Collect (split (Grp A f)) \<Longrightarrow> fst z \<in> A"
blanchet@58352
   122
  unfolding Grp_def comp_def by auto
traytel@51893
   123
traytel@51893
   124
definition "pick_middlep P Q a c = (SOME b. P a b \<and> Q b c)"
traytel@51893
   125
traytel@51893
   126
lemma pick_middlep:
blanchet@58352
   127
  "(P OO Q) a c \<Longrightarrow> P a (pick_middlep P Q a c) \<and> Q (pick_middlep P Q a c) c"
blanchet@58352
   128
  unfolding pick_middlep_def apply(rule someI_ex) by auto
blanchet@49312
   129
blanchet@58352
   130
definition fstOp where
blanchet@58352
   131
  "fstOp P Q ac = (fst ac, pick_middlep P Q (fst ac) (snd ac))"
blanchet@58352
   132
blanchet@58352
   133
definition sndOp where
blanchet@58352
   134
  "sndOp P Q ac = (pick_middlep P Q (fst ac) (snd ac), (snd ac))"
traytel@51893
   135
traytel@51893
   136
lemma fstOp_in: "ac \<in> Collect (split (P OO Q)) \<Longrightarrow> fstOp P Q ac \<in> Collect (split P)"
blanchet@58352
   137
  unfolding fstOp_def mem_Collect_eq
blanchet@58352
   138
  by (subst (asm) surjective_pairing, unfold prod.case) (erule pick_middlep[THEN conjunct1])
blanchet@49312
   139
traytel@51893
   140
lemma fst_fstOp: "fst bc = (fst \<circ> fstOp P Q) bc"
blanchet@58352
   141
  unfolding comp_def fstOp_def by simp
traytel@51893
   142
traytel@51893
   143
lemma snd_sndOp: "snd bc = (snd \<circ> sndOp P Q) bc"
blanchet@58352
   144
  unfolding comp_def sndOp_def by simp
traytel@51893
   145
traytel@51893
   146
lemma sndOp_in: "ac \<in> Collect (split (P OO Q)) \<Longrightarrow> sndOp P Q ac \<in> Collect (split Q)"
blanchet@58352
   147
  unfolding sndOp_def mem_Collect_eq
blanchet@58352
   148
  by (subst (asm) surjective_pairing, unfold prod.case) (erule pick_middlep[THEN conjunct2])
traytel@51893
   149
traytel@51893
   150
lemma csquare_fstOp_sndOp:
blanchet@58352
   151
  "csquare (Collect (split (P OO Q))) snd fst (fstOp P Q) (sndOp P Q)"
blanchet@58352
   152
  unfolding csquare_def fstOp_def sndOp_def using pick_middlep by simp
traytel@51893
   153
blanchet@56635
   154
lemma snd_fst_flip: "snd xy = (fst \<circ> (%(x, y). (y, x))) xy"
blanchet@58352
   155
  by (simp split: prod.split)
blanchet@49312
   156
blanchet@56635
   157
lemma fst_snd_flip: "fst xy = (snd \<circ> (%(x, y). (y, x))) xy"
blanchet@58352
   158
  by (simp split: prod.split)
blanchet@49312
   159
traytel@51893
   160
lemma flip_pred: "A \<subseteq> Collect (split (R ^--1)) \<Longrightarrow> (%(x, y). (y, x)) ` A \<subseteq> Collect (split R)"
blanchet@58352
   161
  by auto
traytel@51893
   162
traytel@51893
   163
lemma Collect_split_mono: "A \<le> B \<Longrightarrow> Collect (split A) \<subseteq> Collect (split B)"
traytel@51893
   164
  by auto
traytel@51893
   165
traytel@51916
   166
lemma Collect_split_mono_strong: 
traytel@55163
   167
  "\<lbrakk>X = fst ` A; Y = snd ` A; \<forall>a\<in>X. \<forall>b \<in> Y. P a b \<longrightarrow> Q a b; A \<subseteq> Collect (split P)\<rbrakk> \<Longrightarrow>
blanchet@58352
   168
   A \<subseteq> Collect (split Q)"
traytel@51916
   169
  by fastforce
traytel@51916
   170
traytel@55163
   171
traytel@51917
   172
lemma predicate2_eqD: "A = B \<Longrightarrow> A a b \<longleftrightarrow> B a b"
blanchet@58352
   173
  by simp
blanchet@49537
   174
blanchet@58352
   175
lemma case_sum_o_inj: "case_sum f g \<circ> Inl = f" "case_sum f g \<circ> Inr = g"
blanchet@58352
   176
  by auto
traytel@52635
   177
blanchet@58352
   178
lemma map_sum_o_inj: "map_sum f g o Inl = Inl o f" "map_sum f g o Inr = Inr o g"
blanchet@58352
   179
  by auto
traytel@57802
   180
traytel@52635
   181
lemma card_order_csum_cone_cexp_def:
traytel@52635
   182
  "card_order r \<Longrightarrow> ( |A1| +c cone) ^c r = |Func UNIV (Inl ` A1 \<union> {Inr ()})|"
traytel@52635
   183
  unfolding cexp_def cone_def Field_csum Field_card_of by (auto dest: Field_card_order)
traytel@52635
   184
traytel@52635
   185
lemma If_the_inv_into_in_Func:
traytel@52635
   186
  "\<lbrakk>inj_on g C; C \<subseteq> B \<union> {x}\<rbrakk> \<Longrightarrow>
blanchet@58352
   187
   (\<lambda>i. if i \<in> g ` C then the_inv_into C g i else x) \<in> Func UNIV (B \<union> {x})"
blanchet@58352
   188
  unfolding Func_def by (auto dest: the_inv_into_into)
traytel@52635
   189
traytel@52635
   190
lemma If_the_inv_into_f_f:
blanchet@58352
   191
  "\<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"
blanchet@58352
   192
  unfolding Func_def by (auto elim: the_inv_into_f_f)
traytel@52635
   193
blanchet@56635
   194
lemma the_inv_f_o_f_id: "inj f \<Longrightarrow> (the_inv f \<circ> f) z = id z"
blanchet@56635
   195
  by (simp add: the_inv_f_f)
blanchet@56635
   196
traytel@52731
   197
lemma vimage2pI: "R (f x) (g y) \<Longrightarrow> vimage2p f g R x y"
traytel@52731
   198
  unfolding vimage2p_def by -
traytel@52719
   199
blanchet@55945
   200
lemma rel_fun_iff_leq_vimage2p: "(rel_fun R S) f g = (R \<le> vimage2p f g S)"
blanchet@55945
   201
  unfolding rel_fun_def vimage2p_def by auto
traytel@52719
   202
wenzelm@57641
   203
lemma convol_image_vimage2p: "\<langle>f \<circ> fst, g \<circ> snd\<rangle> ` Collect (split (vimage2p f g R)) \<subseteq> Collect (split R)"
traytel@52731
   204
  unfolding vimage2p_def convol_def by auto
traytel@52719
   205
traytel@54961
   206
lemma vimage2p_Grp: "vimage2p f g P = Grp UNIV f OO P OO (Grp UNIV g)\<inverse>\<inverse>"
traytel@54961
   207
  unfolding vimage2p_def Grp_def by auto
traytel@54961
   208
desharna@58106
   209
lemma subst_Pair: "P x y \<Longrightarrow> a = (x, y) \<Longrightarrow> P (fst a) (snd a)"
desharna@58106
   210
  by simp
desharna@58106
   211
blanchet@58352
   212
lemma comp_apply_eq: "f (g x) = h (k x) \<Longrightarrow> (f \<circ> g) x = (h \<circ> k) x"
blanchet@58352
   213
  unfolding comp_apply by assumption
blanchet@58352
   214
blanchet@57398
   215
ML_file "Tools/BNF/bnf_util.ML"
blanchet@57398
   216
ML_file "Tools/BNF/bnf_tactics.ML"
blanchet@55062
   217
ML_file "Tools/BNF/bnf_def_tactics.ML"
blanchet@55062
   218
ML_file "Tools/BNF/bnf_def.ML"
blanchet@49309
   219
blanchet@48975
   220
end