src/HOL/Orderings.thy
author haftmann
Fri Mar 17 09:34:23 2006 +0100 (2006-03-17)
changeset 19277 f7602e74d948
parent 19039 8eae46249628
child 19527 9b5c38e8e780
permissions -rw-r--r--
renamed op < <= to Orderings.less(_eq)
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
FIXME: derive more of the min/max laws generically via semilattices
nipkow@15524
     6
*)
nipkow@15524
     7
nipkow@15524
     8
header {* Type classes for $\le$ *}
nipkow@15524
     9
nipkow@15524
    10
theory Orderings
nipkow@15524
    11
imports Lattice_Locales
haftmann@16417
    12
uses ("antisym_setup.ML")
nipkow@15524
    13
begin
nipkow@15524
    14
nipkow@15524
    15
subsection {* Order signatures and orders *}
nipkow@15524
    16
nipkow@15524
    17
axclass
nipkow@15524
    18
  ord < type
nipkow@15524
    19
nipkow@15524
    20
syntax
haftmann@19277
    21
  "less"        :: "['a::ord, 'a] => bool"             ("op <")
haftmann@19277
    22
  "less_eq"     :: "['a::ord, 'a] => bool"             ("op <=")
nipkow@15524
    23
nipkow@15524
    24
consts
haftmann@19277
    25
  "less"        :: "['a::ord, 'a] => bool"             ("(_/ < _)"  [50, 51] 50)
haftmann@19277
    26
  "less_eq"     :: "['a::ord, 'a] => bool"             ("(_/ <= _)" [50, 51] 50)
nipkow@15524
    27
nipkow@15524
    28
syntax (xsymbols)
haftmann@19277
    29
  "less_eq"     :: "['a::ord, 'a] => bool"             ("op \<le>")
haftmann@19277
    30
  "less_eq"     :: "['a::ord, 'a] => bool"             ("(_/ \<le> _)"  [50, 51] 50)
nipkow@15524
    31
nipkow@15524
    32
syntax (HTML output)
haftmann@19277
    33
  "less_eq"     :: "['a::ord, 'a] => bool"             ("op \<le>")
haftmann@19277
    34
  "less_eq"     :: "['a::ord, 'a] => bool"             ("(_/ \<le> _)"  [50, 51] 50)
nipkow@15524
    35
nipkow@15524
    36
text{* Syntactic sugar: *}
nipkow@15524
    37
wenzelm@15822
    38
syntax
nipkow@15524
    39
  "_gt" :: "'a::ord => 'a => bool"             (infixl ">" 50)
nipkow@15524
    40
  "_ge" :: "'a::ord => 'a => bool"             (infixl ">=" 50)
nipkow@15524
    41
translations
nipkow@15524
    42
  "x > y"  => "y < x"
nipkow@15524
    43
  "x >= y" => "y <= x"
nipkow@15524
    44
nipkow@15524
    45
syntax (xsymbols)
nipkow@15524
    46
  "_ge"       :: "'a::ord => 'a => bool"             (infixl "\<ge>" 50)
nipkow@15524
    47
nipkow@15524
    48
syntax (HTML output)
nipkow@15524
    49
  "_ge"       :: "['a::ord, 'a] => bool"             (infixl "\<ge>" 50)
nipkow@15524
    50
nipkow@15524
    51
nipkow@15524
    52
subsection {* Monotonicity *}
nipkow@15524
    53
nipkow@15524
    54
locale mono =
nipkow@15524
    55
  fixes f
nipkow@15524
    56
  assumes mono: "A <= B ==> f A <= f B"
nipkow@15524
    57
nipkow@15524
    58
lemmas monoI [intro?] = mono.intro
nipkow@15524
    59
  and monoD [dest?] = mono.mono
nipkow@15524
    60
nipkow@15524
    61
constdefs
nipkow@15524
    62
  min :: "['a::ord, 'a] => 'a"
nipkow@15524
    63
  "min a b == (if a <= b then a else b)"
nipkow@15524
    64
  max :: "['a::ord, 'a] => 'a"
nipkow@15524
    65
  "max a b == (if a <= b then b else a)"
nipkow@15524
    66
nipkow@15524
    67
lemma min_leastL: "(!!x. least <= x) ==> min least x = least"
nipkow@15524
    68
  by (simp add: min_def)
nipkow@15524
    69
nipkow@15524
    70
lemma min_of_mono:
nipkow@15524
    71
    "ALL x y. (f x <= f y) = (x <= y) ==> min (f m) (f n) = f (min m n)"
nipkow@15524
    72
  by (simp add: min_def)
nipkow@15524
    73
nipkow@15524
    74
lemma max_leastL: "(!!x. least <= x) ==> max least x = x"
nipkow@15524
    75
  by (simp add: max_def)
nipkow@15524
    76
nipkow@15524
    77
lemma max_of_mono:
nipkow@15524
    78
    "ALL x y. (f x <= f y) = (x <= y) ==> max (f m) (f n) = f (max m n)"
nipkow@15524
    79
  by (simp add: max_def)
nipkow@15524
    80
nipkow@15524
    81
nipkow@15524
    82
subsection "Orders"
nipkow@15524
    83
nipkow@15524
    84
axclass order < ord
nipkow@15524
    85
  order_refl [iff]: "x <= x"
nipkow@15524
    86
  order_trans: "x <= y ==> y <= z ==> x <= z"
nipkow@15524
    87
  order_antisym: "x <= y ==> y <= x ==> x = y"
nipkow@15524
    88
  order_less_le: "(x < y) = (x <= y & x ~= y)"
nipkow@15524
    89
nipkow@15524
    90
text{* Connection to locale: *}
nipkow@15524
    91
ballarin@15837
    92
interpretation order:
nipkow@15780
    93
  partial_order["op \<le> :: 'a::order \<Rightarrow> 'a \<Rightarrow> bool"]
