src/HOL/Orderings.thy
author nipkow
Wed Apr 20 17:19:18 2005 +0200 (2005-04-20)
changeset 15780 6744bba5561d
parent 15622 4723248c982b
child 15791 446ec11266be
permissions -rw-r--r--
Used locale interpretations everywhere.
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
nipkow@15524
    12
files ("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
nipkow@15524
    21
  "op <"        :: "['a::ord, 'a] => bool"             ("op <")
nipkow@15524
    22
  "op <="       :: "['a::ord, 'a] => bool"             ("op <=")
nipkow@15524
    23
nipkow@15524
    24
global
nipkow@15524
    25
nipkow@15524
    26
consts
nipkow@15524
    27
  "op <"        :: "['a::ord, 'a] => bool"             ("(_/ < _)"  [50, 51] 50)
nipkow@15524
    28
  "op <="       :: "['a::ord, 'a] => bool"             ("(_/ <= _)" [50, 51] 50)
nipkow@15524
    29
nipkow@15524
    30
local
nipkow@15524
    31
nipkow@15524
    32
syntax (xsymbols)
nipkow@15524
    33
  "op <="       :: "['a::ord, 'a] => bool"             ("op \<le>")
nipkow@15524
    34
  "op <="       :: "['a::ord, 'a] => bool"             ("(_/ \<le> _)"  [50, 51] 50)
nipkow@15524
    35
nipkow@15524
    36
syntax (HTML output)
nipkow@15524
    37
  "op <="       :: "['a::ord, 'a] => bool"             ("op \<le>")
nipkow@15524
    38
  "op <="       :: "['a::ord, 'a] => bool"             ("(_/ \<le> _)"  [50, 51] 50)
nipkow@15524
    39
nipkow@15524
    40
text{* Syntactic sugar: *}
nipkow@15524
    41
nipkow@15524
    42
consts
nipkow@15524
    43
  "_gt" :: "'a::ord => 'a => bool"             (infixl ">" 50)
nipkow@15524
    44
  "_ge" :: "'a::ord => 'a => bool"             (infixl ">=" 50)
nipkow@15524
    45
translations
nipkow@15524
    46
  "x > y"  => "y < x"
nipkow@15524
    47
  "x >= y" => "y <= x"
nipkow@15524
    48
nipkow@15524
    49
syntax (xsymbols)
nipkow@15524
    50
  "_ge"       :: "'a::ord => 'a => bool"             (infixl "\<ge>" 50)
nipkow@15524
    51
nipkow@15524
    52
syntax (HTML output)
nipkow@15524
    53
  "_ge"       :: "['a::ord, 'a] => bool"             (infixl "\<ge>" 50)
nipkow@15524
    54
nipkow@15524
    55
nipkow@15524
    56
subsection {* Monotonicity *}
nipkow@15524
    57
nipkow@15524
    58
locale mono =
nipkow@15524
    59
  fixes f
nipkow@15524
    60
  assumes mono: "A <= B ==> f A <= f B"
nipkow@15524
    61
nipkow@15524
    62
lemmas monoI [intro?] = mono.intro
nipkow@15524
    63
  and monoD [dest?] = mono.mono
nipkow@15524
    64
nipkow@15524
    65
constdefs
nipkow@15524
    66
  min :: "['a::ord, 'a] => 'a"
nipkow@15524
    67
  "min a b == (if a <= b then a else b)"
nipkow@15524
    68
  max :: "['a::ord, 'a] => 'a"
nipkow@15524
    69
  "max a b == (if a <= b then b else a)"
nipkow@15524
    70
nipkow@15524
    71
lemma min_leastL: "(!!x. least <= x) ==> min least x = least"
nipkow@15524
    72
  by (simp add: min_def)
nipkow@15524
    73
nipkow@15524
    74
lemma min_of_mono:
nipkow@15524
    75
    "ALL x y. (f x <= f y) = (x <= y) ==> min (f m) (f n) = f (min m n)"
nipkow@15524
    76
  by (simp add: min_def)
nipkow@15524
    77
nipkow@15524
    78
lemma max_leastL: "(!!x. least <= x) ==> max least x = x"
nipkow@15524
    79
  by (simp add: max_def)
nipkow@15524
    80
nipkow@15524
    81
lemma max_of_mono:
nipkow@15524
    82
    "ALL x y. (f x <= f y) = (x <= y) ==> max (f m) (f n) = f (max m n)"
nipkow@15524
    83
  by (simp add: max_def)
nipkow@15524
    84
nipkow@15524
    85
nipkow@15524
    86
subsection "Orders"
nipkow@15524
    87
nipkow@15524
    88
axclass order < ord
nipkow@15524
    89
  order_refl [iff]: "x <= x"
nipkow@15524
    90
  order_trans: "x <= y ==> y <= z ==> x <= z"
nipkow@15524
    91
  order_antisym: "x <= y ==> y <= x ==> x = y"
nipkow@15524
    92
  order_less_le: "(x < y) = (x <= y & x ~= y)"
nipkow@15524
    93
nipkow@15524
    94
text{* Connection to locale: *}
nipkow@15524
    95
nipkow@15780
    96
interpretation order[rule del]:
nipkow@15780
    97
  partial_order["op \<le> :: 'a::order \<Rightarrow> 'a \<Rightarrow> bool"]
nipkow@15524
    98
apply(rule partial_order.intro)
nipkow@15524
    99
apply(rule order_refl, erule (1) order_trans, erule (1) order_antisym)
nipkow@15524
   100
done
nipkow@15524
   101
nipkow@15524
   102
text {* Reflexivity. *}
nipkow@15524
   103
nipkow@15524
   104
lemma order_eq_refl: "!!x::'a::order. x = y ==> x <= y"
nipkow@15524
   105
    -- {* This form is useful with the classical reasoner. *}
nipkow@15524
   106
  apply (erule ssubst)
nipkow@15524
   107
  apply (rule order_refl)
nipkow@15524
   108
  done
nipkow@15524
   109
nipkow@15524
   110
lemma order_less_irrefl [iff]: "~ x < (x::'a::order)"
nipkow@15524
   111
  by (simp add: order_less_le)
nipkow@15524
   112
nipkow@15524
   113
lemma order_le_less: "((x::'a::order) <= y) = (x < y | x = y)"
nipkow@15524
   114
    -- {* NOT suitable for iff, since it can cause PROOF FAILED. *}
nipkow@15524
   115
  apply (simp add: order_less_le, blast)
nipkow@15524
   116
  done
nipkow@15524
   117
nipkow@15524
   118
lemmas order_le_imp_less_or_eq = order_le_less [THEN iffD1, standard]
nipkow@15524
   119
nipkow@15524
   120
lemma order_less_imp_le: "!!x::'a::order. x < y ==> x <= y"
nipkow@15524
   121
  by (simp add: order_less_le)
nipkow@15524
   122
nipkow@15524
   123
nipkow@15524
   124
text {* Asymmetry. *}
nipkow@15524
   125
nipkow@15524
   126
lemma order_less_not_sym: "(x::'a::order) < y ==> ~ (y < x)"
nipkow@15524
   127
  by (simp add: order_less_le order_antisym)
nipkow@15524
   128
nipkow@15524
   129
lemma order_less_asym: "x < (y::'a::order) ==> (~P ==> y < x) ==> P"
nipkow@15524
   130
  apply (drule order_less_not_sym)
nipkow@15524
   131
  apply (erule contrapos_np, simp)
nipkow@15524
   132
  done
nipkow@15524
   133
nipkow@15524
   134
lemma order_eq_iff: "!!x::'a::order. (x = y) = (x \<le> y & y \<le> x)"
nipkow@15524
   135
by (blast intro: order_antisym)
nipkow@15524
   136
nipkow@15524
   137
lemma order_antisym_conv: "(y::'a::order) <= x ==> (x <= y) = (x = y)"
nipkow@15524
   138
by(blast intro:order_antisym)
nipkow@15524
   139
nipkow@15524
   140
text {* Transitivity. *}
nipkow@15524
   141
nipkow@15524
   142
lemma order_less_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
lemma order_le_less_trans: "!!x::'a::order. [| x <= y; y < z |] ==> x < z"
nipkow@15524
   148
  apply (simp add: order_less_le)
nipkow@15524
   149
  apply (blast intro: order_trans order_antisym)
nipkow@15524
   150
  done
nipkow@15524
   151
nipkow@15524
   152
lemma order_less_le_trans: "!!x::'a::order. [| x < y; y <= z |] ==> x < z"
nipkow@15524
   153
  apply (simp add: order_less_le)
nipkow@15524
   154
  apply (blast intro: order_trans order_antisym)
nipkow@15524
   155
  done
nipkow@15524
   156
nipkow@15524
   157
nipkow@15524
   158
text {* Useful for simplification, but too risky to include by default. *}
nipkow@15524
   159
nipkow@15524
   160
lemma order_less_imp_not_less: "(x::'a::order) < y ==>  (~ y < x) = True"
nipkow@15524
   161
  by (blast elim: order_less_asym)
nipkow@15524
   162
nipkow@15524
   163
lemma order_less_imp_triv: "(x::'a::order) < y ==>  (y < x --> P) = True"
nipkow@15524
   164
  by (blast elim: order_less_asym)
nipkow@15524
   165
nipkow@15524
   166
lemma order_less_imp_not_eq: "(x::'a::order) < y ==>  (x = y) = False"
nipkow@15524
   167
  by auto
nipkow@15524
   168
nipkow@15524
   169
lemma order_less_imp_not_eq2: "(x::'a::order) < y ==>  (y = x) = False"
nipkow@15524
   170
  by auto
nipkow@15524
   171
nipkow@15524
   172
nipkow@15524
   173
text {* Other operators. *}
nipkow@15524
   174
nipkow@15524
   175
lemma min_leastR: "(!!x::'a::order. least <= x) ==> min x least = least"
nipkow@15524
   176
  apply (simp add: min_def)
nipkow@15524
   177
  apply (blast intro: order_antisym)
nipkow@15524
   178
  done
nipkow@15524
   179
nipkow@15524
   180
lemma max_leastR: "(!!x::'a::order. least <= x) ==> max x least = x"
nipkow@15524
   181
  apply (simp add: max_def)
nipkow@15524
   182
  apply (blast intro: order_antisym)
nipkow@15524
   183
  done
nipkow@15524
   184
nipkow@15524
   185
nipkow@15524
   186
subsection {* Transitivity rules for calculational reasoning *}
nipkow@15524
   187
nipkow@15524
   188
nipkow@15524
   189
lemma order_neq_le_trans: "a ~= b ==> (a::'a::order) <= b ==> a < b"
nipkow@15524
   190
  by (simp add: order_less_le)
nipkow@15524
   191
nipkow@15524
   192
lemma order_le_neq_trans: "(a::'a::order) <= b ==> a ~= b ==> a < b"
nipkow@15524
   193
  by (simp add: order_less_le)
nipkow@15524
   194
nipkow@15524
   195
lemma order_less_asym': "(a::'a::order) < b ==> b < a ==> P"
nipkow@15524
   196
  by (rule order_less_asym)
