src/HOL/BNF/BNF_GFP.thy
author panny
Tue Sep 24 15:16:59 2013 +0200 (2013-09-24)
changeset 53822 6304b12c7627
parent 53753 ae7f50e70c09
child 54246 8fdb4dc08ed1
permissions -rw-r--r--
add "primcorec" command (cf. ae7f50e70c09)
blanchet@49509
     1
(*  Title:      HOL/BNF/BNF_GFP.thy
blanchet@48975
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@48975
     3
    Copyright   2012
blanchet@48975
     4
blanchet@48975
     5
Greatest fixed point operation on bounded natural functors.
blanchet@48975
     6
*)
blanchet@48975
     7
blanchet@48975
     8
header {* Greatest Fixed Point Operation on Bounded Natural Functors *}
blanchet@48975
     9
blanchet@48975
    10
theory BNF_GFP
blanchet@53311
    11
imports BNF_FP_Base Equiv_Relations_More "~~/src/HOL/Library/Sublist"
blanchet@48975
    12
keywords
blanchet@53310
    13
  "codatatype" :: thy_decl and
panny@53822
    14
  "primcorecursive" :: thy_goal and
panny@53822
    15
  "primcorec" :: thy_decl
blanchet@48975
    16
begin
blanchet@48975
    17
blanchet@49312
    18
lemma sum_case_expand_Inr: "f o Inl = g \<Longrightarrow> f x = sum_case g (f o Inr) x"
blanchet@49312
    19
by (auto split: sum.splits)
blanchet@49312
    20
traytel@51739
    21
lemma sum_case_expand_Inr': "f o Inl = g \<Longrightarrow> h = f o Inr \<longleftrightarrow> sum_case g h = f"
traytel@52634
    22
by (metis sum_case_o_inj(1,2) surjective_sum)
traytel@51739
    23
blanchet@49312
    24
lemma converse_Times: "(A \<times> B) ^-1 = B \<times> A"
blanchet@49312
    25
by auto
blanchet@49312
    26
blanchet@49312
    27
lemma equiv_proj:
blanchet@49312
    28
  assumes e: "equiv A R" and "z \<in> R"
blanchet@49312
    29
  shows "(proj R o fst) z = (proj R o snd) z"
blanchet@49312
    30
proof -
blanchet@49312
    31
  from assms(2) have z: "(fst z, snd z) \<in> R" by auto
traytel@53695
    32
  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
    33
    unfolding equiv_def sym_def trans_def by blast+
traytel@53695
    34
  then show ?thesis unfolding proj_def[abs_def] by auto
blanchet@49312
    35
qed
blanchet@49312
    36
blanchet@49312
    37
(* Operators: *)
blanchet@49312
    38
definition image2 where "image2 A f g = {(f a, g a) | a. a \<in> A}"
blanchet@49312
    39
blanchet@49312
    40
traytel@51447
    41
lemma Id_onD: "(a, b) \<in> Id_on A \<Longrightarrow> a = b"
traytel@51447
    42
unfolding Id_on_def by simp
blanchet@49312
    43
traytel@51447
    44
lemma Id_onD': "x \<in> Id_on A \<Longrightarrow> fst x = snd x"
traytel@51447
    45
unfolding Id_on_def by auto
blanchet@49312
    46
traytel@51447
    47
lemma Id_on_fst: "x \<in> Id_on A \<Longrightarrow> fst x \<in> A"
traytel@51447
    48
unfolding Id_on_def by auto
blanchet@49312
    49
traytel@51447
    50
lemma Id_on_UNIV: "Id_on UNIV = Id"
traytel@51447
    51
unfolding Id_on_def by auto
blanchet@49312
    52
traytel@51447
    53
lemma Id_on_Comp: "Id_on A = Id_on A O Id_on A"
traytel@51447
    54
unfolding Id_on_def by auto
blanchet@49312
    55
traytel@51447
    56
lemma Id_on_Gr: "Id_on A = Gr A id"
traytel@51447
    57
unfolding Id_on_def Gr_def by auto
blanchet@49312
    58
traytel@51447
    59
