src/HOL/Complete_Partial_Order.thy
author Andreas Lochbihler
Tue Apr 14 13:56:34 2015 +0200 (2015-04-14)
changeset 60061 279472fa0b1d
parent 60057 86fa63ce8156
child 60758 d8d85a8172b5
permissions -rw-r--r--
move some lemmas from AFP/Coinductive
krauss@40106
     1
(* Title:    HOL/Complete_Partial_Order.thy
krauss@40106
     2
   Author:   Brian Huffman, Portland State University
krauss@40106
     3
   Author:   Alexander Krauss, TU Muenchen
krauss@40106
     4
*)
krauss@40106
     5
wenzelm@58889
     6
section {* Chain-complete partial orders and their fixpoints *}
krauss@40106
     7
krauss@40106
     8
theory Complete_Partial_Order
krauss@40106
     9
imports Product_Type
krauss@40106
    10
begin
krauss@40106
    11
krauss@40106
    12
subsection {* Monotone functions *}
krauss@40106
    13
krauss@40106
    14
text {* Dictionary-passing version of @{const Orderings.mono}. *}
krauss@40106
    15
krauss@40106
    16
definition monotone :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> bool"
krauss@40106
    17
where "monotone orda ordb f \<longleftrightarrow> (\<forall>x y. orda x y \<longrightarrow> ordb (f x) (f y))"
krauss@40106
    18
krauss@40106
    19
lemma monotoneI[intro?]: "(\<And>x y. orda x y \<Longrightarrow> ordb (f x) (f y))
krauss@40106
    20
 \<Longrightarrow> monotone orda ordb f"
krauss@40106
    21
unfolding monotone_def by iprover
krauss@40106
    22
krauss@40106
    23
lemma monotoneD[dest?]: "monotone orda ordb f \<Longrightarrow> orda x y \<Longrightarrow> ordb (f x) (f y)"
krauss@40106
    24
unfolding monotone_def by iprover
krauss@40106
    25
krauss@40106
    26
krauss@40106
    27
subsection {* Chains *}
krauss@40106
    28
krauss@40106
    29
text {* A chain is a totally-ordered set. Chains are parameterized over
krauss@40106
    30
  the order for maximal flexibility, since type classes are not enough.
krauss@40106
    31
*}
krauss@40106
    32
krauss@40106
    33
definition
krauss@40106
    34
  chain :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool"
krauss@40106
    35
where
krauss@40106
    36
  "chain ord S \<longleftrightarrow> (\<forall>x\<in>S. \<forall>y\<in>S. ord x y \<or> ord y x)"
krauss@40106
    37
krauss@40106
    38
lemma chainI:
krauss@40106
    39
  assumes "\<And>x y. x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> ord x y \<or> ord y x"
krauss@40106
    40
  shows "chain ord S"
krauss@40106
    41
using assms unfolding chain_def by fast
krauss@40106
    42
krauss@40106
    43
lemma chainD:
krauss@40106
    44
  assumes "chain ord S" and "x \<in> S" and "y \<in> S"
krauss@40106
    45
  shows "ord x y \<or> ord y x"
krauss@40106
    46
using assms unfolding chain_def by fast
krauss@40106
    47
krauss@40106
    48
lemma chainE:
krauss@40106
    49
  assumes "chain ord S" and "x \<in> S" and "y \<in> S"
krauss@40106
    50
  obtains "ord x y" | "ord y x"
krauss@40106
    51
using assms unfolding chain_def by fast
krauss@40106
    52
Andreas@54630
    53
lemma chain_empty: "chain ord {}"
Andreas@54630
    54
by(simp add: chain_def)
Andreas@54630
    55
Andreas@60057
    56
lemma chain_equality: "chain op = A \<longleftrightarrow> (\<forall>x\<in>A. \<forall>y\<in>A. x = y)"
Andreas@60057
    57
by(auto simp add: chain_def)
Andreas@60057
    58
Andreas@60061
    59
lemma chain_subset:
Andreas@60061
    60
  "\<lbrakk> chain ord A; B \<subseteq> A \<rbrakk>
Andreas@60061
    61
  \<Longrightarrow> chain ord B"
Andreas@60061
    62
by(rule chainI)(blast dest: chainD)
Andreas@60061
    63
Andreas@60061
    64
lemma chain_imageI: 
Andreas@60061
    65
  assumes chain: "chain le_a Y"
Andreas@60061
    66
  and mono: "\<And>x y. \<lbrakk> x \<in> Y; y \<in> Y; le_a x y \<rbrakk> \<Longrightarrow> le_b (f x) (f y)"
