src/HOL/BNF_LFP.thy
author traytel
Wed Feb 26 23:09:29 2014 +0100 (2014-02-26)
changeset 55770 f2cf7f92c9ac
parent 55575 a5e33e18fb5c
child 55811 aa1acc25126b
permissions -rw-r--r--
intermediate typedef for the type of the bound (local to lfp)
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@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"
traytel@53695
   138
unfolding bij_betw_def inj_on_def by blast
blanchet@49312
   139
blanchet@49312
   140
lemma surj_fun_eq:
blanchet@49312
   141
  assumes surj_on: "f ` X = UNIV" and eq_on: "\<forall>x \<in> X. (g1 o f) x = (g2 o f) x"
blanchet@49312
   142
  shows "g1 = g2"
blanchet@49312
   143
proof (rule ext)
blanchet@49312
   144
  fix y
blanchet@49312
   145
  from surj_on obtain x where "x \<in> X" and "y = f x" by blast
blanchet@49312
   146
  thus "g1 y = g2 y" using eq_on by simp
blanchet@49312
   147
qed
blanchet@49312
   148
blanchet@49312
   149
lemma Card_order_wo_rel: "Card_order r \<Longrightarrow> wo_rel r"
blanchet@49514
   150
unfolding wo_rel_def card_order_on_def by blast
blanchet@49312
   151
blanchet@49312
   152
lemma Cinfinite_limit: "\<lbrakk>x \<in> Field r; Cinfinite r\<rbrakk> \<Longrightarrow>
blanchet@49312
   153
  \<exists>y \<in> Field r. x \<noteq> y \<and> (x, y) \<in> r"
blanchet@49312
   154
unfolding cinfinite_def by (auto simp add: infinite_Card_order_limit)
blanchet@49312
   155
blanchet@49312
   156
lemma Card_order_trans:
blanchet@49312
   157
  "\<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
   158
unfolding card_order_on_def well_order_on_def linear_order_on_def
blanchet@49312
   159
  partial_order_on_def preorder_on_def trans_def antisym_def by blast
blanchet@49312
   160
blanchet@49312
   161
lemma Cinfinite_limit2:
blanchet@49312
   162
 assumes x1: "x1 \<in> Field r" and x2: "x2 \<in> Field r" and r: "Cinfinite r"
blanchet@49312
   163
 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
   164
proof -
blanchet@49312
   165
  from r have trans: "trans r" and total: "Total r" and antisym: "antisym r"
blanchet@49312
   166
    unfolding card_order_on_def well_order_on_def linear_order_on_def
blanchet@49312
   167
      partial_order_on_def preorder_on_def by auto
blanchet@49312
   168
  obtain y1 where y1: "y1 \<in> Field r" "x1 \<noteq> y1" "(x1, y1) \<in> r"
blanchet@49312
   169
    using Cinfinite_limit[OF x1 r] by blast
blanchet@49312
   170
  obtain y2 where y2: "y2 \<in> Field r" "x2 \<noteq> y2" "(x2, y2) \<in> r"
blanchet@49312
   171
    using Cinfinite_limit[OF x2 r] by blast
blanchet@49312
   172
  show ?thesis
blanchet@49312
   173
  proof (cases "y1 = y2")
blanchet@49312
   174
    case True with y1 y2 show ?thesis by blast
blanchet@49312
   175
  next
blanchet@49312
   176
    case False
blanchet@49312
   177
    with y1(1) y2(1) total have "(y1, y2) \<in> r \<or> (y2, y1) \<in> r"
blanchet@49312
   178
      unfolding total_on_def by auto
blanchet@49312
   179
    thus ?thesis
blanchet@49312
   180
    proof
blanchet@49312
   181
      assume *: "(y1, y2) \<in> r"
blanchet@49312
   182
      with trans y1(3) have "(x1, y2) \<in> r" unfolding trans_def by blast
blanchet@49312
   183
      with False y1 y2 * antisym show ?thesis by (cases "x1 = y2") (auto simp: antisym_def)
blanchet@49312
   184
    next
blanchet@49312
   185
      assume *: "(y2, y1) \<in> r"
blanchet@49312
   186
      with trans y2(3) have "(x2, y1) \<in> r" unfolding trans_def by blast
blanchet@49312
   187
      with False y1 y2 * antisym show ?thesis by (cases "x2 = y1") (auto simp: antisym_def)