nipkow@15524
    94
apply(rule partial_order.intro)
nipkow@15524
    95
apply(rule order_refl, erule (1) order_trans, erule (1) order_antisym)
nipkow@15524
    96
done
nipkow@15524
    97
nipkow@15524
    98
text {* Reflexivity. *}
nipkow@15524
    99
nipkow@15524
   100
lemma order_eq_refl: "!!x::'a::order. x = y ==> x <= y"
nipkow@15524
   101
    -- {* This form is useful with the classical reasoner. *}
nipkow@15524
   102
  apply (erule ssubst)
nipkow@15524
   103
  apply (rule order_refl)
nipkow@15524
   104
  done
nipkow@15524
   105
nipkow@15524
   106
lemma order_less_irrefl [iff]: "~ x < (x::'a::order)"
nipkow@15524
   107
  by (simp add: order_less_le)
nipkow@15524
   108
nipkow@15524
   109
lemma order_le_less: "((x::'a::order) <= y) = (x < y | x = y)"
nipkow@15524
   110
    -- {* NOT suitable for iff, since it can cause PROOF FAILED. *}
nipkow@15524
   111
  apply (simp add: order_less_le, blast)
nipkow@15524
   112
  done
nipkow@15524
   113
nipkow@15524
   114
lemmas order_le_imp_less_or_eq = order_le_less [THEN iffD1, standard]
nipkow@15524
   115
nipkow@15524
   116
lemma order_less_imp_le: "!!x::'a::order. x < y ==> x <= y"
nipkow@15524
   117
  by (simp add: order_less_le)
nipkow@15524
   118
nipkow@15524
   119
nipkow@15524
   120
text {* Asymmetry. *}
nipkow@15524
   121
nipkow@15524
   122
lemma order_less_not_sym: "(x::'a::order) < y ==> ~ (y < x)"
nipkow@15524
   123
  by (simp add: order_less_le order_antisym)
nipkow@15524
   124
nipkow@15524
   125
lemma order_less_asym: "x < (y::'a::order) ==> (~P ==> y < x) ==> P"
nipkow@15524
   126
  apply (drule order_less_not_sym)
nipkow@15524
   127
  apply (erule contrapos_np, simp)
nipkow@15524
   128
  done
nipkow@15524
   129
nipkow@15524
   130
lemma order_eq_iff: "!!x::'a::order. (x = y) = (x \<le> y & y \<le> x)"
nipkow@15524
   131
by (blast intro: order_antisym)
nipkow@15524
   132
nipkow@15524
   133
lemma order_antisym_conv: "(y::'a::order) <= x ==> (x <= y) = (x = y)"
nipkow@15524
   134
by(blast intro:order_antisym)
nipkow@15524
   135
nipkow@15524
   136
text {* Transitivity. *}
nipkow@15524
   137
nipkow@15524
   138
lemma order_less_trans: "!!x::'a::order. [| x < y; y < z |] ==> x < z"
nipkow@15524
   139
  apply (simp add: order_less_le)
nipkow@15524
   140
  apply (blast intro: order_trans order_antisym)
nipkow@15524
   141
  done
nipkow@15524
   142
nipkow@15524
   143
lemma order_le_less_trans: "!!x::'a::order. [| x <= y; y < z |] ==> x < z"
nipkow@15524
   144
  apply (simp add: order_less_le)
nipkow@15524
   145
  apply (blast intro: order_trans order_antisym)
nipkow@15524
   146
  done
nipkow@15524
   147
nipkow@15524
   148
lemma order_less_le_trans: "!!x::'a::order. [| x < y; y <= z |] ==> x < z"
nipkow@15524
   149
  apply (simp add: order_less_le)
nipkow@15524
   150
  apply (blast intro: order_trans order_antisym)
nipkow@15524
   151
  done
nipkow@15524
   152
nipkow@15524
   153
nipkow@15524
   154
text {* Useful for simplification, but too risky to include by default. *}
nipkow@15524
   155
nipkow@15524
   156
lemma order_less_imp_not_less: "(x::'a::order) < y ==>  (~ y < x) = True"
nipkow@15524
   157
  by (blast elim: order_less_asym)
nipkow@15524
   158
nipkow@15524
   159
lemma order_less_imp_triv: "(x::'a::order) < y ==>  (y < x --> P) = True"
nipkow@15524
   160
  by (blast elim: order_less_asym)
nipkow@15524
   161
nipkow@15524
   162
lemma order_less_imp_not_eq: "(x::'a::order) < y ==>  (x = y) = False"
nipkow@15524
   163
  by auto
nipkow@15524
   164
nipkow@15524
   165
lemma order_less_imp_not_eq2: "(x::'a::order) < y ==>  (y = x) = False"
nipkow@15524
   166
  by auto
nipkow@15524
   167
nipkow@15524
   168
nipkow@15524
   169
text {* Other operators. *}
nipkow@15524
   170
nipkow@15524
   171
lemma min_leastR: "(!!x::'a::order. least <= x) ==> min x least = least"
nipkow@15524
   172
  apply (simp add: min_def)
nipkow@15524
   173
  apply (blast intro: order_antisym)
nipkow@15524
   174
  done
nipkow@15524
   175
nipkow@15524
   176
lemma max_leastR: "(!!x::'a::order. least <= x) ==> max x least = x"
nipkow@15524
   177
  apply (simp add: max_def)
nipkow@15524
   178
  apply (blast intro: order_antisym)
nipkow@15524
   179
  done
nipkow@15524
   180
nipkow@15524
   181
nipkow@15524
   182
subsection {* Transitivity rules for calculational reasoning *}
nipkow@15524
   183
nipkow@15524
   184
nipkow@15524
   185
