src/HOL/HOLCF/Porder.thy
author wenzelm
Sun Nov 02 17:16:01 2014 +0100 (2014-11-02)
changeset 58880 0baae4311a9f
parent 42151 4da4fc77664b
child 60586 1d31e3a50373
permissions -rw-r--r--
modernized header;
wenzelm@42151
     1
(*  Title:      HOL/HOLCF/Porder.thy
huffman@25773
     2
    Author:     Franz Regensburger and Brian Huffman
nipkow@243
     3
*)
nipkow@243
     4
wenzelm@58880
     5
section {* Partial orders *}
huffman@15576
     6
huffman@15577
     7
theory Porder
huffman@27317
     8
imports Main
huffman@15577
     9
begin
huffman@15576
    10
huffman@15587
    11
subsection {* Type class for partial orders *}
huffman@15587
    12
huffman@31076
    13
class below =
huffman@31076
    14
  fixes below :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
haftmann@31071
    15
begin
huffman@15576
    16
huffman@23284
    17
notation
huffman@40436
    18
  below (infix "<<" 50)
huffman@15576
    19
huffman@23284
    20
notation (xsymbols)
huffman@40436
    21
  below (infix "\<sqsubseteq>" 50)
huffman@15576
    22
huffman@41182
    23
abbreviation
huffman@41182
    24
  not_below :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infix "~<<" 50)
huffman@41182
    25
  where "not_below x y \<equiv> \<not> below x y"
huffman@41182
    26
huffman@41182
    27
notation (xsymbols)
huffman@41182
    28
  not_below (infix "\<notsqsubseteq>" 50)
huffman@41182
    29
huffman@31076
    30
lemma below_eq_trans: "\<lbrakk>a \<sqsubseteq> b; b = c\<rbrakk> \<Longrightarrow> a \<sqsubseteq> c"
haftmann@31071
    31
  by (rule subst)
haftmann@31071
    32
huffman@31076
    33
lemma eq_below_trans: "\<lbrakk>a = b; b \<sqsubseteq> c\<rbrakk> \<Longrightarrow> a \<sqsubseteq> c"
haftmann@31071
    34
  by (rule ssubst)
haftmann@31071
    35
haftmann@31071
    36
end
haftmann@31071
    37
huffman@31076
    38
class po = below +
huffman@31076
    39
  assumes below_refl [iff]: "x \<sqsubseteq> x"
huffman@31076
    40
  assumes below_trans: "x \<sqsubseteq> y \<Longrightarrow> y \<sqsubseteq> z \<Longrightarrow> x \<sqsubseteq> z"
huffman@31076
    41
  assumes below_antisym: "x \<sqsubseteq> y \<Longrightarrow> y \<sqsubseteq> x \<Longrightarrow> x = y"
haftmann@31071
    42
begin
huffman@15576
    43
huffman@40432
    44
lemma eq_imp_below: "x = y \<Longrightarrow> x \<sqsubseteq> y"
huffman@40432
    45
  by simp
huffman@40432
    46
huffman@31076
    47
lemma box_below: "a \<sqsubseteq> b \<Longrightarrow> c \<sqsubseteq> a \<Longrightarrow> b \<sqsubseteq> d \<Longrightarrow> c \<sqsubseteq> d"
huffman@31076
    48
  by (rule below_trans [OF below_trans])
huffman@17810
    49
haftmann@31071
    50
lemma po_eq_conv: "x = y \<longleftrightarrow> x \<sqsubseteq> y \<and> y \<sqsubseteq> x"
huffman@31076
    51
  by (fast intro!: below_antisym)
huffman@15576
    52
huffman@31076
    53
lemma rev_below_trans: "y \<sqsubseteq> z \<Longrightarrow> x \<sqsubseteq> y \<Longrightarrow> x \<sqsubseteq> z"
huffman@31076
    54
  by (rule below_trans)
huffman@18647
    55
huffman@41182
    56
lemma not_below2not_eq: "x \<notsqsubseteq> y \<Longrightarrow> x \<noteq> y"
haftmann@31071
    57
  by auto
haftmann@31071
    58
