src/HOL/Orderings.thy
author paulson <lp15@cam.ac.uk>
Tue Apr 25 16:39:54 2017 +0100 (2017-04-25)
changeset 65578 e4997c181cce
parent 64758 3b33d2fc5fc0
child 65963 ca1e636fa716
permissions -rw-r--r--
New material from PNT proof, as well as more default [simp] declarations. Also removed duplicate theorems about geometric series
haftmann@28685
     1
(*  Title:      HOL/Orderings.thy
nipkow@15524
     2
    Author:     Tobias Nipkow, Markus Wenzel, and Larry Paulson
nipkow@15524
     3
*)
nipkow@15524
     4
wenzelm@60758
     5
section \<open>Abstract orderings\<close>
nipkow@15524
     6
nipkow@15524
     7
theory Orderings
haftmann@35301
     8
imports HOL
wenzelm@46950
     9
keywords "print_orders" :: diag
nipkow@15524
    10
begin
nipkow@15524
    11
wenzelm@48891
    12
ML_file "~~/src/Provers/order.ML"
wenzelm@48891
    13
ML_file "~~/src/Provers/quasi.ML"  (* FIXME unused? *)
wenzelm@48891
    14
wenzelm@60758
    15
subsection \<open>Abstract ordering\<close>
haftmann@51487
    16
haftmann@51487
    17
locale ordering =
haftmann@63290
    18
  fixes less_eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infix "\<^bold>\<le>" 50)
haftmann@63290
    19
   and less :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infix "\<^bold><" 50)
haftmann@63290
    20
  assumes strict_iff_order: "a \<^bold>< b \<longleftrightarrow> a \<^bold>\<le> b \<and> a \<noteq> b"
haftmann@63290
    21
  assumes refl: "a \<^bold>\<le> a" \<comment> \<open>not \<open>iff\<close>: makes problems due to multiple (dual) interpretations\<close>
haftmann@63290
    22
    and antisym: "a \<^bold>\<le> b \<Longrightarrow> b \<^bold>\<le> a \<Longrightarrow> a = b"
haftmann@63290
    23
    and trans: "a \<^bold>\<le> b \<Longrightarrow> b \<^bold>\<le> c \<Longrightarrow> a \<^bold>\<le> c"
haftmann@51487
    24
begin
haftmann@51487
    25
haftmann@51487
    26
lemma strict_implies_order:
haftmann@63290
    27
  "a \<^bold>< b \<Longrightarrow> a \<^bold>\<le> b"
haftmann@51487
    28
  by (simp add: strict_iff_order)
haftmann@51487
    29
haftmann@51487
    30
lemma strict_implies_not_eq:
haftmann@63290
    31
  "a \<^bold>< b \<Longrightarrow> a \<noteq> b"
haftmann@51487
    32
  by (simp add: strict_iff_order)
haftmann@51487
    33
haftmann@51487
    34
lemma not_eq_order_implies_strict:
haftmann@63290
    35
  "a \<noteq> b \<Longrightarrow> a \<^bold>\<le> b \<Longrightarrow> a \<^bold>< b"
haftmann@51487
    36
  by (simp add: strict_iff_order)
haftmann@51487
    37
haftmann@51487
    38
lemma order_iff_strict:
haftmann@63290
    39
  "a \<^bold>\<le> b \<longleftrightarrow> a \<^bold>< b \<or> a = b"
haftmann@51487
    40
  by (auto simp add: strict_iff_order refl)
haftmann@51487
    41
wenzelm@61799
    42
lemma irrefl: \<comment> \<open>not \<open>iff\<close>: makes problems due to multiple (dual) interpretations\<close>
haftmann@63290
    43
  "\<not> a \<^bold>< a"
haftmann@51487
    44
  by (simp add: strict_iff_order)
haftmann@51487
    45
haftmann@51487
    46
lemma asym:
haftmann@63290
    47
  "a \<^bold>< b \<Longrightarrow> b \<^bold>< a \<Longrightarrow> False"
haftmann@51487
    48
  by (auto simp add: strict_iff_order intro: antisym)
haftmann@51487
    49
haftmann@51487
    50
lemma strict_trans1:
haftmann@63290
    51
  "a \<^bold>\<le> b \<Longrightarrow> b \<^bold>< c \<Longrightarrow> a \<^bold>< c"
haftmann@51487
    52
  by (auto simp add: strict_iff_order intro: trans antisym)
haftmann@51487
    53
haftmann@51487
    54
lemma strict_trans2:
haftmann@63290
    55
  "a \<^bold>< b \<Longrightarrow> b \<^bold>\<le> c \<Longrightarrow> a \<^bold>< c"
haftmann@51487
    56
  by (auto simp add: strict_iff_order intro: trans antisym)
haftmann@51487
    57
haftmann@51487
    58
lemma strict_trans:
haftmann@63290
    59
  "a \<^bold>< b \<Longrightarrow> b \<^bold>< c \<Longrightarrow> a \<^bold>< c"
haftmann@51487
    60
  by (auto intro: strict_trans1 strict_implies_order)
haftmann@51487
    61
haftmann@51487
    62
end
haftmann@51487
    63
haftmann@63819
    64
text \<open>Alternative introduction rule with bias towards strict order\<close>
haftmann@63819
    65
haftmann@63819
    66
lemma ordering_strictI:
haftmann@63819
    67
  fixes less_eq (infix "\<^bold>\<le>" 50)
haftmann@63819
    68
    and less (infix "\<^bold><" 50)
haftmann@63819
    69
  assumes less_eq_less: "\<And>a b. a \<^bold>\<le> b \<longleftrightarrow> a \<^bold>< b \<or> a = b"
haftmann@63819
    70
    assumes asym: "\<And>a b. a \<^bold>< b \<Longrightarrow> \<not> b \<^bold>< a"
haftmann@63819
    71
  assumes irrefl: "\<And>a. \<not> a \<^bold>< a"
haftmann@63819
    72
  assumes trans: "\<And>a b c. a \<^bold>< b \<Longrightarrow> b \<^bold>< c \<Longrightarrow> a \<^bold>< c"
haftmann@63819
    73
  shows "ordering less_eq less"
haftmann@63819
    74
proof
haftmann@63819
    75
  fix a b
haftmann@63819
    76
  show "a \<^bold>< b \<longleftrightarrow> a \<^bold>\<le> b \<and> a \<noteq> b"
haftmann@63819
    77
    by (auto simp add: less_eq_less asym irrefl)
haftmann@63819
    78
next
haftmann@63819
    79
  fix a
haftmann@63819
    80
  show "a \<^bold>\<le> a"
haftmann@63819
    81
    by (auto simp add: less_eq_less)
haftmann@63819
    82
next
haftmann@63819
    83
  fix a b c
haftmann@63819
    84
  assume "a \<^bold>\<le> b" and "b \<^bold>\<le> c" then show "a \<^bold>\<le> c"
haftmann@63819
    85
    by (auto simp add: less_eq_less intro: trans)
haftmann@63819
    86
next
haftmann@63819
    87
  fix a b
haftmann@63819
    88
  assume "a \<^bold>\<le> b" and "b \<^bold>\<le> a" then show "a = b"
haftmann@63819
    89
    by (auto simp add: less_eq_less asym)
haftmann@63819
    90
qed
haftmann@63819
    91
haftmann@63819
    92
lemma ordering_dualI:
haftmann@63819
    93
  fixes less_eq (infix "\<^bold>\<le>" 50)
haftmann@63819
    94
    and less (infix "\<^bold><" 50)
haftmann@63819
    95
  assumes "ordering (\<lambda>a b. b \<^bold>\<le> a) (\<lambda>a b. b \<^bold>< a)"
haftmann@63819
    96
  shows "ordering less_eq less"
haftmann@63819
    97
proof -
haftmann@63819
    98
  from assms interpret ordering "\<lambda>a b. b \<^bold>\<le> a" "\<lambda>a b. b \<^bold>< a" .
haftmann@63819
    99
  show ?thesis
haftmann@63819
   100
    by standard (auto simp: strict_iff_order refl intro: antisym trans)
haftmann@63819
   101
qed
haftmann@63819
   102
haftmann@51487
   103
locale ordering_top = ordering +
haftmann@63290
   104
  fixes top :: "'a"  ("\<^bold>\<top>")
haftmann@63290
   105
  assumes extremum [simp]: "a \<^bold>\<le> \<^bold>\<top>"
haftmann@51487
   106
begin
haftmann@51487
   107
haftmann@51487
   108
lemma extremum_uniqueI:
haftmann@63290
   109
  "\<^bold>\<top> \<^bold>\<le> a \<Longrightarrow> a = \<^bold>\<top>"
haftmann@51487
   110
  by (rule antisym) auto
haftmann@51487
   111
haftmann@51487
   112
lemma extremum_unique:
haftmann@63290
   113
  "\<^bold>\<top> \<^bold>\<le> a \<longleftrightarrow> a = \<^bold>\<top>"
haftmann@51487
   114
  by (auto intro: antisym)
haftmann@51487
   115
haftmann@51487
   116
lemma extremum_strict [simp]:
haftmann@63290
   117
  "\<not> (\<^bold>\<top> \<^bold>< a)"
haftmann@51487
   118
  using extremum [of a] by (auto simp add: order_iff_strict intro: asym irrefl)
haftmann@51487
   119
haftmann@51487
   120
lemma not_eq_extremum:
haftmann@63290
   121
  "a \<noteq> \<^bold>\<top> \<longleftrightarrow> a \<^bold>< \<^bold>\<top>"
haftmann@51487
   122
  by (auto simp add: order_iff_strict intro: not_eq_order_implies_strict extremum)
haftmann@51487
   123
lp15@61824
   124
end
haftmann@51487
   125
haftmann@51487
   126
wenzelm@60758
   127
subsection \<open>Syntactic orders\<close>
haftmann@35092
   128
haftmann@35092
   129
class ord =
haftmann@35092
   130
  fixes less_eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
haftmann@35092
   131
    and less :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
haftmann@35092
   132
begin
haftmann@35092
   133
haftmann@35092
   134
notation
wenzelm@61955
   135
  less_eq  ("op \<le>") and
wenzelm@61955
   136
  less_eq  ("(_/ \<le> _)"  [51, 51] 50) and
haftmann@35092
   137
  less  ("op <") and
haftmann@35092
   138
  less  ("(_/ < _)"  [51, 51] 50)
lp15@61824
   139
wenzelm@61955
   140
abbreviation (input)
wenzelm@61955
   141
  greater_eq  (infix "\<ge>" 50)
wenzelm@61955
   142
  where "x \<ge> y \<equiv> y \<le> x"
haftmann@35092
   143
haftmann@35092
   144
abbreviation (input)
wenzelm@61955
   145
  greater  (infix ">" 50)
wenzelm@61955
   146
  where "x > y \<equiv> y < x"
wenzelm@61955
   147
wenzelm@61955
   148
notation (ASCII)
wenzelm@61955
   149
  less_eq  ("op <=") and
wenzelm@61955
   150
  less_eq  ("(_/ <= _)" [51, 51] 50)
haftmann@35092
   151
haftmann@35092
   152
notation (input)
wenzelm@61955
   153
  greater_eq  (infix ">=" 50)
haftmann@35092
   154
haftmann@35092
   155
end
haftmann@35092
   156
haftmann@35092
   157
wenzelm@60758
   158
subsection \<open>Quasi orders\<close>
nipkow@15524
   159
haftmann@27682
   160
class preorder = ord +
haftmann@27682
   161
  assumes less_le_not_le: "x < y \<longleftrightarrow> x \<le> y \<and> \<not> (y \<le> x)"
haftmann@25062
   162
  and order_refl [iff]: "x \<le> x"
haftmann@25062
   163
  and order_trans: "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
haftmann@21248
   164
begin
haftmann@21248
   165
wenzelm@60758
   166
text \<open>Reflexivity.\<close>
nipkow@15524
   167
haftmann@25062
   168
lemma eq_refl: "x = y \<Longrightarrow> x \<le> y"
wenzelm@61799
   169
    \<comment> \<open>This form is useful with the classical reasoner.\<close>
nipkow@23212
   170
by (erule ssubst) (rule order_refl)
nipkow@15524
   171
haftmann@25062
   172
lemma less_irrefl [iff]: "\<not> x < x"
haftmann@27682
   173
by (simp add: less_le_not_le)
haftmann@27682
   174
haftmann@27682
   175
lemma less_imp_le: "x < y \<Longrightarrow> x \<le> y"
wenzelm@63172
   176
by (simp add: less_le_not_le)
haftmann@27682
   177
haftmann@27682
   178
wenzelm@60758
   179
text \<open>Asymmetry.\<close>
haftmann@27682
   180
haftmann@27682
   181
lemma less_not_sym: "x < y \<Longrightarrow> \<not> (y < x)"
haftmann@27682
   182
by (simp add: less_le_not_le)
haftmann@27682
   183
haftmann@27682
   184