lemma order_neq_le_trans: "a ~= b ==> (a::'a::order) <= b ==> a < b"
nipkow@15524
   186
  by (simp add: order_less_le)
nipkow@15524
   187
nipkow@15524
   188
lemma order_le_neq_trans: "(a::'a::order) <= b ==> a ~= b ==> a < b"
nipkow@15524
   189
  by (simp add: order_less_le)
nipkow@15524
   190
nipkow@15524
   191
lemma order_less_asym': "(a::'a::order) < b ==> b < a ==> P"
nipkow@15524
   192
  by (rule order_less_asym)
nipkow@15524
   193
nipkow@15524
   194
nipkow@15524
   195
subsection {* Least value operator *}
nipkow@15524
   196
nipkow@15524
   197
constdefs
nipkow@15524
   198
  Least :: "('a::ord => bool) => 'a"               (binder "LEAST " 10)
nipkow@15524
   199
  "Least P == THE x. P x & (ALL y. P y --> x <= y)"
nipkow@15524
   200
    -- {* We can no longer use LeastM because the latter requires Hilbert-AC. *}
nipkow@15524
   201
nipkow@15950
   202
lemma LeastI2_order:
nipkow@15524
   203
  "[| P (x::'a::order);
nipkow@15524
   204
      !!y. P y ==> x <= y;
nipkow@15524
   205
      !!x. [| P x; ALL y. P y --> x \<le> y |] ==> Q x |]
nipkow@15524
   206
   ==> Q (Least P)"
nipkow@15524
   207
  apply (unfold Least_def)
nipkow@15524
   208
  apply (rule theI2)
nipkow@15524
   209
    apply (blast intro: order_antisym)+
nipkow@15524
   210
  done
nipkow@15524
   211
nipkow@15524
   212
lemma Least_equality:
nipkow@15524
   213
    "[| P (k::'a::order); !!x. P x ==> k <= x |] ==> (LEAST x. P x) = k"
nipkow@15524
   214
  apply (simp add: Least_def)
nipkow@15524
   215
  apply (rule the_equality)
nipkow@15524
   216
  apply (auto intro!: order_antisym)
nipkow@15524
   217
  done
nipkow@15524
   218
nipkow@15524
   219
nipkow@15524
   220
subsection "Linear / total orders"
nipkow@15524
   221
nipkow@15524
   222
axclass linorder < order
nipkow@15524
   223
  linorder_linear: "x <= y | y <= x"
nipkow@15524
   224
nipkow@15524
   225
lemma linorder_less_linear: "!!x::'a::linorder. x<y | x=y | y<x"
nipkow@15524
   226
  apply (simp add: order_less_le)
nipkow@15524
   227
  apply (insert linorder_linear, blast)
nipkow@15524
   228
  done
nipkow@15524
   229
nipkow@15524
   230
lemma linorder_le_less_linear: "!!x::'a::linorder. x\<le>y | y<x"
nipkow@15524
   231
  by (simp add: order_le_less linorder_less_linear)
nipkow@15524
   232
nipkow@15524
   233
lemma linorder_le_cases [case_names le ge]:
nipkow@15524
   234
    "((x::'a::linorder) \<le> y ==> P) ==> (y \<le> x ==> P) ==> P"
nipkow@15524
   235
  by (insert linorder_linear, blast)
nipkow@15524
   236
nipkow@15524
   237
lemma linorder_cases [case_names less equal greater]:
nipkow@15524
   238
    "((x::'a::linorder) < y ==> P) ==> (x = y ==> P) ==> (y < x ==> P) ==> P"
nipkow@15524
   239
  by (insert linorder_less_linear, blast)
nipkow@15524
   240
nipkow@15524
   241
lemma linorder_not_less: "!!x::'a::linorder. (~ x < y) = (y <= x)"
nipkow@15524
   242
  apply (simp add: order_less_le)
nipkow@15524
   243
  apply (insert linorder_linear)
nipkow@15524
   244
  apply (blast intro: order_antisym)
nipkow@15524
   245
  done
nipkow@15524
   246
nipkow@15524
   247
lemma linorder_not_le: "!!x::'a::linorder. (~ x <= y) = (y < x)"
nipkow@15524
   248
  apply (simp add: order_less_le)
nipkow@15524
   249
  apply (insert linorder_linear)
nipkow@15524
   250
  apply (blast intro: order_antisym)
nipkow@15524
   251
  done
nipkow@15524
   252
nipkow@15524
   253
lemma linorder_neq_iff: "!!x::'a::linorder. (x ~= y) = (x<y | y<x)"
nipkow@15524
   254
by (cut_tac x = x and y = y in linorder_less_linear, auto)
nipkow@15524
   255
nipkow@15524
   256
lemma linorder_neqE: "x ~= (y::'a::linorder) ==> (x < y ==> R) ==> (y < x ==> R) ==> R"
nipkow@15524
   257
by (simp add: linorder_neq_iff, blast)
nipkow@15524
   258
nipkow@15524
   259
lemma linorder_antisym_conv1: "~ (x::'a::linorder) < y ==> (x <= y) = (x = y)"
nipkow@15524
   260
by(blast intro:order_antisym dest:linorder_not_less[THEN iffD1])
nipkow@15524
   261
nipkow@15524
   262
lemma linorder_antisym_conv2: "(x::'a::linorder) <= y ==> (~ x < y) = (x = y)"
nipkow@15524
   263
by(blast intro:order_antisym dest:linorder_not_less[THEN iffD1])
nipkow@15524
   264
nipkow@15524
   265
lemma linorder_antisym_conv3: "~ (y::'a::linorder) < x ==> (~ x < y) = (x = y)"
nipkow@15524
   266
by(blast intro:order_antisym dest:linorder_not_less[THEN iffD1])
nipkow@15524
   267
paulson@16796
   268
text{*Replacing the old Nat.leI*}
paulson@16796
   269
