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