src/HOL/BNF_LFP.thy
author blanchet
Wed Apr 23 10:23:26 2014 +0200 (2014-04-23)
changeset 56638 092a306bcc3d
parent 56346 42533f8f4729
child 56639 c9d6b581bd3b
permissions -rw-r--r--
generate size instances for new-style datatypes
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@56346
    22
lemma image_Collect_subsetI: "(\<And>x. P x \<Longrightarrow> f x \<in> B) \<Longrightarrow> f ` {x. P x} \<subseteq> B"
blanchet@49312
    23
by blast
blanchet@49312
    24
blanchet@49312
    25
lemma Collect_restrict: "{x. x \<in> X \<and> P x} \<subseteq> X"
blanchet@49312
    26
by auto
blanchet@49312
    27
blanchet@49312
    28
lemma prop_restrict: "\<lbrakk>x \<in> Z; Z \<subseteq> {x. x \<in> X \<and> P x}\<rbrakk> \<Longrightarrow> P x"
blanchet@49312
    29
by auto
blanchet@49312
    30
blanchet@55023
    31
lemma underS_I: "\<lbrakk>i \<noteq> j; (i, j) \<in> R\<rbrakk> \<Longrightarrow> i \<in> underS R j"
blanchet@55023
    32
unfolding underS_def by simp
blanchet@49312
    33
blanchet@55023
    34
lemma underS_E: "i \<in> underS R j \<Longrightarrow> i \<noteq> j \<and> (i, j) \<in> R"
blanchet@55023
    35
unfolding underS_def by simp
blanchet@49312
    36
blanchet@55023
    37
lemma underS_Field: "i \<in> underS R j \<Longrightarrow> i \<in> Field R"
blanchet@55023
    38
unfolding underS_def Field_def by auto
blanchet@49312
    39
blanchet@49312
    40
lemma FieldI2: "(i, j) \<in> R \<Longrightarrow> j \<in> Field R"
blanchet@49312
    41
unfolding Field_def by auto
blanchet@49312
    42
blanchet@49312
    43
lemma fst_convol': "fst (<f, g> x) = f x"
blanchet@49312
    44
using fst_convol unfolding convol_def by simp
blanchet@49312
    45
blanchet@49312
    46
lemma snd_convol': "snd (<f, g> x) = g x"
blanchet@49312
    47
using snd_convol unfolding convol_def by simp
blanchet@49312
    48
blanchet@49312
    49
lemma convol_expand_snd: "fst o f = g \<Longrightarrow>  <g, snd o f> = f"
blanchet@49312
    50
unfolding convol_def by auto
blanchet@49312
    51
traytel@55811
    52
lemma convol_expand_snd':
traytel@55811
    53
  assumes "(fst o f = g)"
traytel@55811
    54
  shows "h = snd o f \<longleftrightarrow> <g, h> = f"
traytel@55811
    55
proof -
traytel@55811
    56
  from assms have *: "<g, snd o f> = f" by (rule convol_expand_snd)
traytel@55811
    57
  then have "h = snd o f \<longleftrightarrow> h = snd o <g, snd o f>" by simp
traytel@55811
    58
  moreover have "\<dots> \<longleftrightarrow> h = snd o f" by (simp add: snd_convol)
traytel@55811
    59
  moreover have "\<dots> \<longleftrightarrow> <g, h> = f" by (subst (2) *[symmetric]) (auto simp: convol_def fun_eq_iff)
traytel@55811
    60
  ultimately show ?thesis by simp
traytel@55811
    61
qed
blanchet@49312
    62
lemma bij_betwE: "bij_betw f A B \<Longrightarrow> \<forall>a\<in>A. f a \<in> B"
blanchet@49312
    63
unfolding bij_betw_def by auto
blanchet@49312
    64
blanchet@49312
    65
lemma bij_betw_imageE: "bij_betw f A B \<Longrightarrow> f ` A = B"
blanchet@49312
    66
unfolding bij_betw_def by auto
blanchet@49312
    67
traytel@56237
    68
lemma f_the_inv_into_f_bij_betw: "bij_betw f A B \<Longrightarrow>
traytel@56237
    69
  (bij_betw f A B \<Longrightarrow> x \<in> B) \<Longrightarrow> f (the_inv_into A f x) = x"
traytel@56237
    70
  unfolding bij_betw_def by (blast intro: f_the_inv_into_f)
blanchet@49312
    71
traytel@56237
    72
lemma ex_bij_betw: "|A| \<le>o (r :: 'b rel) \<Longrightarrow> \<exists>f B :: 'b set. bij_betw f B A"
traytel@56237
    73
  by (subst (asm) internalize_card_of_ordLeq)
