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