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