src/HOL/BNF_Least_Fixpoint.thy
author blanchet
Thu Sep 11 18:54:36 2014 +0200 (2014-09-11)
changeset 58306 117ba6cbe414
parent 58305 57752a91eec4
child 58310 91ea607a34d8
permissions -rw-r--r--
renamed 'rep_datatype' to 'old_rep_datatype' (HOL)
blanchet@58128
     1
(*  Title:      HOL/BNF_Least_Fixpoint.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@57698
     5
    Copyright   2012, 2013, 2014
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@58128
    12
theory BNF_Least_Fixpoint
blanchet@58128
    13
imports BNF_Fixpoint_Base
blanchet@48975
    14
keywords
blanchet@58305
    15
  "datatype" :: thy_decl and
blanchet@53305
    16
  "datatype_new" :: thy_decl and
blanchet@55575
    17
  "datatype_compat" :: thy_decl
blanchet@48975
    18
begin
blanchet@48975
    19
blanchet@49312
    20
lemma subset_emptyI: "(\<And>x. x \<in> A \<Longrightarrow> False) \<Longrightarrow> A \<subseteq> {}"
blanchet@57987
    21
  by blast
blanchet@49312
    22
blanchet@56346
    23
lemma image_Collect_subsetI: "(\<And>x. P x \<Longrightarrow> f x \<in> B) \<Longrightarrow> f ` {x. P x} \<subseteq> B"
blanchet@57987
    24
  by blast
blanchet@49312
    25
blanchet@49312
    26
lemma Collect_restrict: "{x. x \<in> X \<and> P x} \<subseteq> X"
blanchet@57987
    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@57987
    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@57987
    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@57987
    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@57987
    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@57987
    42
  unfolding Field_def by auto
blanchet@49312
    43
wenzelm@57641
    44
lemma fst_convol': "fst (\<langle>f, g\<rangle> x) = f x"
blanchet@57987
    45
  using fst_convol unfolding convol_def by simp
blanchet@49312
    46
wenzelm@57641
    47
lemma snd_convol': "snd (\<langle>f, g\<rangle> x) = g x"
blanchet@57987
    48
  using snd_convol unfolding convol_def by simp
blanchet@49312
    49
wenzelm@57641
    50
lemma convol_expand_snd: "fst o f = g \<Longrightarrow> \<langle>g, snd o f\<rangle> = f"
blanchet@57987
    51
  unfolding convol_def by auto
blanchet@49312
    52
traytel@55811
    53
lemma convol_expand_snd':
traytel@55811
    54
  assumes "(fst o f = g)"
wenzelm@57641
    55
  shows "h = snd o f \<longleftrightarrow> \<langle>g, h\<rangle> = f"
traytel@55811
    56
proof -
wenzelm@57641
    57
  from assms have *: "\<langle>g, snd o f\<rangle> = f" by (rule convol_expand_snd)
wenzelm@57641
    58
  then have "h = snd o f \<longleftrightarrow> h = snd o \<langle>g, snd o f\<rangle>" by simp
traytel@55811
    59
  moreover have "\<dots> \<longleftrightarrow> h = snd o f" by (simp add: snd_convol)
wenzelm@57641
    60
  moreover have "\<dots> \<longleftrightarrow> \<langle>g, h\<rangle> = f" by (subst (2) *[symmetric]) (auto simp: convol_def fun_eq_iff)
traytel@55811
    61
  ultimately show ?thesis by simp
traytel@55811
    62
qed
blanchet@57987
    63
blanchet@49312
    64
lemma bij_betwE: "bij_betw f A B \<Longrightarrow> \<forall>a\<in>A. f a \<in> B"
blanchet@57987
    65
  unfolding bij_betw_def by auto
blanchet@49312
    66
blanchet@49312
    67
lemma bij_betw_imageE: "bij_betw f A B \<Longrightarrow> f ` A = B"
blanchet@57987
    68
  unfolding bij_betw_def by auto
blanchet@49312
    69
blanchet@58159
    70
lemma f_the_inv_into_f_bij_betw:
blanchet@58159
    71
  "bij_betw f A B \<Longrightarrow> (bij_betw f A B \<Longrightarrow> x \<in> B) \<Longrightarrow> f (the_inv_into A f x) = x"
traytel@56237
    72
  unfolding bij_betw_def by (blast intro: f_the_inv_into_f)
blanchet@49312
    73
traytel@56237
    74
lemma ex_bij_betw: "|A| \<le>o (r :: 'b rel) \<Longrightarrow> \<exists>f B :: 'b set. bij_betw f B A"
blanchet@58159
    75
  by (subst (asm) internalize_card_of_ordLeq) (auto dest!: iffD2[OF card_of_ordIso ordIso_symmetric])
blanchet@49312
    76
blanchet@49312
    77
lemma bij_betwI':
blanchet@49312
    78
  "\<lbrakk>\<And>x y. \<lbrakk>x \<in> X; y \<in> X\<rbrakk> \<Longrightarrow> (f x = f y) = (x = y);
blanchet@49312
    79
    \<And>x. x \<in> X \<Longrightarrow> f x \<in> Y;
blanchet@49312
    80
    \<And>y. y \<in> Y \<Longrightarrow> \<exists>x \<in> X. y = f x\<rbrakk> \<Longrightarrow> bij_betw f X Y"
blanchet@57987
    81
  unfolding bij_betw_def inj_on_def by blast
blanchet@49312
    82
blanchet@49312
    83
lemma surj_fun_eq:
blanchet@49312
    84
  assumes surj_on: "f ` X = UNIV" and eq_on: "\<forall>x \<in> X. (g1 o f) x = (g2 o f) x"
blanchet@49312
    85
  shows "g1 = g2"
blanchet@49312
    86
proof (rule ext)
blanchet@49312
    87
  fix y
blanchet@49312
    88
  from surj_on obtain x where "x \<in> X" and "y = f x" by blast
blanchet@49312
    89
  thus "g1 y = g2 y" using eq_on by simp
blanchet@49312
    90
qed
blanchet@49312
    91
blanchet@49312
    92
lemma Card_order_wo_rel: "Card_order r \<Longrightarrow> wo_rel r"
blanchet@58147
    93
  unfolding wo_rel_def card_order_on_def by blast
blanchet@49312
    94
blanchet@58147
    95
lemma Cinfinite_limit: "\<lbrakk>x \<in> Field r; Cinfinite r\<rbrakk> \<Longrightarrow> \<exists>y \<in> Field r. x \<noteq> y \<and> (x, y) \<in> r"
blanchet@58147
    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@58147
   100
  unfolding card_order_on_def well_order_on_def linear_order_on_def
blanchet@58147
   101
    partial_order_on_def preorder_on_def trans_def antisym_def by blast
blanchet@49312
   102
blanchet@49312
   103
lemma Cinfinite_limit2:
blanchet@58147
   104
  assumes x1: "x1 \<in> Field r" and x2: "x2 \<in> Field r" and r: "Cinfinite r"
blanchet@58147
   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@58147
   134
lemma Cinfinite_limit_finite:
blanchet@58147
   135
  "\<lbrakk>finite X; X \<subseteq> Field r; Cinfinite r\<rbrakk> \<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@58147
   155
  by auto
blanchet@49312
   156
blanchet@58136
   157
lemmas well_order_induct_imp = wo_rel.well_order_induct[of r "\<lambda>x. x \<in> Field r \<longrightarrow> P x" for r P]
blanchet@49312
   158
blanchet@49312
   159
lemma meta_spec2:
blanchet@49312
   160
  assumes "(\<And>x y. PROP P x y)"
blanchet@49312
   161
  shows "PROP P x y"
blanchet@58136
   162
  by (rule assms)
blanchet@49312
   163
traytel@54841
   164
lemma nchotomy_relcomppE:
traytel@55811
   165
  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
   166
  shows P
traytel@55811
   167
proof (rule relcompp.cases[OF assms(2)], hypsubst)
traytel@55811
   168
  fix b assume "r a b" "s b c"
traytel@55811
   169
  moreover from assms(1) obtain b' where "b = f b'" by blast
traytel@55811
   170
  ultimately show P by (blast intro: assms(3))
traytel@55811
   171
qed
traytel@54841
   172
blanchet@55945
   173
lemma vimage2p_rel_fun: "rel_fun (vimage2p f g R) R f g"
blanchet@55945
   174
  unfolding rel_fun_def vimage2p_def by auto
traytel@52731
   175
traytel@52731
   176
lemma predicate2D_vimage2p: "\<lbrakk>R \<le> vimage2p f g S; R x y\<rbrakk> \<Longrightarrow> S (f x) (g y)"
traytel@52731
   177
  unfolding vimage2p_def by auto
traytel@52731
   178
blanchet@55945
   179
lemma id_transfer: "rel_fun A A id id"
blanchet@55945
   180
  unfolding rel_fun_def by simp
blanchet@55084
   181
traytel@55770
   182
lemma ssubst_Pair_rhs: "\<lbrakk>(r, s) \<in> R; s' = s\<rbrakk> \<Longrightarrow> (r, s') \<in> R"
blanchet@55851
   183
  by (rule ssubst)
traytel@55770
   184
blanchet@58211
   185
lemma all_mem_range1:
blanchet@58211
   186
  "(\<And>y. y \<in> range f \<Longrightarrow> P y) \<equiv> (\<And>x. P (f x)) "
blanchet@58211
   187
  by (rule equal_intr_rule) fast+
blanchet@58211
   188
blanchet@58211
   189
lemma all_mem_range2:
blanchet@58211
   190
  "(\<And>fa y. fa \<in> range f \<Longrightarrow> y \<in> range fa \<Longrightarrow> P y) \<equiv> (\<And>x xa. P (f x xa))"
blanchet@58211
   191
  by (rule equal_intr_rule) fast+
blanchet@58211
   192
blanchet@58211
   193
lemma all_mem_range3:
blanchet@58211
   194
  "(\<And>fa fb y. fa \<in> range f \<Longrightarrow> fb \<in> range fa \<Longrightarrow> y \<in> range fb \<Longrightarrow> P y) \<equiv> (\<And>x xa xb. P (f x xa xb))"
blanchet@58211
   195
  by (rule equal_intr_rule) fast+
blanchet@58211
   196
blanchet@58211
   197
lemma all_mem_range4:
blanchet@58211
   198
  "(\<And>fa fb fc y. fa \<in> range f \<Longrightarrow> fb \<in> range fa \<Longrightarrow> fc \<in> range fb \<Longrightarrow> y \<in> range fc \<Longrightarrow> P y) \<equiv>
blanchet@58211
   199
   (\<And>x xa xb xc. P (f x xa xb xc))"
blanchet@58211
   200
  by (rule equal_intr_rule) fast+
blanchet@58211
   201
blanchet@58211
   202
lemma all_mem_range5:
blanchet@58211
   203
  "(\<And>fa fb fc fd y. fa \<in> range f \<Longrightarrow> fb \<in> range fa \<Longrightarrow> fc \<in> range fb \<Longrightarrow> fd \<in> range fc \<Longrightarrow>
blanchet@58211
   204
     y \<in> range fd \<Longrightarrow> P y) \<equiv>
