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