src/HOL/Orderings.thy
author haftmann
Tue May 04 08:55:43 2010 +0200 (2010-05-04)
changeset 36635 080b755377c0
parent 35828 46cfc4b8112e
child 36960 01594f816e3a
permissions -rw-r--r--
locale predicates of classes carry a mandatory "class" prefix
haftmann@28685
     1
(*  Title:      HOL/Orderings.thy
nipkow@15524
     2
    Author:     Tobias Nipkow, Markus Wenzel, and Larry Paulson
nipkow@15524
     3
*)
nipkow@15524
     4
haftmann@25614
     5
header {* Abstract orderings *}
nipkow@15524
     6
nipkow@15524
     7
theory Orderings
haftmann@35301
     8
imports HOL
wenzelm@32215
     9
uses
wenzelm@32215
    10
  "~~/src/Provers/order.ML"
wenzelm@32215
    11
  "~~/src/Provers/quasi.ML"  (* FIXME unused? *)
nipkow@15524
    12
begin
nipkow@15524
    13
haftmann@35092
    14
subsection {* Syntactic orders *}
haftmann@35092
    15
haftmann@35092
    16
class ord =
haftmann@35092
    17
  fixes less_eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
haftmann@35092
    18
    and less :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
haftmann@35092
    19
begin
haftmann@35092
    20
haftmann@35092
    21
notation
haftmann@35092
    22
  less_eq  ("op <=") and
haftmann@35092
    23
  less_eq  ("(_/ <= _)" [51, 51] 50) and
haftmann@35092
    24
  less  ("op <") and
haftmann@35092
    25
  less  ("(_/ < _)"  [51, 51] 50)
haftmann@35092
    26
  
haftmann@35092
    27
notation (xsymbols)
haftmann@35092
    28
  less_eq  ("op \<le>") and
haftmann@35092
    29
  less_eq  ("(_/ \<le> _)"  [51, 51] 50)
haftmann@35092
    30
haftmann@35092
    31
notation (HTML output)
haftmann@35092
    32
  less_eq  ("op \<le>") and
haftmann@35092
    33
  less_eq  ("(_/ \<le> _)"  [51, 51] 50)
haftmann@35092
    34
haftmann@35092
    35
abbreviation (input)
haftmann@35092
    36
  greater_eq  (infix ">=" 50) where
haftmann@35092
    37
  "x >= y \<equiv> y <= x"
haftmann@35092
    38
haftmann@35092
    39
notation (input)
haftmann@35092
    40
  greater_eq  (infix "\<ge>" 50)
haftmann@35092
    41
haftmann@35092
    42
abbreviation (input)
haftmann@35092
    43
  greater  (infix ">" 50) where
haftmann@35092
    44
  "x > y \<equiv> y < x"
haftmann@35092
    45
haftmann@35092
    46
end
haftmann@35092
    47
haftmann@35092
    48
haftmann@27682
    49
subsection {* Quasi orders *}
nipkow@15524
    50
haftmann@27682
    51
class preorder = ord +
haftmann@27682
    52
  assumes less_le_not_le: "x < y \<longleftrightarrow> x \<le> y \<and> \<not> (y \<le> x)"
haftmann@25062
    53
  and order_refl [iff]: "x \<le> x"
haftmann@25062
    54
  and order_trans: "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
haftmann@21248
    55
begin
haftmann@21248
    56
nipkow@15524
    57
text {* Reflexivity. *}
nipkow@15524
    58
haftmann@25062
    59
lemma eq_refl: "x = y \<Longrightarrow> x \<le> y"
nipkow@15524
    60
    -- {* This form is useful with the classical reasoner. *}
nipkow@23212
    61
by (erule ssubst) (rule order_refl)
nipkow@15524
    62
haftmann@25062
    63
lemma less_irrefl [iff]: "\<not> x < x"
haftmann@27682
    64
by (simp add: less_le_not_le)
haftmann@27682
    65
haftmann@27682
    66
lemma less_imp_le: "x < y \<Longrightarrow> x \<le> y"
haftmann@27682
    67
unfolding less_le_not_le by blast
haftmann@27682
    68
haftmann@27682
    69
haftmann@27682
    70
text {* Asymmetry. *}
haftmann@27682
    71
haftmann@27682
    72
lemma less_not_sym: "x < y \<Longrightarrow> \<not> (y < x)"
haftmann@27682
    73
by (simp add: less_le_not_le)
haftmann@27682
    74
haftmann@27682
    75
lemma less_asym: "x < y \<Longrightarrow> (\<not> P \<Longrightarrow> y < x) \<Longrightarrow> P"
haftmann@27682
    76
by (drule less_not_sym, erule contrapos_np) simp
haftmann@27682
    77
haftmann@27682
    78
haftmann@27682
    79
text {* Transitivity. *}
haftmann@27682
    80
haftmann@27682
    81
lemma less_trans: "x < y \<Longrightarrow> y < z \<Longrightarrow> x < z"
haftmann@27682
    82
by (auto simp add: less_le_not_le intro: order_trans) 
haftmann@27682
    83
haftmann@27682
    84
lemma le_less_trans: "x \<le> y \<Longrightarrow> y < z \<Longrightarrow> x < z"
haftmann@27682
    85
by (auto simp add: less_le_not_le intro: order_trans) 
haftmann@27682
    86
haftmann@27682
    87
lemma less_le_trans: "x < y \<Longrightarrow> y \<le> z \<Longrightarrow> x < z"
haftmann@27682
    88
by (auto simp add: less_le_not_le intro: order_trans) 
haftmann@27682
    89
haftmann@27682
    90
haftmann@27682
    91
text {* Useful for simplification, but too risky to include by default. *}
haftmann@27682
    92
haftmann@27682
    93
lemma less_imp_not_less: "x < y \<Longrightarrow> (\<not> y < x) \<longleftrightarrow> True"
haftmann@27682
    94
by (blast elim: less_asym)
haftmann@27682
    95
haftmann@27682
    96
lemma less_imp_triv: "x < y \<Longrightarrow> (y < x \<longrightarrow> P) \<longleftrightarrow> True"
haftmann@27682
    97
by (blast elim: less_asym)
haftmann@27682
    98
haftmann@27682
    99
haftmann@27682
   100
text {* Transitivity rules for calculational reasoning *}
haftmann@27682
   101
haftmann@27682
   102
lemma less_asym': "a < b \<Longrightarrow> b < a \<Longrightarrow> P"
haftmann@27682
   103
by (rule less_asym)
haftmann@27682
   104
haftmann@27682
   105
haftmann@27682
   106
text {* Dual order *}
haftmann@27682
   107
haftmann@27682
   108
lemma dual_preorder:
haftmann@36635
   109
  "class.preorder (op \<ge>) (op >)"
haftmann@28823
   110
proof qed (auto simp add: less_le_not_le intro: order_trans)
haftmann@27682
   111
haftmann@27682
   112
end
haftmann@27682
   113
haftmann@27682
   114
haftmann@27682
   115
subsection {* Partial orders *}
haftmann@27682
   116
haftmann@27682
   117
class order = preorder +
haftmann@27682
   118
  assumes antisym: "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y"
haftmann@27682
   119
begin
haftmann@27682
   120
haftmann@27682
   121
text {* Reflexivity. *}
haftmann@27682
   122
haftmann@27682
   123
lemma less_le: "x < y \<longleftrightarrow> x \<le> y \<and> x \<noteq> y"
haftmann@27682
   124
by (auto simp add: less_le_not_le intro: antisym)
nipkow@15524
   125
haftmann@25062
   126
lemma le_less: "x \<le> y \<longleftrightarrow> x < y \<or> x = y"
nipkow@15524
   127
    -- {* NOT suitable for iff, since it can cause PROOF FAILED. *}
nipkow@23212
   128
by (simp add: less_le) blast
nipkow@15524
   129
haftmann@25062
   130
lemma le_imp_less_or_eq: "x \<le> y \<Longrightarrow> x < y \<or> x = y"
nipkow@23212
   131
unfolding less_le by blast
nipkow@15524
   132
haftmann@21329
   133
haftmann@21329
   134
text {* Useful for simplification, but too risky to include by default. *}
haftmann@21329
   135
haftmann@25062
   136
lemma less_imp_not_eq: "x < y \<Longrightarrow> (x = y) \<longleftrightarrow> False"
nipkow@23212
   137
by auto
haftmann@21329
   138
haftmann@25062
   139
lemma less_imp_not_eq2: "x < y \<Longrightarrow> (y = x) \<longleftrightarrow> False"
nipkow@23212
   140
by auto
haftmann@21329
   141
haftmann@21329
   142
haftmann@21329
   143
text {* Transitivity rules for calculational reasoning *}
haftmann@21329
   144
haftmann@25062
   145
lemma neq_le_trans: "a \<noteq> b \<Longrightarrow> a \<le> b \<Longrightarrow> a < b"
nipkow@23212
   146
by (simp add: less_le)
haftmann@21329
   147
haftmann@25062
   148
lemma le_neq_trans: "a \<le> b \<Longrightarrow> a \<noteq> b \<Longrightarrow> a < b"
nipkow@23212
   149
by (simp add: less_le)
haftmann@21329
   150
nipkow@15524
   151
nipkow@15524
   152
text {* Asymmetry. *}
nipkow@15524
   153
haftmann@25062
   154
lemma eq_iff: "x = y \<longleftrightarrow> x \<le> y \<and> y \<le> x"
nipkow@23212
   155
by (blast intro: antisym)
nipkow@15524
   156
haftmann@25062
   157
lemma antisym_conv: "y \<le> x \<Longrightarrow> x \<le> y \<longleftrightarrow> x = y"
nipkow@23212
   158
by (blast intro: antisym)
nipkow@15524
   159
haftmann@25062
   160
