src/HOL/BNF_Def.thy
author traytel
Tue Feb 16 22:28:19 2016 +0100 (2016-02-16)
changeset 62324 ae44f16dcea5
parent 61681 ca53150406c9
child 62329 9f7fa08d2307
permissions -rw-r--r--
make predicator a first-class bnf citizen
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
wenzelm@60758
     9
section \<open>Definition of Bounded Natural Functors\<close>
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
haftmann@61424
    18
lemma Collect_case_prodD: "x \<in> Collect (case_prod A) \<Longrightarrow> A (fst x) (snd x)"
desharna@58104
    19
  by auto
desharna@58104
    20
traytel@58916
    21
inductive
traytel@58916
    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
desharna@58446
    23
where
traytel@58916
    24
  "R1 a c \<Longrightarrow> rel_sum R1 R2 (Inl a) (Inl c)"
traytel@58916
    25
| "R2 b d \<Longrightarrow> rel_sum R1 R2 (Inr b) (Inr d)"
traytel@58916
    26
desharna@58446
    27
definition
blanchet@57398
    28
  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
    29
where
blanchet@57398
    30
  "rel_fun A B = (\<lambda>f g. \<forall>x y. A x y \<longrightarrow> B (f x) (g y))"
blanchet@57398
    31
blanchet@57398
    32
lemma rel_funI [intro]:
blanchet@57398
    33
  assumes "\<And>x y. A x y \<Longrightarrow> B (f x) (g y)"
blanchet@57398
    34
  shows "rel_fun A B f g"
blanchet@57398
    35
  using assms by (simp add: rel_fun_def)
blanchet@57398
    36
blanchet@57398
    37
lemma rel_funD:
blanchet@57398
    38
  assumes "rel_fun A B f g" and "A x y"
blanchet@57398
    39
  shows "B (f x) (g y)"
blanchet@57398
    40
  using assms by (simp add: rel_fun_def)
blanchet@57398
    41
Andreas@59513
    42
lemma rel_fun_mono:
Andreas@59513
    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"
Andreas@59513
    44
by(simp add: rel_fun_def)
Andreas@59513
    45
Andreas@59513
    46
lemma rel_fun_mono' [mono]:
Andreas@59513
    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"
Andreas@59513
    48
by(simp add: rel_fun_def)
Andreas@59513
    49
desharna@58104
    50
definition rel_set :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> bool"
desharna@58104
    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))"
desharna@58104
    52
desharna@58104
    53
lemma rel_setI:
desharna@58104
    54
  assumes "\<And>x. x \<in> A \<Longrightarrow> \<exists>y\<in>B. R x y"
desharna@58104
    55
  assumes "\<And>y. y \<in> B \<Longrightarrow> \<exists>x\<in>A. R x y"
desharna@58104
    56
  shows "rel_set R A B"
desharna@58104
    57
  using assms unfolding rel_set_def by simp
desharna@58104
    58
desharna@58104
    59
lemma predicate2_transferD:
desharna@58104
    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>
desharna@58104
    61
   P (fst a) (fst b) \<longleftrightarrow> Q (snd a) (snd b)"
haftmann@61424
    62
  unfolding rel_fun_def by (blast dest!: Collect_case_prodD)
desharna@58104
    63
blanchet@57398
    64
definition collect where
blanchet@58352
    65
  "collect F x = (\<Union>f \<in> F. f x)"
blanchet@57398
    66
blanchet@57398
    67
lemma fstI: "x = (y, z) \<Longrightarrow> fst x = y"
blanchet@58352
    68
  by simp
blanchet@57398
    69
blanchet@57398
    70
lemma sndI: "x = (y, z) \<Longrightarrow> snd x = z"
blanchet@58352
    71
  by simp
blanchet@57398
    72
blanchet@57398
    73
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
    74
  unfolding bij_def inj_on_def by auto blast
blanchet@57398
    75
blanchet@57398
    76
(* Operator: *)
blanchet@57398
    77
definition "Gr A f = {(a, f a) | a. a \<in> A}"
blanchet@57398
    78
blanchet@57398
    79
definition "Grp A f = (\<lambda>a b. b = f a \<and> a \<in> A)"
blanchet@57398
    80
blanchet@57398
    81
definition vimage2p where
blanchet@57398
    82
  "vimage2p f g R = (\<lambda>x y. R (f x) (g y))"
blanchet@57398
    83
blanchet@56635
    84
lemma collect_comp: "collect F \<circ> g = collect ((\<lambda>f. f \<circ> g) ` F)"
blanchet@55066
    85
  by (rule ext) (auto simp only: comp_apply collect_def)