haftmann@31071
    59
end
huffman@18647
    60
huffman@18647
    61
lemmas HOLCF_trans_rules [trans] =
huffman@31076
    62
  below_trans
huffman@31076
    63
  below_antisym
huffman@31076
    64
  below_eq_trans
huffman@31076
    65
  eq_below_trans
huffman@18647
    66
haftmann@31071
    67
context po
haftmann@31071
    68
begin
haftmann@31071
    69
huffman@25777
    70
subsection {* Upper bounds *}
huffman@18071
    71
huffman@40436
    72
definition is_ub :: "'a set \<Rightarrow> 'a \<Rightarrow> bool" (infix "<|" 55) where
huffman@39968
    73
  "S <| x \<longleftrightarrow> (\<forall>y\<in>S. y \<sqsubseteq> x)"
huffman@18071
    74
huffman@25777
    75
lemma is_ubI: "(\<And>x. x \<in> S \<Longrightarrow> x \<sqsubseteq> u) \<Longrightarrow> S <| u"
haftmann@31071
    76
  by (simp add: is_ub_def)
huffman@25777
    77
huffman@25777
    78
lemma is_ubD: "\<lbrakk>S <| u; x \<in> S\<rbrakk> \<Longrightarrow> x \<sqsubseteq> u"
haftmann@31071
    79
  by (simp add: is_ub_def)
huffman@25777
    80
huffman@25777
    81
lemma ub_imageI: "(\<And>x. x \<in> S \<Longrightarrow> f x \<sqsubseteq> u) \<Longrightarrow> (\<lambda>x. f x) ` S <| u"
haftmann@31071
    82
  unfolding is_ub_def by fast
huffman@25777
    83
huffman@25777
    84
lemma ub_imageD: "\<lbrakk>f ` S <| u; x \<in> S\<rbrakk> \<Longrightarrow> f x \<sqsubseteq> u"
haftmann@31071
    85
  unfolding is_ub_def by fast
huffman@25777
    86
huffman@25777
    87
lemma ub_rangeI: "(\<And>i. S i \<sqsubseteq> x) \<Longrightarrow> range S <| x"
haftmann@31071
    88
  unfolding is_ub_def by fast
huffman@25777
    89
huffman@25777
    90
lemma ub_rangeD: "range S <| x \<Longrightarrow> S i \<sqsubseteq> x"
haftmann@31071
    91
  unfolding is_ub_def by fast
huffman@25777
    92
huffman@25828
    93
lemma is_ub_empty [simp]: "{} <| u"
haftmann@31071
    94
  unfolding is_ub_def by fast
huffman@25828
    95
huffman@25828
    96
lemma is_ub_insert [simp]: "(insert x A) <| y = (x \<sqsubseteq> y \<and> A <| y)"
haftmann@31071
    97
  unfolding is_ub_def by fast
huffman@25828
    98
huffman@25828
    99
lemma is_ub_upward: "\<lbrakk>S <| x; x \<sqsubseteq> y\<rbrakk> \<Longrightarrow> S <| y"
huffman@31076
   100
  unfolding is_ub_def by (fast intro: below_trans)
huffman@25828
   101
huffman@25777
   102
subsection {* Least upper bounds *}
huffman@25777
   103
huffman@40436
   104
definition is_lub :: "'a set \<Rightarrow> 'a \<Rightarrow> bool" (infix "<<|" 55) where
haftmann@31071
   105
  "S <<| x \<longleftrightarrow> S <| x \<and> (\<forall>u. S <| u \<longrightarrow> x \<sqsubseteq> u)"
huffman@18071
   106
haftmann@31071
   107
definition lub :: "'a set \<Rightarrow> 'a" where
wenzelm@25131
   108
  "lub S = (THE x. S <<| x)"
nipkow@243
   109
haftmann@31071
   110
end
haftmann@31071
   111
huffman@25777
   112
syntax
huffman@25777
   113
  "_BLub" :: "[pttrn, 'a set, 'b] \<Rightarrow> 'b" ("(3LUB _:_./ _)" [0,0, 10] 10)
huffman@25777
   114
huffman@25777
   115
