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