src/HOL/Orderings.thy
author ballarin
Thu Sep 27 17:28:05 2007 +0200 (2007-09-27)
changeset 24741 a53f5db5acbb
parent 24704 9a95634ab135
child 24748 ee0a0eb6b738
permissions -rw-r--r--
Fixed setup of transitivity reasoner (function decomp).
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@21329
     6
header {* Syntactic and abstract orders *}
nipkow@15524
     7
nipkow@15524
     8
theory Orderings
haftmann@23881
     9
imports Set Fun
haftmann@23263
    10
uses
haftmann@23263
    11
  "~~/src/Provers/order.ML"
nipkow@15524
    12
begin
nipkow@15524
    13
haftmann@22841
    14
subsection {* Partial orders *}
nipkow@15524
    15
haftmann@22841
    16
class order = ord +
haftmann@22316
    17
  assumes less_le: "x \<sqsubset> y \<longleftrightarrow> x \<sqsubseteq> y \<and> x \<noteq> y"
haftmann@22384
    18
  and order_refl [iff]: "x \<sqsubseteq> x"
haftmann@22384
    19
  and order_trans: "x \<sqsubseteq> y \<Longrightarrow> y \<sqsubseteq> z \<Longrightarrow> x \<sqsubseteq> z"
haftmann@22841
    20
  assumes antisym: "x \<sqsubseteq> y \<Longrightarrow> y \<sqsubseteq> x \<Longrightarrow> x = y"
haftmann@22841
    21
haftmann@21248
    22
begin
haftmann@21248
    23
nipkow@15524
    24
text {* Reflexivity. *}
nipkow@15524
    25
haftmann@22841
    26
lemma eq_refl: "x = y \<Longrightarrow> x \<^loc>\<le> y"
nipkow@15524
    27
    -- {* This form is useful with the classical reasoner. *}
nipkow@23212
    28
by (erule ssubst) (rule order_refl)
nipkow@15524
    29
haftmann@22841
    30
lemma less_irrefl [iff]: "\<not> x \<^loc>< x"
nipkow@23212
    31
by (simp add: less_le)
nipkow@15524
    32
haftmann@22841
    33
lemma le_less: "x \<^loc>\<le> y \<longleftrightarrow> x \<^loc>< y \<or> x = y"
nipkow@15524
    34
    -- {* NOT suitable for iff, since it can cause PROOF FAILED. *}
nipkow@23212
    35
by (simp add: less_le) blast
nipkow@15524
    36
haftmann@22841
    37
lemma le_imp_less_or_eq: "x \<^loc>\<le> y \<Longrightarrow> x \<^loc>< y \<or> x = y"
nipkow@23212
    38
unfolding less_le by blast
nipkow@15524
    39
haftmann@22841
    40
lemma less_imp_le: "x \<^loc>< y \<Longrightarrow> x \<^loc>\<le> y"
nipkow@23212
    41
unfolding less_le by blast
haftmann@21248
    42
haftmann@22841
    43
lemma less_imp_neq: "x \<^loc>< y \<Longrightarrow> x \<noteq> y"
nipkow@23212
    44
by (erule contrapos_pn, erule subst, rule less_irrefl)
haftmann@21329
    45
haftmann@21329
    46
haftmann@21329
    47
text {* Useful for simplification, but too risky to include by default. *}
haftmann@21329
    48
haftmann@22841
    49
lemma less_imp_not_eq: "x \<^loc>< y \<Longrightarrow> (x = y) \<longleftrightarrow> False"
nipkow@23212
    50
by auto
haftmann@21329
    51
haftmann@22841
    52
lemma less_imp_not_eq2: "x \<^loc>< y \<Longrightarrow> (y = x) \<longleftrightarrow> False"
nipkow@23212
    53
by auto
haftmann@21329
    54
haftmann@21329
    55
haftmann@21329
    56
text {* Transitivity rules for calculational reasoning *}
haftmann@21329
    57
haftmann@22841
    58
lemma neq_le_trans: "a \<noteq> b \<Longrightarrow> a \<^loc>\<le> b \<Longrightarrow> a \<^loc>< b"
nipkow@23212
    59
by (simp add: less_le)
haftmann@21329
    60
haftmann@22841
    61
lemma le_neq_trans: "a \<^loc>\<le> b \<Longrightarrow> a \<noteq> b \<Longrightarrow> a \<^loc>< b"
nipkow@23212
    62
by (simp add: less_le)
haftmann@21329
    63
nipkow@15524
    64
nipkow@15524
    65
text {* Asymmetry. *}
nipkow@15524
    66
haftmann@22841
    67
lemma less_not_sym: "x \<^loc>< y \<Longrightarrow> \<not> (y \<^loc>< x)"
nipkow@23212
    68
by (simp add: less_le antisym)
nipkow@15524
    69
haftmann@22841
    70
lemma less_asym: "x \<^loc>< y \<Longrightarrow> (\<not> P \<Longrightarrow> y \<^loc>< x) \<Longrightarrow> P"
nipkow@23212
    71
by (drule less_not_sym, erule contrapos_np) simp
nipkow@15524
    72
haftmann@22841
    73
lemma eq_iff: "x = y \<longleftrightarrow> x \<^loc>\<le> y \<and> y \<^loc>\<le> x"
nipkow@23212
    74
by (blast intro: antisym)
nipkow@15524
    75
haftmann@22841
    76
lemma antisym_conv: "y \<^loc>\<le> x \<Longrightarrow> x \<^loc>\<le> y \<longleftrightarrow> x = y"
nipkow@23212
    77
by (blast intro: antisym)
nipkow@15524
    78
haftmann@22841
    79
lemma less_imp_neq: "x \<^loc>< y \<Longrightarrow> x \<noteq> y"
nipkow@23212
    80
by (erule contrapos_pn, erule subst, rule less_irrefl)
haftmann@21248
    81
haftmann@21083
    82
nipkow@15524
    83
text {* Transitivity. *}
nipkow@15524
    84
haftmann@22841
    85
lemma less_trans: "x \<^loc>< y \<Longrightarrow> y \<^loc>< z \<Longrightarrow> x \<^loc>< z"
nipkow@23212
    86
by (simp add: less_le) (blast intro: order_trans antisym)
nipkow@15524
    87
haftmann@22841
    88
lemma le_less_trans: "x \<^loc>\<le> y \<Longrightarrow> y \<^loc>< z \<Longrightarrow> x \<^loc>< z"
nipkow@23212
    89
by (simp add: less_le) (blast intro: order_trans antisym)
nipkow@15524
    90
haftmann@22841
    91
lemma less_le_trans: "x \<^loc>< y \<Longrightarrow> y \<^loc>\<le> z \<Longrightarrow> x \<^loc>< z"
nipkow@23212
    92
by (simp add: less_le) (blast intro: order_trans antisym)
nipkow@15524
    93
nipkow@15524
    94
nipkow@15524
    95
text {* Useful for simplification, but too risky to include by default. *}
nipkow@15524
    96
haftmann@22841
    97
lemma less_imp_not_less: "x \<^loc>< y \<Longrightarrow> (\<not> y \<^loc>< x) \<longleftrightarrow> True"
nipkow@23212
    98
by (blast elim: less_asym)
nipkow@15524
    99
haftmann@22841
   100
lemma less_imp_triv: "x \<^loc>< y \<Longrightarrow> (y \<^loc>< x \<longrightarrow> P) \<longleftrightarrow> True"
nipkow@23212
   101
by (blast elim: less_asym)
nipkow@15524
   102
haftmann@21248
   103
haftmann@21083
   104
text {* Transitivity rules for calculational reasoning *}
nipkow@15524
   105
haftmann@22841
   106
lemma less_asym': "a \<^loc>< b \<Longrightarrow> b \<^loc>< a \<Longrightarrow> P"
nipkow@23212
   107
by (rule less_asym)
haftmann@21248
   108
haftmann@22916
   109
haftmann@22916
   110
text {* Reverse order *}
haftmann@22916
   111
haftmann@22916
   112
lemma order_reverse:
haftmann@23018
   113
  "order (\<lambda>x y. y \<^loc>\<le> x) (\<lambda>x y. y \<^loc>< x)"
nipkow@23212
   114
by unfold_locales
nipkow@23212
   115
   (simp add: less_le, auto intro: antisym order_trans)
