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