src/HOL/Orderings.thy
author wenzelm
Sun Sep 18 20:33:48 2016 +0200 (2016-09-18)
changeset 63915 bab633745c7f
parent 63819 58f74e90b96d
child 64287 d85d88722745
permissions -rw-r--r--
tuned proofs;
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
wenzelm@60758
   408
text \<open>Dual order\<close>
haftmann@22916
   409
haftmann@26014
   410
lemma dual_linorder:
haftmann@36635
   411
  "class.linorder (op \<ge>) (op >)"
haftmann@36635
   412
by (rule class.linorder.intro, rule dual_order) (unfold_locales, rule linear)
haftmann@22916
   413
haftmann@21248
   414
end
haftmann@21248
   415
haftmann@23948
   416
wenzelm@60758
   417
text \<open>Alternative introduction rule with bias towards strict order\<close>
haftmann@56545
   418
haftmann@56545
   419
lemma linorder_strictI:
haftmann@63819
   420
  fixes less_eq (infix "\<^bold>\<le>" 50)
haftmann@63819
   421
    and less (infix "\<^bold><" 50)
haftmann@56545
   422
  assumes "class.order less_eq less"
haftmann@63819
   423
  assumes trichotomy: "\<And>a b. a \<^bold>< b \<or> a = b \<or> b \<^bold>< a"
haftmann@56545
   424
  shows "class.linorder less_eq less"
haftmann@56545
   425
proof -
haftmann@56545
   426
  interpret order less_eq less
wenzelm@60758
   427
    by (fact \<open>class.order less_eq less\<close>)
haftmann@56545
   428
  show ?thesis
haftmann@56545
   429
  proof
haftmann@56545
   430
    fix a b
haftmann@63819
   431
    show "a \<^bold>\<le> b \<or> b \<^bold>\<le> a"
haftmann@56545
   432
      using trichotomy by (auto simp add: le_less)
haftmann@56545
   433
  qed
haftmann@56545
   434
qed
haftmann@56545
   435
haftmann@56545
   436
wenzelm@60758
   437
subsection \<open>Reasoning tools setup\<close>
haftmann@21083
   438
wenzelm@60758
   439
ML \<open>
ballarin@24641
   440
signature ORDERS =
ballarin@24641
   441
sig
ballarin@24641
   442
  val print_structures: Proof.context -> unit
wenzelm@32215
   443
  val order_tac: Proof.context -> thm list -> int -> tactic
wenzelm@58826
   444
  val add_struct: string * term list -> string -> attribute
wenzelm@58826
   445
  val del_struct: string * term list -> attribute
ballarin@24641
   446
end;
haftmann@21091
   447
ballarin@24641
   448
structure Orders: ORDERS =
haftmann@21248
   449
struct
ballarin@24641
   450
wenzelm@56508
   451
(* context data *)
ballarin@24641
   452
ballarin@24641
   453
fun struct_eq ((s1: string, ts1), (s2, ts2)) =
wenzelm@56508
   454
  s1 = s2 andalso eq_list (op aconv) (ts1, ts2);
ballarin@24641
   455
wenzelm@33519
   456
structure Data = Generic_Data
ballarin@24641
   457
(
ballarin@24641
   458
  type T = ((string * term list) * Order_Tac.less_arith) list;
ballarin@24641
   459
    (* Order structures:
ballarin@24641
   460
       identifier of the structure, list of operations and record of theorems
ballarin@24641
   461
       needed to set up the transitivity reasoner,
ballarin@24641
   462
       identifier and operations identify the structure uniquely. *)
ballarin@24641
   463
  val empty = [];
ballarin@24641
   464
  val extend = I;
wenzelm@33519
   465
  fun merge data = AList.join struct_eq (K fst) data;
ballarin@24641
   466
);
ballarin@24641
   467
ballarin@24641
   468
fun print_structures ctxt =
ballarin@24641
   469
  let
ballarin@24641
   470
    val structs = Data.get (Context.Proof ctxt);
ballarin@24641
   471
    fun pretty_term t = Pretty.block
wenzelm@24920
   472
      [Pretty.quote (Syntax.pretty_term ctxt t), Pretty.brk 1,
ballarin@24641
   473
        Pretty.str "::", Pretty.brk 1,
wenzelm@24920
   474
        Pretty.quote (Syntax.pretty_typ ctxt (type_of t))];
ballarin@24641
   475
    fun pretty_struct ((s, ts), _) = Pretty.block
ballarin@24641
   476
      [Pretty.str s, Pretty.str ":", Pretty.brk 1,
ballarin@24641
   477
       Pretty.enclose "(" ")" (Pretty.breaks (map pretty_term ts))];
ballarin@24641
   478
  in
wenzelm@51579
   479
    Pretty.writeln (Pretty.big_list "order structures:" (map pretty_struct structs))
ballarin@24641
   480
  end;
ballarin@24641
   481
wenzelm@56508
   482
val _ =
wenzelm@59936
   483
  Outer_Syntax.command @{command_keyword print_orders}
wenzelm@56508
   484
    "print order structures available to transitivity reasoner"
wenzelm@60097
   485
    (Scan.succeed (Toplevel.keep (print_structures o Toplevel.context_of)));
haftmann@21091
   486
wenzelm@56508
   487
wenzelm@56508
   488
(* tactics *)
wenzelm@56508
   489
wenzelm@56508
   490
fun struct_tac ((s, ops), thms) ctxt facts =
ballarin@24641
   491
  let
wenzelm@56508
   492
    val [eq, le, less] = ops;
berghofe@30107
   493
    fun decomp thy (@{const Trueprop} $ t) =
wenzelm@56508
   494
          let
wenzelm@56508
   495
            fun excluded t =
wenzelm@56508
   496
              (* exclude numeric types: linear arithmetic subsumes transitivity *)
wenzelm@56508
   497
              let val T = type_of t
wenzelm@56508
   498
              in
wenzelm@56508
   499
                T = HOLogic.natT orelse T = HOLogic.intT orelse T = HOLogic.realT
wenzelm@56508
   500
              end;
wenzelm@56508
   501
            fun rel (bin_op $ t1 $ t2) =
wenzelm@56508
   502
                  if excluded t1 then NONE
wenzelm@56508
   503
                  else if Pattern.matches thy (eq, bin_op) then SOME (t1, "=", t2)
wenzelm@56508
   504
                  else if Pattern.matches thy (le, bin_op) then SOME (t1, "<=", t2)
wenzelm@56508
   505
                  else if Pattern.matches thy (less, bin_op) then SOME (t1, "<", t2)
wenzelm@56508
   506
                  else NONE
wenzelm@56508
   507
              | rel _ = NONE;
wenzelm@56508
   508
            fun dec (Const (@{const_name Not}, _) $ t) =
wenzelm@56508
   509
                  (case rel t of NONE =>
wenzelm@56508
   510
                    NONE
wenzelm@56508
   511
                  | SOME (t1, rel, t2) => SOME (t1, "~" ^ rel, t2))
wenzelm@56508
   512
              | dec x = rel x;
wenzelm@56508
   513
          in dec t end
wenzelm@56508
   514
      | decomp _ _ = NONE;
ballarin@24641
   515
  in
wenzelm@56508
   516
    (case s of
wenzelm@56508
   517
      "order" => Order_Tac.partial_tac decomp thms ctxt facts
wenzelm@56508
   518
    | "linorder" => Order_Tac.linear_tac decomp thms ctxt facts
wenzelm@56508
   519
    | _ => error ("Unknown order kind " ^ quote s ^ " encountered in transitivity reasoner"))
ballarin@24641
   520
  end
ballarin@24641
   521
wenzelm@56508
   522
fun order_tac ctxt facts =
wenzelm@56508
   523
  FIRST' (map (fn s => CHANGED o struct_tac s ctxt facts) (Data.get (Context.Proof ctxt)));
ballarin@24641
   524
ballarin@24641
   525
wenzelm@56508
   526
(* attributes *)
ballarin@24641
   527
wenzelm@58826
   528
fun add_struct s tag =
ballarin@24641
   529
  Thm.declaration_attribute
ballarin@24641
   530
    (fn thm => Data.map (AList.map_default struct_eq (s, Order_Tac.empty TrueI) (Order_Tac.update tag thm)));
ballarin@24641
   531
fun del_struct s =
ballarin@24641
   532
  Thm.declaration_attribute
ballarin@24641
   533
    (fn _ => Data.map (AList.delete struct_eq s));
ballarin@24641
   534
haftmann@21091
   535
end;
wenzelm@60758
   536
\<close>
haftmann@21091
   537
wenzelm@60758
   538
attribute_setup order = \<open>
wenzelm@58826
   539
  Scan.lift ((Args.add -- Args.name >> (fn (_, s) => SOME s) || Args.del >> K NONE) --|
wenzelm@58826
   540
    Args.colon (* FIXME || Scan.succeed true *) ) -- Scan.lift Args.name --
wenzelm@58826
   541
    Scan.repeat Args.term
wenzelm@58826
   542
    >> (fn ((SOME tag, n), ts) => Orders.add_struct (n, ts) tag
wenzelm@58826
   543
         | ((NONE, n), ts) => Orders.del_struct (n, ts))
wenzelm@60758
   544
\<close> "theorems controlling transitivity reasoner"
wenzelm@58826
   545
wenzelm@60758
   546