Andreas@60061
    67
  shows "chain le_b (f ` Y)"
Andreas@60061
    68
by(blast intro: chainI dest: chainD[OF chain] mono)
Andreas@60061
    69
krauss@40106
    70
subsection {* Chain-complete partial orders *}
krauss@40106
    71
krauss@40106
    72
text {*
krauss@40106
    73
  A ccpo has a least upper bound for any chain.  In particular, the
krauss@40106
    74
  empty set is a chain, so every ccpo must have a bottom element.
krauss@40106
    75
*}
krauss@40106
    76
huffman@46041
    77
class ccpo = order + Sup +
huffman@46041
    78
  assumes ccpo_Sup_upper: "\<lbrakk>chain (op \<le>) A; x \<in> A\<rbrakk> \<Longrightarrow> x \<le> Sup A"
huffman@46041
    79
  assumes ccpo_Sup_least: "\<lbrakk>chain (op \<le>) A; \<And>x. x \<in> A \<Longrightarrow> x \<le> z\<rbrakk> \<Longrightarrow> Sup A \<le> z"
krauss@40106
    80
begin
krauss@40106
    81
Andreas@60061
    82
lemma chain_singleton: "Complete_Partial_Order.chain op \<le> {x}"
Andreas@60061
    83
by(rule chainI) simp
Andreas@60061
    84
Andreas@60061
    85
lemma ccpo_Sup_singleton [simp]: "\<Squnion>{x} = x"
Andreas@60061
    86
by(rule antisym)(auto intro: ccpo_Sup_least ccpo_Sup_upper simp add: chain_singleton)
Andreas@60061
    87
krauss@40106
    88
subsection {* Transfinite iteration of a function *}
krauss@40106
    89
krauss@40106
    90
inductive_set iterates :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a set"
krauss@40106
    91
for f :: "'a \<Rightarrow> 'a"
krauss@40106
    92
where
krauss@40106
    93
  step: "x \<in> iterates f \<Longrightarrow> f x \<in> iterates f"
huffman@46041
    94
| Sup: "chain (op \<le>) M \<Longrightarrow> \<forall>x\<in>M. x \<in> iterates f \<Longrightarrow> Sup M \<in> iterates f"
krauss@40106
    95
krauss@40106
    96
lemma iterates_le_f:
krauss@40106
    97
  "x \<in> iterates f \<Longrightarrow> monotone (op \<le>) (op \<le>) f \<Longrightarrow> x \<le> f x"
krauss@40106
    98
by (induct x rule: iterates.induct)
huffman@46041
    99
  (force dest: monotoneD intro!: ccpo_Sup_upper ccpo_Sup_least)+
krauss@40106
   100
krauss@40106
   101
lemma chain_iterates:
krauss@40106
   102
  assumes f: "monotone (op \<le>) (op \<le>) f"
krauss@40106
   103
  shows "chain (op \<le>) (iterates f)" (is "chain _ ?C")
krauss@40106
   104
proof (rule chainI)
krauss@40106
   105
  fix x y assume "x \<in> ?C" "y \<in> ?C"
krauss@40106
   106
  then show "x \<le> y \<or> y \<le> x"
krauss@40106
   107
  proof (induct x arbitrary: y rule: iterates.induct)
krauss@40106
   108
    fix x y assume y: "y \<in> ?C"
krauss@40106
   109
    and IH: "\<And>z. z \<in> ?C \<Longrightarrow> x \<le> z \<or> z \<le> x"
krauss@40106
   110
    from y show "f x \<le> y \<or> y \<le> f x"
krauss@40106
   111
    proof (induct y rule: iterates.induct)
krauss@40106
   112
      case (step y) with IH f show ?case by (auto dest: monotoneD)
krauss@40106
   113
    next
huffman@46041
   114
      case (Sup M)
krauss@40106
   115
      then have chM: "chain (op \<le>) M"
krauss@40106
   116
        and IH': "\<And>z. z \<in> M \<Longrightarrow> f x \<le> z \<or> z \<le> f x" by auto
huffman@46041
   117
      show "f x \<le> Sup M \<or> Sup M \<le> f x"
krauss@40106
   118
      proof (cases "\<exists>z\<in>M. f x \<le> z")
huffman@46041
   119
        case True then have "f x \<le> Sup M"
krauss@40106
   120
          apply rule
krauss@40106
   121
          apply (erule order_trans)
huffman@46041
   122
          by (rule ccpo_Sup_upper[OF chM])
