src/HOL/BNF_LFP.thy
author traytel
Fri Feb 28 17:54:52 2014 +0100 (2014-02-28)
changeset 55811 aa1acc25126b
parent 55770 f2cf7f92c9ac
child 55851 3d40cf74726c
permissions -rw-r--r--
load Metis a little later
blanchet@55059
     1
(*  Title:      HOL/BNF_LFP.thy
blanchet@48975
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@53305
     3
    Author:     Lorenz Panny, TU Muenchen
blanchet@53305
     4
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@53305
     5
    Copyright   2012, 2013
blanchet@48975
     6
blanchet@48975
     7
Least fixed point operation on bounded natural functors.
blanchet@48975
     8
*)
blanchet@48975
     9
blanchet@48975
    10
header {* Least Fixed Point Operation on Bounded Natural Functors *}
blanchet@48975
    11
blanchet@48975
    12
theory BNF_LFP
blanchet@53311
    13
imports BNF_FP_Base
blanchet@48975
    14
keywords
blanchet@53305
    15
  "datatype_new" :: thy_decl and
blanchet@55575
    16
  "datatype_compat" :: thy_decl
blanchet@48975
    17
begin
blanchet@48975
    18
blanchet@49312
    19
lemma subset_emptyI: "(\<And>x. x \<in> A \<Longrightarrow> False) \<Longrightarrow> A \<subseteq> {}"
blanchet@49312
    20
by blast
blanchet@49312
    21
blanchet@49312
    22
lemma image_Collect_subsetI:
blanchet@49312
    23
  "(\<And>x. P x \<Longrightarrow> f x \<in> B) \<Longrightarrow> f ` {x. P x} \<subseteq> B"
blanchet@49312
    24
by blast
blanchet@49312
    25
blanchet@49312
    26
lemma Collect_restrict: "{x. x \<in> X \<and> P x} \<subseteq> X"
blanchet@49312
    27
by auto
blanchet@49312
    28
blanchet@49312
    29
lemma prop_restrict: "\<lbrakk>x \<in> Z; Z \<subseteq> {x. x \<in> X \<and> P x}\<rbrakk> \<Longrightarrow> P x"
blanchet@49312
    30
by auto
blanchet@49312
    31
blanchet@55023
    32
lemma underS_I: "\<lbrakk>i \<noteq> j; (i, j) \<in> R\<rbrakk> \<Longrightarrow> i \<in> underS R j"
blanchet@55023
    33
unfolding underS_def by simp
blanchet@49312
    34
blanchet@55023
    35
lemma underS_E: "i \<in> underS R j \<Longrightarrow> i \<noteq> j \<and> (i, j) \<in> R"
blanchet@55023
    36
unfolding underS_def by simp
blanchet@49312
    37
blanchet@55023
    38
lemma underS_Field: "i \<in> underS R j \<Longrightarrow> i \<in> Field R"
blanchet@55023
    39
unfolding underS_def Field_def by auto
blanchet@49312
    40
blanchet@49312
    41
lemma FieldI2: "(i, j) \<in> R \<Longrightarrow> j \<in> Field R"
blanchet@49312
    42
unfolding Field_def by auto
blanchet@49312
    43
blanchet@49312
    44
lemma fst_convol': "fst (<f, g> x) = f x"
blanchet@49312
    45
using fst_convol unfolding convol_def by simp
blanchet@49312
    46
blanchet@49312
    47
lemma snd_convol': "snd (<f, g> x) = g x"
blanchet@49312
    48
using snd_convol unfolding convol_def by simp
blanchet@49312
    49
blanchet@49312
    50
lemma convol_expand_snd: "fst o f = g \<Longrightarrow>  <g, snd o f> = f"
blanchet@49312
    51
unfolding convol_def by auto
blanchet@49312
    52
traytel@55811
    53
lemma convol_expand_snd':
traytel@55811
    54
  assumes "(fst o f = g)"
traytel@55811
    55
  shows "h = snd o f \<longleftrightarrow> <g, h> = f"
traytel@55811
    56
proof -
traytel@55811
    57
  from assms have *: "<g, snd o f> = f" by (rule convol_expand_snd)
traytel@55811
    58
  then have "h = snd o f \<longleftrightarrow> h = snd o <g, snd o f>" by simp
