src/HOL/Orderings.thy
author haftmann
Tue Nov 07 14:03:06 2006 +0100 (2006-11-07)
changeset 21216 1c8580913738
parent 21204 1e96553668c6
child 21248 3fd22b0939ff
permissions -rw-r--r--
made locale partial_order compatible with axclass order; changed import order; consecutive changes
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
haftmann@21083
     6
header {* Abstract orderings *}
nipkow@15524
     7
nipkow@15524
     8
theory Orderings
haftmann@21216
     9
imports Code_Generator
nipkow@15524
    10
begin
nipkow@15524
    11
haftmann@21083
    12
section {* Abstract orderings *}
haftmann@21083
    13
haftmann@21083
    14
subsection {* Order signatures *}
nipkow@15524
    15
haftmann@21194
    16
class ord =
haftmann@20588
    17
  fixes less_eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
wenzelm@21204
    18
    and less :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
wenzelm@21204
    19
begin
wenzelm@21204
    20
wenzelm@21204
    21
notation
wenzelm@21204
    22
  less_eq  ("op \<^loc><=")
wenzelm@21204
    23
  less_eq  ("(_/ \<^loc><= _)" [50, 51] 50)
wenzelm@21204
    24
  less  ("op \<^loc><")
wenzelm@21204
    25
  less  ("(_/ \<^loc>< _)"  [50, 51] 50)
wenzelm@21204
    26
wenzelm@21204
    27
notation (xsymbols)
wenzelm@21204
    28
  less_eq  ("op \<^loc>\<le>")
wenzelm@21204
    29
  less_eq  ("(_/ \<^loc>\<le> _)"  [50, 51] 50)
nipkow@15524
    30
wenzelm@21204
    31
notation (HTML output)
wenzelm@21204
    32
  less_eq  ("op \<^loc>\<le>")
wenzelm@21204
    33
  less_eq  ("(_/ \<^loc>\<le> _)"  [50, 51] 50)
wenzelm@21204
    34
wenzelm@21204
    35
abbreviation (input)
wenzelm@21204
    36
  greater  (infix "\<^loc>>" 50)
wenzelm@21204
    37
  "x \<^loc>> y \<equiv> y \<^loc>< x"
wenzelm@21204
    38
  greater_eq  (infix "\<^loc>>=" 50)
wenzelm@21204
    39
  "x \<^loc>>= y \<equiv> y \<^loc><= x"
wenzelm@21204
    40
wenzelm@21204
    41
notation (xsymbols)
wenzelm@21204
    42
  greater_eq  (infixl "\<^loc>\<ge>" 50)
wenzelm@21204
    43
wenzelm@21204
    44
end
wenzelm@21204
    45
wenzelm@21204
    46
notation
wenzelm@19656
    47
  less_eq  ("op <=")
wenzelm@19656
    48
  less_eq  ("(_/ <= _)" [50, 51] 50)
wenzelm@21204
    49
  less  ("op <")
wenzelm@21204
    50
  less  ("(_/ < _)"  [50, 51] 50)
wenzelm@21204
    51
  
wenzelm@21204
    52
notation (xsymbols)
wenzelm@19656
    53
  less_eq  ("op \<le>")
wenzelm@19656
    54
  less_eq  ("(_/ \<le> _)"  [50, 51] 50)
nipkow@15524
    55
wenzelm@21204
    56
notation (HTML output)
wenzelm@21204
    57
  less_eq  ("op \<le>")
wenzelm@21204
    58
  less_eq  ("(_/ \<le> _)"  [50, 51] 50)
haftmann@20714
    59
wenzelm@19536
    60
abbreviation (input)
wenzelm@19536
    61
  greater  (infixl ">" 50)
haftmann@20714
    62
  "x > y \<equiv> y < x"
wenzelm@19536
    63
  greater_eq  (infixl ">=" 50)
haftmann@20714
    64
  "x >= y \<equiv> y <= x"
haftmann@20714
    65
  
wenzelm@21204
    66
notation (xsymbols)
wenzelm@19656
    67
  greater_eq  (infixl "\<ge>" 50)
nipkow@15524
    68
nipkow@15524
    69
haftmann@21083
    70
subsection {* Partial orderings *}
nipkow@15524
    71
haftmann@21216
    72
locale partial_order =
haftmann@21216
    73
  fixes below :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infixl "\<sqsubseteq>" 50)
