src/HOL/BNF_GFP.thy
author traytel
Wed Feb 19 10:21:02 2014 +0100 (2014-02-19)
changeset 55578 32774e40afb0
parent 55542 4394bb0b522b
child 55644 b657146dc030
permissions -rw-r--r--
removed not anymore used constants and theorems
blanchet@55059
     1
(*  Title:      HOL/BNF_GFP.thy
blanchet@48975
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@55059
     3
    Author:     Lorenz Panny, TU Muenchen
blanchet@55059
     4
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@55059
     5
    Copyright   2012, 2013
blanchet@48975
     6
blanchet@48975
     7
Greatest fixed point operation on bounded natural functors.
blanchet@48975
     8
*)
blanchet@48975
     9
blanchet@48975
    10
header {* Greatest Fixed Point Operation on Bounded Natural Functors *}
blanchet@48975
    11
blanchet@48975
    12
theory BNF_GFP
traytel@55578
    13
imports BNF_FP_Base String
blanchet@48975
    14
keywords
blanchet@53310
    15
  "codatatype" :: thy_decl and
panny@53822
    16
  "primcorecursive" :: thy_goal and
panny@53822
    17
  "primcorec" :: thy_decl
blanchet@48975
    18
begin
blanchet@48975
    19
blanchet@55024
    20
setup {*
blanchet@55024
    21
Sign.const_alias @{binding proj} @{const_name Equiv_Relations.proj}
blanchet@55024
    22
*}
blanchet@55024
    23
blanchet@54485
    24
lemma not_TrueE: "\<not> True \<Longrightarrow> P"
blanchet@54485
    25
by (erule notE, rule TrueI)
blanchet@54485
    26
blanchet@54485
    27
lemma neq_eq_eq_contradict: "\<lbrakk>t \<noteq> u; s = t; s = u\<rbrakk> \<Longrightarrow> P"
blanchet@54485
    28
by fast
blanchet@54485
    29
blanchet@55414
    30
lemma case_sum_expand_Inr: "f o Inl = g \<Longrightarrow> f x = case_sum g (f o Inr) x"
blanchet@49312
    31
by (auto split: sum.splits)
blanchet@49312
    32
blanchet@55414
    33
lemma case_sum_expand_Inr': "f o Inl = g \<Longrightarrow> h = f o Inr \<longleftrightarrow> case_sum g h = f"
blanchet@54488
    34
apply rule
blanchet@54488
    35
 apply (rule ext, force split: sum.split)
blanchet@55414
    36
by (rule ext, metis case_sum_o_inj(2))
traytel@51739
    37
blanchet@49312
    38
lemma converse_Times: "(A \<times> B) ^-1 = B \<times> A"
blanchet@54488
    39
by fast
blanchet@49312
    40
blanchet@49312
    41
lemma equiv_proj:
blanchet@49312
    42
  assumes e: "equiv A R" and "z \<in> R"
blanchet@49312
    43
  shows "(proj R o fst) z = (proj R o snd) z"
blanchet@49312
    44
proof -
blanchet@49312
    45
  from assms(2) have z: "(fst z, snd z) \<in> R" by auto
traytel@53695
    46
  with e have "\<And>x. (fst z, x) \<in> R \<Longrightarrow> (snd z, x) \<in> R" "\<And>x. (snd z, x) \<in> R \<Longrightarrow> (fst z, x) \<in> R"
traytel@53695
    47
    unfolding equiv_def sym_def trans_def by blast+
traytel@53695
    48
  then show ?thesis unfolding proj_def[abs_def] by auto
blanchet@49312
    49
qed
blanchet@49312
    50
blanchet@49312
    51
(* Operators: *)
blanchet@49312
    52
definition image2 where "image2 A f g = {(f a, g a) | a. a \<in> A}"
blanchet@49312
    53
traytel@51447
    54
lemma Id_on_Gr: "Id_on A = Gr A id"
traytel@51447
    55
unfolding Id_on_def Gr_def by auto
blanchet@49312
    56