syntax (xsymbols)
huffman@25777
   116
  "_BLub" :: "[pttrn, 'a set, 'b] \<Rightarrow> 'b" ("(3\<Squnion>_\<in>_./ _)" [0,0, 10] 10)
huffman@25777
   117
huffman@25777
   118
translations
huffman@25777
   119
  "LUB x:A. t" == "CONST lub ((%x. t) ` A)"
huffman@25777
   120
haftmann@31071
   121
context po
haftmann@31071
   122
begin
haftmann@31071
   123
wenzelm@21524
   124
abbreviation
wenzelm@21524
   125
  Lub  (binder "LUB " 10) where
wenzelm@21524
   126
  "LUB n. t n == lub (range t)"
oheimb@2394
   127
wenzelm@21524
   128
notation (xsymbols)
wenzelm@21524
   129
  Lub  (binder "\<Squnion> " 10)
nipkow@243
   130
huffman@25813
   131
text {* access to some definition as inference rule *}
huffman@25813
   132
huffman@25813
   133
lemma is_lubD1: "S <<| x \<Longrightarrow> S <| x"
haftmann@31071
   134
  unfolding is_lub_def by fast
huffman@25813
   135
huffman@40771
   136
lemma is_lubD2: "\<lbrakk>S <<| x; S <| u\<rbrakk> \<Longrightarrow> x \<sqsubseteq> u"
haftmann@31071
   137
  unfolding is_lub_def by fast
huffman@25813
   138
huffman@25813
   139
lemma is_lubI: "\<lbrakk>S <| x; \<And>u. S <| u \<Longrightarrow> x \<sqsubseteq> u\<rbrakk> \<Longrightarrow> S <<| x"
haftmann@31071
   140
  unfolding is_lub_def by fast
huffman@25813
   141
huffman@39969
   142
lemma is_lub_below_iff: "S <<| x \<Longrightarrow> x \<sqsubseteq> u \<longleftrightarrow> S <| u"
huffman@39969
   143
  unfolding is_lub_def is_ub_def by (metis below_trans)
huffman@39969
   144
huffman@15576
   145
text {* lubs are unique *}
huffman@15562
   146
huffman@40771
   147
lemma is_lub_unique: "\<lbrakk>S <<| x; S <<| y\<rbrakk> \<Longrightarrow> x = y"
huffman@40771
   148
  unfolding is_lub_def is_ub_def by (blast intro: below_antisym)
huffman@15562
   149
huffman@15576
   150
text {* technical lemmas about @{term lub} and @{term is_lub} *}
huffman@15562
   151
huffman@40771
   152
lemma is_lub_lub: "M <<| x \<Longrightarrow> M <<| lub M"
huffman@40771
   153
  unfolding lub_def by (rule theI [OF _ is_lub_unique])
huffman@15562
   154
huffman@40771
   155
lemma lub_eqI: "M <<| l \<Longrightarrow> lub M = l"
huffman@40771
   156
  by (rule is_lub_unique [OF is_lub_lub])
huffman@15562
   157
huffman@25780
   158
lemma is_lub_singleton: "{x} <<| x"
haftmann@31071
   159
  by (simp add: is_lub_def)
huffman@25780
   160
huffman@17810
   161
lemma lub_singleton [simp]: "lub {x} = x"
huffman@40771
   162
  by (rule is_lub_singleton [THEN lub_eqI])
huffman@25780
   163
huffman@25780
   164
lemma is_lub_bin: "x \<sqsubseteq> y \<Longrightarrow> {x, y} <<| y"
haftmann@31071
   165
  by (simp add: is_lub_def)
huffman@25780
   166
huffman@25780
   167
lemma lub_bin: "x \<sqsubseteq> y \<Longrightarrow> lub {x, y} = y"
huffman@40771
   168
  by (rule is_lub_bin [THEN lub_eqI])
huffman@15562
   169
huffman@25813
   170
lemma is_lub_maximal: "\<lbrakk>S <| x; x \<in> S\<rbrakk> \<Longrightarrow> S <<| x"
haftmann@31071
   171
  by (erule is_lubI, erule (1) is_ubD)
