src/HOL/Codatatype/BNF_LFP.thy
author blanchet
Wed Sep 12 10:36:00 2012 +0200 (2012-09-12)
changeset 49326 a063a96c8662
parent 49312 c874ff5658dc
child 49509 163914705f8d
permissions -rw-r--r--
got rid of metis calls
blanchet@48975
     1
(*  Title:      HOL/Codatatype/BNF_LFP.thy
blanchet@48975
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@48975
     3
    Copyright   2012
blanchet@48975
     4
blanchet@48975
     5
Least fixed point operation on bounded natural functors.
blanchet@48975
     6
*)
blanchet@48975
     7
blanchet@48975
     8
header {* Least Fixed Point Operation on Bounded Natural Functors *}
blanchet@48975
     9
blanchet@48975
    10
theory BNF_LFP
blanchet@49308
    11
imports BNF_FP
blanchet@48975
    12
keywords
blanchet@49308
    13
  "data_raw" :: thy_decl and
blanchet@49308
    14
  "data" :: thy_decl
blanchet@48975
    15
begin
blanchet@48975
    16
blanchet@49312
    17
lemma subset_emptyI: "(\<And>x. x \<in> A \<Longrightarrow> False) \<Longrightarrow> A \<subseteq> {}"
blanchet@49312
    18
by blast
blanchet@49312
    19
blanchet@49312
    20
lemma image_Collect_subsetI:
blanchet@49312
    21
  "(\<And>x. P x \<Longrightarrow> f x \<in> B) \<Longrightarrow> f ` {x. P x} \<subseteq> B"
blanchet@49312
    22
by blast
blanchet@49312
    23
blanchet@49312
    24
lemma Collect_restrict: "{x. x \<in> X \<and> P x} \<subseteq> X"
blanchet@49312
    25
by auto
blanchet@49312
    26
blanchet@49312
    27
lemma prop_restrict: "\<lbrakk>x \<in> Z; Z \<subseteq> {x. x \<in> X \<and> P x}\<rbrakk> \<Longrightarrow> P x"
blanchet@49312
    28
by auto
blanchet@49312
    29
blanchet@49312
    30
lemma underS_I: "\<lbrakk>i \<noteq> j; (i, j) \<in> R\<rbrakk> \<Longrightarrow> i \<in> rel.underS R j"
blanchet@49312
    31
unfolding rel.underS_def by simp
blanchet@49312
    32
blanchet@49312
    33
lemma underS_E: "i \<in> rel.underS R j \<Longrightarrow> i \<noteq> j \<and> (i, j) \<in> R"
blanchet@49312
    34
unfolding rel.underS_def by simp
blanchet@49312
    35
blanchet@49312
    36
lemma underS_Field: "i \<in> rel.underS R j \<Longrightarrow> i \<in> Field R"
blanchet@49312
    37
unfolding rel.underS_def Field_def by auto
blanchet@49312
    38
blanchet@49312
    39
lemma FieldI2: "(i, j) \<in> R \<Longrightarrow> j \<in> Field R"
blanchet@49312
    40
unfolding Field_def by auto
blanchet@49312
    41
blanchet@49312
    42
lemma fst_convol': "fst (<f, g> x) = f x"
blanchet@49312
    43
using fst_convol unfolding convol_def by simp
blanchet@49312
    44
blanchet@49312
    45
lemma snd_convol': "snd (<f, g> x) = g x"
blanchet@49312
    46
using snd_convol unfolding convol_def by simp
blanchet@49312
    47
blanchet@49312
    48
lemma convol_expand_snd: "fst o f = g \<Longrightarrow>  <g, snd o f> = f"
blanchet@49312
    49
unfolding convol_def by auto
blanchet@49312
    50
blanchet@49312
    51
definition inver where
blanchet@49312
    52
  "inver g f A = (ALL a : A. g (f a) = a)"
blanchet@49312
    53
blanchet@49312
    54
lemma bij_betw_iff_ex:
blanchet@49312
    55
  "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
    56
proof (rule iffI)
blanchet@49312
    57
  assume ?L
blanchet@49312
    58
  hence f: "f ` A = B" and inj_f: "inj_on f A" unfolding bij_betw_def by auto
