src/HOL/Codatatype/BNF_LFP.thy
author blanchet
Wed Sep 12 06:27:48 2012 +0200 (2012-09-12)
changeset 49312 c874ff5658dc
parent 49309 f20b24214ac2
child 49326 a063a96c8662
permissions -rw-r--r--
moved theorems closer to where they are used
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@49312
    64
  have gf: "inver g f A" unfolding inver_def by (metis gg imageI inj_f the_inv_into_f_f)
blanchet@49312
    65
  moreover have "g ` B \<le> A \<and> inver f g B" using g unfolding inver_def by blast
blanchet@49312
    66
  moreover have "A \<le> g ` B"
blanchet@49312
    67
  proof safe
blanchet@49312
    68
    fix a assume a: "a : A"
blanchet@49312
    69
    hence "f a : B" using f by auto
blanchet@49312
    70
    moreover have "a = g (f a)" using a gf unfolding inver_def by auto
blanchet@49312
    71
    ultimately show "a : g ` B" by blast
blanchet@49312
    72
  qed
blanchet@49312
    73
  ultimately show ?R by blast
blanchet@49312
    74
next
blanchet@49312
    75
  assume ?R
blanchet@49312
    76
  then obtain g where g: "g ` B = A \<and> inver g f A \<and> inver f g B" by blast
blanchet@49312
    77
  show ?L unfolding bij_betw_def
blanchet@49312
    78
  proof safe
blanchet@49312
    79
    show "inj_on f A" unfolding inj_on_def
blanchet@49312
    80
    proof safe
blanchet@49312
    81
      fix a1 a2 assume a: "a1 : A"  "a2 : A" and "f a1 = f a2"
blanchet@49312
    82
      hence "g (f a1) = g (f a2)" by simp
blanchet@49312
    83
      thus "a1 = a2" using a g unfolding inver_def by simp
blanchet@49312
    84
    qed
blanchet@49312
    85
  next
blanchet@49312
    86
    fix a assume "a : A"
blanchet@49312
    87
    then obtain b where b: "b : B" and a: "a = g b" using g by blast
blanchet@49312
    88
    hence "b = f (g b)" using g unfolding inver_def by auto
blanchet@49312
    89
    thus "f a : B" unfolding a using b by simp
blanchet@49312
    90
  next
blanchet@49312
    91
    fix b assume "b : B"
blanchet@49312
    92
    hence "g b : A \<and> b = f (g b)" using g unfolding inver_def by auto
blanchet@49312
    93
    thus "b : f ` A" by auto
blanchet@49312
    94
  qed
blanchet@49312
    95
qed
blanchet@49312
    96
blanchet@49312
    97
lemma bij_betw_ex_weakE:
blanchet@49312
    98
  "\<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
    99
by (auto simp only: bij_betw_iff_ex)
blanchet@49312
   100
blanchet@49312
   101
lemma inver_surj: "\<lbrakk>g ` B \<subseteq> A; f ` A \<subseteq> B; inver g f A\<rbrakk> \<Longrightarrow> g ` B = A"
blanchet@49312
   102
unfolding inver_def by auto (rule rev_image_eqI, auto)
blanchet@49312
   103
blanchet@49312
   104
lemma inver_mono: "\<lbrakk>A \<subseteq> B; inver f g B\<rbrakk> \<Longrightarrow> inver f g A"
blanchet@49312
   105
unfolding inver_def by auto
blanchet@49312
   106
blanchet@49312
   107
lemma inver_pointfree: "inver f g A = (\<forall>a \<in> A. (f o g) a = a)"
blanchet@49312
   108
unfolding inver_def by simp
blanchet@49312
   109
blanchet@49312
   110
lemma bij_betwE: "bij_betw f A B \<Longrightarrow> \<forall>a\<in>A. f a \<in> B"
blanchet@49312
   111
unfolding bij_betw_def by auto
blanchet@49312
   112
blanchet@49312
   113