nipkow@15524
   197
nipkow@15524
   198
nipkow@15524
   199
subsection {* Least value operator *}
nipkow@15524
   200
nipkow@15524
   201
constdefs
nipkow@15524
   202
  Least :: "('a::ord => bool) => 'a"               (binder "LEAST " 10)
nipkow@15524
   203
  "Least P == THE x. P x & (ALL y. P y --> x <= y)"
nipkow@15524
   204
    -- {* We can no longer use LeastM because the latter requires Hilbert-AC. *}
nipkow@15524
   205
nipkow@15524
   206
lemma LeastI2:
nipkow@15524
   207
  "[| P (x::'a::order);
nipkow@15524
   208
      !!y. P y ==> x <= y;
nipkow@15524
   209
      !!x. [| P x; ALL y. P y --> x \<le> y |] ==> Q x |]
nipkow@15524
   210
   ==> Q (Least P)"
nipkow@15524
   211
  apply (unfold Least_def)
nipkow@15524
   212
  apply (rule theI2)
nipkow@15524
   213
    apply (blast intro: order_antisym)+
nipkow@15524
   214
  done
nipkow@15524
   215
nipkow@15524
   216
lemma Least_equality:
nipkow@15524
   217
    "[| P (k::'a::order); !!x. P x ==> k <= x |] ==> (LEAST x. P x) = k"