lemma Id_on_UNIV_I: "x = y \<Longrightarrow> (x, y) \<in> Id_on UNIV"
traytel@51447
    60
unfolding Id_on_def by auto
blanchet@49312
    61
blanchet@49312
    62
lemma image2_eqI: "\<lbrakk>b = f x; c = g x; x \<in> A\<rbrakk> \<Longrightarrow> (b, c) \<in> image2 A f g"
blanchet@49312
    63
unfolding image2_def by auto
blanchet@49312
    64
blanchet@49312
    65
lemma IdD: "(a, b) \<in> Id \<Longrightarrow> a = b"
blanchet@49312
    66
by auto
blanchet@49312
    67
blanchet@49312
    68
lemma image2_Gr: "image2 A f g = (Gr A f)^-1 O (Gr A g)"
blanchet@49312
    69
unfolding image2_def Gr_def by auto
blanchet@49312
    70
blanchet@49312
    71
lemma GrD1: "(x, fx) \<in> Gr A f \<Longrightarrow> x \<in> A"
blanchet@49312
    72
unfolding Gr_def by simp
blanchet@49312
    73
blanchet@49312
    74
lemma GrD2: "(x, fx) \<in> Gr A f \<Longrightarrow> f x = fx"
blanchet@49312
    75
unfolding Gr_def by simp
blanchet@49312
    76
blanchet@49312
    77
lemma Gr_incl: "Gr A f \<subseteq> A <*> B \<longleftrightarrow> f ` A \<subseteq> B"
blanchet@49312
    78
unfolding Gr_def by auto
blanchet@49312
    79
traytel@51893
    80
lemma in_rel_Collect_split_eq: "in_rel (Collect (split X)) = X"
traytel@51893
    81
unfolding fun_eq_iff by auto
traytel@51893
    82
traytel@51893
    83
lemma Collect_split_in_rel_leI: "X \<subseteq> Y \<Longrightarrow> X \<subseteq> Collect (split (in_rel Y))"
traytel@51893
    84
by auto
traytel@51893
    85
traytel@51893
    86
lemma Collect_split_in_rel_leE: "X \<subseteq> Collect (split (in_rel Y)) \<Longrightarrow> (X \<subseteq> Y \<Longrightarrow> R) \<Longrightarrow> R"
traytel@51893
    87
by force
traytel@51893
    88
traytel@51893
    89
lemma Collect_split_in_relI: "x \<in> X \<Longrightarrow> x \<in> Collect (split (in_rel X))"
traytel@51893
    90
by auto
traytel@51893
    91
traytel@51893
    92
lemma conversep_in_rel: "(in_rel R)\<inverse>\<inverse> = in_rel (R\<inverse>)"
traytel@51893
    93
unfolding fun_eq_iff by auto
traytel@51893
    94
traytel@51893
    95
lemma relcompp_in_rel: "in_rel R OO in_rel S = in_rel (R O S)"
traytel@51893
    96
unfolding fun_eq_iff by auto
traytel@51893
    97
traytel@51893
    98
lemma in_rel_Gr: "in_rel (Gr A f) = Grp A f"
traytel@51893
    99
unfolding Gr_def Grp_def fun_eq_iff by auto
traytel@51893
   100
traytel@51893
   101
lemma in_rel_Id_on_UNIV: "in_rel (Id_on UNIV) = op ="
traytel@51893
   102
unfolding fun_eq_iff by auto
traytel@51893
   103
blanchet@49312
   104
definition relImage where
blanchet@49312
   105
"relImage R f \<equiv> {(f a1, f a2) | a1 a2. (a1,a2) \<in> R}"
blanchet@49312
   106
blanchet@49312
   107
definition relInvImage where
blanchet@49312
   108
"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
   109
blanchet@49312
   110
lemma relImage_Gr:
blanchet@49312
   111
"\<lbrakk>R \<subseteq> A \<times> A\<rbrakk> \<Longrightarrow> relImage R f = (Gr A f)^-1 O R O Gr A f"
blanchet@49312
   112