lemma bij_betw_imageE: "bij_betw f A B \<Longrightarrow> f ` A = B"
blanchet@49312
   114
unfolding bij_betw_def by auto
blanchet@49312
   115
blanchet@49312
   116
lemma inverE: "\<lbrakk>inver f f' A; x \<in> A\<rbrakk> \<Longrightarrow> f (f' x) = x"
blanchet@49312
   117
unfolding inver_def by auto
blanchet@49312
   118
blanchet@49312
   119
lemma bij_betw_inver1: "bij_betw f A B \<Longrightarrow> inver (inv_into A f) f A"
blanchet@49312
   120
unfolding bij_betw_def inver_def by auto
blanchet@49312
   121
blanchet@49312
   122
lemma bij_betw_inver2: "bij_betw f A B \<Longrightarrow> inver f (inv_into A f) B"
blanchet@49312
   123
unfolding bij_betw_def inver_def by auto
blanchet@49312
   124
blanchet@49312
   125
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@49312
   126
by (metis bij_betw_iff_ex bij_betw_imageE)
blanchet@49312
   127
blanchet@49312
   128
lemma bij_betwI':
blanchet@49312
   129
  "\<lbrakk>\<And>x y. \<lbrakk>x \<in> X; y \<in> X\<rbrakk> \<Longrightarrow> (f x = f y) = (x = y);
blanchet@49312
   130
    \<And>x. x \<in> X \<Longrightarrow> f x \<in> Y;
blanchet@49312
   131
    \<And>y. y \<in> Y \<Longrightarrow> \<exists>x \<in> X. y = f x\<rbrakk> \<Longrightarrow> bij_betw f X Y"
blanchet@49312
   132
unfolding bij_betw_def inj_on_def by auto (metis rev_image_eqI)
blanchet@49312
   133
blanchet@49312
   134
lemma surj_fun_eq:
blanchet@49312
   135
  assumes surj_on: "f ` X = UNIV" and eq_on: "\<forall>x \<in> X. (g1 o f) x = (g2 o f) x"
blanchet@49312
   136
  shows "g1 = g2"
blanchet@49312
   137
proof (rule ext)
blanchet@49312
   138
  fix y
blanchet@49312
   139
  from surj_on obtain x where "x \<in> X" and "y = f x" by blast
blanchet@49312
   140
  thus "g1 y = g2 y" using eq_on by simp
blanchet@49312
   141
qed
blanchet@49312
   142
blanchet@49312
   143
lemma Card_order_wo_rel: "Card_order r \<Longrightarrow> wo_rel r"
blanchet@49312
   144
unfolding wo_rel_def card_order_on_def by blast 
blanchet@49312
   145
blanchet@49312
   146
lemma Cinfinite_limit: "\<lbrakk>x \<in> Field r; Cinfinite r\<rbrakk> \<Longrightarrow>
blanchet@49312
   147
  \<exists>y \<in> Field r. x \<noteq> y \<and> (x, y) \<in> r"
blanchet@49312
   148
unfolding cinfinite_def by (auto simp add: infinite_Card_order_limit)
blanchet@49312
   149
blanchet@49312
   150
lemma Card_order_trans:
blanchet@49312
   151
  "\<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
   152
unfolding card_order_on_def well_order_on_def linear_order_on_def
blanchet@49312
   153
  partial_order_on_def preorder_on_def trans_def antisym_def by blast
blanchet@49312
   154
blanchet@49312
   155
lemma Cinfinite_limit2:
blanchet@49312
   156
 assumes x1: "x1 \<in> Field r" and x2: "x2 \<in> Field r" and r: "Cinfinite r"
blanchet@49312
   157
 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
   158
proof -
blanchet@49312
   159
  from r have trans: "trans r" and total: "Total r" and antisym: "antisym r"
blanchet@49312
   160
    unfolding card_order_on_def well_order_on_def linear_order_on_def
blanchet@49312
   161
      partial_order_on_def preorder_on_def by auto
blanchet@49312
   162
  obtain y1 where y1: "y1 \<in> Field r" "x1 \<noteq> y1" "(x1, y1) \<in> r"
