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