blanchet@58211
   205
   (\<And>x xa xb xc xd. P (f x xa xb xc xd))"
blanchet@58211
   206
  by (rule equal_intr_rule) fast+
blanchet@58211
   207
blanchet@58211
   208
lemma all_mem_range6:
blanchet@58211
   209
  "(\<And>fa fb fc fd fe ff y. fa \<in> range f \<Longrightarrow> fb \<in> range fa \<Longrightarrow> fc \<in> range fb \<Longrightarrow> fd \<in> range fc \<Longrightarrow>
blanchet@58211
   210
     fe \<in> range fd \<Longrightarrow> ff \<in> range fe \<Longrightarrow> y \<in> range ff \<Longrightarrow> P y) \<equiv>
blanchet@58211
   211
   (\<And>x xa xb xc xd xe xf. P (f x xa xb xc xd xe xf))"
blanchet@58211
   212
  by (rule equal_intr_rule) (fastforce, fast)
blanchet@58211
   213
blanchet@58211
   214
lemma all_mem_range7:
blanchet@58211
   215
  "(\<And>fa fb fc fd fe ff fg y. fa \<in> range f \<Longrightarrow> fb \<in> range fa \<Longrightarrow> fc \<in> range fb \<Longrightarrow> fd \<in> range fc \<Longrightarrow>