traytel@51893
    86
wenzelm@57641
    87
definition convol ("\<langle>(_,/ _)\<rangle>") where
blanchet@58352
    88
  "\<langle>f, g\<rangle> \<equiv> \<lambda>a. (f a, g a)"
traytel@49495
    89
blanchet@58352
    90
lemma fst_convol: "fst \<circ> \<langle>f, g\<rangle> = f"
blanchet@58352
    91
  apply(rule ext)
blanchet@58352
    92
  unfolding convol_def by simp
traytel@49495
    93
blanchet@58352
    94
lemma snd_convol: "snd \<circ> \<langle>f, g\<rangle> = g"
blanchet@58352
    95
  apply(rule ext)
blanchet@58352
    96
  unfolding convol_def by simp
traytel@49495
    97
traytel@51893
    98
lemma convol_mem_GrpI:
haftmann@61032
    99
  "x \<in> A \<Longrightarrow> \<langle>id, g\<rangle> x \<in> (Collect (case_prod (Grp A g)))"
blanchet@58352
   100
  unfolding convol_def Grp_def by auto
traytel@51893
   101
blanchet@49312
   102
definition csquare where
blanchet@58352
   103
  "csquare A f1 f2 p1 p2 \<longleftrightarrow> (\<forall> a \<in> A. f1 (p1 a) = f2 (p2 a))"
blanchet@49312
   104
traytel@51893
   105
lemma eq_alt: "op = = Grp UNIV id"
blanchet@58352
   106
  unfolding Grp_def by auto
traytel@51893
   107
traytel@51893
   108
lemma leq_conversepI: "R = op = \<Longrightarrow> R \<le> R^--1"
traytel@51893
   109
  by auto
traytel@51893
   110
traytel@54841
   111
lemma leq_OOI: "R = op = \<Longrightarrow> R \<le> R OO R"
traytel@51893
   112
  by auto
traytel@51893
   113
traytel@53561
   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)"
traytel@53561
   115
  unfolding Grp_def by auto
traytel@53561
   116
traytel@51893
   117
lemma Grp_UNIV_id: "f = id \<Longrightarrow> (Grp UNIV f)^--1 OO Grp UNIV f = Grp UNIV f"
blanchet@58352
   118
  unfolding Grp_def by auto
traytel@51893
   119
traytel@51893
   120
lemma Grp_UNIV_idI: "x = y \<Longrightarrow> Grp UNIV id x y"
blanchet@58352
   121
  unfolding Grp_def by auto
traytel@51893
   122
traytel@51893
   123
lemma Grp_mono: "A \<le> B \<Longrightarrow> Grp A f \<le> Grp B f"
blanchet@58352
   124
  unfolding Grp_def by auto
traytel@51893
   125
traytel@51893
   126
lemma GrpI: "\<lbrakk>f x = y; x \<in> A\<rbrakk> \<Longrightarrow> Grp A f x y"
blanchet@58352
   127
  unfolding Grp_def by auto
traytel@51893
   128
traytel@51893
   129
lemma GrpE: "Grp A f x y \<Longrightarrow> (\<lbrakk>f x = y; x \<in> A\<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R"
blanchet@58352
   130
  unfolding Grp_def by auto
traytel@51893
   131
haftmann@61424
   132
lemma Collect_case_prod_Grp_eqD: "z \<in> Collect (case_prod (Grp A f)) \<Longrightarrow> (f \<circ> fst) z = snd z"
blanchet@58352
   133
  unfolding Grp_def comp_def by auto
traytel@51893
   134
haftmann@61424
   135
lemma Collect_case_prod_Grp_in: "z \<in> Collect (case_prod (Grp A f)) \<Longrightarrow> fst z \<in> A"
blanchet@58352
   136
  unfolding Grp_def comp_def by auto
traytel@51893
   137
traytel@51893
   138
definition "pick_middlep P Q a c = (SOME b. P a b \<and> Q b c)"
traytel@51893
   139
traytel@51893
   140
lemma pick_middlep:
blanchet@58352
   141
  "(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
   142
  unfolding pick_middlep_def apply(rule someI_ex) by auto
blanchet@49312
   143
blanchet@58352
   144
definition fstOp where
blanchet@58352
   145
  "fstOp P Q ac = (fst ac, pick_middlep P Q (fst ac) (snd ac))"
blanchet@58352
   146
blanchet@58352
   147
definition sndOp where
blanchet@58352
   148
  "sndOp P Q ac = (pick_middlep P Q (fst ac) (snd ac), (snd ac))"
