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