nipkow@15524
   218
  apply (simp add: Least_def)
nipkow@15524
   219
  apply (rule the_equality)
nipkow@15524
   220
  apply (auto intro!: order_antisym)
nipkow@15524
   221
  done
nipkow@15524
   222
nipkow@15524
   223
nipkow@15524
   224
subsection "Linear / total orders"
nipkow@15524
   225
nipkow@15524
   226
axclass linorder < order
nipkow@15524
   227
  linorder_linear: "x <= y | y <= x"
nipkow@15524
   228
nipkow@15780
   229
(* Could (should?) follow automatically from the interpretation of
nipkow@15780
   230
   partial_order by class order. rule del is needed because two copies
nipkow@15780
   231
   of refl with classes order and linorder confuse blast (and are pointless)*)
nipkow@15780
   232
interpretation order[rule del]:
nipkow@15780
   233
  partial_order["op \<le> :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> bool"]
nipkow@15780
   234
apply(rule partial_order.intro)
nipkow@15780
   235
apply(rule order_refl, erule (1) order_trans, erule (1) order_antisym)
nipkow@15780
   236
done
nipkow@15780
   237
nipkow@15780
   238
nipkow@15524
   239
lemma linorder_less_linear: "!!x::'a::linorder. x<y | x=y | y<x"
nipkow@15524
   240
  apply (simp add: order_less_le)
nipkow@15524
   241
  apply (insert linorder_linear, blast)
nipkow@15524
   242
  done
nipkow@15524
   243
nipkow@15524
   244
lemma linorder_le_less_linear: "!!x::'a::linorder. x\<le>y | y<x"
nipkow@15524
   245
  by (simp add: order_le_less linorder_less_linear)
nipkow@15524
   246
nipkow@15524
   247
lemma linorder_le_cases [case_names le ge]:
nipkow@15524
   248
    "((x::'a::linorder) \<le> y ==> P) ==> (y \<le> x ==> P) ==> P"
nipkow@15524
   249
  by (insert linorder_linear, blast)
nipkow@15524
   250
nipkow@15524
   251
lemma linorder_cases [case_names less equal greater]:
nipkow@15524
   252
    "((x::'a::linorder) < y ==> P) ==> (x = y ==> P) ==> (y < x ==> P) ==> P"
nipkow@15524
   253
  by (insert linorder_less_linear, blast)
nipkow@15524
   254
nipkow@15524
   255