blanchet@49312
    57
lemma image2_eqI: "\<lbrakk>b = f x; c = g x; x \<in> A\<rbrakk> \<Longrightarrow> (b, c) \<in> image2 A f g"
blanchet@49312
    58
unfolding image2_def by auto
blanchet@49312
    59
blanchet@49312
    60
lemma IdD: "(a, b) \<in> Id \<Longrightarrow> a = b"
blanchet@49312
    61
by auto
blanchet@49312
    62
blanchet@49312
    63
lemma image2_Gr: "image2 A f g = (Gr A f)^-1 O (Gr A g)"
blanchet@49312
    64
unfolding image2_def Gr_def by auto
blanchet@49312
    65
blanchet@49312
    66
lemma GrD1: "(x, fx) \<in> Gr A f \<Longrightarrow> x \<in> A"
blanchet@49312
    67
unfolding Gr_def by simp
blanchet@49312
    68
blanchet@49312
    69
lemma GrD2: "(x, fx) \<in> Gr A f \<Longrightarrow> f x = fx"
blanchet@49312
    70
unfolding Gr_def by simp
blanchet@49312
    71
blanchet@49312
    72
lemma Gr_incl: "Gr A f \<subseteq> A <*> B \<longleftrightarrow> f ` A \<subseteq> B"
blanchet@49312
    73
unfolding Gr_def by auto
blanchet@49312
    74
blanchet@54485
    75
lemma subset_Collect_iff: "B \<subseteq> A \<Longrightarrow> (B \<subseteq> {x \<in> A. P x}) = (\<forall>x \<in> B. P x)"
blanchet@54485
    76
by blast
blanchet@54485
    77
blanchet@54485
    78
lemma subset_CollectI: "B \<subseteq> A \<Longrightarrow> (\<And>x. x \<in> B \<Longrightarrow> Q x \<Longrightarrow> P x) \<Longrightarrow> ({x \<in> B. Q x} \<subseteq> {x \<in> A. P x})"
blanchet@54485
    79
by blast
blanchet@54485
    80
traytel@51893
    81
lemma in_rel_Collect_split_eq: "in_rel (Collect (split X)) = X"
traytel@51893
    82
unfolding fun_eq_iff by auto
traytel@51893
    83
traytel@51893
    84
lemma Collect_split_in_rel_leI: "X \<subseteq> Y \<Longrightarrow> X \<subseteq> Collect (split (in_rel Y))"
traytel@51893
    85
by auto
traytel@51893
    86
traytel@51893
    87
lemma Collect_split_in_rel_leE: "X \<subseteq> Collect (split (in_rel Y)) \<Longrightarrow> (X \<subseteq> Y \<Longrightarrow> R) \<Longrightarrow> R"
traytel@51893
    88
by force
traytel@51893
    89
traytel@51893
    90
lemma conversep_in_rel: "(in_rel R)\<inverse>\<inverse> = in_rel (R\<inverse>)"
traytel@51893
    91
unfolding fun_eq_iff by auto
traytel@51893
    92
traytel@51893
    93
lemma relcompp_in_rel: "in_rel R OO in_rel S = in_rel (R O S)"
traytel@51893
    94
unfolding fun_eq_iff by auto
traytel@51893
    95
traytel@51893
    96
lemma in_rel_Gr: "in_rel (Gr A f) = Grp A f"
traytel@51893
    97
unfolding Gr_def Grp_def fun_eq_iff by auto
traytel@51893
    98
blanchet@49312
    99
definition relImage where
blanchet@49312
   100
"relImage R f \<equiv> {(f a1, f a2) | a1 a2. (a1,a2) \<in> R}"
blanchet@49312
   101
blanchet@49312
   102
definition relInvImage where
blanchet@49312
   103
"relInvImage A R f \<equiv> {(a1, a2) | a1 a2. a1 \<in> A \<and> a2 \<in> A \<and> (f a1, f a2) \<in> R}"
blanchet@49312
   104