lemma leI: "~ x < y ==> y <= (x::'a::linorder)"
paulson@16796
   270
  by (simp only: linorder_not_less)
paulson@16796
   271
paulson@16796
   272
lemma leD: "y <= (x::'a::linorder) ==> ~ x < y"
paulson@16796
   273
  by (simp only: linorder_not_less)
paulson@16796
   274
paulson@16796
   275
(*FIXME inappropriate name (or delete altogether)*)
paulson@16796
   276
lemma not_leE: "~ y <= (x::'a::linorder) ==> x < y"
paulson@16796
   277
  by (simp only: linorder_not_le)
paulson@16796
   278
nipkow@15524
   279
use "antisym_setup.ML";
nipkow@15524
   280
setup antisym_setup
nipkow@15524
   281
nipkow@15524
   282
subsection {* Setup of transitivity reasoner as Solver *}
nipkow@15524
   283
nipkow@15524
   284
lemma less_imp_neq: "[| (x::'a::order) < y |] ==> x ~= y"
nipkow@15524
   285
  by (erule contrapos_pn, erule subst, rule order_less_irrefl)
nipkow@15524
   286
nipkow@15524
   287
lemma eq_neq_eq_imp_neq: "[| x = a ; a ~= b; b = y |] ==> x ~= y"
nipkow@15524
   288
  by (erule subst, erule ssubst, assumption)
nipkow@15524
   289
nipkow@15524
   290
ML_setup {*
nipkow@15524
   291
nipkow@15524
   292
(* The setting up of Quasi_Tac serves as a demo.  Since there is no
nipkow@15524
   293
   class for quasi orders, the tactics Quasi_Tac.trans_tac and
nipkow@15524
   294
   Quasi_Tac.quasi_tac are not of much use. *)
nipkow@15524
   295
nipkow@15524
   296
fun decomp_gen sort sign (Trueprop $ t) =
ballarin@15622
   297
  let fun of_sort t = let val T = type_of t in
ballarin@15622
   298
        (* exclude numeric types: linear arithmetic subsumes transitivity *)
ballarin@15622
   299
        T <> HOLogic.natT andalso T <> HOLogic.intT andalso
ballarin@15622
   300
        T <> HOLogic.realT andalso Sign.of_sort sign (T, sort) end
nipkow@15524
   301
  fun dec (Const ("Not", _) $ t) = (
nipkow@15524
   302
	  case dec t of
skalberg@15531
   303
	    NONE => NONE
skalberg@15531
   304
	  | SOME (t1, rel, t2) => SOME (t1, "~" ^ rel, t2))
nipkow@15524
   305
	| dec (Const ("op =",  _) $ t1 $ t2) =
nipkow@15524
   306
	    if of_sort t1
skalberg@15531
   307
	    then SOME (t1, "=", t2)
skalberg@15531
   308
	    else NONE
haftmann@19277
   309
	| dec (Const ("Orderings.less_eq",  _) $ t1 $ t2) =
nipkow@15524
   310
	    if of_sort t1
skalberg@15531
   311
	    then SOME (t1, "<=", t2)
skalberg@15531
   312
	    else NONE
haftmann@19277
   313
	| dec (Const ("Orderings.less",  _) $ t1 $ t2) =
nipkow@15524
   314
	    if of_sort t1
skalberg@15531
   315
	    then SOME (t1, "<", t2)
skalberg@15531
   316
	    else NONE
skalberg@15531
   317
	| dec _ = NONE
nipkow@15524
   318
  in dec t end;
nipkow@15524
   319
nipkow@15524
   320
structure Quasi_Tac = Quasi_Tac_Fun (
nipkow@15524
   321
  struct
nipkow@15524
   322
    val le_trans = thm "order_trans";
nipkow@15524
   323
    val le_refl = thm "order_refl";
nipkow@15524
   324
    val eqD1 = thm "order_eq_refl";
nipkow@15524
   325
    val eqD2 = thm "sym" RS thm "order_eq_refl";
nipkow@15524
   326
    val less_reflE = thm "order_less_irrefl" RS thm "notE";
nipkow@15524
   327
    val less_imp_le = thm "order_less_imp_le";
nipkow@15524
   328
    val le_neq_trans = thm "order_le_neq_trans";
nipkow@15524
   329
    val neq_le_trans = thm "order_neq_le_trans";
nipkow@15524
   330
    val less_imp_neq = thm "less_imp_neq";
nipkow@15524
   331
    val decomp_trans = decomp_gen ["Orderings.order"];
nipkow@15524
   332
    val decomp_quasi = decomp_gen ["Orderings.order"];
nipkow@15524
   333
nipkow@15524
   334
  end);  (* struct *)
nipkow@15524
   335
nipkow@15524
   336
structure Order_Tac = Order_Tac_Fun (
nipkow@15524
   337
  struct
nipkow@15524
   338
    val less_reflE = thm "order_less_irrefl" RS thm "notE";
nipkow@15524
   339
    val le_refl = thm "order_refl";
nipkow@15524
   340
    val less_imp_le = thm "order_less_imp_le";
nipkow@15524
   341
    val not_lessI = thm "linorder_not_less" RS thm "iffD2";
nipkow@15524
   342
    val not_leI = thm "linorder_not_le" RS thm "iffD2";
nipkow@15524
   343
    val not_lessD = thm "linorder_not_less" RS thm "iffD1";
nipkow@15524
   344
    val not_leD = thm "linorder_not_le" RS thm "iffD1";
nipkow@15524
   345
    val eqI = thm "order_antisym";
nipkow@15524
   346
    val eqD1 = thm "order_eq_refl";
nipkow@15524
   347
    val eqD2 = thm "sym" RS thm "order_eq_refl";
nipkow@15524
   348
    val less_trans = thm "order_less_trans";
nipkow@15524
   349
    val less_le_trans = thm "order_less_le_trans";
nipkow@15524
   350
    val le_less_trans = thm "order_le_less_trans";
nipkow@15524
   351
    val le_trans = thm "order_trans";
nipkow@15524
   352
    val le_neq_trans = thm "order_le_neq_trans";
nipkow@15524
   353
    val neq_le_trans = thm "order_neq_le_trans";
nipkow@15524
   354
    val less_imp_neq = thm "less_imp_neq";
nipkow@15524
   355
    val eq_neq_eq_imp_neq = thm "eq_neq_eq_imp_neq";
obua@16743
   356
    val not_sym = thm "not_sym";
nipkow@15524
   357
    val decomp_part = decomp_gen ["Orderings.order"];
nipkow@15524
   358
    val decomp_lin = decomp_gen ["Orderings.linorder"];
nipkow@15524
   359
nipkow@15524
   360
  end);  (* struct *)
nipkow@15524
   361
wenzelm@17876
   362
change_simpset (fn ss => ss
nipkow@15524
   363
    addSolver (mk_solver "Trans_linear" (fn _ => Order_Tac.linear_tac))
wenzelm@17876
   364
    addSolver (mk_solver "Trans_partial" (fn _ => Order_Tac.partial_tac)));
nipkow@15524
   365
  (* Adding the transitivity reasoners also as safe solvers showed a slight
nipkow@15524
   366
     speed up, but the reasoning strength appears to be not higher (at least
nipkow@15524
   367
     no breaking of additional proofs in the entire HOL distribution, as
nipkow@15524
   368
     of 5 March 2004, was observed). *)
nipkow@15524
   369
*}
nipkow@15524
   370