traytel@55811
    59
  moreover have "\<dots> \<longleftrightarrow> h = snd o f" by (simp add: snd_convol)
traytel@55811
    60
  moreover have "\<dots> \<longleftrightarrow> <g, h> = f" by (subst (2) *[symmetric]) (auto simp: convol_def fun_eq_iff)
traytel@55811
    61
  ultimately show ?thesis by simp
traytel@55811
    62
qed
traytel@51739
    63
blanchet@49312
    64
definition inver where
blanchet@49312
    65
  "inver g f A = (ALL a : A. g (f a) = a)"
blanchet@49312
    66
blanchet@49312
    67
lemma bij_betw_iff_ex:
blanchet@49312
    68
  "bij_betw f A B = (EX g. g ` B = A \<and> inver g f A \<and> inver f g B)" (is "?L = ?R")
blanchet@49312
    69
proof (rule iffI)
blanchet@49312
    70
  assume ?L
blanchet@49312
    71
  hence f: "f ` A = B" and inj_f: "inj_on f A" unfolding bij_betw_def by auto
blanchet@49312
    72
  let ?phi = "% b a. a : A \<and> f a = b"
blanchet@49312
    73
  have "ALL b : B. EX a. ?phi b a" using f by blast
blanchet@49312
    74
  then obtain g where g: "ALL b : B. g b : A \<and> f (g b) = b"
blanchet@49312
    75
    using bchoice[of B ?phi] by blast
blanchet@49312
    76
  hence gg: "ALL b : f ` A. g b : A \<and> f (g b) = b" using f by blast
blanchet@49326
    77
  have gf: "inver g f A" unfolding inver_def
traytel@55811
    78
  proof
traytel@55811
    79
    fix a assume "a \<in> A"
traytel@55811
    80
    then show "g (f a) = a" using the_inv_into_f_f[OF inj_f, of "g (f a)"]
traytel@55811
    81
      the_inv_into_f_f[OF inj_f, of a] gg imageI[of a A f] by auto
traytel@55811
    82
  qed
blanchet@49312
    83
  moreover have "g ` B \<le> A \<and> inver f g B" using g unfolding inver_def by blast
blanchet@49312
    84
  moreover have "A \<le> g ` B"
blanchet@49312
    85
  proof safe
blanchet@49312
    86
    fix a assume a: "a : A"
blanchet@49312
    87
    hence "f a : B" using f by auto
blanchet@49312
    88
    moreover have "a = g (f a)" using a gf unfolding inver_def by auto
blanchet@49312
    89
    ultimately show "a : g ` B" by blast
blanchet@49312
    90
  qed
blanchet@49312
    91
  ultimately show ?R by blast
blanchet@49312
    92
next
blanchet@49312
    93
  assume ?R
blanchet@49312
    94
  then obtain g where g: "g ` B = A \<and> inver g f A \<and> inver f g B" by blast
blanchet@49312
    95
  show ?L unfolding bij_betw_def
blanchet@49312
    96
  proof safe
blanchet@49312
    97
    show "inj_on f A" unfolding inj_on_def
blanchet@49312
    98
    proof safe
blanchet@49312
    99
      fix a1 a2 assume a: "a1 : A"  "a2 : A" and "f a1 = f a2"
blanchet@49312
   100
      hence "g (f a1) = g (f a2)" by simp
blanchet@49312
   101
      thus "a1 = a2" using a g unfolding inver_def by simp
blanchet@49312
   102
    qed
blanchet@49312
   103
  next
blanchet@49312
   104
    fix a assume "a : A"
blanchet@49312
   105
    then obtain b where b: "b : B" and a: "a = g b" using g by blast
blanchet@49312
   106
    hence "b = f (g b)" using g unfolding inver_def by auto
blanchet@49312
   107
    thus "f a : B" unfolding a using b by simp
blanchet@49312
   108
  next
blanchet@49312
   109
    fix b assume "b : B"
blanchet@49312
   110
    hence "g b : A \<and> b = f (g b)" using g unfolding inver_def by auto
blanchet@49312
   111
    thus "b : f ` A" by auto
blanchet@49312
   112
  qed
blanchet@49312
   113
qed
blanchet@49312
   114
blanchet@49312
   115
lemma bij_betw_ex_weakE:
blanchet@49312
   116
  "\<lbrakk>bij_betw f A B\<rbrakk> \<Longrightarrow> \<exists>g. g ` B \<subseteq> A \<and> inver g f A \<and> inver f g B"