blanchet@49312
   105
lemma relImage_Gr:
blanchet@49312
   106
"\<lbrakk>R \<subseteq> A \<times> A\<rbrakk> \<Longrightarrow> relImage R f = (Gr A f)^-1 O R O Gr A f"
blanchet@49312
   107
unfolding relImage_def Gr_def relcomp_def by auto
blanchet@49312
   108
blanchet@49312
   109
lemma relInvImage_Gr: "\<lbrakk>R \<subseteq> B \<times> B\<rbrakk> \<Longrightarrow> relInvImage A R f = Gr A f O R O (Gr A f)^-1"
blanchet@49312
   110
unfolding Gr_def relcomp_def image_def relInvImage_def by auto
blanchet@49312
   111
blanchet@49312
   112
lemma relImage_mono:
blanchet@49312
   113
"R1 \<subseteq> R2 \<Longrightarrow> relImage R1 f \<subseteq> relImage R2 f"
blanchet@49312
   114
unfolding relImage_def by auto
blanchet@49312
   115
blanchet@49312
   116
lemma relInvImage_mono:
blanchet@49312
   117
"R1 \<subseteq> R2 \<Longrightarrow> relInvImage A R1 f \<subseteq> relInvImage A R2 f"
blanchet@49312
   118
unfolding relInvImage_def by auto
blanchet@49312
   119
traytel@51447
   120
lemma relInvImage_Id_on:
traytel@51447
   121
"(\<And>a1 a2. f a1 = f a2 \<longleftrightarrow> a1 = a2) \<Longrightarrow> relInvImage A (Id_on B) f \<subseteq> Id"
traytel@51447
   122
unfolding relInvImage_def Id_on_def by auto
blanchet@49312
   123
blanchet@49312
   124
lemma relInvImage_UNIV_relImage:
blanchet@49312
   125
"R \<subseteq> relInvImage UNIV (relImage R f) f"
blanchet@49312
   126
unfolding relInvImage_def relImage_def by auto
blanchet@49312
   127
blanchet@49312
   128
lemma relImage_proj:
blanchet@49312
   129
assumes "equiv A R"
traytel@51447
   130
shows "relImage R (proj R) \<subseteq> Id_on (A//R)"
traytel@51447
   131
unfolding relImage_def Id_on_def
traytel@51447
   132
using proj_iff[OF assms] equiv_class_eq_iff[OF assms]
traytel@51447
   133
by (auto simp: proj_preserves)
blanchet@49312
   134
blanchet@49312
   135
lemma relImage_relInvImage:
blanchet@49312
   136
assumes "R \<subseteq> f ` A <*> f ` A"
blanchet@49312
   137
shows "relImage (relInvImage A R f) f = R"
blanchet@54488
   138
using assms unfolding relImage_def relInvImage_def by fast
blanchet@49312
   139
blanchet@49312
   140
lemma subst_Pair: "P x y \<Longrightarrow> a = (x, y) \<Longrightarrow> P (fst a) (snd a)"
blanchet@49312
   141
by simp
blanchet@49312
   142
blanchet@49312
   143
lemma fst_diag_id: "(fst \<circ> (%x. (x, x))) z = id z"
blanchet@49312
   144
by simp
blanchet@49312
   145
blanchet@49312
   146
lemma snd_diag_id: "(snd \<circ> (%x. (x, x))) z = id z"
blanchet@49312
   147
by simp
blanchet@49312
   148
blanchet@49312
   149
lemma image_convolD: "\<lbrakk>(a, b) \<in> <f, g> ` X\<rbrakk> \<Longrightarrow> \<exists>x. x \<in> X \<and> a = f x \<and> b = g x"
blanchet@49312
   150
unfolding convol_def by auto
blanchet@49312
   151
blanchet@49312
   152
definition Succ where "Succ Kl kl = {k . kl @ [k] \<in> Kl}"
blanchet@49312
   153
definition Shift where "Shift Kl k = {kl. k # kl \<in> Kl}"
blanchet@49312
   154