huffman@15562
   172
huffman@25813
   173
lemma lub_maximal: "\<lbrakk>S <| x; x \<in> S\<rbrakk> \<Longrightarrow> lub S = x"
huffman@40771
   174
  by (rule is_lub_maximal [THEN lub_eqI])
nipkow@243
   175
huffman@25695
   176
subsection {* Countable chains *}
huffman@25695
   177
haftmann@31071
   178
definition chain :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool" where
huffman@25695
   179
  -- {* Here we use countable chains and I prefer to code them as functions! *}
huffman@25922
   180
  "chain Y = (\<forall>i. Y i \<sqsubseteq> Y (Suc i))"
huffman@25922
   181
huffman@25922
   182
lemma chainI: "(\<And>i. Y i \<sqsubseteq> Y (Suc i)) \<Longrightarrow> chain Y"
haftmann@31071
   183
  unfolding chain_def by fast
huffman@25922
   184
huffman@25922
   185
lemma chainE: "chain Y \<Longrightarrow> Y i \<sqsubseteq> Y (Suc i)"
haftmann@31071
   186
  unfolding chain_def by fast
huffman@25695
   187
huffman@25695
   188
text {* chains are monotone functions *}
huffman@25695
   189
huffman@27317
   190
lemma chain_mono_less: "\<lbrakk>chain Y; i < j\<rbrakk> \<Longrightarrow> Y i \<sqsubseteq> Y j"
huffman@31076
   191
  by (erule less_Suc_induct, erule chainE, erule below_trans)
huffman@25695
   192
huffman@27317
   193
lemma chain_mono: "\<lbrakk>chain Y; i \<le> j\<rbrakk> \<Longrightarrow> Y i \<sqsubseteq> Y j"
haftmann@31071
   194
  by (cases "i = j", simp, simp add: chain_mono_less)
huffman@15562
   195
huffman@17810
   196
lemma chain_shift: "chain Y \<Longrightarrow> chain (\<lambda>i. Y (i + j))"
haftmann@31071
   197
  by (rule chainI, simp, erule chainE)
huffman@15562
   198
huffman@15576
   199
text {* technical lemmas about (least) upper bounds of chains *}
huffman@15562
   200
huffman@40771
   201
lemma is_lub_rangeD1: "range S <<| x \<Longrightarrow> S i \<sqsubseteq> x"
haftmann@31071
   202
  by (rule is_lubD1 [THEN ub_rangeD])
huffman@15562
   203
huffman@16318
   204
lemma is_ub_range_shift:
huffman@16318
   205
  "chain S \<Longrightarrow> range (\<lambda>i. S (i + j)) <| x = range S <| x"
huffman@16318
   206
apply (rule iffI)
huffman@16318
   207
apply (rule ub_rangeI)
huffman@31076
   208
apply (rule_tac y="S (i + j)" in below_trans)
huffman@25922
   209
apply (erule chain_mono)
huffman@16318
   210
apply (rule le_add1)
huffman@16318
   211
apply (erule ub_rangeD)
huffman@16318
   212
apply (rule ub_rangeI)
huffman@16318
   213
apply (erule ub_rangeD)
huffman@16318
   214
done
huffman@16318
   215
huffman@16318
   216
lemma is_lub_range_shift:
huffman@16318
   217
  "chain S \<Longrightarrow> range (\<lambda>i. S (i + j)) <<| x = range S <<| x"
haftmann@31071
   218
  by (simp add: is_lub_def is_ub_range_shift)
huffman@16318
   219
huffman@25695
   220
text {* the lub of a constant chain is the constant *}
huffman@25695
   221
huffman@25695
   222
lemma chain_const [simp]: "chain (\<lambda>i. c)"
haftmann@31071
   223
  by (simp add: chainI)
huffman@25695
   224
huffman@40771
   225
lemma is_lub_const: "range (\<lambda>x. c) <<| c"
huffman@25695
   226
by (blast dest: ub_rangeD intro: is_lubI ub_rangeI)
huffman@25695
   227
huffman@40771
   228