haftmann@22916
   116
haftmann@21248
   117
end
nipkow@15524
   118
haftmann@21329
   119
haftmann@21329
   120
subsection {* Linear (total) orders *}
haftmann@21329
   121
haftmann@22316
   122
class linorder = order +
haftmann@21216
   123
  assumes linear: "x \<sqsubseteq> y \<or> y \<sqsubseteq> x"
haftmann@21248
   124
begin
haftmann@21248
   125
haftmann@22841
   126
lemma less_linear: "x \<^loc>< y \<or> x = y \<or> y \<^loc>< x"
nipkow@23212
   127
unfolding less_le using less_le linear by blast
haftmann@21248
   128
haftmann@22841
   129
lemma le_less_linear: "x \<^loc>\<le> y \<or> y \<^loc>< x"
nipkow@23212
   130
by (simp add: le_less less_linear)
haftmann@21248
   131
haftmann@21248
   132
lemma le_cases [case_names le ge]:
haftmann@22841
   133
  "(x \<^loc>\<le> y \<Longrightarrow> P) \<Longrightarrow> (y \<^loc>\<le> x \<Longrightarrow> P) \<Longrightarrow> P"
nipkow@23212
   134
using linear by blast
haftmann@21248
   135
haftmann@22384
   136
lemma linorder_cases [case_names less equal greater]:
nipkow@23212
   137
  "(x \<^loc>< y \<Longrightarrow> P) \<Longrightarrow> (x = y \<Longrightarrow> P) \<Longrightarrow> (y \<^loc>< x \<Longrightarrow> P) \<Longrightarrow> P"
nipkow@23212
   138
using less_linear by blast
haftmann@21248
   139
haftmann@22841
   140
lemma not_less: "\<not> x \<^loc>< y \<longleftrightarrow> y \<^loc>\<le> x"
nipkow@23212
   141
apply (simp add: less_le)
nipkow@23212
   142
using linear apply (blast intro: antisym)
nipkow@23212
   143
done
nipkow@23212
   144
nipkow@23212
   145
lemma not_less_iff_gr_or_eq:
nipkow@23212
   146
 "\<not>(x \<^loc>< y) \<longleftrightarrow> (x \<^loc>> y | x = y)"
nipkow@23212
   147
apply(simp add:not_less le_less)
nipkow@23212
   148
apply blast
nipkow@23212
   149
done
nipkow@15524
   150
haftmann@22841
   151
lemma not_le: "\<not> x \<^loc>\<le> y \<longleftrightarrow> y \<^loc>< x"
nipkow@23212
   152
apply (simp add: less_le)
nipkow@23212
   153
using linear apply (blast intro: antisym)
nipkow@23212
   154
done
nipkow@15524
   155
haftmann@22841
   156
lemma neq_iff: "x \<noteq> y \<longleftrightarrow> x \<^loc>< y \<or> y \<^loc>< x"
nipkow@23212
   157
by (cut_tac x = x and y = y in less_linear, auto)
nipkow@15524
   158
haftmann@22841
   159
lemma neqE: "x \<noteq> y \<Longrightarrow> (x \<^loc>< y \<Longrightarrow> R) \<Longrightarrow> (y \<^loc>< x \<Longrightarrow> R) \<Longrightarrow> R"
nipkow@23212
   160
by (simp add: neq_iff) blast
nipkow@15524
   161
haftmann@22841
   162
lemma antisym_conv1: "\<not> x \<^loc>< y \<Longrightarrow> x \<^loc>\<le> y \<longleftrightarrow> x = y"
nipkow@23212
   163
by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   164
haftmann@22841
   165
lemma antisym_conv2: "x \<^loc>\<le> y \<Longrightarrow> \<not> x \<^loc>< y \<longleftrightarrow> x = y"
nipkow@23212
   166
by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   167
haftmann@22841
   168
lemma antisym_conv3: "\<not> y \<^loc>< x \<Longrightarrow> \<not> x \<^loc>< y \<longleftrightarrow> x = y"
nipkow@23212
   169
by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   170
paulson@16796
   171
text{*Replacing the old Nat.leI*}
haftmann@22841
   172
lemma leI: "\<not> x \<^loc>< y \<Longrightarrow> y \<^loc>\<le> x"
nipkow@23212
   173
unfolding not_less .
paulson@16796
   174
haftmann@22841
   175
lemma leD: "y \<^loc>\<le> x \<Longrightarrow> \<not> x \<^loc>< y"
nipkow@23212
   176
unfolding not_less .
paulson@16796
   177
paulson@16796
   178
(*FIXME inappropriate name (or delete altogether)*)
haftmann@22841
   179
lemma not_leE: "\<not> y \<^loc>\<le> x \<Longrightarrow> x \<^loc>< y"
nipkow@23212
   180
unfolding not_le .
haftmann@21248
   181
haftmann@22916
   182
haftmann@22916
   183
text {* Reverse order *}
haftmann@22916
   184
haftmann@22916
   185
lemma linorder_reverse:
haftmann@23018
   186
  "linorder (\<lambda>x y. y \<^loc>\<le> x) (\<lambda>x y. y \<^loc>< x)"
nipkow@23212
   187
by unfold_locales
nipkow@23212
   188
  (simp add: less_le, auto intro: antisym order_trans simp add: linear)
haftmann@22916
   189
haftmann@22916
   190
haftmann@23881
   191
text {* min/max *}
haftmann@23881
   192
haftmann@23881
   193
text {* for historic reasons, definitions are done in context ord *}
haftmann@23881
   194
haftmann@23881
   195
definition (in ord)
haftmann@23881
   196
  min :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@23948
   197
  [code unfold, code inline del]: "min a b = (if a \<^loc>\<le> b then a else b)"
haftmann@23881
   198
haftmann@23881
   199
definition (in ord)
haftmann@23881
   200
  max :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@23948
   201
  [code unfold, code inline del]: "max a b = (if a \<^loc>\<le> b then b else a)"
haftmann@22384
   202
haftmann@21383
   203
lemma min_le_iff_disj:
haftmann@22841
   204
  "min x y \<^loc>\<le> z \<longleftrightarrow> x \<^loc>\<le> z \<or> y \<^loc>\<le> z"
nipkow@23212
   205
unfolding min_def using linear by (auto intro: order_trans)
haftmann@21383
   206
haftmann@21383
   207
lemma le_max_iff_disj:
haftmann@22841
   208
  "z \<^loc>\<le> max x y \<longleftrightarrow> z \<^loc>\<le> x \<or> z \<^loc>\<le> y"
nipkow@23212
   209
unfolding max_def using linear by (auto intro: order_trans)
haftmann@21383
   210
haftmann@21383
   211
lemma min_less_iff_disj:
haftmann@22841
   212
  "min x y \<^loc>< z \<longleftrightarrow> x \<^loc>< z \<or> y \<^loc>< z"
nipkow@23212
   213
unfolding min_def le_less using less_linear by (auto intro: less_trans)
haftmann@21383
   214
haftmann@21383
   215
lemma less_max_iff_disj:
haftmann@22841
   216
  "z \<^loc>< max x y \<longleftrightarrow> z \<^loc>< x \<or> z \<^loc>< y"
nipkow@23212
   217
unfolding max_def le_less using less_linear by (auto intro: less_trans)
haftmann@21383
   218
haftmann@21383
   219
lemma min_less_iff_conj [simp]:
haftmann@22841
   220
  "z \<^loc>< min x y \<longleftrightarrow> z \<^loc>< x \<and> z \<^loc>< y"
nipkow@23212
   221
unfolding min_def le_less using less_linear by (auto intro: less_trans)
haftmann@21383
   222
haftmann@21383
   223
lemma max_less_iff_conj [simp]:
haftmann@22841
   224
  "max x y \<^loc>< z \<longleftrightarrow> x \<^loc>< z \<and> y \<^loc>< z"
nipkow@23212
   225
unfolding max_def le_less using less_linear by (auto intro: less_trans)
haftmann@21383
   226
paulson@24286
   227
lemma split_min [noatp]:
haftmann@22841
   228
  "P (min i j) \<longleftrightarrow> (i \<^loc>\<le> j \<longrightarrow> P i) \<and> (\<not> i \<^loc>\<le> j \<longrightarrow> P j)"
nipkow@23212
   229
