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