traytel@51893
   149
haftmann@61032
   150
lemma fstOp_in: "ac \<in> Collect (case_prod (P OO Q)) \<Longrightarrow> fstOp P Q ac \<in> Collect (case_prod P)"
blanchet@58352
   151
  unfolding fstOp_def mem_Collect_eq
blanchet@58352
   152
  by (subst (asm) surjective_pairing, unfold prod.case) (erule pick_middlep[THEN conjunct1])
blanchet@49312
   153
traytel@51893
   154
lemma fst_fstOp: "fst bc = (fst \<circ> fstOp P Q) bc"
blanchet@58352
   155
  unfolding comp_def fstOp_def by simp
traytel@51893
   156
traytel@51893
   157
lemma snd_sndOp: "snd bc = (snd \<circ> sndOp P Q) bc"
blanchet@58352
   158
  unfolding comp_def sndOp_def by simp
traytel@51893
   159
haftmann@61032
   160
lemma sndOp_in: "ac \<in> Collect (case_prod (P OO Q)) \<Longrightarrow> sndOp P Q ac \<in> Collect (case_prod Q)"
blanchet@58352
   161
  unfolding sndOp_def mem_Collect_eq
blanchet@58352
   162
  by (subst (asm) surjective_pairing, unfold prod.case) (erule pick_middlep[THEN conjunct2])
traytel@51893
   163
traytel@51893
   164
lemma csquare_fstOp_sndOp:
haftmann@61423
   165
  "csquare (Collect (f (P OO Q))) snd fst (fstOp P Q) (sndOp P Q)"
blanchet@58352
   166
  unfolding csquare_def fstOp_def sndOp_def using pick_middlep by simp
traytel@51893
   167
blanchet@56635
   168
lemma snd_fst_flip: "snd xy = (fst \<circ> (%(x, y). (y, x))) xy"
blanchet@58352
   169
  by (simp split: prod.split)
blanchet@49312
   170
blanchet@56635
   171
lemma fst_snd_flip: "fst xy = (snd \<circ> (%(x, y). (y, x))) xy"
blanchet@58352
   172
  by (simp split: prod.split)
blanchet@49312
   173
haftmann@61032
   174
lemma flip_pred: "A \<subseteq> Collect (case_prod (R ^--1)) \<Longrightarrow> (%(x, y). (y, x)) ` A \<subseteq> Collect (case_prod R)"
blanchet@58352
   175
  by auto
traytel@51893
   176
traytel@51917
   177
lemma predicate2_eqD: "A = B \<Longrightarrow> A a b \<longleftrightarrow> B a b"
blanchet@58352
   178
  by simp
blanchet@49537
   179
blanchet@58352
   180
lemma case_sum_o_inj: "case_sum f g \<circ> Inl = f" "case_sum f g \<circ> Inr = g"
blanchet@58352
   181
  by auto
traytel@52635
   182
blanchet@58352
   183
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
   184
  by auto
traytel@57802
   185
traytel@52635
   186
lemma card_order_csum_cone_cexp_def:
traytel@52635
   187
  "card_order r \<Longrightarrow> ( |A1| +c cone) ^c r = |Func UNIV (Inl ` A1 \<union> {Inr ()})|"
traytel@52635
   188
  unfolding cexp_def cone_def Field_csum Field_card_of by (auto dest: Field_card_order)
traytel@52635
   189
traytel@52635
   190
lemma If_the_inv_into_in_Func:
traytel@52635
   191
  "\<lbrakk>inj_on g C; C \<subseteq> B \<union> {x}\<rbrakk> \<Longrightarrow>