definition shift where "shift lab k = (\<lambda>kl. lab (k # kl))"
blanchet@49312
   155
blanchet@49312
   156
lemma empty_Shift: "\<lbrakk>[] \<in> Kl; k \<in> Succ Kl []\<rbrakk> \<Longrightarrow> [] \<in> Shift Kl k"
blanchet@49312
   157
unfolding Shift_def Succ_def by simp
blanchet@49312
   158
blanchet@49312
   159
lemma SuccD: "k \<in> Succ Kl kl \<Longrightarrow> kl @ [k] \<in> Kl"
blanchet@49312
   160
unfolding Succ_def by simp
blanchet@49312
   161
blanchet@49312
   162
lemmas SuccE = SuccD[elim_format]
blanchet@49312
   163
blanchet@49312
   164
lemma SuccI: "kl @ [k] \<in> Kl \<Longrightarrow> k \<in> Succ Kl kl"
blanchet@49312
   165
unfolding Succ_def by simp
blanchet@49312
   166
blanchet@49312
   167
lemma ShiftD: "kl \<in> Shift Kl k \<Longrightarrow> k # kl \<in> Kl"
blanchet@49312
   168
unfolding Shift_def by simp
blanchet@49312
   169
blanchet@49312
   170
lemma Succ_Shift: "Succ (Shift Kl k) kl = Succ Kl (k # kl)"
blanchet@49312
   171
unfolding Succ_def Shift_def by auto
blanchet@49312
   172
blanchet@49312
   173
lemma length_Cons: "length (x # xs) = Suc (length xs)"
blanchet@49312
   174
by simp
blanchet@49312
   175
blanchet@49312
   176
lemma length_append_singleton: "length (xs @ [x]) = Suc (length xs)"
blanchet@49312
   177
by simp
blanchet@49312
   178
blanchet@49312
   179
(*injection into the field of a cardinal*)
blanchet@49312
   180
definition "toCard_pred A r f \<equiv> inj_on f A \<and> f ` A \<subseteq> Field r \<and> Card_order r"
blanchet@49312
   181
definition "toCard A r \<equiv> SOME f. toCard_pred A r f"
blanchet@49312
   182
blanchet@49312
   183
lemma ex_toCard_pred:
blanchet@49312
   184
"\<lbrakk>|A| \<le>o r; Card_order r\<rbrakk> \<Longrightarrow> \<exists> f. toCard_pred A r f"
blanchet@49312
   185
unfolding toCard_pred_def
blanchet@49312
   186
using card_of_ordLeq[of A "Field r"]
blanchet@49312
   187
      ordLeq_ordIso_trans[OF _ card_of_unique[of "Field r" r], of "|A|"]
blanchet@49312
   188
by blast
blanchet@49312
   189
blanchet@49312
   190
lemma toCard_pred_toCard:
blanchet@49312
   191
  "\<lbrakk>|A| \<le>o r; Card_order r\<rbrakk> \<Longrightarrow> toCard_pred A r (toCard A r)"
blanchet@49312
   192
unfolding toCard_def using someI_ex[OF ex_toCard_pred] .
blanchet@49312
   193
blanchet@49312
   194
lemma toCard_inj: "\<lbrakk>|A| \<le>o r; Card_order r; x \<in> A; y \<in> A\<rbrakk> \<Longrightarrow>
blanchet@49312
   195
  toCard A r x = toCard A r y \<longleftrightarrow> x = y"
blanchet@49312
   196
using toCard_pred_toCard unfolding inj_on_def toCard_pred_def by blast
blanchet@49312
   197
blanchet@49312
   198
definition "fromCard A r k \<equiv> SOME b. b \<in> A \<and> toCard A r b = k"
blanchet@49312
   199
blanchet@49312
   200
lemma fromCard_toCard:
blanchet@49312
   201
"\<lbrakk>|A| \<le>o r; Card_order r; b \<in> A\<rbrakk> \<Longrightarrow> fromCard A r (toCard A r b) = b"
blanchet@49312
   202
