src/HOL/BNF/BNF_LFP.thy
author blanchet
Fri Aug 30 12:05:22 2013 +0200 (2013-08-30)
changeset 53305 29c267cb9314
parent 52913 2d2d9d1de1a9
child 53309 42a99f732a40
permissions -rw-r--r--
rationalized files
blanchet@49509
     1
(*  Title:      HOL/BNF/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@51850
    13
imports BNF_FP_Basic
blanchet@48975
    14
keywords
blanchet@53305
    15
  "datatype_new" :: thy_decl and
blanchet@53305
    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@49312
    32
lemma underS_I: "\<lbrakk>i \<noteq> j; (i, j) \<in> R\<rbrakk> \<Longrightarrow> i \<in> rel.underS R j"
blanchet@49312
    33
unfolding rel.underS_def by simp
blanchet@49312
    34
blanchet@49312
    35
lemma underS_E: "i \<in> rel.underS R j \<Longrightarrow> i \<noteq> j \<and> (i, j) \<in> R"
blanchet@49312
    36
unfolding rel.underS_def by simp
blanchet@49312
    37
blanchet@49312
    38
lemma underS_Field: "i \<in> rel.underS R j \<Longrightarrow> i \<in> Field R"
blanchet@49312
    39
unfolding rel.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@51739
    53
lemma convol_expand_snd': "(fst o f = g) \<Longrightarrow> (h = snd o f) \<longleftrightarrow> (<g, h> = f)"
traytel@51739
    54
  by (metis convol_expand_snd snd_convol)
traytel@51739
    55
blanchet@49312
    56
definition inver where
blanchet@49312
    57
  "inver g f A = (ALL a : A. g (f a) = a)"
blanchet@49312
    58
blanchet@49312
    59
lemma bij_betw_iff_ex:
blanchet@49312
    60
  "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
    61
proof (rule iffI)
blanchet@49312
    62
  assume ?L
blanchet@49312
    63
  hence f: "f ` A = B" and inj_f: "inj_on f A" unfolding bij_betw_def by auto
blanchet@49312
    64
  let ?phi = "% b a. a : A \<and> f a = b"
blanchet@49312
    65
  have "ALL b : B. EX a. ?phi b a" using f by blast
blanchet@49312
    66
  then obtain g where g: "ALL b : B. g b : A \<and> f (g b) = b"
blanchet@49312
    67
    using bchoice[of B ?phi] by blast
blanchet@49312
    68
  hence gg: "ALL b : f ` A. g b : A \<and> f (g b) = b" using f by blast
blanchet@49326
    69
  have gf: "inver g f A" unfolding inver_def
blanchet@49326
    70
    by (metis (no_types) gg imageI[of _ A f] the_inv_into_f_f[OF inj_f])
blanchet@49312
    71
  moreover have "g ` B \<le> A \<and> inver f g B" using g unfolding inver_def by blast
blanchet@49312
    72
  moreover have "A \<le> g ` B"
blanchet@49312
    73
  proof safe
blanchet@49312
    74
    fix a assume a: "a : A"
blanchet@49312
    75
    hence "f a : B" using f by auto
blanchet@49312
    76
    moreover have "a = g (f a)" using a gf unfolding inver_def by auto
blanchet@49312
    77
    ultimately show "a : g ` B" by blast
blanchet@49312
    78
  qed
blanchet@49312
    79
  ultimately show ?R by blast
blanchet@49312
    80
next
blanchet@49312
    81
  assume ?R
blanchet@49312
    82
  then obtain g where g: "g ` B = A \<and> inver g f A \<and> inver f g B" by blast
blanchet@49312
    83
  show ?L unfolding bij_betw_def
blanchet@49312
    84
  proof safe
blanchet@49312
    85
    show "inj_on f A" unfolding inj_on_def
blanchet@49312
    86
    proof safe
blanchet@49312
    87
      fix a1 a2 assume a: "a1 : A"  "a2 : A" and "f a1 = f a2"
blanchet@49312
    88
      hence "g (f a1) = g (f a2)" by simp
blanchet@49312
    89
      thus "a1 = a2" using a g unfolding inver_def by simp
blanchet@49312
    90
    qed
blanchet@49312
    91
  next
blanchet@49312
    92
    fix a assume "a : A"
blanchet@49312
    93
    then obtain b where b: "b : B" and a: "a = g b" using g by blast
blanchet@49312
    94
    hence "b = f (g b)" using g unfolding inver_def by auto
blanchet@49312
    95
    thus "f a : B" unfolding a using b by simp
blanchet@49312
    96
  next
blanchet@49312
    97
    fix b assume "b : B"
blanchet@49312
    98
    hence "g b : A \<and> b = f (g b)" using g unfolding inver_def by auto
blanchet@49312
    99
    thus "b : f ` A" by auto
blanchet@49312
   100
  qed
blanchet@49312
   101
qed
blanchet@49312
   102
blanchet@49312
   103
lemma bij_betw_ex_weakE:
blanchet@49312
   104
  "\<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
   105
by (auto simp only: bij_betw_iff_ex)
blanchet@49312
   106
blanchet@49312
   107
lemma inver_surj: "\<lbrakk>g ` B \<subseteq> A; f ` A \<subseteq> B; inver g f A\<rbrakk> \<Longrightarrow> g ` B = A"
blanchet@49312
   108
unfolding inver_def by auto (rule rev_image_eqI, auto)
blanchet@49312
   109
blanchet@49312
   110
lemma inver_mono: "\<lbrakk>A \<subseteq> B; inver f g B\<rbrakk> \<Longrightarrow> inver f g A"
blanchet@49312
   111
unfolding inver_def by auto
blanchet@49312
   112
blanchet@49312
   113
lemma inver_pointfree: "inver f g A = (\<forall>a \<in> A. (f o g) a = a)"
blanchet@49312
   114
unfolding inver_def by simp
blanchet@49312
   115
blanchet@49312
   116
lemma bij_betwE: "bij_betw f A B \<Longrightarrow> \<forall>a\<in>A. f a \<in> B"
blanchet@49312
   117
unfolding bij_betw_def by auto
blanchet@49312
   118
blanchet@49312
   119
lemma bij_betw_imageE: "bij_betw f A B \<Longrightarrow> f ` A = B"
blanchet@49312
   120
unfolding bij_betw_def by auto
blanchet@49312
   121
blanchet@49312
   122
lemma inverE: "\<lbrakk>inver f f' A; x \<in> A\<rbrakk> \<Longrightarrow> f (f' x) = x"
blanchet@49312
   123
unfolding inver_def by auto
blanchet@49312
   124
blanchet@49312
   125
lemma bij_betw_inver1: "bij_betw f A B \<Longrightarrow> inver (inv_into A f) f A"
blanchet@49312
   126
unfolding bij_betw_def inver_def by auto
blanchet@49312
   127
blanchet@49312
   128
lemma bij_betw_inver2: "bij_betw f A B \<Longrightarrow> inver f (inv_into A f) B"
blanchet@49312
   129
unfolding bij_betw_def inver_def by auto
blanchet@49312
   130
blanchet@49312
   131
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
   132
by (drule bij_betw_imageE, unfold bij_betw_iff_ex) blast
blanchet@49312
   133
blanchet@49312
   134
lemma bij_betwI':
blanchet@49312
   135
  "\<lbrakk>\<And>x y. \<lbrakk>x \<in> X; y \<in> X\<rbrakk> \<Longrightarrow> (f x = f y) = (x = y);
blanchet@49312
   136
    \<And>x. x \<in> X \<Longrightarrow> f x \<in> Y;
blanchet@49312
   137
    \<And>y. y \<in> Y \<Longrightarrow> \<exists>x \<in> X. y = f x\<rbrakk> \<Longrightarrow> bij_betw f X Y"
blanchet@49326
   138
unfolding bij_betw_def inj_on_def
blanchet@49326
   139
apply (rule conjI)
blanchet@49326
   140
 apply blast
blanchet@49326
   141
by (erule thin_rl) blast
blanchet@49312
   142
blanchet@49312
   143
lemma surj_fun_eq:
blanchet@49312
   144
  assumes surj_on: "f ` X = UNIV" and eq_on: "\<forall>x \<in> X. (g1 o f) x = (g2 o f) x"
blanchet@49312
   145
  shows "g1 = g2"
blanchet@49312
   146
proof (rule ext)
blanchet@49312
   147
  fix y
blanchet@49312
   148
  from surj_on obtain x where "x \<in> X" and "y = f x" by blast
blanchet@49312
   149
  thus "g1 y = g2 y" using eq_on by simp
blanchet@49312
   150
qed
blanchet@49312
   151
blanchet@49312
   152
lemma Card_order_wo_rel: "Card_order r \<Longrightarrow> wo_rel r"
blanchet@49514
   153
unfolding wo_rel_def card_order_on_def by blast
blanchet@49312
   154
blanchet@49312
   155
lemma Cinfinite_limit: "\<lbrakk>x \<in> Field r; Cinfinite r\<rbrakk> \<Longrightarrow>
blanchet@49312
   156
  \<exists>y \<in> Field r. x \<noteq> y \<and> (x, y) \<in> r"
blanchet@49312
   157
unfolding cinfinite_def by (auto simp add: infinite_Card_order_limit)
blanchet@49312
   158
blanchet@49312
   159
lemma Card_order_trans:
blanchet@49312
   160
  "\<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
   161
unfolding card_order_on_def well_order_on_def linear_order_on_def
blanchet@49312
   162
  partial_order_on_def preorder_on_def trans_def antisym_def by blast
blanchet@49312
   163
blanchet@49312
   164
lemma Cinfinite_limit2:
blanchet@49312
   165
 assumes x1: "x1 \<in> Field r" and x2: "x2 \<in> Field r" and r: "Cinfinite r"
blanchet@49312
   166
 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
   167
proof -
blanchet@49312
   168
  from r have trans: "trans r" and total: "Total r" and antisym: "antisym r"
blanchet@49312
   169
    unfolding card_order_on_def well_order_on_def linear_order_on_def
blanchet@49312
   170
      partial_order_on_def preorder_on_def by auto
blanchet@49312
   171
  obtain y1 where y1: "y1 \<in> Field r" "x1 \<noteq> y1" "(x1, y1) \<in> r"
blanchet@49312
   172
    using Cinfinite_limit[OF x1 r] by blast
blanchet@49312
   173
  obtain y2 where y2: "y2 \<in> Field r" "x2 \<noteq> y2" "(x2, y2) \<in> r"
blanchet@49312
   174
    using Cinfinite_limit[OF x2 r] by blast
blanchet@49312
   175
  show ?thesis
blanchet@49312
   176
  proof (cases "y1 = y2")
blanchet@49312
   177
    case True with y1 y2 show ?thesis by blast
blanchet@49312
   178
  next
blanchet@49312
   179
    case False
blanchet@49312
   180
    with y1(1) y2(1) total have "(y1, y2) \<in> r \<or> (y2, y1) \<in> r"
blanchet@49312
   181
      unfolding total_on_def by auto
blanchet@49312
   182
    thus ?thesis
blanchet@49312
   183
    proof
blanchet@49312
   184
      assume *: "(y1, y2) \<in> r"
blanchet@49312
   185
      with trans y1(3) have "(x1, y2) \<in> r" unfolding trans_def by blast
blanchet@49312
   186
      with False y1 y2 * antisym show ?thesis by (cases "x1 = y2") (auto simp: antisym_def)
blanchet@49312
   187
    next
blanchet@49312
   188
      assume *: "(y2, y1) \<in> r"
blanchet@49312
   189
      with trans y2(3) have "(x2, y1) \<in> r" unfolding trans_def by blast
blanchet@49312
   190
      with False y1 y2 * antisym show ?thesis by (cases "x2 = y1") (auto simp: antisym_def)
blanchet@49312
   191
    qed
blanchet@49312
   192
  qed
blanchet@49312
   193
qed
blanchet@49312
   194
blanchet@49312
   195
lemma Cinfinite_limit_finite: "\<lbrakk>finite X; X \<subseteq> Field r; Cinfinite r\<rbrakk>
blanchet@49312
   196
 \<Longrightarrow> \<exists>y \<in> Field r. \<forall>x \<in> X. (x \<noteq> y \<and> (x, y) \<in> r)"
blanchet@49312
   197
proof (induct X rule: finite_induct)
blanchet@49312
   198
  case empty thus ?case unfolding cinfinite_def using ex_in_conv[of "Field r"] finite.emptyI by auto
blanchet@49312
   199
next
blanchet@49312
   200
  case (insert x X)
blanchet@49312
   201
  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
   202
  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
   203
    using Cinfinite_limit2[OF _ y(1) insert(5), of x] insert(4) by blast
blanchet@49326
   204
  show ?case
blanchet@49326
   205
    apply (intro bexI ballI)
blanchet@49326
   206
    apply (erule insertE)
blanchet@49326
   207
    apply hypsubst
blanchet@49326
   208
    apply (rule z(2))
blanchet@49326
   209
    using Card_order_trans[OF insert(5)[THEN conjunct2]] y(2) z(3)
blanchet@49326
   210
    apply blast
blanchet@49326
   211
    apply (rule z(1))
blanchet@49326
   212
    done
blanchet@49312
   213
qed
blanchet@49312
   214
blanchet@49312
   215
lemma insert_subsetI: "\<lbrakk>x \<in> A; X \<subseteq> A\<rbrakk> \<Longrightarrow> insert x X \<subseteq> A"
blanchet@49312
   216
by auto
blanchet@49312
   217
blanchet@49312
   218
(*helps resolution*)
blanchet@49312
   219
lemma well_order_induct_imp:
blanchet@49312
   220
  "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
   221
     x \<in> Field r \<longrightarrow> P x"
blanchet@49312
   222
by (erule wo_rel.well_order_induct)
blanchet@49312
   223
blanchet@49312
   224
lemma meta_spec2:
blanchet@49312
   225
  assumes "(\<And>x y. PROP P x y)"
blanchet@49312
   226
  shows "PROP P x y"
blanchet@49312
   227
by (rule `(\<And>x y. PROP P x y)`)
blanchet@49312
   228
traytel@52731
   229
lemma vimage2p_fun_rel: "(fun_rel (vimage2p f g R) R) f g"
traytel@52731
   230
  unfolding fun_rel_def vimage2p_def by auto
traytel@52731
   231
traytel@52731
   232
lemma predicate2D_vimage2p: "\<lbrakk>R \<le> vimage2p f g S; R x y\<rbrakk> \<Longrightarrow> S (f x) (g y)"
traytel@52731
   233
  unfolding vimage2p_def by auto
traytel@52731
   234
blanchet@49309
   235
ML_file "Tools/bnf_lfp_util.ML"
blanchet@49309
   236
ML_file "Tools/bnf_lfp_tactics.ML"
blanchet@49309
   237
ML_file "Tools/bnf_lfp.ML"
blanchet@53305
   238
ML_file "Tools/bnf_lfp_compat.ML"
blanchet@49309
   239
blanchet@48975
   240
end