src/HOL/Orderings.thy
author haftmann
Mon Aug 11 14:49:53 2008 +0200 (2008-08-11)
changeset 27823 52971512d1a2
parent 27689 268a7d02cf7a
child 28516 e6fdcaaadbd3
permissions -rw-r--r--
moved class wellorder to theory Orderings
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@27689
   406
declare less_irrefl [THEN notE, order add less_reflE: order "op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool" "op <=" "op <"]
haftmann@27689
   407
  
haftmann@27689
   408
declare order_refl  [order add le_refl: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   409
  
haftmann@27689
   410
declare less_imp_le [order add less_imp_le: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   411
  
haftmann@27689
   412
declare antisym [order add eqI: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   413
haftmann@27689
   414
declare eq_refl [order add eqD1: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   415
haftmann@27689
   416
declare sym [THEN eq_refl, order add eqD2: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   417
haftmann@27689
   418
declare less_trans [order add less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   419
  
haftmann@27689
   420
declare less_le_trans [order add less_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   421
  
haftmann@27689
   422
declare le_less_trans [order add le_less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   423
haftmann@27689
   424
declare order_trans [order add le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   425
haftmann@27689
   426
declare le_neq_trans [order add le_neq_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   427
haftmann@27689
   428
declare neq_le_trans [order add neq_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   429
haftmann@27689
   430
declare less_imp_neq [order add less_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   431
haftmann@27689
   432
declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   433
haftmann@27689
   434
declare not_sym [order add not_sym: order "op = :: 'a => 'a => bool" "op <=" "op <"]
ballarin@24641
   435
haftmann@25076
   436
end
haftmann@25076
   437
haftmann@25076
   438
context linorder
haftmann@25076
   439
begin
ballarin@24641
   440
haftmann@27689
   441
declare [[order del: order "op = :: 'a => 'a => bool" "op <=" "op <"]]
haftmann@27689
   442
haftmann@27689
   443
declare less_irrefl [THEN notE, order add less_reflE: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   444
haftmann@27689
   445
declare order_refl [order add le_refl: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   446
haftmann@27689
   447
declare less_imp_le [order add less_imp_le: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   448
haftmann@27689
   449
declare not_less [THEN iffD2, order add not_lessI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   450
haftmann@27689
   451
declare not_le [THEN iffD2, order add not_leI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   452
haftmann@27689
   453
declare not_less [THEN iffD1, order add not_lessD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   454
haftmann@27689
   455
declare not_le [THEN iffD1, order add not_leD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   456
haftmann@27689
   457
declare antisym [order add eqI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   458
haftmann@27689
   459
declare eq_refl [order add eqD1: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@25076
   460
haftmann@27689
   461
declare sym [THEN eq_refl, order add eqD2: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   462
haftmann@27689
   463
declare less_trans [order add less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   464
haftmann@27689
   465
declare less_le_trans [order add less_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   466
haftmann@27689
   467
declare le_less_trans [order add le_less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   468
haftmann@27689
   469
declare order_trans [order add le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   470
haftmann@27689
   471
declare le_neq_trans [order add le_neq_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   472
haftmann@27689
   473
declare neq_le_trans [order add neq_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   474
haftmann@27689
   475
declare less_imp_neq [order add less_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   476
haftmann@27689
   477
declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   478
haftmann@27689
   479
declare not_sym [order add not_sym: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
ballarin@24641
   480
haftmann@25076
   481
end
haftmann@25076
   482
ballarin@24641
   483
haftmann@21083
   484
setup {*
haftmann@21083
   485
let
haftmann@21083
   486
haftmann@21083
   487
fun prp t thm = (#prop (rep_thm thm) = t);
nipkow@15524
   488
haftmann@21083
   489
fun prove_antisym_le sg ss ((le as Const(_,T)) $ r $ s) =
haftmann@21083
   490
  let val prems = prems_of_ss ss;
haftmann@22916
   491
      val less = Const (@{const_name less}, T);
haftmann@21083
   492
      val t = HOLogic.mk_Trueprop(le $ s $ r);
haftmann@21083
   493
  in case find_first (prp t) prems of
haftmann@21083
   494
       NONE =>
haftmann@21083
   495
         let val t = HOLogic.mk_Trueprop(HOLogic.Not $ (less $ r $ s))
haftmann@21083
   496
         in case find_first (prp t) prems of
haftmann@21083
   497
              NONE => NONE
haftmann@24422
   498
            | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv1}))
haftmann@21083
   499
         end
haftmann@24422
   500
     | SOME thm => SOME(mk_meta_eq(thm RS @{thm order_class.antisym_conv}))
haftmann@21083
   501
  end
haftmann@21083
   502
  handle THM _ => NONE;
nipkow@15524
   503
haftmann@21083
   504
fun prove_antisym_less sg ss (NotC $ ((less as Const(_,T)) $ r $ s)) =
haftmann@21083
   505
  let val prems = prems_of_ss ss;
haftmann@22916
   506
      val le = Const (@{const_name less_eq}, T);
haftmann@21083
   507
      val t = HOLogic.mk_Trueprop(le $ r $ s);
haftmann@21083
   508
  in case find_first (prp t) prems of
haftmann@21083
   509
       NONE =>
haftmann@21083
   510
         let val t = HOLogic.mk_Trueprop(NotC $ (less $ s $ r))
haftmann@21083
   511
         in case find_first (prp t) prems of
haftmann@21083
   512
              NONE => NONE
haftmann@24422
   513
            | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv3}))
haftmann@21083
   514
         end
haftmann@24422
   515
     | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv2}))
haftmann@21083
   516
  end
haftmann@21083
   517
  handle THM _ => NONE;
nipkow@15524
   518
haftmann@21248
   519
fun add_simprocs procs thy =
wenzelm@26496
   520
  Simplifier.map_simpset (fn ss => ss
haftmann@21248
   521
    addsimprocs (map (fn (name, raw_ts, proc) =>
wenzelm@26496
   522
      Simplifier.simproc thy name raw_ts proc) procs)) thy;
wenzelm@26496
   523
fun add_solver name tac =
wenzelm@26496
   524
  Simplifier.map_simpset (fn ss => ss addSolver
wenzelm@26496
   525
    mk_solver' name (fn ss => tac (Simplifier.prems_of_ss ss) (Simplifier.the_context ss)));
haftmann@21083
   526
haftmann@21083
   527
in
haftmann@21248
   528
  add_simprocs [
haftmann@21248
   529
       ("antisym le", ["(x::'a::order) <= y"], prove_antisym_le),
haftmann@21248
   530
       ("antisym less", ["~ (x::'a::linorder) < y"], prove_antisym_less)
haftmann@21248
   531
     ]
ballarin@24641
   532
  #> add_solver "Transitivity" Orders.order_tac
haftmann@21248
   533
  (* Adding the transitivity reasoners also as safe solvers showed a slight
haftmann@21248
   534
     speed up, but the reasoning strength appears to be not higher (at least
haftmann@21248
   535
     no breaking of additional proofs in the entire HOL distribution, as
haftmann@21248
   536
     of 5 March 2004, was observed). *)
haftmann@21083
   537
end
haftmann@21083
   538
*}
nipkow@15524
   539
nipkow@15524
   540
haftmann@24422
   541
subsection {* Name duplicates *}
haftmann@24422
   542
haftmann@24422
   543
lemmas order_less_le = less_le
haftmann@27682
   544
lemmas order_eq_refl = preorder_class.eq_refl
haftmann@27682
   545
lemmas order_less_irrefl = preorder_class.less_irrefl
haftmann@24422
   546
lemmas order_le_less = order_class.le_less
haftmann@24422
   547
lemmas order_le_imp_less_or_eq = order_class.le_imp_less_or_eq
haftmann@27682
   548
lemmas order_less_imp_le = preorder_class.less_imp_le
haftmann@24422
   549
lemmas order_less_imp_not_eq = order_class.less_imp_not_eq
haftmann@24422
   550
lemmas order_less_imp_not_eq2 = order_class.less_imp_not_eq2
haftmann@24422
   551
lemmas order_neq_le_trans = order_class.neq_le_trans
haftmann@24422
   552
lemmas order_le_neq_trans = order_class.le_neq_trans
haftmann@24422
   553
haftmann@24422
   554
lemmas order_antisym = antisym
haftmann@27682
   555
lemmas order_less_not_sym = preorder_class.less_not_sym
haftmann@27682
   556
lemmas order_less_asym = preorder_class.less_asym
haftmann@24422
   557
lemmas order_eq_iff = order_class.eq_iff
haftmann@24422
   558
lemmas order_antisym_conv = order_class.antisym_conv
haftmann@27682
   559
lemmas order_less_trans = preorder_class.less_trans
haftmann@27682
   560
lemmas order_le_less_trans = preorder_class.le_less_trans
haftmann@27682
   561
lemmas order_less_le_trans = preorder_class.less_le_trans
haftmann@27682
   562
lemmas order_less_imp_not_less = preorder_class.less_imp_not_less
haftmann@27682
   563
lemmas order_less_imp_triv = preorder_class.less_imp_triv
haftmann@27682
   564
lemmas order_less_asym' = preorder_class.less_asym'
haftmann@24422
   565
haftmann@24422
   566
lemmas linorder_linear = linear
haftmann@24422
   567
lemmas linorder_less_linear = linorder_class.less_linear
haftmann@24422
   568
lemmas linorder_le_less_linear = linorder_class.le_less_linear
haftmann@24422
   569
lemmas linorder_le_cases = linorder_class.le_cases
haftmann@24422
   570
lemmas linorder_not_less = linorder_class.not_less
haftmann@24422
   571
lemmas linorder_not_le = linorder_class.not_le
haftmann@24422
   572
lemmas linorder_neq_iff = linorder_class.neq_iff
haftmann@24422
   573
lemmas linorder_neqE = linorder_class.neqE
haftmann@24422
   574
lemmas linorder_antisym_conv1 = linorder_class.antisym_conv1
haftmann@24422
   575
lemmas linorder_antisym_conv2 = linorder_class.antisym_conv2
haftmann@24422
   576
lemmas linorder_antisym_conv3 = linorder_class.antisym_conv3
haftmann@24422
   577
haftmann@24422
   578
haftmann@21083
   579
subsection {* Bounded quantifiers *}
haftmann@21083
   580
haftmann@21083
   581
syntax
wenzelm@21180
   582
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3ALL _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   583
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3EX _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   584
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   585
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   586
wenzelm@21180
   587
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3ALL _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   588
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3EX _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   589
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _>=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   590
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _>=_./ _)" [0, 0, 10] 10)
haftmann@21083
   591
haftmann@21083
   592
syntax (xsymbols)
wenzelm@21180
   593
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   594
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   595
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   596
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   597
wenzelm@21180
   598
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   599
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   600
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   601
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   602
haftmann@21083
   603
syntax (HOL)
wenzelm@21180
   604
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3! _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   605
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3? _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   606
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3! _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   607
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3? _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   608
haftmann@21083
   609
syntax (HTML output)
wenzelm@21180
   610
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   611
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   612
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   613
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   614
wenzelm@21180
   615
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   616
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   617
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   618
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   619
haftmann@21083
   620
translations
haftmann@21083
   621
  "ALL x<y. P"   =>  "ALL x. x < y \<longrightarrow> P"
haftmann@21083
   622
  "EX x<y. P"    =>  "EX x. x < y \<and> P"
haftmann@21083
   623
  "ALL x<=y. P"  =>  "ALL x. x <= y \<longrightarrow> P"
haftmann@21083
   624
  "EX x<=y. P"   =>  "EX x. x <= y \<and> P"
haftmann@21083
   625
  "ALL x>y. P"   =>  "ALL x. x > y \<longrightarrow> P"
haftmann@21083
   626
  "EX x>y. P"    =>  "EX x. x > y \<and> P"
haftmann@21083
   627
  "ALL x>=y. P"  =>  "ALL x. x >= y \<longrightarrow> P"
haftmann@21083
   628
  "EX x>=y. P"   =>  "EX x. x >= y \<and> P"
haftmann@21083
   629
haftmann@21083
   630
print_translation {*
haftmann@21083
   631
let
haftmann@22916
   632
  val All_binder = Syntax.binder_name @{const_syntax All};
haftmann@22916
   633
  val Ex_binder = Syntax.binder_name @{const_syntax Ex};
wenzelm@22377
   634
  val impl = @{const_syntax "op -->"};
wenzelm@22377
   635
  val conj = @{const_syntax "op &"};
haftmann@22916
   636
  val less = @{const_syntax less};
haftmann@22916
   637
  val less_eq = @{const_syntax less_eq};
wenzelm@21180
   638
wenzelm@21180
   639
  val trans =
wenzelm@21524
   640
   [((All_binder, impl, less), ("_All_less", "_All_greater")),
wenzelm@21524
   641
    ((All_binder, impl, less_eq), ("_All_less_eq", "_All_greater_eq")),
wenzelm@21524
   642
    ((Ex_binder, conj, less), ("_Ex_less", "_Ex_greater")),
wenzelm@21524
   643
    ((Ex_binder, conj, less_eq), ("_Ex_less_eq", "_Ex_greater_eq"))];
wenzelm@21180
   644
krauss@22344
   645
  fun matches_bound v t = 
krauss@22344
   646
     case t of (Const ("_bound", _) $ Free (v', _)) => (v = v')
krauss@22344
   647
              | _ => false
krauss@22344
   648
  fun contains_var v = Term.exists_subterm (fn Free (x, _) => x = v | _ => false)
krauss@22344
   649
  fun mk v c n P = Syntax.const c $ Syntax.mark_bound v $ n $ P
wenzelm@21180
   650
wenzelm@21180
   651
  fun tr' q = (q,
wenzelm@21180
   652
    fn [Const ("_bound", _) $ Free (v, _), Const (c, _) $ (Const (d, _) $ t $ u) $ P] =>
wenzelm@21180
   653
      (case AList.lookup (op =) trans (q, c, d) of
wenzelm@21180
   654
        NONE => raise Match
wenzelm@21180
   655
      | SOME (l, g) =>
krauss@22344
   656
          if matches_bound v t andalso not (contains_var v u) then mk v l u P
krauss@22344
   657
          else if matches_bound v u andalso not (contains_var v t) then mk v g t P
krauss@22344
   658
          else raise Match)
wenzelm@21180
   659
     | _ => raise Match);
wenzelm@21524
   660
in [tr' All_binder, tr' Ex_binder] end
haftmann@21083
   661
*}
haftmann@21083
   662
haftmann@21083
   663
haftmann@21383
   664
subsection {* Transitivity reasoning *}
haftmann@21383
   665
haftmann@25193
   666
context ord
haftmann@25193
   667
begin
haftmann@21383
   668
haftmann@25193
   669
lemma ord_le_eq_trans: "a \<le> b \<Longrightarrow> b = c \<Longrightarrow> a \<le> c"
haftmann@25193
   670
  by (rule subst)
haftmann@21383
   671
haftmann@25193
   672
lemma ord_eq_le_trans: "a = b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
haftmann@25193
   673
  by (rule ssubst)
haftmann@21383
   674
haftmann@25193
   675
lemma ord_less_eq_trans: "a < b \<Longrightarrow> b = c \<Longrightarrow> a < c"
haftmann@25193
   676
  by (rule subst)
haftmann@25193
   677
haftmann@25193
   678
lemma ord_eq_less_trans: "a = b \<Longrightarrow> b < c \<Longrightarrow> a < c"
haftmann@25193
   679
  by (rule ssubst)
haftmann@25193
   680
haftmann@25193
   681
end
haftmann@21383
   682
haftmann@21383
   683
lemma order_less_subst2: "(a::'a::order) < b ==> f b < (c::'c::order) ==>
haftmann@21383
   684
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   685
proof -
haftmann@21383
   686
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   687
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   688
  also assume "f b < c"
haftmann@21383
   689
  finally (order_less_trans) show ?thesis .
haftmann@21383
   690
qed
haftmann@21383
   691
haftmann@21383
   692
lemma order_less_subst1: "(a::'a::order) < f b ==> (b::'b::order) < c ==>
haftmann@21383
   693
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   694
proof -
haftmann@21383
   695
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   696
  assume "a < f b"
haftmann@21383
   697
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@21383
   698
  finally (order_less_trans) show ?thesis .
haftmann@21383
   699
qed
haftmann@21383
   700
haftmann@21383
   701
lemma order_le_less_subst2: "(a::'a::order) <= b ==> f b < (c::'c::order) ==>
haftmann@21383
   702
  (!!x y. x <= y ==> f x <= f y) ==> f a < c"
haftmann@21383
   703
proof -
haftmann@21383
   704
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   705
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   706
  also assume "f b < c"
haftmann@21383
   707
  finally (order_le_less_trans) show ?thesis .
haftmann@21383
   708
qed
haftmann@21383
   709
haftmann@21383
   710
lemma order_le_less_subst1: "(a::'a::order) <= f b ==> (b::'b::order) < c ==>
haftmann@21383
   711
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   712
proof -
haftmann@21383
   713
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   714
  assume "a <= f b"
haftmann@21383
   715
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@21383
   716
  finally (order_le_less_trans) show ?thesis .
haftmann@21383
   717
qed
haftmann@21383
   718
haftmann@21383
   719
lemma order_less_le_subst2: "(a::'a::order) < b ==> f b <= (c::'c::order) ==>
haftmann@21383
   720
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   721
proof -
haftmann@21383
   722
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   723
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   724
  also assume "f b <= c"
haftmann@21383
   725
  finally (order_less_le_trans) show ?thesis .
haftmann@21383
   726
qed
haftmann@21383
   727
haftmann@21383
   728
lemma order_less_le_subst1: "(a::'a::order) < f b ==> (b::'b::order) <= c ==>
haftmann@21383
   729
  (!!x y. x <= y ==> f x <= f y) ==> a < f c"
haftmann@21383
   730
proof -
haftmann@21383
   731
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   732
  assume "a < f b"
haftmann@21383
   733
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   734
  finally (order_less_le_trans) show ?thesis .
haftmann@21383
   735
qed
haftmann@21383
   736
haftmann@21383
   737
lemma order_subst1: "(a::'a::order) <= f b ==> (b::'b::order) <= c ==>
haftmann@21383
   738
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   739
proof -
haftmann@21383
   740
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   741
  assume "a <= f b"
haftmann@21383
   742
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   743
  finally (order_trans) show ?thesis .
haftmann@21383
   744
qed
haftmann@21383
   745
haftmann@21383
   746
lemma order_subst2: "(a::'a::order) <= b ==> f b <= (c::'c::order) ==>
haftmann@21383
   747
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   748
proof -
haftmann@21383
   749
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   750
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   751
  also assume "f b <= c"
haftmann@21383
   752
  finally (order_trans) show ?thesis .
haftmann@21383
   753
qed
haftmann@21383
   754
haftmann@21383
   755
lemma ord_le_eq_subst: "a <= b ==> f b = c ==>
haftmann@21383
   756
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   757
proof -
haftmann@21383
   758
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   759
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   760
  also assume "f b = c"
haftmann@21383
   761
  finally (ord_le_eq_trans) show ?thesis .
haftmann@21383
   762
qed
haftmann@21383
   763
haftmann@21383
   764
lemma ord_eq_le_subst: "a = f b ==> b <= c ==>
haftmann@21383
   765
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   766
proof -
haftmann@21383
   767
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   768
  assume "a = f b"
haftmann@21383
   769
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   770
  finally (ord_eq_le_trans) show ?thesis .
haftmann@21383
   771
qed
haftmann@21383
   772
haftmann@21383
   773
lemma ord_less_eq_subst: "a < b ==> f b = c ==>
haftmann@21383
   774
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   775
proof -
haftmann@21383
   776
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   777
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   778
  also assume "f b = c"
haftmann@21383
   779
  finally (ord_less_eq_trans) show ?thesis .
haftmann@21383
   780
qed
haftmann@21383
   781
haftmann@21383
   782
lemma ord_eq_less_subst: "a = f b ==> b < c ==>
haftmann@21383
   783
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   784
proof -
haftmann@21383
   785
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   786
  assume "a = f b"
haftmann@21383
   787
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@21383
   788
  finally (ord_eq_less_trans) show ?thesis .
haftmann@21383
   789
qed
haftmann@21383
   790
haftmann@21383
   791
text {*
haftmann@21383
   792
  Note that this list of rules is in reverse order of priorities.
haftmann@21383
   793
*}
haftmann@21383
   794
haftmann@27682
   795
lemmas [trans] =
haftmann@21383
   796
  order_less_subst2
haftmann@21383
   797
  order_less_subst1
haftmann@21383
   798
  order_le_less_subst2
haftmann@21383
   799
  order_le_less_subst1
haftmann@21383
   800
  order_less_le_subst2
haftmann@21383
   801
  order_less_le_subst1
haftmann@21383
   802
  order_subst2
haftmann@21383
   803
  order_subst1
haftmann@21383
   804
  ord_le_eq_subst
haftmann@21383
   805
  ord_eq_le_subst
haftmann@21383
   806
  ord_less_eq_subst
haftmann@21383
   807
  ord_eq_less_subst
haftmann@21383
   808
  forw_subst
haftmann@21383
   809
  back_subst
haftmann@21383
   810
  rev_mp
haftmann@21383
   811
  mp
haftmann@27682
   812
haftmann@27682
   813
lemmas (in order) [trans] =
haftmann@27682
   814
  neq_le_trans
haftmann@27682
   815
  le_neq_trans
haftmann@27682
   816
haftmann@27682
   817
lemmas (in preorder) [trans] =
haftmann@27682
   818
  less_trans
haftmann@27682
   819
  less_asym'
haftmann@27682
   820
  le_less_trans
haftmann@27682
   821
  less_le_trans
haftmann@21383
   822
  order_trans
haftmann@27682
   823
haftmann@27682
   824
lemmas (in order) [trans] =
haftmann@27682
   825
  antisym
haftmann@27682
   826
haftmann@27682
   827
lemmas (in ord) [trans] =
haftmann@27682
   828
  ord_le_eq_trans
haftmann@27682
   829
  ord_eq_le_trans
haftmann@27682
   830
  ord_less_eq_trans
haftmann@27682
   831
  ord_eq_less_trans
haftmann@27682
   832
haftmann@27682
   833
lemmas [trans] =
haftmann@27682
   834
  trans
haftmann@27682
   835
haftmann@27682
   836
lemmas order_trans_rules =
haftmann@27682
   837
  order_less_subst2
haftmann@27682
   838
  order_less_subst1
haftmann@27682
   839
  order_le_less_subst2
haftmann@27682
   840
  order_le_less_subst1
haftmann@27682
   841
  order_less_le_subst2
haftmann@27682
   842
  order_less_le_subst1
haftmann@27682
   843
  order_subst2
haftmann@27682
   844
  order_subst1
haftmann@27682
   845
  ord_le_eq_subst
haftmann@27682
   846
  ord_eq_le_subst
haftmann@27682
   847
  ord_less_eq_subst
haftmann@27682
   848
  ord_eq_less_subst
haftmann@27682
   849
  forw_subst
haftmann@27682
   850
  back_subst
haftmann@27682
   851
  rev_mp
haftmann@27682
   852
  mp
haftmann@27682
   853
  neq_le_trans
haftmann@27682
   854
  le_neq_trans
haftmann@27682
   855
  less_trans
haftmann@27682
   856
  less_asym'
haftmann@27682
   857
  le_less_trans
haftmann@27682
   858
  less_le_trans
haftmann@27682
   859
  order_trans
haftmann@27682
   860
  antisym
haftmann@21383
   861
  ord_le_eq_trans
haftmann@21383
   862
  ord_eq_le_trans
haftmann@21383
   863
  ord_less_eq_trans
haftmann@21383
   864
  ord_eq_less_trans
haftmann@21383
   865
  trans
haftmann@21383
   866
wenzelm@21180
   867
(* FIXME cleanup *)
wenzelm@21180
   868
haftmann@21083
   869
text {* These support proving chains of decreasing inequalities
haftmann@21083
   870
    a >= b >= c ... in Isar proofs. *}
haftmann@21083
   871
haftmann@21083
   872
lemma xt1:
haftmann@21083
   873
  "a = b ==> b > c ==> a > c"
haftmann@21083
   874
  "a > b ==> b = c ==> a > c"
haftmann@21083
   875
  "a = b ==> b >= c ==> a >= c"
haftmann@21083
   876
  "a >= b ==> b = c ==> a >= c"
haftmann@21083
   877
  "(x::'a::order) >= y ==> y >= x ==> x = y"
haftmann@21083
   878
  "(x::'a::order) >= y ==> y >= z ==> x >= z"
haftmann@21083
   879
  "(x::'a::order) > y ==> y >= z ==> x > z"
haftmann@21083
   880
  "(x::'a::order) >= y ==> y > z ==> x > z"
wenzelm@23417
   881
  "(a::'a::order) > b ==> b > a ==> P"
haftmann@21083
   882
  "(x::'a::order) > y ==> y > z ==> x > z"
haftmann@21083
   883
  "(a::'a::order) >= b ==> a ~= b ==> a > b"
haftmann@21083
   884
  "(a::'a::order) ~= b ==> a >= b ==> a > b"
haftmann@21083
   885
  "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c" 
haftmann@21083
   886
  "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   887
  "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
   888
  "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@25076
   889
  by auto
haftmann@21083
   890
haftmann@21083
   891
lemma xt2:
haftmann@21083
   892
  "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
   893
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
   894
haftmann@21083
   895
lemma xt3: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==> 
haftmann@21083
   896
    (!!x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@21083
   897
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
   898
haftmann@21083
   899
lemma xt4: "(a::'a::order) > f b ==> (b::'b::order) >= c ==>
haftmann@21083
   900
  (!!x y. x >= y ==> f x >= f y) ==> a > f c"
haftmann@21083
   901
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
   902
haftmann@21083
   903
lemma xt5: "(a::'a::order) > b ==> (f b::'b::order) >= c==>
haftmann@21083
   904
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   905
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
   906
haftmann@21083
   907
lemma xt6: "(a::'a::order) >= f b ==> b > c ==>
haftmann@21083
   908
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   909
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
   910
haftmann@21083
   911
lemma xt7: "(a::'a::order) >= b ==> (f b::'b::order) > c ==>
haftmann@21083
   912
    (!!x y. x >= y ==> f x >= f y) ==> f a > c"
haftmann@21083
   913
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
   914
haftmann@21083
   915
lemma xt8: "(a::'a::order) > f b ==> (b::'b::order) > c ==>
haftmann@21083
   916
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   917
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
   918
haftmann@21083
   919
lemma xt9: "(a::'a::order) > b ==> (f b::'b::order) > c ==>
haftmann@21083
   920
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   921
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
   922
haftmann@21083
   923
lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9
haftmann@21083
   924
haftmann@21083
   925
(* 
haftmann@21083
   926
  Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands
haftmann@21083
   927
  for the wrong thing in an Isar proof.
haftmann@21083
   928
haftmann@21083
   929
  The extra transitivity rules can be used as follows: 
haftmann@21083
   930
haftmann@21083
   931
lemma "(a::'a::order) > z"
haftmann@21083
   932
proof -
haftmann@21083
   933
  have "a >= b" (is "_ >= ?rhs")
haftmann@21083
   934
    sorry
haftmann@21083
   935
  also have "?rhs >= c" (is "_ >= ?rhs")
haftmann@21083
   936
    sorry
haftmann@21083
   937
  also (xtrans) have "?rhs = d" (is "_ = ?rhs")
haftmann@21083
   938
    sorry
haftmann@21083
   939
  also (xtrans) have "?rhs >= e" (is "_ >= ?rhs")
haftmann@21083
   940
    sorry
haftmann@21083
   941
  also (xtrans) have "?rhs > f" (is "_ > ?rhs")
haftmann@21083
   942
    sorry
haftmann@21083
   943
  also (xtrans) have "?rhs > z"
haftmann@21083
   944
    sorry
haftmann@21083
   945
  finally (xtrans) show ?thesis .
haftmann@21083
   946
qed
haftmann@21083
   947
haftmann@21083
   948
  Alternatively, one can use "declare xtrans [trans]" and then
haftmann@21083
   949
  leave out the "(xtrans)" above.
haftmann@21083
   950
*)
haftmann@21083
   951
haftmann@21546
   952
subsection {* Order on bool *}
haftmann@21546
   953
haftmann@26324
   954
instantiation bool :: order
haftmann@25510
   955
begin
haftmann@25510
   956
haftmann@25510
   957
definition
haftmann@25510
   958
  le_bool_def [code func del]: "P \<le> Q \<longleftrightarrow> P \<longrightarrow> Q"
haftmann@25510
   959
haftmann@25510
   960
definition
haftmann@25510
   961
  less_bool_def [code func del]: "(P\<Colon>bool) < Q \<longleftrightarrow> P \<le> Q \<and> P \<noteq> Q"
haftmann@25510
   962
haftmann@25510
   963
instance
haftmann@22916
   964
  by intro_classes (auto simp add: le_bool_def less_bool_def)
haftmann@25510
   965
haftmann@25510
   966
end
haftmann@21546
   967
haftmann@21546
   968
lemma le_boolI: "(P \<Longrightarrow> Q) \<Longrightarrow> P \<le> Q"
nipkow@23212
   969
by (simp add: le_bool_def)
haftmann@21546
   970
haftmann@21546
   971
lemma le_boolI': "P \<longrightarrow> Q \<Longrightarrow> P \<le> Q"
nipkow@23212
   972
by (simp add: le_bool_def)
haftmann@21546
   973
haftmann@21546
   974
lemma le_boolE: "P \<le> Q \<Longrightarrow> P \<Longrightarrow> (Q \<Longrightarrow> R) \<Longrightarrow> R"
nipkow@23212
   975
by (simp add: le_bool_def)
haftmann@21546
   976
haftmann@21546
   977
lemma le_boolD: "P \<le> Q \<Longrightarrow> P \<longrightarrow> Q"
nipkow@23212
   978
by (simp add: le_bool_def)
haftmann@21546
   979
haftmann@22348
   980
lemma [code func]:
haftmann@22348
   981
  "False \<le> b \<longleftrightarrow> True"
haftmann@22348
   982
  "True \<le> b \<longleftrightarrow> b"
haftmann@22348
   983
  "False < b \<longleftrightarrow> b"
haftmann@22348
   984
  "True < b \<longleftrightarrow> False"
haftmann@22348
   985
  unfolding le_bool_def less_bool_def by simp_all
haftmann@22348
   986
haftmann@22424
   987
haftmann@23881
   988
subsection {* Order on functions *}
haftmann@23881
   989
haftmann@25510
   990
instantiation "fun" :: (type, ord) ord
haftmann@25510
   991
begin
haftmann@25510
   992
haftmann@25510
   993
definition
haftmann@25510
   994
  le_fun_def [code func del]: "f \<le> g \<longleftrightarrow> (\<forall>x. f x \<le> g x)"
haftmann@23881
   995
haftmann@25510
   996
definition
haftmann@25510
   997
  less_fun_def [code func del]: "(f\<Colon>'a \<Rightarrow> 'b) < g \<longleftrightarrow> f \<le> g \<and> f \<noteq> g"
haftmann@25510
   998
haftmann@25510
   999
instance ..
haftmann@25510
  1000
haftmann@25510
  1001
end
haftmann@23881
  1002
haftmann@23881
  1003
instance "fun" :: (type, order) order
haftmann@23881
  1004
  by default
berghofe@26796
  1005
    (auto simp add: le_fun_def less_fun_def
berghofe@26796
  1006
       intro: order_trans order_antisym intro!: ext)
haftmann@23881
  1007
haftmann@23881
  1008
lemma le_funI: "(\<And>x. f x \<le> g x) \<Longrightarrow> f \<le> g"
haftmann@23881
  1009
  unfolding le_fun_def by simp
haftmann@23881
  1010
haftmann@23881
  1011
lemma le_funE: "f \<le> g \<Longrightarrow> (f x \<le> g x \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@23881
  1012
  unfolding le_fun_def by simp
haftmann@23881
  1013
haftmann@23881
  1014
lemma le_funD: "f \<le> g \<Longrightarrow> f x \<le> g x"
haftmann@23881
  1015
  unfolding le_fun_def by simp
haftmann@23881
  1016
haftmann@23881
  1017
text {*
haftmann@23881
  1018
  Handy introduction and elimination rules for @{text "\<le>"}
haftmann@23881
  1019
  on unary and binary predicates
haftmann@23881
  1020
*}
haftmann@23881
  1021
berghofe@26796
  1022
lemma predicate1I:
haftmann@23881
  1023
  assumes PQ: "\<And>x. P x \<Longrightarrow> Q x"
haftmann@23881
  1024
  shows "P \<le> Q"
haftmann@23881
  1025
  apply (rule le_funI)
haftmann@23881
  1026
  apply (rule le_boolI)
haftmann@23881
  1027
  apply (rule PQ)
haftmann@23881
  1028
  apply assumption
haftmann@23881
  1029
  done
haftmann@23881
  1030
haftmann@23881
  1031
lemma predicate1D [Pure.dest, dest]: "P \<le> Q \<Longrightarrow> P x \<Longrightarrow> Q x"
haftmann@23881
  1032
  apply (erule le_funE)
haftmann@23881
  1033
  apply (erule le_boolE)
haftmann@23881
  1034
  apply assumption+
haftmann@23881
  1035
  done
haftmann@23881
  1036
haftmann@23881
  1037
lemma predicate2I [Pure.intro!, intro!]:
haftmann@23881
  1038
  assumes PQ: "\<And>x y. P x y \<Longrightarrow> Q x y"
haftmann@23881
  1039
  shows "P \<le> Q"
haftmann@23881
  1040
  apply (rule le_funI)+
haftmann@23881
  1041
  apply (rule le_boolI)
haftmann@23881
  1042
  apply (rule PQ)
haftmann@23881
  1043
  apply assumption
haftmann@23881
  1044
  done
haftmann@23881
  1045
haftmann@23881
  1046
lemma predicate2D [Pure.dest, dest]: "P \<le> Q \<Longrightarrow> P x y \<Longrightarrow> Q x y"
haftmann@23881
  1047
  apply (erule le_funE)+
haftmann@23881
  1048
  apply (erule le_boolE)
haftmann@23881
  1049
  apply assumption+
haftmann@23881
  1050
  done
haftmann@23881
  1051
haftmann@23881
  1052
lemma rev_predicate1D: "P x ==> P <= Q ==> Q x"
haftmann@23881
  1053
  by (rule predicate1D)
haftmann@23881
  1054
haftmann@23881
  1055
lemma rev_predicate2D: "P x y ==> P <= Q ==> Q x y"
haftmann@23881
  1056
  by (rule predicate2D)
haftmann@23881
  1057
haftmann@23881
  1058
haftmann@23881
  1059
subsection {* Monotonicity, least value operator and min/max *}
haftmann@21083
  1060
haftmann@25076
  1061
context order
haftmann@25076
  1062
begin
haftmann@25076
  1063
haftmann@25076
  1064
definition
haftmann@25076
  1065
  mono :: "('a \<Rightarrow> 'b\<Colon>order) \<Rightarrow> bool"
haftmann@25076
  1066
where
haftmann@25076
  1067
  "mono f \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> f x \<le> f y)"
haftmann@25076
  1068
haftmann@25076
  1069
lemma monoI [intro?]:
haftmann@25076
  1070
  fixes f :: "'a \<Rightarrow> 'b\<Colon>order"
haftmann@25076
  1071
  shows "(\<And>x y. x \<le> y \<Longrightarrow> f x \<le> f y) \<Longrightarrow> mono f"
haftmann@25076
  1072
  unfolding mono_def by iprover
haftmann@21216
  1073
haftmann@25076
  1074
lemma monoD [dest?]:
haftmann@25076
  1075
  fixes f :: "'a \<Rightarrow> 'b\<Colon>order"
haftmann@25076
  1076
  shows "mono f \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
haftmann@25076
  1077
  unfolding mono_def by iprover
haftmann@25076
  1078
haftmann@25076
  1079
end
haftmann@25076
  1080
haftmann@25076
  1081
context linorder
haftmann@25076
  1082
begin
haftmann@25076
  1083
haftmann@25076
  1084
lemma min_of_mono:
haftmann@25076
  1085
  fixes f :: "'a \<Rightarrow> 'b\<Colon>linorder"
wenzelm@25377
  1086
  shows "mono f \<Longrightarrow> min (f m) (f n) = f (min m n)"
haftmann@25076
  1087
  by (auto simp: mono_def Orderings.min_def min_def intro: Orderings.antisym)
haftmann@25076
  1088
haftmann@25076
  1089
lemma max_of_mono:
haftmann@25076
  1090
  fixes f :: "'a \<Rightarrow> 'b\<Colon>linorder"
wenzelm@25377
  1091
  shows "mono f \<Longrightarrow> max (f m) (f n) = f (max m n)"
haftmann@25076
  1092
  by (auto simp: mono_def Orderings.max_def max_def intro: Orderings.antisym)
haftmann@25076
  1093
haftmann@25076
  1094
end
haftmann@21083
  1095
haftmann@21383
  1096
lemma min_leastL: "(!!x. least <= x) ==> min least x = least"
nipkow@23212
  1097
by (simp add: min_def)
haftmann@21383
  1098
haftmann@21383
  1099
lemma max_leastL: "(!!x. least <= x) ==> max least x = x"
nipkow@23212
  1100
by (simp add: max_def)
haftmann@21383
  1101
haftmann@21383
  1102
lemma min_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> min x least = least"
nipkow@23212
  1103
apply (simp add: min_def)
nipkow@23212
  1104
apply (blast intro: order_antisym)
nipkow@23212
  1105
done
haftmann@21383
  1106
haftmann@21383
  1107
lemma max_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> max x least = x"
nipkow@23212
  1108
apply (simp add: max_def)
nipkow@23212
  1109
apply (blast intro: order_antisym)
nipkow@23212
  1110
done
haftmann@21383
  1111
haftmann@27823
  1112
haftmann@27823
  1113
subsection {* Dense orders *}
haftmann@27823
  1114
haftmann@27823
  1115
class dense_linear_order = linorder + 
haftmann@27823
  1116
  assumes gt_ex: "\<exists>y. x < y" 
haftmann@27823
  1117
  and lt_ex: "\<exists>y. y < x"
haftmann@27823
  1118
  and dense: "x < y \<Longrightarrow> (\<exists>z. x < z \<and> z < y)"
haftmann@27823
  1119
  (*see further theory Dense_Linear_Order*)
haftmann@27823
  1120
haftmann@27823
  1121
haftmann@27823
  1122
subsection {* Wellorders *}
haftmann@27823
  1123
haftmann@27823
  1124
class wellorder = linorder +
haftmann@27823
  1125
  assumes less_induct [case_names less]: "(\<And>x. (\<And>y. y < x \<Longrightarrow> P y) \<Longrightarrow> P x) \<Longrightarrow> P a"
haftmann@27823
  1126
begin
haftmann@27823
  1127
haftmann@27823
  1128
lemma wellorder_Least_lemma:
haftmann@27823
  1129
  fixes k :: 'a
haftmann@27823
  1130
  assumes "P k"
haftmann@27823
  1131
  shows "P (LEAST x. P x)" and "(LEAST x. P x) \<le> k"
haftmann@27823
  1132
proof -
haftmann@27823
  1133
  have "P (LEAST x. P x) \<and> (LEAST x. P x) \<le> k"
haftmann@27823
  1134
  using assms proof (induct k rule: less_induct)
haftmann@27823
  1135
    case (less x) then have "P x" by simp
haftmann@27823
  1136
    show ?case proof (rule classical)
haftmann@27823
  1137
      assume assm: "\<not> (P (LEAST a. P a) \<and> (LEAST a. P a) \<le> x)"
haftmann@27823
  1138
      have "\<And>y. P y \<Longrightarrow> x \<le> y"
haftmann@27823
  1139
      proof (rule classical)
haftmann@27823
  1140
        fix y
haftmann@27823
  1141
        assume "P y" and "\<not> x \<le> y" 
haftmann@27823
  1142
        with less have "P (LEAST a. P a)" and "(LEAST a. P a) \<le> y"
haftmann@27823
  1143
          by (auto simp add: not_le)
haftmann@27823
  1144
        with assm have "x < (LEAST a. P a)" and "(LEAST a. P a) \<le> y"
haftmann@27823
  1145
          by auto
haftmann@27823
  1146
        then show "x \<le> y" by auto
haftmann@27823
  1147
      qed
haftmann@27823
  1148
      with `P x` have Least: "(LEAST a. P a) = x"
haftmann@27823
  1149
        by (rule Least_equality)
haftmann@27823
  1150
      with `P x` show ?thesis by simp
haftmann@27823
  1151
    qed
haftmann@27823
  1152
  qed
haftmann@27823
  1153
  then show "P (LEAST x. P x)" and "(LEAST x. P x) \<le> k" by auto
haftmann@27823
  1154
qed
haftmann@27823
  1155
haftmann@27823
  1156
lemmas LeastI   = wellorder_Least_lemma(1)
haftmann@27823
  1157
lemmas Least_le = wellorder_Least_lemma(2)
haftmann@27823
  1158
haftmann@27823
  1159
-- "The following 3 lemmas are due to Brian Huffman"
haftmann@27823
  1160
lemma LeastI_ex: "\<exists>x. P x \<Longrightarrow> P (Least P)"
haftmann@27823
  1161
  by (erule exE) (erule LeastI)
haftmann@27823
  1162
haftmann@27823
  1163
lemma LeastI2:
haftmann@27823
  1164
  "P a \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> Q (Least P)"
haftmann@27823
  1165
  by (blast intro: LeastI)
haftmann@27823
  1166
haftmann@27823
  1167
lemma LeastI2_ex:
haftmann@27823
  1168
  "\<exists>a. P a \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> Q (Least P)"
haftmann@27823
  1169
  by (blast intro: LeastI_ex)
haftmann@27823
  1170
haftmann@27823
  1171
lemma not_less_Least: "k < (LEAST x. P x) \<Longrightarrow> \<not> P k"
haftmann@27823
  1172
apply (simp (no_asm_use) add: not_le [symmetric])
haftmann@27823
  1173
apply (erule contrapos_nn)
haftmann@27823
  1174
apply (erule Least_le)
haftmann@27823
  1175
done
haftmann@27823
  1176
haftmann@27823
  1177
end  
haftmann@27823
  1178
nipkow@15524
  1179
end