blanchet@58211
   216
     fe \<in> range fd \<Longrightarrow> ff \<in> range fe \<Longrightarrow> fg \<in> range ff \<Longrightarrow> y \<in> range fg \<Longrightarrow> P y) \<equiv>
blanchet@58211
   217
   (\<And>x xa xb xc xd xe xf xg. P (f x xa xb xc xd xe xf xg))"
blanchet@58211
   218
  by (rule equal_intr_rule) (fastforce, fast)
blanchet@58211
   219
blanchet@58211
   220
lemma all_mem_range8:
blanchet@58211
   221
  "(\<And>fa fb fc fd fe ff fg fh y. fa \<in> range f \<Longrightarrow> fb \<in> range fa \<Longrightarrow> fc \<in> range fb \<Longrightarrow> fd \<in> range fc \<Longrightarrow>
blanchet@58211
   222
     fe \<in> range fd \<Longrightarrow> ff \<in> range fe \<Longrightarrow> fg \<in> range ff \<Longrightarrow> fh \<in> range fg \<Longrightarrow> y \<in> range fh \<Longrightarrow> P y) \<equiv>
blanchet@58211
   223
   (\<And>x xa xb xc xd xe xf xg xh. P (f x xa xb xc xd xe xf xg xh))"
blanchet@58211
   224
  by (rule equal_intr_rule) (fastforce, fast)