lemma less_asym: "x < y \<Longrightarrow> (\<not> P \<Longrightarrow> y < x) \<Longrightarrow> P"
haftmann@27682
   185
by (drule less_not_sym, erule contrapos_np) simp
haftmann@27682
   186
haftmann@27682
   187
wenzelm@60758
   188
text \<open>Transitivity.\<close>
haftmann@27682
   189
haftmann@27682
   190
lemma less_trans: "x < y \<Longrightarrow> y < z \<Longrightarrow> x < z"
lp15@61824
   191
by (auto simp add: less_le_not_le intro: order_trans)
haftmann@27682
   192
haftmann@27682
   193
lemma le_less_trans: "x \<le> y \<Longrightarrow> y < z \<Longrightarrow> x < z"
lp15@61824
   194
by (auto simp add: less_le_not_le intro: order_trans)
haftmann@27682
   195
haftmann@27682
   196
lemma less_le_trans: "x < y \<Longrightarrow> y \<le> z \<Longrightarrow> x < z"
lp15@61824
   197
by (auto simp add: less_le_not_le intro: order_trans)
haftmann@27682
   198
haftmann@27682
   199
wenzelm@60758
   200
text \<open>Useful for simplification, but too risky to include by default.\<close>
haftmann@27682
   201
haftmann@27682
   202
lemma less_imp_not_less: "x < y \<Longrightarrow> (\<not> y < x) \<longleftrightarrow> True"
haftmann@27682
   203
by (blast elim: less_asym)
haftmann@27682
   204
haftmann@27682
   205
lemma less_imp_triv: "x < y \<Longrightarrow> (y < x \<longrightarrow> P) \<longleftrightarrow> True"
haftmann@27682
   206
by (blast elim: less_asym)
haftmann@27682
   207
haftmann@27682
   208
wenzelm@60758
   209
text \<open>Transitivity rules for calculational reasoning\<close>
haftmann@27682
   210
haftmann@27682
   211
lemma less_asym': "a < b \<Longrightarrow> b < a \<Longrightarrow> P"
haftmann@27682
   212
by (rule less_asym)
haftmann@27682
   213
haftmann@27682
   214
wenzelm@60758
   215
text \<open>Dual order\<close>
haftmann@27682
   216
haftmann@27682
   217
lemma dual_preorder:
haftmann@36635
   218
  "class.preorder (op \<ge>) (op >)"
haftmann@63819
   219
  by standard (auto simp add: less_le_not_le intro: order_trans)
haftmann@27682
   220
haftmann@27682
   221
end
haftmann@27682
   222
haftmann@27682
   223
wenzelm@60758
   224
subsection \<open>Partial orders\<close>
haftmann@27682
   225
haftmann@27682
   226
class order = preorder +
haftmann@27682
   227
  assumes antisym: "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y"
haftmann@27682
   228
begin
haftmann@27682
   229
haftmann@51487
   230
lemma less_le: "x < y \<longleftrightarrow> x \<le> y \<and> x \<noteq> y"
haftmann@51487
   231
  by (auto simp add: less_le_not_le intro: antisym)
haftmann@51487
   232
haftmann@63819
   233
sublocale order: ordering less_eq less + dual_order: ordering greater_eq greater
haftmann@63819
   234
proof -
haftmann@63819
   235
  interpret ordering less_eq less
haftmann@63819
   236
    by standard (auto intro: antisym order_trans simp add: less_le)
haftmann@63819
   237
  show "ordering less_eq less"
haftmann@63819
   238
    by (fact ordering_axioms)
haftmann@63819
   239
  then show "ordering greater_eq greater"
haftmann@63819
   240
    by (rule ordering_dualI)
haftmann@63819
   241
qed
haftmann@51487
   242
wenzelm@60758
   243
text \<open>Reflexivity.\<close>
nipkow@15524
   244
haftmann@25062
   245
lemma le_less: "x \<le> y \<longleftrightarrow> x < y \<or> x = y"
wenzelm@61799
   246
    \<comment> \<open>NOT suitable for iff, since it can cause PROOF FAILED.\<close>
haftmann@51546
   247
by (fact order.order_iff_strict)
nipkow@15524
   248
haftmann@25062
   249
lemma le_imp_less_or_eq: "x \<le> y \<Longrightarrow> x < y \<or> x = y"
wenzelm@63172
   250
by (simp add: less_le)
nipkow@15524
   251
haftmann@21329
   252
wenzelm@60758
   253
text \<open>Useful for simplification, but too risky to include by default.\<close>
haftmann@21329
   254
haftmann@25062
   255
lemma less_imp_not_eq: "x < y \<Longrightarrow> (x = y) \<longleftrightarrow> False"
nipkow@23212
   256
by auto
haftmann@21329
   257
haftmann@25062
   258
lemma less_imp_not_eq2: "x < y \<Longrightarrow> (y = x) \<longleftrightarrow> False"
nipkow@23212
   259
by auto
haftmann@21329
   260
haftmann@21329
   261
wenzelm@60758
   262
text \<open>Transitivity rules for calculational reasoning\<close>
haftmann@21329
   263
haftmann@25062
   264
lemma neq_le_trans: "a \<noteq> b \<Longrightarrow> a \<le> b \<Longrightarrow> a < b"
haftmann@51546
   265
by (fact order.not_eq_order_implies_strict)
haftmann@21329
   266
haftmann@25062
   267
lemma le_neq_trans: "a \<le> b \<Longrightarrow> a \<noteq> b \<Longrightarrow> a < b"
haftmann@51546
   268
by (rule order.not_eq_order_implies_strict)
haftmann@21329
   269
nipkow@15524
   270
wenzelm@60758
   271
text \<open>Asymmetry.\<close>
nipkow@15524
   272
haftmann@25062
   273
lemma eq_iff: "x = y \<longleftrightarrow> x \<le> y \<and> y \<le> x"
nipkow@23212
   274
by (blast intro: antisym)
nipkow@15524
   275
haftmann@25062
   276
lemma antisym_conv: "y \<le> x \<Longrightarrow> x \<le> y \<longleftrightarrow> x = y"
nipkow@23212
   277
by (blast intro: antisym)
nipkow@15524
   278
haftmann@25062
   279
lemma less_imp_neq: "x < y \<Longrightarrow> x \<noteq> y"
haftmann@51546
   280
by (fact order.strict_implies_not_eq)
haftmann@21248
   281
haftmann@21083
   282
wenzelm@60758
   283
text \<open>Least value operator\<close>
haftmann@27107
   284
haftmann@27299
   285
definition (in ord)
haftmann@27107
   286
  Least :: "('a \<Rightarrow> bool) \<Rightarrow> 'a" (binder "LEAST " 10) where
haftmann@27107
   287
  "Least P = (THE x. P x \<and> (\<forall>y. P y \<longrightarrow> x \<le> y))"
haftmann@27107
   288
haftmann@27107
   289
lemma Least_equality:
haftmann@27107
   290
  assumes "P x"
haftmann@27107
   291
    and "\<And>y. P y \<Longrightarrow> x \<le> y"
haftmann@27107
   292
  shows "Least P = x"
haftmann@27107
   293
unfolding Least_def by (rule the_equality)
haftmann@27107
   294
  (blast intro: assms antisym)+
haftmann@27107
   295
haftmann@27107
   296
lemma LeastI2_order:
haftmann@27107
   297
  assumes "P x"
haftmann@27107
   298
    and "\<And>y. P y \<Longrightarrow> x \<le> y"
haftmann@27107
   299
    and "\<And>x. P x \<Longrightarrow> \<forall>y. P y \<longrightarrow> x \<le> y \<Longrightarrow> Q x"
haftmann@27107
   300
  shows "Q (Least P)"
haftmann@27107
   301
unfolding Least_def by (rule theI2)
haftmann@27107
   302
  (blast intro: assms antisym)+
haftmann@27107
   303
haftmann@21248
   304
end
nipkow@15524
   305
haftmann@63819
   306
lemma ordering_orderI:
haftmann@63819
   307
  fixes less_eq (infix "\<^bold>\<le>" 50)
haftmann@63819
   308
    and less (infix "\<^bold><" 50)
haftmann@63819
   309
  assumes "ordering less_eq less"
haftmann@63819
   310
  shows "class.order less_eq less"
haftmann@63819
   311
proof -
haftmann@63819
   312
  from assms interpret ordering less_eq less .
haftmann@63819
   313
  show ?thesis
haftmann@63819
   314
    by standard (auto intro: antisym trans simp add: refl strict_iff_order)
haftmann@63819
   315
qed
haftmann@56545
   316
haftmann@56545
   317
lemma order_strictI:
haftmann@56545
   318
  fixes less (infix "\<sqsubset>" 50)
haftmann@56545
   319
    and less_eq (infix "\<sqsubseteq>" 50)
haftmann@63819
   320
  assumes "\<And>a b. a \<sqsubseteq> b \<longleftrightarrow> a \<sqsubset> b \<or> a = b"
haftmann@63819
   321
    assumes "\<And>a b. a \<sqsubset> b \<Longrightarrow> \<not> b \<sqsubset> a"
haftmann@63819
   322
  assumes "\<And>a. \<not> a \<sqsubset> a"
haftmann@63819
   323
  assumes "\<And>a b c. a \<sqsubset> b \<Longrightarrow> b \<sqsubset> c \<Longrightarrow> a \<sqsubset> c"
haftmann@56545
   324
  shows "class.order less_eq less"
haftmann@63819
   325
  by (rule ordering_orderI) (rule ordering_strictI, (fact assms)+)
haftmann@63819
   326
haftmann@63819
   327
context order
haftmann@63819
   328
begin
haftmann@63819
   329
haftmann@63819
   330
text \<open>Dual order\<close>
haftmann@63819
   331
haftmann@63819
   332
lemma dual_order:
haftmann@63819
   333
  "class.order (op \<ge>) (op >)"
haftmann@63819
   334
  using dual_order.ordering_axioms by (rule ordering_orderI)
haftmann@63819
   335
haftmann@63819
   336
end
haftmann@56545
   337
haftmann@56545
   338
wenzelm@60758
   339
subsection \<open>Linear (total) orders\<close>
haftmann@21329
   340
haftmann@22316
   341
class linorder = order +
haftmann@25207
   342
  assumes linear: "x \<le> y \<or> y \<le> x"
haftmann@21248
   343
begin
haftmann@21248
   344
haftmann@25062
   345
lemma less_linear: "x < y \<or> x = y \<or> y < x"
nipkow@23212
   346
unfolding less_le using less_le linear by blast
haftmann@21248
   347
haftmann@25062
   348
lemma le_less_linear: "x \<le> y \<or> y < x"
nipkow@23212
   349
by (simp add: le_less less_linear)
haftmann@21248
   350
haftmann@21248
   351
lemma le_cases [case_names le ge]:
haftmann@25062
   352
  "(x \<le> y \<Longrightarrow> P) \<Longrightarrow> (y \<le> x \<Longrightarrow> P) \<Longrightarrow> P"
nipkow@23212
   353
using linear by blast
haftmann@21248
   354
lp15@61762
   355
lemma (in linorder) le_cases3:
lp15@61762
   356
  "\<lbrakk>\<lbrakk>x \<le> y; y \<le> z\<rbrakk> \<Longrightarrow> P; \<lbrakk>y \<le> x; x \<le> z\<rbrakk> \<Longrightarrow> P; \<lbrakk>x \<le> z; z \<le> y\<rbrakk> \<Longrightarrow> P;
lp15@61762
   357
    \<lbrakk>z \<le> y; y \<le> x\<rbrakk> \<Longrightarrow> P; \<lbrakk>y \<le> z; z \<le> x\<rbrakk> \<Longrightarrow> P; \<lbrakk>z \<le> x; x \<le> y\<rbrakk> \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P"
lp15@61762
   358
by (blast intro: le_cases)
lp15@61762
   359
haftmann@22384
   360
lemma linorder_cases [case_names less equal greater]:
haftmann@25062
   361
  "(x < y \<Longrightarrow> P) \<Longrightarrow> (x = y \<Longrightarrow> P) \<Longrightarrow> (y < x \<Longrightarrow> P) \<Longrightarrow> P"
nipkow@23212
   362
using less_linear by blast
haftmann@21248
   363
hoelzl@57447
   364
lemma linorder_wlog[case_names le sym]:
hoelzl@57447
   365
  "(\<And>a b. a \<le> b \<Longrightarrow> P a b) \<Longrightarrow> (\<And>a b. P b a \<Longrightarrow> P a b) \<Longrightarrow> P a b"
hoelzl@57447
   366
  by (cases rule: le_cases[of a b]) blast+
hoelzl@57447
   367
haftmann@25062
   368
lemma not_less: "\<not> x < y \<longleftrightarrow> y \<le> x"
nipkow@23212
   369
apply (simp add: less_le)
nipkow@23212
   370
using linear apply (blast intro: antisym)
nipkow@23212
   371
done
nipkow@23212
   372
nipkow@23212
   373
lemma not_less_iff_gr_or_eq:
haftmann@25062
   374
 "\<not>(x < y) \<longleftrightarrow> (x > y | x = y)"
