src/HOL/BNF_Least_Fixpoint.thy
author wenzelm
Tue Oct 10 19:23:03 2017 +0200 (23 months ago)
changeset 66831 29ea2b900a05
parent 66290 88714f2e40e8
child 67091 1393c2340eec
permissions -rw-r--r--
tuned: each session has at most one defining entry;
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@58352
     7
Least fixpoint (datatype) operation on bounded natural functors.
blanchet@48975
     8
*)
blanchet@48975
     9
wenzelm@60758
    10
section \<open>Least Fixpoint (Datatype) Operation on Bounded Natural Functors\<close>
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@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@57987
    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@57987
    23
  by blast
blanchet@49312
    24
blanchet@49312
    25
lemma Collect_restrict: "{x. x \<in> X \<and> P x} \<subseteq> X"
blanchet@57987
    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@57987
    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@57987
    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@57987
    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@57987
    38
  unfolding underS_def Field_def by auto
blanchet@49312
    39
blanchet@49312
    40
lemma bij_betwE: "bij_betw f A B \<Longrightarrow> \<forall>a\<in>A. f a \<in> B"
blanchet@57987
    41
  unfolding bij_betw_def by auto
blanchet@49312
    42
blanchet@58159
    43
lemma f_the_inv_into_f_bij_betw:
blanchet@58159
    44
  "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
    45
  unfolding bij_betw_def by (blast intro: f_the_inv_into_f)
blanchet@49312
    46
traytel@56237
    47
lemma ex_bij_betw: "|A| \<le>o (r :: 'b rel) \<Longrightarrow> \<exists>f B :: 'b set. bij_betw f B A"
blanchet@58159
    48
  by (subst (asm) internalize_card_of_ordLeq) (auto dest!: iffD2[OF card_of_ordIso ordIso_symmetric])
blanchet@49312
    49
blanchet@49312
    50
lemma bij_betwI':
blanchet@49312
    51
  "\<lbrakk>\<And>x y. \<lbrakk>x \<in> X; y \<in> X\<rbrakk> \<Longrightarrow> (f x = f y) = (x = y);
blanchet@49312
    52
    \<And>x. x \<in> X \<Longrightarrow> f x \<in> Y;
blanchet@49312
    53
    \<And>y. y \<in> Y \<Longrightarrow> \<exists>x \<in> X. y = f x\<rbrakk> \<Longrightarrow> bij_betw f X Y"
blanchet@57987
    54
  unfolding bij_betw_def inj_on_def by blast
blanchet@49312
    55
blanchet@49312
    56
lemma surj_fun_eq:
blanchet@49312
    57
  assumes surj_on: "f ` X = UNIV" and eq_on: "\<forall>x \<in> X. (g1 o f) x = (g2 o f) x"
blanchet@49312
    58
  shows "g1 = g2"
blanchet@49312
    59
proof (rule ext)
blanchet@49312
    60
  fix y
blanchet@49312
    61
  from surj_on obtain x where "x \<in> X" and "y = f x" by blast
blanchet@49312
    62
  thus "g1 y = g2 y" using eq_on by simp
blanchet@49312
    63
qed
blanchet@49312
    64
blanchet@49312
    65
lemma Card_order_wo_rel: "Card_order r \<Longrightarrow> wo_rel r"
blanchet@58147
    66
  unfolding wo_rel_def card_order_on_def by blast
blanchet@49312
    67
blanchet@58147
    68
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
    69
  unfolding cinfinite_def by (auto simp add: infinite_Card_order_limit)
blanchet@49312
    70
blanchet@49312
    71
lemma Card_order_trans:
blanchet@49312
    72
  "\<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
    73
  unfolding card_order_on_def well_order_on_def linear_order_on_def
blanchet@58147
    74
    partial_order_on_def preorder_on_def trans_def antisym_def by blast
blanchet@49312
    75
blanchet@49312
    76
lemma Cinfinite_limit2:
blanchet@58147
    77
  assumes x1: "x1 \<in> Field r" and x2: "x2 \<in> Field r" and r: "Cinfinite r"
blanchet@58147
    78
  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
    79
proof -
blanchet@49312
    80
  from r have trans: "trans r" and total: "Total r" and antisym: "antisym r"
blanchet@49312
    81
    unfolding card_order_on_def well_order_on_def linear_order_on_def
