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