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