traytel@56237
    74
    (auto dest!: iffD2[OF card_of_ordIso ordIso_symmetric])
blanchet@49312
    75
blanchet@49312
    76
lemma bij_betwI':
blanchet@49312
    77
  "\<lbrakk>\<And>x y. \<lbrakk>x \<in> X; y \<in> X\<rbrakk> \<Longrightarrow> (f x = f y) = (x = y);
blanchet@49312
    78
    \<And>x. x \<in> X \<Longrightarrow> f x \<in> Y;
blanchet@49312
    79
    \<And>y. y \<in> Y \<Longrightarrow> \<exists>x \<in> X. y = f x\<rbrakk> \<Longrightarrow> bij_betw f X Y"
traytel@53695
    80
unfolding bij_betw_def inj_on_def by blast
blanchet@49312
    81
blanchet@49312
    82
lemma surj_fun_eq:
blanchet@49312
    83
  assumes surj_on: "f ` X = UNIV" and eq_on: "\<forall>x \<in> X. (g1 o f) x = (g2 o f) x"
blanchet@49312
    84
  shows "g1 = g2"
blanchet@49312
    85
proof (rule ext)
blanchet@49312
    86
  fix y
blanchet@49312
    87
  from surj_on obtain x where "x \<in> X" and "y = f x" by blast
blanchet@49312
    88
  thus "g1 y = g2 y" using eq_on by simp
blanchet@49312
    89
qed
blanchet@49312
    90
blanchet@49312
    91
lemma Card_order_wo_rel: "Card_order r \<Longrightarrow> wo_rel r"
blanchet@49514
    92
unfolding wo_rel_def card_order_on_def by blast
blanchet@49312
    93
blanchet@49312
    94
lemma Cinfinite_limit: "\<lbrakk>x \<in> Field r; Cinfinite r\<rbrakk> \<Longrightarrow>
blanchet@49312
    95
  \<exists>y \<in> Field r. x \<noteq> y \<and> (x, y) \<in> r"
blanchet@49312
    96
unfolding cinfinite_def by (auto simp add: infinite_Card_order_limit)
blanchet@49312
    97
blanchet@49312
    98
lemma Card_order_trans:
blanchet@49312
    99
  "\<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
   100
unfolding card_order_on_def well_order_on_def linear_order_on_def
blanchet@49312
   101
  partial_order_on_def preorder_on_def trans_def antisym_def by blast
blanchet@49312
   102
blanchet@49312
   103
lemma Cinfinite_limit2:
blanchet@49312
   104
 assumes x1: "x1 \<in> Field r" and x2: "x2 \<in> Field r" and r: "Cinfinite r"
blanchet@49312
   105
 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
   106
proof -
blanchet@49312
   107
  from r have trans: "trans r" and total: "Total r" and antisym: "antisym r"
blanchet@49312
   108
    unfolding card_order_on_def well_order_on_def linear_order_on_def
blanchet@49312
   109
      partial_order_on_def preorder_on_def by auto
blanchet@49312
   110
  obtain y1 where y1: "y1 \<in> Field r" "x1 \<noteq> y1" "(x1, y1) \<in> r"
blanchet@49312
   111
    using Cinfinite_limit[OF x1 r] by blast
blanchet@49312
   112
  obtain y2 where y2: "y2 \<in> Field r" "x2 \<noteq> y2" "(x2, y2) \<in> r"
blanchet@49312
   113
    using Cinfinite_limit[OF x2 r] by blast
blanchet@49312
   114
  show ?thesis
blanchet@49312
   115
  proof (cases "y1 = y2")
blanchet@49312
   116
    case True with y1 y2 show ?thesis by blast
blanchet@49312
   117
  next
blanchet@49312
   118
    case False
blanchet@49312
   119
    with y1(1) y2(1) total have "(y1, y2) \<in> r \<or> (y2, y1) \<in> r"
blanchet@49312
   120
      unfolding total_on_def by auto
blanchet@49312
   121
    thus ?thesis
blanchet@49312
   122
    proof
blanchet@49312
   123
      assume *: "(y1, y2) \<in> r"
blanchet@49312
   124
      with trans y1(3) have "(x1, y2) \<in> r" unfolding trans_def by blast
blanchet@49312
   125
      with False y1 y2 * antisym show ?thesis by (cases "x1 = y2") (auto simp: antisym_def)
blanchet@49312
   126
    next
blanchet@49312
   127
      assume *: "(y2, y1) \<in> r"
blanchet@49312
   128
      with trans y2(3) have "(x2, y1) \<in> r" unfolding trans_def by blast