lemma linorder_not_less: "!!x::'a::linorder. (~ x < y) = (y <= x)"
nipkow@15524
   256
  apply (simp add: order_less_le)
nipkow@15524
   257
  apply (insert linorder_linear)
nipkow@15524
   258
  apply (blast intro: order_antisym)
nipkow@15524
   259
  done
nipkow@15524
   260
nipkow@15524
   261
lemma linorder_not_le: "!!x::'a::linorder. (~ x <= y) = (y < x)"
nipkow@15524
   262
  apply (simp add: order_less_le)
nipkow@15524
   263
  apply (insert linorder_linear)
nipkow@15524
   264
  apply (blast intro: order_antisym)
nipkow@15524
   265
  done
nipkow@15524
   266
nipkow@15524
   267
lemma linorder_neq_iff: "!!x::'a::linorder. (x ~= y) = (x<y | y<x)"
nipkow@15524
   268
by (cut_tac x = x and y = y in linorder_less_linear, auto)
nipkow@15524
   269
nipkow@15524
   270
lemma linorder_neqE: "x ~= (y::'a::linorder) ==> (x < y ==> R) ==> (y < x ==> R) ==> R"
nipkow@15524
   271
by (simp add: linorder_neq_iff, blast)
nipkow@15524
   272
nipkow@15524
   273
lemma linorder_antisym_conv1: "~ (x::'a::linorder) < y ==> (x <= y) = (x = y)"
nipkow@15524
   274
by(blast intro:order_antisym dest:linorder_not_less[THEN iffD1])
nipkow@15524
   275
nipkow@15524
   276
lemma linorder_antisym_conv2: "(x::'a::linorder) <= y ==> (~ x < y) = (x = y)"
nipkow@15524
   277
by(blast intro:order_antisym dest:linorder_not_less[THEN iffD1])
nipkow@15524
   278
nipkow@15524
   279
lemma linorder_antisym_conv3: "~ (y::'a::linorder) < x ==> (~ x < y) = (x = y)"
nipkow@15524
   280
by(blast intro:order_antisym dest:linorder_not_less[THEN iffD1])
nipkow@15524
   281
nipkow@15524
   282
use "antisym_setup.ML";
nipkow@15524
   283
setup antisym_setup
nipkow@15524
   284
nipkow@15524
   285
subsection {* Setup of transitivity reasoner as Solver *}
nipkow@15524
   286
nipkow@15524
   287
lemma less_imp_neq: "[| (x::'a::order) < y |] ==> x ~= y"
nipkow@15524
   288
  by (erule contrapos_pn, erule subst, rule order_less_irrefl)
nipkow@15524
   289
nipkow@15524
   290
lemma eq_neq_eq_imp_neq: "[| x = a ; a ~= b; b = y |] ==> x ~= y"
nipkow@15524
   291
  by (erule subst, erule ssubst, assumption)
nipkow@15524
   292
nipkow@15524
   293