blanchet@49312
    59
  let ?phi = "% b a. a : A \<and> f a = b"
blanchet@49312
    60
  have "ALL b : B. EX a. ?phi b a" using f by blast
blanchet@49312
    61
  then obtain g where g: "ALL b : B. g b : A \<and> f (g b) = b"
blanchet@49312
    62
    using bchoice[of B ?phi] by blast
blanchet@49312
    63
  hence gg: "ALL b : f ` A. g b : A \<and> f (g b) = b" using f by blast
blanchet@49326
    64
  have gf: "inver g f A" unfolding inver_def
blanchet@49326
    65
    by (metis (no_types) gg imageI[of _ A f] the_inv_into_f_f[OF inj_f])
blanchet@49312
    66
  moreover have "g ` B \<le> A \<and> inver f g B" using g unfolding inver_def by blast
blanchet@49312
    67
  moreover have "A \<le> g ` B"
blanchet@49312
    68
  proof safe
blanchet@49312
    69
    fix a assume a: "a : A"
blanchet@49312
    70
    hence "f a : B" using f by auto
blanchet@49312
    71
    moreover have "a = g (f a)" using a gf unfolding inver_def by auto
blanchet@49312
    72
    ultimately show "a : g ` B" by blast
blanchet@49312
    73
  qed
blanchet@49312
    74
  ultimately show ?R by blast
blanchet@49312
    75
next
blanchet@49312
    76
  assume ?R
blanchet@49312
    77
  then obtain g where g: "g ` B = A \<and> inver g f A \<and> inver f g B" by blast
blanchet@49312
    78
  show ?L unfolding bij_betw_def
blanchet@49312
    79
  proof safe
blanchet@49312
    80
    show "inj_on f A" unfolding inj_on_def
blanchet@49312
    81
    proof safe
blanchet@49312
    82
      fix a1 a2 assume a: "a1 : A"  "a2 : A" and "f a1 = f a2"
blanchet@49312
    83
      hence "g (f a1) = g (f a2)" by simp
blanchet@49312
    84
      thus "a1 = a2" using a g unfolding inver_def by simp
blanchet@49312
    85
    qed
blanchet@49312
    86
  next
blanchet@49312
    87
    fix a assume "a : A"
blanchet@49312
    88
    then obtain b where b: "b : B" and a: "a = g b" using g by blast
blanchet@49312
    89
    hence "b = f (g b)" using g unfolding inver_def by auto
blanchet@49312
    90
    thus "f a : B" unfolding a using b by simp
blanchet@49312
    91
  next
blanchet@49312
    92
    fix b assume "b : B"
blanchet@49312
    93
    hence "g b : A \<and> b = f (g b)" using g unfolding inver_def by auto
blanchet@49312
    94
    thus "b : f ` A" by auto
blanchet@49312
    95
  qed
blanchet@49312
    96
qed
blanchet@49312
    97
blanchet@49312
    98
lemma bij_betw_ex_weakE:
blanchet@49312
    99
  "\<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
   100
by (auto simp only: bij_betw_iff_ex)
blanchet@49312
   101
blanchet@49312
   102
lemma inver_surj: "\<lbrakk>g ` B \<subseteq> A; f ` A \<subseteq> B; inver g f A\<rbrakk> \<Longrightarrow> g ` B = A"
blanchet@49312
   103
unfolding inver_def by auto (rule rev_image_eqI, auto)
blanchet@49312
   104
blanchet@49312
   105
lemma inver_mono: "\<lbrakk>A \<subseteq> B; inver f g B\<rbrakk> \<Longrightarrow> inver f g A"
blanchet@49312
   106
unfolding inver_def by auto
blanchet@49312
   107
blanchet@49312
   108
lemma inver_pointfree: "inver f g A = (\<forall>a \<in> A. (f o g) a = a)"
blanchet@49312
   109
unfolding inver_def by simp
blanchet@49312
   110
blanchet@49312
   111
lemma bij_betwE: "bij_betw f A B \<Longrightarrow> \<forall>a\<in>A. f a \<in> B"
blanchet@49312
   112
unfolding bij_betw_def by auto
blanchet@49312
   113
blanchet@49312
   114
lemma bij_betw_imageE: "bij_betw f A B \<Longrightarrow> f ` A = B"
blanchet@49312
   115