blanchet@49312
   129
      with False y1 y2 * antisym show ?thesis by (cases "x2 = y1") (auto simp: antisym_def)
blanchet@49312
   130
    qed
blanchet@49312
   131
  qed
blanchet@49312
   132
qed
blanchet@49312
   133
blanchet@49312
   134
lemma Cinfinite_limit_finite: "\<lbrakk>finite X; X \<subseteq> Field r; Cinfinite r\<rbrakk>
blanchet@49312
   135
 \<Longrightarrow> \<exists>y \<in> Field r. \<forall>x \<in> X. (x \<noteq> y \<and> (x, y) \<in> r)"
blanchet@49312
   136
proof (induct X rule: finite_induct)
blanchet@49312
   137
  case empty thus ?case unfolding cinfinite_def using ex_in_conv[of "Field r"] finite.emptyI by auto
blanchet@49312
   138
next
blanchet@49312
   139
  case (insert x X)
blanchet@49312
   140
  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
   141
  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
   142
    using Cinfinite_limit2[OF _ y(1) insert(5), of x] insert(4) by blast
blanchet@49326
   143
  show ?case
blanchet@49326
   144
    apply (intro bexI ballI)
blanchet@49326
   145
    apply (erule insertE)
blanchet@49326
   146
    apply hypsubst
blanchet@49326
   147
    apply (rule z(2))
blanchet@49326
   148
    using Card_order_trans[OF insert(5)[THEN conjunct2]] y(2) z(3)
blanchet@49326
   149
    apply blast
blanchet@49326
   150
    apply (rule z(1))
blanchet@49326
   151
    done
blanchet@49312
   152
qed
blanchet@49312
   153
blanchet@49312
   154
lemma insert_subsetI: "\<lbrakk>x \<in> A; X \<subseteq> A\<rbrakk> \<Longrightarrow> insert x X \<subseteq> A"
blanchet@49312
   155
by auto
blanchet@49312
   156
blanchet@49312
   157
(*helps resolution*)
blanchet@49312
   158
lemma well_order_induct_imp:
blanchet@49312
   159
  "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
   160
     x \<in> Field r \<longrightarrow> P x"
blanchet@49312
   161
by (erule wo_rel.well_order_induct)
blanchet@49312
   162
blanchet@49312
   163
lemma meta_spec2:
blanchet@49312
   164
  assumes "(\<And>x y. PROP P x y)"
blanchet@49312
   165
  shows "PROP P x y"
blanchet@55084
   166
by (rule assms)
blanchet@49312
   167
traytel@54841
   168
lemma nchotomy_relcomppE:
traytel@55811
   169
  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
   170
  shows P
traytel@55811
   171
proof (rule relcompp.cases[OF assms(2)], hypsubst)
traytel@55811
   172
  fix b assume "r a b" "s b c"
traytel@55811
   173
  moreover from assms(1) obtain b' where "b = f b'" by blast
traytel@55811
   174
  ultimately show P by (blast intro: assms(3))
traytel@55811
   175
qed
traytel@54841
   176
blanchet@55945
   177
lemma vimage2p_rel_fun: "rel_fun (vimage2p f g R) R f g"
blanchet@55945
   178
  unfolding rel_fun_def vimage2p_def by auto
traytel@52731
   179
traytel@52731
   180
lemma predicate2D_vimage2p: "\<lbrakk>R \<le> vimage2p f g S; R x y\<rbrakk> \<Longrightarrow> S (f x) (g y)"
traytel@52731
   181
  unfolding vimage2p_def by auto
traytel@52731
   182
blanchet@55945
   183
lemma id_transfer: "rel_fun A A id id"
blanchet@55945
   184
  unfolding rel_fun_def by simp
blanchet@55084
   185
traytel@55770
   186
lemma ssubst_Pair_rhs: "\<lbrakk>(r, s) \<in> R; s' = s\<rbrakk> \<Longrightarrow> (r, s') \<in> R"
blanchet@55851
   187
  by (rule ssubst)
traytel@55770
   188
blanchet@56638
   189
lemma snd_o_convol: "(snd \<circ> (\<lambda>x. (f x, g x))) = g"
blanchet@56638
   190
  by (rule ext) simp
blanchet@56638
   191
blanchet@56638
   192
lemma inj_on_convol_id: "inj_on (\<lambda>x. (x, f x)) X"
blanchet@56638
   193
  unfolding inj_on_def by simp
blanchet@56638
   194
blanchet@55062
   195
ML_file "Tools/BNF/bnf_lfp_util.ML"
blanchet@55062
   196