haftmann@21216
    74
  fixes less :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infixl "\<sqsubset>" 50)
haftmann@21216
    75
  assumes refl [iff]: "x \<sqsubseteq> x"
haftmann@21216
    76
  and trans: "x \<sqsubseteq> y \<Longrightarrow> y \<sqsubseteq> z \<Longrightarrow> x \<sqsubseteq> z"
haftmann@21216
    77
  and antisym: "x \<sqsubseteq> y \<Longrightarrow> y \<sqsubseteq> x \<Longrightarrow> x = y"
haftmann@21216
    78
  and less_le: "(x \<sqsubset> y) = (x \<sqsubseteq> y \<and> x \<noteq> y)"
haftmann@21216
    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
ballarin@15837
    86
interpretation order:
haftmann@21216
    87
  partial_order ["op \<le> \<Colon> 'a\<Colon>order \<Rightarrow> 'a \<Rightarrow> bool" "op < \<Colon> 'a\<Colon>order \<Rightarrow> 'a \<Rightarrow> bool"]
nipkow@15524
    88
apply(rule partial_order.intro)
haftmann@21216
    89
apply(rule order_refl, erule (1) order_trans, erule (1) order_antisym, rule order_less_le)
nipkow@15524
    90
done
nipkow@15524
    91
nipkow@15524
    92
text {* Reflexivity. *}
nipkow@15524
    93
haftmann@21216
    94
lemma order_eq_refl: "(x \<Colon> 'a\<Colon>order) = y \<Longrightarrow> x \<le> 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
text {* Asymmetry. *}
nipkow@15524
   114
nipkow@15524
   115
lemma order_less_not_sym: "(x::'a::order) < y ==> ~ (y < x)"
nipkow@15524
   116
  by (simp add: order_less_le order_antisym)
nipkow@15524
   117
nipkow@15524
   118
lemma order_less_asym: "x < (y::'a::order) ==> (~P ==> y < x) ==> P"
nipkow@15524
   119
  apply (drule order_less_not_sym)
nipkow@15524
   120
  apply (erule contrapos_np, simp)
nipkow@15524
   121
  done
nipkow@15524
   122
nipkow@15524
   123
lemma order_eq_iff: "!!x::'a::order. (x = y) = (x \<le> y & y \<le> x)"
nipkow@15524
   124
by (blast intro: order_antisym)
nipkow@15524
   125
nipkow@15524
   126
lemma order_antisym_conv: "(y::'a::order) <= x ==> (x <= y) = (x = y)"
nipkow@15524
   127
by(blast intro:order_antisym)
nipkow@15524
   128
haftmann@21083
   129
lemma less_imp_neq: "[| (x::'a::order) < y |] ==> x ~= y"
haftmann@21083
   130
  by (erule contrapos_pn, erule subst, rule order_less_irrefl)
haftmann@21083
   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
haftmann@21083
   149
lemma eq_neq_eq_imp_neq: "[| x = a ; a ~= b; b = y |] ==> x ~= y"
haftmann@21083
   150
  by (erule subst, erule ssubst, assumption)
nipkow@15524
   151
nipkow@15524
   152
text {* Useful for simplification, but too risky to include by default. *}
nipkow@15524
   153
nipkow@15524
   154
lemma order_less_imp_not_less: "(x::'a::order) < y ==>  (~ y < x) = True"
nipkow@15524
   155
  by (blast elim: order_less_asym)
nipkow@15524
   156
nipkow@15524
   157
lemma order_less_imp_triv: "(x::'a::order) < y ==>  (y < x --> P) = True"
nipkow@15524
   158
  by (blast elim: order_less_asym)
nipkow@15524
   159
nipkow@15524
   160
lemma order_less_imp_not_eq: "(x::'a::order) < y ==>  (x = y) = False"
nipkow@15524
   161
  by auto
nipkow@15524
   162
nipkow@15524
   163
lemma order_less_imp_not_eq2: "(x::'a::order) < y ==>  (y = x) = False"
nipkow@15524
   164
  by auto
nipkow@15524
   165
haftmann@21083
   166
text {* Transitivity rules for calculational reasoning *}
nipkow@15524
   167
nipkow@15524
   168
lemma order_neq_le_trans: "a ~= b ==> (a::'a::order) <= b ==> a < b"
nipkow@15524
   169
  by (simp add: order_less_le)