method_setup order = \<open>
wenzelm@47432
   547
  Scan.succeed (fn ctxt => SIMPLE_METHOD' (Orders.order_tac ctxt []))
wenzelm@60758
   548
\<close> "transitivity reasoner"
ballarin@24641
   549
ballarin@24641
   550
wenzelm@60758
   551
text \<open>Declarations to set up transitivity reasoner of partial and linear orders.\<close>
ballarin@24641
   552
haftmann@25076
   553
context order
haftmann@25076
   554
begin
haftmann@25076
   555
ballarin@24641
   556
(* The type constraint on @{term op =} below is necessary since the operation
ballarin@24641
   557
   is not a parameter of the locale. *)
haftmann@25076
   558
haftmann@27689
   559
declare less_irrefl [THEN notE, order add less_reflE: order "op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool" "op <=" "op <"]
lp15@61824
   560
haftmann@27689
   561
declare order_refl  [order add le_refl: order "op = :: 'a => 'a => bool" "op <=" "op <"]
lp15@61824
   562
haftmann@27689
   563
declare less_imp_le [order add less_imp_le: order "op = :: 'a => 'a => bool" "op <=" "op <"]
lp15@61824
   564
haftmann@27689
   565
declare antisym [order add eqI: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   566
haftmann@27689
   567
declare eq_refl [order add eqD1: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   568
haftmann@27689
   569
declare sym [THEN eq_refl, order add eqD2: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   570
haftmann@27689
   571
declare less_trans [order add less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
lp15@61824
   572
haftmann@27689
   573
declare less_le_trans [order add less_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
lp15@61824
   574
haftmann@27689
   575
declare le_less_trans [order add le_less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   576
haftmann@27689
   577
declare order_trans [order add le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   578
haftmann@27689
   579
declare le_neq_trans [order add le_neq_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   580
haftmann@27689
   581
declare neq_le_trans [order add neq_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   582
haftmann@27689
   583
declare less_imp_neq [order add less_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   584
haftmann@27689
   585
declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   586
haftmann@27689
   587
declare not_sym [order add not_sym: order "op = :: 'a => 'a => bool" "op <=" "op <"]
ballarin@24641
   588
haftmann@25076
   589
end
haftmann@25076
   590
haftmann@25076
   591
context linorder
haftmann@25076
   592
begin
ballarin@24641
   593
haftmann@27689
   594
declare [[order del: order "op = :: 'a => 'a => bool" "op <=" "op <"]]
haftmann@27689
   595
haftmann@27689
   596
declare less_irrefl [THEN notE, order add less_reflE: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   597
haftmann@27689
   598
declare order_refl [order add le_refl: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   599
haftmann@27689
   600
declare less_imp_le [order add less_imp_le: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   601
haftmann@27689
   602
declare not_less [THEN iffD2, order add not_lessI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   603
haftmann@27689
   604
declare not_le [THEN iffD2, order add not_leI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   605
haftmann@27689
   606
declare not_less [THEN iffD1, order add not_lessD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   607
haftmann@27689
   608
declare not_le [THEN iffD1, order add not_leD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   609
haftmann@27689
   610
declare antisym [order add eqI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   611
haftmann@27689
   612
declare eq_refl [order add eqD1: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@25076
   613
haftmann@27689
   614
declare sym [THEN eq_refl, order add eqD2: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   615
haftmann@27689
   616
declare less_trans [order add less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   617
haftmann@27689
   618
declare less_le_trans [order add less_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   619
haftmann@27689
   620
declare le_less_trans [order add le_less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   621
haftmann@27689
   622
declare order_trans [order add le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   623
haftmann@27689
   624
declare le_neq_trans [order add le_neq_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   625
haftmann@27689
   626
declare neq_le_trans [order add neq_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   627
haftmann@27689
   628
declare less_imp_neq [order add less_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   629
haftmann@27689
   630
declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   631
haftmann@27689
   632
declare not_sym [order add not_sym: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
ballarin@24641
   633
haftmann@25076
   634
end
haftmann@25076
   635
wenzelm@60758
   636
setup \<open>
wenzelm@56509
   637
  map_theory_simpset (fn ctxt0 => ctxt0 addSolver
wenzelm@56509
   638
    mk_solver "Transitivity" (fn ctxt => Orders.order_tac ctxt (Simplifier.prems_of ctxt)))
wenzelm@56509
   639
  (*Adding the transitivity reasoners also as safe solvers showed a slight
wenzelm@56509
   640
    speed up, but the reasoning strength appears to be not higher (at least
wenzelm@56509
   641
    no breaking of additional proofs in the entire HOL distribution, as
wenzelm@56509
   642
    of 5 March 2004, was observed).*)
wenzelm@60758
   643
\<close>
nipkow@15524
   644
wenzelm@60758
   645
ML \<open>
wenzelm@56509
   646
local
wenzelm@56509
   647
  fun prp t thm = Thm.prop_of thm = t;  (* FIXME proper aconv!? *)
wenzelm@56509
   648
in
nipkow@15524
   649
wenzelm@56509
   650
fun antisym_le_simproc ctxt ct =
wenzelm@59582
   651
  (case Thm.term_of ct of
wenzelm@56509
   652
    (le as Const (_, T)) $ r $ s =>
wenzelm@56509
   653
     (let
wenzelm@56509
   654
        val prems = Simplifier.prems_of ctxt;
wenzelm@56509
   655
        val less = Const (@{const_name less}, T);
wenzelm@56509
   656
        val t = HOLogic.mk_Trueprop(le $ s $ r);
wenzelm@56509
   657
      in
wenzelm@56509
   658
        (case find_first (prp t) prems of
wenzelm@56509
   659
          NONE =>
wenzelm@56509
   660
            let val t = HOLogic.mk_Trueprop(HOLogic.Not $ (less $ r $ s)) in
wenzelm@56509
   661
              (case find_first (prp t) prems of
wenzelm@56509
   662
                NONE => NONE
wenzelm@56509
   663
              | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv1})))
wenzelm@56509
   664
             end
wenzelm@56509
   665
         | SOME thm => SOME (mk_meta_eq (thm RS @{thm order_class.antisym_conv})))
wenzelm@56509
   666
      end handle THM _ => NONE)
wenzelm@56509
   667
  | _ => NONE);
nipkow@15524
   668
wenzelm@56509
   669
fun antisym_less_simproc ctxt ct =
wenzelm@59582
   670
  (case Thm.term_of ct of
wenzelm@56509
   671
    NotC $ ((less as Const(_,T)) $ r $ s) =>
wenzelm@56509
   672
     (let
wenzelm@56509
   673
       val prems = Simplifier.prems_of ctxt;
wenzelm@56509
   674
       val le = Const (@{const_name less_eq}, T);
wenzelm@56509
   675
       val t = HOLogic.mk_Trueprop(le $ r $ s);
wenzelm@56509
   676
      in
wenzelm@56509
   677
        (case find_first (prp t) prems of
wenzelm@56509
   678
          NONE =>
wenzelm@56509
   679
            let val t = HOLogic.mk_Trueprop (NotC $ (less $ s $ r)) in
wenzelm@56509
   680
              (case find_first (prp t) prems of
wenzelm@56509
   681
                NONE => NONE
wenzelm@56509
   682
              | SOME thm => SOME (mk_meta_eq(thm RS @{thm linorder_class.antisym_conv3})))
wenzelm@56509
   683
            end
wenzelm@56509
   684
        | SOME thm => SOME (mk_meta_eq (thm RS @{thm linorder_class.antisym_conv2})))
wenzelm@56509
   685
      end handle THM _ => NONE)
wenzelm@56509
   686
  | _ => NONE);
haftmann@21083
   687
wenzelm@56509
   688
end;
wenzelm@60758
   689
\<close>
nipkow@15524
   690
wenzelm@56509
   691
simproc_setup antisym_le ("(x::'a::order) \<le> y") = "K antisym_le_simproc"
wenzelm@56509
   692
simproc_setup antisym_less ("\<not> (x::'a::linorder) < y") = "K antisym_less_simproc"
wenzelm@56509
   693
nipkow@15524
   694
wenzelm@60758
   695
subsection \<open>Bounded quantifiers\<close>
haftmann@21083
   696
wenzelm@61955
   697
syntax (ASCII)
wenzelm@21180
   698
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3ALL _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   699
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3EX _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   700
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   701
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   702
wenzelm@21180
   703
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3ALL _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   704
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3EX _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   705
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _>=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   706
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _>=_./ _)" [0, 0, 10] 10)
haftmann@21083
   707
wenzelm@61955
   708
syntax
wenzelm@21180
   709
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   710
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   711
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   712
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   713
wenzelm@21180
   714
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   715
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   716
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   717
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   718
wenzelm@62521
   719
syntax (input)
wenzelm@21180
   720
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3! _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   721
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3? _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   722
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3! _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   723
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3? _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   724
haftmann@21083
   725
translations
haftmann@21083
   726
  "ALL x<y. P"   =>  "ALL x. x < y \<longrightarrow> P"
haftmann@21083
   727
  "EX x<y. P"    =>  "EX x. x < y \<and> P"
haftmann@21083
   728
  "ALL x<=y. P"  =>  "ALL x. x <= y \<longrightarrow> P"
haftmann@21083
   729
  "EX x<=y. P"   =>  "EX x. x <= y \<and> P"
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
wenzelm@60758
   735
print_translation \<open>
haftmann@21083
   736
let
wenzelm@42287
   737
  val All_binder = Mixfix.binder_name @{const_syntax All};
wenzelm@42287
   738
  val Ex_binder = Mixfix.binder_name @{const_syntax Ex};
haftmann@38786
   739
  val impl = @{const_syntax HOL.implies};
haftmann@38795
   740
  val conj = @{const_syntax HOL.conj};
haftmann@22916
   741
  val less = @{const_syntax less};
haftmann@22916
   742
  val less_eq = @{const_syntax less_eq};
wenzelm@21180
   743
wenzelm@21180
   744
  val trans =
wenzelm@35115
   745
   [((All_binder, impl, less),
wenzelm@35115
   746
    (@{syntax_const "_All_less"}, @{syntax_const "_All_greater"})),
wenzelm@35115
   747
    ((All_binder, impl, less_eq),
wenzelm@35115
   748
    (@{syntax_const "_All_less_eq"}, @{syntax_const "_All_greater_eq"})),
wenzelm@35115
   749
    ((Ex_binder, conj, less),
wenzelm@35115
   750
    (@{syntax_const "_Ex_less"}, @{syntax_const "_Ex_greater"})),
wenzelm@35115
   751
    ((Ex_binder, conj, less_eq),
wenzelm@35115
   752
    (@{syntax_const "_Ex_less_eq"}, @{syntax_const "_Ex_greater_eq"}))];
wenzelm@21180
   753
wenzelm@35115
   754
  fun matches_bound v t =
wenzelm@35115
   755
    (case t of
wenzelm@35364
   756
      Const (@{syntax_const "_bound"}, _) $ Free (v', _) => v = v'
wenzelm@35115
   757
    | _ => false);
wenzelm@35115
   758
  fun contains_var v = Term.exists_subterm (fn Free (x, _) => x = v | _ => false);
wenzelm@49660
   759
  fun mk x c n P = Syntax.const c $ Syntax_Trans.mark_bound_body x $ n $ P;
wenzelm@21180
   760
wenzelm@52143
   761
  fun tr' q = (q, fn _ =>
wenzelm@52143
   762
    (fn [Const (@{syntax_const "_bound"}, _) $ Free (v, T),
wenzelm@35364
   763
        Const (c, _) $ (Const (d, _) $ t $ u) $ P] =>
wenzelm@35115
   764
        (case AList.lookup (op =) trans (q, c, d) of
wenzelm@35115
   765
          NONE => raise Match
wenzelm@35115
   766
        | SOME (l, g) =>
wenzelm@49660
   767
            if matches_bound v t andalso not (contains_var v u) then mk (v, T) l u P
wenzelm@49660
   768
            else if matches_bound v u andalso not (contains_var v t) then mk (v, T) g t P
wenzelm@35115
   769
            else raise Match)
wenzelm@52143
   770
      | _ => raise Match));
wenzelm@21524
   771
in [tr' All_binder, tr' Ex_binder] end
wenzelm@60758
   772
\<close>
haftmann@21083
   773
haftmann@21083
   774
wenzelm@60758
   775
subsection \<open>Transitivity reasoning\<close>
haftmann@21383
   776
haftmann@25193
   777
context ord
haftmann@25193
   778
begin
haftmann@21383
   779
haftmann@25193
   780
lemma ord_le_eq_trans: "a \<le> b \<Longrightarrow> b = c \<Longrightarrow> a \<le> c"
haftmann@25193
   781
  by (rule subst)
haftmann@21383
   782
haftmann@25193
   783
lemma ord_eq_le_trans: "a = b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
haftmann@25193
   784
  by (rule ssubst)
haftmann@21383
   785
haftmann@25193
   786
lemma ord_less_eq_trans: "a < b \<Longrightarrow> b = c \<Longrightarrow> a < c"
haftmann@25193
   787
  by (rule subst)
haftmann@25193
   788
haftmann@25193
   789
lemma ord_eq_less_trans: "a = b \<Longrightarrow> b < c \<Longrightarrow> a < c"
haftmann@25193
   790
  by (rule ssubst)
haftmann@25193
   791
haftmann@25193
   792
end
haftmann@21383
   793
haftmann@21383
   794
lemma order_less_subst2: "(a::'a::order) < b ==> f b < (c::'c::order) ==>
haftmann@21383
   795
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   796
proof -
haftmann@21383
   797
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   798
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   799
  also assume "f b < c"
haftmann@34250
   800
  finally (less_trans) show ?thesis .
haftmann@21383
   801
qed
haftmann@21383
   802
haftmann@21383
   803
lemma order_less_subst1: "(a::'a::order) < f b ==> (b::'b::order) < c ==>
haftmann@21383
   804
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   805
proof -
haftmann@21383
   806
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   807
  assume "a < f b"
haftmann@21383
   808
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@34250
   809
  finally (less_trans) show ?thesis .
haftmann@21383
   810
qed
haftmann@21383
   811
haftmann@21383
   812
lemma order_le_less_subst2: "(a::'a::order) <= b ==> f b < (c::'c::order) ==>
haftmann@21383
   813
  (!!x y. x <= y ==> f x <= f y) ==> f a < c"
haftmann@21383
   814
proof -
haftmann@21383
   815
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   816
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   817
  also assume "f b < c"
haftmann@34250
   818
  finally (le_less_trans) show ?thesis .
haftmann@21383
   819
qed
haftmann@21383
   820
haftmann@21383
   821
lemma order_le_less_subst1: "(a::'a::order) <= f b ==> (b::'b::order) < c ==>
haftmann@21383
   822
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   823
proof -
haftmann@21383
   824
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   825
  assume "a <= f b"
haftmann@21383
   826
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@34250
   827
  finally (le_less_trans) show ?thesis .
haftmann@21383
   828
qed
haftmann@21383
   829
haftmann@21383
   830
lemma order_less_le_subst2: "(a::'a::order) < b ==> f b <= (c::'c::order) ==>
haftmann@21383
   831
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   832
proof -
haftmann@21383
   833
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   834
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   835
  also assume "f b <= c"
haftmann@34250
   836
  finally (less_le_trans) show ?thesis .
haftmann@21383
   837
qed
haftmann@21383
   838
haftmann@21383
   839
lemma order_less_le_subst1: "(a::'a::order) < f b ==> (b::'b::order) <= c ==>
haftmann@21383
   840
  (!!x y. x <= y ==> f x <= f y) ==> a < f c"
haftmann@21383
   841
proof -
haftmann@21383
   842
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   843
  assume "a < f b"
haftmann@21383
   844
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@34250
   845
  finally (less_le_trans) show ?thesis .
haftmann@21383
   846
qed
haftmann@21383
   847
haftmann@21383
   848
lemma order_subst1: "(a::'a::order) <= f b ==> (b::'b::order) <= c ==>
haftmann@21383
   849
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   850
proof -
haftmann@21383
   851
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   852
  assume "a <= f b"
haftmann@21383
   853
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   854
  finally (order_trans) show ?thesis .
haftmann@21383
   855
qed
haftmann@21383
   856
haftmann@21383
   857
lemma order_subst2: "(a::'a::order) <= b ==> f b <= (c::'c::order) ==>
haftmann@21383
   858
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   859
proof -
haftmann@21383
   860
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   861
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   862
  also assume "f b <= c"
haftmann@21383
   863
  finally (order_trans) show ?thesis .
haftmann@21383
   864
qed
haftmann@21383
   865
haftmann@21383
   866
lemma ord_le_eq_subst: "a <= b ==> f b = c ==>
haftmann@21383
   867
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   868
proof -
haftmann@21383
   869
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   870
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   871
  also assume "f b = c"
haftmann@21383
   872
  finally (ord_le_eq_trans) show ?thesis .
haftmann@21383
   873
qed
haftmann@21383
   874
haftmann@21383
   875
lemma ord_eq_le_subst: "a = f b ==> b <= c ==>
haftmann@21383
   876
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   877
proof -
haftmann@21383
   878
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   879
  assume "a = f b"
haftmann@21383
   880
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   881
  finally (ord_eq_le_trans) show ?thesis .
haftmann@21383
   882
qed
haftmann@21383
   883
haftmann@21383
   884
lemma ord_less_eq_subst: "a < b ==> f b = c ==>
haftmann@21383
   885
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   886
proof -
haftmann@21383
   887
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   888
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   889
  also assume "f b = c"
haftmann@21383
   890
  finally (ord_less_eq_trans) show ?thesis .
haftmann@21383
   891
qed
haftmann@21383
   892
haftmann@21383
   893
lemma ord_eq_less_subst: "a = f b ==> b < c ==>
haftmann@21383
   894
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   895
proof -
haftmann@21383
   896
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   897
  assume "a = f b"
haftmann@21383
   898
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@21383
   899
  finally (ord_eq_less_trans) show ?thesis .
haftmann@21383
   900
qed
haftmann@21383
   901
wenzelm@60758
   902
text \<open>
haftmann@21383
   903
  Note that this list of rules is in reverse order of priorities.
wenzelm@60758
   904
\<close>
haftmann@21383
   905
haftmann@27682
   906
lemmas [trans] =
haftmann@21383
   907
  order_less_subst2
haftmann@21383
   908
  order_less_subst1
haftmann@21383
   909
  order_le_less_subst2
haftmann@21383
   910
  order_le_less_subst1
haftmann@21383
   911
  order_less_le_subst2
haftmann@21383
   912
  order_less_le_subst1
haftmann@21383
   913
  order_subst2
haftmann@21383
   914
  order_subst1
haftmann@21383
   915
  ord_le_eq_subst
haftmann@21383
   916
  ord_eq_le_subst
haftmann@21383
   917
  ord_less_eq_subst
haftmann@21383
   918
  ord_eq_less_subst
haftmann@21383
   919
  forw_subst
haftmann@21383
   920
  back_subst
haftmann@21383
   921
  rev_mp
haftmann@21383
   922
  mp
haftmann@27682
   923
haftmann@27682
   924
lemmas (in order) [trans] =
haftmann@27682
   925
  neq_le_trans
haftmann@27682
   926
  le_neq_trans
haftmann@27682
   927
haftmann@27682
   928
lemmas (in preorder) [trans] =
haftmann@27682
   929
  less_trans
haftmann@27682
   930
  less_asym'
haftmann@27682
   931
  le_less_trans
haftmann@27682
   932
  less_le_trans
haftmann@21383
   933
  order_trans
haftmann@27682
   934
haftmann@27682
   935
lemmas (in order) [trans] =
haftmann@27682
   936
  antisym
haftmann@27682
   937
haftmann@27682
   938
lemmas (in ord) [trans] =
haftmann@27682
   939
  ord_le_eq_trans
haftmann@27682
   940
  ord_eq_le_trans
haftmann@27682
   941
  ord_less_eq_trans
haftmann@27682
   942
  ord_eq_less_trans
haftmann@27682
   943
haftmann@27682
   944
lemmas [trans] =
haftmann@27682
   945
  trans
haftmann@27682
   946
haftmann@27682
   947
lemmas order_trans_rules =
haftmann@27682
   948
  order_less_subst2
haftmann@27682
   949
  order_less_subst1
haftmann@27682
   950
  order_le_less_subst2
haftmann@27682
   951
  order_le_less_subst1
haftmann@27682
   952
  order_less_le_subst2
haftmann@27682
   953
  order_less_le_subst1
haftmann@27682
   954
  order_subst2
haftmann@27682
   955
  order_subst1
haftmann@27682
   956
  ord_le_eq_subst
haftmann@27682
   957
  ord_eq_le_subst
haftmann@27682
   958
  ord_less_eq_subst
haftmann@27682
   959
  ord_eq_less_subst
haftmann@27682
   960
  forw_subst
haftmann@27682
   961
  back_subst
haftmann@27682
   962
  rev_mp
haftmann@27682
   963
  mp
haftmann@27682
   964
  neq_le_trans
haftmann@27682
   965
  le_neq_trans
haftmann@27682
   966
  less_trans
haftmann@27682
   967
  less_asym'
haftmann@27682
   968
  le_less_trans
haftmann@27682
   969
  less_le_trans
haftmann@27682
   970
  order_trans
haftmann@27682
   971
  antisym
haftmann@21383
   972
  ord_le_eq_trans
haftmann@21383
   973
  ord_eq_le_trans
haftmann@21383
   974
  ord_less_eq_trans
haftmann@21383
   975
  ord_eq_less_trans
haftmann@21383
   976
  trans
haftmann@21383
   977
wenzelm@60758
   978
text \<open>These support proving chains of decreasing inequalities
wenzelm@60758
   979
    a >= b >= c ... in Isar proofs.\<close>
haftmann@21083
   980
blanchet@45221
   981
lemma xt1 [no_atp]:
haftmann@21083
   982
  "a = b ==> b > c ==> a > c"
haftmann@21083
   983
  "a > b ==> b = c ==> a > c"
haftmann@21083
   984
  "a = b ==> b >= c ==> a >= c"
haftmann@21083
   985
  "a >= b ==> b = c ==> a >= c"
haftmann@21083
   986
  "(x::'a::order) >= y ==> y >= x ==> x = y"
haftmann@21083
   987
  "(x::'a::order) >= y ==> y >= z ==> x >= z"
haftmann@21083
   988
  "(x::'a::order) > y ==> y >= z ==> x > z"
haftmann@21083
   989
  "(x::'a::order) >= y ==> y > z ==> x > z"
wenzelm@23417
   990
  "(a::'a::order) > b ==> b > a ==> P"
haftmann@21083
   991
  "(x::'a::order) > y ==> y > z ==> x > z"
haftmann@21083
   992
  "(a::'a::order) >= b ==> a ~= b ==> a > b"
haftmann@21083
   993
  "(a::'a::order) ~= b ==> a >= b ==> a > b"
lp15@61824
   994
  "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   995
  "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   996
  "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
   997
  "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@25076
   998
  by auto
haftmann@21083
   999
blanchet@45221
  1000
lemma xt2 [no_atp]:
haftmann@21083
  1001
  "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
  1002
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
  1003
blanchet@45221
  1004
lemma xt3 [no_atp]: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==>
haftmann@21083
  1005
    (!!x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@21083
  1006
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
  1007
blanchet@45221
  1008
lemma xt4 [no_atp]: "(a::'a::order) > f b ==> (b::'b::order) >= c ==>
haftmann@21083
  1009
  (!!x y. x >= y ==> f x >= f y) ==> a > f c"
haftmann@21083
  1010
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
  1011
blanchet@45221
  1012
lemma xt5 [no_atp]: "(a::'a::order) > b ==> (f b::'b::order) >= c==>
haftmann@21083
  1013
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
  1014
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
  1015
blanchet@45221
  1016
lemma xt6 [no_atp]: "(a::'a::order) >= f b ==> b > c ==>
haftmann@21083
  1017
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
  1018
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
  1019
blanchet@45221
  1020
lemma xt7 [no_atp]: "(a::'a::order) >= b ==> (f b::'b::order) > c ==>
haftmann@21083
  1021
    (!!x y. x >= y ==> f x >= f y) ==> f a > c"
haftmann@21083
  1022
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
  1023
blanchet@45221
  1024
lemma xt8 [no_atp]: "(a::'a::order) > f b ==> (b::'b::order) > c ==>
haftmann@21083
  1025
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
  1026
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
  1027
blanchet@45221
  1028
lemma xt9 [no_atp]: "(a::'a::order) > b ==> (f b::'b::order) > c ==>
haftmann@21083
  1029
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
  1030
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
  1031
blanchet@54147
  1032
lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9
haftmann@21083
  1033
lp15@61824
  1034
(*
haftmann@21083
  1035
  Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands
haftmann@21083
  1036
  for the wrong thing in an Isar proof.
haftmann@21083
  1037
lp15@61824
  1038
  The extra transitivity rules can be used as follows:
haftmann@21083
  1039
haftmann@21083
  1040
lemma "(a::'a::order) > z"
haftmann@21083
  1041
proof -
haftmann@21083
  1042
  have "a >= b" (is "_ >= ?rhs")
haftmann@21083
  1043
    sorry
haftmann@21083
  1044
  also have "?rhs >= c" (is "_ >= ?rhs")
haftmann@21083
  1045
    sorry
haftmann@21083
  1046
  also (xtrans) have "?rhs = d" (is "_ = ?rhs")
haftmann@21083
  1047
    sorry
haftmann@21083
  1048
  also (xtrans) have "?rhs >= e" (is "_ >= ?rhs")
haftmann@21083
  1049
    sorry
haftmann@21083
  1050
  also (xtrans) have "?rhs > f" (is "_ > ?rhs")
haftmann@21083
  1051
    sorry
haftmann@21083
  1052
  also (xtrans) have "?rhs > z"
haftmann@21083
  1053
    sorry
haftmann@21083
  1054
  finally (xtrans) show ?thesis .
haftmann@21083
  1055
qed
haftmann@21083
  1056
haftmann@21083
  1057
  Alternatively, one can use "declare xtrans [trans]" and then
haftmann@21083
  1058
  leave out the "(xtrans)" above.
haftmann@21083
  1059
*)
haftmann@21083
  1060
haftmann@23881
  1061
wenzelm@60758
  1062
subsection \<open>Monotonicity\<close>
haftmann@21083
  1063
haftmann@25076
  1064
context order
haftmann@25076
  1065
begin
haftmann@25076
  1066
wenzelm@61076
  1067
definition mono :: "('a \<Rightarrow> 'b::order) \<Rightarrow> bool" where
haftmann@25076
  1068
  "mono f \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> f x \<le> f y)"
haftmann@25076
  1069
haftmann@25076
  1070
lemma monoI [intro?]:
wenzelm@61076
  1071
  fixes f :: "'a \<Rightarrow> 'b::order"
haftmann@25076
  1072
  shows "(\<And>x y. x \<le> y \<Longrightarrow> f x \<le> f y) \<Longrightarrow> mono f"
haftmann@25076
  1073
  unfolding mono_def by iprover
haftmann@21216
  1074
haftmann@25076
  1075
lemma monoD [dest?]:
wenzelm@61076
  1076
  fixes f :: "'a \<Rightarrow> 'b::order"
haftmann@25076
  1077
  shows "mono f \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
haftmann@25076
  1078
  unfolding mono_def by iprover
haftmann@25076
  1079
haftmann@51263
  1080
lemma monoE:
wenzelm@61076
  1081
  fixes f :: "'a \<Rightarrow> 'b::order"
haftmann@51263
  1082
  assumes "mono f"
haftmann@51263
  1083
  assumes "x \<le> y"
haftmann@51263
  1084
  obtains "f x \<le> f y"
haftmann@51263
  1085
proof
haftmann@51263
  1086
  from assms show "f x \<le> f y" by (simp add: mono_def)
haftmann@51263
  1087
qed
haftmann@51263
  1088
wenzelm@61076
  1089
definition antimono :: "('a \<Rightarrow> 'b::order) \<Rightarrow> bool" where
hoelzl@56020
  1090
  "antimono f \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> f x \<ge> f y)"
hoelzl@56020
  1091
hoelzl@56020
  1092
lemma antimonoI [intro?]:
wenzelm@61076
  1093
  fixes f :: "'a \<Rightarrow> 'b::order"
hoelzl@56020
  1094
  shows "(\<And>x y. x \<le> y \<Longrightarrow> f x \<ge> f y) \<Longrightarrow> antimono f"
hoelzl@56020
  1095
  unfolding antimono_def by iprover
hoelzl@56020
  1096
hoelzl@56020
  1097
lemma antimonoD [dest?]:
wenzelm@61076
  1098
  fixes f :: "'a \<Rightarrow> 'b::order"
hoelzl@56020
  1099
  shows "antimono f \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<ge> f y"
hoelzl@56020
  1100
  unfolding antimono_def by iprover
hoelzl@56020
  1101
hoelzl@56020
  1102
lemma antimonoE:
wenzelm@61076
  1103
  fixes f :: "'a \<Rightarrow> 'b::order"
hoelzl@56020
  1104
  assumes "antimono f"
hoelzl@56020
  1105
  assumes "x \<le> y"
hoelzl@56020
  1106
  obtains "f x \<ge> f y"
hoelzl@56020
  1107
proof
hoelzl@56020
  1108
  from assms show "f x \<ge> f y" by (simp add: antimono_def)
hoelzl@56020
  1109
qed
hoelzl@56020
  1110
wenzelm@61076
  1111
definition strict_mono :: "('a \<Rightarrow> 'b::order) \<Rightarrow> bool" where
haftmann@30298
  1112
  "strict_mono f \<longleftrightarrow> (\<forall>x y. x < y \<longrightarrow> f x < f y)"
haftmann@30298
  1113
haftmann@30298
  1114
lemma strict_monoI [intro?]:
haftmann@30298
  1115
  assumes "\<And>x y. x < y \<Longrightarrow> f x < f y"
haftmann@30298
  1116
  shows "strict_mono f"
haftmann@30298
  1117
  using assms unfolding strict_mono_def by auto
haftmann@30298
  1118
haftmann@30298
  1119
lemma strict_monoD [dest?]:
haftmann@30298
  1120
  "strict_mono f \<Longrightarrow> x < y \<Longrightarrow> f x < f y"
haftmann@30298
  1121
  unfolding strict_mono_def by auto
haftmann@30298
  1122
haftmann@30298
  1123
lemma strict_mono_mono [dest?]:
haftmann@30298
  1124
  assumes "strict_mono f"
haftmann@30298
  1125
  shows "mono f"
haftmann@30298
  1126
proof (rule monoI)
haftmann@30298
  1127
  fix x y
haftmann@30298
  1128
  assume "x \<le> y"
haftmann@30298
  1129
  show "f x \<le> f y"
haftmann@30298
  1130
  proof (cases "x = y")
haftmann@30298
  1131
    case True then show ?thesis by simp
haftmann@30298
  1132
  next
wenzelm@60758
  1133
    case False with \<open>x \<le> y\<close> have "x < y" by simp
haftmann@30298
  1134
    with assms strict_monoD have "f x < f y" by auto
haftmann@30298
  1135
    then show ?thesis by simp
haftmann@30298
  1136
  qed
haftmann@30298
  1137
qed
haftmann@30298
  1138
haftmann@25076
  1139
end
haftmann@25076
  1140
haftmann@25076
  1141
context linorder
haftmann@25076
  1142
begin
haftmann@25076
  1143
haftmann@51263
  1144
lemma mono_invE:
wenzelm@61076
  1145
  fixes f :: "'a \<Rightarrow> 'b::order"
haftmann@51263
  1146
  assumes "mono f"
haftmann@51263
  1147
  assumes "f x < f y"
haftmann@51263
  1148
  obtains "x \<le> y"
haftmann@51263
  1149
proof
haftmann@51263
  1150
  show "x \<le> y"
haftmann@51263
  1151
  proof (rule ccontr)
haftmann@51263
  1152
    assume "\<not> x \<le> y"
haftmann@51263
  1153
    then have "y \<le> x" by simp
wenzelm@60758
  1154
    with \<open>mono f\<close> obtain "f y \<le> f x" by (rule monoE)
wenzelm@60758
  1155
    with \<open>f x < f y\<close> show False by simp
haftmann@51263
  1156
  qed
haftmann@51263
  1157
qed
haftmann@51263
  1158
haftmann@30298
  1159
lemma strict_mono_eq:
haftmann@30298
  1160
  assumes "strict_mono f"
haftmann@30298
  1161
  shows "f x = f y \<longleftrightarrow> x = y"
haftmann@30298
  1162
proof
haftmann@30298
  1163
  assume "f x = f y"
haftmann@30298
  1164
  show "x = y" proof (cases x y rule: linorder_cases)
haftmann@30298
  1165
    case less with assms strict_monoD have "f x < f y" by auto
wenzelm@60758
  1166
    with \<open>f x = f y\<close> show ?thesis by simp
haftmann@30298
  1167
  next
haftmann@30298
  1168
    case equal then show ?thesis .
haftmann@30298
  1169
  next
haftmann@30298
  1170
    case greater with assms strict_monoD have "f y < f x" by auto
wenzelm@60758
  1171
    with \<open>f x = f y\<close> show ?thesis by simp
haftmann@30298
  1172
  qed
haftmann@30298
  1173
qed simp
haftmann@30298
  1174
haftmann@30298
  1175
lemma strict_mono_less_eq:
haftmann@30298
  1176
  assumes "strict_mono f"
haftmann@30298
  1177
  shows "f x \<le> f y \<longleftrightarrow> x \<le> y"
haftmann@30298
  1178
proof
haftmann@30298
  1179
  assume "x \<le> y"
haftmann@30298
  1180
  with assms strict_mono_mono monoD show "f x \<le> f y" by auto
haftmann@30298
  1181
next
haftmann@30298
  1182
  assume "f x \<le> f y"
haftmann@30298
  1183
  show "x \<le> y" proof (rule ccontr)
haftmann@30298
  1184
    assume "\<not> x \<le> y" then have "y < x" by simp
haftmann@30298
  1185
    with assms strict_monoD have "f y < f x" by auto
wenzelm@60758
  1186
    with \<open>f x \<le> f y\<close> show False by simp
haftmann@30298
  1187
  qed
haftmann@30298
  1188
qed
lp15@61824
  1189
haftmann@30298
  1190
lemma strict_mono_less:
haftmann@30298
  1191
  assumes "strict_mono f"
haftmann@30298
  1192
  shows "f x < f y \<longleftrightarrow> x < y"
haftmann@30298
  1193
  using assms
haftmann@30298
  1194
    by (auto simp add: less_le Orderings.less_le strict_mono_eq strict_mono_less_eq)
haftmann@30298
  1195
haftmann@54860
  1196
end
haftmann@54860
  1197
haftmann@54860
  1198
wenzelm@60758
  1199
subsection \<open>min and max -- fundamental\<close>
haftmann@54860
  1200
haftmann@54860
  1201
definition (in ord) min :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@54860
  1202
  "min a b = (if a \<le> b then a else b)"
haftmann@54860
  1203
haftmann@54860
  1204
definition (in ord) max :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@54860
  1205
  "max a b = (if a \<le> b then b else a)"
haftmann@54860
  1206
noschinl@45931
  1207
lemma min_absorb1: "x \<le> y \<Longrightarrow> min x y = x"
haftmann@54861
  1208
  by (simp add: min_def)
haftmann@21383
  1209
haftmann@54857
  1210
lemma max_absorb2: "x \<le> y \<Longrightarrow> max x y = y"
haftmann@54861
  1211
  by (simp add: max_def)
haftmann@21383
  1212
wenzelm@61076
  1213
lemma min_absorb2: "(y::'a::order) \<le> x \<Longrightarrow> min x y = y"
haftmann@54861
  1214
  by (simp add:min_def)
noschinl@45893
  1215
wenzelm@61076
  1216
lemma max_absorb1: "(y::'a::order) \<le> x \<Longrightarrow> max x y = x"
haftmann@54861
  1217
  by (simp add: max_def)
noschinl@45893
  1218
Andreas@61630
  1219
lemma max_min_same [simp]:
Andreas@61630
  1220
  fixes x y :: "'a :: linorder"
Andreas@61630
  1221
  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
  1222
by(auto simp add: max_def min_def)
noschinl@45893
  1223
wenzelm@60758
  1224
subsection \<open>(Unique) top and bottom elements\<close>
haftmann@28685
  1225
haftmann@52729
  1226
class bot =
haftmann@43853
  1227
  fixes bot :: 'a ("\<bottom>")
haftmann@52729
  1228
haftmann@52729
  1229
class order_bot = order + bot +
haftmann@51487
  1230
  assumes bot_least: "\<bottom> \<le> a"
haftmann@54868
  1231
begin
haftmann@51487
  1232
wenzelm@61605
  1233
sublocale bot: ordering_top greater_eq greater bot
wenzelm@61169
  1234
  by standard (fact bot_least)
haftmann@51487
  1235
haftmann@43853
  1236
lemma le_bot:
haftmann@43853
  1237
  "a \<le> \<bottom> \<Longrightarrow> a = \<bottom>"
haftmann@51487
  1238
  by (fact bot.extremum_uniqueI)
haftmann@43853
  1239
haftmann@43816
  1240
lemma bot_unique:
haftmann@43853
  1241
  "a \<le> \<bottom> \<longleftrightarrow> a = \<bottom>"
haftmann@51487
  1242
  by (fact bot.extremum_unique)
haftmann@43853
  1243
haftmann@51487
  1244
lemma not_less_bot:
haftmann@51487
  1245
  "\<not> a < \<bottom>"
haftmann@51487
  1246
  by (fact bot.extremum_strict)
haftmann@43816
  1247
haftmann@43814
  1248
lemma bot_less:
haftmann@43853
  1249
  "a \<noteq> \<bottom> \<longleftrightarrow> \<bottom> < a"
haftmann@51487
  1250
  by (fact bot.not_eq_extremum)
haftmann@43814
  1251
haftmann@43814
  1252
end
haftmann@41082
  1253
haftmann@52729
  1254
class top =
haftmann@43853
  1255
  fixes top :: 'a ("\<top>")
haftmann@52729
  1256
haftmann@52729
  1257
class order_top = order + top +
haftmann@51487
  1258
  assumes top_greatest: "a \<le> \<top>"
haftmann@54868
  1259
begin
haftmann@51487
  1260
wenzelm@61605
  1261
sublocale top: ordering_top less_eq less top
wenzelm@61169
  1262
  by standard (fact top_greatest)
haftmann@51487
  1263
haftmann@43853
  1264
lemma top_le:
haftmann@43853
  1265
  "\<top> \<le> a \<Longrightarrow> a = \<top>"
haftmann@51487
  1266
  by (fact top.extremum_uniqueI)
haftmann@43853
  1267
haftmann@43816
  1268
lemma top_unique:
haftmann@43853
  1269
  "\<top> \<le> a \<longleftrightarrow> a = \<top>"
haftmann@51487
  1270
  by (fact top.extremum_unique)
haftmann@43853
  1271
haftmann@51487
  1272
lemma not_top_less:
haftmann@51487
  1273
  "\<not> \<top> < a"
haftmann@51487
  1274
  by (fact top.extremum_strict)
haftmann@43816
  1275
haftmann@43814
  1276
lemma less_top:
haftmann@43853
  1277
  "a \<noteq> \<top> \<longleftrightarrow> a < \<top>"
haftmann@51487
  1278
  by (fact top.not_eq_extremum)
haftmann@43814
  1279
haftmann@43814
  1280
end
haftmann@28685
  1281
haftmann@28685
  1282
wenzelm@60758
  1283
subsection \<open>Dense orders\<close>
haftmann@27823
  1284
hoelzl@53216
  1285
class dense_order = order +
hoelzl@51329
  1286
  assumes dense: "x < y \<Longrightarrow> (\<exists>z. x < z \<and> z < y)"
hoelzl@51329
  1287
hoelzl@53216
  1288
class dense_linorder = linorder + dense_order
hoelzl@35579
  1289
begin
haftmann@27823
  1290
hoelzl@35579
  1291
lemma dense_le:
hoelzl@35579
  1292
  fixes y z :: 'a
hoelzl@35579
  1293
  assumes "\<And>x. x < y \<Longrightarrow> x \<le> z"
hoelzl@35579
  1294
  shows "y \<le> z"
hoelzl@35579
  1295
proof (rule ccontr)
hoelzl@35579
  1296
  assume "\<not> ?thesis"
hoelzl@35579
  1297
  hence "z < y" by simp
hoelzl@35579
  1298
  from dense[OF this]
hoelzl@35579
  1299
  obtain x where "x < y" and "z < x" by safe
wenzelm@60758
  1300
  moreover have "x \<le> z" using assms[OF \<open>x < y\<close>] .
hoelzl@35579
  1301
  ultimately show False by auto
hoelzl@35579
  1302
qed
hoelzl@35579
  1303
hoelzl@35579
  1304
lemma dense_le_bounded:
hoelzl@35579
  1305
  fixes x y z :: 'a
hoelzl@35579
  1306
  assumes "x < y"
hoelzl@35579
  1307
  assumes *: "\<And>w. \<lbrakk> x < w ; w < y \<rbrakk> \<Longrightarrow> w \<le> z"
hoelzl@35579
  1308
  shows "y \<le> z"
hoelzl@35579
  1309
proof (rule dense_le)
hoelzl@35579
  1310
  fix w assume "w < y"
wenzelm@60758
  1311
  from dense[OF \<open>x < y\<close>] obtain u where "x < u" "u < y" by safe
hoelzl@35579
  1312
  from linear[of u w]
hoelzl@35579
  1313
  show "w \<le> z"
hoelzl@35579
  1314
  proof (rule disjE)
hoelzl@35579
  1315
    assume "u \<le> w"
wenzelm@60758
  1316
    from less_le_trans[OF \<open>x < u\<close> \<open>u \<le> w\<close>] \<open>w < y\<close>
hoelzl@35579
  1317
    show "w \<le> z" by (rule *)
hoelzl@35579
  1318
  next
hoelzl@35579
  1319
    assume "w \<le> u"
wenzelm@60758
  1320
    from \<open>w \<le> u\<close> *[OF \<open>x < u\<close> \<open>u < y\<close>]
hoelzl@35579
  1321
    show "w \<le> z" by (rule order_trans)
hoelzl@35579
  1322
  qed
hoelzl@35579
  1323
qed
hoelzl@35579
  1324
hoelzl@51329
  1325
lemma dense_ge:
hoelzl@51329
  1326
  fixes y z :: 'a
hoelzl@51329
  1327
  assumes "\<And>x. z < x \<Longrightarrow> y \<le> x"
hoelzl@51329
  1328
  shows "y \<le> z"
hoelzl@51329
  1329
proof (rule ccontr)
hoelzl@51329
  1330
  assume "\<not> ?thesis"
hoelzl@51329
  1331
  hence "z < y" by simp
hoelzl@51329
  1332
  from dense[OF this]
hoelzl@51329
  1333
  obtain x where "x < y" and "z < x" by safe
wenzelm@60758
  1334
  moreover have "y \<le> x" using assms[OF \<open>z < x\<close>] .
hoelzl@51329
  1335
  ultimately show False by auto
hoelzl@51329
  1336
qed
hoelzl@51329
  1337
hoelzl@51329
  1338
lemma dense_ge_bounded:
hoelzl@51329
  1339
  fixes x y z :: 'a
hoelzl@51329
  1340
  assumes "z < x"
hoelzl@51329
  1341
  assumes *: "\<And>w. \<lbrakk> z < w ; w < x \<rbrakk> \<Longrightarrow> y \<le> w"
hoelzl@51329
  1342
  shows "y \<le> z"
hoelzl@51329
  1343
proof (rule dense_ge)
hoelzl@51329
  1344
  fix w assume "z < w"
wenzelm@60758
  1345
  from dense[OF \<open>z < x\<close>] obtain u where "z < u" "u < x" by safe
hoelzl@51329
  1346
  from linear[of u w]
hoelzl@51329
  1347
  show "y \<le> w"
hoelzl@51329
  1348
  proof (rule disjE)
hoelzl@51329
  1349
    assume "w \<le> u"
wenzelm@60758
  1350
    from \<open>z < w\<close> le_less_trans[OF \<open>w \<le> u\<close> \<open>u < x\<close>]
hoelzl@51329
  1351
    show "y \<le> w" by (rule *)
hoelzl@51329
  1352
  next
hoelzl@51329
  1353
    assume "u \<le> w"
wenzelm@60758
  1354
    from *[OF \<open>z < u\<close> \<open>u < x\<close>] \<open>u \<le> w\<close>
hoelzl@51329
  1355
    show "y \<le> w" by (rule order_trans)
hoelzl@51329
  1356
  qed
hoelzl@51329
  1357
qed
hoelzl@51329
  1358
hoelzl@35579
  1359
end
haftmann@27823
  1360
lp15@61824
  1361
class no_top = order +
hoelzl@51329
  1362
  assumes gt_ex: "\<exists>y. x < y"
hoelzl@51329
  1363
lp15@61824
  1364
class no_bot = order +
hoelzl@51329
  1365
  assumes lt_ex: "\<exists>y. y < x"
hoelzl@51329
  1366
hoelzl@53216
  1367
class unbounded_dense_linorder = dense_linorder + no_top + no_bot
hoelzl@51329
  1368
haftmann@51546
  1369
wenzelm@60758
  1370
subsection \<open>Wellorders\<close>
haftmann@27823
  1371
haftmann@27823
  1372
class wellorder = linorder +
haftmann@27823
  1373
  assumes less_induct [case_names less]: "(\<And>x. (\<And>y. y < x \<Longrightarrow> P y) \<Longrightarrow> P x) \<Longrightarrow> P a"
haftmann@27823
  1374
begin
haftmann@27823
  1375
haftmann@27823
  1376
lemma wellorder_Least_lemma:
haftmann@27823
  1377
  fixes k :: 'a
haftmann@27823
  1378
  assumes "P k"
haftmann@34250
  1379
  shows LeastI: "P (LEAST x. P x)" and Least_le: "(LEAST x. P x) \<le> k"
haftmann@27823
  1380
proof -
haftmann@27823
  1381
  have "P (LEAST x. P x) \<and> (LEAST x. P x) \<le> k"
haftmann@27823
  1382
  using assms proof (induct k rule: less_induct)
haftmann@27823
  1383
    case (less x) then have "P x" by simp
haftmann@27823
  1384
    show ?case proof (rule classical)
haftmann@27823
  1385
      assume assm: "\<not> (P (LEAST a. P a) \<and> (LEAST a. P a) \<le> x)"
haftmann@27823
  1386
      have "\<And>y. P y \<Longrightarrow> x \<le> y"
haftmann@27823
  1387
      proof (rule classical)
haftmann@27823
  1388
        fix y
hoelzl@38705
  1389
        assume "P y" and "\<not> x \<le> y"
haftmann@27823
  1390
        with less have "P (LEAST a. P a)" and "(LEAST a. P a) \<le> y"
haftmann@27823
  1391
          by (auto simp add: not_le)
haftmann@27823
  1392
        with assm have "x < (LEAST a. P a)" and "(LEAST a. P a) \<le> y"
haftmann@27823
  1393
          by auto
haftmann@27823
  1394
        then show "x \<le> y" by auto
haftmann@27823
  1395
      qed
wenzelm@60758
  1396
      with \<open>P x\<close> have Least: "(LEAST a. P a) = x"
haftmann@27823
  1397
        by (rule Least_equality)
wenzelm@60758
  1398
      with \<open>P x\<close> show ?thesis by simp
haftmann@27823
  1399
    qed
haftmann@27823
  1400
  qed
haftmann@27823
  1401
  then show "P (LEAST x. P x)" and "(LEAST x. P x) \<le> k" by auto
haftmann@27823
  1402
qed
haftmann@27823
  1403
wenzelm@61799
  1404
\<comment> "The following 3 lemmas are due to Brian Huffman"
haftmann@27823
  1405
lemma LeastI_ex: "\<exists>x. P x \<Longrightarrow> P (Least P)"
haftmann@27823
  1406
  by (erule exE) (erule LeastI)
haftmann@27823
  1407
haftmann@27823
  1408
lemma LeastI2:
haftmann@27823
  1409
  "P a \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> Q (Least P)"
haftmann@27823
  1410
  by (blast intro: LeastI)
haftmann@27823
  1411
haftmann@27823
  1412
lemma LeastI2_ex:
haftmann@27823
  1413
  "\<exists>a. P a \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> Q (Least P)"
haftmann@27823
  1414
  by (blast intro: LeastI_ex)
haftmann@27823
  1415
hoelzl@38705
  1416
lemma LeastI2_wellorder:
hoelzl@38705
  1417
  assumes "P a"
hoelzl@38705
  1418
  and "\<And>a. \<lbrakk> P a; \<forall>b. P b \<longrightarrow> a \<le> b \<rbrakk> \<Longrightarrow> Q a"
hoelzl@38705
  1419
  shows "Q (Least P)"
hoelzl@38705
  1420
proof (rule LeastI2_order)
wenzelm@60758
  1421
  show "P (Least P)" using \<open>P a\<close> by (rule LeastI)
hoelzl@38705
  1422
next
hoelzl@38705
  1423
  fix y assume "P y" thus "Least P \<le> y" by (rule Least_le)
hoelzl@38705
  1424
next
hoelzl@38705
  1425
  fix x assume "P x" "\<forall>y. P y \<longrightarrow> x \<le> y" thus "Q x" by (rule assms(2))
hoelzl@38705
  1426
qed
hoelzl@38705
  1427
lp15@61699
  1428
lemma LeastI2_wellorder_ex:
lp15@61699
  1429
  assumes "\<exists>x. P x"
lp15@61699
  1430
  and "\<And>a. \<lbrakk> P a; \<forall>b. P b \<longrightarrow> a \<le> b \<rbrakk> \<Longrightarrow> Q a"
lp15@61699
  1431
  shows "Q (Least P)"
lp15@61699
  1432
using assms by clarify (blast intro!: LeastI2_wellorder)
lp15@61699
  1433
haftmann@27823
  1434
lemma not_less_Least: "k < (LEAST x. P x) \<Longrightarrow> \<not> P k"
lp15@61699
  1435
apply (simp add: not_le [symmetric])
haftmann@27823
  1436
apply (erule contrapos_nn)
haftmann@27823
  1437
apply (erule Least_le)
haftmann@27823
  1438
done
haftmann@27823
  1439
hoelzl@38705
  1440
end
haftmann@27823
  1441
haftmann@28685
  1442
wenzelm@60758
  1443
subsection \<open>Order on @{typ bool}\<close>
haftmann@28685
  1444
haftmann@52729
  1445
instantiation bool :: "{order_bot, order_top, linorder}"
haftmann@28685
  1446
begin
haftmann@28685
  1447
haftmann@28685
  1448
definition
haftmann@41080
  1449
  le_bool_def [simp]: "P \<le> Q \<longleftrightarrow> P \<longrightarrow> Q"
haftmann@28685
  1450
haftmann@28685
  1451
definition
wenzelm@61076
  1452
  [simp]: "(P::bool) < Q \<longleftrightarrow> \<not> P \<and> Q"
haftmann@28685
  1453
haftmann@28685
  1454
definition
haftmann@46631
  1455
  [simp]: "\<bottom> \<longleftrightarrow> False"
haftmann@28685
  1456
haftmann@28685
  1457
definition
haftmann@46631
  1458
  [simp]: "\<top> \<longleftrightarrow> True"
haftmann@28685
  1459
haftmann@28685
  1460
instance proof
haftmann@41080
  1461
qed auto
haftmann@28685
  1462
nipkow@15524
  1463
end
haftmann@28685
  1464
haftmann@28685
  1465
lemma le_boolI: "(P \<Longrightarrow> Q) \<Longrightarrow> P \<le> Q"
haftmann@41080
  1466
  by simp
haftmann@28685
  1467
haftmann@28685
  1468
lemma le_boolI': "P \<longrightarrow> Q \<Longrightarrow> P \<le> Q"
haftmann@41080
  1469
  by simp
haftmann@28685
  1470
haftmann@28685
  1471
lemma le_boolE: "P \<le> Q \<Longrightarrow> P \<Longrightarrow> (Q \<Longrightarrow> R) \<Longrightarrow> R"
haftmann@41080
  1472
  by simp
haftmann@28685
  1473
haftmann@28685
  1474
lemma le_boolD: "P \<le> Q \<Longrightarrow> P \<longrightarrow> Q"
haftmann@41080
  1475
  by simp
haftmann@32899
  1476
haftmann@46631
  1477
lemma bot_boolE: "\<bottom> \<Longrightarrow> P"
haftmann@41080
  1478
  by simp
haftmann@32899
  1479
haftmann@46631
  1480
lemma top_boolI: \<top>
haftmann@41080
  1481
  by simp
haftmann@28685
  1482
haftmann@28685
  1483
lemma [code]:
haftmann@28685
  1484
  "False \<le> b \<longleftrightarrow> True"
haftmann@28685
  1485
  "True \<le> b \<longleftrightarrow> b"
haftmann@28685
  1486
  "False < b \<longleftrightarrow> b"
haftmann@28685
  1487
  "True < b \<longleftrightarrow> False"
haftmann@41080
  1488
  by simp_all
haftmann@28685
  1489
haftmann@28685
  1490
wenzelm@60758
  1491
subsection \<open>Order on @{typ "_ \<Rightarrow> _"}\<close>
haftmann@28685
  1492
haftmann@28685
  1493
instantiation "fun" :: (type, ord) ord
haftmann@28685
  1494
begin
haftmann@28685
  1495
haftmann@28685
  1496
definition
haftmann@37767
  1497
  le_fun_def: "f \<le> g \<longleftrightarrow> (\<forall>x. f x \<le> g x)"
haftmann@28685
  1498
haftmann@28685
  1499
definition
wenzelm@61076
  1500
  "(f::'a \<Rightarrow> 'b) < g \<longleftrightarrow> f \<le> g \<and> \<not> (g \<le> f)"
haftmann@28685
  1501
haftmann@28685
  1502
instance ..
haftmann@28685
  1503
haftmann@28685
  1504
end
haftmann@28685
  1505
haftmann@28685
  1506
instance "fun" :: (type, preorder) preorder proof
haftmann@28685
  1507
qed (auto simp add: le_fun_def less_fun_def
huffman@44921
  1508
  intro: order_trans antisym)
haftmann@28685
  1509
haftmann@28685
  1510
instance "fun" :: (type, order) order proof
huffman@44921
  1511
qed (auto simp add: le_fun_def intro: antisym)
haftmann@28685
  1512
haftmann@41082
  1513
instantiation "fun" :: (type, bot) bot
haftmann@41082
  1514
begin
haftmann@41082
  1515
haftmann@41082
  1516
definition
haftmann@46631
  1517
  "\<bottom> = (\<lambda>x. \<bottom>)"
haftmann@41082
  1518
haftmann@52729
  1519
instance ..
haftmann@52729
  1520
haftmann@52729
  1521
end
haftmann@52729
  1522
haftmann@52729
  1523
instantiation "fun" :: (type, order_bot) order_bot
haftmann@52729
  1524
begin
haftmann@52729
  1525
haftmann@49769
  1526
lemma bot_apply [simp, code]:
haftmann@46631
  1527
  "\<bottom> x = \<bottom>"
haftmann@41082
  1528
  by (simp add: bot_fun_def)
haftmann@41082
  1529
haftmann@41082
  1530
instance proof
noschinl@46884
  1531
qed (simp add: le_fun_def)
haftmann@41082
  1532
haftmann@41082
  1533
end
haftmann@41082
  1534
haftmann@28685
  1535
instantiation "fun" :: (type, top) top
haftmann@28685
  1536
begin
haftmann@28685
  1537
haftmann@28685
  1538
definition
haftmann@46631
  1539
  [no_atp]: "\<top> = (\<lambda>x. \<top>)"
haftmann@28685
  1540
haftmann@52729
  1541
instance ..
haftmann@52729
  1542
haftmann@52729
  1543
end
haftmann@52729
  1544
haftmann@52729
  1545
instantiation "fun" :: (type, order_top) order_top
haftmann@52729
  1546
begin
haftmann@52729
  1547
haftmann@49769
  1548
lemma top_apply [simp, code]:
haftmann@46631
  1549
  "\<top> x = \<top>"
haftmann@41080
  1550
  by (simp add: top_fun_def)
haftmann@41080
  1551
haftmann@28685
  1552
instance proof
noschinl@46884
  1553
qed (simp add: le_fun_def)
haftmann@28685
  1554
haftmann@28685
  1555
end
haftmann@28685
  1556
haftmann@28685
  1557
lemma le_funI: "(\<And>x. f x \<le> g x) \<Longrightarrow> f \<le> g"
haftmann@28685
  1558
  unfolding le_fun_def by simp
haftmann@28685
  1559
haftmann@28685
  1560
lemma le_funE: "f \<le> g \<Longrightarrow> (f x \<le> g x \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@28685
  1561
  unfolding le_fun_def by simp
haftmann@28685
  1562
haftmann@28685
  1563
lemma le_funD: "f \<le> g \<Longrightarrow> f x \<le> g x"
haftmann@54860
  1564
  by (rule le_funE)
haftmann@28685
  1565
hoelzl@59000
  1566
lemma mono_compose: "mono Q \<Longrightarrow> mono (\<lambda>i x. Q i (f x))"
hoelzl@59000
  1567
  unfolding mono_def le_fun_def by auto
hoelzl@59000
  1568
haftmann@34250
  1569
wenzelm@60758
  1570
subsection \<open>Order on unary and binary predicates\<close>
haftmann@46631
  1571
haftmann@46631
  1572
lemma predicate1I:
haftmann@46631
  1573
  assumes PQ: "\<And>x. P x \<Longrightarrow> Q x"
haftmann@46631
  1574
  shows "P \<le> Q"
haftmann@46631
  1575
  apply (rule le_funI)
haftmann@46631
  1576
  apply (rule le_boolI)
haftmann@46631
  1577
  apply (rule PQ)
haftmann@46631
  1578
  apply assumption
haftmann@46631
  1579
  done
haftmann@46631
  1580
haftmann@46631
  1581
lemma predicate1D:
haftmann@46631
  1582
  "P \<le> Q \<Longrightarrow> P x \<Longrightarrow> Q x"
haftmann@46631
  1583
  apply (erule le_funE)
haftmann@46631
  1584
  apply (erule le_boolE)
haftmann@46631
  1585
  apply assumption+
haftmann@46631
  1586
  done
haftmann@46631
  1587
haftmann@46631
  1588
lemma rev_predicate1D:
haftmann@46631
  1589
  "P x \<Longrightarrow> P \<le> Q \<Longrightarrow> Q x"
haftmann@46631
  1590
  by (rule predicate1D)
haftmann@46631
  1591
haftmann@46631
  1592
lemma predicate2I:
haftmann@46631
  1593
  assumes PQ: "\<And>x y. P x y \<Longrightarrow> Q x y"
haftmann@46631
  1594
  shows "P \<le> Q"
haftmann@46631
  1595
  apply (rule le_funI)+
haftmann@46631
  1596
  apply (rule le_boolI)
haftmann@46631
  1597
  apply (rule PQ)
haftmann@46631
  1598
  apply assumption
haftmann@46631
  1599
  done
haftmann@46631
  1600
haftmann@46631
  1601
lemma predicate2D:
haftmann@46631
  1602
  "P \<le> Q \<Longrightarrow> P x y \<Longrightarrow> Q x y"
haftmann@46631
  1603
  apply (erule le_funE)+
haftmann@46631
  1604
  apply (erule le_boolE)
haftmann@46631
  1605
  apply assumption+
haftmann@46631
  1606
  done
haftmann@46631
  1607
haftmann@46631
  1608
lemma rev_predicate2D:
haftmann@46631
  1609
  "P x y \<Longrightarrow> P \<le> Q \<Longrightarrow> Q x y"
haftmann@46631
  1610
  by (rule predicate2D)
haftmann@46631
  1611
haftmann@46631
  1612
lemma bot1E [no_atp]: "\<bottom> x \<Longrightarrow> P"
haftmann@46631
  1613
  by (simp add: bot_fun_def)
haftmann@46631
  1614
haftmann@46631
  1615
lemma bot2E: "\<bottom> x y \<Longrightarrow> P"
haftmann@46631
  1616
  by (simp add: bot_fun_def)
haftmann@46631
  1617
haftmann@46631
  1618
lemma top1I: "\<top> x"
haftmann@46631
  1619
  by (simp add: top_fun_def)
haftmann@46631
  1620
haftmann@46631
  1621
lemma top2I: "\<top> x y"
haftmann@46631
  1622
  by (simp add: top_fun_def)
haftmann@46631
  1623
haftmann@46631
  1624
wenzelm@60758
  1625
subsection \<open>Name duplicates\<close>
haftmann@34250
  1626
haftmann@34250
  1627
lemmas order_eq_refl = preorder_class.eq_refl
haftmann@34250
  1628
lemmas order_less_irrefl = preorder_class.less_irrefl
haftmann@34250
  1629
lemmas order_less_imp_le = preorder_class.less_imp_le
haftmann@34250
  1630
lemmas order_less_not_sym = preorder_class.less_not_sym
haftmann@34250
  1631
lemmas order_less_asym = preorder_class.less_asym
haftmann@34250
  1632
lemmas order_less_trans = preorder_class.less_trans
haftmann@34250
  1633
lemmas order_le_less_trans = preorder_class.le_less_trans
haftmann@34250
  1634
lemmas order_less_le_trans = preorder_class.less_le_trans
haftmann@34250
  1635
lemmas order_less_imp_not_less = preorder_class.less_imp_not_less
haftmann@34250
  1636
lemmas order_less_imp_triv = preorder_class.less_imp_triv
haftmann@34250
  1637
lemmas order_less_asym' = preorder_class.less_asym'
haftmann@34250
  1638
haftmann@34250
  1639
lemmas order_less_le = order_class.less_le
haftmann@34250
  1640
lemmas order_le_less = order_class.le_less
haftmann@34250
  1641
lemmas order_le_imp_less_or_eq = order_class.le_imp_less_or_eq
haftmann@34250
  1642
lemmas order_less_imp_not_eq = order_class.less_imp_not_eq
haftmann@34250
  1643
lemmas order_less_imp_not_eq2 = order_class.less_imp_not_eq2
haftmann@34250
  1644
lemmas order_neq_le_trans = order_class.neq_le_trans
haftmann@34250
  1645
lemmas order_le_neq_trans = order_class.le_neq_trans
haftmann@34250
  1646
lemmas order_antisym = order_class.antisym
haftmann@34250
  1647
lemmas order_eq_iff = order_class.eq_iff
haftmann@34250
  1648
lemmas order_antisym_conv = order_class.antisym_conv
haftmann@34250
  1649
haftmann@34250
  1650
lemmas linorder_linear = linorder_class.linear
haftmann@34250
  1651
lemmas linorder_less_linear = linorder_class.less_linear
haftmann@34250
  1652
lemmas linorder_le_less_linear = linorder_class.le_less_linear
haftmann@34250
  1653
lemmas linorder_le_cases = linorder_class.le_cases
haftmann@34250
  1654
lemmas linorder_not_less = linorder_class.not_less
haftmann@34250
  1655
lemmas linorder_not_le = linorder_class.not_le
haftmann@34250
  1656
lemmas linorder_neq_iff = linorder_class.neq_iff
haftmann@34250
  1657
lemmas linorder_neqE = linorder_class.neqE
haftmann@34250
  1658
lemmas linorder_antisym_conv1 = linorder_class.antisym_conv1
haftmann@34250
  1659
lemmas linorder_antisym_conv2 = linorder_class.antisym_conv2
haftmann@34250
  1660
lemmas linorder_antisym_conv3 = linorder_class.antisym_conv3
haftmann@34250
  1661
haftmann@28685
  1662
end