krauss@40106
   123
        thus ?thesis ..
krauss@40106
   124
      next
krauss@40106
   125
        case False with IH'
huffman@46041
   126
        show ?thesis by (auto intro: ccpo_Sup_least[OF chM])
krauss@40106
   127
      qed
krauss@40106
   128
    qed
krauss@40106
   129
  next
huffman@46041
   130
    case (Sup M y)
krauss@40106
   131
    show ?case
krauss@40106
   132
    proof (cases "\<exists>x\<in>M. y \<le> x")
huffman@46041
   133
      case True then have "y \<le> Sup M"
krauss@40106
   134
        apply rule
krauss@40106
   135
        apply (erule order_trans)
huffman@46041
   136
        by (rule ccpo_Sup_upper[OF Sup(1)])
krauss@40106
   137
      thus ?thesis ..
krauss@40106
   138
    next
huffman@46041
   139
      case False with Sup
huffman@46041
   140
      show ?thesis by (auto intro: ccpo_Sup_least)
krauss@40106
   141
    qed
krauss@40106
   142
  qed
krauss@40106
   143
qed
krauss@40106
   144
Andreas@54630
   145
lemma bot_in_iterates: "Sup {} \<in> iterates f"
Andreas@54630
   146
by(auto intro: iterates.Sup simp add: chain_empty)
Andreas@54630
   147
krauss@40106
   148
subsection {* Fixpoint combinator *}
krauss@40106
   149
krauss@40106
   150
definition
krauss@40106
   151
  fixp :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a"
krauss@40106
   152
where
huffman@46041
   153
  "fixp f = Sup (iterates f)"
krauss@40106
   154
krauss@40106
   155
lemma iterates_fixp:
krauss@40106
   156
  assumes f: "monotone (op \<le>) (op \<le>) f" shows "fixp f \<in> iterates f"
krauss@40106
   157
unfolding fixp_def
huffman@46041
   158
by (simp add: iterates.Sup chain_iterates f)
krauss@40106
   159
krauss@40106
   160
lemma fixp_unfold:
krauss@40106
   161
  assumes f: "monotone (op \<le>) (op \<le>) f"
krauss@40106
   162
  shows "fixp f = f (fixp f)"
krauss@40106
   163
proof (rule antisym)
krauss@40106
   164
  show "fixp f \<le> f (fixp f)"
krauss@40106
   165
    by (intro iterates_le_f iterates_fixp f)
huffman@46041
   166
  have "f (fixp f) \<le> Sup (iterates f)"
huffman@46041
   167
    by (intro ccpo_Sup_upper chain_iterates f iterates.step iterates_fixp)
krauss@40106
   168
  thus "f (fixp f) \<le> fixp f"
krauss@40106
   169
    unfolding fixp_def .
krauss@40106
   170
qed
krauss@40106
   171
krauss@40106
   172
lemma fixp_lowerbound:
krauss@40106
   173
  assumes f: "monotone (op \<le>) (op \<le>) f" and z: "f z \<le> z" shows "fixp f \<le> z"
krauss@40106
   174
unfolding fixp_def
huffman@46041
   175
proof (rule ccpo_Sup_least[OF chain_iterates[OF f]])
krauss@40106
   176
  fix x assume "x \<in> iterates f"
krauss@40106
   177
  thus "x \<le> z"
krauss@40106
   178
  proof (induct x rule: iterates.induct)
krauss@40106
   179
    fix x assume "x \<le> z" with f have "f x \<le> f z" by (rule monotoneD)
krauss@40106
   180
    also note z finally show "f x \<le> z" .
huffman@46041
   181
  qed (auto intro: ccpo_Sup_least)
krauss@40106
   182
qed
krauss@40106
   183
Andreas@53361
   184
end
krauss@40106
   185
krauss@40106
   186
subsection {* Fixpoint induction *}
krauss@40106
   187
Andreas@53361
   188
setup {* Sign.map_naming (Name_Space.mandatory_path "ccpo") *}
Andreas@53361
   189
Andreas@53361
   190
definition admissible :: "('a set \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
Andreas@54630
   191
where "admissible lub ord P = (\<forall>A. chain ord A \<longrightarrow> (A \<noteq> {}) \<longrightarrow> (\<forall>x\<in>A. P x) \<longrightarrow> P (lub A))"
krauss@40106
   192
krauss@40106
   193
lemma admissibleI:
Andreas@54630
   194
  assumes "\<And>A. chain ord A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> \<forall>x\<in>A. P x \<Longrightarrow> P (lub A)"