lemma lub_const [simp]: "(\<Squnion>i. c) = c"
huffman@40771
   229
  by (rule is_lub_const [THEN lub_eqI])
huffman@25695
   230
huffman@25695
   231
subsection {* Finite chains *}
huffman@25695
   232
haftmann@31071
   233
definition max_in_chain :: "nat \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> bool" where
huffman@25695
   234
  -- {* finite chains, needed for monotony of continuous functions *}
haftmann@31071
   235
  "max_in_chain i C \<longleftrightarrow> (\<forall>j. i \<le> j \<longrightarrow> C i = C j)"
huffman@25695
   236
haftmann@31071
   237
definition finite_chain :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool" where
huffman@25695
   238
  "finite_chain C = (chain C \<and> (\<exists>i. max_in_chain i C))"
huffman@25695
   239
huffman@15576
   240
text {* results about finite chains *}
huffman@15562
   241
huffman@25878
   242
lemma max_in_chainI: "(\<And>j. i \<le> j \<Longrightarrow> Y i = Y j) \<Longrightarrow> max_in_chain i Y"
haftmann@31071
   243
  unfolding max_in_chain_def by fast
huffman@25878
   244
huffman@25878
   245
lemma max_in_chainD: "\<lbrakk>max_in_chain i Y; i \<le> j\<rbrakk> \<Longrightarrow> Y i = Y j"
haftmann@31071
   246
  unfolding max_in_chain_def by fast
huffman@25878
   247
huffman@27317
   248
lemma finite_chainI:
huffman@27317
   249
  "\<lbrakk>chain C; max_in_chain i C\<rbrakk> \<Longrightarrow> finite_chain C"
haftmann@31071
   250
  unfolding finite_chain_def by fast
huffman@27317
   251
huffman@27317
   252
lemma finite_chainE:
huffman@27317
   253
  "\<lbrakk>finite_chain C; \<And>i. \<lbrakk>chain C; max_in_chain i C\<rbrakk> \<Longrightarrow> R\<rbrakk> \<Longrightarrow> R"
haftmann@31071
   254
  unfolding finite_chain_def by fast
huffman@27317
   255
huffman@17810
   256
lemma lub_finch1: "\<lbrakk>chain C; max_in_chain i C\<rbrakk> \<Longrightarrow> range C <<| C i"
huffman@15562
   257
apply (rule is_lubI)
huffman@17810
   258
apply (rule ub_rangeI, rename_tac j)
huffman@17810
   259
apply (rule_tac x=i and y=j in linorder_le_cases)
huffman@25878
   260
apply (drule (1) max_in_chainD, simp)
huffman@25922
   261
apply (erule (1) chain_mono)
huffman@15562
   262
apply (erule ub_rangeD)
huffman@15562
   263
done
huffman@15562
   264
wenzelm@25131
   265
lemma lub_finch2:
huffman@27317
   266
  "finite_chain C \<Longrightarrow> range C <<| C (LEAST i. max_in_chain i C)"
huffman@27317
   267
apply (erule finite_chainE)
huffman@27317
   268
apply (erule LeastI2 [where Q="\<lambda>i. range C <<| C i"])
huffman@17810
   269
apply (erule (1) lub_finch1)
huffman@15562
   270
done
huffman@15562
   271
huffman@19621
   272
lemma finch_imp_finite_range: "finite_chain Y \<Longrightarrow> finite (range Y)"
huffman@27317
   273
 apply (erule finite_chainE)
huffman@27317
   274
 apply (rule_tac B="Y ` {..i}" in finite_subset)
huffman@19621
   275
  apply (rule subsetI)
huffman@19621
   276
  apply (erule rangeE, rename_tac j)
huffman@19621
   277
  apply (rule_tac x=i and y=j in linorder_le_cases)
huffman@19621
   278
   apply (subgoal_tac "Y j = Y i", simp)
huffman@19621
   279
   apply (simp add: max_in_chain_def)
huffman@19621
   280
  apply simp
huffman@27317
   281
 apply simp
huffman@19621
   282
done
huffman@19621
   283
huffman@27317
   284