nipkow@23212
   375
apply(simp add:not_less le_less)
nipkow@23212
   376
apply blast
nipkow@23212
   377
done
nipkow@15524
   378
haftmann@25062
   379
lemma not_le: "\<not> x \<le> y \<longleftrightarrow> y < x"
nipkow@23212
   380
apply (simp add: less_le)
nipkow@23212
   381
using linear apply (blast intro: antisym)
nipkow@23212
   382
done
nipkow@15524
   383
haftmann@25062
   384
lemma neq_iff: "x \<noteq> y \<longleftrightarrow> x < y \<or> y < x"
nipkow@23212
   385
by (cut_tac x = x and y = y in less_linear, auto)
nipkow@15524
   386
haftmann@25062
   387
lemma neqE: "x \<noteq> y \<Longrightarrow> (x < y \<Longrightarrow> R) \<Longrightarrow> (y < x \<Longrightarrow> R) \<Longrightarrow> R"
nipkow@23212
   388
by (simp add: neq_iff) blast
nipkow@15524
   389
haftmann@25062
   390
lemma antisym_conv1: "\<not> x < y \<Longrightarrow> x \<le> y \<longleftrightarrow> x = y"
nipkow@23212
   391
by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   392
haftmann@25062
   393
lemma antisym_conv2: "x \<le> y \<Longrightarrow> \<not> x < y \<longleftrightarrow> x = y"
nipkow@23212
   394
by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   395
haftmann@25062
   396
lemma antisym_conv3: "\<not> y < x \<Longrightarrow> \<not> x < y \<longleftrightarrow> x = y"
nipkow@23212
   397
by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   398
haftmann@25062
   399
lemma leI: "\<not> x < y \<Longrightarrow> y \<le> x"
nipkow@23212
   400
unfolding not_less .
paulson@16796
   401
haftmann@25062
   402
lemma leD: "y \<le> x \<Longrightarrow> \<not> x < y"
nipkow@23212
   403
unfolding not_less .
paulson@16796
   404
lp15@61824
   405
lemma not_le_imp_less: "\<not> y \<le> x \<Longrightarrow> x < y"
nipkow@23212
   406
unfolding not_le .
haftmann@21248
   407
lp15@64758
   408
lemma linorder_less_wlog[case_names less refl sym]:
lp15@64758
   409
     "\<lbrakk>\<And>a b. a < b \<Longrightarrow> P a b;  \<And>a. P a a;  \<And>a b. P b a \<Longrightarrow> P a b\<rbrakk> \<Longrightarrow> P a b"
lp15@64758
   410
  using antisym_conv3 by blast
lp15@64758
   411
wenzelm@60758
   412
text \<open>Dual order\<close>
haftmann@22916
   413
haftmann@26014
   414
lemma dual_linorder:
haftmann@36635
   415
  "class.linorder (op \<ge>) (op >)"
haftmann@36635
   416
by (rule class.linorder.intro, rule dual_order) (unfold_locales, rule linear)
haftmann@22916
   417
haftmann@21248
   418
end
haftmann@21248
   419
haftmann@23948
   420
wenzelm@60758
   421
text \<open>Alternative introduction rule with bias towards strict order\<close>
haftmann@56545
   422
haftmann@56545
   423
lemma linorder_strictI:
haftmann@63819
   424
  fixes less_eq (infix "\<^bold>\<le>" 50)
haftmann@63819
   425
    and less (infix "\<^bold><" 50)
haftmann@56545
   426
  assumes "class.order less_eq less"
haftmann@63819
   427
  assumes trichotomy: "\<And>a b. a \<^bold>< b \<or> a = b \<or> b \<^bold>< a"
haftmann@56545
   428
  shows "class.linorder less_eq less"
haftmann@56545
   429
proof -
haftmann@56545
   430
  interpret order less_eq less
wenzelm@60758
   431
    by (fact \<open>class.order less_eq less\<close>)
haftmann@56545
   432
  show ?thesis
haftmann@56545
   433
  proof
haftmann@56545
   434
    fix a b
haftmann@63819
   435
    show "a \<^bold>\<le> b \<or> b \<^bold>\<le> a"
haftmann@56545
   436
      using trichotomy by (auto simp add: le_less)
haftmann@56545
   437
  qed
haftmann@56545
   438
qed
haftmann@56545
   439
haftmann@56545
   440
wenzelm@60758
   441
subsection \<open>Reasoning tools setup\<close>
haftmann@21083
   442
wenzelm@60758
   443
ML \<open>
ballarin@24641
   444
signature ORDERS =
ballarin@24641
   445
sig
ballarin@24641
   446
  val print_structures: Proof.context -> unit
wenzelm@32215
   447
  val order_tac: Proof.context -> thm list -> int -> tactic
wenzelm@58826
   448
  val add_struct: string * term list -> string -> attribute
wenzelm@58826
   449
  val del_struct: string * term list -> attribute
ballarin@24641
   450
end;
haftmann@21091
   451
ballarin@24641
   452
structure Orders: ORDERS =
haftmann@21248
   453
struct
ballarin@24641
   454
wenzelm@56508
   455
(* context data *)
ballarin@24641
   456
ballarin@24641
   457
fun struct_eq ((s1: string, ts1), (s2, ts2)) =
wenzelm@56508
   458
  s1 = s2 andalso eq_list (op aconv) (ts1, ts2);
ballarin@24641
   459
wenzelm@33519
   460
structure Data = Generic_Data
ballarin@24641
   461
(
ballarin@24641
   462
  type T = ((string * term list) * Order_Tac.less_arith) list;
ballarin@24641
   463
    (* Order structures:
ballarin@24641
   464
       identifier of the structure, list of operations and record of theorems
ballarin@24641
   465
       needed to set up the transitivity reasoner,
ballarin@24641
   466
       identifier and operations identify the structure uniquely. *)
ballarin@24641
   467
  val empty = [];
ballarin@24641
   468
  val extend = I;
wenzelm@33519
   469
  fun merge data = AList.join struct_eq (K fst) data;
ballarin@24641
   470
);
ballarin@24641
   471
ballarin@24641
   472
fun print_structures ctxt =
ballarin@24641
   473
  let
ballarin@24641
   474
    val structs = Data.get (Context.Proof ctxt);
ballarin@24641
   475
    fun pretty_term t = Pretty.block
wenzelm@24920
   476
      [Pretty.quote (Syntax.pretty_term ctxt t), Pretty.brk 1,
ballarin@24641
   477
        Pretty.str "::", Pretty.brk 1,
wenzelm@24920
   478
        Pretty.quote (Syntax.pretty_typ ctxt (type_of t))];
ballarin@24641
   479
    fun pretty_struct ((s, ts), _) = Pretty.block
ballarin@24641
   480
      [Pretty.str s, Pretty.str ":", Pretty.brk 1,
ballarin@24641
   481
       Pretty.enclose "(" ")" (Pretty.breaks (map pretty_term ts))];
ballarin@24641
   482
  in
wenzelm@51579
   483
    Pretty.writeln (Pretty.big_list "order structures:" (map pretty_struct structs))
ballarin@24641
   484
  end;
ballarin@24641
   485
wenzelm@56508
   486
val _ =
wenzelm@59936
   487
  Outer_Syntax.command @{command_keyword print_orders}
wenzelm@56508
   488
    "print order structures available to transitivity reasoner"
wenzelm@60097
   489
    (Scan.succeed (Toplevel.keep (print_structures o Toplevel.context_of)));
haftmann@21091
   490
wenzelm@56508
   491
wenzelm@56508
   492
(* tactics *)
wenzelm@56508
   493
wenzelm@56508
   494
fun struct_tac ((s, ops), thms) ctxt facts =
ballarin@24641
   495
  let
wenzelm@56508
   496
    val [eq, le, less] = ops;
berghofe@30107
   497
    fun decomp thy (@{const Trueprop} $ t) =
wenzelm@56508
   498
          let
wenzelm@56508
   499
            fun excluded t =
wenzelm@56508
   500
              (* exclude numeric types: linear arithmetic subsumes transitivity *)
wenzelm@56508
   501
              let val T = type_of t
wenzelm@56508
   502
              in
wenzelm@56508
   503
                T = HOLogic.natT orelse T = HOLogic.intT orelse T = HOLogic.realT
wenzelm@56508
   504
              end;
wenzelm@56508
   505
            fun rel (bin_op $ t1 $ t2) =
wenzelm@56508
   506
                  if excluded t1 then NONE
wenzelm@56508
   507
                  else if Pattern.matches thy (eq, bin_op) then SOME (t1, "=", t2)
wenzelm@56508
   508
                  else if Pattern.matches thy (le, bin_op) then SOME (t1, "<=", t2)
wenzelm@56508
   509
                  else if Pattern.matches thy (less, bin_op) then SOME (t1, "<", t2)
wenzelm@56508
   510
                  else NONE
wenzelm@56508
   511
              | rel _ = NONE;
wenzelm@56508
   512
            fun dec (Const (@{const_name Not}, _) $ t) =
wenzelm@56508
   513
                  (case rel t of NONE =>
wenzelm@56508
   514
                    NONE
wenzelm@56508
   515
                  | SOME (t1, rel, t2) => SOME (t1, "~" ^ rel, t2))
wenzelm@56508
   516
              | dec x = rel x;
wenzelm@56508
   517
          in dec t end
wenzelm@56508
   518
      | decomp _ _ = NONE;
ballarin@24641
   519
  in
wenzelm@56508
   520
    (case s of
wenzelm@56508
   521
      "order" => Order_Tac.partial_tac decomp thms ctxt facts
wenzelm@56508
   522
    | "linorder" => Order_Tac.linear_tac decomp thms ctxt facts
wenzelm@56508
   523
    | _ => error ("Unknown order kind " ^ quote s ^ " encountered in transitivity reasoner"))
ballarin@24641
   524
  end
ballarin@24641
   525
wenzelm@56508
   526
fun order_tac ctxt facts =
wenzelm@56508
   527
  FIRST' (map (fn s => CHANGED o struct_tac s ctxt facts) (Data.get (Context.Proof ctxt)));
ballarin@24641
   528
ballarin@24641
   529
wenzelm@56508
   530
(* attributes *)
ballarin@24641
   531
wenzelm@58826
   532
fun add_struct s tag =
ballarin@24641
   533
  Thm.declaration_attribute
ballarin@24641
   534
    (fn thm => Data.map (AList.map_default struct_eq (s, Order_Tac.empty TrueI) (Order_Tac.update tag thm)));
ballarin@24641
   535
fun del_struct s =
ballarin@24641
   536
  Thm.declaration_attribute
ballarin@24641
   537
    (fn _ => Data.map (AList.delete struct_eq s));
ballarin@24641
   538
haftmann@21091
   539
end;
wenzelm@60758
   540
\<close>
haftmann@21091
   541
wenzelm@60758
   542
attribute_setup order = \<open>
wenzelm@58826
   543
  Scan.lift ((Args.add -- Args.name >> (fn (_, s) => SOME s) || Args.del >> K NONE) --|
wenzelm@58826
   544
    Args.colon (* FIXME || Scan.succeed true *) ) -- Scan.lift Args.name --
wenzelm@58826
   545
    Scan.repeat Args.term
wenzelm@58826
   546
    >> (fn ((SOME tag, n), ts) => Orders.add_struct (n, ts) tag
wenzelm@58826
   547
         | ((NONE, n), ts) => Orders.del_struct (n, ts))
wenzelm@60758
   548
\<close> "theorems controlling transitivity reasoner"
wenzelm@58826
   549
wenzelm@60758
   550