Andreas@53361
   195
  shows "ccpo.admissible lub ord P"
Andreas@53361
   196
using assms unfolding ccpo.admissible_def by fast
krauss@40106
   197
krauss@40106
   198
lemma admissibleD:
Andreas@53361
   199
  assumes "ccpo.admissible lub ord P"
Andreas@53361
   200
  assumes "chain ord A"
Andreas@54630
   201
  assumes "A \<noteq> {}"
krauss@40106
   202
  assumes "\<And>x. x \<in> A \<Longrightarrow> P x"
Andreas@53361
   203
  shows "P (lub A)"
Andreas@53361
   204
using assms by (auto simp: ccpo.admissible_def)
krauss@40106
   205
Andreas@53361
   206
setup {* Sign.map_naming Name_Space.parent_path *}
Andreas@53361
   207
Andreas@53361
   208
lemma (in ccpo) fixp_induct:
Andreas@53361
   209
  assumes adm: "ccpo.admissible Sup (op \<le>) P"
krauss@40106
   210
  assumes mono: "monotone (op \<le>) (op \<le>) f"
Andreas@54630
   211
  assumes bot: "P (Sup {})"
krauss@40106
   212
  assumes step: "\<And>x. P x \<Longrightarrow> P (f x)"
krauss@40106
   213
  shows "P (fixp f)"
krauss@40106
   214
unfolding fixp_def using adm chain_iterates[OF mono]
Andreas@53361
   215
proof (rule ccpo.admissibleD)
Andreas@54630
   216
  show "iterates f \<noteq> {}" using bot_in_iterates by auto
krauss@40106
   217
  fix x assume "x \<in> iterates f"
krauss@40106
   218
  thus "P x"
krauss@40106
   219
    by (induct rule: iterates.induct)
Andreas@54630
   220
      (case_tac "M = {}", auto intro: step bot ccpo.admissibleD adm)
krauss@40106
   221
qed
krauss@40106
   222
Andreas@53361
   223
lemma admissible_True: "ccpo.admissible lub ord (\<lambda>x. True)"
Andreas@53361
   224
unfolding ccpo.admissible_def by simp
krauss@40106
   225
Andreas@54630
   226
(*lemma admissible_False: "\<not> ccpo.admissible lub ord (\<lambda>x. False)"
Andreas@53361
   227
unfolding ccpo.admissible_def chain_def by simp
Andreas@54630
   228
*)
Andreas@54630
   229
lemma admissible_const: "ccpo.admissible lub ord (\<lambda>x. t)"
Andreas@54630
   230
by(auto intro: ccpo.admissibleI)
krauss@40106
   231
krauss@40106
   232
lemma admissible_conj:
Andreas@53361
   233
  assumes "ccpo.admissible lub ord (\<lambda>x. P x)"
Andreas@53361
   234
  assumes "ccpo.admissible lub ord (\<lambda>x. Q x)"
Andreas@53361
   235
  shows "ccpo.admissible lub ord (\<lambda>x. P x \<and> Q x)"
Andreas@53361
   236
using assms unfolding ccpo.admissible_def by simp
krauss@40106
   237
krauss@40106
   238
lemma admissible_all:
Andreas@53361
   239
  assumes "\<And>y. ccpo.admissible lub ord (\<lambda>x. P x y)"
Andreas@53361
   240
  shows "ccpo.admissible lub ord (\<lambda>x. \<forall>y. P x y)"
Andreas@53361
   241
using assms unfolding ccpo.admissible_def by fast
krauss@40106
   242
krauss@40106
   243
lemma admissible_ball:
Andreas@53361
   244
  assumes "\<And>y. y \<in> A \<Longrightarrow> ccpo.admissible lub ord (\<lambda>x. P x y)"
Andreas@53361
   245
  shows "ccpo.admissible lub ord (\<lambda>x. \<forall>y\<in>A. P x y)"
Andreas@53361
   246
using assms unfolding ccpo.admissible_def by fast
krauss@40106
   247
Andreas@53361
   248
lemma chain_compr: "chain ord A \<Longrightarrow> chain ord {x \<in> A. P x}"
krauss@40106
   249
unfolding chain_def by fast
krauss@40106
   250
Andreas@53361
   251
context ccpo begin
Andreas@53361
   252
krauss@40106
   253
lemma admissible_disj_lemma:
krauss@40106
   254
  assumes A: "chain (op \<le>)A"