blanchet@49312
   163
    using Cinfinite_limit[OF x1 r] by blast
blanchet@49312
   164
  obtain y2 where y2: "y2 \<in> Field r" "x2 \<noteq> y2" "(x2, y2) \<in> r"
blanchet@49312
   165
    using Cinfinite_limit[OF x2 r] by blast
blanchet@49312
   166
  show ?thesis
blanchet@49312
   167
  proof (cases "y1 = y2")
blanchet@49312
   168
    case True with y1 y2 show ?thesis by blast
blanchet@49312
   169
  next
blanchet@49312
   170
    case False
blanchet@49312
   171
    with y1(1) y2(1) total have "(y1, y2) \<in> r \<or> (y2, y1) \<in> r"
blanchet@49312
   172
      unfolding total_on_def by auto
blanchet@49312
   173
    thus ?thesis
blanchet@49312
   174
    proof
blanchet@49312
   175
      assume *: "(y1, y2) \<in> r"
blanchet@49312
   176
      with trans y1(3) have "(x1, y2) \<in> r" unfolding trans_def by blast
blanchet@49312
   177
      with False y1 y2 * antisym show ?thesis by (cases "x1 = y2") (auto simp: antisym_def)
blanchet@49312
   178
    next
blanchet@49312
   179
      assume *: "(y2, y1) \<in> r"
blanchet@49312
   180
      with trans y2(3) have "(x2, y1) \<in> r" unfolding trans_def by blast
blanchet@49312
   181
      with False y1 y2 * antisym show ?thesis by (cases "x2 = y1") (auto simp: antisym_def)
blanchet@49312
   182
    qed
blanchet@49312
   183
  qed
blanchet@49312
   184
qed
blanchet@49312
   185
blanchet@49312
   186
lemma Cinfinite_limit_finite: "\<lbrakk>finite X; X \<subseteq> Field r; Cinfinite r\<rbrakk>
blanchet@49312
   187
 \<Longrightarrow> \<exists>y \<in> Field r. \<forall>x \<in> X. (x \<noteq> y \<and> (x, y) \<in> r)"
blanchet@49312
   188
proof (induct X rule: finite_induct)
blanchet@49312
   189
  case empty thus ?case unfolding cinfinite_def using ex_in_conv[of "Field r"] finite.emptyI by auto
blanchet@49312
   190
next
blanchet@49312
   191
  case (insert x X)
blanchet@49312
   192
  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
   193
  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
   194
    using Cinfinite_limit2[OF _ y(1) insert(5), of x] insert(4) by blast
blanchet@49312
   195
  show ?case by (metis Card_order_trans insert(5) insertE y(2) z)
blanchet@49312
   196
qed
blanchet@49312
   197
blanchet@49312
   198
lemma insert_subsetI: "\<lbrakk>x \<in> A; X \<subseteq> A\<rbrakk> \<Longrightarrow> insert x X \<subseteq> A"
blanchet@49312
   199
by auto
blanchet@49312
   200
blanchet@49312
   201
(*helps resolution*)
blanchet@49312
   202
lemma well_order_induct_imp:
blanchet@49312
   203
  "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
   204
     x \<in> Field r \<longrightarrow> P x"
blanchet@49312
   205
by (erule wo_rel.well_order_induct)
blanchet@49312
   206
blanchet@49312
   207
lemma meta_spec2:
blanchet@49312
   208
  assumes "(\<And>x y. PROP P x y)"
blanchet@49312
   209
  shows "PROP P x y"
blanchet@49312
   210
by (rule `(\<And>x y. PROP P x y)`)
blanchet@49312
   211
blanchet@49309
   212
ML_file "Tools/bnf_lfp_util.ML"
blanchet@49309
   213
ML_file "Tools/bnf_lfp_tactics.ML"
blanchet@49309
   214
ML_file "Tools/bnf_lfp.ML"
blanchet@49309
   215
blanchet@48975
   216
end