blanchet@58211
   225
blanchet@58211
   226
lemmas all_mem_range = all_mem_range1 all_mem_range2 all_mem_range3 all_mem_range4 all_mem_range5
blanchet@58211
   227
  all_mem_range6 all_mem_range7 all_mem_range8
blanchet@58211
   228
blanchet@55062
   229
ML_file "Tools/BNF/bnf_lfp_util.ML"
blanchet@55062
   230
ML_file "Tools/BNF/bnf_lfp_tactics.ML"
blanchet@55062
   231
ML_file "Tools/BNF/bnf_lfp.ML"
blanchet@55062
   232
ML_file "Tools/BNF/bnf_lfp_compat.ML"
blanchet@55571
   233
ML_file "Tools/BNF/bnf_lfp_rec_sugar_more.ML"
blanchet@58179
   234
ML_file "Tools/BNF/bnf_lfp_size.ML"
blanchet@58179
   235
ML_file "Tools/Function/old_size.ML"
blanchet@58275
   236
ML_file "Tools/datatype_realizer.ML"
blanchet@58179
   237
blanchet@58179
   238
lemma size_bool[code]: "size (b\<Colon>bool) = 0"
blanchet@58179
   239
  by (cases b) auto
blanchet@58179
   240
blanchet@58179
   241
lemma size_nat[simp, code]: "size (n\<Colon>nat) = n"
blanchet@58179
   242
  by (induct n) simp_all
blanchet@58179
   243
blanchet@58179
   244
declare prod.size[no_atp]
blanchet@58179
   245
blanchet@58179
   246
lemma size_sum_o_map: "size_sum g1 g2 \<circ> map_sum f1 f2 = size_sum (g1 \<circ> f1) (g2 \<circ> f2)"
blanchet@58179
   247
  by (rule ext) (case_tac x, auto)
blanchet@58179
   248
blanchet@58179
   249
lemma size_prod_o_map: "size_prod g1 g2 \<circ> map_prod f1 f2 = size_prod (g1 \<circ> f1) (g2 \<circ> f2)"
blanchet@58179
   250
  by (rule ext) auto
blanchet@58179
   251
blanchet@58179
   252
setup {*
blanchet@58179
   253
BNF_LFP_Size.register_size_global @{type_name sum} @{const_name size_sum} @{thms sum.size}
blanchet@58179
   254
  @{thms size_sum_o_map}
blanchet@58179
   255
#> BNF_LFP_Size.register_size_global @{type_name prod} @{const_name size_prod} @{thms prod.size}
blanchet@58179
   256
  @{thms size_prod_o_map}
blanchet@58179
   257
*}
blanchet@56643
   258
blanchet@55084
   259
hide_fact (open) id_transfer
blanchet@55084
   260
blanchet@48975
   261
end