krauss@40106
   255
  assumes P: "\<forall>x\<in>A. \<exists>y\<in>A. x \<le> y \<and> P y"
huffman@46041
   256
  shows "Sup A = Sup {x \<in> A. P x}"
krauss@40106
   257
proof (rule antisym)
krauss@40106
   258
  have *: "chain (op \<le>) {x \<in> A. P x}"
krauss@40106
   259
    by (rule chain_compr [OF A])
huffman@46041
   260
  show "Sup A \<le> Sup {x \<in> A. P x}"
huffman@46041
   261
    apply (rule ccpo_Sup_least [OF A])
krauss@40106
   262
    apply (drule P [rule_format], clarify)
krauss@40106
   263
    apply (erule order_trans)
huffman@46041
   264
    apply (simp add: ccpo_Sup_upper [OF *])
krauss@40106
   265
    done
huffman@46041
   266
  show "Sup {x \<in> A. P x} \<le> Sup A"
huffman@46041
   267
    apply (rule ccpo_Sup_least [OF *])
krauss@40106
   268
    apply clarify
huffman@46041
   269
    apply (simp add: ccpo_Sup_upper [OF A])
krauss@40106
   270
    done
krauss@40106
   271
qed
krauss@40106
   272
krauss@40106
   273
lemma admissible_disj:
krauss@40106
   274
  fixes P Q :: "'a \<Rightarrow> bool"
Andreas@53361
   275
  assumes P: "ccpo.admissible Sup (op \<le>) (\<lambda>x. P x)"
Andreas@53361
   276
  assumes Q: "ccpo.admissible Sup (op \<le>) (\<lambda>x. Q x)"
Andreas@53361
   277
  shows "ccpo.admissible Sup (op \<le>) (\<lambda>x. P x \<or> Q x)"
Andreas@53361
   278
proof (rule ccpo.admissibleI)
krauss@40106
   279
  fix A :: "'a set" assume A: "chain (op \<le>) A"
Andreas@54630
   280
  assume "A \<noteq> {}"
Andreas@54630
   281
    and "\<forall>x\<in>A. P x \<or> Q x"
Andreas@54630
   282
  hence "(\<exists>x\<in>A. P x) \<and> (\<forall>x\<in>A. \<exists>y\<in>A. x \<le> y \<and> P y) \<or> (\<exists>x\<in>A. Q x) \<and> (\<forall>x\<in>A. \<exists>y\<in>A. x \<le> y \<and> Q y)"
krauss@40106
   283
    using chainD[OF A] by blast
Andreas@54630
   284
  hence "(\<exists>x. x \<in> A \<and> P x) \<and> Sup A = Sup {x \<in> A. P x} \<or> (\<exists>x. x \<in> A \<and> Q x) \<and> Sup A = Sup {x \<in> A. Q x}"
Andreas@54630
   285
    using admissible_disj_lemma [OF A] by blast
huffman@46041
   286
  thus "P (Sup A) \<or> Q (Sup A)"
krauss@40106
   287
    apply (rule disjE, simp_all)
Andreas@54630
   288
    apply (rule disjI1, rule ccpo.admissibleD [OF P chain_compr [OF A]], simp, simp)
Andreas@54630
   289
    apply (rule disjI2, rule ccpo.admissibleD [OF Q chain_compr [OF A]], simp, simp)
krauss@40106
   290
    done
krauss@40106
   291
qed
krauss@40106
   292
krauss@40106
   293
end
krauss@40106
   294
huffman@46041
   295
instance complete_lattice \<subseteq> ccpo
huffman@46041
   296
  by default (fast intro: Sup_upper Sup_least)+
huffman@46041
   297
huffman@46041
   298
lemma lfp_eq_fixp:
huffman@46041
   299
  assumes f: "mono f" shows "lfp f = fixp f"
huffman@46041
   300
proof (rule antisym)
huffman@46041
   301
  from f have f': "monotone (op \<le>) (op \<le>) f"
huffman@46041
   302
    unfolding mono_def monotone_def .
huffman@46041
   303
  show "lfp f \<le> fixp f"
huffman@46041
   304
    by (rule lfp_lowerbound, subst fixp_unfold [OF f'], rule order_refl)
huffman@46041
   305
  show "fixp f \<le> lfp f"
huffman@46041
   306
    by (rule fixp_lowerbound [OF f'], subst lfp_unfold [OF f], rule order_refl)
huffman@46041
   307
qed
huffman@46041
   308
Andreas@53361
   309
hide_const (open) iterates fixp
krauss@40106
   310
krauss@40106
   311
end