blanchet@49312
    82
      partial_order_on_def preorder_on_def by auto
blanchet@49312
    83
  obtain y1 where y1: "y1 \<in> Field r" "x1 \<noteq> y1" "(x1, y1) \<in> r"
blanchet@49312
    84
    using Cinfinite_limit[OF x1 r] by blast
blanchet@49312
    85
  obtain y2 where y2: "y2 \<in> Field r" "x2 \<noteq> y2" "(x2, y2) \<in> r"
blanchet@49312
    86
    using Cinfinite_limit[OF x2 r] by blast
blanchet@49312
    87
  show ?thesis
blanchet@49312
    88
  proof (cases "y1 = y2")
blanchet@49312
    89
    case True with y1 y2 show ?thesis by blast
blanchet@49312
    90
  next
blanchet@49312
    91
    case False
blanchet@49312
    92
    with y1(1) y2(1) total have "(y1, y2) \<in> r \<or> (y2, y1) \<in> r"
blanchet@49312
    93
      unfolding total_on_def by auto
blanchet@49312
    94
    thus ?thesis
blanchet@49312
    95
    proof
blanchet@49312
    96
      assume *: "(y1, y2) \<in> r"
blanchet@49312
    97
      with trans y1(3) have "(x1, y2) \<in> r" unfolding trans_def by blast
blanchet@49312
    98
      with False y1 y2 * antisym show ?thesis by (cases "x1 = y2") (auto simp: antisym_def)
blanchet@49312
    99
    next
blanchet@49312
   100
      assume *: "(y2, y1) \<in> r"
blanchet@49312
   101
      with trans y2(3) have "(x2, y1) \<in> r" unfolding trans_def by blast
blanchet@49312
   102
      with False y1 y2 * antisym show ?thesis by (cases "x2 = y1") (auto simp: antisym_def)
blanchet@49312
   103
    qed
blanchet@49312
   104
  qed
blanchet@49312
   105
qed
blanchet@49312
   106
blanchet@58147
   107
lemma Cinfinite_limit_finite:
blanchet@58147
   108
  "\<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
   109
proof (induct X rule: finite_induct)
blanchet@49312
   110
  case empty thus ?case unfolding cinfinite_def using ex_in_conv[of "Field r"] finite.emptyI by auto
blanchet@49312
   111
next
blanchet@49312
   112
  case (insert x X)
blanchet@49312
   113
  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
   114
  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
   115
    using Cinfinite_limit2[OF _ y(1) insert(5), of x] insert(4) by blast
blanchet@49326
   116
  show ?case
blanchet@49326
   117
    apply (intro bexI ballI)
blanchet@49326
   118
    apply (erule insertE)
blanchet@49326
   119
    apply hypsubst
blanchet@49326
   120
    apply (rule z(2))
blanchet@49326
   121
    using Card_order_trans[OF insert(5)[THEN conjunct2]] y(2) z(3)
blanchet@49326
   122
    apply blast
blanchet@49326
   123
    apply (rule z(1))
blanchet@49326
   124
    done
blanchet@49312
   125
qed
blanchet@49312
   126
blanchet@49312
   127
lemma insert_subsetI: "\<lbrakk>x \<in> A; X \<subseteq> A\<rbrakk> \<Longrightarrow> insert x X \<subseteq> A"
blanchet@58147
   128
  by auto
blanchet@49312
   129
blanchet@58136
   130
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
   131
blanchet@49312
   132
lemma meta_spec2:
blanchet@49312
   133
  assumes "(\<And>x y. PROP P x y)"
blanchet@49312
   134
  shows "PROP P x y"
blanchet@58136
   135
  by (rule assms)
blanchet@49312
   136
traytel@54841
   137
lemma nchotomy_relcomppE:
traytel@55811
   138
  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
   139
  shows P
traytel@55811
   140
proof (rule relcompp.cases[OF assms(2)], hypsubst)
traytel@55811
   141
  fix b assume "r a b" "s b c"
traytel@55811
   142
  moreover from assms(1) obtain b' where "b = f b'" by blast
traytel@55811
   143
  ultimately show P by (blast intro: assms(3))
traytel@55811
   144
qed
traytel@54841
   145
traytel@52731
   146