unfolding fromCard_def by (rule some_equality) (auto simp add: toCard_inj)
blanchet@49312
   203
blanchet@49312
   204
lemma Inl_Field_csum: "a \<in> Field r \<Longrightarrow> Inl a \<in> Field (r +c s)"
blanchet@49312
   205
unfolding Field_card_of csum_def by auto
blanchet@49312
   206
blanchet@49312
   207
lemma Inr_Field_csum: "a \<in> Field s \<Longrightarrow> Inr a \<in> Field (r +c s)"
blanchet@49312
   208
unfolding Field_card_of csum_def by auto
blanchet@49312
   209
blanchet@55415
   210
lemma rec_nat_0_imp: "f = rec_nat f1 (%n rec. f2 n rec) \<Longrightarrow> f 0 = f1"
blanchet@49312
   211
by auto
blanchet@49312
   212
blanchet@55415
   213
lemma rec_nat_Suc_imp: "f = rec_nat f1 (%n rec. f2 n rec) \<Longrightarrow> f (Suc n) = f2 n (f n)"
blanchet@49312
   214
by auto
blanchet@49312
   215
blanchet@55413
   216
lemma rec_list_Nil_imp: "f = rec_list f1 (%x xs rec. f2 x xs rec) \<Longrightarrow> f [] = f1"
blanchet@49312
   217
by auto
blanchet@49312
   218
blanchet@55413
   219
lemma rec_list_Cons_imp: "f = rec_list f1 (%x xs rec. f2 x xs rec) \<Longrightarrow> f (x # xs) = f2 x xs (f xs)"
blanchet@49312
   220
by auto
blanchet@49312
   221
blanchet@49312
   222
lemma not_arg_cong_Inr: "x \<noteq> y \<Longrightarrow> Inr x \<noteq> Inr y"
blanchet@49312
   223
by simp
blanchet@49312
   224
traytel@51925
   225
lemma Collect_splitD: "x \<in> Collect (split A) \<Longrightarrow> A (fst x) (snd x)"
traytel@51925
   226
by auto
traytel@51925
   227
traytel@52731
   228
definition image2p where
traytel@52731
   229
  "image2p f g R = (\<lambda>x y. \<exists>x' y'. R x' y' \<and> f x' = x \<and> g y' = y)"
traytel@52731
   230
blanchet@55463
   231
lemma image2pI: "R x y \<Longrightarrow> image2p f g R (f x) (g y)"
traytel@52731
   232
  unfolding image2p_def by blast
traytel@52731
   233
blanchet@55463
   234
lemma image2pE: "\<lbrakk>image2p f g R fx gy; (\<And>x y. fx = f x \<Longrightarrow> gy = g y \<Longrightarrow> R x y \<Longrightarrow> P)\<rbrakk> \<Longrightarrow> P"
traytel@52731
   235
  unfolding image2p_def by blast
traytel@52731
   236
blanchet@55463
   237
lemma fun_rel_iff_geq_image2p: "fun_rel R S f g = (image2p f g R \<le> S)"
traytel@52731
   238
  unfolding fun_rel_def image2p_def by auto
traytel@52731
   239
blanchet@55463
   240
lemma fun_rel_image2p: "fun_rel R (image2p f g R) f g"
traytel@52731
   241
  unfolding fun_rel_def image2p_def by auto
traytel@52731
   242
blanchet@55022
   243
blanchet@55022
   244