unfolding bij_betw_def by auto
blanchet@49312
   116
blanchet@49312
   117
lemma inverE: "\<lbrakk>inver f f' A; x \<in> A\<rbrakk> \<Longrightarrow> f (f' x) = x"
blanchet@49312
   118
unfolding inver_def by auto
blanchet@49312
   119
blanchet@49312
   120
lemma bij_betw_inver1: "bij_betw f A B \<Longrightarrow> inver (inv_into A f) f A"
blanchet@49312
   121
unfolding bij_betw_def inver_def by auto
blanchet@49312
   122
blanchet@49312
   123
lemma bij_betw_inver2: "bij_betw f A B \<Longrightarrow> inver f (inv_into A f) B"
blanchet@49312
   124
unfolding bij_betw_def inver_def by auto
blanchet@49312
   125
blanchet@49312
   126
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
   127
by (drule bij_betw_imageE, unfold bij_betw_iff_ex) blast
blanchet@49312
   128
blanchet@49312
   129
lemma bij_betwI':
blanchet@49312
   130
  "\<lbrakk>\<And>x y. \<lbrakk>x \<in> X; y \<in> X\<rbrakk> \<Longrightarrow> (f x = f y) = (x = y);
blanchet@49312
   131
    \<And>x. x \<in> X \<Longrightarrow> f x \<in> Y;
blanchet@49312
   132
    \<And>y. y \<in> Y \<Longrightarrow> \<exists>x \<in> X. y = f x\<rbrakk> \<Longrightarrow> bij_betw f X Y"
blanchet@49326
   133
unfolding bij_betw_def inj_on_def
blanchet@49326
   134
apply (rule conjI)
blanchet@49326
   135
 apply blast
blanchet@49326
   136
by (erule thin_rl) blast
blanchet@49312
   137
blanchet@49312
   138
lemma surj_fun_eq:
blanchet@49312
   139
  assumes surj_on: "f ` X = UNIV" and eq_on: "\<forall>x \<in> X. (g1 o f) x = (g2 o f) x"
blanchet@49312
   140
  shows "g1 = g2"
blanchet@49312
   141
proof (rule ext)
blanchet@49312
   142
  fix y
blanchet@49312
   143
  from surj_on obtain x where "x \<in> X" and "y = f x" by blast
blanchet@49312
   144
  thus "g1 y = g2 y" using eq_on by simp
blanchet@49312
   145
qed
blanchet@49312
   146
blanchet@49312
   147
lemma Card_order_wo_rel: "Card_order r \<Longrightarrow> wo_rel r"
blanchet@49312
   148
unfolding wo_rel_def card_order_on_def by blast 
blanchet@49312
   149
blanchet@49312
   150
lemma Cinfinite_limit: "\<lbrakk>x \<in> Field r; Cinfinite r\<rbrakk> \<Longrightarrow>
blanchet@49312
   151
  \<exists>y \<in> Field r. x \<noteq> y \<and> (x, y) \<in> r"
blanchet@49312
   152
unfolding cinfinite_def by (auto simp add: infinite_Card_order_limit)
blanchet@49312
   153
blanchet@49312
   154
lemma Card_order_trans:
blanchet@49312
   155
  "\<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
   156
unfolding card_order_on_def well_order_on_def linear_order_on_def
blanchet@49312
   157
  partial_order_on_def preorder_on_def trans_def antisym_def by blast
blanchet@49312
   158
blanchet@49312
   159
lemma Cinfinite_limit2:
blanchet@49312
   160
 assumes x1: "x1 \<in> Field r" and x2: "x2 \<in> Field r" and r: "Cinfinite r"
blanchet@49312
   161
 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
   162
proof -
blanchet@49312
   163
  from r have trans: "trans r" and total: "Total r" and antisym: "antisym r"
blanchet@49312
   164
    unfolding card_order_on_def well_order_on_def linear_order_on_def
blanchet@49312
   165
      partial_order_on_def preorder_on_def by auto
blanchet@49312
   166
  obtain y1 where y1: "y1 \<in> Field r" "x1 \<noteq> y1" "(x1, y1) \<in> r"
blanchet@49312
   167
    using Cinfinite_limit[OF x1 r] by blast