by (simp add: min_def)
haftmann@21383
   230
paulson@24286
   231
lemma split_max [noatp]:
haftmann@22841
   232
  "P (max i j) \<longleftrightarrow> (i \<^loc>\<le> j \<longrightarrow> P j) \<and> (\<not> i \<^loc>\<le> j \<longrightarrow> P i)"
nipkow@23212
   233
by (simp add: max_def)
haftmann@21383
   234
haftmann@21248
   235
end
haftmann@21248
   236
haftmann@23948
   237
haftmann@21083
   238
subsection {* Reasoning tools setup *}
haftmann@21083
   239
haftmann@21091
   240
ML {*
haftmann@21091
   241
ballarin@24641
   242
signature ORDERS =
ballarin@24641
   243
sig
ballarin@24641
   244
  val print_structures: Proof.context -> unit
ballarin@24641
   245
  val setup: theory -> theory
ballarin@24704
   246
  val order_tac: thm list -> Proof.context -> int -> tactic
ballarin@24641
   247
end;
haftmann@21091
   248
ballarin@24641
   249
structure Orders: ORDERS =
haftmann@21248
   250
struct
ballarin@24641
   251
ballarin@24641
   252
(** Theory and context data **)
ballarin@24641
   253
ballarin@24641
   254
fun struct_eq ((s1: string, ts1), (s2, ts2)) =
ballarin@24641
   255
  (s1 = s2) andalso eq_list (op aconv) (ts1, ts2);
ballarin@24641
   256
ballarin@24641
   257
structure Data = GenericDataFun
ballarin@24641
   258
(
ballarin@24641
   259
  type T = ((string * term list) * Order_Tac.less_arith) list;
ballarin@24641
   260
    (* Order structures:
ballarin@24641
   261
       identifier of the structure, list of operations and record of theorems
ballarin@24641
   262
       needed to set up the transitivity reasoner,
ballarin@24641
   263
       identifier and operations identify the structure uniquely. *)
ballarin@24641
   264
  val empty = [];
ballarin@24641
   265
  val extend = I;
ballarin@24641
   266
  fun merge _ = AList.join struct_eq (K fst);
ballarin@24641
   267
);
ballarin@24641
   268
ballarin@24641
   269
fun print_structures ctxt =
ballarin@24641
   270
  let
ballarin@24641
   271
    val structs = Data.get (Context.Proof ctxt);
ballarin@24641
   272
    fun pretty_term t = Pretty.block
ballarin@24641
   273
      [Pretty.quote (ProofContext.pretty_term ctxt t), Pretty.brk 1,
ballarin@24641
   274
        Pretty.str "::", Pretty.brk 1,
ballarin@24641
   275
        Pretty.quote (ProofContext.pretty_typ ctxt (type_of t))];
ballarin@24641
   276
    fun pretty_struct ((s, ts), _) = Pretty.block
ballarin@24641
   277
      [Pretty.str s, Pretty.str ":", Pretty.brk 1,
ballarin@24641
   278
       Pretty.enclose "(" ")" (Pretty.breaks (map pretty_term ts))];
ballarin@24641
   279
  in
ballarin@24641
   280
    Pretty.writeln (Pretty.big_list "Order structures:" (map pretty_struct structs))
ballarin@24641
   281
  end;
ballarin@24641
   282
ballarin@24641
   283
ballarin@24641
   284
(** Method **)
haftmann@21091
   285
ballarin@24704
   286
fun struct_tac ((s, [eq, le, less]), thms) prems =
ballarin@24641
   287
  let
ballarin@24641
   288
    fun decomp thy (Trueprop $ t) =
ballarin@24641
   289
      let
ballarin@24641
   290
        fun excluded t =
ballarin@24641
   291
          (* exclude numeric types: linear arithmetic subsumes transitivity *)
ballarin@24641
   292
          let val T = type_of t
ballarin@24641
   293
          in
ballarin@24641
   294
	    T = HOLogic.natT orelse T = HOLogic.intT orelse T = HOLogic.realT
ballarin@24641
   295
          end;
ballarin@24741
   296
	fun rel (bin_op $ t1 $ t2) =
ballarin@24641
   297
              if excluded t1 then NONE
ballarin@24641
   298
              else if Pattern.matches thy (eq, bin_op) then SOME (t1, "=", t2)
ballarin@24641
   299
              else if Pattern.matches thy (le, bin_op) then SOME (t1, "<=", t2)
ballarin@24641
   300
              else if Pattern.matches thy (less, bin_op) then SOME (t1, "<", t2)
ballarin@24641
   301
              else NONE
ballarin@24741
   302
	  | rel _ = NONE;
ballarin@24741
   303
	fun dec (Const (@{const_name Not}, _) $ t) = (case rel t
ballarin@24741
   304
	      of NONE => NONE
ballarin@24741
   305
	       | SOME (t1, rel, t2) => SOME (t1, "~" ^ rel, t2))
ballarin@24741
   306
          | dec x = rel x;
ballarin@24641
   307
      in dec t end;
ballarin@24641
   308
  in
ballarin@24641
   309
    case s of
ballarin@24704
   310
      "order" => Order_Tac.partial_tac decomp thms prems
ballarin@24704
   311
    | "linorder" => Order_Tac.linear_tac decomp thms prems
ballarin@24641
   312
    | _ => error ("Unknown kind of order `" ^ s ^ "' encountered in transitivity reasoner.")
ballarin@24641
   313
  end
ballarin@24641
   314
ballarin@24704
   315
fun order_tac prems ctxt =
ballarin@24704
   316
  FIRST' (map (fn s => CHANGED o struct_tac s prems) (Data.get (Context.Proof ctxt)));
ballarin@24641
   317
ballarin@24641
   318
ballarin@24641
   319
(** Attribute **)
ballarin@24641
   320
ballarin@24641
   321
fun add_struct_thm s tag =
ballarin@24641
   322
  Thm.declaration_attribute
ballarin@24641
   323
    (fn thm => Data.map (AList.map_default struct_eq (s, Order_Tac.empty TrueI) (Order_Tac.update tag thm)));
ballarin@24641
   324
fun del_struct s =
ballarin@24641
   325
  Thm.declaration_attribute
ballarin@24641
   326
    (fn _ => Data.map (AList.delete struct_eq s));
ballarin@24641
   327
ballarin@24641
   328
val attribute = Attrib.syntax
ballarin@24641
   329
     (Scan.lift ((Args.add -- Args.name >> (fn (_, s) => SOME s) ||
ballarin@24641
   330
          Args.del >> K NONE) --| Args.colon (* FIXME ||
ballarin@24641
   331
        Scan.succeed true *) ) -- Scan.lift Args.name --
ballarin@24641
   332
      Scan.repeat Args.term
ballarin@24641
   333
      >> (fn ((SOME tag, n), ts) => add_struct_thm (n, ts) tag
ballarin@24641
   334
           | ((NONE, n), ts) => del_struct (n, ts)));
ballarin@24641
   335
ballarin@24641
   336
ballarin@24641
   337
(** Diagnostic command **)
ballarin@24641
   338
ballarin@24641
   339
val print = Toplevel.unknown_context o
ballarin@24641
   340
  Toplevel.keep (Toplevel.node_case
ballarin@24641
   341
    (Context.cases (print_structures o ProofContext.init) print_structures)
ballarin@24641
   342
    (print_structures o Proof.context_of));
ballarin@24641
   343
ballarin@24641
   344
val printP =
ballarin@24641
   345
  OuterSyntax.improper_command "print_orders"
ballarin@24641
   346
    "print order structures available to transitivity reasoner" OuterKeyword.diag
ballarin@24641
   347
    (Scan.succeed (Toplevel.no_timing o print));
ballarin@24641
   348
ballarin@24641
   349
ballarin@24641
   350
(** Setup **)
ballarin@24641
   351
ballarin@24641
   352
val setup = let val _ = OuterSyntax.add_parsers [printP] in
ballarin@24704
   353
    Method.add_methods [("order", Method.ctxt_args (Method.SIMPLE_METHOD' o order_tac []),
ballarin@24641
   354
      "normalisation of algebraic structure")] #>
ballarin@24641
   355
    Attrib.add_attributes [("order", attribute, "theorems controlling transitivity reasoner")]
ballarin@24641
   356
  end;
haftmann@21091
   357
haftmann@21091
   358
end;
ballarin@24641
   359
haftmann@21091
   360
*}
haftmann@21091
   361
ballarin@24641
   362
setup Orders.setup
ballarin@24641
   363
ballarin@24641
   364
ballarin@24641
   365
text {* Declarations to set up transitivity reasoner of partial and linear orders. *}
ballarin@24641
   366
ballarin@24641
   367
(* The type constraint on @{term op =} below is necessary since the operation
ballarin@24641
   368
   is not a parameter of the locale. *)
ballarin@24641
   369
lemmas (in order)
ballarin@24641
   370
  [order add less_reflE: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   371
  less_irrefl [THEN notE]
ballarin@24641
   372
lemmas (in order)
ballarin@24641
   373
  [order add le_refl: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   374
  order_refl
ballarin@24641
   375
lemmas (in order)
ballarin@24641
   376
  [order add less_imp_le: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   377
  less_imp_le
ballarin@24641
   378
lemmas (in order)
ballarin@24641
   379
  [order add eqI: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   380
  antisym
ballarin@24641
   381
lemmas (in order)
ballarin@24641
   382
  [order add eqD1: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   383
  eq_refl
ballarin@24641
   384
lemmas (in order)
ballarin@24641
   385
  [order add eqD2: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   386
  sym [THEN eq_refl]
ballarin@24641
   387
lemmas (in order)
ballarin@24641
   388
  [order add less_trans: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   389
  less_trans
ballarin@24641
   390
lemmas (in order)
ballarin@24641
   391
  [order add less_le_trans: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   392
  less_le_trans
ballarin@24641
   393
lemmas (in order)
ballarin@24641
   394
  [order add le_less_trans: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   395
  le_less_trans
ballarin@24641
   396
lemmas (in order)
ballarin@24641
   397
  [order add le_trans: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   398
  order_trans
ballarin@24641
   399
lemmas (in order)
ballarin@24641
   400
  [order add le_neq_trans: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   401
  le_neq_trans
ballarin@24641
   402
lemmas (in order)
ballarin@24641
   403
  [order add neq_le_trans: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   404
  neq_le_trans
ballarin@24641
   405
lemmas (in order)
ballarin@24641
   406
  [order add less_imp_neq: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   407
  less_imp_neq
ballarin@24641
   408
lemmas (in order)
ballarin@24641
   409
  [order add eq_neq_eq_imp_neq: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   410
   eq_neq_eq_imp_neq
ballarin@24641
   411
lemmas (in order)
ballarin@24641
   412
  [order add not_sym: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   413
  not_sym
ballarin@24641
   414
ballarin@24641
   415
lemmas (in linorder) [order del: order "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] = _
ballarin@24641
   416
ballarin@24641
   417
lemmas (in linorder)
ballarin@24641
   418
  [order add less_reflE: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   419
  less_irrefl [THEN notE]
ballarin@24641
   420
lemmas (in linorder)
ballarin@24641
   421
  [order add le_refl: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   422
  order_refl
ballarin@24641
   423
lemmas (in linorder)
ballarin@24641
   424
  [order add less_imp_le: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   425
  less_imp_le
ballarin@24641
   426
lemmas (in linorder)
ballarin@24641
   427
  [order add not_lessI: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   428
  not_less [THEN iffD2]
ballarin@24641
   429
lemmas (in linorder)
ballarin@24641
   430
  [order add not_leI: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   431
  not_le [THEN iffD2]
ballarin@24641
   432
lemmas (in linorder)
ballarin@24641
   433
  [order add not_lessD: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   434
  not_less [THEN iffD1]
ballarin@24641
   435
lemmas (in linorder)
ballarin@24641
   436
  [order add not_leD: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   437
  not_le [THEN iffD1]
ballarin@24641
   438
lemmas (in linorder)
ballarin@24641
   439
  [order add eqI: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   440
  antisym
ballarin@24641
   441
lemmas (in linorder)
ballarin@24641
   442
  [order add eqD1: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   443
  eq_refl
ballarin@24641
   444
lemmas (in linorder)
ballarin@24641
   445
  [order add eqD2: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   446
  sym [THEN eq_refl]
ballarin@24641
   447
lemmas (in linorder)
ballarin@24641
   448
  [order add less_trans: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   449
  less_trans
ballarin@24641
   450
lemmas (in linorder)
ballarin@24641
   451
  [order add less_le_trans: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   452
  less_le_trans
ballarin@24641
   453
lemmas (in linorder)
ballarin@24641
   454
  [order add le_less_trans: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   455
  le_less_trans
ballarin@24641
   456
lemmas (in linorder)
ballarin@24641
   457
  [order add le_trans: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   458
  order_trans
ballarin@24641
   459
lemmas (in linorder)
ballarin@24641
   460
  [order add le_neq_trans: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   461
  le_neq_trans
ballarin@24641
   462
lemmas (in linorder)
ballarin@24641
   463
  [order add neq_le_trans: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   464
  neq_le_trans
ballarin@24641
   465
lemmas (in linorder)
ballarin@24641
   466
  [order add less_imp_neq: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   467
  less_imp_neq
ballarin@24641
   468
lemmas (in linorder)
ballarin@24641
   469
  [order add eq_neq_eq_imp_neq: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   470
  eq_neq_eq_imp_neq
ballarin@24641
   471
lemmas (in linorder)
ballarin@24641
   472
  [order add not_sym: linorder "op = :: 'a => 'a => bool" "op \<^loc><=" "op \<^loc><"] =
ballarin@24641
   473
  not_sym
ballarin@24641
   474
ballarin@24641
   475
haftmann@21083
   476
setup {*
haftmann@21083
   477
let
haftmann@21083
   478
haftmann@21083
   479
fun prp t thm = (#prop (rep_thm thm) = t);
nipkow@15524
   480
haftmann@21083
   481
fun prove_antisym_le sg ss ((le as Const(_,T)) $ r $ s) =
haftmann@21083
   482
  let val prems = prems_of_ss ss;
haftmann@22916
   483
      val less = Const (@{const_name less}, T);
haftmann@21083
   484
      val t = HOLogic.mk_Trueprop(le $ s $ r);
haftmann@21083
   485
  in case find_first (prp t) prems of
haftmann@21083
   486
       NONE =>
haftmann@21083
   487
         let val t = HOLogic.mk_Trueprop(HOLogic.Not $ (less $ r $ s))
haftmann@21083
   488
         in case find_first (prp t) prems of
haftmann@21083
   489
              NONE => NONE
haftmann@24422
   490
            | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv1}))
haftmann@21083
   491
         end
haftmann@24422
   492
     | SOME thm => SOME(mk_meta_eq(thm RS @{thm order_class.antisym_conv}))
haftmann@21083
   493
  end
haftmann@21083
   494
  handle THM _ => NONE;
nipkow@15524
   495
haftmann@21083
   496
fun prove_antisym_less sg ss (NotC $ ((less as Const(_,T)) $ r $ s)) =
haftmann@21083
   497
  let val prems = prems_of_ss ss;
haftmann@22916
   498
      val le = Const (@{const_name less_eq}, T);
haftmann@21083
   499
      val t = HOLogic.mk_Trueprop(le $ r $ s);
haftmann@21083
   500
  in case find_first (prp t) prems of
haftmann@21083
   501
       NONE =>
haftmann@21083
   502
         let val t = HOLogic.mk_Trueprop(NotC $ (less $ s $ r))
haftmann@21083
   503
         in case find_first (prp t) prems of
haftmann@21083
   504
              NONE => NONE
haftmann@24422
   505
            | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv3}))
haftmann@21083
   506
         end
haftmann@24422
   507
     | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv2}))
haftmann@21083
   508
  end
haftmann@21083
   509
  handle THM _ => NONE;
nipkow@15524
   510
haftmann@21248
   511
fun add_simprocs procs thy =
haftmann@21248
   512
  (Simplifier.change_simpset_of thy (fn ss => ss
haftmann@21248
   513
    addsimprocs (map (fn (name, raw_ts, proc) =>
haftmann@21248
   514
      Simplifier.simproc thy name raw_ts proc)) procs); thy);
haftmann@21248
   515
fun add_solver name tac thy =
haftmann@21248
   516
  (Simplifier.change_simpset_of thy (fn ss => ss addSolver
ballarin@24704
   517
    (mk_solver' name (fn ss => tac (MetaSimplifier.prems_of_ss ss) (MetaSimplifier.the_context ss)))); thy);
haftmann@21083
   518
haftmann@21083
   519
in
haftmann@21248
   520
  add_simprocs [
haftmann@21248
   521
       ("antisym le", ["(x::'a::order) <= y"], prove_antisym_le),
haftmann@21248
   522
       ("antisym less", ["~ (x::'a::linorder) < y"], prove_antisym_less)
haftmann@21248
   523
     ]
ballarin@24641
   524
  #> add_solver "Transitivity" Orders.order_tac
haftmann@21248
   525
  (* Adding the transitivity reasoners also as safe solvers showed a slight
haftmann@21248
   526
     speed up, but the reasoning strength appears to be not higher (at least
haftmann@21248
   527
     no breaking of additional proofs in the entire HOL distribution, as
haftmann@21248
   528
     of 5 March 2004, was observed). *)
haftmann@21083
   529
end
haftmann@21083
   530
*}
nipkow@15524
   531
nipkow@15524
   532
haftmann@24422
   533
subsection {* Dense orders *}
haftmann@24422
   534
haftmann@24422
   535
class dense_linear_order = linorder + 
haftmann@24422
   536
  assumes gt_ex: "\<exists>y. x \<sqsubset> y" 
haftmann@24422
   537
  and lt_ex: "\<exists>y. y \<sqsubset> x"
haftmann@24422
   538
  and dense: "x \<sqsubset> y \<Longrightarrow> (\<exists>z. x \<sqsubset> z \<and> z \<sqsubset> y)"
haftmann@24422
   539
  (*see further theory Dense_Linear_Order*)
haftmann@24422
   540
ballarin@24641
   541
haftmann@24422
   542
lemma interval_empty_iff:
haftmann@24422
   543
  fixes x y z :: "'a\<Colon>dense_linear_order"
haftmann@24422
   544
  shows "{y. x < y \<and> y < z} = {} \<longleftrightarrow> \<not> x < z"
haftmann@24422
   545
  by (auto dest: dense)
haftmann@24422
   546
haftmann@24422
   547
subsection {* Name duplicates *}
haftmann@24422
   548
haftmann@24422
   549
lemmas order_less_le = less_le
haftmann@24422
   550
lemmas order_eq_refl = order_class.eq_refl
haftmann@24422
   551
lemmas order_less_irrefl = order_class.less_irrefl
haftmann@24422
   552
lemmas order_le_less = order_class.le_less
haftmann@24422
   553
lemmas order_le_imp_less_or_eq = order_class.le_imp_less_or_eq
haftmann@24422
   554
lemmas order_less_imp_le = order_class.less_imp_le
haftmann@24422
   555
lemmas order_less_imp_not_eq = order_class.less_imp_not_eq
haftmann@24422
   556
lemmas order_less_imp_not_eq2 = order_class.less_imp_not_eq2
haftmann@24422
   557
lemmas order_neq_le_trans = order_class.neq_le_trans
haftmann@24422
   558
lemmas order_le_neq_trans = order_class.le_neq_trans
haftmann@24422
   559
haftmann@24422
   560
lemmas order_antisym = antisym
haftmann@24422
   561
lemmas order_less_not_sym = order_class.less_not_sym
haftmann@24422
   562
lemmas order_less_asym = order_class.less_asym
haftmann@24422
   563
lemmas order_eq_iff = order_class.eq_iff
haftmann@24422
   564
lemmas order_antisym_conv = order_class.antisym_conv
haftmann@24422
   565
lemmas order_less_trans = order_class.less_trans
haftmann@24422
   566
lemmas order_le_less_trans = order_class.le_less_trans
haftmann@24422
   567
lemmas order_less_le_trans = order_class.less_le_trans
haftmann@24422
   568
lemmas order_less_imp_not_less = order_class.less_imp_not_less
haftmann@24422
   569
lemmas order_less_imp_triv = order_class.less_imp_triv
haftmann@24422
   570
lemmas order_less_asym' = order_class.less_asym'
haftmann@24422
   571
haftmann@24422
   572
lemmas linorder_linear = linear
haftmann@24422
   573
lemmas linorder_less_linear = linorder_class.less_linear
haftmann@24422
   574
lemmas linorder_le_less_linear = linorder_class.le_less_linear
haftmann@24422
   575
lemmas linorder_le_cases = linorder_class.le_cases
haftmann@24422
   576
lemmas linorder_not_less = linorder_class.not_less
haftmann@24422
   577
lemmas linorder_not_le = linorder_class.not_le
haftmann@24422
   578
lemmas linorder_neq_iff = linorder_class.neq_iff
haftmann@24422
   579
lemmas linorder_neqE = linorder_class.neqE
haftmann@24422
   580
lemmas linorder_antisym_conv1 = linorder_class.antisym_conv1
haftmann@24422
   581
lemmas linorder_antisym_conv2 = linorder_class.antisym_conv2
haftmann@24422
   582
lemmas linorder_antisym_conv3 = linorder_class.antisym_conv3
haftmann@24422
   583
haftmann@24422
   584
lemmas min_le_iff_disj = linorder_class.min_le_iff_disj
haftmann@24422
   585
lemmas le_max_iff_disj = linorder_class.le_max_iff_disj
haftmann@24422
   586
lemmas min_less_iff_disj = linorder_class.min_less_iff_disj
haftmann@24422
   587
lemmas less_max_iff_disj = linorder_class.less_max_iff_disj
haftmann@24422
   588
lemmas min_less_iff_conj [simp] = linorder_class.min_less_iff_conj
haftmann@24422
   589
lemmas max_less_iff_conj [simp] = linorder_class.max_less_iff_conj
haftmann@24422
   590
lemmas split_min = linorder_class.split_min
haftmann@24422
   591
lemmas split_max = linorder_class.split_max
haftmann@24422
   592
haftmann@24422
   593
haftmann@21083
   594
subsection {* Bounded quantifiers *}
haftmann@21083
   595
haftmann@21083
   596
syntax
wenzelm@21180
   597
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3ALL _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   598
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3EX _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   599
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   600
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   601
wenzelm@21180
   602
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3ALL _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   603
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3EX _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   604
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _>=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   605
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _>=_./ _)" [0, 0, 10] 10)
haftmann@21083
   606
haftmann@21083
   607
syntax (xsymbols)
wenzelm@21180
   608
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   609
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   610
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   611
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   612
wenzelm@21180
   613
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   614
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   615
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   616
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   617
haftmann@21083
   618
syntax (HOL)
wenzelm@21180
   619
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3! _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   620
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3? _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   621
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3! _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   622
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3? _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   623
haftmann@21083
   624
syntax (HTML output)
wenzelm@21180
   625
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   626
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   627
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   628
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   629
wenzelm@21180
   630
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   631
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   632
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   633
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   634
haftmann@21083
   635
translations
haftmann@21083
   636
  "ALL x<y. P"   =>  "ALL x. x < y \<longrightarrow> P"
haftmann@21083
   637
  "EX x<y. P"    =>  "EX x. x < y \<and> P"
haftmann@21083
   638
  "ALL x<=y. P"  =>  "ALL x. x <= y \<longrightarrow> P"
haftmann@21083
   639
  "EX x<=y. P"   =>  "EX x. x <= y \<and> P"
haftmann@21083
   640
  "ALL x>y. P"   =>  "ALL x. x > y \<longrightarrow> P"
haftmann@21083
   641
  "EX x>y. P"    =>  "EX x. x > y \<and> P"
haftmann@21083
   642
  "ALL x>=y. P"  =>  "ALL x. x >= y \<longrightarrow> P"
haftmann@21083
   643
  "EX x>=y. P"   =>  "EX x. x >= y \<and> P"
haftmann@21083
   644
haftmann@21083
   645
print_translation {*
haftmann@21083
   646
let
haftmann@22916
   647
  val All_binder = Syntax.binder_name @{const_syntax All};
haftmann@22916
   648
  val Ex_binder = Syntax.binder_name @{const_syntax Ex};
wenzelm@22377
   649
  val impl = @{const_syntax "op -->"};
wenzelm@22377
   650
  val conj = @{const_syntax "op &"};
haftmann@22916
   651
  val less = @{const_syntax less};
haftmann@22916
   652
  val less_eq = @{const_syntax less_eq};
wenzelm@21180
   653
wenzelm@21180
   654
  val trans =
wenzelm@21524
   655
   [((All_binder, impl, less), ("_All_less", "_All_greater")),
wenzelm@21524
   656
    ((All_binder, impl, less_eq), ("_All_less_eq", "_All_greater_eq")),
wenzelm@21524
   657
    ((Ex_binder, conj, less), ("_Ex_less", "_Ex_greater")),
wenzelm@21524
   658
    ((Ex_binder, conj, less_eq), ("_Ex_less_eq", "_Ex_greater_eq"))];
wenzelm@21180
   659
krauss@22344
   660
  fun matches_bound v t = 
krauss@22344
   661
     case t of (Const ("_bound", _) $ Free (v', _)) => (v = v')
krauss@22344
   662
              | _ => false
krauss@22344
   663
  fun contains_var v = Term.exists_subterm (fn Free (x, _) => x = v | _ => false)
krauss@22344
   664
  fun mk v c n P = Syntax.const c $ Syntax.mark_bound v $ n $ P
wenzelm@21180
   665
wenzelm@21180
   666
  fun tr' q = (q,
wenzelm@21180
   667
    fn [Const ("_bound", _) $ Free (v, _), Const (c, _) $ (Const (d, _) $ t $ u) $ P] =>
wenzelm@21180
   668
      (case AList.lookup (op =) trans (q, c, d) of
wenzelm@21180
   669
        NONE => raise Match
wenzelm@21180
   670
      | SOME (l, g) =>
krauss@22344
   671
          if matches_bound v t andalso not (contains_var v u) then mk v l u P
krauss@22344
   672
          else if matches_bound v u andalso not (contains_var v t) then mk v g t P
krauss@22344
   673
          else raise Match)
wenzelm@21180
   674
     | _ => raise Match);
wenzelm@21524
   675
in [tr' All_binder, tr' Ex_binder] end
haftmann@21083
   676
*}
haftmann@21083
   677
haftmann@21083
   678
haftmann@21383
   679
subsection {* Transitivity reasoning *}
haftmann@21383
   680
haftmann@21383
   681
lemma ord_le_eq_trans: "a <= b ==> b = c ==> a <= c"
nipkow@23212
   682
by (rule subst)
haftmann@21383
   683
haftmann@21383
   684
lemma ord_eq_le_trans: "a = b ==> b <= c ==> a <= c"
nipkow@23212
   685
by (rule ssubst)
haftmann@21383
   686
haftmann@21383
   687
lemma ord_less_eq_trans: "a < b ==> b = c ==> a < c"
nipkow@23212
   688
by (rule subst)
haftmann@21383
   689
haftmann@21383
   690
lemma ord_eq_less_trans: "a = b ==> b < c ==> a < c"
nipkow@23212
   691
by (rule ssubst)
haftmann@21383
   692
haftmann@21383
   693
lemma order_less_subst2: "(a::'a::order) < b ==> f b < (c::'c::order) ==>
haftmann@21383
   694
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   695
proof -
haftmann@21383
   696
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   697
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   698
  also assume "f b < c"
haftmann@21383
   699
  finally (order_less_trans) show ?thesis .
haftmann@21383
   700
qed
haftmann@21383
   701
haftmann@21383
   702
lemma order_less_subst1: "(a::'a::order) < f b ==> (b::'b::order) < c ==>
haftmann@21383
   703
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   704
proof -
haftmann@21383
   705
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   706
  assume "a < f b"
haftmann@21383
   707
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@21383
   708
  finally (order_less_trans) show ?thesis .
haftmann@21383
   709
qed
haftmann@21383
   710
haftmann@21383
   711
lemma order_le_less_subst2: "(a::'a::order) <= b ==> f b < (c::'c::order) ==>
haftmann@21383
   712
  (!!x y. x <= y ==> f x <= f y) ==> f a < c"
haftmann@21383
   713
proof -
haftmann@21383
   714
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   715
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   716
  also assume "f b < c"
haftmann@21383
   717
  finally (order_le_less_trans) show ?thesis .
haftmann@21383
   718
qed
haftmann@21383
   719
haftmann@21383
   720
lemma order_le_less_subst1: "(a::'a::order) <= f b ==> (b::'b::order) < c ==>
haftmann@21383
   721
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   722
proof -
haftmann@21383
   723
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   724
  assume "a <= f b"
haftmann@21383
   725
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@21383
   726
  finally (order_le_less_trans) show ?thesis .
haftmann@21383
   727
qed
haftmann@21383
   728
haftmann@21383
   729
lemma order_less_le_subst2: "(a::'a::order) < b ==> f b <= (c::'c::order) ==>
haftmann@21383
   730
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   731
proof -
haftmann@21383
   732
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   733
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   734
  also assume "f b <= c"
haftmann@21383
   735
  finally (order_less_le_trans) show ?thesis .
haftmann@21383
   736
qed
haftmann@21383
   737
haftmann@21383
   738
lemma order_less_le_subst1: "(a::'a::order) < f b ==> (b::'b::order) <= c ==>
haftmann@21383
   739
  (!!x y. x <= y ==> f x <= f y) ==> a < f c"
haftmann@21383
   740
proof -
haftmann@21383
   741
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   742
  assume "a < f b"
haftmann@21383
   743
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   744
  finally (order_less_le_trans) show ?thesis .
haftmann@21383
   745
qed
haftmann@21383
   746
haftmann@21383
   747
lemma order_subst1: "(a::'a::order) <= f b ==> (b::'b::order) <= c ==>
haftmann@21383
   748
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   749
proof -
haftmann@21383
   750
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   751
  assume "a <= f b"
haftmann@21383
   752
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   753
  finally (order_trans) show ?thesis .
haftmann@21383
   754
qed
haftmann@21383
   755
haftmann@21383
   756
lemma order_subst2: "(a::'a::order) <= b ==> f b <= (c::'c::order) ==>
haftmann@21383
   757
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   758
proof -
haftmann@21383
   759
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   760
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   761
  also assume "f b <= c"
haftmann@21383
   762
  finally (order_trans) show ?thesis .
haftmann@21383
   763
qed
haftmann@21383
   764
haftmann@21383
   765
lemma ord_le_eq_subst: "a <= b ==> f b = c ==>
haftmann@21383
   766
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   767
proof -
haftmann@21383
   768
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   769
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   770
  also assume "f b = c"
haftmann@21383
   771
  finally (ord_le_eq_trans) show ?thesis .
haftmann@21383
   772
qed
haftmann@21383
   773
haftmann@21383
   774
lemma ord_eq_le_subst: "a = f b ==> b <= c ==>
haftmann@21383
   775
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   776
proof -
haftmann@21383
   777
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   778
  assume "a = f b"
haftmann@21383
   779
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   780
  finally (ord_eq_le_trans) show ?thesis .
haftmann@21383
   781
qed
haftmann@21383
   782
haftmann@21383
   783
lemma ord_less_eq_subst: "a < b ==> f b = c ==>
haftmann@21383
   784
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   785
proof -
haftmann@21383
   786
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   787
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   788
  also assume "f b = c"
haftmann@21383
   789
  finally (ord_less_eq_trans) show ?thesis .
haftmann@21383
   790
qed
haftmann@21383
   791
haftmann@21383
   792
lemma ord_eq_less_subst: "a = f b ==> b < c ==>
haftmann@21383
   793
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   794
proof -
haftmann@21383
   795
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   796
  assume "a = f b"
haftmann@21383
   797
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@21383
   798
  finally (ord_eq_less_trans) show ?thesis .
haftmann@21383
   799
qed
haftmann@21383
   800
haftmann@21383
   801
text {*
haftmann@21383
   802
  Note that this list of rules is in reverse order of priorities.
haftmann@21383
   803
*}
haftmann@21383
   804
haftmann@21383
   805
lemmas order_trans_rules [trans] =
haftmann@21383
   806
  order_less_subst2
haftmann@21383
   807
  order_less_subst1
haftmann@21383
   808
  order_le_less_subst2
haftmann@21383
   809
  order_le_less_subst1
haftmann@21383
   810
  order_less_le_subst2
haftmann@21383
   811
  order_less_le_subst1
haftmann@21383
   812
  order_subst2
haftmann@21383
   813
  order_subst1
haftmann@21383
   814
  ord_le_eq_subst
haftmann@21383
   815
  ord_eq_le_subst
haftmann@21383
   816
  ord_less_eq_subst
haftmann@21383
   817
  ord_eq_less_subst
haftmann@21383
   818
  forw_subst
haftmann@21383
   819
  back_subst
haftmann@21383
   820
  rev_mp
haftmann@21383
   821
  mp
haftmann@21383
   822
  order_neq_le_trans
haftmann@21383
   823
  order_le_neq_trans
haftmann@21383
   824
  order_less_trans
haftmann@21383
   825
  order_less_asym'
haftmann@21383
   826
  order_le_less_trans
haftmann@21383
   827
  order_less_le_trans
haftmann@21383
   828
  order_trans
haftmann@21383
   829
  order_antisym
haftmann@21383
   830
  ord_le_eq_trans
haftmann@21383
   831
  ord_eq_le_trans
haftmann@21383
   832
  ord_less_eq_trans
haftmann@21383
   833
  ord_eq_less_trans
haftmann@21383
   834
  trans
haftmann@21383
   835
haftmann@21083
   836
wenzelm@21180
   837
(* FIXME cleanup *)
wenzelm@21180
   838
haftmann@21083
   839
text {* These support proving chains of decreasing inequalities
haftmann@21083
   840
    a >= b >= c ... in Isar proofs. *}
haftmann@21083
   841
haftmann@21083
   842
lemma xt1:
haftmann@21083
   843
  "a = b ==> b > c ==> a > c"
haftmann@21083
   844
  "a > b ==> b = c ==> a > c"
haftmann@21083
   845
  "a = b ==> b >= c ==> a >= c"
haftmann@21083
   846
  "a >= b ==> b = c ==> a >= c"
haftmann@21083
   847
  "(x::'a::order) >= y ==> y >= x ==> x = y"
haftmann@21083
   848
  "(x::'a::order) >= y ==> y >= z ==> x >= z"
haftmann@21083
   849
  "(x::'a::order) > y ==> y >= z ==> x > z"
haftmann@21083
   850
  "(x::'a::order) >= y ==> y > z ==> x > z"
wenzelm@23417
   851
  "(a::'a::order) > b ==> b > a ==> P"
haftmann@21083
   852
  "(x::'a::order) > y ==> y > z ==> x > z"
haftmann@21083
   853
  "(a::'a::order) >= b ==> a ~= b ==> a > b"
haftmann@21083
   854
  "(a::'a::order) ~= b ==> a >= b ==> a > b"
haftmann@21083
   855
  "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c" 
haftmann@21083
   856
  "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   857
  "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
   858
  "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@21083
   859
by auto
haftmann@21083
   860
haftmann@21083
   861
lemma xt2:
haftmann@21083
   862
  "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
   863
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
   864
haftmann@21083
   865
lemma xt3: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==> 
haftmann@21083
   866
    (!!x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@21083
   867
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
   868
haftmann@21083
   869
lemma xt4: "(a::'a::order) > f b ==> (b::'b::order) >= c ==>
haftmann@21083
   870
  (!!x y. x >= y ==> f x >= f y) ==> a > f c"
haftmann@21083
   871
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
   872
haftmann@21083
   873
lemma xt5: "(a::'a::order) > b ==> (f b::'b::order) >= c==>
haftmann@21083
   874
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   875
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
   876
haftmann@21083
   877
lemma xt6: "(a::'a::order) >= f b ==> b > c ==>
haftmann@21083
   878
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   879
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
   880
haftmann@21083
   881
lemma xt7: "(a::'a::order) >= b ==> (f b::'b::order) > c ==>
haftmann@21083
   882
    (!!x y. x >= y ==> f x >= f y) ==> f a > c"
haftmann@21083
   883
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
   884
haftmann@21083
   885
lemma xt8: "(a::'a::order) > f b ==> (b::'b::order) > c ==>
haftmann@21083
   886
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   887
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
   888
haftmann@21083
   889
lemma xt9: "(a::'a::order) > b ==> (f b::'b::order) > c ==>
haftmann@21083
   890
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   891
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
   892
haftmann@21083
   893
lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9
haftmann@21083
   894
haftmann@21083
   895
(* 
haftmann@21083
   896
  Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands
haftmann@21083
   897
  for the wrong thing in an Isar proof.
haftmann@21083
   898
haftmann@21083
   899
  The extra transitivity rules can be used as follows: 
haftmann@21083
   900
haftmann@21083
   901
lemma "(a::'a::order) > z"
haftmann@21083
   902
proof -
haftmann@21083
   903
  have "a >= b" (is "_ >= ?rhs")
haftmann@21083
   904
    sorry
haftmann@21083
   905
  also have "?rhs >= c" (is "_ >= ?rhs")
haftmann@21083
   906
    sorry
haftmann@21083
   907
  also (xtrans) have "?rhs = d" (is "_ = ?rhs")
haftmann@21083
   908
    sorry
haftmann@21083
   909
  also (xtrans) have "?rhs >= e" (is "_ >= ?rhs")
haftmann@21083
   910
    sorry
haftmann@21083
   911
  also (xtrans) have "?rhs > f" (is "_ > ?rhs")
haftmann@21083
   912
    sorry
haftmann@21083
   913
  also (xtrans) have "?rhs > z"
haftmann@21083
   914
    sorry
haftmann@21083
   915
  finally (xtrans) show ?thesis .
haftmann@21083
   916
qed
haftmann@21083
   917
haftmann@21083
   918
  Alternatively, one can use "declare xtrans [trans]" and then
haftmann@21083
   919
  leave out the "(xtrans)" above.
haftmann@21083
   920
*)
haftmann@21083
   921
haftmann@21546
   922
subsection {* Order on bool *}
haftmann@21546
   923
haftmann@22886
   924
instance bool :: order 
haftmann@21546
   925
  le_bool_def: "P \<le> Q \<equiv> P \<longrightarrow> Q"
haftmann@21546
   926
  less_bool_def: "P < Q \<equiv> P \<le> Q \<and> P \<noteq> Q"
haftmann@22916
   927
  by intro_classes (auto simp add: le_bool_def less_bool_def)
haftmann@24422
   928
lemmas [code func del] = le_bool_def less_bool_def
haftmann@21546
   929
haftmann@21546
   930
lemma le_boolI: "(P \<Longrightarrow> Q) \<Longrightarrow> P \<le> Q"
nipkow@23212
   931
by (simp add: le_bool_def)
haftmann@21546
   932
haftmann@21546
   933
lemma le_boolI': "P \<longrightarrow> Q \<Longrightarrow> P \<le> Q"
nipkow@23212
   934
by (simp add: le_bool_def)
haftmann@21546
   935
haftmann@21546
   936
lemma le_boolE: "P \<le> Q \<Longrightarrow> P \<Longrightarrow> (Q \<Longrightarrow> R) \<Longrightarrow> R"
nipkow@23212
   937
by (simp add: le_bool_def)
haftmann@21546
   938
haftmann@21546
   939
lemma le_boolD: "P \<le> Q \<Longrightarrow> P \<longrightarrow> Q"
nipkow@23212
   940
by (simp add: le_bool_def)
haftmann@21546
   941
haftmann@22348
   942
lemma [code func]:
haftmann@22348
   943
  "False \<le> b \<longleftrightarrow> True"
haftmann@22348
   944
  "True \<le> b \<longleftrightarrow> b"
haftmann@22348
   945
  "False < b \<longleftrightarrow> b"
haftmann@22348
   946
  "True < b \<longleftrightarrow> False"
haftmann@22348
   947
  unfolding le_bool_def less_bool_def by simp_all
haftmann@22348
   948
haftmann@22424
   949
haftmann@23881
   950
subsection {* Order on sets *}
haftmann@23881
   951
haftmann@23881
   952
instance set :: (type) order
haftmann@23881
   953
  by (intro_classes,
haftmann@23881
   954
      (assumption | rule subset_refl subset_trans subset_antisym psubset_eq)+)
haftmann@23881
   955
haftmann@23881
   956
lemmas basic_trans_rules [trans] =
haftmann@23881
   957
  order_trans_rules set_rev_mp set_mp
haftmann@23881
   958
haftmann@23881
   959
haftmann@23881
   960
subsection {* Order on functions *}
haftmann@23881
   961
haftmann@23881
   962
instance "fun" :: (type, ord) ord
haftmann@23881
   963
  le_fun_def: "f \<le> g \<equiv> \<forall>x. f x \<le> g x"
haftmann@23881
   964
  less_fun_def: "f < g \<equiv> f \<le> g \<and> f \<noteq> g" ..
haftmann@23881
   965
haftmann@23881
   966
lemmas [code func del] = le_fun_def less_fun_def
haftmann@23881
   967
haftmann@23881
   968
instance "fun" :: (type, order) order
haftmann@23881
   969
  by default
haftmann@23881
   970
    (auto simp add: le_fun_def less_fun_def expand_fun_eq
haftmann@23881
   971
       intro: order_trans order_antisym)
haftmann@23881
   972
haftmann@23881
   973
lemma le_funI: "(\<And>x. f x \<le> g x) \<Longrightarrow> f \<le> g"
haftmann@23881
   974
  unfolding le_fun_def by simp
haftmann@23881
   975
haftmann@23881
   976
lemma le_funE: "f \<le> g \<Longrightarrow> (f x \<le> g x \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@23881
   977
  unfolding le_fun_def by simp
haftmann@23881
   978
haftmann@23881
   979
lemma le_funD: "f \<le> g \<Longrightarrow> f x \<le> g x"
haftmann@23881
   980
  unfolding le_fun_def by simp
haftmann@23881
   981
haftmann@23881
   982
text {*
haftmann@23881
   983
  Handy introduction and elimination rules for @{text "\<le>"}
haftmann@23881
   984
  on unary and binary predicates
haftmann@23881
   985
*}
haftmann@23881
   986
haftmann@23881
   987
lemma predicate1I [Pure.intro!, intro!]:
haftmann@23881
   988
  assumes PQ: "\<And>x. P x \<Longrightarrow> Q x"
haftmann@23881
   989
  shows "P \<le> Q"
haftmann@23881
   990
  apply (rule le_funI)
haftmann@23881
   991
  apply (rule le_boolI)
haftmann@23881
   992
  apply (rule PQ)
haftmann@23881
   993
  apply assumption
haftmann@23881
   994
  done
haftmann@23881
   995
haftmann@23881
   996
lemma predicate1D [Pure.dest, dest]: "P \<le> Q \<Longrightarrow> P x \<Longrightarrow> Q x"
haftmann@23881
   997
  apply (erule le_funE)
haftmann@23881
   998
  apply (erule le_boolE)
haftmann@23881
   999
  apply assumption+
haftmann@23881
  1000
  done
haftmann@23881
  1001
haftmann@23881
  1002
lemma predicate2I [Pure.intro!, intro!]:
haftmann@23881
  1003
  assumes PQ: "\<And>x y. P x y \<Longrightarrow> Q x y"
haftmann@23881
  1004
  shows "P \<le> Q"
haftmann@23881
  1005
  apply (rule le_funI)+
haftmann@23881
  1006
  apply (rule le_boolI)
haftmann@23881
  1007
  apply (rule PQ)
haftmann@23881
  1008
  apply assumption
haftmann@23881
  1009
  done
haftmann@23881
  1010
haftmann@23881
  1011
lemma predicate2D [Pure.dest, dest]: "P \<le> Q \<Longrightarrow> P x y \<Longrightarrow> Q x y"
haftmann@23881
  1012
  apply (erule le_funE)+
haftmann@23881
  1013
  apply (erule le_boolE)
haftmann@23881
  1014
  apply assumption+
haftmann@23881
  1015
  done
haftmann@23881
  1016
haftmann@23881
  1017
lemma rev_predicate1D: "P x ==> P <= Q ==> Q x"
haftmann@23881
  1018
  by (rule predicate1D)
haftmann@23881
  1019
haftmann@23881
  1020
lemma rev_predicate2D: "P x y ==> P <= Q ==> Q x y"
haftmann@23881
  1021
  by (rule predicate2D)
haftmann@23881
  1022
haftmann@23881
  1023
haftmann@23881
  1024
subsection {* Monotonicity, least value operator and min/max *}
haftmann@21083
  1025
haftmann@21216
  1026
locale mono =
haftmann@21216
  1027
  fixes f
haftmann@21216
  1028
  assumes mono: "A \<le> B \<Longrightarrow> f A \<le> f B"
haftmann@21216
  1029
haftmann@21216
  1030
lemmas monoI [intro?] = mono.intro
haftmann@21216
  1031
  and monoD [dest?] = mono.mono
haftmann@21083
  1032
haftmann@21383
  1033
lemma LeastI2_order:
haftmann@21383
  1034
  "[| P (x::'a::order);
haftmann@21383
  1035
      !!y. P y ==> x <= y;
haftmann@21383
  1036
      !!x. [| P x; ALL y. P y --> x \<le> y |] ==> Q x |]
haftmann@21383
  1037
   ==> Q (Least P)"
nipkow@23212
  1038
apply (unfold Least_def)
nipkow@23212
  1039
apply (rule theI2)
nipkow@23212
  1040
  apply (blast intro: order_antisym)+
nipkow@23212
  1041
done
haftmann@21383
  1042
haftmann@23881
  1043
lemma Least_mono:
haftmann@23881
  1044
  "mono (f::'a::order => 'b::order) ==> EX x:S. ALL y:S. x <= y
haftmann@23881
  1045
    ==> (LEAST y. y : f ` S) = f (LEAST x. x : S)"
haftmann@23881
  1046
    -- {* Courtesy of Stephan Merz *}
haftmann@23881
  1047
  apply clarify
haftmann@23881
  1048
  apply (erule_tac P = "%x. x : S" in LeastI2_order, fast)
haftmann@23881
  1049
  apply (rule LeastI2_order)
haftmann@23881
  1050
  apply (auto elim: monoD intro!: order_antisym)
haftmann@23881
  1051
  done
haftmann@23881
  1052
haftmann@21383
  1053
lemma Least_equality:
nipkow@23212
  1054
  "[| P (k::'a::order); !!x. P x ==> k <= x |] ==> (LEAST x. P x) = k"
nipkow@23212
  1055
apply (simp add: Least_def)
nipkow@23212
  1056
apply (rule the_equality)
nipkow@23212
  1057
apply (auto intro!: order_antisym)
nipkow@23212
  1058
done
haftmann@21383
  1059
haftmann@21383
  1060
lemma min_leastL: "(!!x. least <= x) ==> min least x = least"
nipkow@23212
  1061
by (simp add: min_def)
haftmann@21383
  1062
haftmann@21383
  1063
lemma max_leastL: "(!!x. least <= x) ==> max least x = x"
nipkow@23212
  1064
by (simp add: max_def)
haftmann@21383
  1065
haftmann@21383
  1066
lemma min_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> min x least = least"
nipkow@23212
  1067
apply (simp add: min_def)
nipkow@23212
  1068
apply (blast intro: order_antisym)
nipkow@23212
  1069
done
haftmann@21383
  1070
haftmann@21383
  1071
lemma max_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> max x least = x"
nipkow@23212
  1072
apply (simp add: max_def)
nipkow@23212
  1073
apply (blast intro: order_antisym)
nipkow@23212
  1074
done
haftmann@21383
  1075
haftmann@21383
  1076
lemma min_of_mono:
nipkow@23212
  1077
  "(!!x y. (f x <= f y) = (x <= y)) ==> min (f m) (f n) = f (min m n)"
nipkow@23212
  1078
by (simp add: min_def)
haftmann@21383
  1079
haftmann@21383
  1080
lemma max_of_mono:
nipkow@23212
  1081
  "(!!x y. (f x <= f y) = (x <= y)) ==> max (f m) (f n) = f (max m n)"
nipkow@23212
  1082
by (simp add: max_def)
haftmann@21383
  1083
haftmann@22548
  1084
haftmann@22548
  1085
subsection {* legacy ML bindings *}
wenzelm@21673
  1086
wenzelm@21673
  1087
ML {*
haftmann@22548
  1088
val monoI = @{thm monoI};
haftmann@22886
  1089
*}
wenzelm@21673
  1090
nipkow@15524
  1091
end