ML_setup {*
nipkow@15524
   294
nipkow@15524
   295
(* The setting up of Quasi_Tac serves as a demo.  Since there is no
nipkow@15524
   296
   class for quasi orders, the tactics Quasi_Tac.trans_tac and
nipkow@15524
   297
   Quasi_Tac.quasi_tac are not of much use. *)
nipkow@15524
   298
nipkow@15524
   299
fun decomp_gen sort sign (Trueprop $ t) =
ballarin@15622
   300
  let fun of_sort t = let val T = type_of t in
ballarin@15622
   301
        (* exclude numeric types: linear arithmetic subsumes transitivity *)
ballarin@15622
   302
        T <> HOLogic.natT andalso T <> HOLogic.intT andalso
ballarin@15622
   303
        T <> HOLogic.realT andalso Sign.of_sort sign (T, sort) end
nipkow@15524
   304
  fun dec (Const ("Not", _) $ t) = (
nipkow@15524
   305
	  case dec t of
skalberg@15531
   306
	    NONE => NONE
skalberg@15531
   307
	  | SOME (t1, rel, t2) => SOME (t1, "~" ^ rel, t2))
nipkow@15524
   308
	| dec (Const ("op =",  _) $ t1 $ t2) =
nipkow@15524
   309
	    if of_sort t1
skalberg@15531
   310
	    then SOME (t1, "=", t2)
skalberg@15531
   311
	    else NONE
nipkow@15524
   312
	| dec (Const ("op <=",  _) $ t1 $ t2) =
nipkow@15524
   313
	    if of_sort t1
skalberg@15531
   314
	    then SOME (t1, "<=", t2)
skalberg@15531
   315
	    else NONE
nipkow@15524
   316
	| dec (Const ("op <",  _) $ t1 $ t2) =
nipkow@15524
   317
	    if of_sort t1
skalberg@15531
   318
	    then SOME (t1, "<", t2)
skalberg@15531
   319
	    else NONE
skalberg@15531
   320
	| dec _ = NONE
nipkow@15524
   321
  in dec t end;
nipkow@15524
   322
nipkow@15524
   323
structure Quasi_Tac = Quasi_Tac_Fun (
nipkow@15524
   324
  struct
nipkow@15524
   325
    val le_trans = thm "order_trans";
nipkow@15524
   326
    val le_refl = thm "order_refl";
nipkow@15524
   327
    val eqD1 = thm "order_eq_refl";
nipkow@15524
   328
    val eqD2 = thm "sym" RS thm "order_eq_refl";
nipkow@15524
   329
    val less_reflE = thm "order_less_irrefl" RS thm "notE";
nipkow@15524
   330
    val less_imp_le = thm "order_less_imp_le";
nipkow@15524
   331
    val le_neq_trans = thm "order_le_neq_trans";
nipkow@15524
   332
    val neq_le_trans = thm "order_neq_le_trans";
nipkow@15524
   333
    val less_imp_neq = thm "less_imp_neq";
nipkow@15524
   334
    val decomp_trans = decomp_gen ["Orderings.order"];
nipkow@15524
   335
    val decomp_quasi = decomp_gen ["Orderings.order"];
nipkow@15524
   336
nipkow@15524
   337
  end);  (* struct *)
nipkow@15524
   338
nipkow@15524
   339
structure Order_Tac = Order_Tac_Fun (
nipkow@15524
   340
  struct
nipkow@15524
   341
    val less_reflE = thm "order_less_irrefl" RS thm "notE";
nipkow@15524
   342
    val le_refl = thm "order_refl";
nipkow@15524
   343
    val less_imp_le = thm "order_less_imp_le";
nipkow@15524
   344
    val not_lessI = thm "linorder_not_less" RS thm "iffD2";
nipkow@15524
   345
    val not_leI = thm "linorder_not_le" RS thm "iffD2";
nipkow@15524
   346
    val not_lessD = thm "linorder_not_less" RS thm "iffD1";
nipkow@15524
   347
    val not_leD = thm "linorder_not_le" RS thm "iffD1";
nipkow@15524
   348
    val eqI = thm "order_antisym";
nipkow@15524
   349
    val eqD1 = thm "order_eq_refl";
nipkow@15524
   350
    val eqD2 = thm "sym" RS thm "order_eq_refl";
nipkow@15524
   351
    val less_trans = thm "order_less_trans";
nipkow@15524
   352
    val less_le_trans = thm "order_less_le_trans";
nipkow@15524
   353
    val le_less_trans = thm "order_le_less_trans";
nipkow@15524
   354
    val le_trans = thm "order_trans";
nipkow@15524
   355
    val le_neq_trans = thm "order_le_neq_trans";
nipkow@15524
   356
    val neq_le_trans = thm "order_neq_le_trans";
nipkow@15524
   357
    val less_imp_neq = thm "less_imp_neq";
nipkow@15524
   358
    val eq_neq_eq_imp_neq = thm "eq_neq_eq_imp_neq";
nipkow@15524
   359
    val decomp_part = decomp_gen ["Orderings.order"];
nipkow@15524
   360
    val decomp_lin = decomp_gen ["Orderings.linorder"];
nipkow@15524
   361
nipkow@15524
   362
  end);  (* struct *)
nipkow@15524
   363
nipkow@15524
   364
simpset_ref() := simpset ()
nipkow@15524
   365
    addSolver (mk_solver "Trans_linear" (fn _ => Order_Tac.linear_tac))
nipkow@15524
   366
    addSolver (mk_solver "Trans_partial" (fn _ => Order_Tac.partial_tac));
nipkow@15524
   367
  (* Adding the transitivity reasoners also as safe solvers showed a slight
nipkow@15524
   368
     speed up, but the reasoning strength appears to be not higher (at least
nipkow@15524
   369
     no breaking of additional proofs in the entire HOL distribution, as
nipkow@15524
   370
     of 5 March 2004, was observed). *)
nipkow@15524
   371
*}
nipkow@15524
   372
nipkow@15524
   373
(* Optional setup of methods *)
nipkow@15524
   374
nipkow@15524
   375