ML_file "Tools/BNF/bnf_lfp_tactics.ML"
blanchet@55062
   197
ML_file "Tools/BNF/bnf_lfp.ML"
blanchet@55062
   198
ML_file "Tools/BNF/bnf_lfp_compat.ML"
blanchet@55571
   199
ML_file "Tools/BNF/bnf_lfp_rec_sugar_more.ML"
blanchet@56638
   200
ML_file "Tools/BNF/bnf_lfp_size.ML"
blanchet@49309
   201
blanchet@55084
   202
hide_fact (open) id_transfer
blanchet@55084
   203
blanchet@56638
   204
datatype_new x = X nat
blanchet@56638
   205
thm x.size
blanchet@56638
   206
blanchet@56638
   207
datatype_new 'a l = N | C 'a "'a l"
blanchet@56638
   208
thm l.size
blanchet@56638
   209
thm l.size_map
blanchet@56638
   210
thm size_l_def size_l_overloaded_def
blanchet@56638
   211
blanchet@56638
   212
datatype_new
blanchet@56638
   213
  'a tl = TN | TC "'a mt" "'a tl" and
blanchet@56638
   214
  'a mt = MT 'a "'a tl"
blanchet@56638
   215
blanchet@56638
   216
thm size_tl_def size_tl_overloaded_def
blanchet@56638
   217
thm size_mt_def size_mt_overloaded_def
blanchet@56638
   218
blanchet@56638
   219
datatype_new 'a t = T 'a "'a t l"
blanchet@56638
   220
thm t.size
blanchet@56638
   221
blanchet@56638
   222
lemma size_l_cong: "(ALL x : set_l t. f x = g x) --> size_l f t = size_l g t"
blanchet@56638
   223
  apply (induct_tac t)
blanchet@56638
   224
  apply (simp only: l.size simp_thms)
blanchet@56638
   225
  apply (simp add: l.set l.size simp_thms)
blanchet@56638
   226
  done
blanchet@56638
   227
blanchet@56638
   228
lemma t_size_map_t: "size_t g (map_t f t) = size_t (g \<circ> f) t"
blanchet@56638
   229
  apply (rule t.induct)
blanchet@56638
   230
  apply (simp_all only: t.map t.size comp_def l.size_map)
blanchet@56638
   231
  apply (auto intro: size_l_cong)
blanchet@56638
   232
  apply (subst size_l_cong[rule_format], assumption)
blanchet@56638
   233
  apply (rule refl)
blanchet@56638
   234
  done
blanchet@56638
   235
blanchet@56638
   236
blanchet@56638
   237
thm t.size
blanchet@56638
   238
blanchet@56638
   239
lemmas size_t_def' =
blanchet@56638
   240
  size_t_def[THEN meta_eq_to_obj_eq, THEN fun_cong, THEN fun_cong]
blanchet@56638
   241
blanchet@56638
   242
thm trans[OF size_t_def' t.rec(1), unfolded l.size_map snd_o_convol, folded size_t_def']
blanchet@56638
   243
blanchet@56638
   244
lemma "size_t f (T x ts) = f x + size_l (size_t f) ts + Suc 0"
blanchet@56638
   245
  unfolding size_t_def t.rec l.size_map snd_o_convol
blanchet@56638
   246
  by rule
blanchet@56638
   247
blanchet@56638
   248
blanchet@56638
   249
lemma "       (\<And>x2aa. x2aa \<in> set_l x2a \<Longrightarrow>
blanchet@56638
   250
                size_t f1 (map_t g1 x2aa) = size_t (f1 \<circ> g1) x2aa) \<Longrightarrow>
blanchet@56638
   251
       f1 (g1 x1a) +
blanchet@56638
   252
       size_l snd (map_l (\<lambda>t. (t, size_t f1 t)) (map_l (map_t g1) x2a)) +
blanchet@56638
   253
       Suc 0 =
blanchet@56638
   254
       f1 (g1 x1a) + size_l snd (map_l (\<lambda>t. (t, size_t (\<lambda>x1. f1 (g1 x1)) t)) x2a) +
blanchet@56638
   255
       Suc 0"
blanchet@56638
   256
apply (simp only: l.size_map comp_def snd_conv t.size_map snd_o_convol)
blanchet@56638
   257
blanchet@56638
   258
thm size_t_def size_t_overloaded_def
blanchet@56638
   259
blanchet@56638
   260
xdatatype_new ('a, 'b, 'c) x = XN 'c | XC 'a "('a, 'b, 'c) x"
blanchet@56638
   261
thm size_x_def size_x_overloaded_def
blanchet@56638
   262
blanchet@48975
   263
end