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