nipkow@15524
   170
nipkow@15524
   171
lemma order_le_neq_trans: "(a::'a::order) <= b ==> a ~= b ==> a < b"
nipkow@15524
   172
  by (simp add: order_less_le)
nipkow@15524
   173
nipkow@15524
   174
lemma order_less_asym': "(a::'a::order) < b ==> b < a ==> P"
nipkow@15524
   175
  by (rule order_less_asym)
nipkow@15524
   176
nipkow@15524
   177
haftmann@21216
   178
subsection {* Linear (total) orderings *}
haftmann@21216
   179
haftmann@21216
   180
locale linear_order = partial_order +
haftmann@21216
   181
  assumes linear: "x \<sqsubseteq> y \<or> y \<sqsubseteq> x"
nipkow@15524
   182
nipkow@15524
   183
axclass linorder < order
nipkow@15524
   184
  linorder_linear: "x <= y | y <= x"
nipkow@15524
   185
haftmann@21216
   186
interpretation linorder:
haftmann@21216
   187
  linear_order ["op \<le> \<Colon> 'a\<Colon>linorder \<Rightarrow> 'a \<Rightarrow> bool" "op < \<Colon> 'a\<Colon>linorder \<Rightarrow> 'a \<Rightarrow> bool"]
haftmann@21216
   188
  by unfold_locales (rule linorder_linear)
haftmann@21216
   189
nipkow@15524
   190
lemma linorder_less_linear: "!!x::'a::linorder. x<y | x=y | y<x"
nipkow@15524
   191
  apply (simp add: order_less_le)
nipkow@15524
   192
  apply (insert linorder_linear, blast)
nipkow@15524
   193
  done
nipkow@15524
   194
nipkow@15524
   195
lemma linorder_le_less_linear: "!!x::'a::linorder. x\<le>y | y<x"
nipkow@15524
   196
  by (simp add: order_le_less linorder_less_linear)
nipkow@15524
   197
nipkow@15524
   198
lemma linorder_le_cases [case_names le ge]:
nipkow@15524
   199
    "((x::'a::linorder) \<le> y ==> P) ==> (y \<le> x ==> P) ==> P"
nipkow@15524
   200
  by (insert linorder_linear, blast)
nipkow@15524
   201
nipkow@15524
   202
lemma linorder_cases [case_names less equal greater]:
nipkow@15524
   203
    "((x::'a::linorder) < y ==> P) ==> (x = y ==> P) ==> (y < x ==> P) ==> P"
nipkow@15524
   204
  by (insert linorder_less_linear, blast)
nipkow@15524
   205
nipkow@15524
   206
lemma linorder_not_less: "!!x::'a::linorder. (~ x < y) = (y <= x)"
nipkow@15524
   207
  apply (simp add: order_less_le)
nipkow@15524
   208
  apply (insert linorder_linear)
nipkow@15524
   209
  apply (blast intro: order_antisym)
nipkow@15524
   210
  done
nipkow@15524
   211
nipkow@15524
   212
lemma linorder_not_le: "!!x::'a::linorder. (~ x <= y) = (y < x)"
nipkow@15524
   213
  apply (simp add: order_less_le)
nipkow@15524
   214
  apply (insert linorder_linear)
nipkow@15524
   215
  apply (blast intro: order_antisym)
nipkow@15524
   216
  done
nipkow@15524
   217
nipkow@15524
   218
lemma linorder_neq_iff: "!!x::'a::linorder. (x ~= y) = (x<y | y<x)"
nipkow@15524
   219
by (cut_tac x = x and y = y in linorder_less_linear, auto)
nipkow@15524
   220
nipkow@15524
   221
lemma linorder_neqE: "x ~= (y::'a::linorder) ==> (x < y ==> R) ==> (y < x ==> R) ==> R"
nipkow@15524
   222
by (simp add: linorder_neq_iff, blast)
nipkow@15524
   223
nipkow@15524
   224
lemma linorder_antisym_conv1: "~ (x::'a::linorder) < y ==> (x <= y) = (x = y)"
nipkow@15524
   225
by(blast intro:order_antisym dest:linorder_not_less[THEN iffD1])
nipkow@15524
   226
nipkow@15524
   227