blanchet@49312
   168
  obtain y2 where y2: "y2 \<in> Field r" "x2 \<noteq> y2" "(x2, y2) \<in> r"
blanchet@49312
   169
    using Cinfinite_limit[OF x2 r] by blast
blanchet@49312
   170
  show ?thesis
blanchet@49312
   171
  proof (cases "y1 = y2")
blanchet@49312
   172
    case True with y1 y2 show ?thesis by blast
blanchet@49312
   173
  next
blanchet@49312
   174
    case False
blanchet@49312
   175
    with y1(1) y2(1) total have "(y1, y2) \<in> r \<or> (y2, y1) \<in> r"
blanchet@49312
   176
      unfolding total_on_def by auto
blanchet@49312
   177
    thus ?thesis
blanchet@49312
   178
    proof
blanchet@49312
   179
      assume *: "(y1, y2) \<in> r"
blanchet@49312
   180
      with trans y1(3) have "(x1, y2) \<in> r" unfolding trans_def by blast
blanchet@49312
   181
      with False y1 y2 * antisym show ?thesis by (cases "x1 = y2") (auto simp: antisym_def)
blanchet@49312
   182
    next
blanchet@49312
   183
      assume *: "(y2, y1) \<in> r"
blanchet@49312
   184
      with trans y2(3) have "(x2, y1) \<in> r" unfolding trans_def by blast
blanchet@49312
   185
      with False y1 y2 * antisym show ?thesis by (cases "x2 = y1") (auto simp: antisym_def)
blanchet@49312
   186
    qed
blanchet@49312
   187
  qed
blanchet@49312
   188
qed
blanchet@49312
   189
blanchet@49312
   190
lemma Cinfinite_limit_finite: "\<lbrakk>finite X; X \<subseteq> Field r; Cinfinite r\<rbrakk>
blanchet@49312
   191
 \<Longrightarrow> \<exists>y \<in> Field r. \<forall>x \<in> X. (x \<noteq> y \<and> (x, y) \<in> r)"
blanchet@49312
   192
proof (induct X rule: finite_induct)
blanchet@49312
   193
  case empty thus ?case unfolding cinfinite_def using ex_in_conv[of "Field r"] finite.emptyI by auto
blanchet@49312
   194
next
blanchet@49312
   195
  case (insert x X)
blanchet@49312
   196
  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
   197
  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
   198
    using Cinfinite_limit2[OF _ y(1) insert(5), of x] insert(4) by blast
blanchet@49326
   199
  show ?case
blanchet@49326
   200
    apply (intro bexI ballI)
blanchet@49326
   201
    apply (erule insertE)
blanchet@49326
   202
    apply hypsubst
blanchet@49326
   203
    apply (rule z(2))
blanchet@49326
   204
    using Card_order_trans[OF insert(5)[THEN conjunct2]] y(2) z(3)
blanchet@49326
   205
    apply blast
blanchet@49326
   206
    apply (rule z(1))
blanchet@49326
   207
    done
blanchet@49312
   208
qed
blanchet@49312
   209
blanchet@49312
   210
lemma insert_subsetI: "\<lbrakk>x \<in> A; X \<subseteq> A\<rbrakk> \<Longrightarrow> insert x X \<subseteq> A"
blanchet@49312
   211
by auto
blanchet@49312
   212
blanchet@49312
   213
(*helps resolution*)
blanchet@49312
   214
lemma well_order_induct_imp:
blanchet@49312
   215
  "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
   216
     x \<in> Field r \<longrightarrow> P x"
blanchet@49312
   217
by (erule wo_rel.well_order_induct)
blanchet@49312
   218
blanchet@49312
   219
lemma meta_spec2:
blanchet@49312
   220
  assumes "(\<And>x y. PROP P x y)"
blanchet@49312
   221
  shows "PROP P x y"
blanchet@49312
   222
by (rule `(\<And>x y. PROP P x y)`)
blanchet@49312
   223
blanchet@49309
   224
ML_file "Tools/bnf_lfp_util.ML"
blanchet@49309
   225
ML_file "Tools/bnf_lfp_tactics.ML"
blanchet@49309
   226
ML_file "Tools/bnf_lfp.ML"
blanchet@49309
   227
blanchet@48975
   228
end