unfolding relImage_def Gr_def relcomp_def by auto
blanchet@49312
   113
blanchet@49312
   114
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
   115
unfolding Gr_def relcomp_def image_def relInvImage_def by auto
blanchet@49312
   116
blanchet@49312
   117
lemma relImage_mono:
blanchet@49312
   118
"R1 \<subseteq> R2 \<Longrightarrow> relImage R1 f \<subseteq> relImage R2 f"
blanchet@49312
   119
unfolding relImage_def by auto
blanchet@49312
   120
blanchet@49312
   121
lemma relInvImage_mono:
blanchet@49312
   122
"R1 \<subseteq> R2 \<Longrightarrow> relInvImage A R1 f \<subseteq> relInvImage A R2 f"
blanchet@49312
   123
unfolding relInvImage_def by auto
blanchet@49312
   124
traytel@51447
   125
lemma relInvImage_Id_on:
traytel@51447
   126
"(\<And>a1 a2. f a1 = f a2 \<longleftrightarrow> a1 = a2) \<Longrightarrow> relInvImage A (Id_on B) f \<subseteq> Id"
traytel@51447
   127
unfolding relInvImage_def Id_on_def by auto
blanchet@49312
   128
blanchet@49312
   129
lemma relInvImage_UNIV_relImage:
blanchet@49312
   130
"R \<subseteq> relInvImage UNIV (relImage R f) f"
blanchet@49312
   131
unfolding relInvImage_def relImage_def by auto
blanchet@49312
   132
blanchet@49312
   133
lemma equiv_Image: "equiv A R \<Longrightarrow> (\<And>a b. (a, b) \<in> R \<Longrightarrow> a \<in> A \<and> b \<in> A \<and> R `` {a} = R `` {b})"
blanchet@49312
   134
unfolding equiv_def refl_on_def Image_def by (auto intro: transD symD)
blanchet@49312
   135
blanchet@49312
   136
lemma relImage_proj:
blanchet@49312
   137
assumes "equiv A R"
traytel@51447
   138
shows "relImage R (proj R) \<subseteq> Id_on (A//R)"
traytel@51447
   139
unfolding relImage_def Id_on_def
traytel@51447
   140
using proj_iff[OF assms] equiv_class_eq_iff[OF assms]
traytel@51447
   141
by (auto simp: proj_preserves)
blanchet@49312
   142
blanchet@49312
   143
lemma relImage_relInvImage:
blanchet@49312
   144
assumes "R \<subseteq> f ` A <*> f ` A"
blanchet@49312
   145
shows "relImage (relInvImage A R f) f = R"
blanchet@49312
   146
using assms unfolding relImage_def relInvImage_def by fastforce
blanchet@49312
   147
blanchet@49312
   148
lemma subst_Pair: "P x y \<Longrightarrow> a = (x, y) \<Longrightarrow> P (fst a) (snd a)"
blanchet@49312
   149
by simp
blanchet@49312
   150
blanchet@49312
   151
lemma fst_diag_id: "(fst \<circ> (%x. (x, x))) z = id z"
blanchet@49312
   152
by simp
blanchet@49312
   153
blanchet@49312
   154
lemma snd_diag_id: "(snd \<circ> (%x. (x, x))) z = id z"
blanchet@49312
   155
by simp
blanchet@49312
   156
blanchet@49312
   157
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
   158
unfolding convol_def by auto
blanchet@49312
   159
blanchet@49312
   160
(*Extended Sublist*)
blanchet@49312
   161
blanchet@49312
   162
definition prefCl where
traytel@50058
   163
  "prefCl Kl = (\<forall> kl1 kl2. prefixeq kl1 kl2 \<and> kl2 \<in> Kl \<longrightarrow> kl1 \<in> Kl)"
blanchet@49312
   164
definition PrefCl where
traytel@50058
   165
  "PrefCl A n = (\<forall>kl kl'. kl \<in> A n \<and> prefixeq kl' kl \<longrightarrow> (\<exists>m\<le>n. kl' \<in> A m))"
blanchet@49312
   166
blanchet@49312
   167