(*
nipkow@15524
   376
method_setup trans_partial =
nipkow@15524
   377
  {* Method.no_args (Method.SIMPLE_METHOD' HEADGOAL (Order_Tac.partial_tac)) *}
nipkow@15524
   378
  {* transitivity reasoner for partial orders *}	
nipkow@15524
   379
method_setup trans_linear =
nipkow@15524
   380
  {* Method.no_args (Method.SIMPLE_METHOD' HEADGOAL (Order_Tac.linear_tac)) *}
nipkow@15524
   381
  {* transitivity reasoner for linear orders *}
nipkow@15524
   382
*)
nipkow@15524
   383
nipkow@15524
   384
(*
nipkow@15524
   385
declare order.order_refl [simp del] order_less_irrefl [simp del]
nipkow@15524
   386
nipkow@15524
   387
can currently not be removed, abel_cancel relies on it.
nipkow@15524
   388
*)
nipkow@15524
   389
nipkow@15524
   390
nipkow@15524
   391
subsection "Min and max on (linear) orders"
nipkow@15524
   392
nipkow@15524
   393
lemma min_same [simp]: "min (x::'a::order) x = x"
nipkow@15524
   394
  by (simp add: min_def)
nipkow@15524
   395
nipkow@15524
   396
lemma max_same [simp]: "max (x::'a::order) x = x"
nipkow@15524
   397
  by (simp add: max_def)
nipkow@15524
   398
nipkow@15524
   399
text{* Instantiate locales: *}
nipkow@15524
   400
nipkow@15780
   401
interpretation min_max [rule del]:
nipkow@15780
   402
  lower_semilattice["op \<le>" "min :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a"]
nipkow@15780
   403
apply -
nipkow@15524
   404
apply(rule lower_semilattice_axioms.intro)
nipkow@15524
   405
apply(simp add:min_def linorder_not_le order_less_imp_le)
nipkow@15524
   406
apply(simp add:min_def linorder_not_le order_less_imp_le)
nipkow@15524
   407
apply(simp add:min_def linorder_not_le order_less_imp_le)
nipkow@15524
   408
done
nipkow@15524
   409
nipkow@15780
   410
interpretation min_max [rule del]:
nipkow@15780
   411
  upper_semilattice["op \<le>" "max :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a"]
nipkow@15780
   412
apply -
nipkow@15524
   413
apply(rule upper_semilattice_axioms.intro)
nipkow@15524
   414
apply(simp add: max_def linorder_not_le order_less_imp_le)
nipkow@15524
   415
apply(simp add: max_def linorder_not_le order_less_imp_le)
nipkow@15524
   416
apply(simp add: max_def linorder_not_le order_less_imp_le)
nipkow@15524
   417
done
nipkow@15524
   418
nipkow@15780
   419
interpretation min_max [rule del]:
nipkow@15780
   420
  lattice["op \<le>" "min :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a" "max"]
nipkow@15780
   421
.
nipkow@15524
   422
nipkow@15780
   423
interpretation min_max [rule del]:
nipkow@15780
   424
  distrib_lattice["op \<le>" "min :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a" "max"]
nipkow@15524
   425
apply(rule distrib_lattice_axioms.intro)
nipkow@15524
   426
apply(rule_tac x=x and y=y in linorder_le_cases)
nipkow@15524
   427
apply(rule_tac x=x and y=z in linorder_le_cases)
nipkow@15524
   428
apply(rule_tac x=y and y=z in linorder_le_cases)
nipkow@15524
   429
apply(simp add:min_def max_def)
nipkow@15524
   430
apply(simp add:min_def max_def)
nipkow@15524
   431
apply(rule_tac x=y and y=z in linorder_le_cases)
nipkow@15524
   432
apply(simp add:min_def max_def)
nipkow@15524
   433
apply(simp add:min_def max_def)
nipkow@15524
   434
apply(rule_tac x=x and y=z in linorder_le_cases)
nipkow@15524
   435
apply(rule_tac x=y and y=z in linorder_le_cases)
nipkow@15524
   436
apply(simp add:min_def max_def)
nipkow@15524
   437
apply(simp add:min_def max_def)
nipkow@15524
   438
apply(rule_tac x=y and y=z in linorder_le_cases)
nipkow@15524
   439
apply(simp add:min_def max_def)
nipkow@15524
   440
apply(simp add:min_def max_def)
nipkow@15524
   441
done
nipkow@15524
   442
nipkow@15524
   443
lemma le_max_iff_disj: "!!z::'a::linorder. (z <= max x y) = (z <= x | z <= y)"
nipkow@15524
   444
  apply(simp add:max_def)
nipkow@15524
   445
  apply (insert linorder_linear)
nipkow@15524
   446
  apply (blast intro: order_trans)
nipkow@15524
   447
  done
nipkow@15524
   448
nipkow@15780
   449
lemmas le_maxI1 = min_max.sup_ge1
nipkow@15780
   450
lemmas le_maxI2 = min_max.sup_ge2
nipkow@15524
   451
nipkow@15524
   452
lemma less_max_iff_disj: "!!z::'a::linorder. (z < max x y) = (z < x | z < y)"
nipkow@15524
   453
  apply (simp add: max_def order_le_less)
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 max_less_iff_conj [simp]:
nipkow@15524
   459
    "!!z::'a::linorder. (max x y < z) = (x < z & y < z)"
nipkow@15524
   460
  apply (simp add: order_le_less max_def)
nipkow@15524
   461
  apply (insert linorder_less_linear)
nipkow@15524
   462
  apply (blast intro: order_less_trans)
nipkow@15524
   463
  done
nipkow@15780
   464
(*
nipkow@15524
   465
lemma le_min_iff_conj [simp]:
nipkow@15524
   466
    "!!z::'a::linorder. (z <= min x y) = (z <= x & z <= y)"
nipkow@15524
   467
    -- {* @{text "[iff]"} screws up a @{text blast} in MiniML *}
nipkow@15524
   468
by (rule lower_semilattice.below_inf_conv[OF lower_semilattice_lin_min])
nipkow@15780
   469
*)
nipkow@15524
   470
lemma min_less_iff_conj [simp]:
nipkow@15524
   471
    "!!z::'a::linorder. (z < min x y) = (z < x & z < y)"
nipkow@15524
   472
  apply (simp add: order_le_less min_def)
nipkow@15524
   473
  apply (insert linorder_less_linear)
nipkow@15524
   474
  apply (blast intro: order_less_trans)
nipkow@15524
   475
  done
nipkow@15524
   476
nipkow@15524
   477
lemma min_le_iff_disj: "!!z::'a::linorder. (min x y <= z) = (x <= z | y <= z)"
nipkow@15524
   478
  apply (simp add: min_def)
nipkow@15524
   479
  apply (insert linorder_linear)
nipkow@15524
   480
  apply (blast intro: order_trans)
nipkow@15524
   481
  done
nipkow@15524
   482
nipkow@15524
   483
lemma min_less_iff_disj: "!!z::'a::linorder. (min x y < z) = (x < z | y < z)"
nipkow@15524
   484
  apply (simp add: min_def order_le_less)
nipkow@15524
   485
  apply (insert linorder_less_linear)
nipkow@15524
   486
  apply (blast intro: order_less_trans)
nipkow@15524
   487
  done
nipkow@15780
   488
(*
nipkow@15524
   489
lemma max_assoc: "!!x::'a::linorder. max (max x y) z = max x (max y z)"
nipkow@15524
   490
by (rule upper_semilattice.sup_assoc[OF upper_semilattice_lin_max])
nipkow@15524
   491
nipkow@15524
   492
lemma max_commute: "!!x::'a::linorder. max x y = max y x"
nipkow@15524
   493
by (rule upper_semilattice.sup_commute[OF upper_semilattice_lin_max])
nipkow@15780
   494
*)
nipkow@15780
   495
lemmas max_ac = min_max.sup_assoc min_max.sup_commute
nipkow@15780
   496
               mk_left_commute[of max,OF min_max.sup_assoc min_max.sup_commute]
nipkow@15780
   497
(*
nipkow@15524
   498
lemma min_assoc: "!!x::'a::linorder. min (min x y) z = min x (min y z)"
nipkow@15524
   499
by (rule lower_semilattice.inf_assoc[OF lower_semilattice_lin_min])
nipkow@15524
   500
nipkow@15524
   501
lemma min_commute: "!!x::'a::linorder. min x y = min y x"
nipkow@15524
   502
by (rule lower_semilattice.inf_commute[OF lower_semilattice_lin_min])
nipkow@15780
   503
*)
nipkow@15780
   504
lemmas min_ac = min_max.inf_assoc min_max.inf_commute
nipkow@15780
   505
               mk_left_commute[of min,OF min_max.inf_assoc min_max.inf_commute]
nipkow@15524
   506
nipkow@15524
   507
lemma split_min:
nipkow@15524
   508
    "P (min (i::'a::linorder) j) = ((i <= j --> P(i)) & (~ i <= j --> P(j)))"
nipkow@15524
   509
  by (simp add: min_def)
nipkow@15524
   510
nipkow@15524
   511
lemma split_max:
nipkow@15524
   512
    "P (max (i::'a::linorder) j) = ((i <= j --> P(j)) & (~ i <= j --> P(i)))"
nipkow@15524
   513
  by (simp add: max_def)
nipkow@15524
   514
nipkow@15524
   515
nipkow@15524
   516
subsection "Bounded quantifiers"
nipkow@15524
   517
nipkow@15524
   518
syntax
nipkow@15524
   519
  "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3ALL _<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   520
  "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3EX _<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   521
  "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3ALL _<=_./ _)" [0, 0, 10] 10)
nipkow@15524
   522
  "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3EX _<=_./ _)" [0, 0, 10] 10)
nipkow@15524
   523
nipkow@15524
   524
  "_gtAll" :: "[idt, 'a, bool] => bool"   ("(3ALL _>_./ _)"  [0, 0, 10] 10)
nipkow@15524
   525
  "_gtEx"  :: "[idt, 'a, bool] => bool"   ("(3EX _>_./ _)"  [0, 0, 10] 10)
nipkow@15524
   526
  "_geAll"   :: "[idt, 'a, bool] => bool"   ("(3ALL _>=_./ _)" [0, 0, 10] 10)
nipkow@15524
   527
  "_geEx"    :: "[idt, 'a, bool] => bool"   ("(3EX _>=_./ _)" [0, 0, 10] 10)
nipkow@15524
   528
nipkow@15524
   529
syntax (xsymbols)
nipkow@15524
   530
  "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   531
  "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   532
  "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
nipkow@15524
   533
  "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
nipkow@15524
   534
nipkow@15524
   535
  "_gtAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
nipkow@15524
   536
  "_gtEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
nipkow@15524
   537
  "_geAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
nipkow@15524
   538
  "_geEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
nipkow@15524
   539
nipkow@15524
   540
syntax (HOL)
nipkow@15524
   541
  "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3! _<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   542
  "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3? _<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   543
  "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3! _<=_./ _)" [0, 0, 10] 10)
nipkow@15524
   544
  "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3? _<=_./ _)" [0, 0, 10] 10)
