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