src/HOL/Orderings.thy
author haftmann
Thu Nov 29 17:08:26 2007 +0100 (2007-11-29)
changeset 25502 9200b36280c0
parent 25377 dcde128c84a2
child 25510 38c15efe603b
permissions -rw-r--r--
instance command as rudimentary class target
     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 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 lemma less_imp_neq: "x < y \<Longrightarrow> x \<noteq> y"
    43 by (erule contrapos_pn, erule subst, rule less_irrefl)
    44 
    45 
    46 text {* Useful for simplification, but too risky to include by default. *}
    47 
    48 lemma less_imp_not_eq: "x < y \<Longrightarrow> (x = y) \<longleftrightarrow> False"
    49 by auto
    50 
    51 lemma less_imp_not_eq2: "x < y \<Longrightarrow> (y = x) \<longleftrightarrow> False"
    52 by auto
    53 
    54 
    55 text {* Transitivity rules for calculational reasoning *}
    56 
    57 lemma neq_le_trans: "a \<noteq> b \<Longrightarrow> a \<le> b \<Longrightarrow> a < b"
    58 by (simp add: less_le)
    59 
    60 lemma le_neq_trans: "a \<le> b \<Longrightarrow> a \<noteq> b \<Longrightarrow> a < b"
    61 by (simp add: less_le)
    62 
    63 
    64 text {* Asymmetry. *}
    65 
    66 lemma less_not_sym: "x < y \<Longrightarrow> \<not> (y < x)"
    67 by (simp add: less_le antisym)
    68 
    69 lemma less_asym: "x < y \<Longrightarrow> (\<not> P \<Longrightarrow> y < x) \<Longrightarrow> P"
    70 by (drule less_not_sym, erule contrapos_np) simp
    71 
    72 lemma eq_iff: "x = y \<longleftrightarrow> x \<le> y \<and> y \<le> x"
    73 by (blast intro: antisym)
    74 
    75 lemma antisym_conv: "y \<le> x \<Longrightarrow> x \<le> y \<longleftrightarrow> x = y"
    76 by (blast intro: antisym)
    77 
    78 lemma less_imp_neq: "x < y \<Longrightarrow> x \<noteq> y"
    79 by (erule contrapos_pn, erule subst, rule less_irrefl)
    80 
    81 
    82 text {* Transitivity. *}
    83 
    84 lemma less_trans: "x < y \<Longrightarrow> y < z \<Longrightarrow> x < z"
    85 by (simp add: less_le) (blast intro: order_trans antisym)
    86 
    87 lemma le_less_trans: "x \<le> y \<Longrightarrow> y < z \<Longrightarrow> x < z"
    88 by (simp add: less_le) (blast intro: order_trans antisym)
    89 
    90 lemma less_le_trans: "x < y \<Longrightarrow> y \<le> z \<Longrightarrow> x < z"
    91 by (simp add: less_le) (blast intro: order_trans antisym)
    92 
    93 
    94 text {* Useful for simplification, but too risky to include by default. *}
    95 
    96 lemma less_imp_not_less: "x < y \<Longrightarrow> (\<not> y < x) \<longleftrightarrow> True"
    97 by (blast elim: less_asym)
    98 
    99 lemma less_imp_triv: "x < y \<Longrightarrow> (y < x \<longrightarrow> P) \<longleftrightarrow> True"
   100 by (blast elim: less_asym)
   101 
   102 
   103 text {* Transitivity rules for calculational reasoning *}
   104 
   105 lemma less_asym': "a < b \<Longrightarrow> b < a \<Longrightarrow> P"
   106 by (rule less_asym)
   107 
   108 
   109 text {* Reverse order *}
   110 
   111 lemma order_reverse:
   112   "order (op \<ge>) (op >)"
   113 by unfold_locales
   114    (simp add: less_le, auto intro: antisym order_trans)
   115 
   116 end
   117 
   118 
   119 subsection {* Linear (total) orders *}
   120 
   121 class linorder = order +
   122   assumes linear: "x \<le> y \<or> y \<le> x"
   123 begin
   124 
   125 lemma less_linear: "x < y \<or> x = y \<or> y < x"
   126 unfolding less_le using less_le linear by blast
   127 
   128 lemma le_less_linear: "x \<le> y \<or> y < x"
   129 by (simp add: le_less less_linear)
   130 
   131 lemma le_cases [case_names le ge]:
   132   "(x \<le> y \<Longrightarrow> P) \<Longrightarrow> (y \<le> x \<Longrightarrow> P) \<Longrightarrow> P"
   133 using linear by blast
   134 
   135 lemma linorder_cases [case_names less equal greater]:
   136   "(x < y \<Longrightarrow> P) \<Longrightarrow> (x = y \<Longrightarrow> P) \<Longrightarrow> (y < x \<Longrightarrow> P) \<Longrightarrow> P"
   137 using less_linear by blast
   138 
   139 lemma not_less: "\<not> x < y \<longleftrightarrow> y \<le> x"
   140 apply (simp add: less_le)
   141 using linear apply (blast intro: antisym)
   142 done
   143 
   144 lemma not_less_iff_gr_or_eq:
   145  "\<not>(x < y) \<longleftrightarrow> (x > y | x = y)"
   146 apply(simp add:not_less le_less)
   147 apply blast
   148 done
   149 
   150 lemma not_le: "\<not> x \<le> y \<longleftrightarrow> y < x"
   151 apply (simp add: less_le)
   152 using linear apply (blast intro: antisym)
   153 done
   154 
   155 lemma neq_iff: "x \<noteq> y \<longleftrightarrow> x < y \<or> y < x"
   156 by (cut_tac x = x and y = y in less_linear, auto)
   157 
   158 lemma neqE: "x \<noteq> y \<Longrightarrow> (x < y \<Longrightarrow> R) \<Longrightarrow> (y < x \<Longrightarrow> R) \<Longrightarrow> R"
   159 by (simp add: neq_iff) blast
   160 
   161 lemma antisym_conv1: "\<not> x < y \<Longrightarrow> x \<le> y \<longleftrightarrow> x = y"
   162 by (blast intro: antisym dest: not_less [THEN iffD1])
   163 
   164 lemma antisym_conv2: "x \<le> y \<Longrightarrow> \<not> x < y \<longleftrightarrow> x = y"
   165 by (blast intro: antisym dest: not_less [THEN iffD1])
   166 
   167 lemma antisym_conv3: "\<not> y < x \<Longrightarrow> \<not> x < y \<longleftrightarrow> x = y"
   168 by (blast intro: antisym dest: not_less [THEN iffD1])
   169 
   170 text{*Replacing the old Nat.leI*}
   171 lemma leI: "\<not> x < y \<Longrightarrow> y \<le> x"
   172 unfolding not_less .
   173 
   174 lemma leD: "y \<le> x \<Longrightarrow> \<not> x < y"
   175 unfolding not_less .
   176 
   177 (*FIXME inappropriate name (or delete altogether)*)
   178 lemma not_leE: "\<not> y \<le> x \<Longrightarrow> x < y"
   179 unfolding not_le .
   180 
   181 
   182 text {* Reverse order *}
   183 
   184 lemma linorder_reverse:
   185   "linorder (op \<ge>) (op >)"
   186 by unfold_locales
   187   (simp add: less_le, auto intro: antisym order_trans simp add: linear)
   188 
   189 
   190 text {* min/max *}
   191 
   192 text {* for historic reasons, definitions are done in context ord *}
   193 
   194 definition (in ord)
   195   min :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
   196   [code unfold, code inline del]: "min a b = (if a \<le> b then a else b)"
   197 
   198 definition (in ord)
   199   max :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
   200   [code unfold, code inline del]: "max a b = (if a \<le> b then b else a)"
   201 
   202 lemma min_le_iff_disj:
   203   "min x y \<le> z \<longleftrightarrow> x \<le> z \<or> y \<le> z"
   204 unfolding min_def using linear by (auto intro: order_trans)
   205 
   206 lemma le_max_iff_disj:
   207   "z \<le> max x y \<longleftrightarrow> z \<le> x \<or> z \<le> y"
   208 unfolding max_def using linear by (auto intro: order_trans)
   209 
   210 lemma min_less_iff_disj:
   211   "min x y < z \<longleftrightarrow> x < z \<or> y < z"
   212 unfolding min_def le_less using less_linear by (auto intro: less_trans)
   213 
   214 lemma less_max_iff_disj:
   215   "z < max x y \<longleftrightarrow> z < x \<or> z < y"
   216 unfolding max_def le_less using less_linear by (auto intro: less_trans)
   217 
   218 lemma min_less_iff_conj [simp]:
   219   "z < min x y \<longleftrightarrow> z < x \<and> z < y"
   220 unfolding min_def le_less using less_linear by (auto intro: less_trans)
   221 
   222 lemma max_less_iff_conj [simp]:
   223   "max x y < z \<longleftrightarrow> x < z \<and> y < z"
   224 unfolding max_def le_less using less_linear by (auto intro: less_trans)
   225 
   226 lemma split_min [noatp]:
   227   "P (min i j) \<longleftrightarrow> (i \<le> j \<longrightarrow> P i) \<and> (\<not> i \<le> j \<longrightarrow> P j)"
   228 by (simp add: min_def)
   229 
   230 lemma split_max [noatp]:
   231   "P (max i j) \<longleftrightarrow> (i \<le> j \<longrightarrow> P j) \<and> (\<not> i \<le> j \<longrightarrow> P i)"
   232 by (simp add: max_def)
   233 
   234 end
   235 
   236 
   237 subsection {* Reasoning tools setup *}
   238 
   239 ML {*
   240 
   241 signature ORDERS =
   242 sig
   243   val print_structures: Proof.context -> unit
   244   val setup: theory -> theory
   245   val order_tac: thm list -> Proof.context -> int -> tactic
   246 end;
   247 
   248 structure Orders: ORDERS =
   249 struct
   250 
   251 (** Theory and context data **)
   252 
   253 fun struct_eq ((s1: string, ts1), (s2, ts2)) =
   254   (s1 = s2) andalso eq_list (op aconv) (ts1, ts2);
   255 
   256 structure Data = GenericDataFun
   257 (
   258   type T = ((string * term list) * Order_Tac.less_arith) list;
   259     (* Order structures:
   260        identifier of the structure, list of operations and record of theorems
   261        needed to set up the transitivity reasoner,
   262        identifier and operations identify the structure uniquely. *)
   263   val empty = [];
   264   val extend = I;
   265   fun merge _ = AList.join struct_eq (K fst);
   266 );
   267 
   268 fun print_structures ctxt =
   269   let
   270     val structs = Data.get (Context.Proof ctxt);
   271     fun pretty_term t = Pretty.block
   272       [Pretty.quote (Syntax.pretty_term ctxt t), Pretty.brk 1,
   273         Pretty.str "::", Pretty.brk 1,
   274         Pretty.quote (Syntax.pretty_typ ctxt (type_of t))];
   275     fun pretty_struct ((s, ts), _) = Pretty.block
   276       [Pretty.str s, Pretty.str ":", Pretty.brk 1,
   277        Pretty.enclose "(" ")" (Pretty.breaks (map pretty_term ts))];
   278   in
   279     Pretty.writeln (Pretty.big_list "Order structures:" (map pretty_struct structs))
   280   end;
   281 
   282 
   283 (** Method **)
   284 
   285 fun struct_tac ((s, [eq, le, less]), thms) prems =
   286   let
   287     fun decomp thy (Trueprop $ t) =
   288       let
   289         fun excluded t =
   290           (* exclude numeric types: linear arithmetic subsumes transitivity *)
   291           let val T = type_of t
   292           in
   293 	    T = HOLogic.natT orelse T = HOLogic.intT orelse T = HOLogic.realT
   294           end;
   295 	fun rel (bin_op $ t1 $ t2) =
   296               if excluded t1 then NONE
   297               else if Pattern.matches thy (eq, bin_op) then SOME (t1, "=", t2)
   298               else if Pattern.matches thy (le, bin_op) then SOME (t1, "<=", t2)
   299               else if Pattern.matches thy (less, bin_op) then SOME (t1, "<", t2)
   300               else NONE
   301 	  | rel _ = NONE;
   302 	fun dec (Const (@{const_name Not}, _) $ t) = (case rel t
   303 	      of NONE => NONE
   304 	       | SOME (t1, rel, t2) => SOME (t1, "~" ^ rel, t2))
   305           | dec x = rel x;
   306       in dec t end;
   307   in
   308     case s of
   309       "order" => Order_Tac.partial_tac decomp thms prems
   310     | "linorder" => Order_Tac.linear_tac decomp thms prems
   311     | _ => error ("Unknown kind of order `" ^ s ^ "' encountered in transitivity reasoner.")
   312   end
   313 
   314 fun order_tac prems ctxt =
   315   FIRST' (map (fn s => CHANGED o struct_tac s prems) (Data.get (Context.Proof ctxt)));
   316 
   317 
   318 (** Attribute **)
   319 
   320 fun add_struct_thm s tag =
   321   Thm.declaration_attribute
   322     (fn thm => Data.map (AList.map_default struct_eq (s, Order_Tac.empty TrueI) (Order_Tac.update tag thm)));
   323 fun del_struct s =
   324   Thm.declaration_attribute
   325     (fn _ => Data.map (AList.delete struct_eq s));
   326 
   327 val attribute = Attrib.syntax
   328      (Scan.lift ((Args.add -- Args.name >> (fn (_, s) => SOME s) ||
   329           Args.del >> K NONE) --| Args.colon (* FIXME ||
   330         Scan.succeed true *) ) -- Scan.lift Args.name --
   331       Scan.repeat Args.term
   332       >> (fn ((SOME tag, n), ts) => add_struct_thm (n, ts) tag
   333            | ((NONE, n), ts) => del_struct (n, ts)));
   334 
   335 
   336 (** Diagnostic command **)
   337 
   338 val print = Toplevel.unknown_context o
   339   Toplevel.keep (Toplevel.node_case
   340     (Context.cases (print_structures o ProofContext.init) print_structures)
   341     (print_structures o Proof.context_of));
   342 
   343 val _ =
   344   OuterSyntax.improper_command "print_orders"
   345     "print order structures available to transitivity reasoner" OuterKeyword.diag
   346     (Scan.succeed (Toplevel.no_timing o print));
   347 
   348 
   349 (** Setup **)
   350 
   351 val setup =
   352   Method.add_methods
   353     [("order", Method.ctxt_args (Method.SIMPLE_METHOD' o order_tac []), "transitivity reasoner")] #>
   354   Attrib.add_attributes [("order", attribute, "theorems controlling transitivity reasoner")];
   355 
   356 end;
   357 
   358 *}
   359 
   360 setup Orders.setup
   361 
   362 
   363 text {* Declarations to set up transitivity reasoner of partial and linear orders. *}
   364 
   365 context order
   366 begin
   367 
   368 (* The type constraint on @{term op =} below is necessary since the operation
   369    is not a parameter of the locale. *)
   370 
   371 lemmas
   372   [order add less_reflE: order "op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool" "op <=" "op <"] =
   373   less_irrefl [THEN notE]
   374 lemmas
   375   [order add le_refl: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   376   order_refl
   377 lemmas
   378   [order add less_imp_le: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   379   less_imp_le
   380 lemmas
   381   [order add eqI: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   382   antisym
   383 lemmas
   384   [order add eqD1: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   385   eq_refl
   386 lemmas
   387   [order add eqD2: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   388   sym [THEN eq_refl]
   389 lemmas
   390   [order add less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   391   less_trans
   392 lemmas
   393   [order add less_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   394   less_le_trans
   395 lemmas
   396   [order add le_less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   397   le_less_trans
   398 lemmas
   399   [order add le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   400   order_trans
   401 lemmas
   402   [order add le_neq_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   403   le_neq_trans
   404 lemmas
   405   [order add neq_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   406   neq_le_trans
   407 lemmas
   408   [order add less_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   409   less_imp_neq
   410 lemmas
   411   [order add eq_neq_eq_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   412    eq_neq_eq_imp_neq
   413 lemmas
   414   [order add not_sym: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   415   not_sym
   416 
   417 end
   418 
   419 context linorder
   420 begin
   421 
   422 lemmas
   423   [order del: order "op = :: 'a => 'a => bool" "op <=" "op <"] = _
   424 
   425 lemmas
   426   [order add less_reflE: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   427   less_irrefl [THEN notE]
   428 lemmas
   429   [order add le_refl: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   430   order_refl
   431 lemmas
   432   [order add less_imp_le: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   433   less_imp_le
   434 lemmas
   435   [order add not_lessI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   436   not_less [THEN iffD2]
   437 lemmas
   438   [order add not_leI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   439   not_le [THEN iffD2]
   440 lemmas
   441   [order add not_lessD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   442   not_less [THEN iffD1]
   443 lemmas
   444   [order add not_leD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   445   not_le [THEN iffD1]
   446 lemmas
   447   [order add eqI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   448   antisym
   449 lemmas
   450   [order add eqD1: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   451   eq_refl
   452 lemmas
   453   [order add eqD2: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   454   sym [THEN eq_refl]
   455 lemmas
   456   [order add less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   457   less_trans
   458 lemmas
   459   [order add less_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   460   less_le_trans
   461 lemmas
   462   [order add le_less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   463   le_less_trans
   464 lemmas
   465   [order add le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   466   order_trans
   467 lemmas
   468   [order add le_neq_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   469   le_neq_trans
   470 lemmas
   471   [order add neq_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   472   neq_le_trans
   473 lemmas
   474   [order add less_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   475   less_imp_neq
   476 lemmas
   477   [order add eq_neq_eq_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   478   eq_neq_eq_imp_neq
   479 lemmas
   480   [order add not_sym: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   481   not_sym
   482 
   483 end
   484 
   485 
   486 setup {*
   487 let
   488 
   489 fun prp t thm = (#prop (rep_thm thm) = t);
   490 
   491 fun prove_antisym_le sg ss ((le as Const(_,T)) $ r $ s) =
   492   let val prems = prems_of_ss ss;
   493       val less = Const (@{const_name less}, T);
   494       val t = HOLogic.mk_Trueprop(le $ s $ r);
   495   in case find_first (prp t) prems of
   496        NONE =>
   497          let val t = HOLogic.mk_Trueprop(HOLogic.Not $ (less $ r $ s))
   498          in case find_first (prp t) prems of
   499               NONE => NONE
   500             | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv1}))
   501          end
   502      | SOME thm => SOME(mk_meta_eq(thm RS @{thm order_class.antisym_conv}))
   503   end
   504   handle THM _ => NONE;
   505 
   506 fun prove_antisym_less sg ss (NotC $ ((less as Const(_,T)) $ r $ s)) =
   507   let val prems = prems_of_ss ss;
   508       val le = Const (@{const_name less_eq}, T);
   509       val t = HOLogic.mk_Trueprop(le $ r $ s);
   510   in case find_first (prp t) prems of
   511        NONE =>
   512          let val t = HOLogic.mk_Trueprop(NotC $ (less $ s $ r))
   513          in case find_first (prp t) prems of
   514               NONE => NONE
   515             | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv3}))
   516          end
   517      | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv2}))
   518   end
   519   handle THM _ => NONE;
   520 
   521 fun add_simprocs procs thy =
   522   (Simplifier.change_simpset_of thy (fn ss => ss
   523     addsimprocs (map (fn (name, raw_ts, proc) =>
   524       Simplifier.simproc thy name raw_ts proc)) procs); thy);
   525 fun add_solver name tac thy =
   526   (Simplifier.change_simpset_of thy (fn ss => ss addSolver
   527     (mk_solver' name (fn ss => tac (MetaSimplifier.prems_of_ss ss) (MetaSimplifier.the_context ss)))); thy);
   528 
   529 in
   530   add_simprocs [
   531        ("antisym le", ["(x::'a::order) <= y"], prove_antisym_le),
   532        ("antisym less", ["~ (x::'a::linorder) < y"], prove_antisym_less)
   533      ]
   534   #> add_solver "Transitivity" Orders.order_tac
   535   (* Adding the transitivity reasoners also as safe solvers showed a slight
   536      speed up, but the reasoning strength appears to be not higher (at least
   537      no breaking of additional proofs in the entire HOL distribution, as
   538      of 5 March 2004, was observed). *)
   539 end
   540 *}
   541 
   542 
   543 subsection {* Dense orders *}
   544 
   545 class dense_linear_order = linorder + 
   546   assumes gt_ex: "\<exists>y. x < y" 
   547   and lt_ex: "\<exists>y. y < x"
   548   and dense: "x < y \<Longrightarrow> (\<exists>z. x < z \<and> z < y)"
   549   (*see further theory Dense_Linear_Order*)
   550 begin
   551 
   552 lemma interval_empty_iff:
   553   "{y. x < y \<and> y < z} = {} \<longleftrightarrow> \<not> x < z"
   554   by (auto dest: dense)
   555 
   556 end
   557 
   558 subsection {* Name duplicates *}
   559 
   560 lemmas order_less_le = less_le
   561 lemmas order_eq_refl = order_class.eq_refl
   562 lemmas order_less_irrefl = order_class.less_irrefl
   563 lemmas order_le_less = order_class.le_less
   564 lemmas order_le_imp_less_or_eq = order_class.le_imp_less_or_eq
   565 lemmas order_less_imp_le = order_class.less_imp_le
   566 lemmas order_less_imp_not_eq = order_class.less_imp_not_eq
   567 lemmas order_less_imp_not_eq2 = order_class.less_imp_not_eq2
   568 lemmas order_neq_le_trans = order_class.neq_le_trans
   569 lemmas order_le_neq_trans = order_class.le_neq_trans
   570 
   571 lemmas order_antisym = antisym
   572 lemmas order_less_not_sym = order_class.less_not_sym
   573 lemmas order_less_asym = order_class.less_asym
   574 lemmas order_eq_iff = order_class.eq_iff
   575 lemmas order_antisym_conv = order_class.antisym_conv
   576 lemmas order_less_trans = order_class.less_trans
   577 lemmas order_le_less_trans = order_class.le_less_trans
   578 lemmas order_less_le_trans = order_class.less_le_trans
   579 lemmas order_less_imp_not_less = order_class.less_imp_not_less
   580 lemmas order_less_imp_triv = order_class.less_imp_triv
   581 lemmas order_less_asym' = order_class.less_asym'
   582 
   583 lemmas linorder_linear = linear
   584 lemmas linorder_less_linear = linorder_class.less_linear
   585 lemmas linorder_le_less_linear = linorder_class.le_less_linear
   586 lemmas linorder_le_cases = linorder_class.le_cases
   587 lemmas linorder_not_less = linorder_class.not_less
   588 lemmas linorder_not_le = linorder_class.not_le
   589 lemmas linorder_neq_iff = linorder_class.neq_iff
   590 lemmas linorder_neqE = linorder_class.neqE
   591 lemmas linorder_antisym_conv1 = linorder_class.antisym_conv1
   592 lemmas linorder_antisym_conv2 = linorder_class.antisym_conv2
   593 lemmas linorder_antisym_conv3 = linorder_class.antisym_conv3
   594 
   595 
   596 subsection {* Bounded quantifiers *}
   597 
   598 syntax
   599   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3ALL _<_./ _)"  [0, 0, 10] 10)
   600   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3EX _<_./ _)"  [0, 0, 10] 10)
   601   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _<=_./ _)" [0, 0, 10] 10)
   602   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _<=_./ _)" [0, 0, 10] 10)
   603 
   604   "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3ALL _>_./ _)"  [0, 0, 10] 10)
   605   "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3EX _>_./ _)"  [0, 0, 10] 10)
   606   "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _>=_./ _)" [0, 0, 10] 10)
   607   "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _>=_./ _)" [0, 0, 10] 10)
   608 
   609 syntax (xsymbols)
   610   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
   611   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
   612   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
   613   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
   614 
   615   "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
   616   "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
   617   "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
   618   "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
   619 
   620 syntax (HOL)
   621   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3! _<_./ _)"  [0, 0, 10] 10)
   622   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3? _<_./ _)"  [0, 0, 10] 10)
   623   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3! _<=_./ _)" [0, 0, 10] 10)
   624   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3? _<=_./ _)" [0, 0, 10] 10)
   625 
   626 syntax (HTML output)
   627   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
   628   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
   629   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
   630   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
   631 
   632   "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
   633   "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
   634   "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
   635   "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
   636 
   637 translations
   638   "ALL x<y. P"   =>  "ALL x. x < y \<longrightarrow> P"
   639   "EX x<y. P"    =>  "EX x. x < y \<and> P"
   640   "ALL x<=y. P"  =>  "ALL x. x <= y \<longrightarrow> P"
   641   "EX x<=y. P"   =>  "EX x. x <= y \<and> P"
   642   "ALL x>y. P"   =>  "ALL x. x > y \<longrightarrow> P"
   643   "EX x>y. P"    =>  "EX x. x > y \<and> P"
   644   "ALL x>=y. P"  =>  "ALL x. x >= y \<longrightarrow> P"
   645   "EX x>=y. P"   =>  "EX x. x >= y \<and> P"
   646 
   647 print_translation {*
   648 let
   649   val All_binder = Syntax.binder_name @{const_syntax All};
   650   val Ex_binder = Syntax.binder_name @{const_syntax Ex};
   651   val impl = @{const_syntax "op -->"};
   652   val conj = @{const_syntax "op &"};
   653   val less = @{const_syntax less};
   654   val less_eq = @{const_syntax less_eq};
   655 
   656   val trans =
   657    [((All_binder, impl, less), ("_All_less", "_All_greater")),
   658     ((All_binder, impl, less_eq), ("_All_less_eq", "_All_greater_eq")),
   659     ((Ex_binder, conj, less), ("_Ex_less", "_Ex_greater")),
   660     ((Ex_binder, conj, less_eq), ("_Ex_less_eq", "_Ex_greater_eq"))];
   661 
   662   fun matches_bound v t = 
   663      case t of (Const ("_bound", _) $ Free (v', _)) => (v = v')
   664               | _ => false
   665   fun contains_var v = Term.exists_subterm (fn Free (x, _) => x = v | _ => false)
   666   fun mk v c n P = Syntax.const c $ Syntax.mark_bound v $ n $ P
   667 
   668   fun tr' q = (q,
   669     fn [Const ("_bound", _) $ Free (v, _), Const (c, _) $ (Const (d, _) $ t $ u) $ P] =>
   670       (case AList.lookup (op =) trans (q, c, d) of
   671         NONE => raise Match
   672       | SOME (l, g) =>
   673           if matches_bound v t andalso not (contains_var v u) then mk v l u P
   674           else if matches_bound v u andalso not (contains_var v t) then mk v g t P
   675           else raise Match)
   676      | _ => raise Match);
   677 in [tr' All_binder, tr' Ex_binder] end
   678 *}
   679 
   680 
   681 subsection {* Transitivity reasoning *}
   682 
   683 context ord
   684 begin
   685 
   686 lemma ord_le_eq_trans: "a \<le> b \<Longrightarrow> b = c \<Longrightarrow> a \<le> c"
   687   by (rule subst)
   688 
   689 lemma ord_eq_le_trans: "a = b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
   690   by (rule ssubst)
   691 
   692 lemma ord_less_eq_trans: "a < b \<Longrightarrow> b = c \<Longrightarrow> a < c"
   693   by (rule subst)
   694 
   695 lemma ord_eq_less_trans: "a = b \<Longrightarrow> b < c \<Longrightarrow> a < c"
   696   by (rule ssubst)
   697 
   698 end
   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\<Colon>bool) < 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\<Colon>'a \<Rightarrow> 'b) < 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> 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> 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 end