lemma linorder_antisym_conv2: "(x::'a::linorder) <= y ==> (~ x < y) = (x = y)"
nipkow@15524
   228
by(blast intro:order_antisym dest:linorder_not_less[THEN iffD1])
nipkow@15524
   229
nipkow@15524
   230
lemma linorder_antisym_conv3: "~ (y::'a::linorder) < x ==> (~ x < y) = (x = y)"
nipkow@15524
   231
by(blast intro:order_antisym dest:linorder_not_less[THEN iffD1])
nipkow@15524
   232
paulson@16796
   233
text{*Replacing the old Nat.leI*}
paulson@16796
   234
lemma leI: "~ x < y ==> y <= (x::'a::linorder)"
paulson@16796
   235
  by (simp only: linorder_not_less)
paulson@16796
   236
paulson@16796
   237
lemma leD: "y <= (x::'a::linorder) ==> ~ x < y"
paulson@16796
   238
  by (simp only: linorder_not_less)
paulson@16796
   239
paulson@16796
   240
(*FIXME inappropriate name (or delete altogether)*)
paulson@16796
   241
lemma not_leE: "~ y <= (x::'a::linorder) ==> x < y"
paulson@16796
   242
  by (simp only: linorder_not_le)
paulson@16796
   243
haftmann@21083
   244
haftmann@21083
   245
subsection {* Reasoning tools setup *}
haftmann@21083
   246
haftmann@21091
   247
ML {*
haftmann@21091
   248
local
haftmann@21091
   249
haftmann@21091
   250
fun decomp_gen sort thy (Trueprop $ t) =
haftmann@21091
   251
  let fun of_sort t = let val T = type_of t in
haftmann@21091
   252
        (* exclude numeric types: linear arithmetic subsumes transitivity *)
haftmann@21091
   253
        T <> HOLogic.natT andalso T <> HOLogic.intT andalso
haftmann@21091
   254
        T <> HOLogic.realT andalso Sign.of_sort thy (T, sort) end
haftmann@21091
   255
  fun dec (Const ("Not", _) $ t) = (
haftmann@21091
   256
	  case dec t of
haftmann@21091
   257
	    NONE => NONE
haftmann@21091
   258
	  | SOME (t1, rel, t2) => SOME (t1, "~" ^ rel, t2))
haftmann@21091
   259
	| dec (Const ("op =",  _) $ t1 $ t2) =
haftmann@21091
   260
	    if of_sort t1
haftmann@21091
   261
	    then SOME (t1, "=", t2)
haftmann@21091
   262
	    else NONE
haftmann@21091
   263
	| dec (Const ("Orderings.less_eq",  _) $ t1 $ t2) =
haftmann@21091
   264
	    if of_sort t1
haftmann@21091
   265
	    then SOME (t1, "<=", t2)
haftmann@21091
   266
	    else NONE
haftmann@21091
   267
	| dec (Const ("Orderings.less",  _) $ t1 $ t2) =
haftmann@21091
   268
	    if of_sort t1
haftmann@21091
   269
	    then SOME (t1, "<", t2)
haftmann@21091
   270
	    else NONE
haftmann@21091
   271
	| dec _ = NONE
haftmann@21091
   272
  in dec t end;
haftmann@21091
   273
haftmann@21091
   274
in
haftmann@21091
   275
haftmann@21091
   276
structure Quasi_Tac = Quasi_Tac_Fun (
haftmann@21091
   277
(* The setting up of Quasi_Tac serves as a demo.  Since there is no
haftmann@21091
   278
   class for quasi orders, the tactics Quasi_Tac.trans_tac and
haftmann@21091
   279
   Quasi_Tac.quasi_tac are not of much use. *)
haftmann@21091
   280
  struct
haftmann@21091
   281
    val le_trans = thm "order_trans";
haftmann@21091
   282
    val le_refl = thm "order_refl";
haftmann@21091
   283
    val eqD1 = thm "order_eq_refl";
haftmann@21091
   284
    val eqD2 = thm "sym" RS thm "order_eq_refl";
haftmann@21091
   285
    val less_reflE = thm "order_less_irrefl" RS thm "notE";
haftmann@21091
   286
    val less_imp_le = thm "order_less_imp_le";
haftmann@21091
   287
    val le_neq_trans = thm "order_le_neq_trans";
haftmann@21091
   288
    val neq_le_trans = thm "order_neq_le_trans";
haftmann@21091
   289
    val less_imp_neq = thm "less_imp_neq";
haftmann@21091
   290
    val decomp_trans = decomp_gen ["Orderings.order"];
haftmann@21091
   291
    val decomp_quasi = decomp_gen ["Orderings.order"];
haftmann@21091
   292
haftmann@21091
   293
  end);
haftmann@21091
   294
haftmann@21091
   295
structure Order_Tac = Order_Tac_Fun (
haftmann@21091
   296
  struct
haftmann@21091
   297
    val less_reflE = thm "order_less_irrefl" RS thm "notE";
haftmann@21091
   298
    val le_refl = thm "order_refl";
haftmann@21091
   299
    val less_imp_le = thm "order_less_imp_le";
haftmann@21091
   300
    val not_lessI = thm "linorder_not_less" RS thm "iffD2";
haftmann@21091
   301
    val not_leI = thm "linorder_not_le" RS thm "iffD2";
haftmann@21091
   302
    val not_lessD = thm "linorder_not_less" RS thm "iffD1";
haftmann@21091
   303
    val not_leD = thm "linorder_not_le" RS thm "iffD1";
haftmann@21091
   304
    val eqI = thm "order_antisym";
haftmann@21091
   305
    val eqD1 = thm "order_eq_refl";
haftmann@21091
   306
    val eqD2 = thm "sym" RS thm "order_eq_refl";
haftmann@21091
   307
    val less_trans = thm "order_less_trans";
haftmann@21091
   308
    val less_le_trans = thm "order_less_le_trans";
haftmann@21091
   309
    val le_less_trans = thm "order_le_less_trans";
haftmann@21091
   310
    val le_trans = thm "order_trans";
haftmann@21091
   311
    val le_neq_trans = thm "order_le_neq_trans";
haftmann@21091
   312
    val neq_le_trans = thm "order_neq_le_trans";
haftmann@21091
   313
    val less_imp_neq = thm "less_imp_neq";
haftmann@21091
   314
    val eq_neq_eq_imp_neq = thm "eq_neq_eq_imp_neq";
haftmann@21091
   315
    val not_sym = thm "not_sym";
haftmann@21091
   316
    val decomp_part = decomp_gen ["Orderings.order"];
haftmann@21091
   317
    val decomp_lin = decomp_gen ["Orderings.linorder"];
haftmann@21091
   318
haftmann@21091
   319
  end);
haftmann@21091
   320
haftmann@21091
   321
end;
haftmann@21091
   322
*}
haftmann@21091
   323