lemma prefCl_UN:
blanchet@49312
   168
  "\<lbrakk>\<And>n. PrefCl A n\<rbrakk> \<Longrightarrow> prefCl (\<Union>n. A n)"
blanchet@49312
   169
unfolding prefCl_def PrefCl_def by fastforce
blanchet@49312
   170
blanchet@49312
   171
definition Succ where "Succ Kl kl = {k . kl @ [k] \<in> Kl}"
blanchet@49312
   172
definition Shift where "Shift Kl k = {kl. k # kl \<in> Kl}"
blanchet@49312
   173
definition shift where "shift lab k = (\<lambda>kl. lab (k # kl))"
blanchet@49312
   174
blanchet@49312
   175
lemma empty_Shift: "\<lbrakk>[] \<in> Kl; k \<in> Succ Kl []\<rbrakk> \<Longrightarrow> [] \<in> Shift Kl k"
blanchet@49312
   176
unfolding Shift_def Succ_def by simp
blanchet@49312
   177
blanchet@49312
   178
lemma Shift_clists: "Kl \<subseteq> Field (clists r) \<Longrightarrow> Shift Kl k \<subseteq> Field (clists r)"
blanchet@49312
   179
unfolding Shift_def clists_def Field_card_of by auto
blanchet@49312
   180
blanchet@49312
   181
lemma Shift_prefCl: "prefCl Kl \<Longrightarrow> prefCl (Shift Kl k)"
blanchet@49312
   182
unfolding prefCl_def Shift_def
blanchet@49312
   183
proof safe
blanchet@49312
   184
  fix kl1 kl2
traytel@50058
   185
  assume "\<forall>kl1 kl2. prefixeq kl1 kl2 \<and> kl2 \<in> Kl \<longrightarrow> kl1 \<in> Kl"
traytel@50058
   186
    "prefixeq kl1 kl2" "k # kl2 \<in> Kl"
traytel@50058
   187
  thus "k # kl1 \<in> Kl" using Cons_prefixeq_Cons[of k kl1 k kl2] by blast
blanchet@49312
   188
qed
blanchet@49312
   189
blanchet@49312
   190
lemma not_in_Shift: "kl \<notin> Shift Kl x \<Longrightarrow> x # kl \<notin> Kl"
blanchet@49312
   191
unfolding Shift_def by simp
blanchet@49312
   192
blanchet@49312
   193
lemma SuccD: "k \<in> Succ Kl kl \<Longrightarrow> kl @ [k] \<in> Kl"
blanchet@49312
   194
unfolding Succ_def by simp
blanchet@49312
   195
blanchet@49312
   196
lemmas SuccE = SuccD[elim_format]
blanchet@49312
   197
blanchet@49312
   198
lemma SuccI: "kl @ [k] \<in> Kl \<Longrightarrow> k \<in> Succ Kl kl"
blanchet@49312
   199
unfolding Succ_def by simp
blanchet@49312
   200
blanchet@49312
   201
lemma ShiftD: "kl \<in> Shift Kl k \<Longrightarrow> k # kl \<in> Kl"
blanchet@49312
   202
unfolding Shift_def by simp
blanchet@49312
   203
blanchet@49312
   204
lemma Succ_Shift: "Succ (Shift Kl k) kl = Succ Kl (k # kl)"
blanchet@49312
   205
unfolding Succ_def Shift_def by auto
blanchet@49312
   206
blanchet@49312
   207
lemma Nil_clists: "{[]} \<subseteq> Field (clists r)"
blanchet@49312
   208
unfolding clists_def Field_card_of by auto
blanchet@49312
   209
blanchet@49312
   210
lemma Cons_clists:
blanchet@49312
   211
  "\<lbrakk>x \<in> Field r; xs \<in> Field (clists r)\<rbrakk> \<Longrightarrow> x # xs \<in> Field (clists r)"
blanchet@49312
   212
unfolding clists_def Field_card_of by auto
blanchet@49312
   213
blanchet@49312
   214
lemma length_Cons: "length (x # xs) = Suc (length xs)"
blanchet@49312
   215
by simp
blanchet@49312
   216