blanchet@58352
   192
   (\<lambda>i. if i \<in> g ` C then the_inv_into C g i else x) \<in> Func UNIV (B \<union> {x})"
blanchet@58352
   193
  unfolding Func_def by (auto dest: the_inv_into_into)
traytel@52635
   194
traytel@52635
   195
lemma If_the_inv_into_f_f:
blanchet@58352
   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"
blanchet@58352
   197
  unfolding Func_def by (auto elim: the_inv_into_f_f)
traytel@52635
   198
blanchet@56635
   199
lemma the_inv_f_o_f_id: "inj f \<Longrightarrow> (the_inv f \<circ> f) z = id z"
blanchet@56635
   200
  by (simp add: the_inv_f_f)
blanchet@56635
   201
traytel@52731
   202
lemma vimage2pI: "R (f x) (g y) \<Longrightarrow> vimage2p f g R x y"
traytel@52731
   203
  unfolding vimage2p_def by -
traytel@52719
   204
blanchet@55945
   205
lemma rel_fun_iff_leq_vimage2p: "(rel_fun R S) f g = (R \<le> vimage2p f g S)"
blanchet@55945
   206
  unfolding rel_fun_def vimage2p_def by auto
traytel@52719
   207
haftmann@61032
   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)"
traytel@52731
   209
  unfolding vimage2p_def convol_def by auto
traytel@52719
   210
traytel@54961
   211
lemma vimage2p_Grp: "vimage2p f g P = Grp UNIV f OO P OO (Grp UNIV g)\<inverse>\<inverse>"
traytel@54961
   212
  unfolding vimage2p_def Grp_def by auto
traytel@54961
   213
desharna@58106
   214
lemma subst_Pair: "P x y \<Longrightarrow> a = (x, y) \<Longrightarrow> P (fst a) (snd a)"
desharna@58106
   215
  by simp
desharna@58106
   216
blanchet@58352
   217
lemma comp_apply_eq: "f (g x) = h (k x) \<Longrightarrow> (f \<circ> g) x = (h \<circ> k) x"
blanchet@58352
   218
  unfolding comp_apply by assumption
blanchet@58352
   219
traytel@59726
   220
lemma refl_ge_eq: "(\<And>x. R x x) \<Longrightarrow> op = \<le> R"
traytel@59726
   221
  by auto
traytel@59726
   222
traytel@59726
   223
lemma ge_eq_refl: "op = \<le> R \<Longrightarrow> R x x"
traytel@59726
   224
  by auto
traytel@59726
   225
traytel@61240
   226
lemma reflp_eq: "reflp R = (op = \<le> R)"
traytel@61240
   227
  by (auto simp: reflp_def fun_eq_iff)
traytel@61240
   228
traytel@61240
   229
lemma transp_relcompp: "transp r \<longleftrightarrow> r OO r \<le> r"
traytel@61240
   230
  by (auto simp: transp_def)
traytel@61240
   231
traytel@61240
   232
lemma symp_conversep: "symp R = (R\<inverse>\<inverse> \<le> R)"
traytel@61240
   233
  by (auto simp: symp_def fun_eq_iff)
traytel@61240
   234
traytel@61240
   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"
traytel@61240
   236
  by blast
traytel@61240
   237
traytel@62324
   238
definition eq_onp :: "('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"
traytel@62324
   239
  where "eq_onp R = (\<lambda>x y. R x \<and> x = y)"
traytel@62324
   240
traytel@62324
   241
lemma eq_onp_Grp: "eq_onp P = BNF_Def.Grp (Collect P) id"
traytel@62324
   242
  unfolding eq_onp_def Grp_def by auto
traytel@62324
   243
traytel@62324
   244
lemma eq_onp_to_eq: "eq_onp P x y \<Longrightarrow> x = y"
traytel@62324
   245
  by (simp add: eq_onp_def)
traytel@62324
   246
traytel@62324
   247
lemma eq_onp_top_eq_eq: "eq_onp top = op ="
traytel@62324
   248
  by (simp add: eq_onp_def)
traytel@62324
   249
traytel@62324
   250
lemma eq_onp_same_args: "eq_onp P x x = P x"
traytel@62324
   251
  using assms by (auto simp add: eq_onp_def)
traytel@62324
   252
traytel@62324
   253
lemma eq_onp_eqD: "eq_onp P = Q \<Longrightarrow> P x = Q x x"
traytel@62324
   254
  unfolding eq_onp_def by blast
traytel@62324
   255
traytel@62324
   256
lemma Ball_Collect: "Ball A P = (A \<subseteq> (Collect P))"
traytel@62324
   257
  by auto
traytel@62324
   258
traytel@62324
   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"
traytel@62324
   260
  unfolding eq_onp_def by auto
traytel@62324
   261
traytel@62324
   262
lemma eq_onp_True: "eq_onp (\<lambda>_. True) = (op =)"
traytel@62324
   263
  unfolding eq_onp_def by simp
traytel@62324
   264
traytel@62324
   265
lemma Ball_image_comp: "Ball (f ` A) g = Ball A (g o f)"
traytel@62324
   266
  by auto
traytel@62324
   267
blanchet@57398
   268
ML_file "Tools/BNF/bnf_util.ML"
blanchet@57398
   269
ML_file "Tools/BNF/bnf_tactics.ML"
blanchet@55062
   270
ML_file "Tools/BNF/bnf_def_tactics.ML"
blanchet@55062
   271
ML_file "Tools/BNF/bnf_def.ML"
blanchet@49309
   272
blanchet@48975
   273
end