blanchet@49312
   117
by (auto simp only: bij_betw_iff_ex)
blanchet@49312
   118
blanchet@49312
   119
lemma inver_surj: "\<lbrakk>g ` B \<subseteq> A; f ` A \<subseteq> B; inver g f A\<rbrakk> \<Longrightarrow> g ` B = A"
blanchet@49312
   120
unfolding inver_def by auto (rule rev_image_eqI, auto)
blanchet@49312
   121
blanchet@49312
   122
lemma inver_mono: "\<lbrakk>A \<subseteq> B; inver f g B\<rbrakk> \<Longrightarrow> inver f g A"
blanchet@49312
   123
unfolding inver_def by auto
blanchet@49312
   124
blanchet@49312
   125
lemma inver_pointfree: "inver f g A = (\<forall>a \<in> A. (f o g) a = a)"
blanchet@49312
   126
unfolding inver_def by simp
blanchet@49312
   127
blanchet@49312
   128
lemma bij_betwE: "bij_betw f A B \<Longrightarrow> \<forall>a\<in>A. f a \<in> B"
blanchet@49312
   129
unfolding bij_betw_def by auto
blanchet@49312
   130
blanchet@49312
   131
lemma bij_betw_imageE: "bij_betw f A B \<Longrightarrow> f ` A = B"
blanchet@49312
   132
unfolding bij_betw_def by auto
blanchet@49312
   133
blanchet@49312
   134
lemma inverE: "\<lbrakk>inver f f' A; x \<in> A\<rbrakk> \<Longrightarrow> f (f' x) = x"
blanchet@49312
   135
unfolding inver_def by auto
blanchet@49312
   136
blanchet@49312
   137
lemma bij_betw_inver1: "bij_betw f A B \<Longrightarrow> inver (inv_into A f) f A"
blanchet@49312
   138
unfolding bij_betw_def inver_def by auto
blanchet@49312
   139
blanchet@49312
   140
lemma bij_betw_inver2: "bij_betw f A B \<Longrightarrow> inver f (inv_into A f) B"
blanchet@49312
   141
unfolding bij_betw_def inver_def by auto
blanchet@49312
   142
blanchet@49312
   143
lemma bij_betwI: "\<lbrakk>bij_betw g B A; inver g f A; inver f g B\<rbrakk> \<Longrightarrow> bij_betw f A B"
blanchet@49326
   144
by (drule bij_betw_imageE, unfold bij_betw_iff_ex) blast
blanchet@49312
   145
blanchet@49312
   146
lemma bij_betwI':
blanchet@49312
   147
  "\<lbrakk>\<And>x y. \<lbrakk>x \<in> X; y \<in> X\<rbrakk> \<Longrightarrow> (f x = f y) = (x = y);
blanchet@49312
   148
    \<And>x. x \<in> X \<Longrightarrow> f x \<in> Y;
blanchet@49312
   149
    \<And>y. y \<in> Y \<Longrightarrow> \<exists>x \<in> X. y = f x\<rbrakk> \<Longrightarrow> bij_betw f X Y"
traytel@53695
   150
unfolding bij_betw_def inj_on_def by blast
blanchet@49312
   151
blanchet@49312
   152
lemma surj_fun_eq:
blanchet@49312
   153
  assumes surj_on: "f ` X = UNIV" and eq_on: "\<forall>x \<in> X. (g1 o f) x = (g2 o f) x"
blanchet@49312
   154
  shows "g1 = g2"
blanchet@49312
   155
proof (rule ext)
blanchet@49312
   156
  fix y
blanchet@49312
   157
  from surj_on obtain x where "x \<in> X" and "y = f x" by blast
blanchet@49312
   158
  thus "g1 y = g2 y" using eq_on by simp
blanchet@49312
   159
qed
blanchet@49312
   160
blanchet@49312
   161
lemma Card_order_wo_rel: "Card_order r \<Longrightarrow> wo_rel r"
blanchet@49514
   162
unfolding wo_rel_def card_order_on_def by blast
blanchet@49312
   163
blanchet@49312
   164
lemma Cinfinite_limit: "\<lbrakk>x \<in> Field r; Cinfinite r\<rbrakk> \<Longrightarrow>
blanchet@49312
   165
  \<exists>y \<in> Field r. x \<noteq> y \<and> (x, y) \<in> r"