haftmann@21083
   324
setup {*
haftmann@21083
   325
let
haftmann@21083
   326
haftmann@21083
   327
val order_antisym_conv = thm "order_antisym_conv"
haftmann@21083
   328
val linorder_antisym_conv1 = thm "linorder_antisym_conv1"
haftmann@21083
   329
val linorder_antisym_conv2 = thm "linorder_antisym_conv2"
haftmann@21083
   330
val linorder_antisym_conv3 = thm "linorder_antisym_conv3"
haftmann@21083
   331
haftmann@21083
   332
fun prp t thm = (#prop (rep_thm thm) = t);
nipkow@15524
   333
haftmann@21083
   334
fun prove_antisym_le sg ss ((le as Const(_,T)) $ r $ s) =
haftmann@21083
   335
  let val prems = prems_of_ss ss;
haftmann@21083
   336
      val less = Const("Orderings.less",T);
haftmann@21083
   337
      val t = HOLogic.mk_Trueprop(le $ s $ r);
haftmann@21083
   338
  in case find_first (prp t) prems of
haftmann@21083
   339
       NONE =>
haftmann@21083
   340
         let val t = HOLogic.mk_Trueprop(HOLogic.Not $ (less $ r $ s))
haftmann@21083
   341
         in case find_first (prp t) prems of
haftmann@21083
   342
              NONE => NONE
haftmann@21083
   343
            | SOME thm => SOME(mk_meta_eq(thm RS linorder_antisym_conv1))
haftmann@21083
   344
         end
haftmann@21083
   345
     | SOME thm => SOME(mk_meta_eq(thm RS order_antisym_conv))
haftmann@21083
   346
  end
haftmann@21083
   347
  handle THM _ => NONE;
nipkow@15524
   348
haftmann@21083
   349
fun prove_antisym_less sg ss (NotC $ ((less as Const(_,T)) $ r $ s)) =
haftmann@21083
   350
  let val prems = prems_of_ss ss;
haftmann@21083
   351
      val le = Const("Orderings.less_eq",T);
haftmann@21083
   352
      val t = HOLogic.mk_Trueprop(le $ r $ s);
haftmann@21083
   353
  in case find_first (prp t) prems of
haftmann@21083
   354
       NONE =>
haftmann@21083
   355
         let val t = HOLogic.mk_Trueprop(NotC $ (less $ s $ r))
haftmann@21083
   356
         in case find_first (prp t) prems of
haftmann@21083
   357
              NONE => NONE
haftmann@21083
   358
            | SOME thm => SOME(mk_meta_eq(thm RS linorder_antisym_conv3))
haftmann@21083
   359
         end
haftmann@21083
   360
     | SOME thm => SOME(mk_meta_eq(thm RS linorder_antisym_conv2))
haftmann@21083
   361
  end
haftmann@21083
   362
  handle THM _ => NONE;
nipkow@15524
   363
haftmann@21083
   364
val antisym_le = Simplifier.simproc (the_context())
haftmann@21083
   365
  "antisym le" ["(x::'a::order) <= y"] prove_antisym_le;
haftmann@21083
   366
val antisym_less = Simplifier.simproc (the_context())
haftmann@21083
   367
  "antisym less" ["~ (x::'a::linorder) < y"] prove_antisym_less;
haftmann@21083
   368
haftmann@21083
   369
in
haftmann@21091
   370
  (fn thy => (Simplifier.change_simpset_of thy
haftmann@21091
   371
    (fn ss => ss
haftmann@21091
   372
       addsimprocs [antisym_le, antisym_less]
haftmann@21091
   373
       addSolver (mk_solver "Trans_linear" (fn _ => Order_Tac.linear_tac))
haftmann@21091
   374
       addSolver (mk_solver "Trans_partial" (fn _ => Order_Tac.partial_tac)))
haftmann@21091
   375
       (* Adding the transitivity reasoners also as safe solvers showed a slight
haftmann@21091
   376
          speed up, but the reasoning strength appears to be not higher (at least
haftmann@21091
   377
          no breaking of additional proofs in the entire HOL distribution, as
haftmann@21091
   378
          of 5 March 2004, was observed). *); thy))
haftmann@21083
   379
end
haftmann@21083
   380
*}
nipkow@15524
   381
nipkow@15524
   382
haftmann@21083
   383
subsection {* Bounded quantifiers *}
haftmann@21083
   384
haftmann@21083
   385
syntax
wenzelm@21180
   386
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3ALL _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   387
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3EX _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   388
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   389
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   390
wenzelm@21180
   391
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3ALL _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   392
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3EX _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   393
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _>=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   394
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _>=_./ _)" [0, 0, 10] 10)
haftmann@21083
   395