blanchet@49312
   188
    qed
blanchet@49312
   189
  qed
blanchet@49312
   190
qed
blanchet@49312
   191
blanchet@49312
   192
lemma Cinfinite_limit_finite: "\<lbrakk>finite X; X \<subseteq> Field r; Cinfinite r\<rbrakk>
blanchet@49312
   193
 \<Longrightarrow> \<exists>y \<in> Field r. \<forall>x \<in> X. (x \<noteq> y \<and> (x, y) \<in> r)"
blanchet@49312
   194
proof (induct X rule: finite_induct)
blanchet@49312
   195
  case empty thus ?case unfolding cinfinite_def using ex_in_conv[of "Field r"] finite.emptyI by auto
blanchet@49312
   196
next
blanchet@49312
   197
  case (insert x X)
blanchet@49312
   198
  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
   199
  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
   200
    using Cinfinite_limit2[OF _ y(1) insert(5), of x] insert(4) by blast
blanchet@49326
   201
  show ?case
blanchet@49326
   202
    apply (intro bexI ballI)
blanchet@49326
   203
    apply (erule insertE)
blanchet@49326
   204
    apply hypsubst
blanchet@49326
   205
    apply (rule z(2))
blanchet@49326
   206
    using Card_order_trans[OF insert(5)[THEN conjunct2]] y(2) z(3)
blanchet@49326
   207
    apply blast
blanchet@49326
   208
    apply (rule z(1))
blanchet@49326
   209
    done
blanchet@49312
   210
qed
blanchet@49312
   211
blanchet@49312
   212
lemma insert_subsetI: "\<lbrakk>x \<in> A; X \<subseteq> A\<rbrakk> \<Longrightarrow> insert x X \<subseteq> A"
blanchet@49312
   213
by auto
blanchet@49312
   214
blanchet@49312
   215
(*helps resolution*)
blanchet@49312
   216
lemma well_order_induct_imp:
blanchet@49312
   217
  "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
   218
     x \<in> Field r \<longrightarrow> P x"
blanchet@49312
   219
by (erule wo_rel.well_order_induct)
blanchet@49312
   220
blanchet@49312
   221
lemma meta_spec2:
blanchet@49312
   222
  assumes "(\<And>x y. PROP P x y)"
blanchet@49312
   223
  shows "PROP P x y"
blanchet@55084
   224
by (rule assms)
blanchet@49312
   225
traytel@54841
   226
lemma nchotomy_relcomppE:
blanchet@55066
   227
  "\<lbrakk>\<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\<rbrakk> \<Longrightarrow> P"
traytel@54841
   228
  by (metis relcompp.cases)
traytel@54841
   229
blanchet@55066
   230
lemma vimage2p_fun_rel: "fun_rel (vimage2p f g R) R f g"
traytel@52731
   231
  unfolding fun_rel_def vimage2p_def by auto
traytel@52731
   232
traytel@52731
   233
lemma predicate2D_vimage2p: "\<lbrakk>R \<le> vimage2p f g S; R x y\<rbrakk> \<Longrightarrow> S (f x) (g y)"
traytel@52731
   234
  unfolding vimage2p_def by auto
traytel@52731
   235
blanchet@55084
   236
lemma id_transfer: "fun_rel A A id id"
blanchet@55084
   237
unfolding fun_rel_def by simp
blanchet@55084
   238
traytel@55770
   239
lemma ssubst_Pair_rhs: "\<lbrakk>(r, s) \<in> R; s' = s\<rbrakk> \<Longrightarrow> (r, s') \<in> R"
traytel@55770
   240
  by simp
traytel@55770
   241
blanchet@55062
   242
ML_file "Tools/BNF/bnf_lfp_util.ML"
blanchet@55062
   243
ML_file "Tools/BNF/bnf_lfp_tactics.ML"
blanchet@55062
   244
ML_file "Tools/BNF/bnf_lfp.ML"
blanchet@55062
   245
ML_file "Tools/BNF/bnf_lfp_compat.ML"
blanchet@55571
   246
ML_file "Tools/BNF/bnf_lfp_rec_sugar_more.ML"
blanchet@49309
   247
blanchet@55084
   248
hide_fact (open) id_transfer
blanchet@55084
   249
blanchet@48975
   250
end