nipkow@15524
   545
nipkow@15524
   546
syntax (HTML output)
nipkow@15524
   547
  "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   548
  "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
nipkow@15524
   549
  "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
nipkow@15524
   550
  "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
nipkow@15524
   551
nipkow@15524
   552
  "_gtAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
nipkow@15524
   553
  "_gtEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
nipkow@15524
   554
  "_geAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
nipkow@15524
   555
  "_geEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
nipkow@15524
   556
nipkow@15524
   557
translations
nipkow@15524
   558
 "ALL x<y. P"   =>  "ALL x. x < y --> P"
nipkow@15524
   559
 "EX x<y. P"    =>  "EX x. x < y  & P"
nipkow@15524
   560
 "ALL x<=y. P"  =>  "ALL x. x <= y --> P"
nipkow@15524
   561
 "EX x<=y. P"   =>  "EX x. x <= y & P"
nipkow@15524
   562
 "ALL x>y. P"   =>  "ALL x. x > y --> P"
nipkow@15524
   563
 "EX x>y. P"    =>  "EX x. x > y  & P"
nipkow@15524
   564
 "ALL x>=y. P"  =>  "ALL x. x >= y --> P"
nipkow@15524
   565
 "EX x>=y. P"   =>  "EX x. x >= y & P"
nipkow@15524
   566