haftmann@21083
   396
syntax (xsymbols)
wenzelm@21180
   397
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   398
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   399
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   400
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   401
wenzelm@21180
   402
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   403
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   404
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   405
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   406
haftmann@21083
   407
syntax (HOL)
wenzelm@21180
   408
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3! _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   409
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3? _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   410
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3! _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   411
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3? _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   412
haftmann@21083
   413
syntax (HTML output)
wenzelm@21180
   414
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   415
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   416
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   417
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   418
wenzelm@21180
   419
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   420
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   421
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   422
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   423
haftmann@21083
   424
translations
haftmann@21083
   425
  "ALL x<y. P"   =>  "ALL x. x < y \<longrightarrow> P"
haftmann@21083
   426
  "EX x<y. P"    =>  "EX x. x < y \<and> P"
haftmann@21083
   427
  "ALL x<=y. P"  =>  "ALL x. x <= y \<longrightarrow> P"
haftmann@21083
   428
  "EX x<=y. P"   =>  "EX x. x <= y \<and> P"
haftmann@21083
   429
  "ALL x>y. P"   =>  "ALL x. x > y \<longrightarrow> P"
haftmann@21083
   430
  "EX x>y. P"    =>  "EX x. x > y \<and> P"
haftmann@21083
   431
  "ALL x>=y. P"  =>  "ALL x. x >= y \<longrightarrow> P"