nipkow@15524
   371
(* Optional setup of methods *)
nipkow@15524
   372
nipkow@15524
   373
(*
nipkow@15524
   374
method_setup trans_partial =
nipkow@15524
   375
  {* Method.no_args (Method.SIMPLE_METHOD' HEADGOAL (Order_Tac.partial_tac)) *}
nipkow@15524
   376
  {* transitivity reasoner for partial orders *}	
nipkow@15524
   377
method_setup trans_linear =
nipkow@15524
   378
  {* Method.no_args (Method.SIMPLE_METHOD' HEADGOAL (Order_Tac.linear_tac)) *}
nipkow@15524
   379
  {* transitivity reasoner for linear orders *}
nipkow@15524
   380
*)
nipkow@15524
   381
nipkow@15524
   382
(*
nipkow@15524
   383
declare order.order_refl [simp del] order_less_irrefl [simp del]
nipkow@15524
   384
nipkow@15524
   385
can currently not be removed, abel_cancel relies on it.
nipkow@15524
   386
*)
nipkow@15524
   387
nipkow@15524
   388
nipkow@15524
   389
subsection "Min and max on (linear) orders"
nipkow@15524
   390
nipkow@15524
   391
text{* Instantiate locales: *}
nipkow@15524
   392
ballarin@15837
   393
interpretation min_max:
nipkow@15780
   394
  lower_semilattice["op \<le>" "min :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a"]
nipkow@15524
   395
apply(rule lower_semilattice_axioms.intro)
nipkow@15524
   396
apply(simp add:min_def linorder_not_le order_less_imp_le)
nipkow@15524
   397
apply(simp add:min_def linorder_not_le order_less_imp_le)
nipkow@15524
   398
apply(simp add:min_def linorder_not_le order_less_imp_le)
nipkow@15524
   399
done
nipkow@15524
   400
ballarin@15837
   401
interpretation min_max:
nipkow@15780
   402
  upper_semilattice["op \<le>" "max :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a"]
nipkow@15780
   403
apply -
nipkow@15524
   404
apply(rule upper_semilattice_axioms.intro)
nipkow@15524
   405
apply(simp add: max_def linorder_not_le order_less_imp_le)
nipkow@15524
   406
apply(simp add: max_def linorder_not_le order_less_imp_le)
nipkow@15524
   407
apply(simp add: max_def linorder_not_le order_less_imp_le)
nipkow@15524
   408
done
nipkow@15524
   409
ballarin@15837
   410
interpretation min_max:
nipkow@15780
   411
  lattice["op \<le>" "min :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a" "max"]
nipkow@15780
   412
.
nipkow@15524
   413
ballarin@15837
   414
interpretation min_max:
nipkow@15780
   415
  distrib_lattice["op \<le>" "min :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a" "max"]
nipkow@15524
   416
apply(rule distrib_lattice_axioms.intro)
nipkow@15524
   417
apply(rule_tac x=x and y=y in linorder_le_cases)
nipkow@15524
   418
apply(rule_tac x=x and y=z in linorder_le_cases)
nipkow@15524
   419
apply(rule_tac x=y and y=z in linorder_le_cases)
nipkow@15524
   420
apply(simp add:min_def max_def)
nipkow@15524
   421
apply(simp add:min_def max_def)
nipkow@15524
   422
apply(rule_tac x=y and y=z in linorder_le_cases)
nipkow@15524
   423
apply(simp add:min_def max_def)
nipkow@15524
   424
apply(simp add:min_def max_def)
nipkow@15524
   425
apply(rule_tac x=x and y=z in linorder_le_cases)
nipkow@15524
   426
apply(rule_tac x=y and y=z in linorder_le_cases)
nipkow@15524
   427
apply(simp add:min_def max_def)
nipkow@15524
   428
apply(simp add:min_def max_def)
nipkow@15524
   429
apply(rule_tac x=y and y=z in linorder_le_cases)
nipkow@15524
   430
apply(simp add:min_def max_def)
nipkow@15524
   431
apply(simp add:min_def max_def)
nipkow@15524
   432
done
nipkow@15524
   433
nipkow@15524
   434
lemma le_max_iff_disj: "!!z::'a::linorder. (z <= max x y) = (z <= x | z <= y)"
nipkow@15524
   435
  apply(simp add:max_def)
nipkow@15524
   436
  apply (insert linorder_linear)
nipkow@15524
   437
  apply (blast intro: order_trans)
nipkow@15524
   438
  done
nipkow@15524
   439
nipkow@15780
   440
lemmas le_maxI1 = min_max.sup_ge1
nipkow@15780
   441
lemmas le_maxI2 = min_max.sup_ge2
nipkow@15524
   442
nipkow@15524
   443
lemma less_max_iff_disj: "!!z::'a::linorder. (z < max x y) = (z < x | z < y)"
nipkow@15524
   444
  apply (simp add: max_def order_le_less)
nipkow@15524
   445
  apply (insert linorder_less_linear)
nipkow@15524
   446
  apply (blast intro: order_less_trans)
nipkow@15524
   447
  done
nipkow@15524
   448
nipkow@15524
   449
lemma max_less_iff_conj [simp]:
nipkow@15524
   450
    "!!z::'a::linorder. (max x y < z) = (x < z & y < z)"
nipkow@15524
   451
  apply (simp add: order_le_less max_def)
nipkow@15524
   452
  apply (insert linorder_less_linear)
nipkow@15524
   453
  apply (blast intro: order_less_trans)
nipkow@15524
   454
  done
nipkow@15791
   455
nipkow@15524
   456
lemma min_less_iff_conj [simp]:
nipkow@15524
   457
    "!!z::'a::linorder. (z < min x y) = (z < x & z < y)"
nipkow@15524
   458
  apply (simp add: order_le_less min_def)
nipkow@15524
   459
  apply (insert linorder_less_linear)
nipkow@15524
   460
  apply (blast intro: order_less_trans)
nipkow@15524
   461
  done
nipkow@15524
   462
nipkow@15524
   463
lemma min_le_iff_disj: "!!z::'a::linorder. (min x y <= z) = (x <= z | y <= z)"
nipkow@15524
   464
  apply (simp add: min_def)
nipkow@15524
   465
  apply (insert linorder_linear)
nipkow@15524
   466
  apply (blast intro: order_trans)
nipkow@15524
   467
  done
nipkow@15524
   468
nipkow@15524
   469
lemma min_less_iff_disj: "!!z::'a::linorder. (min x y < z) = (x < z | y < z)"
nipkow@15524
   470
  apply (simp add: min_def order_le_less)
nipkow@15524
   471
  apply (insert linorder_less_linear)
nipkow@15524
   472
  apply (blast intro: order_less_trans)
nipkow@15524
   473
  done
nipkow@15524
   474
nipkow@15780
   475
lemmas max_ac = min_max.sup_assoc min_max.sup_commute
nipkow@15780
   476
               mk_left_commute[of max,OF min_max.sup_assoc min_max.sup_commute]
nipkow@15524
   477
nipkow@15780
   478
lemmas min_ac = min_max.inf_assoc min_max.inf_commute
nipkow@15780
   479
               mk_left_commute[of min,OF min_max.inf_assoc min_max.inf_commute]
nipkow@15524
   480
nipkow@15524
   481
lemma split_min:
nipkow@15524
   482
    "P (min (i::'a::linorder) j) = ((i <= j --> P(i)) & (~ i <= j --> P(j)))"
nipkow@15524
   483
  by (simp add: min_def)
nipkow@15524
   484
nipkow@15524
   485
lemma split_max:
nipkow@15524
   486
    "P (max (i::'a::linorder) j) = ((i <= j --> P(j)) & (~ i <= j --> P(i)))"
nipkow@15524
   487
  by (simp add: max_def)
nipkow@15524
   488
nipkow@15524
   489
nipkow@15524
   490
subsection "Bounded quantifiers"
nipkow@15524
   491
nipkow@15524
   492
syntax
nipkow@15524
   493
  "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3ALL _<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   494
  "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3EX _<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   495
  "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3ALL _<=_./ _)" [0, 0, 10] 10)
nipkow@15524
   496
  "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3EX _<=_./ _)" [0, 0, 10] 10)
nipkow@15524
   497
nipkow@15524
   498
  "_gtAll" :: "[idt, 'a, bool] => bool"   ("(3ALL _>_./ _)"  [0, 0, 10] 10)
nipkow@15524
   499
  "_gtEx"  :: "[idt, 'a, bool] => bool"   ("(3EX _>_./ _)"  [0, 0, 10] 10)
nipkow@15524
   500
  "_geAll"   :: "[idt, 'a, bool] => bool"   ("(3ALL _>=_./ _)" [0, 0, 10] 10)
nipkow@15524
   501
  "_geEx"    :: "[idt, 'a, bool] => bool"   ("(3EX _>=_./ _)" [0, 0, 10] 10)
nipkow@15524
   502
nipkow@15524
   503
syntax (xsymbols)
nipkow@15524
   504
  "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   505
  "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   506
  "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
nipkow@15524
   507
  "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
nipkow@15524
   508
nipkow@15524
   509
  "_gtAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
nipkow@15524
   510
  "_gtEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
nipkow@15524
   511
  "_geAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
nipkow@15524
   512
  "_geEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
nipkow@15524
   513
nipkow@15524
   514
syntax (HOL)
nipkow@15524
   515
  "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3! _<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   516
  "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3? _<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   517
  "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3! _<=_./ _)" [0, 0, 10] 10)
nipkow@15524
   518
  "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3? _<=_./ _)" [0, 0, 10] 10)