lemma finite_range_has_max:
huffman@27317
   285
  fixes f :: "nat \<Rightarrow> 'a" and r :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
huffman@27317
   286
  assumes mono: "\<And>i j. i \<le> j \<Longrightarrow> r (f i) (f j)"
huffman@27317
   287
  assumes finite_range: "finite (range f)"
huffman@27317
   288
  shows "\<exists>k. \<forall>i. r (f i) (f k)"
huffman@27317
   289
proof (intro exI allI)
huffman@27317
   290
  fix i :: nat
huffman@27317
   291
  let ?j = "LEAST k. f k = f i"
huffman@27317
   292
  let ?k = "Max ((\<lambda>x. LEAST k. f k = x) ` range f)"
huffman@27317
   293
  have "?j \<le> ?k"
huffman@27317
   294
  proof (rule Max_ge)
huffman@27317
   295
    show "finite ((\<lambda>x. LEAST k. f k = x) ` range f)"
huffman@27317
   296
      using finite_range by (rule finite_imageI)
huffman@27317
   297
    show "?j \<in> (\<lambda>x. LEAST k. f k = x) ` range f"
huffman@27317
   298
      by (intro imageI rangeI)
huffman@27317
   299
  qed
huffman@27317
   300
  hence "r (f ?j) (f ?k)"
huffman@27317
   301
    by (rule mono)
huffman@27317
   302
  also have "f ?j = f i"
huffman@27317
   303
    by (rule LeastI, rule refl)
huffman@27317
   304
  finally show "r (f i) (f ?k)" .
huffman@27317
   305
qed
huffman@27317
   306
huffman@19621
   307
lemma finite_range_imp_finch:
huffman@19621
   308
  "\<lbrakk>chain Y; finite (range Y)\<rbrakk> \<Longrightarrow> finite_chain Y"
huffman@27317
   309
 apply (subgoal_tac "\<exists>k. \<forall>i. Y i \<sqsubseteq> Y k")
huffman@27317
   310
  apply (erule exE)
huffman@27317
   311
  apply (rule finite_chainI, assumption)
huffman@27317
   312
  apply (rule max_in_chainI)
huffman@31076
   313
  apply (rule below_antisym)
huffman@27317
   314
   apply (erule (1) chain_mono)
huffman@27317
   315
  apply (erule spec)
huffman@27317
   316
 apply (rule finite_range_has_max)
huffman@27317
   317
  apply (erule (1) chain_mono)
huffman@27317
   318
 apply assumption
huffman@19621
   319
done
huffman@19621
   320
huffman@17810
   321
lemma bin_chain: "x \<sqsubseteq> y \<Longrightarrow> chain (\<lambda>i. if i=0 then x else y)"
haftmann@31071
   322
  by (rule chainI, simp)
huffman@17810
   323
huffman@17810
   324
lemma bin_chainmax:
huffman@17810
   325
  "x \<sqsubseteq> y \<Longrightarrow> max_in_chain (Suc 0) (\<lambda>i. if i=0 then x else y)"
haftmann@31071
   326
  unfolding max_in_chain_def by simp
huffman@15562
   327
huffman@40771
   328
lemma is_lub_bin_chain:
huffman@17810
   329
  "x \<sqsubseteq> y \<Longrightarrow> range (\<lambda>i::nat. if i=0 then x else y) <<| y"
huffman@17810
   330
apply (frule bin_chain)
huffman@17810
   331
apply (drule bin_chainmax)
huffman@17810
   332
apply (drule (1) lub_finch1)
huffman@17810
   333
apply simp
huffman@15562
   334
done
huffman@15562
   335
huffman@15576
   336
text {* the maximal element in a chain is its lub *}
huffman@15562
   337
huffman@17810
   338
lemma lub_chain_maxelem: "\<lbrakk>Y i = c; \<forall>i. Y i \<sqsubseteq> c\<rbrakk> \<Longrightarrow> lub (range Y) = c"
huffman@40771
   339
  by (blast dest: ub_rangeD intro: lub_eqI is_lubI ub_rangeI)
huffman@15562
   340
huffman@18071
   341
end
haftmann@31071
   342
huffman@31076
   343
end