method_setup order = \<open>
wenzelm@47432
   551
  Scan.succeed (fn ctxt => SIMPLE_METHOD' (Orders.order_tac ctxt []))
wenzelm@60758
   552
\<close> "transitivity reasoner"
ballarin@24641
   553
ballarin@24641
   554
wenzelm@60758
   555
text \<open>Declarations to set up transitivity reasoner of partial and linear orders.\<close>
ballarin@24641
   556
haftmann@25076
   557
context order
haftmann@25076
   558
begin
haftmann@25076
   559
ballarin@24641
   560
(* The type constraint on @{term op =} below is necessary since the operation
ballarin@24641
   561
   is not a parameter of the locale. *)
haftmann@25076
   562
haftmann@27689
   563
declare less_irrefl [THEN notE, order add less_reflE: order "op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool" "op <=" "op <"]
lp15@61824
   564
haftmann@27689
   565
declare order_refl  [order add le_refl: order "op = :: 'a => 'a => bool" "op <=" "op <"]
lp15@61824
   566
haftmann@27689
   567
declare less_imp_le [order add less_imp_le: order "op = :: 'a => 'a => bool" "op <=" "op <"]
lp15@61824
   568
haftmann@27689
   569
declare antisym [order add eqI: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   570
haftmann@27689
   571
declare eq_refl [order add eqD1: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   572
haftmann@27689
   573
declare sym [THEN eq_refl, order add eqD2: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   574
haftmann@27689
   575
declare less_trans [order add less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
lp15@61824
   576
haftmann@27689
   577
declare less_le_trans [order add less_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
lp15@61824
   578
haftmann@27689
   579
declare le_less_trans [order add le_less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   580
haftmann@27689
   581
declare order_trans [order add le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   582
haftmann@27689
   583
declare le_neq_trans [order add le_neq_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   584
haftmann@27689
   585
declare neq_le_trans [order add neq_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   586
haftmann@27689
   587
declare less_imp_neq [order add less_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   588
haftmann@27689
   589
declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   590
haftmann@27689
   591
declare not_sym [order add not_sym: order "op = :: 'a => 'a => bool" "op <=" "op <"]
ballarin@24641
   592
haftmann@25076
   593
end
haftmann@25076
   594
haftmann@25076
   595
context linorder
haftmann@25076
   596
begin
ballarin@24641
   597
haftmann@27689
   598
declare [[order del: order "op = :: 'a => 'a => bool" "op <=" "op <"]]
haftmann@27689
   599
haftmann@27689
   600
declare less_irrefl [THEN notE, order add less_reflE: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   601
haftmann@27689
   602
declare order_refl [order add le_refl: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   603
haftmann@27689
   604
declare less_imp_le [order add less_imp_le: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   605
haftmann@27689
   606
declare not_less [THEN iffD2, order add not_lessI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   607
haftmann@27689
   608
declare not_le [THEN iffD2, order add not_leI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   609
haftmann@27689
   610
declare not_less [THEN iffD1, order add not_lessD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   611
haftmann@27689
   612
declare not_le [THEN iffD1, order add not_leD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   613
haftmann@27689
   614
declare antisym [order add eqI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   615
haftmann@27689
   616
declare eq_refl [order add eqD1: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@25076
   617
haftmann@27689
   618
declare sym [THEN eq_refl, order add eqD2: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   619
haftmann@27689
   620
declare less_trans [order add less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   621
haftmann@27689
   622
declare less_le_trans [order add less_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   623
haftmann@27689
   624
declare le_less_trans [order add le_less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   625
haftmann@27689
   626
declare order_trans [order add le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   627
haftmann@27689
   628
declare le_neq_trans [order add le_neq_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   629
haftmann@27689
   630
declare neq_le_trans [order add neq_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   631
haftmann@27689
   632
declare less_imp_neq [order add less_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   633
haftmann@27689
   634
declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   635
haftmann@27689
   636
declare not_sym [order add not_sym: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
ballarin@24641
   637
haftmann@25076
   638
end
haftmann@25076
   639
wenzelm@60758
   640
setup \<open>
wenzelm@56509
   641
  map_theory_simpset (fn ctxt0 => ctxt0 addSolver
wenzelm@56509
   642
    mk_solver "Transitivity" (fn ctxt => Orders.order_tac ctxt (Simplifier.prems_of ctxt)))
wenzelm@56509
   643
  (*Adding the transitivity reasoners also as safe solvers showed a slight
wenzelm@56509
   644
    speed up, but the reasoning strength appears to be not higher (at least
wenzelm@56509
   645
    no breaking of additional proofs in the entire HOL distribution, as
wenzelm@56509
   646
    of 5 March 2004, was observed).*)
wenzelm@60758
   647
\<close>
nipkow@15524
   648
wenzelm@60758
   649
ML \<open>
wenzelm@56509
   650
local
wenzelm@56509
   651
  fun prp t thm = Thm.prop_of thm = t;  (* FIXME proper aconv!? *)
wenzelm@56509
   652
in
nipkow@15524
   653
wenzelm@56509
   654
fun antisym_le_simproc ctxt ct =
wenzelm@59582
   655
  (case Thm.term_of ct of
wenzelm@56509
   656
    (le as Const (_, T)) $ r $ s =>
wenzelm@56509
   657
     (let
wenzelm@56509
   658
        val prems = Simplifier.prems_of ctxt;
wenzelm@56509
   659
        val less = Const (@{const_name less}, T);
wenzelm@56509
   660
        val t = HOLogic.mk_Trueprop(le $ s $ r);
wenzelm@56509
   661
      in
wenzelm@56509
   662
        (case find_first (prp t) prems of
wenzelm@56509
   663
          NONE =>
wenzelm@56509
   664
            let val t = HOLogic.mk_Trueprop(HOLogic.Not $ (less $ r $ s)) in
wenzelm@56509
   665
              (case find_first (prp t) prems of
wenzelm@56509
   666
                NONE => NONE
wenzelm@56509
   667
              | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv1})))
wenzelm@56509
   668
             end
wenzelm@56509
   669
         | SOME thm => SOME (mk_meta_eq (thm RS @{thm order_class.antisym_conv})))
wenzelm@56509
   670
      end handle THM _ => NONE)
wenzelm@56509
   671
  | _ => NONE);
nipkow@15524
   672
wenzelm@56509
   673
fun antisym_less_simproc ctxt ct =
wenzelm@59582
   674
  (case Thm.term_of ct of
wenzelm@56509
   675
    NotC $ ((less as Const(_,T)) $ r $ s) =>
wenzelm@56509
   676
     (let
wenzelm@56509
   677
       val prems = Simplifier.prems_of ctxt;
wenzelm@56509
   678
       val le = Const (@{const_name less_eq}, T);
wenzelm@56509
   679
       val t = HOLogic.mk_Trueprop(le $ r $ s);
wenzelm@56509
   680
      in
wenzelm@56509
   681
        (case find_first (prp t) prems of
wenzelm@56509
   682
          NONE =>
wenzelm@56509
   683
            let val t = HOLogic.mk_Trueprop (NotC $ (less $ s $ r)) in
wenzelm@56509
   684
              (case find_first (prp t) prems of
wenzelm@56509
   685
                NONE => NONE
wenzelm@56509
   686
              | SOME thm => SOME (mk_meta_eq(thm RS @{thm linorder_class.antisym_conv3})))
wenzelm@56509
   687
            end
wenzelm@56509
   688
        | SOME thm => SOME (mk_meta_eq (thm RS @{thm linorder_class.antisym_conv2})))
wenzelm@56509
   689
      end handle THM _ => NONE)
wenzelm@56509
   690
  | _ => NONE);
haftmann@21083
   691
wenzelm@56509
   692
end;
wenzelm@60758
   693
\<close>
nipkow@15524
   694
wenzelm@56509
   695
simproc_setup antisym_le ("(x::'a::order) \<le> y") = "K antisym_le_simproc"
wenzelm@56509
   696
simproc_setup antisym_less ("\<not> (x::'a::linorder) < y") = "K antisym_less_simproc"
wenzelm@56509
   697
nipkow@15524
   698
wenzelm@60758
   699
subsection \<open>Bounded quantifiers\<close>
haftmann@21083
   700
wenzelm@61955
   701
syntax (ASCII)
wenzelm@21180
   702
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3ALL _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   703
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3EX _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   704
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   705
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   706
wenzelm@21180
   707
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3ALL _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   708
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3EX _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   709
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _>=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   710
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _>=_./ _)" [0, 0, 10] 10)
haftmann@21083
   711
wenzelm@61955
   712
syntax
wenzelm@21180
   713
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   714
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   715
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   716
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   717
wenzelm@21180
   718
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   719
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   720
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   721
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   722
wenzelm@62521
   723
syntax (input)
wenzelm@21180
   724
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3! _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   725
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3? _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   726
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3! _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   727
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3? _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   728
haftmann@21083
   729
translations
haftmann@21083
   730
  "ALL x<y. P"   =>  "ALL x. x < y \<longrightarrow> P"
haftmann@21083
   731
  "EX x<y. P"    =>  "EX x. x < y \<and> P"
haftmann@21083
   732
  "ALL x<=y. P"  =>  "ALL x. x <= y \<longrightarrow> P"
haftmann@21083
   733
  "EX x<=y. P"   =>  "EX x. x <= y \<and> P"
haftmann@21083
   734
  "ALL x>y. P"   =>  "ALL x. x > y \<longrightarrow> P"
haftmann@21083
   735
  "EX x>y. P"    =>  "EX x. x > y \<and> P"
haftmann@21083
   736
  "ALL x>=y. P"  =>  "ALL x. x >= y \<longrightarrow> P"
haftmann@21083
   737
  "EX x>=y. P"   =>  "EX x. x >= y \<and> P"
haftmann@21083
   738
wenzelm@60758
   739
print_translation \<open>
haftmann@21083
   740
let
wenzelm@42287
   741
  val All_binder = Mixfix.binder_name @{const_syntax All};
wenzelm@42287
   742
  val Ex_binder = Mixfix.binder_name @{const_syntax Ex};
haftmann@38786
   743
  val impl = @{const_syntax HOL.implies};
haftmann@38795
   744
  val conj = @{const_syntax HOL.conj};
haftmann@22916
   745
  val less = @{const_syntax less};
haftmann@22916
   746
  val less_eq = @{const_syntax less_eq};
wenzelm@21180
   747
wenzelm@21180
   748
  val trans =
wenzelm@35115
   749
   [((All_binder, impl, less),
wenzelm@35115
   750
    (@{syntax_const "_All_less"}, @{syntax_const "_All_greater"})),
wenzelm@35115
   751
    ((All_binder, impl, less_eq),
wenzelm@35115
   752
    (@{syntax_const "_All_less_eq"}, @{syntax_const "_All_greater_eq"})),
wenzelm@35115
   753
    ((Ex_binder, conj, less),
wenzelm@35115
   754
    (@{syntax_const "_Ex_less"}, @{syntax_const "_Ex_greater"})),
wenzelm@35115
   755
    ((Ex_binder, conj, less_eq),
wenzelm@35115
   756
    (@{syntax_const "_Ex_less_eq"}, @{syntax_const "_Ex_greater_eq"}))];
wenzelm@21180
   757
wenzelm@35115
   758
  fun matches_bound v t =
wenzelm@35115
   759
    (case t of
wenzelm@35364
   760
      Const (@{syntax_const "_bound"}, _) $ Free (v', _) => v = v'
wenzelm@35115
   761
    | _ => false);
wenzelm@35115
   762
  fun contains_var v = Term.exists_subterm (fn Free (x, _) => x = v | _ => false);
wenzelm@49660
   763
  fun mk x c n P = Syntax.const c $ Syntax_Trans.mark_bound_body x $ n $ P;
wenzelm@21180
   764
wenzelm@52143
   765
  fun tr' q = (q, fn _ =>
wenzelm@52143
   766
    (fn [Const (@{syntax_const "_bound"}, _) $ Free (v, T),
wenzelm@35364
   767
        Const (c, _) $ (Const (d, _) $ t $ u) $ P] =>
wenzelm@35115
   768
        (case AList.lookup (op =) trans (q, c, d) of
wenzelm@35115
   769
          NONE => raise Match
wenzelm@35115
   770
        | SOME (l, g) =>
wenzelm@49660
   771
            if matches_bound v t andalso not (contains_var v u) then mk (v, T) l u P
wenzelm@49660
   772
            else if matches_bound v u andalso not (contains_var v t) then mk (v, T) g t P
wenzelm@35115
   773
            else raise Match)
wenzelm@52143
   774
      | _ => raise Match));
wenzelm@21524
   775
in [tr' All_binder, tr' Ex_binder] end
wenzelm@60758
   776
\<close>
haftmann@21083
   777
haftmann@21083
   778
wenzelm@60758
   779
subsection \<open>Transitivity reasoning\<close>
haftmann@21383
   780
haftmann@25193
   781
context ord
haftmann@25193
   782
begin
haftmann@21383
   783
haftmann@25193
   784
lemma ord_le_eq_trans: "a \<le> b \<Longrightarrow> b = c \<Longrightarrow> a \<le> c"
haftmann@25193
   785
  by (rule subst)
haftmann@21383
   786
haftmann@25193
   787
lemma ord_eq_le_trans: "a = b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
haftmann@25193
   788
  by (rule ssubst)
haftmann@21383
   789
haftmann@25193
   790
lemma ord_less_eq_trans: "a < b \<Longrightarrow> b = c \<Longrightarrow> a < c"
haftmann@25193
   791
  by (rule subst)
haftmann@25193
   792
haftmann@25193
   793
lemma ord_eq_less_trans: "a = b \<Longrightarrow> b < c \<Longrightarrow> a < c"
haftmann@25193
   794
  by (rule ssubst)
haftmann@25193
   795
haftmann@25193
   796
end
haftmann@21383
   797
haftmann@21383
   798
lemma order_less_subst2: "(a::'a::order) < b ==> f b < (c::'c::order) ==>
haftmann@21383
   799
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   800
proof -
haftmann@21383
   801
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   802
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   803
  also assume "f b < c"
haftmann@34250
   804
  finally (less_trans) show ?thesis .
haftmann@21383
   805
qed
haftmann@21383
   806
haftmann@21383
   807
lemma order_less_subst1: "(a::'a::order) < f b ==> (b::'b::order) < c ==>
haftmann@21383
   808
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   809
proof -
haftmann@21383
   810
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   811
  assume "a < f b"
haftmann@21383
   812
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@34250
   813
  finally (less_trans) show ?thesis .
haftmann@21383
   814
qed
haftmann@21383
   815
haftmann@21383
   816
lemma order_le_less_subst2: "(a::'a::order) <= b ==> f b < (c::'c::order) ==>
haftmann@21383
   817
  (!!x y. x <= y ==> f x <= f y) ==> f a < c"
haftmann@21383
   818
proof -
haftmann@21383
   819
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   820
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   821
  also assume "f b < c"
haftmann@34250
   822
  finally (le_less_trans) show ?thesis .
haftmann@21383
   823
qed
haftmann@21383
   824
haftmann@21383
   825
lemma order_le_less_subst1: "(a::'a::order) <= f b ==> (b::'b::order) < c ==>
haftmann@21383
   826
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   827
proof -
haftmann@21383
   828
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   829
  assume "a <= f b"
haftmann@21383
   830
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@34250
   831
  finally (le_less_trans) show ?thesis .
haftmann@21383
   832
qed
haftmann@21383
   833
haftmann@21383
   834
lemma order_less_le_subst2: "(a::'a::order) < b ==> f b <= (c::'c::order) ==>
haftmann@21383
   835
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   836
proof -
haftmann@21383
   837
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   838
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   839
  also assume "f b <= c"
haftmann@34250
   840
  finally (less_le_trans) show ?thesis .
haftmann@21383
   841
qed
haftmann@21383
   842
haftmann@21383
   843
lemma order_less_le_subst1: "(a::'a::order) < f b ==> (b::'b::order) <= c ==>
haftmann@21383
   844
  (!!x y. x <= y ==> f x <= f y) ==> a < f c"
haftmann@21383
   845
proof -
haftmann@21383
   846
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   847
  assume "a < f b"
haftmann@21383
   848
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@34250
   849
  finally (less_le_trans) show ?thesis .
haftmann@21383
   850
qed
haftmann@21383
   851
haftmann@21383
   852
lemma order_subst1: "(a::'a::order) <= f b ==> (b::'b::order) <= c ==>
haftmann@21383
   853
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   854
proof -
haftmann@21383
   855
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   856
  assume "a <= f b"
haftmann@21383
   857
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   858
  finally (order_trans) show ?thesis .
haftmann@21383
   859
qed
haftmann@21383
   860
haftmann@21383
   861
lemma order_subst2: "(a::'a::order) <= b ==> f b <= (c::'c::order) ==>
haftmann@21383
   862
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   863
proof -
haftmann@21383
   864
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   865
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   866
  also assume "f b <= c"
haftmann@21383
   867
  finally (order_trans) show ?thesis .
haftmann@21383
   868
qed
haftmann@21383
   869
haftmann@21383
   870
lemma ord_le_eq_subst: "a <= b ==> f b = c ==>
haftmann@21383
   871
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   872
proof -
haftmann@21383
   873
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   874
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   875
  also assume "f b = c"
haftmann@21383
   876
  finally (ord_le_eq_trans) show ?thesis .
haftmann@21383
   877
qed
haftmann@21383
   878
haftmann@21383
   879
lemma ord_eq_le_subst: "a = f b ==> b <= c ==>
haftmann@21383
   880
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   881
proof -
haftmann@21383
   882
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   883
  assume "a = f b"
haftmann@21383
   884
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   885
  finally (ord_eq_le_trans) show ?thesis .
haftmann@21383
   886
qed
haftmann@21383
   887
haftmann@21383
   888
lemma ord_less_eq_subst: "a < b ==> f b = c ==>
haftmann@21383
   889
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   890
proof -
haftmann@21383
   891
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   892
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   893
  also assume "f b = c"
haftmann@21383
   894
  finally (ord_less_eq_trans) show ?thesis .
haftmann@21383
   895
qed
haftmann@21383
   896
haftmann@21383
   897
lemma ord_eq_less_subst: "a = f b ==> b < c ==>
haftmann@21383
   898
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   899
proof -
haftmann@21383
   900
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   901
  assume "a = f b"
haftmann@21383
   902
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@21383
   903
  finally (ord_eq_less_trans) show ?thesis .
haftmann@21383
   904
qed
haftmann@21383
   905
wenzelm@60758
   906
text \<open>
haftmann@21383
   907
  Note that this list of rules is in reverse order of priorities.
wenzelm@60758
   908
\<close>
haftmann@21383
   909
haftmann@27682
   910
lemmas [trans] =
haftmann@21383
   911
  order_less_subst2
haftmann@21383
   912
  order_less_subst1
haftmann@21383
   913
  order_le_less_subst2
haftmann@21383
   914
  order_le_less_subst1
haftmann@21383
   915
  order_less_le_subst2
haftmann@21383
   916
  order_less_le_subst1
haftmann@21383
   917
  order_subst2
haftmann@21383
   918
  order_subst1
haftmann@21383
   919
  ord_le_eq_subst
haftmann@21383
   920
  ord_eq_le_subst
haftmann@21383
   921
  ord_less_eq_subst
haftmann@21383
   922
  ord_eq_less_subst
haftmann@21383
   923
  forw_subst
haftmann@21383
   924
  back_subst
haftmann@21383
   925
  rev_mp
haftmann@21383
   926
  mp
haftmann@27682
   927
haftmann@27682
   928
lemmas (in order) [trans] =
haftmann@27682
   929
  neq_le_trans
haftmann@27682
   930
  le_neq_trans
haftmann@27682
   931
haftmann@27682
   932
lemmas (in preorder) [trans] =
haftmann@27682
   933
  less_trans
haftmann@27682
   934
  less_asym'
haftmann@27682
   935
  le_less_trans
haftmann@27682
   936
  less_le_trans
haftmann@21383
   937
  order_trans
haftmann@27682
   938
haftmann@27682
   939
lemmas (in order) [trans] =
haftmann@27682
   940
  antisym
haftmann@27682
   941
haftmann@27682
   942
lemmas (in ord) [trans] =
haftmann@27682
   943
  ord_le_eq_trans
haftmann@27682
   944
  ord_eq_le_trans
haftmann@27682
   945
  ord_less_eq_trans
haftmann@27682
   946
  ord_eq_less_trans
haftmann@27682
   947
haftmann@27682
   948
lemmas [trans] =
haftmann@27682
   949
  trans
haftmann@27682
   950
haftmann@27682
   951
lemmas order_trans_rules =
haftmann@27682
   952
  order_less_subst2
haftmann@27682
   953
  order_less_subst1
haftmann@27682
   954
  order_le_less_subst2
haftmann@27682
   955
  order_le_less_subst1
haftmann@27682
   956
  order_less_le_subst2
haftmann@27682
   957
  order_less_le_subst1
haftmann@27682
   958
  order_subst2
haftmann@27682
   959
  order_subst1
haftmann@27682
   960
  ord_le_eq_subst
haftmann@27682
   961
  ord_eq_le_subst
haftmann@27682
   962
  ord_less_eq_subst
haftmann@27682
   963
  ord_eq_less_subst
haftmann@27682
   964
  forw_subst
haftmann@27682
   965
  back_subst
haftmann@27682
   966
  rev_mp
haftmann@27682
   967
  mp
haftmann@27682
   968
  neq_le_trans
haftmann@27682
   969
  le_neq_trans
haftmann@27682
   970
  less_trans
haftmann@27682
   971
  less_asym'
haftmann@27682
   972
  le_less_trans
haftmann@27682
   973
  less_le_trans
haftmann@27682
   974
  order_trans
haftmann@27682
   975
  antisym
haftmann@21383
   976
  ord_le_eq_trans
haftmann@21383
   977
  ord_eq_le_trans
haftmann@21383
   978
  ord_less_eq_trans
haftmann@21383
   979
  ord_eq_less_trans
haftmann@21383
   980
  trans
haftmann@21383
   981
wenzelm@60758
   982
text \<open>These support proving chains of decreasing inequalities
wenzelm@60758
   983
    a >= b >= c ... in Isar proofs.\<close>
haftmann@21083
   984
blanchet@45221
   985
lemma xt1 [no_atp]:
haftmann@21083
   986
  "a = b ==> b > c ==> a > c"
haftmann@21083
   987
  "a > b ==> b = c ==> a > c"
haftmann@21083
   988
  "a = b ==> b >= c ==> a >= c"
haftmann@21083
   989
  "a >= b ==> b = c ==> a >= c"
haftmann@21083
   990
  "(x::'a::order) >= y ==> y >= x ==> x = y"
haftmann@21083
   991
  "(x::'a::order) >= y ==> y >= z ==> x >= z"
haftmann@21083
   992
  "(x::'a::order) > y ==> y >= z ==> x > z"
haftmann@21083
   993
  "(x::'a::order) >= y ==> y > z ==> x > z"
wenzelm@23417
   994
  "(a::'a::order) > b ==> b > a ==> P"
haftmann@21083
   995
  "(x::'a::order) > y ==> y > z ==> x > z"
haftmann@21083
   996
  "(a::'a::order) >= b ==> a ~= b ==> a > b"
haftmann@21083
   997
  "(a::'a::order) ~= b ==> a >= b ==> a > b"
lp15@61824
   998
  "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   999
  "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
  1000
  "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
  1001
  "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@25076
  1002
  by auto
haftmann@21083
  1003
blanchet@45221
  1004
lemma xt2 [no_atp]:
haftmann@21083
  1005
  "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
  1006
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
  1007
blanchet@45221
  1008
lemma xt3 [no_atp]: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==>
haftmann@21083
  1009
    (!!x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@21083
  1010
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
  1011
blanchet@45221
  1012
lemma xt4 [no_atp]: "(a::'a::order) > f b ==> (b::'b::order) >= c ==>
haftmann@21083
  1013
  (!!x y. x >= y ==> f x >= f y) ==> a > f c"
haftmann@21083
  1014
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
  1015
blanchet@45221
  1016
lemma xt5 [no_atp]: "(a::'a::order) > b ==> (f b::'b::order) >= c==>
haftmann@21083
  1017
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
  1018
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
  1019
blanchet@45221
  1020
lemma xt6 [no_atp]: "(a::'a::order) >= f b ==> b > c ==>
haftmann@21083
  1021
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
  1022
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
  1023
blanchet@45221
  1024
lemma xt7 [no_atp]: "(a::'a::order) >= b ==> (f b::'b::order) > c ==>
haftmann@21083
  1025
    (!!x y. x >= y ==> f x >= f y) ==> f a > c"
haftmann@21083
  1026
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
  1027
blanchet@45221
  1028
lemma xt8 [no_atp]: "(a::'a::order) > f b ==> (b::'b::order) > c ==>
haftmann@21083
  1029
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
  1030
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
  1031
blanchet@45221
  1032
lemma xt9 [no_atp]: "(a::'a::order) > b ==> (f b::'b::order) > c ==>
haftmann@21083
  1033
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
  1034
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
  1035
blanchet@54147
  1036
lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9
haftmann@21083
  1037
lp15@61824
  1038
(*
haftmann@21083
  1039
  Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands
haftmann@21083
  1040
  for the wrong thing in an Isar proof.
haftmann@21083
  1041
lp15@61824
  1042
  The extra transitivity rules can be used as follows:
haftmann@21083
  1043
haftmann@21083
  1044
lemma "(a::'a::order) > z"
haftmann@21083
  1045
proof -
haftmann@21083
  1046
  have "a >= b" (is "_ >= ?rhs")
haftmann@21083
  1047
    sorry
haftmann@21083
  1048
  also have "?rhs >= c" (is "_ >= ?rhs")
haftmann@21083
  1049
    sorry
haftmann@21083
  1050
  also (xtrans) have "?rhs = d" (is "_ = ?rhs")
haftmann@21083
  1051
    sorry
haftmann@21083
  1052
  also (xtrans) have "?rhs >= e" (is "_ >= ?rhs")
haftmann@21083
  1053
    sorry
haftmann@21083
  1054
  also (xtrans) have "?rhs > f" (is "_ > ?rhs")
haftmann@21083
  1055
    sorry
haftmann@21083
  1056
  also (xtrans) have "?rhs > z"
haftmann@21083
  1057
    sorry
haftmann@21083
  1058
  finally (xtrans) show ?thesis .
haftmann@21083
  1059
qed
haftmann@21083
  1060
haftmann@21083
  1061
  Alternatively, one can use "declare xtrans [trans]" and then
haftmann@21083
  1062
  leave out the "(xtrans)" above.
haftmann@21083
  1063
*)
haftmann@21083
  1064
haftmann@23881
  1065
wenzelm@60758
  1066
subsection \<open>Monotonicity\<close>
haftmann@21083
  1067
haftmann@25076
  1068
context order
haftmann@25076
  1069
begin
haftmann@25076
  1070
wenzelm@61076
  1071
definition mono :: "('a \<Rightarrow> 'b::order) \<Rightarrow> bool" where
haftmann@25076
  1072
  "mono f \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> f x \<le> f y)"
haftmann@25076
  1073
haftmann@25076
  1074
lemma monoI [intro?]:
wenzelm@61076
  1075
  fixes f :: "'a \<Rightarrow> 'b::order"
haftmann@25076
  1076
  shows "(\<And>x y. x \<le> y \<Longrightarrow> f x \<le> f y) \<Longrightarrow> mono f"
haftmann@25076
  1077
  unfolding mono_def by iprover
haftmann@21216
  1078
haftmann@25076
  1079
lemma monoD [dest?]:
wenzelm@61076
  1080
  fixes f :: "'a \<Rightarrow> 'b::order"
haftmann@25076
  1081
  shows "mono f \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
haftmann@25076
  1082
  unfolding mono_def by iprover
haftmann@25076
  1083
haftmann@51263
  1084
lemma monoE:
wenzelm@61076
  1085
  fixes f :: "'a \<Rightarrow> 'b::order"
haftmann@51263
  1086
  assumes "mono f"
haftmann@51263
  1087
  assumes "x \<le> y"
haftmann@51263
  1088
  obtains "f x \<le> f y"
haftmann@51263
  1089
proof
haftmann@51263
  1090
  from assms show "f x \<le> f y" by (simp add: mono_def)
haftmann@51263
  1091
qed
haftmann@51263
  1092
wenzelm@61076
  1093
definition antimono :: "('a \<Rightarrow> 'b::order) \<Rightarrow> bool" where
hoelzl@56020
  1094
  "antimono f \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> f x \<ge> f y)"
hoelzl@56020
  1095
hoelzl@56020
  1096
lemma antimonoI [intro?]:
wenzelm@61076
  1097
  fixes f :: "'a \<Rightarrow> 'b::order"
hoelzl@56020
  1098
  shows "(\<And>x y. x \<le> y \<Longrightarrow> f x \<ge> f y) \<Longrightarrow> antimono f"
hoelzl@56020
  1099
  unfolding antimono_def by iprover
hoelzl@56020
  1100
hoelzl@56020
  1101
lemma antimonoD [dest?]:
wenzelm@61076
  1102
  fixes f :: "'a \<Rightarrow> 'b::order"
hoelzl@56020
  1103
  shows "antimono f \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<ge> f y"
hoelzl@56020
  1104
  unfolding antimono_def by iprover
hoelzl@56020
  1105
hoelzl@56020
  1106
lemma antimonoE:
wenzelm@61076
  1107
  fixes f :: "'a \<Rightarrow> 'b::order"
hoelzl@56020
  1108
  assumes "antimono f"
hoelzl@56020
  1109
  assumes "x \<le> y"
hoelzl@56020
  1110
  obtains "f x \<ge> f y"
hoelzl@56020
  1111
proof
hoelzl@56020
  1112
  from assms show "f x \<ge> f y" by (simp add: antimono_def)
hoelzl@56020
  1113
qed
hoelzl@56020
  1114
wenzelm@61076
  1115
definition strict_mono :: "('a \<Rightarrow> 'b::order) \<Rightarrow> bool" where
haftmann@30298
  1116
  "strict_mono f \<longleftrightarrow> (\<forall>x y. x < y \<longrightarrow> f x < f y)"
haftmann@30298
  1117
haftmann@30298
  1118
lemma strict_monoI [intro?]:
haftmann@30298
  1119
  assumes "\<And>x y. x < y \<Longrightarrow> f x < f y"
haftmann@30298
  1120
  shows "strict_mono f"
haftmann@30298
  1121
  using assms unfolding strict_mono_def by auto
haftmann@30298
  1122
haftmann@30298
  1123
lemma strict_monoD [dest?]:
haftmann@30298
  1124
  "strict_mono f \<Longrightarrow> x < y \<Longrightarrow> f x < f y"
haftmann@30298
  1125
  unfolding strict_mono_def by auto
haftmann@30298
  1126
haftmann@30298
  1127
lemma strict_mono_mono [dest?]:
haftmann@30298
  1128
  assumes "strict_mono f"
haftmann@30298
  1129
  shows "mono f"
haftmann@30298
  1130
proof (rule monoI)
haftmann@30298
  1131
  fix x y
haftmann@30298
  1132
  assume "x \<le> y"
haftmann@30298
  1133
  show "f x \<le> f y"
haftmann@30298
  1134
  proof (cases "x = y")
haftmann@30298
  1135
    case True then show ?thesis by simp
haftmann@30298
  1136
  next
wenzelm@60758
  1137
    case False with \<open>x \<le> y\<close> have "x < y" by simp
haftmann@30298
  1138
    with assms strict_monoD have "f x < f y" by auto
haftmann@30298
  1139
    then show ?thesis by simp
haftmann@30298
  1140
  qed
haftmann@30298
  1141
qed
haftmann@30298
  1142
haftmann@25076
  1143
end
haftmann@25076
  1144
haftmann@25076
  1145
context linorder
haftmann@25076
  1146
begin
haftmann@25076
  1147
haftmann@51263
  1148
lemma mono_invE:
wenzelm@61076
  1149
  fixes f :: "'a \<Rightarrow> 'b::order"
haftmann@51263
  1150
  assumes "mono f"
haftmann@51263
  1151
  assumes "f x < f y"
haftmann@51263
  1152
  obtains "x \<le> y"
haftmann@51263
  1153
proof
haftmann@51263
  1154
  show "x \<le> y"
haftmann@51263
  1155
  proof (rule ccontr)
haftmann@51263
  1156
    assume "\<not> x \<le> y"
haftmann@51263
  1157
    then have "y \<le> x" by simp
wenzelm@60758
  1158
    with \<open>mono f\<close> obtain "f y \<le> f x" by (rule monoE)
wenzelm@60758
  1159
    with \<open>f x < f y\<close> show False by simp
haftmann@51263
  1160
  qed
haftmann@51263
  1161
qed
haftmann@51263
  1162
haftmann@30298
  1163
lemma strict_mono_eq:
haftmann@30298
  1164
  assumes "strict_mono f"
haftmann@30298
  1165
  shows "f x = f y \<longleftrightarrow> x = y"
haftmann@30298
  1166
proof
haftmann@30298
  1167
  assume "f x = f y"
haftmann@30298
  1168
  show "x = y" proof (cases x y rule: linorder_cases)
haftmann@30298
  1169
    case less with assms strict_monoD have "f x < f y" by auto
wenzelm@60758
  1170
    with \<open>f x = f y\<close> show ?thesis by simp
haftmann@30298
  1171
  next
haftmann@30298
  1172
    case equal then show ?thesis .
haftmann@30298
  1173
  next
haftmann@30298
  1174
    case greater with assms strict_monoD have "f y < f x" by auto
wenzelm@60758
  1175
    with \<open>f x = f y\<close> show ?thesis by simp
haftmann@30298
  1176
  qed
haftmann@30298
  1177
qed simp
haftmann@30298
  1178
haftmann@30298
  1179
lemma strict_mono_less_eq:
haftmann@30298
  1180
  assumes "strict_mono f"
haftmann@30298
  1181
  shows "f x \<le> f y \<longleftrightarrow> x \<le> y"
haftmann@30298
  1182
proof
haftmann@30298
  1183
  assume "x \<le> y"
haftmann@30298
  1184
  with assms strict_mono_mono monoD show "f x \<le> f y" by auto
haftmann@30298
  1185
next
haftmann@30298
  1186
  assume "f x \<le> f y"
haftmann@30298
  1187
  show "x \<le> y" proof (rule ccontr)
haftmann@30298
  1188
    assume "\<not> x \<le> y" then have "y < x" by simp
haftmann@30298
  1189
    with assms strict_monoD have "f y < f x" by auto
wenzelm@60758
  1190
    with \<open>f x \<le> f y\<close> show False by simp
haftmann@30298
  1191
  qed
haftmann@30298
  1192
qed
lp15@61824
  1193
haftmann@30298
  1194
lemma strict_mono_less:
haftmann@30298
  1195
  assumes "strict_mono f"
haftmann@30298
  1196
  shows "f x < f y \<longleftrightarrow> x < y"
haftmann@30298
  1197
  using assms
haftmann@30298
  1198
    by (auto simp add: less_le Orderings.less_le strict_mono_eq strict_mono_less_eq)
haftmann@30298
  1199
haftmann@54860
  1200
end
haftmann@54860
  1201
haftmann@54860
  1202
wenzelm@60758
  1203
subsection \<open>min and max -- fundamental\<close>
haftmann@54860
  1204
haftmann@54860
  1205
definition (in ord) min :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@54860
  1206
  "min a b = (if a \<le> b then a else b)"
haftmann@54860
  1207
haftmann@54860
  1208
definition (in ord) max :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@54860
  1209
  "max a b = (if a \<le> b then b else a)"
haftmann@54860
  1210
noschinl@45931
  1211
lemma min_absorb1: "x \<le> y \<Longrightarrow> min x y = x"
haftmann@54861
  1212
  by (simp add: min_def)
haftmann@21383
  1213
haftmann@54857
  1214
lemma max_absorb2: "x \<le> y \<Longrightarrow> max x y = y"
haftmann@54861
  1215
  by (simp add: max_def)
haftmann@21383
  1216
wenzelm@61076
  1217
lemma min_absorb2: "(y::'a::order) \<le> x \<Longrightarrow> min x y = y"
haftmann@54861
  1218
  by (simp add:min_def)
noschinl@45893
  1219
wenzelm@61076
  1220
lemma max_absorb1: "(y::'a::order) \<le> x \<Longrightarrow> max x y = x"
haftmann@54861
  1221
  by (simp add: max_def)
noschinl@45893
  1222
Andreas@61630
  1223
lemma max_min_same [simp]:
Andreas@61630
  1224
  fixes x y :: "'a :: linorder"
Andreas@61630
  1225
  shows "max x (min x y) = x" "max (min x y) x = x" "max (min x y) y = y" "max y (min x y) = y"
Andreas@61630
  1226
by(auto simp add: max_def min_def)
noschinl@45893
  1227
wenzelm@60758
  1228
subsection \<open>(Unique) top and bottom elements\<close>
haftmann@28685
  1229
haftmann@52729
  1230
class bot =
haftmann@43853
  1231
  fixes bot :: 'a ("\<bottom>")
haftmann@52729
  1232
haftmann@52729
  1233
class order_bot = order + bot +
haftmann@51487
  1234
  assumes bot_least: "\<bottom> \<le> a"
haftmann@54868
  1235
begin
haftmann@51487
  1236
wenzelm@61605
  1237
sublocale bot: ordering_top greater_eq greater bot
wenzelm@61169
  1238
  by standard (fact bot_least)
haftmann@51487
  1239
haftmann@43853
  1240
lemma le_bot:
haftmann@43853
  1241
  "a \<le> \<bottom> \<Longrightarrow> a = \<bottom>"
haftmann@51487
  1242
  by (fact bot.extremum_uniqueI)
haftmann@43853
  1243
haftmann@43816
  1244
lemma bot_unique:
haftmann@43853
  1245
  "a \<le> \<bottom> \<longleftrightarrow> a = \<bottom>"
haftmann@51487
  1246
  by (fact bot.extremum_unique)
haftmann@43853
  1247
haftmann@51487
  1248
lemma not_less_bot:
haftmann@51487
  1249
  "\<not> a < \<bottom>"
haftmann@51487
  1250
  by (fact bot.extremum_strict)
haftmann@43816
  1251
haftmann@43814
  1252
lemma bot_less:
haftmann@43853
  1253
  "a \<noteq> \<bottom> \<longleftrightarrow> \<bottom> < a"
haftmann@51487
  1254
  by (fact bot.not_eq_extremum)
haftmann@43814
  1255
haftmann@43814
  1256
end
haftmann@41082
  1257
haftmann@52729
  1258
class top =
haftmann@43853
  1259
  fixes top :: 'a ("\<top>")
haftmann@52729
  1260
haftmann@52729
  1261
class order_top = order + top +
haftmann@51487
  1262
  assumes top_greatest: "a \<le> \<top>"
haftmann@54868
  1263
begin
haftmann@51487
  1264
wenzelm@61605
  1265
sublocale top: ordering_top less_eq less top
wenzelm@61169
  1266
  by standard (fact top_greatest)
haftmann@51487
  1267
haftmann@43853
  1268
lemma top_le:
haftmann@43853
  1269
  "\<top> \<le> a \<Longrightarrow> a = \<top>"
haftmann@51487
  1270
  by (fact top.extremum_uniqueI)
haftmann@43853
  1271
haftmann@43816
  1272
lemma top_unique:
haftmann@43853
  1273
  "\<top> \<le> a \<longleftrightarrow> a = \<top>"
haftmann@51487
  1274
  by (fact top.extremum_unique)
haftmann@43853
  1275
haftmann@51487
  1276
lemma not_top_less:
haftmann@51487
  1277
  "\<not> \<top> < a"
haftmann@51487
  1278
  by (fact top.extremum_strict)
haftmann@43816
  1279
haftmann@43814
  1280
lemma less_top:
haftmann@43853
  1281
  "a \<noteq> \<top> \<longleftrightarrow> a < \<top>"
haftmann@51487
  1282
  by (fact top.not_eq_extremum)
haftmann@43814
  1283
haftmann@43814
  1284
end
haftmann@28685
  1285
haftmann@28685
  1286
wenzelm@60758
  1287
subsection \<open>Dense orders\<close>
haftmann@27823
  1288
hoelzl@53216
  1289
class dense_order = order +
hoelzl@51329
  1290
  assumes dense: "x < y \<Longrightarrow> (\<exists>z. x < z \<and> z < y)"
hoelzl@51329
  1291
hoelzl@53216
  1292
class dense_linorder = linorder + dense_order
hoelzl@35579
  1293
begin
haftmann@27823
  1294
hoelzl@35579
  1295
lemma dense_le:
hoelzl@35579
  1296
  fixes y z :: 'a
hoelzl@35579
  1297
  assumes "\<And>x. x < y \<Longrightarrow> x \<le> z"
hoelzl@35579
  1298
  shows "y \<le> z"
hoelzl@35579
  1299
proof (rule ccontr)
hoelzl@35579
  1300
  assume "\<not> ?thesis"
hoelzl@35579
  1301
  hence "z < y" by simp
hoelzl@35579
  1302
  from dense[OF this]
hoelzl@35579
  1303
  obtain x where "x < y" and "z < x" by safe
wenzelm@60758
  1304
  moreover have "x \<le> z" using assms[OF \<open>x < y\<close>] .
hoelzl@35579
  1305
  ultimately show False by auto
hoelzl@35579
  1306
qed
hoelzl@35579
  1307
hoelzl@35579
  1308
lemma dense_le_bounded:
hoelzl@35579
  1309
  fixes x y z :: 'a
hoelzl@35579
  1310
  assumes "x < y"
hoelzl@35579
  1311
  assumes *: "\<And>w. \<lbrakk> x < w ; w < y \<rbrakk> \<Longrightarrow> w \<le> z"
hoelzl@35579
  1312
  shows "y \<le> z"
hoelzl@35579
  1313
proof (rule dense_le)
hoelzl@35579
  1314
  fix w assume "w < y"
wenzelm@60758
  1315
  from dense[OF \<open>x < y\<close>] obtain u where "x < u" "u < y" by safe
hoelzl@35579
  1316
  from linear[of u w]
hoelzl@35579
  1317
  show "w \<le> z"
hoelzl@35579
  1318
  proof (rule disjE)
hoelzl@35579
  1319
    assume "u \<le> w"
wenzelm@60758
  1320
    from less_le_trans[OF \<open>x < u\<close> \<open>u \<le> w\<close>] \<open>w < y\<close>
hoelzl@35579
  1321
    show "w \<le> z" by (rule *)
hoelzl@35579
  1322
  next
hoelzl@35579
  1323
    assume "w \<le> u"
wenzelm@60758
  1324
    from \<open>w \<le> u\<close> *[OF \<open>x < u\<close> \<open>u < y\<close>]
hoelzl@35579
  1325
    show "w \<le> z" by (rule order_trans)
hoelzl@35579
  1326
  qed
hoelzl@35579
  1327
qed
hoelzl@35579
  1328
hoelzl@51329
  1329
lemma dense_ge:
hoelzl@51329
  1330
  fixes y z :: 'a
hoelzl@51329
  1331
  assumes "\<And>x. z < x \<Longrightarrow> y \<le> x"
hoelzl@51329
  1332
  shows "y \<le> z"
hoelzl@51329
  1333
proof (rule ccontr)
hoelzl@51329
  1334
  assume "\<not> ?thesis"
hoelzl@51329
  1335
  hence "z < y" by simp
hoelzl@51329
  1336
  from dense[OF this]
hoelzl@51329
  1337
  obtain x where "x < y" and "z < x" by safe
wenzelm@60758
  1338
  moreover have "y \<le> x" using assms[OF \<open>z < x\<close>] .
hoelzl@51329
  1339
  ultimately show False by auto
hoelzl@51329
  1340
qed
hoelzl@51329
  1341
hoelzl@51329
  1342
lemma dense_ge_bounded:
hoelzl@51329
  1343
  fixes x y z :: 'a
hoelzl@51329
  1344
  assumes "z < x"
hoelzl@51329
  1345
  assumes *: "\<And>w. \<lbrakk> z < w ; w < x \<rbrakk> \<Longrightarrow> y \<le> w"
hoelzl@51329
  1346
  shows "y \<le> z"
hoelzl@51329
  1347
proof (rule dense_ge)
hoelzl@51329
  1348
  fix w assume "z < w"
wenzelm@60758
  1349
  from dense[OF \<open>z < x\<close>] obtain u where "z < u" "u < x" by safe
hoelzl@51329
  1350
  from linear[of u w]
hoelzl@51329
  1351
  show "y \<le> w"
hoelzl@51329
  1352
  proof (rule disjE)
hoelzl@51329
  1353
    assume "w \<le> u"
wenzelm@60758
  1354
    from \<open>z < w\<close> le_less_trans[OF \<open>w \<le> u\<close> \<open>u < x\<close>]
hoelzl@51329
  1355
    show "y \<le> w" by (rule *)
hoelzl@51329
  1356
  next
hoelzl@51329
  1357
    assume "u \<le> w"
wenzelm@60758
  1358
    from *[OF \<open>z < u\<close> \<open>u < x\<close>] \<open>u \<le> w\<close>
hoelzl@51329
  1359
    show "y \<le> w" by (rule order_trans)
hoelzl@51329
  1360
  qed
hoelzl@51329
  1361
qed
hoelzl@51329
  1362
hoelzl@35579
  1363
end
haftmann@27823
  1364
lp15@61824
  1365
class no_top = order +
hoelzl@51329
  1366
  assumes gt_ex: "\<exists>y. x < y"
hoelzl@51329
  1367
lp15@61824
  1368
class no_bot = order +
hoelzl@51329
  1369
  assumes lt_ex: "\<exists>y. y < x"
hoelzl@51329
  1370
hoelzl@53216
  1371
class unbounded_dense_linorder = dense_linorder + no_top + no_bot
hoelzl@51329
  1372
haftmann@51546
  1373
wenzelm@60758
  1374
subsection \<open>Wellorders\<close>
haftmann@27823
  1375
haftmann@27823
  1376
class wellorder = linorder +
haftmann@27823
  1377
  assumes less_induct [case_names less]: "(\<And>x. (\<And>y. y < x \<Longrightarrow> P y) \<Longrightarrow> P x) \<Longrightarrow> P a"
haftmann@27823
  1378
begin
haftmann@27823
  1379
haftmann@27823
  1380
lemma wellorder_Least_lemma:
haftmann@27823
  1381
  fixes k :: 'a
haftmann@27823
  1382
  assumes "P k"
haftmann@34250
  1383
  shows LeastI: "P (LEAST x. P x)" and Least_le: "(LEAST x. P x) \<le> k"
haftmann@27823
  1384
proof -
haftmann@27823
  1385
  have "P (LEAST x. P x) \<and> (LEAST x. P x) \<le> k"
haftmann@27823
  1386
  using assms proof (induct k rule: less_induct)
haftmann@27823
  1387
    case (less x) then have "P x" by simp
haftmann@27823
  1388
    show ?case proof (rule classical)
haftmann@27823
  1389
      assume assm: "\<not> (P (LEAST a. P a) \<and> (LEAST a. P a) \<le> x)"
haftmann@27823
  1390
      have "\<And>y. P y \<Longrightarrow> x \<le> y"
haftmann@27823
  1391
      proof (rule classical)
haftmann@27823
  1392
        fix y
hoelzl@38705
  1393
        assume "P y" and "\<not> x \<le> y"
haftmann@27823
  1394
        with less have "P (LEAST a. P a)" and "(LEAST a. P a) \<le> y"
haftmann@27823
  1395
          by (auto simp add: not_le)
haftmann@27823
  1396
        with assm have "x < (LEAST a. P a)" and "(LEAST a. P a) \<le> y"
haftmann@27823
  1397
          by auto
haftmann@27823
  1398
        then show "x \<le> y" by auto
haftmann@27823
  1399
      qed
wenzelm@60758
  1400
      with \<open>P x\<close> have Least: "(LEAST a. P a) = x"
haftmann@27823
  1401
        by (rule Least_equality)
wenzelm@60758
  1402
      with \<open>P x\<close> show ?thesis by simp
haftmann@27823
  1403
    qed
haftmann@27823
  1404
  qed
haftmann@27823
  1405
  then show "P (LEAST x. P x)" and "(LEAST x. P x) \<le> k" by auto
haftmann@27823
  1406
qed
haftmann@27823
  1407
wenzelm@61799
  1408
\<comment> "The following 3 lemmas are due to Brian Huffman"
haftmann@27823
  1409
lemma LeastI_ex: "\<exists>x. P x \<Longrightarrow> P (Least P)"
haftmann@27823
  1410
  by (erule exE) (erule LeastI)
haftmann@27823
  1411
haftmann@27823
  1412
lemma LeastI2:
haftmann@27823
  1413
  "P a \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> Q (Least P)"
haftmann@27823
  1414
  by (blast intro: LeastI)
haftmann@27823
  1415
haftmann@27823
  1416
lemma LeastI2_ex:
haftmann@27823
  1417
  "\<exists>a. P a \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> Q (Least P)"
haftmann@27823
  1418
  by (blast intro: LeastI_ex)
haftmann@27823
  1419
hoelzl@38705
  1420
lemma LeastI2_wellorder:
hoelzl@38705
  1421
  assumes "P a"
hoelzl@38705
  1422
  and "\<And>a. \<lbrakk> P a; \<forall>b. P b \<longrightarrow> a \<le> b \<rbrakk> \<Longrightarrow> Q a"
hoelzl@38705
  1423
  shows "Q (Least P)"
hoelzl@38705
  1424
proof (rule LeastI2_order)
wenzelm@60758
  1425
  show "P (Least P)" using \<open>P a\<close> by (rule LeastI)
hoelzl@38705
  1426
next
hoelzl@38705
  1427
  fix y assume "P y" thus "Least P \<le> y" by (rule Least_le)
hoelzl@38705
  1428
next
hoelzl@38705
  1429
  fix x assume "P x" "\<forall>y. P y \<longrightarrow> x \<le> y" thus "Q x" by (rule assms(2))
hoelzl@38705
  1430
qed
hoelzl@38705
  1431
lp15@61699
  1432
lemma LeastI2_wellorder_ex:
lp15@61699
  1433
  assumes "\<exists>x. P x"
lp15@61699
  1434
  and "\<And>a. \<lbrakk> P a; \<forall>b. P b \<longrightarrow> a \<le> b \<rbrakk> \<Longrightarrow> Q a"
lp15@61699
  1435
  shows "Q (Least P)"
lp15@61699
  1436
using assms by clarify (blast intro!: LeastI2_wellorder)
lp15@61699
  1437
haftmann@27823
  1438
lemma not_less_Least: "k < (LEAST x. P x) \<Longrightarrow> \<not> P k"
lp15@61699
  1439
apply (simp add: not_le [symmetric])
haftmann@27823
  1440
apply (erule contrapos_nn)
haftmann@27823
  1441
apply (erule Least_le)
haftmann@27823
  1442
done
haftmann@27823
  1443
lp15@64287
  1444
lemma exists_least_iff: "(\<exists>n. P n) \<longleftrightarrow> (\<exists>n. P n \<and> (\<forall>m < n. \<not> P m))" (is "?lhs \<longleftrightarrow> ?rhs")
lp15@64287
  1445
proof
lp15@64287
  1446
  assume ?rhs thus ?lhs by blast
lp15@64287
  1447
next
lp15@64287
  1448
  assume H: ?lhs then obtain n where n: "P n" by blast
lp15@64287
  1449
  let ?x = "Least P"
lp15@64287
  1450
  { fix m assume m: "m < ?x"
lp15@64287
  1451
    from not_less_Least[OF m] have "\<not> P m" . }
lp15@64287
  1452
  with LeastI_ex[OF H] show ?rhs by blast
lp15@64287
  1453
qed
lp15@64287
  1454
hoelzl@38705
  1455
end
haftmann@27823
  1456
haftmann@28685
  1457
wenzelm@60758
  1458
subsection \<open>Order on @{typ bool}\<close>
haftmann@28685
  1459
haftmann@52729
  1460
instantiation bool :: "{order_bot, order_top, linorder}"
haftmann@28685
  1461
begin
haftmann@28685
  1462
haftmann@28685
  1463
definition
haftmann@41080
  1464
  le_bool_def [simp]: "P \<le> Q \<longleftrightarrow> P \<longrightarrow> Q"
haftmann@28685
  1465
haftmann@28685
  1466
definition
wenzelm@61076
  1467
  [simp]: "(P::bool) < Q \<longleftrightarrow> \<not> P \<and> Q"
haftmann@28685
  1468
haftmann@28685
  1469
definition
haftmann@46631
  1470
  [simp]: "\<bottom> \<longleftrightarrow> False"
haftmann@28685
  1471
haftmann@28685
  1472
definition
haftmann@46631
  1473
  [simp]: "\<top> \<longleftrightarrow> True"
haftmann@28685
  1474
haftmann@28685
  1475
instance proof
haftmann@41080
  1476
qed auto
haftmann@28685
  1477
nipkow@15524
  1478
end
haftmann@28685
  1479
haftmann@28685
  1480
lemma le_boolI: "(P \<Longrightarrow> Q) \<Longrightarrow> P \<le> Q"
haftmann@41080
  1481
  by simp
haftmann@28685
  1482
haftmann@28685
  1483
lemma le_boolI': "P \<longrightarrow> Q \<Longrightarrow> P \<le> Q"
haftmann@41080
  1484
  by simp
haftmann@28685
  1485
haftmann@28685
  1486
lemma le_boolE: "P \<le> Q \<Longrightarrow> P \<Longrightarrow> (Q \<Longrightarrow> R) \<Longrightarrow> R"
haftmann@41080
  1487
  by simp
haftmann@28685
  1488
haftmann@28685
  1489
lemma le_boolD: "P \<le> Q \<Longrightarrow> P \<longrightarrow> Q"
haftmann@41080
  1490
  by simp
haftmann@32899
  1491
haftmann@46631
  1492
lemma bot_boolE: "\<bottom> \<Longrightarrow> P"
haftmann@41080
  1493
  by simp
haftmann@32899
  1494
haftmann@46631
  1495
lemma top_boolI: \<top>
haftmann@41080
  1496
  by simp
haftmann@28685
  1497
haftmann@28685
  1498
lemma [code]:
haftmann@28685
  1499
  "False \<le> b \<longleftrightarrow> True"
haftmann@28685
  1500
  "True \<le> b \<longleftrightarrow> b"
haftmann@28685
  1501
  "False < b \<longleftrightarrow> b"
haftmann@28685
  1502
  "True < b \<longleftrightarrow> False"
haftmann@41080
  1503
  by simp_all
haftmann@28685
  1504
haftmann@28685
  1505
wenzelm@60758
  1506
subsection \<open>Order on @{typ "_ \<Rightarrow> _"}\<close>
haftmann@28685
  1507
haftmann@28685
  1508
instantiation "fun" :: (type, ord) ord
haftmann@28685
  1509
begin
haftmann@28685
  1510
haftmann@28685
  1511
definition
haftmann@37767
  1512
  le_fun_def: "f \<le> g \<longleftrightarrow> (\<forall>x. f x \<le> g x)"
haftmann@28685
  1513
haftmann@28685
  1514
definition
wenzelm@61076
  1515
  "(f::'a \<Rightarrow> 'b) < g \<longleftrightarrow> f \<le> g \<and> \<not> (g \<le> f)"
haftmann@28685
  1516
haftmann@28685
  1517
instance ..
haftmann@28685
  1518
haftmann@28685
  1519
end
haftmann@28685
  1520
haftmann@28685
  1521
instance "fun" :: (type, preorder) preorder proof
haftmann@28685
  1522
qed (auto simp add: le_fun_def less_fun_def
huffman@44921
  1523
  intro: order_trans antisym)
haftmann@28685
  1524
haftmann@28685
  1525
instance "fun" :: (type, order) order proof
huffman@44921
  1526
qed (auto simp add: le_fun_def intro: antisym)
haftmann@28685
  1527
haftmann@41082
  1528
instantiation "fun" :: (type, bot) bot
haftmann@41082
  1529
begin
haftmann@41082
  1530
haftmann@41082
  1531
definition
haftmann@46631
  1532
  "\<bottom> = (\<lambda>x. \<bottom>)"
haftmann@41082
  1533
haftmann@52729
  1534
instance ..
haftmann@52729
  1535
haftmann@52729
  1536
end
haftmann@52729
  1537
haftmann@52729
  1538
instantiation "fun" :: (type, order_bot) order_bot
haftmann@52729
  1539
begin
haftmann@52729
  1540
haftmann@49769
  1541
lemma bot_apply [simp, code]:
haftmann@46631
  1542
  "\<bottom> x = \<bottom>"
haftmann@41082
  1543
  by (simp add: bot_fun_def)
haftmann@41082
  1544
haftmann@41082
  1545
instance proof
noschinl@46884
  1546
qed (simp add: le_fun_def)
haftmann@41082
  1547
haftmann@41082
  1548
end
haftmann@41082
  1549
haftmann@28685
  1550
instantiation "fun" :: (type, top) top
haftmann@28685
  1551
begin
haftmann@28685
  1552
haftmann@28685
  1553
definition
haftmann@46631
  1554
  [no_atp]: "\<top> = (\<lambda>x. \<top>)"
haftmann@28685
  1555
haftmann@52729
  1556
instance ..
haftmann@52729
  1557
haftmann@52729
  1558
end
haftmann@52729
  1559
haftmann@52729
  1560
instantiation "fun" :: (type, order_top) order_top
haftmann@52729
  1561
begin
haftmann@52729
  1562
haftmann@49769
  1563
lemma top_apply [simp, code]:
haftmann@46631
  1564
  "\<top> x = \<top>"
haftmann@41080
  1565
  by (simp add: top_fun_def)
haftmann@41080
  1566
haftmann@28685
  1567
instance proof
noschinl@46884
  1568
qed (simp add: le_fun_def)
haftmann@28685
  1569
haftmann@28685
  1570
end
haftmann@28685
  1571
haftmann@28685
  1572
lemma le_funI: "(\<And>x. f x \<le> g x) \<Longrightarrow> f \<le> g"
haftmann@28685
  1573
  unfolding le_fun_def by simp
haftmann@28685
  1574
haftmann@28685
  1575
lemma le_funE: "f \<le> g \<Longrightarrow> (f x \<le> g x \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@28685
  1576
  unfolding le_fun_def by simp
haftmann@28685
  1577
haftmann@28685
  1578
lemma le_funD: "f \<le> g \<Longrightarrow> f x \<le> g x"
haftmann@54860
  1579
  by (rule le_funE)
haftmann@28685
  1580
hoelzl@59000
  1581
lemma mono_compose: "mono Q \<Longrightarrow> mono (\<lambda>i x. Q i (f x))"
hoelzl@59000
  1582
  unfolding mono_def le_fun_def by auto
hoelzl@59000
  1583
haftmann@34250
  1584
wenzelm@60758
  1585
subsection \<open>Order on unary and binary predicates\<close>
haftmann@46631
  1586
haftmann@46631
  1587
lemma predicate1I:
haftmann@46631
  1588
  assumes PQ: "\<And>x. P x \<Longrightarrow> Q x"
haftmann@46631
  1589
  shows "P \<le> Q"
haftmann@46631
  1590
  apply (rule le_funI)
haftmann@46631
  1591
  apply (rule le_boolI)
haftmann@46631
  1592
  apply (rule PQ)
haftmann@46631
  1593
  apply assumption
haftmann@46631
  1594
  done
haftmann@46631
  1595
haftmann@46631
  1596
lemma predicate1D:
haftmann@46631
  1597
  "P \<le> Q \<Longrightarrow> P x \<Longrightarrow> Q x"
haftmann@46631
  1598
  apply (erule le_funE)
haftmann@46631
  1599
  apply (erule le_boolE)
haftmann@46631
  1600
  apply assumption+
haftmann@46631
  1601
  done
haftmann@46631
  1602
haftmann@46631
  1603
lemma rev_predicate1D:
haftmann@46631
  1604
  "P x \<Longrightarrow> P \<le> Q \<Longrightarrow> Q x"
haftmann@46631
  1605
  by (rule predicate1D)
haftmann@46631
  1606
haftmann@46631
  1607
lemma predicate2I:
haftmann@46631
  1608
  assumes PQ: "\<And>x y. P x y \<Longrightarrow> Q x y"
haftmann@46631
  1609
  shows "P \<le> Q"
haftmann@46631
  1610
  apply (rule le_funI)+
haftmann@46631
  1611
  apply (rule le_boolI)
haftmann@46631
  1612
  apply (rule PQ)
haftmann@46631
  1613
  apply assumption
haftmann@46631
  1614
  done
haftmann@46631
  1615
haftmann@46631
  1616
lemma predicate2D:
haftmann@46631
  1617
  "P \<le> Q \<Longrightarrow> P x y \<Longrightarrow> Q x y"
haftmann@46631
  1618
  apply (erule le_funE)+
haftmann@46631
  1619
  apply (erule le_boolE)
haftmann@46631
  1620
  apply assumption+
haftmann@46631
  1621
  done
haftmann@46631
  1622
haftmann@46631
  1623
lemma rev_predicate2D:
haftmann@46631
  1624
  "P x y \<Longrightarrow> P \<le> Q \<Longrightarrow> Q x y"
haftmann@46631
  1625
  by (rule predicate2D)
haftmann@46631
  1626
haftmann@46631
  1627
lemma bot1E [no_atp]: "\<bottom> x \<Longrightarrow> P"
haftmann@46631
  1628
  by (simp add: bot_fun_def)
haftmann@46631
  1629
haftmann@46631
  1630
lemma bot2E: "\<bottom> x y \<Longrightarrow> P"
haftmann@46631
  1631
  by (simp add: bot_fun_def)
haftmann@46631
  1632
haftmann@46631
  1633
lemma top1I: "\<top> x"
haftmann@46631
  1634
  by (simp add: top_fun_def)
haftmann@46631
  1635
haftmann@46631
  1636
lemma top2I: "\<top> x y"
haftmann@46631
  1637
  by (simp add: top_fun_def)
haftmann@46631
  1638
haftmann@46631
  1639
wenzelm@60758
  1640
subsection \<open>Name duplicates\<close>
haftmann@34250
  1641
haftmann@34250
  1642
lemmas order_eq_refl = preorder_class.eq_refl
haftmann@34250
  1643
lemmas order_less_irrefl = preorder_class.less_irrefl
haftmann@34250
  1644
lemmas order_less_imp_le = preorder_class.less_imp_le
haftmann@34250
  1645
lemmas order_less_not_sym = preorder_class.less_not_sym
haftmann@34250
  1646
lemmas order_less_asym = preorder_class.less_asym
haftmann@34250
  1647
lemmas order_less_trans = preorder_class.less_trans
haftmann@34250
  1648
lemmas order_le_less_trans = preorder_class.le_less_trans
haftmann@34250
  1649
lemmas order_less_le_trans = preorder_class.less_le_trans
haftmann@34250
  1650
lemmas order_less_imp_not_less = preorder_class.less_imp_not_less
haftmann@34250
  1651
lemmas order_less_imp_triv = preorder_class.less_imp_triv
haftmann@34250
  1652
lemmas order_less_asym' = preorder_class.less_asym'
haftmann@34250
  1653
haftmann@34250
  1654
lemmas order_less_le = order_class.less_le
haftmann@34250
  1655
lemmas order_le_less = order_class.le_less
haftmann@34250
  1656
lemmas order_le_imp_less_or_eq = order_class.le_imp_less_or_eq
haftmann@34250
  1657
lemmas order_less_imp_not_eq = order_class.less_imp_not_eq
haftmann@34250
  1658
lemmas order_less_imp_not_eq2 = order_class.less_imp_not_eq2
haftmann@34250
  1659
lemmas order_neq_le_trans = order_class.neq_le_trans
haftmann@34250
  1660
lemmas order_le_neq_trans = order_class.le_neq_trans
haftmann@34250
  1661
lemmas order_antisym = order_class.antisym
haftmann@34250
  1662
lemmas order_eq_iff = order_class.eq_iff
haftmann@34250
  1663
lemmas order_antisym_conv = order_class.antisym_conv
haftmann@34250
  1664
haftmann@34250
  1665
lemmas linorder_linear = linorder_class.linear
haftmann@34250
  1666
lemmas linorder_less_linear = linorder_class.less_linear
haftmann@34250
  1667
lemmas linorder_le_less_linear = linorder_class.le_less_linear
haftmann@34250
  1668
lemmas linorder_le_cases = linorder_class.le_cases
haftmann@34250
  1669
lemmas linorder_not_less = linorder_class.not_less
haftmann@34250
  1670
lemmas linorder_not_le = linorder_class.not_le
haftmann@34250
  1671
lemmas linorder_neq_iff = linorder_class.neq_iff
haftmann@34250
  1672
lemmas linorder_neqE = linorder_class.neqE
haftmann@34250
  1673
lemmas linorder_antisym_conv1 = linorder_class.antisym_conv1
haftmann@34250
  1674
lemmas linorder_antisym_conv2 = linorder_class.antisym_conv2
haftmann@34250
  1675
lemmas linorder_antisym_conv3 = linorder_class.antisym_conv3
haftmann@34250
  1676
haftmann@28685
  1677
end