blanchet@49312
   217
lemma length_append_singleton: "length (xs @ [x]) = Suc (length xs)"
blanchet@49312
   218
by simp
blanchet@49312
   219
blanchet@49312
   220
(*injection into the field of a cardinal*)
blanchet@49312
   221
definition "toCard_pred A r f \<equiv> inj_on f A \<and> f ` A \<subseteq> Field r \<and> Card_order r"
blanchet@49312
   222
definition "toCard A r \<equiv> SOME f. toCard_pred A r f"
blanchet@49312
   223
blanchet@49312
   224
lemma ex_toCard_pred:
blanchet@49312
   225
"\<lbrakk>|A| \<le>o r; Card_order r\<rbrakk> \<Longrightarrow> \<exists> f. toCard_pred A r f"
blanchet@49312
   226
unfolding toCard_pred_def
blanchet@49312
   227
using card_of_ordLeq[of A "Field r"]
blanchet@49312
   228
      ordLeq_ordIso_trans[OF _ card_of_unique[of "Field r" r], of "|A|"]
blanchet@49312
   229
by blast
blanchet@49312
   230
blanchet@49312
   231
lemma toCard_pred_toCard:
blanchet@49312
   232
  "\<lbrakk>|A| \<le>o r; Card_order r\<rbrakk> \<Longrightarrow> toCard_pred A r (toCard A r)"
blanchet@49312
   233
unfolding toCard_def using someI_ex[OF ex_toCard_pred] .
blanchet@49312
   234
blanchet@49312
   235
lemma toCard_inj: "\<lbrakk>|A| \<le>o r; Card_order r; x \<in> A; y \<in> A\<rbrakk> \<Longrightarrow>
blanchet@49312
   236
  toCard A r x = toCard A r y \<longleftrightarrow> x = y"
blanchet@49312
   237
using toCard_pred_toCard unfolding inj_on_def toCard_pred_def by blast
blanchet@49312
   238
blanchet@49312
   239
lemma toCard: "\<lbrakk>|A| \<le>o r; Card_order r; b \<in> A\<rbrakk> \<Longrightarrow> toCard A r b \<in> Field r"
blanchet@49312
   240
using toCard_pred_toCard unfolding toCard_pred_def by blast
blanchet@49312
   241
blanchet@49312
   242
definition "fromCard A r k \<equiv> SOME b. b \<in> A \<and> toCard A r b = k"
blanchet@49312
   243
blanchet@49312
   244
lemma fromCard_toCard:
blanchet@49312
   245
"\<lbrakk>|A| \<le>o r; Card_order r; b \<in> A\<rbrakk> \<Longrightarrow> fromCard A r (toCard A r b) = b"
blanchet@49312
   246
unfolding fromCard_def by (rule some_equality) (auto simp add: toCard_inj)
blanchet@49312
   247
blanchet@49312
   248
(* pick according to the weak pullback *)
blanchet@49312
   249
definition pickWP where
traytel@51446
   250
"pickWP A p1 p2 b1 b2 \<equiv> SOME a. a \<in> A \<and> p1 a = b1 \<and> p2 a = b2"
blanchet@49312
   251
blanchet@49312
   252
lemma pickWP_pred:
blanchet@49312
   253
assumes "wpull A B1 B2 f1 f2 p1 p2" and
blanchet@49312
   254
"b1 \<in> B1" and "b2 \<in> B2" and "f1 b1 = f2 b2"
traytel@51446
   255
shows "\<exists> a. a \<in> A \<and> p1 a = b1 \<and> p2 a = b2"
traytel@51446
   256
using assms unfolding wpull_def by blast
blanchet@49312
   257
blanchet@49312
   258
lemma pickWP:
blanchet@49312
   259
assumes "wpull A B1 B2 f1 f2 p1 p2" and
blanchet@49312
   260
"b1 \<in> B1" and "b2 \<in> B2" and "f1 b1 = f2 b2"
blanchet@49312
   261
shows "pickWP A p1 p2 b1 b2 \<in> A"
blanchet@49312
   262
      "p1 (pickWP A p1 p2 b1 b2) = b1"