subsection {* Equivalence relations, quotients, and Hilbert's choice *}
blanchet@55022
   245
blanchet@55022
   246
lemma equiv_Eps_in:
blanchet@55022
   247
"\<lbrakk>equiv A r; X \<in> A//r\<rbrakk> \<Longrightarrow> Eps (%x. x \<in> X) \<in> X"
blanchet@55022
   248
apply (rule someI2_ex)
blanchet@55022
   249
using in_quotient_imp_non_empty by blast
blanchet@55022
   250
blanchet@55022
   251
lemma equiv_Eps_preserves:
blanchet@55022
   252
assumes ECH: "equiv A r" and X: "X \<in> A//r"
blanchet@55022
   253
shows "Eps (%x. x \<in> X) \<in> A"
blanchet@55022
   254
apply (rule in_mono[rule_format])
blanchet@55022
   255
 using assms apply (rule in_quotient_imp_subset)
blanchet@55022
   256
by (rule equiv_Eps_in) (rule assms)+
blanchet@55022
   257
blanchet@55022
   258
lemma proj_Eps:
blanchet@55022
   259
assumes "equiv A r" and "X \<in> A//r"
blanchet@55022
   260
shows "proj r (Eps (%x. x \<in> X)) = X"
blanchet@55022
   261
unfolding proj_def proof auto
blanchet@55022
   262
  fix x assume x: "x \<in> X"
blanchet@55022
   263
  thus "(Eps (%x. x \<in> X), x) \<in> r" using assms equiv_Eps_in in_quotient_imp_in_rel by fast
blanchet@55022
   264
next
blanchet@55022
   265
  fix x assume "(Eps (%x. x \<in> X),x) \<in> r"
blanchet@55022
   266
  thus "x \<in> X" using in_quotient_imp_closed[OF assms equiv_Eps_in[OF assms]] by fast
blanchet@55022
   267
qed
blanchet@55022
   268
blanchet@55022
   269
definition univ where "univ f X == f (Eps (%x. x \<in> X))"
blanchet@55022
   270
blanchet@55022
   271
lemma univ_commute:
blanchet@55022
   272
assumes ECH: "equiv A r" and RES: "f respects r" and x: "x \<in> A"
blanchet@55022
   273
shows "(univ f) (proj r x) = f x"
blanchet@55022
   274
unfolding univ_def proof -
blanchet@55022
   275
  have prj: "proj r x \<in> A//r" using x proj_preserves by fast
blanchet@55022
   276
  hence "Eps (%y. y \<in> proj r x) \<in> A" using ECH equiv_Eps_preserves by fast
blanchet@55022
   277
  moreover have "proj r (Eps (%y. y \<in> proj r x)) = proj r x" using ECH prj proj_Eps by fast
blanchet@55022
   278
  ultimately have "(x, Eps (%y. y \<in> proj r x)) \<in> r" using x ECH proj_iff by fast
blanchet@55022
   279
  thus "f (Eps (%y. y \<in> proj r x)) = f x" using RES unfolding congruent_def by fastforce
blanchet@55022
   280
qed
blanchet@55022
   281
blanchet@55022
   282
lemma univ_preserves:
blanchet@55022
   283
assumes ECH: "equiv A r" and RES: "f respects r" and
blanchet@55022
   284
        PRES: "\<forall> x \<in> A. f x \<in> B"
blanchet@55463
   285
shows "\<forall>X \<in> A//r. univ f X \<in> B"
blanchet@55022
   286
proof
blanchet@55022
   287
  fix X assume "X \<in> A//r"
blanchet@55022
   288
  then obtain x where x: "x \<in> A" and X: "X = proj r x" using ECH proj_image[of r A] by blast
blanchet@55022
   289
  hence "univ f X = f x" using assms univ_commute by fastforce
blanchet@55022
   290
  thus "univ f X \<in> B" using x PRES by simp
blanchet@55022
   291
qed
blanchet@55022
   292
blanchet@55062
   293
ML_file "Tools/BNF/bnf_gfp_util.ML"
blanchet@55062
   294
ML_file "Tools/BNF/bnf_gfp_tactics.ML"
blanchet@55062
   295
ML_file "Tools/BNF/bnf_gfp.ML"
blanchet@55538
   296
ML_file "Tools/BNF/bnf_gfp_rec_sugar_tactics.ML"
blanchet@55538
   297
ML_file "Tools/BNF/bnf_gfp_rec_sugar.ML"
blanchet@49309
   298
blanchet@48975
   299
end