nipkow@15524
   519
nipkow@15524
   520
syntax (HTML output)
nipkow@15524
   521
  "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   522
  "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   523
  "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
nipkow@15524
   524
  "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
nipkow@15524
   525
nipkow@15524
   526
  "_gtAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
nipkow@15524
   527
  "_gtEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
nipkow@15524
   528
  "_geAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
nipkow@15524
   529
  "_geEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
nipkow@15524
   530
nipkow@15524
   531
translations
nipkow@15524
   532
 "ALL x<y. P"   =>  "ALL x. x < y --> P"
nipkow@15524
   533
 "EX x<y. P"    =>  "EX x. x < y  & P"
nipkow@15524
   534
 "ALL x<=y. P"  =>  "ALL x. x <= y --> P"
nipkow@15524
   535
 "EX x<=y. P"   =>  "EX x. x <= y & P"
nipkow@15524
   536
 "ALL x>y. P"   =>  "ALL x. x > y --> P"
nipkow@15524
   537
 "EX x>y. P"    =>  "EX x. x > y  & P"
nipkow@15524
   538
 "ALL x>=y. P"  =>  "ALL x. x >= y --> P"
nipkow@15524
   539
 "EX x>=y. P"   =>  "EX x. x >= y & P"
nipkow@15524
   540
