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