lemma predicate2D_vimage2p: "\<lbrakk>R \<le> vimage2p f g S; R x y\<rbrakk> \<Longrightarrow> S (f x) (g y)"
traytel@52731
   147
  unfolding vimage2p_def by auto
traytel@52731
   148
traytel@55770
   149
lemma ssubst_Pair_rhs: "\<lbrakk>(r, s) \<in> R; s' = s\<rbrakk> \<Longrightarrow> (r, s') \<in> R"
blanchet@55851
   150
  by (rule ssubst)
traytel@55770
   151
blanchet@58211
   152
lemma all_mem_range1:
blanchet@58211
   153
  "(\<And>y. y \<in> range f \<Longrightarrow> P y) \<equiv> (\<And>x. P (f x)) "
blanchet@58211
   154
  by (rule equal_intr_rule) fast+
blanchet@58211
   155
blanchet@58211
   156
lemma all_mem_range2:
blanchet@58211
   157
  "(\<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
   158
  by (rule equal_intr_rule) fast+
blanchet@58211
   159
blanchet@58211
   160
lemma all_mem_range3:
blanchet@58211
   161
  "(\<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
   162
  by (rule equal_intr_rule) fast+
blanchet@58211
   163
blanchet@58211
   164
lemma all_mem_range4:
blanchet@58211
   165
  "(\<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
   166
   (\<And>x xa xb xc. P (f x xa xb xc))"
blanchet@58211
   167
  by (rule equal_intr_rule) fast+
blanchet@58211
   168
blanchet@58211
   169
lemma all_mem_range5:
blanchet@58211
   170
  "(\<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
   171
     y \<in> range fd \<Longrightarrow> P y) \<equiv>
blanchet@58211
   172
   (\<And>x xa xb xc xd. P (f x xa xb xc xd))"
blanchet@58211
   173
  by (rule equal_intr_rule) fast+
blanchet@58211
   174
blanchet@58211
   175
lemma all_mem_range6:
blanchet@58211
   176
  "(\<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
   177
     fe \<in> range fd \<Longrightarrow> ff \<in> range fe \<Longrightarrow> y \<in> range ff \<Longrightarrow> P y) \<equiv>
blanchet@58211
   178
   (\<And>x xa xb xc xd xe xf. P (f x xa xb xc xd xe xf))"
blanchet@58211
   179
  by (rule equal_intr_rule) (fastforce, fast)
blanchet@58211
   180
blanchet@58211
   181
lemma all_mem_range7:
blanchet@58211
   182
  "(\<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
   183
     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
   184
   (\<And>x xa xb xc xd xe xf xg. P (f x xa xb xc xd xe xf xg))"
blanchet@58211
   185
  by (rule equal_intr_rule) (fastforce, fast)
blanchet@58211
   186
blanchet@58211
   187
lemma all_mem_range8:
blanchet@58211
   188
  "(\<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
   189
     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
   190
   (\<And>x xa xb xc xd xe xf xg xh. P (f x xa xb xc xd xe xf xg xh))"
blanchet@58211
   191
  by (rule equal_intr_rule) (fastforce, fast)
blanchet@58211
   192
blanchet@58211
   193
lemmas all_mem_range = all_mem_range1 all_mem_range2 all_mem_range3 all_mem_range4 all_mem_range5
blanchet@58211
   194
  all_mem_range6 all_mem_range7 all_mem_range8
blanchet@58211
   195
blanchet@66290
   196
lemma pred_fun_True_id: "NO_MATCH id p \<Longrightarrow> pred_fun (\<lambda>x. True) p f = pred_fun (\<lambda>x. True) id (p \<circ> f)"
blanchet@66290
   197
  unfolding fun.pred_map unfolding comp_def id_def ..
blanchet@66290
   198
blanchet@55062
   199
ML_file "Tools/BNF/bnf_lfp_util.ML"
blanchet@55062
   200
ML_file "Tools/BNF/bnf_lfp_tactics.ML"
blanchet@55062
   201
ML_file "Tools/BNF/bnf_lfp.ML"
blanchet@55062
   202
ML_file "Tools/BNF/bnf_lfp_compat.ML"
blanchet@55571
   203
ML_file "Tools/BNF/bnf_lfp_rec_sugar_more.ML"
blanchet@58179
   204
ML_file "Tools/BNF/bnf_lfp_size.ML"
blanchet@58179
   205
blanchet@48975
   206
end