blanchet@49312
   263
      "p2 (pickWP A p1 p2 b1 b2) = b2"
traytel@51446
   264
unfolding pickWP_def using assms someI_ex[OF pickWP_pred] by fastforce+
blanchet@49312
   265
blanchet@49312
   266
lemma Inl_Field_csum: "a \<in> Field r \<Longrightarrow> Inl a \<in> Field (r +c s)"
blanchet@49312
   267
unfolding Field_card_of csum_def by auto
blanchet@49312
   268
blanchet@49312
   269
lemma Inr_Field_csum: "a \<in> Field s \<Longrightarrow> Inr a \<in> Field (r +c s)"
blanchet@49312
   270
unfolding Field_card_of csum_def by auto
blanchet@49312
   271
blanchet@49312
   272
lemma nat_rec_0: "f = nat_rec f1 (%n rec. f2 n rec) \<Longrightarrow> f 0 = f1"
blanchet@49312
   273
by auto
blanchet@49312
   274
blanchet@49312
   275
lemma nat_rec_Suc: "f = nat_rec f1 (%n rec. f2 n rec) \<Longrightarrow> f (Suc n) = f2 n (f n)"
blanchet@49312
   276
by auto
blanchet@49312
   277
blanchet@49312
   278
lemma list_rec_Nil: "f = list_rec f1 (%x xs rec. f2 x xs rec) \<Longrightarrow> f [] = f1"
blanchet@49312
   279
by auto
blanchet@49312
   280
blanchet@49312
   281
lemma list_rec_Cons: "f = list_rec f1 (%x xs rec. f2 x xs rec) \<Longrightarrow> f (x # xs) = f2 x xs (f xs)"
blanchet@49312
   282
by auto
blanchet@49312
   283
blanchet@49312
   284
lemma not_arg_cong_Inr: "x \<noteq> y \<Longrightarrow> Inr x \<noteq> Inr y"
blanchet@49312
   285
by simp
blanchet@49312
   286
traytel@51925
   287
lemma Collect_splitD: "x \<in> Collect (split A) \<Longrightarrow> A (fst x) (snd x)"
traytel@51925
   288
by auto
traytel@51925
   289
traytel@52731
   290
definition image2p where
traytel@52731
   291
  "image2p f g R = (\<lambda>x y. \<exists>x' y'. R x' y' \<and> f x' = x \<and> g y' = y)"
traytel@52731
   292
traytel@52731
   293
lemma image2pI: "R x y \<Longrightarrow> (image2p f g R) (f x) (g y)"
traytel@52731
   294
  unfolding image2p_def by blast
traytel@52731
   295
traytel@52731
   296
lemma image2p_eqI: "\<lbrakk>fx = f x; gy = g y; R x y\<rbrakk> \<Longrightarrow> (image2p f g R) fx gy"
traytel@52731
   297
  unfolding image2p_def by blast
traytel@52731
   298
traytel@52731
   299
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
   300
  unfolding image2p_def by blast
traytel@52731
   301
traytel@52731
   302
lemma fun_rel_iff_geq_image2p: "(fun_rel R S) f g = (image2p f g R \<le> S)"
traytel@52731
   303
  unfolding fun_rel_def image2p_def by auto
traytel@52731
   304
traytel@52731
   305
lemma convol_image_image2p: "<f o fst, g o snd> ` Collect (split R) \<subseteq> Collect (split (image2p f g R))"
traytel@52731
   306
  unfolding convol_def image2p_def by fastforce
traytel@52731
   307
traytel@52731
   308
lemma fun_rel_image2p: "(fun_rel R (image2p f g R)) f g"
traytel@52731
   309
  unfolding fun_rel_def image2p_def by auto
traytel@52731
   310
blanchet@49309
   311
ML_file "Tools/bnf_gfp_util.ML"
blanchet@49309
   312
ML_file "Tools/bnf_gfp_tactics.ML"
blanchet@49309
   313
ML_file "Tools/bnf_gfp.ML"
blanchet@49309
   314
blanchet@48975
   315
end