nipkow@15524
   567
print_translation {*
nipkow@15524
   568
let
nipkow@15524
   569
  fun mk v v' q n P =
nipkow@15524
   570
    if v=v' andalso not(v  mem (map fst (Term.add_frees([],n))))
nipkow@15524
   571
    then Syntax.const q $ Syntax.mark_bound v' $ n $ P else raise Match;
nipkow@15524
   572
  fun all_tr' [Const ("_bound",_) $ Free (v,_),
nipkow@15524
   573
               Const("op -->",_) $ (Const ("op <",_) $ (Const ("_bound",_) $ Free (v',_)) $ n ) $ P] =
nipkow@15524
   574
    mk v v' "_lessAll" n P
nipkow@15524
   575
nipkow@15524
   576
  | all_tr' [Const ("_bound",_) $ Free (v,_),
nipkow@15524
   577
               Const("op -->",_) $ (Const ("op <=",_) $ (Const ("_bound",_) $ Free (v',_)) $ n ) $ P] =
nipkow@15524
   578
    mk v v' "_leAll" n P
nipkow@15524
   579
nipkow@15524
   580
  | all_tr' [Const ("_bound",_) $ Free (v,_),
nipkow@15524
   581
               Const("op -->",_) $ (Const ("op <",_) $ n $ (Const ("_bound",_) $ Free (v',_))) $ P] =
nipkow@15524
   582
    mk v v' "_gtAll" n P
nipkow@15524
   583
nipkow@15524
   584
  | all_tr' [Const ("_bound",_) $ Free (v,_),
nipkow@15524
   585
               Const("op -->",_) $ (Const ("op <=",_) $ n $ (Const ("_bound",_) $ Free (v',_))) $ P] =
nipkow@15524
   586
    mk v v' "_geAll" n P;
nipkow@15524
   587
nipkow@15524
   588
  fun ex_tr' [Const ("_bound",_) $ Free (v,_),
nipkow@15524
   589
               Const("op &",_) $ (Const ("op <",_) $ (Const ("_bound",_) $ Free (v',_)) $ n ) $ P] =
nipkow@15524
   590
    mk v v' "_lessEx" n P
nipkow@15524
   591
nipkow@15524
   592
  | ex_tr' [Const ("_bound",_) $ Free (v,_),
nipkow@15524
   593
               Const("op &",_) $ (Const ("op <=",_) $ (Const ("_bound",_) $ Free (v',_)) $ n ) $ P] =
nipkow@15524
   594
    mk v v' "_leEx" n P
nipkow@15524
   595
nipkow@15524
   596
  | ex_tr' [Const ("_bound",_) $ Free (v,_),
nipkow@15524
   597
               Const("op &",_) $ (Const ("op <",_) $ n $ (Const ("_bound",_) $ Free (v',_))) $ P] =
nipkow@15524
   598
    mk v v' "_gtEx" n P
nipkow@15524
   599
nipkow@15524
   600
  | ex_tr' [Const ("_bound",_) $ Free (v,_),
nipkow@15524
   601
               Const("op &",_) $ (Const ("op <=",_) $ n $ (Const ("_bound",_) $ Free (v',_))) $ P] =
nipkow@15524
   602
    mk v v' "_geEx" n P
nipkow@15524
   603
in
nipkow@15524
   604
[("ALL ", all_tr'), ("EX ", ex_tr')]
nipkow@15524
   605
end
nipkow@15524
   606
*}
nipkow@15524
   607
nipkow@15524
   608
end