lemma less_imp_neq: "x < y \<Longrightarrow> x \<noteq> y"
nipkow@23212
   161
by (erule contrapos_pn, erule subst, rule less_irrefl)
haftmann@21248
   162
haftmann@21083
   163
haftmann@27107
   164
text {* Least value operator *}
haftmann@27107
   165
haftmann@27299
   166
definition (in ord)
haftmann@27107
   167
  Least :: "('a \<Rightarrow> bool) \<Rightarrow> 'a" (binder "LEAST " 10) where
haftmann@27107
   168
  "Least P = (THE x. P x \<and> (\<forall>y. P y \<longrightarrow> x \<le> y))"
haftmann@27107
   169
haftmann@27107
   170
lemma Least_equality:
haftmann@27107
   171
  assumes "P x"
haftmann@27107
   172
    and "\<And>y. P y \<Longrightarrow> x \<le> y"
haftmann@27107
   173
  shows "Least P = x"
haftmann@27107
   174
unfolding Least_def by (rule the_equality)
haftmann@27107
   175
  (blast intro: assms antisym)+
haftmann@27107
   176
haftmann@27107
   177
lemma LeastI2_order:
haftmann@27107
   178
  assumes "P x"
haftmann@27107
   179
    and "\<And>y. P y \<Longrightarrow> x \<le> y"
haftmann@27107
   180
    and "\<And>x. P x \<Longrightarrow> \<forall>y. P y \<longrightarrow> x \<le> y \<Longrightarrow> Q x"
haftmann@27107
   181
  shows "Q (Least P)"
haftmann@27107
   182
unfolding Least_def by (rule theI2)
haftmann@27107
   183
  (blast intro: assms antisym)+
haftmann@27107
   184
haftmann@27107
   185
haftmann@26014
   186
text {* Dual order *}
haftmann@22916
   187
haftmann@26014
   188
lemma dual_order:
haftmann@36635
   189
  "class.order (op \<ge>) (op >)"
haftmann@27682
   190
by (intro_locales, rule dual_preorder) (unfold_locales, rule antisym)
haftmann@22916
   191
haftmann@21248
   192
end
nipkow@15524
   193
haftmann@21329
   194
haftmann@21329
   195
subsection {* Linear (total) orders *}
haftmann@21329
   196
haftmann@22316
   197
class linorder = order +
haftmann@25207
   198
  assumes linear: "x \<le> y \<or> y \<le> x"
haftmann@21248
   199
begin
haftmann@21248
   200
haftmann@25062
   201
lemma less_linear: "x < y \<or> x = y \<or> y < x"
nipkow@23212
   202
unfolding less_le using less_le linear by blast
haftmann@21248
   203
haftmann@25062
   204
lemma le_less_linear: "x \<le> y \<or> y < x"
nipkow@23212
   205
by (simp add: le_less less_linear)
haftmann@21248
   206
haftmann@21248
   207
lemma le_cases [case_names le ge]:
haftmann@25062
   208
  "(x \<le> y \<Longrightarrow> P) \<Longrightarrow> (y \<le> x \<Longrightarrow> P) \<Longrightarrow> P"
nipkow@23212
   209
using linear by blast
haftmann@21248
   210
haftmann@22384
   211
lemma linorder_cases [case_names less equal greater]:
haftmann@25062
   212
  "(x < y \<Longrightarrow> P) \<Longrightarrow> (x = y \<Longrightarrow> P) \<Longrightarrow> (y < x \<Longrightarrow> P) \<Longrightarrow> P"
nipkow@23212
   213
using less_linear by blast
haftmann@21248
   214
haftmann@25062
   215
lemma not_less: "\<not> x < y \<longleftrightarrow> y \<le> x"
nipkow@23212
   216
apply (simp add: less_le)
nipkow@23212
   217
using linear apply (blast intro: antisym)
nipkow@23212
   218
done
nipkow@23212
   219
nipkow@23212
   220
lemma not_less_iff_gr_or_eq:
haftmann@25062
   221
 "\<not>(x < y) \<longleftrightarrow> (x > y | x = y)"
nipkow@23212
   222
apply(simp add:not_less le_less)
nipkow@23212
   223
apply blast
nipkow@23212
   224
done
nipkow@15524
   225
haftmann@25062
   226
lemma not_le: "\<not> x \<le> y \<longleftrightarrow> y < x"
nipkow@23212
   227
apply (simp add: less_le)
nipkow@23212
   228
using linear apply (blast intro: antisym)
nipkow@23212
   229
done
nipkow@15524
   230
haftmann@25062
   231
lemma neq_iff: "x \<noteq> y \<longleftrightarrow> x < y \<or> y < x"
nipkow@23212
   232
by (cut_tac x = x and y = y in less_linear, auto)
nipkow@15524
   233
haftmann@25062
   234
lemma neqE: "x \<noteq> y \<Longrightarrow> (x < y \<Longrightarrow> R) \<Longrightarrow> (y < x \<Longrightarrow> R) \<Longrightarrow> R"
nipkow@23212
   235
by (simp add: neq_iff) blast
nipkow@15524
   236
haftmann@25062
   237
lemma antisym_conv1: "\<not> x < y \<Longrightarrow> x \<le> y \<longleftrightarrow> x = y"
nipkow@23212
   238
by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   239
haftmann@25062
   240
lemma antisym_conv2: "x \<le> y \<Longrightarrow> \<not> x < y \<longleftrightarrow> x = y"
nipkow@23212
   241
by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   242
haftmann@25062
   243
lemma antisym_conv3: "\<not> y < x \<Longrightarrow> \<not> x < y \<longleftrightarrow> x = y"
nipkow@23212
   244
by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   245
haftmann@25062
   246
lemma leI: "\<not> x < y \<Longrightarrow> y \<le> x"
nipkow@23212
   247
unfolding not_less .
paulson@16796
   248
haftmann@25062
   249
lemma leD: "y \<le> x \<Longrightarrow> \<not> x < y"
nipkow@23212
   250
unfolding not_less .
paulson@16796
   251
paulson@16796
   252
(*FIXME inappropriate name (or delete altogether)*)
haftmann@25062
   253
lemma not_leE: "\<not> y \<le> x \<Longrightarrow> x < y"
nipkow@23212
   254
unfolding not_le .
haftmann@21248
   255
haftmann@22916
   256
haftmann@26014
   257
text {* Dual order *}
haftmann@22916
   258
haftmann@26014
   259
lemma dual_linorder:
haftmann@36635
   260
  "class.linorder (op \<ge>) (op >)"
haftmann@36635
   261
by (rule class.linorder.intro, rule dual_order) (unfold_locales, rule linear)
haftmann@22916
   262
haftmann@22916
   263
haftmann@23881
   264
text {* min/max *}
haftmann@23881
   265
haftmann@27299
   266
definition (in ord) min :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@28516
   267
  [code del]: "min a b = (if a \<le> b then a else b)"
haftmann@23881
   268
haftmann@27299
   269
definition (in ord) max :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@28516
   270
  [code del]: "max a b = (if a \<le> b then b else a)"
haftmann@22384
   271
haftmann@21383
   272
lemma min_le_iff_disj:
haftmann@25062
   273
  "min x y \<le> z \<longleftrightarrow> x \<le> z \<or> y \<le> z"
nipkow@23212
   274
unfolding min_def using linear by (auto intro: order_trans)
haftmann@21383
   275
haftmann@21383
   276
lemma le_max_iff_disj:
haftmann@25062
   277
  "z \<le> max x y \<longleftrightarrow> z \<le> x \<or> z \<le> y"
nipkow@23212
   278
unfolding max_def using linear by (auto intro: order_trans)
haftmann@21383
   279
haftmann@21383
   280
lemma min_less_iff_disj:
haftmann@25062
   281
  "min x y < z \<longleftrightarrow> x < z \<or> y < z"
nipkow@23212
   282
unfolding min_def le_less using less_linear by (auto intro: less_trans)
haftmann@21383
   283
haftmann@21383
   284
lemma less_max_iff_disj:
haftmann@25062
   285
  "z < max x y \<longleftrightarrow> z < x \<or> z < y"
nipkow@23212
   286
unfolding max_def le_less using less_linear by (auto intro: less_trans)
haftmann@21383
   287
haftmann@21383
   288
lemma min_less_iff_conj [simp]:
haftmann@25062
   289
  "z < min x y \<longleftrightarrow> z < x \<and> z < y"
nipkow@23212
   290
unfolding min_def le_less using less_linear by (auto intro: less_trans)
haftmann@21383
   291
haftmann@21383
   292
lemma max_less_iff_conj [simp]:
haftmann@25062
   293
  "max x y < z \<longleftrightarrow> x < z \<and> y < z"
nipkow@23212
   294
unfolding max_def le_less using less_linear by (auto intro: less_trans)
haftmann@21383
   295
blanchet@35828
   296
lemma split_min [no_atp]:
haftmann@25062
   297
  "P (min i j) \<longleftrightarrow> (i \<le> j \<longrightarrow> P i) \<and> (\<not> i \<le> j \<longrightarrow> P j)"
nipkow@23212
   298
by (simp add: min_def)
haftmann@21383
   299
blanchet@35828
   300
lemma split_max [no_atp]:
haftmann@25062
   301
  "P (max i j) \<longleftrightarrow> (i \<le> j \<longrightarrow> P j) \<and> (\<not> i \<le> j \<longrightarrow> P i)"
nipkow@23212
   302
by (simp add: max_def)
haftmann@21383
   303
haftmann@21248
   304
end
haftmann@21248
   305
haftmann@28516
   306
text {* Explicit dictionaries for code generation *}
haftmann@28516
   307
haftmann@31998
   308
lemma min_ord_min [code, code_unfold, code_inline del]:
haftmann@28516
   309
  "min = ord.min (op \<le>)"
