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