blanchet@49312
   166
unfolding cinfinite_def by (auto simp add: infinite_Card_order_limit)
blanchet@49312
   167
blanchet@49312
   168
lemma Card_order_trans:
blanchet@49312
   169
  "\<lbrakk>Card_order r; x \<noteq> y; (x, y) \<in> r; y \<noteq> z; (y, z) \<in> r\<rbrakk> \<Longrightarrow> x \<noteq> z \<and> (x, z) \<in> r"
blanchet@49312
   170
unfolding card_order_on_def well_order_on_def linear_order_on_def
blanchet@49312
   171
  partial_order_on_def preorder_on_def trans_def antisym_def by blast
blanchet@49312
   172
blanchet@49312
   173
lemma Cinfinite_limit2:
blanchet@49312
   174
 assumes x1: "x1 \<in> Field r" and x2: "x2 \<in> Field r" and r: "Cinfinite r"
blanchet@49312
   175
 shows "\<exists>y \<in> Field r. (x1 \<noteq> y \<and> (x1, y) \<in> r) \<and> (x2 \<noteq> y \<and> (x2, y) \<in> r)"
blanchet@49312
   176
proof -
blanchet@49312
   177
  from r have trans: "trans r" and total: "Total r" and antisym: "antisym r"
blanchet@49312
   178
    unfolding card_order_on_def well_order_on_def linear_order_on_def
blanchet@49312
   179
      partial_order_on_def preorder_on_def by auto
blanchet@49312
   180
  obtain y1 where y1: "y1 \<in> Field r" "x1 \<noteq> y1" "(x1, y1) \<in> r"
blanchet@49312
   181
    using Cinfinite_limit[OF x1 r] by blast
blanchet@49312
   182
  obtain y2 where y2: "y2 \<in> Field r" "x2 \<noteq> y2" "(x2, y2) \<in> r"
blanchet@49312
   183
    using Cinfinite_limit[OF x2 r] by blast
blanchet@49312
   184
  show ?thesis
blanchet@49312
   185
  proof (cases "y1 = y2")
blanchet@49312
   186
    case True with y1 y2 show ?thesis by blast
blanchet@49312
   187
  next
blanchet@49312
   188
    case False
blanchet@49312
   189
    with y1(1) y2(1) total have "(y1, y2) \<in> r \<or> (y2, y1) \<in> r"
blanchet@49312
   190
      unfolding total_on_def by auto
blanchet@49312
   191
    thus ?thesis
blanchet@49312
   192
    proof
blanchet@49312
   193
      assume *: "(y1, y2) \<in> r"
blanchet@49312
   194
      with trans y1(3) have "(x1, y2) \<in> r" unfolding trans_def by blast
blanchet@49312
   195
      with False y1 y2 * antisym show ?thesis by (cases "x1 = y2") (auto simp: antisym_def)
blanchet@49312
   196
    next
blanchet@49312
   197
      assume *: "(y2, y1) \<in> r"
blanchet@49312
   198
      with trans y2(3) have "(x2, y1) \<in> r" unfolding trans_def by blast
blanchet@49312
   199
      with False y1 y2 * antisym show ?thesis by (cases "x2 = y1") (auto simp: antisym_def)
blanchet@49312
   200
    qed
blanchet@49312
   201
  qed
blanchet@49312
   202
qed
blanchet@49312
   203
blanchet@49312
   204
lemma Cinfinite_limit_finite: "\<lbrakk>finite X; X \<subseteq> Field r; Cinfinite r\<rbrakk>
blanchet@49312
   205
 \<Longrightarrow> \<exists>y \<in> Field r. \<forall>x \<in> X. (x \<noteq> y \<and> (x, y) \<in> r)"
blanchet@49312
   206
proof (induct X rule: finite_induct)
blanchet@49312
   207
  case empty thus ?case unfolding cinfinite_def using ex_in_conv[of "Field r"] finite.emptyI by auto
blanchet@49312
   208
next
blanchet@49312
   209
  case (insert x X)
blanchet@49312
   210
  then obtain y where y: "y \<in> Field r" "\<forall>x \<in> X. (x \<noteq> y \<and> (x, y) \<in> r)" by blast