haftmann@28516
   310
  by (rule ext)+ (simp add: min_def ord.min_def)
haftmann@28516
   311
haftmann@28516
   312
declare ord.min_def [code]
haftmann@28516
   313
haftmann@31998
   314
lemma max_ord_max [code, code_unfold, code_inline del]:
haftmann@28516
   315
  "max = ord.max (op \<le>)"
haftmann@28516
   316
  by (rule ext)+ (simp add: max_def ord.max_def)
haftmann@28516
   317
haftmann@28516
   318
declare ord.max_def [code]
haftmann@28516
   319
haftmann@23948
   320
haftmann@21083
   321
subsection {* Reasoning tools setup *}
haftmann@21083
   322
haftmann@21091
   323
ML {*
haftmann@21091
   324
ballarin@24641
   325
signature ORDERS =
ballarin@24641
   326
sig
ballarin@24641
   327
  val print_structures: Proof.context -> unit
ballarin@24641
   328
  val setup: theory -> theory
wenzelm@32215
   329
  val order_tac: Proof.context -> thm list -> int -> tactic
ballarin@24641
   330
end;
haftmann@21091
   331
ballarin@24641
   332
structure Orders: ORDERS =
haftmann@21248
   333
struct
ballarin@24641
   334
ballarin@24641
   335
(** Theory and context data **)
ballarin@24641
   336
ballarin@24641
   337
fun struct_eq ((s1: string, ts1), (s2, ts2)) =
ballarin@24641
   338
  (s1 = s2) andalso eq_list (op aconv) (ts1, ts2);
ballarin@24641
   339
wenzelm@33519
   340
structure Data = Generic_Data
ballarin@24641
   341
(
ballarin@24641
   342
  type T = ((string * term list) * Order_Tac.less_arith) list;
ballarin@24641
   343
    (* Order structures:
ballarin@24641
   344
       identifier of the structure, list of operations and record of theorems
ballarin@24641
   345
       needed to set up the transitivity reasoner,
ballarin@24641
   346
       identifier and operations identify the structure uniquely. *)
ballarin@24641
   347
  val empty = [];
ballarin@24641
   348
  val extend = I;
wenzelm@33519
   349
  fun merge data = AList.join struct_eq (K fst) data;
ballarin@24641
   350
);
ballarin@24641
   351
ballarin@24641
   352
fun print_structures ctxt =
ballarin@24641
   353
  let
ballarin@24641
   354
    val structs = Data.get (Context.Proof ctxt);
ballarin@24641
   355
    fun pretty_term t = Pretty.block
wenzelm@24920
   356
      [Pretty.quote (Syntax.pretty_term ctxt t), Pretty.brk 1,
ballarin@24641
   357
        Pretty.str "::", Pretty.brk 1,
wenzelm@24920
   358
        Pretty.quote (Syntax.pretty_typ ctxt (type_of t))];
ballarin@24641
   359
    fun pretty_struct ((s, ts), _) = Pretty.block
ballarin@24641
   360
      [Pretty.str s, Pretty.str ":", Pretty.brk 1,
ballarin@24641
   361
       Pretty.enclose "(" ")" (Pretty.breaks (map pretty_term ts))];
ballarin@24641
   362
  in
ballarin@24641
   363
    Pretty.writeln (Pretty.big_list "Order structures:" (map pretty_struct structs))
ballarin@24641
   364
  end;
ballarin@24641
   365
ballarin@24641
   366
ballarin@24641
   367
(** Method **)
haftmann@21091
   368
wenzelm@32215
   369
fun struct_tac ((s, [eq, le, less]), thms) ctxt prems =
ballarin@24641
   370
  let
berghofe@30107
   371
    fun decomp thy (@{const Trueprop} $ t) =
ballarin@24641
   372
      let
ballarin@24641
   373
        fun excluded t =
ballarin@24641
   374
          (* exclude numeric types: linear arithmetic subsumes transitivity *)
ballarin@24641
   375
          let val T = type_of t
ballarin@24641
   376
          in
wenzelm@32960
   377
            T = HOLogic.natT orelse T = HOLogic.intT orelse T = HOLogic.realT
ballarin@24641
   378
          end;
wenzelm@32960
   379
        fun rel (bin_op $ t1 $ t2) =
ballarin@24641
   380
              if excluded t1 then NONE
ballarin@24641
   381
              else if Pattern.matches thy (eq, bin_op) then SOME (t1, "=", t2)
ballarin@24641
   382
              else if Pattern.matches thy (le, bin_op) then SOME (t1, "<=", t2)
ballarin@24641
   383
              else if Pattern.matches thy (less, bin_op) then SOME (t1, "<", t2)
ballarin@24641
   384
              else NONE
wenzelm@32960
   385
          | rel _ = NONE;
wenzelm@32960
   386
        fun dec (Const (@{const_name Not}, _) $ t) = (case rel t
wenzelm@32960
   387
              of NONE => NONE
wenzelm@32960
   388
               | SOME (t1, rel, t2) => SOME (t1, "~" ^ rel, t2))
ballarin@24741
   389
          | dec x = rel x;
berghofe@30107
   390
      in dec t end
berghofe@30107
   391
      | decomp thy _ = NONE;
ballarin@24641
   392
  in
ballarin@24641
   393
    case s of
wenzelm@32215
   394
      "order" => Order_Tac.partial_tac decomp thms ctxt prems
wenzelm@32215
   395
    | "linorder" => Order_Tac.linear_tac decomp thms ctxt prems
ballarin@24641
   396
    | _ => error ("Unknown kind of order `" ^ s ^ "' encountered in transitivity reasoner.")
ballarin@24641
   397
  end
ballarin@24641
   398
wenzelm@32215
   399
fun order_tac ctxt prems =
wenzelm@32215
   400
  FIRST' (map (fn s => CHANGED o struct_tac s ctxt prems) (Data.get (Context.Proof ctxt)));
ballarin@24641
   401
ballarin@24641
   402
ballarin@24641
   403
(** Attribute **)
ballarin@24641
   404
ballarin@24641
   405
fun add_struct_thm s tag =
ballarin@24641
   406
  Thm.declaration_attribute
ballarin@24641
   407
    (fn thm => Data.map (AList.map_default struct_eq (s, Order_Tac.empty TrueI) (Order_Tac.update tag thm)));
ballarin@24641
   408
fun del_struct s =
ballarin@24641
   409
  Thm.declaration_attribute
ballarin@24641
   410
    (fn _ => Data.map (AList.delete struct_eq s));
ballarin@24641
   411
wenzelm@30722
   412
val attrib_setup =
wenzelm@30722
   413
  Attrib.setup @{binding order}
wenzelm@30722
   414
    (Scan.lift ((Args.add -- Args.name >> (fn (_, s) => SOME s) || Args.del >> K NONE) --|
wenzelm@30722
   415
      Args.colon (* FIXME || Scan.succeed true *) ) -- Scan.lift Args.name --
wenzelm@30722
   416
      Scan.repeat Args.term
wenzelm@30722
   417
      >> (fn ((SOME tag, n), ts) => add_struct_thm (n, ts) tag
wenzelm@30722
   418
           | ((NONE, n), ts) => del_struct (n, ts)))
wenzelm@30722
   419
    "theorems controlling transitivity reasoner";
ballarin@24641
   420
ballarin@24641
   421
ballarin@24641
   422
(** Diagnostic command **)
ballarin@24641
   423
wenzelm@24867
   424
val _ =
ballarin@24641
   425
  OuterSyntax.improper_command "print_orders"
ballarin@24641
   426
    "print order structures available to transitivity reasoner" OuterKeyword.diag
wenzelm@30806
   427
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_context o
wenzelm@30806
   428
        Toplevel.keep (print_structures o Toplevel.context_of)));
ballarin@24641
   429
ballarin@24641
   430
ballarin@24641
   431
(** Setup **)
ballarin@24641
   432
wenzelm@24867
   433