nipkow@15524
   541
print_translation {*
nipkow@15524
   542
let
nipkow@15524
   543
  fun mk v v' q n P =
wenzelm@16861
   544
    if v=v' andalso not (v mem (map fst (Term.add_frees n [])))
nipkow@15524
   545
    then Syntax.const q $ Syntax.mark_bound v' $ n $ P else raise Match;
nipkow@15524
   546
  fun all_tr' [Const ("_bound",_) $ Free (v,_),
haftmann@19277
   547
               Const("op -->",_) $ (Const ("Orderings.less",_) $ (Const ("_bound",_) $ Free (v',_)) $ n ) $ P] =
nipkow@15524
   548
    mk v v' "_lessAll" n P
nipkow@15524
   549
nipkow@15524
   550
  | all_tr' [Const ("_bound",_) $ Free (v,_),
haftmann@19277
   551
               Const("op -->",_) $ (Const ("Orderings.less_eq",_) $ (Const ("_bound",_) $ Free (v',_)) $ n ) $ P] =
nipkow@15524
   552
    mk v v' "_leAll" n P
nipkow@15524
   553
nipkow@15524
   554
  | all_tr' [Const ("_bound",_) $ Free (v,_),
haftmann@19277
   555
               Const("op -->",_) $ (Const ("Orderings.less",_) $ n $ (Const ("_bound",_) $ Free (v',_))) $ P] =
nipkow@15524
   556
    mk v v' "_gtAll" n P
nipkow@15524
   557
nipkow@15524
   558
  | all_tr' [Const ("_bound",_) $ Free (v,_),
haftmann@19277
   559
               Const("op -->",_) $ (Const ("Orderings.less_eq",_) $ n $ (Const ("_bound",_) $ Free (v',_))) $ P] =
nipkow@15524
   560
    mk v v' "_geAll" n P;
nipkow@15524
   561
nipkow@15524
   562
  fun ex_tr' [Const ("_bound",_) $ Free (v,_),
haftmann@19277
   563
               Const("op &",_) $ (Const ("Orderings.less",_) $ (Const ("_bound",_) $ Free (v',_)) $ n ) $ P] =
nipkow@15524
   564
    mk v v' "_lessEx" n P
nipkow@15524
   565
nipkow@15524
   566
  | ex_tr' [Const ("_bound",_) $ Free (v,_),
haftmann@19277
   567
               Const("op &",_) $ (Const ("Orderings.less_eq",_) $ (Const ("_bound",_) $ Free (v',_)) $ n ) $ P] =
nipkow@15524
   568
    mk v v' "_leEx" n P
nipkow@15524
   569
nipkow@15524
   570
  | ex_tr' [Const ("_bound",_) $ Free (v,_),
haftmann@19277
   571
               Const("op &",_) $ (Const ("Orderings.less",_) $ n $ (Const ("_bound",_) $ Free (v',_))) $ P] =
nipkow@15524
   572
    mk v v' "_gtEx" n P
nipkow@15524
   573
nipkow@15524
   574
  | ex_tr' [Const ("_bound",_) $ Free (v,_),
haftmann@19277
   575
               Const("op &",_) $ (Const ("Orderings.less_eq",_) $ n $ (Const ("_bound",_) $ Free (v',_))) $ P] =
nipkow@15524
   576
    mk v v' "_geEx" n P
nipkow@15524
   577
in
nipkow@15524
   578
[("ALL ", all_tr'), ("EX ", ex_tr')]
nipkow@15524
   579
end
nipkow@15524
   580
*}
nipkow@15524
   581
avigad@17012
   582
subsection {* Extra transitivity rules *}
avigad@17012
   583
avigad@17012
   584
text {* These support proving chains of decreasing inequalities
avigad@17012
   585
    a >= b >= c ... in Isar proofs. *}
avigad@17012
   586
avigad@17012
   587
lemma xt1: "a = b ==> b > c ==> a > c"
avigad@17012
   588
by simp
avigad@17012
   589
avigad@17012
   590
lemma xt2: "a > b ==> b = c ==> a > c"
avigad@17012
   591
by simp
avigad@17012
   592
avigad@17012
   593
lemma xt3: "a = b ==> b >= c ==> a >= c"
avigad@17012
   594
by simp
avigad@17012
   595
avigad@17012
   596
lemma xt4: "a >= b ==> b = c ==> a >= c"
avigad@17012
   597
by simp
avigad@17012
   598
avigad@17012
   599
lemma xt5: "(x::'a::order) >= y ==> y >= x ==> x = y"
avigad@17012
   600
by simp
avigad@17012
   601
avigad@17012
   602
lemma xt6: "(x::'a::order) >= y ==> y >= z ==> x >= z"
avigad@17012
   603
by simp
avigad@17012
   604
avigad@17012
   605
lemma xt7: "(x::'a::order) > y ==> y >= z ==> x > z"
avigad@17012
   606
by simp
avigad@17012
   607
avigad@17012
   608
lemma xt8: "(x::'a::order) >= y ==> y > z ==> x > z"
avigad@17012
   609
by simp
avigad@17012
   610
avigad@17012
   611
lemma xt9: "(a::'a::order) > b ==> b > a ==> ?P"
avigad@17012
   612
by simp
avigad@17012
   613
avigad@17012
   614
lemma xt10: "(x::'a::order) > y ==> y > z ==> x > z"
avigad@17012
   615
by simp
avigad@17012
   616
avigad@17012
   617
lemma xt11: "(a::'a::order) >= b ==> a ~= b ==> a > b"
avigad@17012
   618
by simp
avigad@17012
   619
avigad@17012
   620
lemma xt12: "(a::'a::order) ~= b ==> a >= b ==> a > b"
avigad@17012
   621
by simp
avigad@17012
   622
avigad@17012
   623
lemma xt13: "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==>
avigad@17012
   624
    a > f c" 
avigad@17012
   625
by simp
avigad@17012
   626
avigad@17012
   627
lemma xt14: "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==>
avigad@17012
   628
    f a > c"
avigad@17012
   629
by auto
avigad@17012
   630
avigad@17012
   631
lemma xt15: "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==>
avigad@17012
   632
    a >= f c"
avigad@17012
   633
by simp
avigad@17012
   634
avigad@17012
   635
lemma xt16: "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==>
avigad@17012
   636
    f a >= c"
avigad@17012
   637
by auto
avigad@17012
   638
avigad@17012
   639
lemma xt17: "(a::'a::order) >= f b ==> b >= c ==> 
avigad@17012
   640
    (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
avigad@17012
   641
by (subgoal_tac "f b >= f c", force, force)
avigad@17012
   642
avigad@17012
   643
lemma xt18: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==> 
avigad@17012
   644
    (!!x y. x >= y ==> f x >= f y) ==> f a >= c"
avigad@17012
   645
by (subgoal_tac "f a >= f b", force, force)
avigad@17012
   646
avigad@17012
   647
lemma xt19: "(a::'a::order) > f b ==> (b::'b::order) >= c ==>
avigad@17012
   648
  (!!x y. x >= y ==> f x >= f y) ==> a > f c"
avigad@17012
   649
by (subgoal_tac "f b >= f c", force, force)
avigad@17012
   650
avigad@17012
   651
lemma xt20: "(a::'a::order) > b ==> (f b::'b::order) >= c==>
avigad@17012
   652
    (!!x y. x > y ==> f x > f y) ==> f a > c"
avigad@17012
   653
by (subgoal_tac "f a > f b", force, force)
avigad@17012
   654
avigad@17012
   655
lemma xt21: "(a::'a::order) >= f b ==> b > c ==>
avigad@17012
   656
    (!!x y. x > y ==> f x > f y) ==> a > f c"
avigad@17012
   657
by (subgoal_tac "f b > f c", force, force)
avigad@17012
   658
avigad@17012
   659
lemma xt22: "(a::'a::order) >= b ==> (f b::'b::order) > c ==>
avigad@17012
   660
    (!!x y. x >= y ==> f x >= f y) ==> f a > c"
avigad@17012
   661
by (subgoal_tac "f a >= f b", force, force)
avigad@17012
   662
avigad@17012
   663
lemma xt23: "(a::'a::order) > f b ==> (b::'b::order) > c ==>
avigad@17012
   664
    (!!x y. x > y ==> f x > f y) ==> a > f c"
avigad@17012
   665
by (subgoal_tac "f b > f c", force, force)
avigad@17012
   666
avigad@17012
   667
lemma xt24: "(a::'a::order) > b ==> (f b::'b::order) > c ==>
avigad@17012
   668
    (!!x y. x > y ==> f x > f y) ==> f a > c"
avigad@17012
   669
by (subgoal_tac "f a > f b", force, force)
avigad@17012
   670
avigad@17012
   671
avigad@17012
   672
lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9 xt10 xt11 xt12
avigad@17012
   673
    xt13 xt14 xt15 xt15 xt17 xt18 xt19 xt20 xt21 xt22 xt23 xt24
avigad@17012
   674
avigad@17012
   675
(* 
avigad@17012
   676
  Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands
avigad@17012
   677
  for the wrong thing in an Isar proof.
avigad@17012
   678
avigad@17012
   679
  The extra transitivity rules can be used as follows: 
avigad@17012
   680
avigad@17012
   681
lemma "(a::'a::order) > z"
avigad@17012
   682
proof -
avigad@17012
   683
  have "a >= b" (is "_ >= ?rhs")
avigad@17012
   684
    sorry
avigad@17012
   685
  also have "?rhs >= c" (is "_ >= ?rhs")
avigad@17012
   686
    sorry
avigad@17012
   687
  also (xtrans) have "?rhs = d" (is "_ = ?rhs")
avigad@17012
   688
    sorry
avigad@17012
   689
  also (xtrans) have "?rhs >= e" (is "_ >= ?rhs")
avigad@17012
   690
    sorry
avigad@17012
   691
  also (xtrans) have "?rhs > f" (is "_ > ?rhs")
avigad@17012
   692
    sorry
avigad@17012
   693
  also (xtrans) have "?rhs > z"
avigad@17012
   694
    sorry
avigad@17012
   695
  finally (xtrans) show ?thesis .
avigad@17012
   696
qed
avigad@17012
   697
avigad@17012
   698
  Alternatively, one can use "declare xtrans [trans]" and then
avigad@17012
   699
  leave out the "(xtrans)" above.
avigad@17012
   700
*)
avigad@17012
   701
nipkow@15524
   702
end