blanchet@49312
   211
  then obtain z where z: "z \<in> Field r" "x \<noteq> z \<and> (x, z) \<in> r" "y \<noteq> z \<and> (y, z) \<in> r"
blanchet@49312
   212
    using Cinfinite_limit2[OF _ y(1) insert(5), of x] insert(4) by blast
blanchet@49326
   213
  show ?case
blanchet@49326
   214
    apply (intro bexI ballI)
blanchet@49326
   215
    apply (erule insertE)
blanchet@49326
   216
    apply hypsubst
blanchet@49326
   217
    apply (rule z(2))
blanchet@49326
   218
    using Card_order_trans[OF insert(5)[THEN conjunct2]] y(2) z(3)
blanchet@49326
   219
    apply blast
blanchet@49326
   220
    apply (rule z(1))
blanchet@49326
   221
    done
blanchet@49312
   222
qed
blanchet@49312
   223
blanchet@49312
   224
lemma insert_subsetI: "\<lbrakk>x \<in> A; X \<subseteq> A\<rbrakk> \<Longrightarrow> insert x X \<subseteq> A"
blanchet@49312
   225
by auto
blanchet@49312
   226
blanchet@49312
   227
(*helps resolution*)
blanchet@49312
   228
lemma well_order_induct_imp:
blanchet@49312
   229
  "wo_rel r \<Longrightarrow> (\<And>x. \<forall>y. y \<noteq> x \<and> (y, x) \<in> r \<longrightarrow> y \<in> Field r \<longrightarrow> P y \<Longrightarrow> x \<in> Field r \<longrightarrow> P x) \<Longrightarrow>
blanchet@49312
   230
     x \<in> Field r \<longrightarrow> P x"
blanchet@49312
   231
by (erule wo_rel.well_order_induct)
blanchet@49312
   232
blanchet@49312
   233
lemma meta_spec2:
blanchet@49312
   234
  assumes "(\<And>x y. PROP P x y)"
blanchet@49312
   235
  shows "PROP P x y"
blanchet@55084
   236
by (rule assms)
blanchet@49312
   237
traytel@54841
   238
lemma nchotomy_relcomppE:
traytel@55811
   239
  assumes "\<And>y. \<exists>x. y = f x" "(r OO s) a c" "\<And>b. r a (f b) \<Longrightarrow> s (f b) c \<Longrightarrow> P"
traytel@55811
   240
  shows P
traytel@55811
   241
proof (rule relcompp.cases[OF assms(2)], hypsubst)
traytel@55811
   242
  fix b assume "r a b" "s b c"
traytel@55811
   243
  moreover from assms(1) obtain b' where "b = f b'" by blast
traytel@55811
   244
  ultimately show P by (blast intro: assms(3))
traytel@55811
   245
qed
traytel@54841
   246
blanchet@55066
   247
lemma vimage2p_fun_rel: "fun_rel (vimage2p f g R) R f g"
traytel@52731
   248
  unfolding fun_rel_def vimage2p_def by auto
traytel@52731
   249
traytel@52731
   250
lemma predicate2D_vimage2p: "\<lbrakk>R \<le> vimage2p f g S; R x y\<rbrakk> \<Longrightarrow> S (f x) (g y)"
traytel@52731
   251
  unfolding vimage2p_def by auto
traytel@52731
   252
blanchet@55084
   253
lemma id_transfer: "fun_rel A A id id"
blanchet@55084
   254
unfolding fun_rel_def by simp
blanchet@55084
   255
traytel@55770
   256
lemma ssubst_Pair_rhs: "\<lbrakk>(r, s) \<in> R; s' = s\<rbrakk> \<Longrightarrow> (r, s') \<in> R"
traytel@55770
   257
  by simp
traytel@55770
   258
blanchet@55062
   259
ML_file "Tools/BNF/bnf_lfp_util.ML"
blanchet@55062
   260
ML_file "Tools/BNF/bnf_lfp_tactics.ML"
blanchet@55062
   261
ML_file "Tools/BNF/bnf_lfp.ML"
blanchet@55062
   262
ML_file "Tools/BNF/bnf_lfp_compat.ML"
blanchet@55571
   263
ML_file "Tools/BNF/bnf_lfp_rec_sugar_more.ML"
blanchet@49309
   264
blanchet@55084
   265
hide_fact (open) id_transfer
blanchet@55084
   266
blanchet@48975
   267
end