src/HOL/Orderings.thy
author haftmann
Thu Oct 18 09:20:55 2007 +0200 (2007-10-18)
changeset 25076 a50b36401c61
parent 25062 af5ef0d4d655
child 25103 1ee419a5a30f
permissions -rw-r--r--
localized mono predicate
     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 (\<lambda>x y. y \<le> x) (\<lambda>x y. y < x)"
   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 (\<lambda>x y. y \<le> x) (\<lambda>x y. y < x)"
   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 lemmas min_le_iff_disj = linorder_class.min_le_iff_disj
   601 lemmas le_max_iff_disj = linorder_class.le_max_iff_disj
   602 lemmas min_less_iff_disj = linorder_class.min_less_iff_disj
   603 lemmas less_max_iff_disj = linorder_class.less_max_iff_disj
   604 lemmas min_less_iff_conj [simp] = linorder_class.min_less_iff_conj
   605 lemmas max_less_iff_conj [simp] = linorder_class.max_less_iff_conj
   606 lemmas split_min = linorder_class.split_min
   607 lemmas split_max = linorder_class.split_max
   608 
   609 
   610 subsection {* Bounded quantifiers *}
   611 
   612 syntax
   613   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3ALL _<_./ _)"  [0, 0, 10] 10)
   614   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3EX _<_./ _)"  [0, 0, 10] 10)
   615   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _<=_./ _)" [0, 0, 10] 10)
   616   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _<=_./ _)" [0, 0, 10] 10)
   617 
   618   "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3ALL _>_./ _)"  [0, 0, 10] 10)
   619   "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3EX _>_./ _)"  [0, 0, 10] 10)
   620   "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _>=_./ _)" [0, 0, 10] 10)
   621   "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _>=_./ _)" [0, 0, 10] 10)
   622 
   623 syntax (xsymbols)
   624   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
   625   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
   626   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
   627   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
   628 
   629   "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
   630   "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
   631   "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
   632   "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
   633 
   634 syntax (HOL)
   635   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3! _<_./ _)"  [0, 0, 10] 10)
   636   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3? _<_./ _)"  [0, 0, 10] 10)
   637   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3! _<=_./ _)" [0, 0, 10] 10)
   638   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3? _<=_./ _)" [0, 0, 10] 10)
   639 
   640 syntax (HTML output)
   641   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
   642   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
   643   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
   644   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
   645 
   646   "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
   647   "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
   648   "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
   649   "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
   650 
   651 translations
   652   "ALL x<y. P"   =>  "ALL x. x < y \<longrightarrow> P"
   653   "EX x<y. P"    =>  "EX x. x < y \<and> P"
   654   "ALL x<=y. P"  =>  "ALL x. x <= y \<longrightarrow> P"
   655   "EX x<=y. P"   =>  "EX x. x <= y \<and> P"
   656   "ALL x>y. P"   =>  "ALL x. x > y \<longrightarrow> P"
   657   "EX x>y. P"    =>  "EX x. x > y \<and> P"
   658   "ALL x>=y. P"  =>  "ALL x. x >= y \<longrightarrow> P"
   659   "EX x>=y. P"   =>  "EX x. x >= y \<and> P"
   660 
   661 print_translation {*
   662 let
   663   val All_binder = Syntax.binder_name @{const_syntax All};
   664   val Ex_binder = Syntax.binder_name @{const_syntax Ex};
   665   val impl = @{const_syntax "op -->"};
   666   val conj = @{const_syntax "op &"};
   667   val less = @{const_syntax less};
   668   val less_eq = @{const_syntax less_eq};
   669 
   670   val trans =
   671    [((All_binder, impl, less), ("_All_less", "_All_greater")),
   672     ((All_binder, impl, less_eq), ("_All_less_eq", "_All_greater_eq")),
   673     ((Ex_binder, conj, less), ("_Ex_less", "_Ex_greater")),
   674     ((Ex_binder, conj, less_eq), ("_Ex_less_eq", "_Ex_greater_eq"))];
   675 
   676   fun matches_bound v t = 
   677      case t of (Const ("_bound", _) $ Free (v', _)) => (v = v')
   678               | _ => false
   679   fun contains_var v = Term.exists_subterm (fn Free (x, _) => x = v | _ => false)
   680   fun mk v c n P = Syntax.const c $ Syntax.mark_bound v $ n $ P
   681 
   682   fun tr' q = (q,
   683     fn [Const ("_bound", _) $ Free (v, _), Const (c, _) $ (Const (d, _) $ t $ u) $ P] =>
   684       (case AList.lookup (op =) trans (q, c, d) of
   685         NONE => raise Match
   686       | SOME (l, g) =>
   687           if matches_bound v t andalso not (contains_var v u) then mk v l u P
   688           else if matches_bound v u andalso not (contains_var v t) then mk v g t P
   689           else raise Match)
   690      | _ => raise Match);
   691 in [tr' All_binder, tr' Ex_binder] end
   692 *}
   693 
   694 
   695 subsection {* Transitivity reasoning *}
   696 
   697 lemma ord_le_eq_trans: "a <= b ==> b = c ==> a <= c"
   698 by (rule subst)
   699 
   700 lemma ord_eq_le_trans: "a = b ==> b <= c ==> a <= c"
   701 by (rule ssubst)
   702 
   703 lemma ord_less_eq_trans: "a < b ==> b = c ==> a < c"
   704 by (rule subst)
   705 
   706 lemma ord_eq_less_trans: "a = b ==> b < c ==> a < c"
   707 by (rule ssubst)
   708 
   709 lemma order_less_subst2: "(a::'a::order) < b ==> f b < (c::'c::order) ==>
   710   (!!x y. x < y ==> f x < f y) ==> f a < c"
   711 proof -
   712   assume r: "!!x y. x < y ==> f x < f y"
   713   assume "a < b" hence "f a < f b" by (rule r)
   714   also assume "f b < c"
   715   finally (order_less_trans) show ?thesis .
   716 qed
   717 
   718 lemma order_less_subst1: "(a::'a::order) < f b ==> (b::'b::order) < c ==>
   719   (!!x y. x < y ==> f x < f y) ==> a < f c"
   720 proof -
   721   assume r: "!!x y. x < y ==> f x < f y"
   722   assume "a < f b"
   723   also assume "b < c" hence "f b < f c" by (rule r)
   724   finally (order_less_trans) show ?thesis .
   725 qed
   726 
   727 lemma order_le_less_subst2: "(a::'a::order) <= b ==> f b < (c::'c::order) ==>
   728   (!!x y. x <= y ==> f x <= f y) ==> f a < c"
   729 proof -
   730   assume r: "!!x y. x <= y ==> f x <= f y"
   731   assume "a <= b" hence "f a <= f b" by (rule r)
   732   also assume "f b < c"
   733   finally (order_le_less_trans) show ?thesis .
   734 qed
   735 
   736 lemma order_le_less_subst1: "(a::'a::order) <= f b ==> (b::'b::order) < c ==>
   737   (!!x y. x < y ==> f x < f y) ==> a < f c"
   738 proof -
   739   assume r: "!!x y. x < y ==> f x < f y"
   740   assume "a <= f b"
   741   also assume "b < c" hence "f b < f c" by (rule r)
   742   finally (order_le_less_trans) show ?thesis .
   743 qed
   744 
   745 lemma order_less_le_subst2: "(a::'a::order) < b ==> f b <= (c::'c::order) ==>
   746   (!!x y. x < y ==> f x < f y) ==> f a < c"
   747 proof -
   748   assume r: "!!x y. x < y ==> f x < f y"
   749   assume "a < b" hence "f a < f b" by (rule r)
   750   also assume "f b <= c"
   751   finally (order_less_le_trans) show ?thesis .
   752 qed
   753 
   754 lemma order_less_le_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_less_le_trans) show ?thesis .
   761 qed
   762 
   763 lemma order_subst1: "(a::'a::order) <= f b ==> (b::'b::order) <= c ==>
   764   (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
   765 proof -
   766   assume r: "!!x y. x <= y ==> f x <= f y"
   767   assume "a <= f b"
   768   also assume "b <= c" hence "f b <= f c" by (rule r)
   769   finally (order_trans) show ?thesis .
   770 qed
   771 
   772 lemma order_subst2: "(a::'a::order) <= b ==> f b <= (c::'c::order) ==>
   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 (order_trans) show ?thesis .
   779 qed
   780 
   781 lemma ord_le_eq_subst: "a <= b ==> f b = c ==>
   782   (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
   783 proof -
   784   assume r: "!!x y. x <= y ==> f x <= f y"
   785   assume "a <= b" hence "f a <= f b" by (rule r)
   786   also assume "f b = c"
   787   finally (ord_le_eq_trans) show ?thesis .
   788 qed
   789 
   790 lemma ord_eq_le_subst: "a = f b ==> b <= c ==>
   791   (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
   792 proof -
   793   assume r: "!!x y. x <= y ==> f x <= f y"
   794   assume "a = f b"
   795   also assume "b <= c" hence "f b <= f c" by (rule r)
   796   finally (ord_eq_le_trans) show ?thesis .
   797 qed
   798 
   799 lemma ord_less_eq_subst: "a < b ==> f b = c ==>
   800   (!!x y. x < y ==> f x < f y) ==> f a < c"
   801 proof -
   802   assume r: "!!x y. x < y ==> f x < f y"
   803   assume "a < b" hence "f a < f b" by (rule r)
   804   also assume "f b = c"
   805   finally (ord_less_eq_trans) show ?thesis .
   806 qed
   807 
   808 lemma ord_eq_less_subst: "a = f b ==> b < c ==>
   809   (!!x y. x < y ==> f x < f y) ==> a < f c"
   810 proof -
   811   assume r: "!!x y. x < y ==> f x < f y"
   812   assume "a = f b"
   813   also assume "b < c" hence "f b < f c" by (rule r)
   814   finally (ord_eq_less_trans) show ?thesis .
   815 qed
   816 
   817 text {*
   818   Note that this list of rules is in reverse order of priorities.
   819 *}
   820 
   821 lemmas order_trans_rules [trans] =
   822   order_less_subst2
   823   order_less_subst1
   824   order_le_less_subst2
   825   order_le_less_subst1
   826   order_less_le_subst2
   827   order_less_le_subst1
   828   order_subst2
   829   order_subst1
   830   ord_le_eq_subst
   831   ord_eq_le_subst
   832   ord_less_eq_subst
   833   ord_eq_less_subst
   834   forw_subst
   835   back_subst
   836   rev_mp
   837   mp
   838   order_neq_le_trans
   839   order_le_neq_trans
   840   order_less_trans
   841   order_less_asym'
   842   order_le_less_trans
   843   order_less_le_trans
   844   order_trans
   845   order_antisym
   846   ord_le_eq_trans
   847   ord_eq_le_trans
   848   ord_less_eq_trans
   849   ord_eq_less_trans
   850   trans
   851 
   852 
   853 (* FIXME cleanup *)
   854 
   855 text {* These support proving chains of decreasing inequalities
   856     a >= b >= c ... in Isar proofs. *}
   857 
   858 lemma xt1:
   859   "a = b ==> b > c ==> a > c"
   860   "a > b ==> b = c ==> a > c"
   861   "a = b ==> b >= c ==> a >= c"
   862   "a >= b ==> b = c ==> a >= c"
   863   "(x::'a::order) >= y ==> y >= x ==> x = y"
   864   "(x::'a::order) >= y ==> y >= z ==> x >= z"
   865   "(x::'a::order) > y ==> y >= z ==> x > z"
   866   "(x::'a::order) >= y ==> y > z ==> x > z"
   867   "(a::'a::order) > b ==> b > a ==> P"
   868   "(x::'a::order) > y ==> y > z ==> x > z"
   869   "(a::'a::order) >= b ==> a ~= b ==> a > b"
   870   "(a::'a::order) ~= b ==> a >= b ==> a > b"
   871   "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c" 
   872   "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
   873   "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
   874   "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
   875   by auto
   876 
   877 lemma xt2:
   878   "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
   879 by (subgoal_tac "f b >= f c", force, force)
   880 
   881 lemma xt3: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==> 
   882     (!!x y. x >= y ==> f x >= f y) ==> f a >= c"
   883 by (subgoal_tac "f a >= f b", force, force)
   884 
   885 lemma xt4: "(a::'a::order) > f b ==> (b::'b::order) >= c ==>
   886   (!!x y. x >= y ==> f x >= f y) ==> a > f c"
   887 by (subgoal_tac "f b >= f c", force, force)
   888 
   889 lemma xt5: "(a::'a::order) > b ==> (f b::'b::order) >= c==>
   890     (!!x y. x > y ==> f x > f y) ==> f a > c"
   891 by (subgoal_tac "f a > f b", force, force)
   892 
   893 lemma xt6: "(a::'a::order) >= f b ==> b > c ==>
   894     (!!x y. x > y ==> f x > f y) ==> a > f c"
   895 by (subgoal_tac "f b > f c", force, force)
   896 
   897 lemma xt7: "(a::'a::order) >= b ==> (f b::'b::order) > c ==>
   898     (!!x y. x >= y ==> f x >= f y) ==> f a > c"
   899 by (subgoal_tac "f a >= f b", force, force)
   900 
   901 lemma xt8: "(a::'a::order) > f b ==> (b::'b::order) > c ==>
   902     (!!x y. x > y ==> f x > f y) ==> a > f c"
   903 by (subgoal_tac "f b > f c", force, force)
   904 
   905 lemma xt9: "(a::'a::order) > b ==> (f b::'b::order) > c ==>
   906     (!!x y. x > y ==> f x > f y) ==> f a > c"
   907 by (subgoal_tac "f a > f b", force, force)
   908 
   909 lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9
   910 
   911 (* 
   912   Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands
   913   for the wrong thing in an Isar proof.
   914 
   915   The extra transitivity rules can be used as follows: 
   916 
   917 lemma "(a::'a::order) > z"
   918 proof -
   919   have "a >= b" (is "_ >= ?rhs")
   920     sorry
   921   also have "?rhs >= c" (is "_ >= ?rhs")
   922     sorry
   923   also (xtrans) have "?rhs = d" (is "_ = ?rhs")
   924     sorry
   925   also (xtrans) have "?rhs >= e" (is "_ >= ?rhs")
   926     sorry
   927   also (xtrans) have "?rhs > f" (is "_ > ?rhs")
   928     sorry
   929   also (xtrans) have "?rhs > z"
   930     sorry
   931   finally (xtrans) show ?thesis .
   932 qed
   933 
   934   Alternatively, one can use "declare xtrans [trans]" and then
   935   leave out the "(xtrans)" above.
   936 *)
   937 
   938 subsection {* Order on bool *}
   939 
   940 instance bool :: order 
   941   le_bool_def: "P \<le> Q \<equiv> P \<longrightarrow> Q"
   942   less_bool_def: "P < Q \<equiv> P \<le> Q \<and> P \<noteq> Q"
   943   by intro_classes (auto simp add: le_bool_def less_bool_def)
   944 lemmas [code func del] = le_bool_def less_bool_def
   945 
   946 lemma le_boolI: "(P \<Longrightarrow> Q) \<Longrightarrow> P \<le> Q"
   947 by (simp add: le_bool_def)
   948 
   949 lemma le_boolI': "P \<longrightarrow> Q \<Longrightarrow> P \<le> Q"
   950 by (simp add: le_bool_def)
   951 
   952 lemma le_boolE: "P \<le> Q \<Longrightarrow> P \<Longrightarrow> (Q \<Longrightarrow> R) \<Longrightarrow> R"
   953 by (simp add: le_bool_def)
   954 
   955 lemma le_boolD: "P \<le> Q \<Longrightarrow> P \<longrightarrow> Q"
   956 by (simp add: le_bool_def)
   957 
   958 lemma [code func]:
   959   "False \<le> b \<longleftrightarrow> True"
   960   "True \<le> b \<longleftrightarrow> b"
   961   "False < b \<longleftrightarrow> b"
   962   "True < b \<longleftrightarrow> False"
   963   unfolding le_bool_def less_bool_def by simp_all
   964 
   965 
   966 subsection {* Order on sets *}
   967 
   968 instance set :: (type) order
   969   by (intro_classes,
   970       (assumption | rule subset_refl subset_trans subset_antisym psubset_eq)+)
   971 
   972 lemmas basic_trans_rules [trans] =
   973   order_trans_rules set_rev_mp set_mp
   974 
   975 
   976 subsection {* Order on functions *}
   977 
   978 instance "fun" :: (type, ord) ord
   979   le_fun_def: "f \<le> g \<equiv> \<forall>x. f x \<le> g x"
   980   less_fun_def: "f < g \<equiv> f \<le> g \<and> f \<noteq> g" ..
   981 
   982 lemmas [code func del] = le_fun_def less_fun_def
   983 
   984 instance "fun" :: (type, order) order
   985   by default
   986     (auto simp add: le_fun_def less_fun_def expand_fun_eq
   987        intro: order_trans order_antisym)
   988 
   989 lemma le_funI: "(\<And>x. f x \<le> g x) \<Longrightarrow> f \<le> g"
   990   unfolding le_fun_def by simp
   991 
   992 lemma le_funE: "f \<le> g \<Longrightarrow> (f x \<le> g x \<Longrightarrow> P) \<Longrightarrow> P"
   993   unfolding le_fun_def by simp
   994 
   995 lemma le_funD: "f \<le> g \<Longrightarrow> f x \<le> g x"
   996   unfolding le_fun_def by simp
   997 
   998 text {*
   999   Handy introduction and elimination rules for @{text "\<le>"}
  1000   on unary and binary predicates
  1001 *}
  1002 
  1003 lemma predicate1I [Pure.intro!, intro!]:
  1004   assumes PQ: "\<And>x. P x \<Longrightarrow> Q x"
  1005   shows "P \<le> Q"
  1006   apply (rule le_funI)
  1007   apply (rule le_boolI)
  1008   apply (rule PQ)
  1009   apply assumption
  1010   done
  1011 
  1012 lemma predicate1D [Pure.dest, dest]: "P \<le> Q \<Longrightarrow> P x \<Longrightarrow> Q x"
  1013   apply (erule le_funE)
  1014   apply (erule le_boolE)
  1015   apply assumption+
  1016   done
  1017 
  1018 lemma predicate2I [Pure.intro!, intro!]:
  1019   assumes PQ: "\<And>x y. P x y \<Longrightarrow> Q x y"
  1020   shows "P \<le> Q"
  1021   apply (rule le_funI)+
  1022   apply (rule le_boolI)
  1023   apply (rule PQ)
  1024   apply assumption
  1025   done
  1026 
  1027 lemma predicate2D [Pure.dest, dest]: "P \<le> Q \<Longrightarrow> P x y \<Longrightarrow> Q x y"
  1028   apply (erule le_funE)+
  1029   apply (erule le_boolE)
  1030   apply assumption+
  1031   done
  1032 
  1033 lemma rev_predicate1D: "P x ==> P <= Q ==> Q x"
  1034   by (rule predicate1D)
  1035 
  1036 lemma rev_predicate2D: "P x y ==> P <= Q ==> Q x y"
  1037   by (rule predicate2D)
  1038 
  1039 
  1040 subsection {* Monotonicity, least value operator and min/max *}
  1041 
  1042 context order
  1043 begin
  1044 
  1045 definition
  1046   mono :: "('a \<Rightarrow> 'b\<Colon>order) \<Rightarrow> bool"
  1047 where
  1048   "mono f \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> f x \<le> f y)"
  1049 
  1050 lemma monoI [intro?]:
  1051   fixes f :: "'a \<Rightarrow> 'b\<Colon>order"
  1052   shows "(\<And>x y. x \<le> y \<Longrightarrow> f x \<le> f y) \<Longrightarrow> mono f"
  1053   unfolding mono_def by iprover
  1054 
  1055 lemma monoD [dest?]:
  1056   fixes f :: "'a \<Rightarrow> 'b\<Colon>order"
  1057   shows "mono f \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
  1058   unfolding mono_def by iprover
  1059 
  1060 end
  1061 
  1062 context linorder
  1063 begin
  1064 
  1065 lemma min_of_mono:
  1066   fixes f :: "'a \<Rightarrow> 'b\<Colon>linorder"
  1067   shows "mono f \<Longrightarrow> Orderings.min (f m) (f n) = f (min m n)"
  1068   by (auto simp: mono_def Orderings.min_def min_def intro: Orderings.antisym)
  1069 
  1070 lemma max_of_mono:
  1071   fixes f :: "'a \<Rightarrow> 'b\<Colon>linorder"
  1072   shows "mono f \<Longrightarrow> Orderings.max (f m) (f n) = f (max m n)"
  1073   by (auto simp: mono_def Orderings.max_def max_def intro: Orderings.antisym)
  1074 
  1075 end
  1076 
  1077 lemma LeastI2_order:
  1078   "[| P (x::'a::order);
  1079       !!y. P y ==> x <= y;
  1080       !!x. [| P x; ALL y. P y --> x \<le> y |] ==> Q x |]
  1081    ==> Q (Least P)"
  1082 apply (unfold Least_def)
  1083 apply (rule theI2)
  1084   apply (blast intro: order_antisym)+
  1085 done
  1086 
  1087 lemma Least_mono:
  1088   "mono (f::'a::order => 'b::order) ==> EX x:S. ALL y:S. x <= y
  1089     ==> (LEAST y. y : f ` S) = f (LEAST x. x : S)"
  1090     -- {* Courtesy of Stephan Merz *}
  1091   apply clarify
  1092   apply (erule_tac P = "%x. x : S" in LeastI2_order, fast)
  1093   apply (rule LeastI2_order)
  1094   apply (auto elim: monoD intro!: order_antisym)
  1095   done
  1096 
  1097 lemma Least_equality:
  1098   "[| P (k::'a::order); !!x. P x ==> k <= x |] ==> (LEAST x. P x) = k"
  1099 apply (simp add: Least_def)
  1100 apply (rule the_equality)
  1101 apply (auto intro!: order_antisym)
  1102 done
  1103 
  1104 lemma min_leastL: "(!!x. least <= x) ==> min least x = least"
  1105 by (simp add: min_def)
  1106 
  1107 lemma max_leastL: "(!!x. least <= x) ==> max least x = x"
  1108 by (simp add: max_def)
  1109 
  1110 lemma min_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> min x least = least"
  1111 apply (simp add: min_def)
  1112 apply (blast intro: order_antisym)
  1113 done
  1114 
  1115 lemma max_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> max x least = x"
  1116 apply (simp add: max_def)
  1117 apply (blast intro: order_antisym)
  1118 done
  1119 
  1120 subsection {* legacy ML bindings *}
  1121 
  1122 ML {*
  1123 val monoI = @{thm monoI};
  1124 *}
  1125 
  1126 end