haftmann@21083
   432
  "EX x>=y. P"   =>  "EX x. x >= y \<and> P"
haftmann@21083
   433
haftmann@21083
   434
print_translation {*
haftmann@21083
   435
let
wenzelm@21180
   436
  val syntax_name = Sign.const_syntax_name (the_context ());
wenzelm@21180
   437
  val impl = syntax_name "op -->";
wenzelm@21180
   438
  val conj = syntax_name "op &";
wenzelm@21180
   439
  val less = syntax_name "Orderings.less";
wenzelm@21180
   440
  val less_eq = syntax_name "Orderings.less_eq";
wenzelm@21180
   441
wenzelm@21180
   442
  val trans =
wenzelm@21180
   443
   [(("ALL ", impl, less), ("_All_less", "_All_greater")),
wenzelm@21180
   444
    (("ALL ", impl, less_eq), ("_All_less_eq", "_All_greater_eq")),
wenzelm@21180
   445
    (("EX ", conj, less), ("_Ex_less", "_Ex_greater")),
wenzelm@21180
   446
    (("EX ", conj, less_eq), ("_Ex_less_eq", "_Ex_greater_eq"))];
wenzelm@21180
   447
haftmann@21083
   448
  fun mk v v' c n P =
wenzelm@21180
   449
    if v = v' andalso not (Term.exists_subterm (fn Free (x, _) => x = v | _ => false) n)
haftmann@21083
   450
    then Syntax.const c $ Syntax.mark_bound v' $ n $ P else raise Match;
wenzelm@21180
   451
wenzelm@21180
   452
  fun tr' q = (q,
wenzelm@21180
   453
    fn [Const ("_bound", _) $ Free (v, _), Const (c, _) $ (Const (d, _) $ t $ u) $ P] =>
wenzelm@21180
   454
      (case AList.lookup (op =) trans (q, c, d) of
wenzelm@21180
   455
        NONE => raise Match
wenzelm@21180
   456
      | SOME (l, g) =>
wenzelm@21180
   457
          (case (t, u) of
wenzelm@21180
   458
            (Const ("_bound", _) $ Free (v', _), n) => mk v v' l n P
wenzelm@21180
   459
          | (n, Const ("_bound", _) $ Free (v', _)) => mk v v' g n P
wenzelm@21180
   460
          | _ => raise Match))
wenzelm@21180
   461
     | _ => raise Match);
wenzelm@21180
   462
in [tr' "ALL ", tr' "EX "] end
haftmann@21083
   463
*}
haftmann@21083
   464
haftmann@21083
   465
haftmann@21083
   466
subsection {* Transitivity reasoning on decreasing inequalities *}
haftmann@21083
   467
wenzelm@21180
   468
(* FIXME cleanup *)
wenzelm@21180
   469
haftmann@21083
   470
text {* These support proving chains of decreasing inequalities
haftmann@21083
   471
    a >= b >= c ... in Isar proofs. *}
haftmann@21083
   472
haftmann@21083
   473
lemma xt1:
haftmann@21083
   474
  "a = b ==> b > c ==> a > c"
haftmann@21083
   475
  "a > b ==> b = c ==> a > c"
haftmann@21083
   476
  "a = b ==> b >= c ==> a >= c"
haftmann@21083
   477
  "a >= b ==> b = c ==> a >= c"
haftmann@21083
   478
  "(x::'a::order) >= y ==> y >= x ==> x = y"
haftmann@21083
   479
  "(x::'a::order) >= y ==> y >= z ==> x >= z"
haftmann@21083
   480
  "(x::'a::order) > y ==> y >= z ==> x > z"
haftmann@21083
   481
  "(x::'a::order) >= y ==> y > z ==> x > z"
haftmann@21083
   482
  "(a::'a::order) > b ==> b > a ==> ?P"
haftmann@21083
   483
  "(x::'a::order) > y ==> y > z ==> x > z"
haftmann@21083
   484
  "(a::'a::order) >= b ==> a ~= b ==> a > b"
haftmann@21083
   485
  "(a::'a::order) ~= b ==> a >= b ==> a > b"
haftmann@21083
   486
  "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c" 
haftmann@21083
   487
  "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   488
  "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
   489
  "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@21083
   490
by auto
haftmann@21083
   491
haftmann@21083
   492
lemma xt2:
haftmann@21083
   493
  "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
   494
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
   495
haftmann@21083
   496
lemma xt3: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==> 
haftmann@21083
   497
    (!!x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@21083
   498
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
   499
haftmann@21083
   500
lemma xt4: "(a::'a::order) > f b ==> (b::'b::order) >= c ==>
haftmann@21083
   501
  (!!x y. x >= y ==> f x >= f y) ==> a > f c"
haftmann@21083
   502
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
   503
haftmann@21083
   504
lemma xt5: "(a::'a::order) > b ==> (f b::'b::order) >= c==>
haftmann@21083
   505
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   506
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
   507
haftmann@21083
   508
lemma xt6: "(a::'a::order) >= f b ==> b > c ==>
haftmann@21083
   509
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   510
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
   511
haftmann@21083
   512
lemma xt7: "(a::'a::order) >= b ==> (f b::'b::order) > c ==>
haftmann@21083
   513
    (!!x y. x >= y ==> f x >= f y) ==> f a > c"
haftmann@21083
   514
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
   515
haftmann@21083
   516
lemma xt8: "(a::'a::order) > f b ==> (b::'b::order) > c ==>
haftmann@21083
   517
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   518
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
   519
haftmann@21083
   520
lemma xt9: "(a::'a::order) > b ==> (f b::'b::order) > c ==>
haftmann@21083
   521
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   522
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
   523
haftmann@21083
   524
lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9
haftmann@21083
   525
haftmann@21083
   526
(* 
haftmann@21083
   527
  Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands
haftmann@21083
   528
  for the wrong thing in an Isar proof.
haftmann@21083
   529
haftmann@21083
   530
  The extra transitivity rules can be used as follows: 
haftmann@21083
   531
haftmann@21083
   532
lemma "(a::'a::order) > z"
haftmann@21083
   533
proof -
haftmann@21083
   534
  have "a >= b" (is "_ >= ?rhs")
haftmann@21083
   535
    sorry
haftmann@21083
   536
  also have "?rhs >= c" (is "_ >= ?rhs")
haftmann@21083
   537
    sorry
haftmann@21083
   538
  also (xtrans) have "?rhs = d" (is "_ = ?rhs")
haftmann@21083
   539
    sorry
haftmann@21083
   540
  also (xtrans) have "?rhs >= e" (is "_ >= ?rhs")
haftmann@21083
   541
    sorry
haftmann@21083
   542
  also (xtrans) have "?rhs > f" (is "_ > ?rhs")
haftmann@21083
   543
    sorry
haftmann@21083
   544
  also (xtrans) have "?rhs > z"
haftmann@21083
   545
    sorry
haftmann@21083
   546
  finally (xtrans) show ?thesis .
haftmann@21083
   547
qed
haftmann@21083
   548
haftmann@21083
   549
  Alternatively, one can use "declare xtrans [trans]" and then
haftmann@21083
   550
  leave out the "(xtrans)" above.
haftmann@21083
   551
*)
haftmann@21083
   552
haftmann@21216
   553
subsection {* Monotonicity, syntactic least value operator and syntactic min/max *}
haftmann@21083
   554
haftmann@21216
   555
locale mono =
haftmann@21216
   556
  fixes f
haftmann@21216
   557
  assumes mono: "A \<le> B \<Longrightarrow> f A \<le> f B"
haftmann@21216
   558
haftmann@21216
   559
lemmas monoI [intro?] = mono.intro
haftmann@21216
   560
  and monoD [dest?] = mono.mono
haftmann@21083
   561
haftmann@21083
   562
constdefs
haftmann@21083
   563
  Least :: "('a::ord => bool) => 'a"               (binder "LEAST " 10)
haftmann@21083
   564
  "Least P == THE x. P x & (ALL y. P y --> x <= y)"
haftmann@21083
   565
    -- {* We can no longer use LeastM because the latter requires Hilbert-AC. *}
haftmann@21083
   566
haftmann@21083
   567
constdefs
haftmann@21083
   568
  min :: "['a::ord, 'a] => 'a"
haftmann@21083
   569
  "min a b == (if a <= b then a else b)"
haftmann@21083
   570
  max :: "['a::ord, 'a] => 'a"
haftmann@21083
   571
  "max a b == (if a <= b then b else a)"
haftmann@21083
   572
nipkow@15524
   573
end