val setup =
wenzelm@32215
   434
  Method.setup @{binding order} (Scan.succeed (fn ctxt => SIMPLE_METHOD' (order_tac ctxt [])))
wenzelm@30722
   435
    "transitivity reasoner" #>
wenzelm@30722
   436
  attrib_setup;
haftmann@21091
   437
haftmann@21091
   438
end;
ballarin@24641
   439
haftmann@21091
   440
*}
haftmann@21091
   441
ballarin@24641
   442
setup Orders.setup
ballarin@24641
   443
ballarin@24641
   444
ballarin@24641
   445
text {* Declarations to set up transitivity reasoner of partial and linear orders. *}
ballarin@24641
   446
haftmann@25076
   447
context order
haftmann@25076
   448
begin
haftmann@25076
   449
ballarin@24641
   450
(* The type constraint on @{term op =} below is necessary since the operation
ballarin@24641
   451
   is not a parameter of the locale. *)
haftmann@25076
   452
haftmann@27689
   453
declare less_irrefl [THEN notE, order add less_reflE: order "op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool" "op <=" "op <"]
haftmann@27689
   454
  
haftmann@27689
   455
declare order_refl  [order add le_refl: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   456
  
haftmann@27689
   457
declare less_imp_le [order add less_imp_le: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   458
  
haftmann@27689
   459
declare antisym [order add eqI: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   460
haftmann@27689
   461
declare eq_refl [order add eqD1: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   462
haftmann@27689
   463
declare sym [THEN eq_refl, order add eqD2: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   464
haftmann@27689
   465
declare less_trans [order add less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   466
  
haftmann@27689
   467
declare less_le_trans [order add less_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   468
  
haftmann@27689
   469
declare le_less_trans [order add le_less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   470
haftmann@27689
   471
declare order_trans [order add le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   472
haftmann@27689
   473
declare le_neq_trans [order add le_neq_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   474
haftmann@27689
   475
declare neq_le_trans [order add neq_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   476
haftmann@27689
   477
declare less_imp_neq [order add less_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   478
haftmann@27689
   479
declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   480
haftmann@27689
   481
declare not_sym [order add not_sym: order "op = :: 'a => 'a => bool" "op <=" "op <"]
ballarin@24641
   482
haftmann@25076
   483
end
haftmann@25076
   484
haftmann@25076
   485
context linorder
haftmann@25076
   486
begin
ballarin@24641
   487
haftmann@27689
   488
declare [[order del: order "op = :: 'a => 'a => bool" "op <=" "op <"]]
haftmann@27689
   489
haftmann@27689
   490
declare less_irrefl [THEN notE, order add less_reflE: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   491
haftmann@27689
   492
declare order_refl [order add le_refl: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   493
haftmann@27689
   494
declare less_imp_le [order add less_imp_le: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   495
haftmann@27689
   496
declare not_less [THEN iffD2, order add not_lessI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   497
haftmann@27689
   498
declare not_le [THEN iffD2, order add not_leI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   499
haftmann@27689
   500
declare not_less [THEN iffD1, order add not_lessD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   501
haftmann@27689
   502
declare not_le [THEN iffD1, order add not_leD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   503
haftmann@27689
   504
declare antisym [order add eqI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   505
haftmann@27689
   506
declare eq_refl [order add eqD1: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@25076
   507
haftmann@27689
   508
declare sym [THEN eq_refl, order add eqD2: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   509
haftmann@27689
   510
declare less_trans [order add less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   511
haftmann@27689
   512
declare less_le_trans [order add less_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   513
haftmann@27689
   514
declare le_less_trans [order add le_less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   515
haftmann@27689
   516
declare order_trans [order add le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   517
haftmann@27689
   518
declare le_neq_trans [order add le_neq_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   519
haftmann@27689
   520
declare neq_le_trans [order add neq_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   521
haftmann@27689
   522
declare less_imp_neq [order add less_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   523
haftmann@27689
   524
declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   525
haftmann@27689
   526
declare not_sym [order add not_sym: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
ballarin@24641
   527
haftmann@25076
   528
end
haftmann@25076
   529
ballarin@24641
   530
haftmann@21083
   531
setup {*
haftmann@21083
   532
let
haftmann@21083
   533
haftmann@21083
   534
fun prp t thm = (#prop (rep_thm thm) = t);
nipkow@15524
   535
haftmann@21083
   536
fun prove_antisym_le sg ss ((le as Const(_,T)) $ r $ s) =
haftmann@21083
   537
  let val prems = prems_of_ss ss;
haftmann@22916
   538
      val less = Const (@{const_name less}, T);
haftmann@21083
   539
      val t = HOLogic.mk_Trueprop(le $ s $ r);
haftmann@21083
   540
  in case find_first (prp t) prems of
haftmann@21083
   541
       NONE =>
haftmann@21083
   542
         let val t = HOLogic.mk_Trueprop(HOLogic.Not $ (less $ r $ s))
haftmann@21083
   543
         in case find_first (prp t) prems of
haftmann@21083
   544
              NONE => NONE
haftmann@24422
   545
            | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv1}))
haftmann@21083
   546
         end
haftmann@24422
   547
     | SOME thm => SOME(mk_meta_eq(thm RS @{thm order_class.antisym_conv}))
haftmann@21083
   548
  end
haftmann@21083
   549
  handle THM _ => NONE;
nipkow@15524
   550
haftmann@21083
   551
fun prove_antisym_less sg ss (NotC $ ((less as Const(_,T)) $ r $ s)) =
haftmann@21083
   552
  let val prems = prems_of_ss ss;
haftmann@22916
   553
      val le = Const (@{const_name less_eq}, T);
haftmann@21083
   554
      val t = HOLogic.mk_Trueprop(le $ r $ s);
haftmann@21083
   555
  in case find_first (prp t) prems of
haftmann@21083
   556
       NONE =>
haftmann@21083
   557
         let val t = HOLogic.mk_Trueprop(NotC $ (less $ s $ r))
haftmann@21083
   558
         in case find_first (prp t) prems of
haftmann@21083
   559
              NONE => NONE
haftmann@24422
   560
            | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv3}))
haftmann@21083
   561
         end
haftmann@24422
   562
     | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv2}))
haftmann@21083
   563
  end
haftmann@21083
   564
  handle THM _ => NONE;
nipkow@15524
   565
haftmann@21248
   566
fun add_simprocs procs thy =
wenzelm@26496
   567
  Simplifier.map_simpset (fn ss => ss
haftmann@21248
   568
    addsimprocs (map (fn (name, raw_ts, proc) =>
wenzelm@26496
   569
      Simplifier.simproc thy name raw_ts proc) procs)) thy;
wenzelm@26496
   570
fun add_solver name tac =
wenzelm@26496
   571
  Simplifier.map_simpset (fn ss => ss addSolver
wenzelm@32215
   572
    mk_solver' name (fn ss => tac (Simplifier.the_context ss) (Simplifier.prems_of_ss ss)));
haftmann@21083
   573
haftmann@21083
   574
in
haftmann@21248
   575
  add_simprocs [
haftmann@21248
   576
       ("antisym le", ["(x::'a::order) <= y"], prove_antisym_le),
haftmann@21248
   577
       ("antisym less", ["~ (x::'a::linorder) < y"], prove_antisym_less)
haftmann@21248
   578
     ]
ballarin@24641
   579
  #> add_solver "Transitivity" Orders.order_tac
haftmann@21248
   580
  (* Adding the transitivity reasoners also as safe solvers showed a slight
haftmann@21248
   581
     speed up, but the reasoning strength appears to be not higher (at least
haftmann@21248
   582
     no breaking of additional proofs in the entire HOL distribution, as
haftmann@21248
   583
     of 5 March 2004, was observed). *)
haftmann@21083
   584
end
haftmann@21083
   585
*}
nipkow@15524
   586
nipkow@15524
   587
haftmann@21083
   588
subsection {* Bounded quantifiers *}
haftmann@21083
   589
haftmann@21083
   590
syntax
wenzelm@21180
   591
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3ALL _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   592
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3EX _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   593
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   594
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   595
wenzelm@21180
   596
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3ALL _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   597
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3EX _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   598
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _>=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   599
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _>=_./ _)" [0, 0, 10] 10)
haftmann@21083
   600
haftmann@21083
   601
syntax (xsymbols)
wenzelm@21180
   602
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   603
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   604
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   605
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   606
wenzelm@21180
   607
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   608
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   609
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   610
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   611
haftmann@21083
   612
syntax (HOL)
wenzelm@21180
   613
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3! _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   614
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3? _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   615
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3! _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   616
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3? _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   617
haftmann@21083
   618
syntax (HTML output)
wenzelm@21180
   619
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   620
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   621
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   622
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   623
wenzelm@21180
   624
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   625
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   626
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   627
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   628
haftmann@21083
   629
translations
haftmann@21083
   630
  "ALL x<y. P"   =>  "ALL x. x < y \<longrightarrow> P"
haftmann@21083
   631
  "EX x<y. P"    =>  "EX x. x < y \<and> P"
haftmann@21083
   632
  "ALL x<=y. P"  =>  "ALL x. x <= y \<longrightarrow> P"
haftmann@21083
   633
  "EX x<=y. P"   =>  "EX x. x <= y \<and> P"
haftmann@21083
   634
  "ALL x>y. P"   =>  "ALL x. x > y \<longrightarrow> P"
haftmann@21083
   635
  "EX x>y. P"    =>  "EX x. x > y \<and> P"
haftmann@21083
   636
  "ALL x>=y. P"  =>  "ALL x. x >= y \<longrightarrow> P"
haftmann@21083
   637
  "EX x>=y. P"   =>  "EX x. x >= y \<and> P"
haftmann@21083
   638
haftmann@21083
   639
print_translation {*
haftmann@21083
   640
let
haftmann@22916
   641
  val All_binder = Syntax.binder_name @{const_syntax All};
haftmann@22916
   642
  val Ex_binder = Syntax.binder_name @{const_syntax Ex};
wenzelm@22377
   643
  val impl = @{const_syntax "op -->"};
wenzelm@22377
   644
  val conj = @{const_syntax "op &"};
haftmann@22916
   645
  val less = @{const_syntax less};
haftmann@22916
   646
  val less_eq = @{const_syntax less_eq};
wenzelm@21180
   647
wenzelm@21180
   648
  val trans =
wenzelm@35115
   649
   [((All_binder, impl, less),
wenzelm@35115
   650
    (@{syntax_const "_All_less"}, @{syntax_const "_All_greater"})),
wenzelm@35115
   651
    ((All_binder, impl, less_eq),
wenzelm@35115
   652
    (@{syntax_const "_All_less_eq"}, @{syntax_const "_All_greater_eq"})),
wenzelm@35115
   653
    ((Ex_binder, conj, less),
wenzelm@35115
   654
    (@{syntax_const "_Ex_less"}, @{syntax_const "_Ex_greater"})),
wenzelm@35115
   655
    ((Ex_binder, conj, less_eq),
wenzelm@35115
   656
    (@{syntax_const "_Ex_less_eq"}, @{syntax_const "_Ex_greater_eq"}))];
wenzelm@21180
   657
wenzelm@35115
   658
  fun matches_bound v t =
wenzelm@35115
   659
    (case t of
wenzelm@35364
   660
      Const (@{syntax_const "_bound"}, _) $ Free (v', _) => v = v'
wenzelm@35115
   661
    | _ => false);
wenzelm@35115
   662
  fun contains_var v = Term.exists_subterm (fn Free (x, _) => x = v | _ => false);
wenzelm@35115
   663
  fun mk v c n P = Syntax.const c $ Syntax.mark_bound v $ n $ P;
wenzelm@21180
   664
wenzelm@21180
   665
  fun tr' q = (q,
wenzelm@35364
   666
    fn [Const (@{syntax_const "_bound"}, _) $ Free (v, _),
wenzelm@35364
   667
        Const (c, _) $ (Const (d, _) $ t $ u) $ P] =>
wenzelm@35115
   668
        (case AList.lookup (op =) trans (q, c, d) of
wenzelm@35115
   669
          NONE => raise Match
wenzelm@35115
   670
        | SOME (l, g) =>
wenzelm@35115
   671
            if matches_bound v t andalso not (contains_var v u) then mk v l u P
wenzelm@35115
   672
            else if matches_bound v u andalso not (contains_var v t) then mk v g t P
wenzelm@35115
   673
            else raise Match)
wenzelm@21180
   674
     | _ => raise Match);
wenzelm@21524
   675
in [tr' All_binder, tr' Ex_binder] end
haftmann@21083
   676
*}
haftmann@21083
   677
haftmann@21083
   678
haftmann@21383
   679
subsection {* Transitivity reasoning *}
haftmann@21383
   680
haftmann@25193
   681
context ord
haftmann@25193
   682
begin
haftmann@21383
   683
haftmann@25193
   684
lemma ord_le_eq_trans: "a \<le> b \<Longrightarrow> b = c \<Longrightarrow> a \<le> c"
haftmann@25193
   685
  by (rule subst)
haftmann@21383
   686
haftmann@25193
   687
lemma ord_eq_le_trans: "a = b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
haftmann@25193
   688
  by (rule ssubst)
haftmann@21383
   689
haftmann@25193
   690
lemma ord_less_eq_trans: "a < b \<Longrightarrow> b = c \<Longrightarrow> a < c"
haftmann@25193
   691
  by (rule subst)
haftmann@25193
   692
haftmann@25193
   693
lemma ord_eq_less_trans: "a = b \<Longrightarrow> b < c \<Longrightarrow> a < c"
haftmann@25193
   694
  by (rule ssubst)
haftmann@25193
   695
haftmann@25193
   696
end
haftmann@21383
   697
haftmann@21383
   698
lemma order_less_subst2: "(a::'a::order) < b ==> f b < (c::'c::order) ==>
haftmann@21383
   699
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   700
proof -
haftmann@21383
   701
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   702
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   703
  also assume "f b < c"
haftmann@34250
   704
  finally (less_trans) show ?thesis .
haftmann@21383
   705
qed
haftmann@21383
   706
haftmann@21383
   707
lemma order_less_subst1: "(a::'a::order) < f b ==> (b::'b::order) < c ==>
haftmann@21383
   708
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   709
proof -
haftmann@21383
   710
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   711
  assume "a < f b"
haftmann@21383
   712
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@34250
   713
  finally (less_trans) show ?thesis .
haftmann@21383
   714
qed
haftmann@21383
   715
haftmann@21383
   716
lemma order_le_less_subst2: "(a::'a::order) <= b ==> f b < (c::'c::order) ==>
haftmann@21383
   717
  (!!x y. x <= y ==> f x <= f y) ==> f a < c"
haftmann@21383
   718
proof -
haftmann@21383
   719
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   720
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   721
  also assume "f b < c"
haftmann@34250
   722
  finally (le_less_trans) show ?thesis .
haftmann@21383
   723
qed
haftmann@21383
   724
haftmann@21383
   725
lemma order_le_less_subst1: "(a::'a::order) <= f b ==> (b::'b::order) < c ==>
haftmann@21383
   726
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   727
proof -
haftmann@21383
   728
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   729
  assume "a <= f b"
haftmann@21383
   730
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@34250
   731
  finally (le_less_trans) show ?thesis .
haftmann@21383
   732
qed
haftmann@21383
   733
haftmann@21383
   734
lemma order_less_le_subst2: "(a::'a::order) < b ==> f b <= (c::'c::order) ==>
haftmann@21383
   735
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   736
proof -
haftmann@21383
   737
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   738
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   739
  also assume "f b <= c"
haftmann@34250
   740
  finally (less_le_trans) show ?thesis .
haftmann@21383
   741
qed
haftmann@21383
   742
haftmann@21383
   743
lemma order_less_le_subst1: "(a::'a::order) < f b ==> (b::'b::order) <= c ==>
haftmann@21383
   744
  (!!x y. x <= y ==> f x <= f y) ==> a < f c"
haftmann@21383
   745
proof -
haftmann@21383
   746
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   747
  assume "a < f b"
haftmann@21383
   748
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@34250
   749
  finally (less_le_trans) show ?thesis .
haftmann@21383
   750
qed
haftmann@21383
   751
haftmann@21383
   752
lemma order_subst1: "(a::'a::order) <= f b ==> (b::'b::order) <= c ==>
haftmann@21383
   753
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   754
proof -
haftmann@21383
   755
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   756
  assume "a <= f b"
haftmann@21383
   757
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   758
  finally (order_trans) show ?thesis .
haftmann@21383
   759
qed
haftmann@21383
   760
haftmann@21383
   761
lemma order_subst2: "(a::'a::order) <= b ==> f b <= (c::'c::order) ==>
haftmann@21383
   762
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   763
proof -
haftmann@21383
   764
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   765
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   766
  also assume "f b <= c"
haftmann@21383
   767
  finally (order_trans) show ?thesis .
haftmann@21383
   768
qed
haftmann@21383
   769
haftmann@21383
   770
lemma ord_le_eq_subst: "a <= b ==> f b = c ==>
haftmann@21383
   771
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   772
proof -
haftmann@21383
   773
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   774
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   775
  also assume "f b = c"
haftmann@21383
   776
  finally (ord_le_eq_trans) show ?thesis .
haftmann@21383
   777
qed
haftmann@21383
   778
haftmann@21383
   779
lemma ord_eq_le_subst: "a = f b ==> b <= c ==>
haftmann@21383
   780
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   781
proof -
haftmann@21383
   782
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   783
  assume "a = f b"
haftmann@21383
   784
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   785
  finally (ord_eq_le_trans) show ?thesis .
haftmann@21383
   786
qed
haftmann@21383
   787
haftmann@21383
   788
lemma ord_less_eq_subst: "a < b ==> f b = c ==>
haftmann@21383
   789
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   790
proof -
haftmann@21383
   791
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   792
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   793
  also assume "f b = c"
haftmann@21383
   794
  finally (ord_less_eq_trans) show ?thesis .
haftmann@21383
   795
qed
haftmann@21383
   796
haftmann@21383
   797
lemma ord_eq_less_subst: "a = f b ==> b < c ==>
haftmann@21383
   798
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   799
proof -
haftmann@21383
   800
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   801
  assume "a = f b"
haftmann@21383
   802
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@21383
   803
  finally (ord_eq_less_trans) show ?thesis .
haftmann@21383
   804
qed
haftmann@21383
   805
haftmann@21383
   806
text {*
haftmann@21383
   807
  Note that this list of rules is in reverse order of priorities.
haftmann@21383
   808
*}
haftmann@21383
   809
haftmann@27682
   810
lemmas [trans] =
haftmann@21383
   811
  order_less_subst2
haftmann@21383
   812
  order_less_subst1
haftmann@21383
   813
  order_le_less_subst2
haftmann@21383
   814
  order_le_less_subst1
haftmann@21383
   815
  order_less_le_subst2
haftmann@21383
   816
  order_less_le_subst1
haftmann@21383
   817
  order_subst2
haftmann@21383
   818
  order_subst1
haftmann@21383
   819
  ord_le_eq_subst
haftmann@21383
   820
  ord_eq_le_subst
haftmann@21383
   821
  ord_less_eq_subst
haftmann@21383
   822
  ord_eq_less_subst
haftmann@21383
   823
  forw_subst
haftmann@21383
   824
  back_subst
haftmann@21383
   825
  rev_mp
haftmann@21383
   826
  mp
haftmann@27682
   827
haftmann@27682
   828
lemmas (in order) [trans] =
haftmann@27682
   829
  neq_le_trans
haftmann@27682
   830
  le_neq_trans
haftmann@27682
   831
haftmann@27682
   832
lemmas (in preorder) [trans] =
haftmann@27682
   833
  less_trans
haftmann@27682
   834
  less_asym'
haftmann@27682
   835
  le_less_trans
haftmann@27682
   836
  less_le_trans
haftmann@21383
   837
  order_trans
haftmann@27682
   838
haftmann@27682
   839
lemmas (in order) [trans] =
haftmann@27682
   840
  antisym
haftmann@27682
   841
haftmann@27682
   842
lemmas (in ord) [trans] =
haftmann@27682
   843
  ord_le_eq_trans
haftmann@27682
   844
  ord_eq_le_trans
haftmann@27682
   845
  ord_less_eq_trans
haftmann@27682
   846
  ord_eq_less_trans
haftmann@27682
   847
haftmann@27682
   848
lemmas [trans] =
haftmann@27682
   849
  trans
haftmann@27682
   850
haftmann@27682
   851
lemmas order_trans_rules =
haftmann@27682
   852
  order_less_subst2
haftmann@27682
   853
  order_less_subst1
haftmann@27682
   854
  order_le_less_subst2
haftmann@27682
   855
  order_le_less_subst1
haftmann@27682
   856
  order_less_le_subst2
haftmann@27682
   857
  order_less_le_subst1
haftmann@27682
   858
  order_subst2
haftmann@27682
   859
  order_subst1
haftmann@27682
   860
  ord_le_eq_subst
haftmann@27682
   861
  ord_eq_le_subst
haftmann@27682
   862
  ord_less_eq_subst
haftmann@27682
   863
  ord_eq_less_subst
haftmann@27682
   864
  forw_subst
haftmann@27682
   865
  back_subst
haftmann@27682
   866
  rev_mp
haftmann@27682
   867
  mp
haftmann@27682
   868
  neq_le_trans
haftmann@27682
   869
  le_neq_trans
haftmann@27682
   870
  less_trans
haftmann@27682
   871
  less_asym'
haftmann@27682
   872
  le_less_trans
haftmann@27682
   873
  less_le_trans
haftmann@27682
   874
  order_trans
haftmann@27682
   875
  antisym
haftmann@21383
   876
  ord_le_eq_trans
haftmann@21383
   877
  ord_eq_le_trans
haftmann@21383
   878
  ord_less_eq_trans
haftmann@21383
   879
  ord_eq_less_trans
haftmann@21383
   880
  trans
haftmann@21383
   881
haftmann@21083
   882
text {* These support proving chains of decreasing inequalities
haftmann@21083
   883
    a >= b >= c ... in Isar proofs. *}
haftmann@21083
   884
haftmann@21083
   885
lemma xt1:
haftmann@21083
   886
  "a = b ==> b > c ==> a > c"
haftmann@21083
   887
  "a > b ==> b = c ==> a > c"
haftmann@21083
   888
  "a = b ==> b >= c ==> a >= c"
haftmann@21083
   889
  "a >= b ==> b = c ==> a >= c"
haftmann@21083
   890
  "(x::'a::order) >= y ==> y >= x ==> x = y"
haftmann@21083
   891
  "(x::'a::order) >= y ==> y >= z ==> x >= z"
haftmann@21083
   892
  "(x::'a::order) > y ==> y >= z ==> x > z"
haftmann@21083
   893
  "(x::'a::order) >= y ==> y > z ==> x > z"
wenzelm@23417
   894
  "(a::'a::order) > b ==> b > a ==> P"
haftmann@21083
   895
  "(x::'a::order) > y ==> y > z ==> x > z"
haftmann@21083
   896
  "(a::'a::order) >= b ==> a ~= b ==> a > b"
haftmann@21083
   897
  "(a::'a::order) ~= b ==> a >= b ==> a > b"
haftmann@21083
   898
  "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c" 
haftmann@21083
   899
  "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   900
  "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
   901
  "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@25076
   902
  by auto
haftmann@21083
   903
haftmann@21083
   904
lemma xt2:
haftmann@21083
   905
  "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
   906
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
   907
haftmann@21083
   908
lemma xt3: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==> 
haftmann@21083
   909
    (!!x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@21083
   910
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
   911
haftmann@21083
   912
lemma xt4: "(a::'a::order) > f b ==> (b::'b::order) >= c ==>
haftmann@21083
   913
  (!!x y. x >= y ==> f x >= f y) ==> a > f c"
haftmann@21083
   914
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
   915
haftmann@21083
   916
lemma xt5: "(a::'a::order) > b ==> (f b::'b::order) >= c==>
haftmann@21083
   917
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   918
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
   919
haftmann@21083
   920
lemma xt6: "(a::'a::order) >= f b ==> b > c ==>
haftmann@21083
   921
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   922
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
   923
haftmann@21083
   924
lemma xt7: "(a::'a::order) >= b ==> (f b::'b::order) > c ==>
haftmann@21083
   925
    (!!x y. x >= y ==> f x >= f y) ==> f a > c"
haftmann@21083
   926
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
   927
haftmann@21083
   928
lemma xt8: "(a::'a::order) > f b ==> (b::'b::order) > c ==>
haftmann@21083
   929
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   930
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
   931
haftmann@21083
   932
lemma xt9: "(a::'a::order) > b ==> (f b::'b::order) > c ==>
haftmann@21083
   933
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   934
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
   935
haftmann@21083
   936
lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9
haftmann@21083
   937
haftmann@21083
   938
(* 
haftmann@21083
   939
  Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands
haftmann@21083
   940
  for the wrong thing in an Isar proof.
haftmann@21083
   941
haftmann@21083
   942
  The extra transitivity rules can be used as follows: 
haftmann@21083
   943
haftmann@21083
   944
lemma "(a::'a::order) > z"
haftmann@21083
   945
proof -
haftmann@21083
   946
  have "a >= b" (is "_ >= ?rhs")
haftmann@21083
   947
    sorry
haftmann@21083
   948
  also have "?rhs >= c" (is "_ >= ?rhs")
haftmann@21083
   949
    sorry
haftmann@21083
   950
  also (xtrans) have "?rhs = d" (is "_ = ?rhs")
haftmann@21083
   951
    sorry
haftmann@21083
   952
  also (xtrans) have "?rhs >= e" (is "_ >= ?rhs")
haftmann@21083
   953
    sorry
haftmann@21083
   954
  also (xtrans) have "?rhs > f" (is "_ > ?rhs")
haftmann@21083
   955
    sorry
haftmann@21083
   956
  also (xtrans) have "?rhs > z"
haftmann@21083
   957
    sorry
haftmann@21083
   958
  finally (xtrans) show ?thesis .
haftmann@21083
   959
qed
haftmann@21083
   960
haftmann@21083
   961
  Alternatively, one can use "declare xtrans [trans]" and then
haftmann@21083
   962
  leave out the "(xtrans)" above.
haftmann@21083
   963
*)
haftmann@21083
   964
haftmann@23881
   965
haftmann@23881
   966
subsection {* Monotonicity, least value operator and min/max *}
haftmann@21083
   967
haftmann@25076
   968
context order
haftmann@25076
   969
begin
haftmann@25076
   970
haftmann@30298
   971
definition mono :: "('a \<Rightarrow> 'b\<Colon>order) \<Rightarrow> bool" where
haftmann@25076
   972
  "mono f \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> f x \<le> f y)"
haftmann@25076
   973
haftmann@25076
   974
lemma monoI [intro?]:
haftmann@25076
   975
  fixes f :: "'a \<Rightarrow> 'b\<Colon>order"
haftmann@25076
   976
  shows "(\<And>x y. x \<le> y \<Longrightarrow> f x \<le> f y) \<Longrightarrow> mono f"
haftmann@25076
   977
  unfolding mono_def by iprover
haftmann@21216
   978
haftmann@25076
   979
lemma monoD [dest?]:
haftmann@25076
   980
  fixes f :: "'a \<Rightarrow> 'b\<Colon>order"
haftmann@25076
   981
  shows "mono f \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
haftmann@25076
   982
  unfolding mono_def by iprover
haftmann@25076
   983
haftmann@30298
   984
definition strict_mono :: "('a \<Rightarrow> 'b\<Colon>order) \<Rightarrow> bool" where
haftmann@30298
   985
  "strict_mono f \<longleftrightarrow> (\<forall>x y. x < y \<longrightarrow> f x < f y)"
haftmann@30298
   986
haftmann@30298
   987
lemma strict_monoI [intro?]:
haftmann@30298
   988
  assumes "\<And>x y. x < y \<Longrightarrow> f x < f y"
haftmann@30298
   989
  shows "strict_mono f"
haftmann@30298
   990
  using assms unfolding strict_mono_def by auto
haftmann@30298
   991
haftmann@30298
   992
lemma strict_monoD [dest?]:
haftmann@30298
   993
  "strict_mono f \<Longrightarrow> x < y \<Longrightarrow> f x < f y"
haftmann@30298
   994
  unfolding strict_mono_def by auto
haftmann@30298
   995
haftmann@30298
   996
lemma strict_mono_mono [dest?]:
haftmann@30298
   997
  assumes "strict_mono f"
haftmann@30298
   998
  shows "mono f"
haftmann@30298
   999
proof (rule monoI)
haftmann@30298
  1000
  fix x y
haftmann@30298
  1001
  assume "x \<le> y"
haftmann@30298
  1002
  show "f x \<le> f y"
haftmann@30298
  1003
  proof (cases "x = y")
haftmann@30298
  1004
    case True then show ?thesis by simp
haftmann@30298
  1005
  next
haftmann@30298
  1006
    case False with `x \<le> y` have "x < y" by simp
haftmann@30298
  1007
    with assms strict_monoD have "f x < f y" by auto
haftmann@30298
  1008
    then show ?thesis by simp
haftmann@30298
  1009
  qed
haftmann@30298
  1010
qed
haftmann@30298
  1011
haftmann@25076
  1012
end
haftmann@25076
  1013
haftmann@25076
  1014
context linorder
haftmann@25076
  1015
begin
haftmann@25076
  1016
haftmann@30298
  1017
lemma strict_mono_eq:
haftmann@30298
  1018
  assumes "strict_mono f"
haftmann@30298
  1019
  shows "f x = f y \<longleftrightarrow> x = y"
haftmann@30298
  1020
proof
haftmann@30298
  1021
  assume "f x = f y"
haftmann@30298
  1022
  show "x = y" proof (cases x y rule: linorder_cases)
haftmann@30298
  1023
    case less with assms strict_monoD have "f x < f y" by auto
haftmann@30298
  1024
    with `f x = f y` show ?thesis by simp
haftmann@30298
  1025
  next
haftmann@30298
  1026
    case equal then show ?thesis .
haftmann@30298
  1027
  next
haftmann@30298
  1028
    case greater with assms strict_monoD have "f y < f x" by auto
haftmann@30298
  1029
    with `f x = f y` show ?thesis by simp
haftmann@30298
  1030
  qed
haftmann@30298
  1031
qed simp
haftmann@30298
  1032
haftmann@30298
  1033
lemma strict_mono_less_eq:
haftmann@30298
  1034
  assumes "strict_mono f"
haftmann@30298
  1035
  shows "f x \<le> f y \<longleftrightarrow> x \<le> y"
haftmann@30298
  1036
proof
haftmann@30298
  1037
  assume "x \<le> y"
haftmann@30298
  1038
  with assms strict_mono_mono monoD show "f x \<le> f y" by auto
haftmann@30298
  1039
next
haftmann@30298
  1040
  assume "f x \<le> f y"
haftmann@30298
  1041
  show "x \<le> y" proof (rule ccontr)
haftmann@30298
  1042
    assume "\<not> x \<le> y" then have "y < x" by simp
haftmann@30298
  1043
    with assms strict_monoD have "f y < f x" by auto
haftmann@30298
  1044
    with `f x \<le> f y` show False by simp
haftmann@30298
  1045
  qed
haftmann@30298
  1046
qed
haftmann@30298
  1047
  
haftmann@30298
  1048
lemma strict_mono_less:
haftmann@30298
  1049
  assumes "strict_mono f"
haftmann@30298
  1050
  shows "f x < f y \<longleftrightarrow> x < y"
haftmann@30298
  1051
  using assms
haftmann@30298
  1052
    by (auto simp add: less_le Orderings.less_le strict_mono_eq strict_mono_less_eq)
haftmann@30298
  1053
haftmann@25076
  1054
lemma min_of_mono:
haftmann@25076
  1055
  fixes f :: "'a \<Rightarrow> 'b\<Colon>linorder"
wenzelm@25377
  1056
  shows "mono f \<Longrightarrow> min (f m) (f n) = f (min m n)"
haftmann@25076
  1057
  by (auto simp: mono_def Orderings.min_def min_def intro: Orderings.antisym)
haftmann@25076
  1058
haftmann@25076
  1059
lemma max_of_mono:
haftmann@25076
  1060
  fixes f :: "'a \<Rightarrow> 'b\<Colon>linorder"
wenzelm@25377
  1061
  shows "mono f \<Longrightarrow> max (f m) (f n) = f (max m n)"
haftmann@25076
  1062
  by (auto simp: mono_def Orderings.max_def max_def intro: Orderings.antisym)
haftmann@25076
  1063
haftmann@25076
  1064
end
haftmann@21083
  1065
haftmann@21383
  1066
lemma min_leastL: "(!!x. least <= x) ==> min least x = least"
nipkow@23212
  1067
by (simp add: min_def)
haftmann@21383
  1068
haftmann@21383
  1069
lemma max_leastL: "(!!x. least <= x) ==> max least x = x"
nipkow@23212
  1070
by (simp add: max_def)
haftmann@21383
  1071
haftmann@21383
  1072
lemma min_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> min x least = least"
nipkow@23212
  1073
apply (simp add: min_def)
haftmann@34250
  1074
apply (blast intro: antisym)
nipkow@23212
  1075
done
haftmann@21383
  1076
haftmann@21383
  1077
lemma max_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> max x least = x"
nipkow@23212
  1078
apply (simp add: max_def)
haftmann@34250
  1079
apply (blast intro: antisym)
nipkow@23212
  1080
done
haftmann@21383
  1081
haftmann@27823
  1082
haftmann@28685
  1083
subsection {* Top and bottom elements *}
haftmann@28685
  1084
haftmann@28685
  1085
class top = preorder +
haftmann@28685
  1086
  fixes top :: 'a
haftmann@28685
  1087
  assumes top_greatest [simp]: "x \<le> top"
haftmann@28685
  1088
haftmann@28685
  1089
class bot = preorder +
haftmann@28685
  1090
  fixes bot :: 'a
haftmann@28685
  1091
  assumes bot_least [simp]: "bot \<le> x"
haftmann@28685
  1092
haftmann@28685
  1093
haftmann@27823
  1094
subsection {* Dense orders *}
haftmann@27823
  1095
haftmann@35028
  1096
class dense_linorder = linorder + 
haftmann@27823
  1097
  assumes gt_ex: "\<exists>y. x < y" 
haftmann@27823
  1098
  and lt_ex: "\<exists>y. y < x"
haftmann@27823
  1099
  and dense: "x < y \<Longrightarrow> (\<exists>z. x < z \<and> z < y)"
hoelzl@35579
  1100
begin
haftmann@27823
  1101
hoelzl@35579
  1102
lemma dense_le:
hoelzl@35579
  1103
  fixes y z :: 'a
hoelzl@35579
  1104
  assumes "\<And>x. x < y \<Longrightarrow> x \<le> z"
hoelzl@35579
  1105
  shows "y \<le> z"
hoelzl@35579
  1106
proof (rule ccontr)
hoelzl@35579
  1107
  assume "\<not> ?thesis"
hoelzl@35579
  1108
  hence "z < y" by simp
hoelzl@35579
  1109
  from dense[OF this]
hoelzl@35579
  1110
  obtain x where "x < y" and "z < x" by safe
hoelzl@35579
  1111
  moreover have "x \<le> z" using assms[OF `x < y`] .
hoelzl@35579
  1112
  ultimately show False by auto
hoelzl@35579
  1113
qed
hoelzl@35579
  1114
hoelzl@35579
  1115
lemma dense_le_bounded:
hoelzl@35579
  1116
  fixes x y z :: 'a
hoelzl@35579
  1117
  assumes "x < y"
hoelzl@35579
  1118
  assumes *: "\<And>w. \<lbrakk> x < w ; w < y \<rbrakk> \<Longrightarrow> w \<le> z"
hoelzl@35579
  1119
  shows "y \<le> z"
hoelzl@35579
  1120
proof (rule dense_le)
hoelzl@35579
  1121
  fix w assume "w < y"
hoelzl@35579
  1122
  from dense[OF `x < y`] obtain u where "x < u" "u < y" by safe
hoelzl@35579
  1123
  from linear[of u w]
hoelzl@35579
  1124
  show "w \<le> z"
hoelzl@35579
  1125
  proof (rule disjE)
hoelzl@35579
  1126
    assume "u \<le> w"
hoelzl@35579
  1127
    from less_le_trans[OF `x < u` `u \<le> w`] `w < y`
hoelzl@35579
  1128
    show "w \<le> z" by (rule *)
hoelzl@35579
  1129
  next
hoelzl@35579
  1130
    assume "w \<le> u"
hoelzl@35579
  1131
    from `w \<le> u` *[OF `x < u` `u < y`]
hoelzl@35579
  1132
    show "w \<le> z" by (rule order_trans)
hoelzl@35579
  1133
  qed
hoelzl@35579
  1134
qed
hoelzl@35579
  1135
hoelzl@35579
  1136
end
haftmann@27823
  1137
haftmann@27823
  1138
subsection {* Wellorders *}
haftmann@27823
  1139
haftmann@27823
  1140
class wellorder = linorder +
haftmann@27823
  1141
  assumes less_induct [case_names less]: "(\<And>x. (\<And>y. y < x \<Longrightarrow> P y) \<Longrightarrow> P x) \<Longrightarrow> P a"
haftmann@27823
  1142
begin
haftmann@27823
  1143
haftmann@27823
  1144
lemma wellorder_Least_lemma:
haftmann@27823
  1145
  fixes k :: 'a
haftmann@27823
  1146
  assumes "P k"
haftmann@34250
  1147
  shows LeastI: "P (LEAST x. P x)" and Least_le: "(LEAST x. P x) \<le> k"
haftmann@27823
  1148
proof -
haftmann@27823
  1149
  have "P (LEAST x. P x) \<and> (LEAST x. P x) \<le> k"
haftmann@27823
  1150
  using assms proof (induct k rule: less_induct)
haftmann@27823
  1151
    case (less x) then have "P x" by simp
haftmann@27823
  1152
    show ?case proof (rule classical)
haftmann@27823
  1153
      assume assm: "\<not> (P (LEAST a. P a) \<and> (LEAST a. P a) \<le> x)"
haftmann@27823
  1154
      have "\<And>y. P y \<Longrightarrow> x \<le> y"
haftmann@27823
  1155
      proof (rule classical)
haftmann@27823
  1156
        fix y
haftmann@27823
  1157
        assume "P y" and "\<not> x \<le> y" 
haftmann@27823
  1158
        with less have "P (LEAST a. P a)" and "(LEAST a. P a) \<le> y"
haftmann@27823
  1159
          by (auto simp add: not_le)
haftmann@27823
  1160
        with assm have "x < (LEAST a. P a)" and "(LEAST a. P a) \<le> y"
haftmann@27823
  1161
          by auto
haftmann@27823
  1162
        then show "x \<le> y" by auto
haftmann@27823
  1163
      qed
haftmann@27823
  1164
      with `P x` have Least: "(LEAST a. P a) = x"
haftmann@27823
  1165
        by (rule Least_equality)
haftmann@27823
  1166
      with `P x` show ?thesis by simp
haftmann@27823
  1167
    qed
haftmann@27823
  1168
  qed
haftmann@27823
  1169
  then show "P (LEAST x. P x)" and "(LEAST x. P x) \<le> k" by auto
haftmann@27823
  1170
qed
haftmann@27823
  1171
haftmann@27823
  1172
-- "The following 3 lemmas are due to Brian Huffman"
haftmann@27823
  1173
lemma LeastI_ex: "\<exists>x. P x \<Longrightarrow> P (Least P)"
haftmann@27823
  1174
  by (erule exE) (erule LeastI)
haftmann@27823
  1175
haftmann@27823
  1176
lemma LeastI2:
haftmann@27823
  1177
  "P a \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> Q (Least P)"
haftmann@27823
  1178
  by (blast intro: LeastI)
haftmann@27823
  1179
haftmann@27823
  1180
lemma LeastI2_ex:
haftmann@27823
  1181
  "\<exists>a. P a \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> Q (Least P)"
haftmann@27823
  1182
  by (blast intro: LeastI_ex)
haftmann@27823
  1183
haftmann@27823
  1184
lemma not_less_Least: "k < (LEAST x. P x) \<Longrightarrow> \<not> P k"
haftmann@27823
  1185
apply (simp (no_asm_use) add: not_le [symmetric])
haftmann@27823
  1186
apply (erule contrapos_nn)
haftmann@27823
  1187
apply (erule Least_le)
haftmann@27823
  1188
done
haftmann@27823
  1189
haftmann@27823
  1190
end  
haftmann@27823
  1191
haftmann@28685
  1192
haftmann@28685
  1193
subsection {* Order on bool *}
haftmann@28685
  1194
haftmann@28685
  1195
instantiation bool :: "{order, top, bot}"
haftmann@28685
  1196
begin
haftmann@28685
  1197
haftmann@28685
  1198
definition
haftmann@28685
  1199
  le_bool_def [code del]: "P \<le> Q \<longleftrightarrow> P \<longrightarrow> Q"
haftmann@28685
  1200
haftmann@28685
  1201
definition
haftmann@28685
  1202
  less_bool_def [code del]: "(P\<Colon>bool) < Q \<longleftrightarrow> \<not> P \<and> Q"
haftmann@28685
  1203
haftmann@28685
  1204
definition
haftmann@28685
  1205
  top_bool_eq: "top = True"
haftmann@28685
  1206
haftmann@28685
  1207
definition
haftmann@28685
  1208
  bot_bool_eq: "bot = False"
haftmann@28685
  1209
haftmann@28685
  1210
instance proof
haftmann@34250
  1211
qed (auto simp add: bot_bool_eq top_bool_eq less_bool_def, auto simp add: le_bool_def)
haftmann@28685
  1212
nipkow@15524
  1213
end
haftmann@28685
  1214
haftmann@28685
  1215
lemma le_boolI: "(P \<Longrightarrow> Q) \<Longrightarrow> P \<le> Q"
haftmann@32899
  1216
  by (simp add: le_bool_def)
haftmann@28685
  1217
haftmann@28685
  1218
lemma le_boolI': "P \<longrightarrow> Q \<Longrightarrow> P \<le> Q"
haftmann@32899
  1219
  by (simp add: le_bool_def)
haftmann@28685
  1220
haftmann@28685
  1221
lemma le_boolE: "P \<le> Q \<Longrightarrow> P \<Longrightarrow> (Q \<Longrightarrow> R) \<Longrightarrow> R"
haftmann@32899
  1222
  by (simp add: le_bool_def)
haftmann@28685
  1223
haftmann@28685
  1224
lemma le_boolD: "P \<le> Q \<Longrightarrow> P \<longrightarrow> Q"
haftmann@32899
  1225
  by (simp add: le_bool_def)
haftmann@32899
  1226
haftmann@32899
  1227
lemma bot_boolE: "bot \<Longrightarrow> P"
haftmann@32899
  1228
  by (simp add: bot_bool_eq)
haftmann@32899
  1229
haftmann@32899
  1230
lemma top_boolI: top
haftmann@32899
  1231
  by (simp add: top_bool_eq)
haftmann@28685
  1232
haftmann@28685
  1233
lemma [code]:
haftmann@28685
  1234
  "False \<le> b \<longleftrightarrow> True"
haftmann@28685
  1235
  "True \<le> b \<longleftrightarrow> b"
haftmann@28685
  1236
  "False < b \<longleftrightarrow> b"
haftmann@28685
  1237
  "True < b \<longleftrightarrow> False"
haftmann@28685
  1238
  unfolding le_bool_def less_bool_def by simp_all
haftmann@28685
  1239
haftmann@28685
  1240
haftmann@28685
  1241
subsection {* Order on functions *}
haftmann@28685
  1242
haftmann@28685
  1243
instantiation "fun" :: (type, ord) ord
haftmann@28685
  1244
begin
haftmann@28685
  1245
haftmann@28685
  1246
definition
haftmann@28685
  1247
  le_fun_def [code del]: "f \<le> g \<longleftrightarrow> (\<forall>x. f x \<le> g x)"
haftmann@28685
  1248
haftmann@28685
  1249
definition
haftmann@28685
  1250
  less_fun_def [code del]: "(f\<Colon>'a \<Rightarrow> 'b) < g \<longleftrightarrow> f \<le> g \<and> \<not> (g \<le> f)"
haftmann@28685
  1251
haftmann@28685
  1252
instance ..
haftmann@28685
  1253
haftmann@28685
  1254
end
haftmann@28685
  1255
haftmann@28685
  1256
instance "fun" :: (type, preorder) preorder proof
haftmann@28685
  1257
qed (auto simp add: le_fun_def less_fun_def
haftmann@34250
  1258
  intro: order_trans antisym intro!: ext)
haftmann@28685
  1259
haftmann@28685
  1260
instance "fun" :: (type, order) order proof
haftmann@34250
  1261
qed (auto simp add: le_fun_def intro: antisym ext)
haftmann@28685
  1262
haftmann@28685
  1263
instantiation "fun" :: (type, top) top
haftmann@28685
  1264
begin
haftmann@28685
  1265
haftmann@28685
  1266
definition
haftmann@28685
  1267
  top_fun_eq: "top = (\<lambda>x. top)"
haftmann@28685
  1268
haftmann@28685
  1269
instance proof
haftmann@28685
  1270
qed (simp add: top_fun_eq le_fun_def)
haftmann@28685
  1271
haftmann@28685
  1272
end
haftmann@28685
  1273
haftmann@28685
  1274
instantiation "fun" :: (type, bot) bot
haftmann@28685
  1275
begin
haftmann@28685
  1276
haftmann@28685
  1277
definition
haftmann@28685
  1278
  bot_fun_eq: "bot = (\<lambda>x. bot)"
haftmann@28685
  1279
haftmann@28685
  1280
instance proof
haftmann@28685
  1281
qed (simp add: bot_fun_eq le_fun_def)
haftmann@28685
  1282
haftmann@28685
  1283
end
haftmann@28685
  1284
haftmann@28685
  1285
lemma le_funI: "(\<And>x. f x \<le> g x) \<Longrightarrow> f \<le> g"
haftmann@28685
  1286
  unfolding le_fun_def by simp
haftmann@28685
  1287
haftmann@28685
  1288
lemma le_funE: "f \<le> g \<Longrightarrow> (f x \<le> g x \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@28685
  1289
  unfolding le_fun_def by simp
haftmann@28685
  1290
haftmann@28685
  1291
lemma le_funD: "f \<le> g \<Longrightarrow> f x \<le> g x"
haftmann@28685
  1292
  unfolding le_fun_def by simp
haftmann@28685
  1293
haftmann@34250
  1294
haftmann@34250
  1295
subsection {* Name duplicates *}
haftmann@34250
  1296
haftmann@34250
  1297
lemmas order_eq_refl = preorder_class.eq_refl
haftmann@34250
  1298
lemmas order_less_irrefl = preorder_class.less_irrefl
haftmann@34250
  1299
lemmas order_less_imp_le = preorder_class.less_imp_le
haftmann@34250
  1300
lemmas order_less_not_sym = preorder_class.less_not_sym
haftmann@34250
  1301
lemmas order_less_asym = preorder_class.less_asym
haftmann@34250
  1302
lemmas order_less_trans = preorder_class.less_trans
haftmann@34250
  1303
lemmas order_le_less_trans = preorder_class.le_less_trans
haftmann@34250
  1304
lemmas order_less_le_trans = preorder_class.less_le_trans
haftmann@34250
  1305
lemmas order_less_imp_not_less = preorder_class.less_imp_not_less
haftmann@34250
  1306
lemmas order_less_imp_triv = preorder_class.less_imp_triv
haftmann@34250
  1307
lemmas order_less_asym' = preorder_class.less_asym'
haftmann@34250
  1308
haftmann@34250
  1309
lemmas order_less_le = order_class.less_le
haftmann@34250
  1310
lemmas order_le_less = order_class.le_less
haftmann@34250
  1311
lemmas order_le_imp_less_or_eq = order_class.le_imp_less_or_eq
haftmann@34250
  1312
lemmas order_less_imp_not_eq = order_class.less_imp_not_eq
haftmann@34250
  1313
lemmas order_less_imp_not_eq2 = order_class.less_imp_not_eq2
haftmann@34250
  1314
lemmas order_neq_le_trans = order_class.neq_le_trans
haftmann@34250
  1315
lemmas order_le_neq_trans = order_class.le_neq_trans
haftmann@34250
  1316
lemmas order_antisym = order_class.antisym
haftmann@34250
  1317
lemmas order_eq_iff = order_class.eq_iff
haftmann@34250
  1318
lemmas order_antisym_conv = order_class.antisym_conv
haftmann@34250
  1319
haftmann@34250
  1320
lemmas linorder_linear = linorder_class.linear
haftmann@34250
  1321
lemmas linorder_less_linear = linorder_class.less_linear
haftmann@34250
  1322
lemmas linorder_le_less_linear = linorder_class.le_less_linear
haftmann@34250
  1323
lemmas linorder_le_cases = linorder_class.le_cases
haftmann@34250
  1324
lemmas linorder_not_less = linorder_class.not_less
haftmann@34250
  1325
lemmas linorder_not_le = linorder_class.not_le
haftmann@34250
  1326
lemmas linorder_neq_iff = linorder_class.neq_iff
haftmann@34250
  1327
lemmas linorder_neqE = linorder_class.neqE
haftmann@34250
  1328
lemmas linorder_antisym_conv1 = linorder_class.antisym_conv1
haftmann@34250
  1329
lemmas linorder_antisym_conv2 = linorder_class.antisym_conv2
haftmann@34250
  1330
lemmas linorder_antisym_conv3 = linorder_class.antisym_conv3
haftmann@34250
  1331
haftmann@28685
  1332
end