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