src/HOL/Orderings.thy
author blanchet
Wed Feb 12 08:35:57 2014 +0100 (2014-02-12)
changeset 55415 05f5fdb8d093
parent 54868 bab6cade3cc5
child 56020 f92479477c52
permissions -rw-r--r--
renamed 'nat_{case,rec}' to '{case,rec}_nat'
     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 
   348 signature ORDERS =
   349 sig
   350   val print_structures: Proof.context -> unit
   351   val attrib_setup: theory -> theory
   352   val order_tac: Proof.context -> thm list -> int -> tactic
   353 end;
   354 
   355 structure Orders: ORDERS =
   356 struct
   357 
   358 (** Theory and context data **)
   359 
   360 fun struct_eq ((s1: string, ts1), (s2, ts2)) =
   361   (s1 = s2) andalso eq_list (op aconv) (ts1, ts2);
   362 
   363 structure Data = Generic_Data
   364 (
   365   type T = ((string * term list) * Order_Tac.less_arith) list;
   366     (* Order structures:
   367        identifier of the structure, list of operations and record of theorems
   368        needed to set up the transitivity reasoner,
   369        identifier and operations identify the structure uniquely. *)
   370   val empty = [];
   371   val extend = I;
   372   fun merge data = AList.join struct_eq (K fst) data;
   373 );
   374 
   375 fun print_structures ctxt =
   376   let
   377     val structs = Data.get (Context.Proof ctxt);
   378     fun pretty_term t = Pretty.block
   379       [Pretty.quote (Syntax.pretty_term ctxt t), Pretty.brk 1,
   380         Pretty.str "::", Pretty.brk 1,
   381         Pretty.quote (Syntax.pretty_typ ctxt (type_of t))];
   382     fun pretty_struct ((s, ts), _) = Pretty.block
   383       [Pretty.str s, Pretty.str ":", Pretty.brk 1,
   384        Pretty.enclose "(" ")" (Pretty.breaks (map pretty_term ts))];
   385   in
   386     Pretty.writeln (Pretty.big_list "order structures:" (map pretty_struct structs))
   387   end;
   388 
   389 
   390 (** Method **)
   391 
   392 fun struct_tac ((s, [eq, le, less]), thms) ctxt prems =
   393   let
   394     fun decomp thy (@{const Trueprop} $ t) =
   395       let
   396         fun excluded t =
   397           (* exclude numeric types: linear arithmetic subsumes transitivity *)
   398           let val T = type_of t
   399           in
   400             T = HOLogic.natT orelse T = HOLogic.intT orelse T = HOLogic.realT
   401           end;
   402         fun rel (bin_op $ t1 $ t2) =
   403               if excluded t1 then NONE
   404               else if Pattern.matches thy (eq, bin_op) then SOME (t1, "=", t2)
   405               else if Pattern.matches thy (le, bin_op) then SOME (t1, "<=", t2)
   406               else if Pattern.matches thy (less, bin_op) then SOME (t1, "<", t2)
   407               else NONE
   408           | rel _ = NONE;
   409         fun dec (Const (@{const_name Not}, _) $ t) = (case rel t
   410               of NONE => NONE
   411                | SOME (t1, rel, t2) => SOME (t1, "~" ^ rel, t2))
   412           | dec x = rel x;
   413       in dec t end
   414       | decomp thy _ = NONE;
   415   in
   416     case s of
   417       "order" => Order_Tac.partial_tac decomp thms ctxt prems
   418     | "linorder" => Order_Tac.linear_tac decomp thms ctxt prems
   419     | _ => error ("Unknown kind of order `" ^ s ^ "' encountered in transitivity reasoner.")
   420   end
   421 
   422 fun order_tac ctxt prems =
   423   FIRST' (map (fn s => CHANGED o struct_tac s ctxt prems) (Data.get (Context.Proof ctxt)));
   424 
   425 
   426 (** Attribute **)
   427 
   428 fun add_struct_thm s tag =
   429   Thm.declaration_attribute
   430     (fn thm => Data.map (AList.map_default struct_eq (s, Order_Tac.empty TrueI) (Order_Tac.update tag thm)));
   431 fun del_struct s =
   432   Thm.declaration_attribute
   433     (fn _ => Data.map (AList.delete struct_eq s));
   434 
   435 val attrib_setup =
   436   Attrib.setup @{binding order}
   437     (Scan.lift ((Args.add -- Args.name >> (fn (_, s) => SOME s) || Args.del >> K NONE) --|
   438       Args.colon (* FIXME || Scan.succeed true *) ) -- Scan.lift Args.name --
   439       Scan.repeat Args.term
   440       >> (fn ((SOME tag, n), ts) => add_struct_thm (n, ts) tag
   441            | ((NONE, n), ts) => del_struct (n, ts)))
   442     "theorems controlling transitivity reasoner";
   443 
   444 
   445 (** Diagnostic command **)
   446 
   447 val _ =
   448   Outer_Syntax.improper_command @{command_spec "print_orders"}
   449     "print order structures available to transitivity reasoner"
   450     (Scan.succeed (Toplevel.unknown_context o
   451       Toplevel.keep (print_structures o Toplevel.context_of)));
   452 
   453 end;
   454 
   455 *}
   456 
   457 setup Orders.attrib_setup
   458 
   459 method_setup order = {*
   460   Scan.succeed (fn ctxt => SIMPLE_METHOD' (Orders.order_tac ctxt []))
   461 *} "transitivity reasoner"
   462 
   463 
   464 text {* Declarations to set up transitivity reasoner of partial and linear orders. *}
   465 
   466 context order
   467 begin
   468 
   469 (* The type constraint on @{term op =} below is necessary since the operation
   470    is not a parameter of the locale. *)
   471 
   472 declare less_irrefl [THEN notE, order add less_reflE: order "op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool" "op <=" "op <"]
   473   
   474 declare order_refl  [order add le_refl: order "op = :: 'a => 'a => bool" "op <=" "op <"]
   475   
   476 declare less_imp_le [order add less_imp_le: order "op = :: 'a => 'a => bool" "op <=" "op <"]
   477   
   478 declare antisym [order add eqI: order "op = :: 'a => 'a => bool" "op <=" "op <"]
   479 
   480 declare eq_refl [order add eqD1: order "op = :: 'a => 'a => bool" "op <=" "op <"]
   481 
   482 declare sym [THEN eq_refl, order add eqD2: order "op = :: 'a => 'a => bool" "op <=" "op <"]
   483 
   484 declare less_trans [order add less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
   485   
   486 declare less_le_trans [order add less_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
   487   
   488 declare le_less_trans [order add le_less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
   489 
   490 declare order_trans [order add le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
   491 
   492 declare le_neq_trans [order add le_neq_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
   493 
   494 declare neq_le_trans [order add neq_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
   495 
   496 declare less_imp_neq [order add less_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"]
   497 
   498 declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"]
   499 
   500 declare not_sym [order add not_sym: order "op = :: 'a => 'a => bool" "op <=" "op <"]
   501 
   502 end
   503 
   504 context linorder
   505 begin
   506 
   507 declare [[order del: order "op = :: 'a => 'a => bool" "op <=" "op <"]]
   508 
   509 declare less_irrefl [THEN notE, order add less_reflE: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   510 
   511 declare order_refl [order add le_refl: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   512 
   513 declare less_imp_le [order add less_imp_le: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   514 
   515 declare not_less [THEN iffD2, order add not_lessI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   516 
   517 declare not_le [THEN iffD2, order add not_leI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   518 
   519 declare not_less [THEN iffD1, order add not_lessD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   520 
   521 declare not_le [THEN iffD1, order add not_leD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   522 
   523 declare antisym [order add eqI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   524 
   525 declare eq_refl [order add eqD1: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   526 
   527 declare sym [THEN eq_refl, order add eqD2: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   528 
   529 declare less_trans [order add less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   530 
   531 declare less_le_trans [order add less_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   532 
   533 declare le_less_trans [order add le_less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   534 
   535 declare order_trans [order add le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   536 
   537 declare le_neq_trans [order add le_neq_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   538 
   539 declare neq_le_trans [order add neq_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   540 
   541 declare less_imp_neq [order add less_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   542 
   543 declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   544 
   545 declare not_sym [order add not_sym: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
   546 
   547 end
   548 
   549 
   550 setup {*
   551 let
   552 
   553 fun prp t thm = Thm.prop_of thm = t;  (* FIXME aconv!? *)
   554 
   555 fun prove_antisym_le ctxt ((le as Const(_,T)) $ r $ s) =
   556   let val prems = Simplifier.prems_of ctxt;
   557       val less = Const (@{const_name less}, T);
   558       val t = HOLogic.mk_Trueprop(le $ s $ r);
   559   in case find_first (prp t) prems of
   560        NONE =>
   561          let val t = HOLogic.mk_Trueprop(HOLogic.Not $ (less $ r $ s))
   562          in case find_first (prp t) prems of
   563               NONE => NONE
   564             | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv1}))
   565          end
   566      | SOME thm => SOME(mk_meta_eq(thm RS @{thm order_class.antisym_conv}))
   567   end
   568   handle THM _ => NONE;
   569 
   570 fun prove_antisym_less ctxt (NotC $ ((less as Const(_,T)) $ r $ s)) =
   571   let val prems = Simplifier.prems_of ctxt;
   572       val le = Const (@{const_name less_eq}, T);
   573       val t = HOLogic.mk_Trueprop(le $ r $ s);
   574   in case find_first (prp t) prems of
   575        NONE =>
   576          let val t = HOLogic.mk_Trueprop(NotC $ (less $ s $ r))
   577          in case find_first (prp t) prems of
   578               NONE => NONE
   579             | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv3}))
   580          end
   581      | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv2}))
   582   end
   583   handle THM _ => NONE;
   584 
   585 fun add_simprocs procs thy =
   586   map_theory_simpset (fn ctxt => ctxt
   587     addsimprocs (map (fn (name, raw_ts, proc) =>
   588       Simplifier.simproc_global thy name raw_ts proc) procs)) thy;
   589 
   590 fun add_solver name tac =
   591   map_theory_simpset (fn ctxt0 => ctxt0 addSolver
   592     mk_solver name (fn ctxt => tac ctxt (Simplifier.prems_of ctxt)));
   593 
   594 in
   595   add_simprocs [
   596        ("antisym le", ["(x::'a::order) <= y"], prove_antisym_le),
   597        ("antisym less", ["~ (x::'a::linorder) < y"], prove_antisym_less)
   598      ]
   599   #> add_solver "Transitivity" Orders.order_tac
   600   (* Adding the transitivity reasoners also as safe solvers showed a slight
   601      speed up, but the reasoning strength appears to be not higher (at least
   602      no breaking of additional proofs in the entire HOL distribution, as
   603      of 5 March 2004, was observed). *)
   604 end
   605 *}
   606 
   607 
   608 subsection {* Bounded quantifiers *}
   609 
   610 syntax
   611   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3ALL _<_./ _)"  [0, 0, 10] 10)
   612   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3EX _<_./ _)"  [0, 0, 10] 10)
   613   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _<=_./ _)" [0, 0, 10] 10)
   614   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _<=_./ _)" [0, 0, 10] 10)
   615 
   616   "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3ALL _>_./ _)"  [0, 0, 10] 10)
   617   "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3EX _>_./ _)"  [0, 0, 10] 10)
   618   "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _>=_./ _)" [0, 0, 10] 10)
   619   "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _>=_./ _)" [0, 0, 10] 10)
   620 
   621 syntax (xsymbols)
   622   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
   623   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
   624   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
   625   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
   626 
   627   "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
   628   "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
   629   "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
   630   "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
   631 
   632 syntax (HOL)
   633   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3! _<_./ _)"  [0, 0, 10] 10)
   634   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3? _<_./ _)"  [0, 0, 10] 10)
   635   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3! _<=_./ _)" [0, 0, 10] 10)
   636   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3? _<=_./ _)" [0, 0, 10] 10)
   637 
   638 syntax (HTML output)
   639   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
   640   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
   641   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
   642   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
   643 
   644   "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
   645   "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
   646   "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
   647   "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
   648 
   649 translations
   650   "ALL x<y. P"   =>  "ALL x. x < y \<longrightarrow> P"
   651   "EX x<y. P"    =>  "EX x. x < y \<and> P"
   652   "ALL x<=y. P"  =>  "ALL x. x <= y \<longrightarrow> P"
   653   "EX x<=y. P"   =>  "EX x. x <= y \<and> P"
   654   "ALL x>y. P"   =>  "ALL x. x > y \<longrightarrow> P"
   655   "EX x>y. P"    =>  "EX x. x > y \<and> P"
   656   "ALL x>=y. P"  =>  "ALL x. x >= y \<longrightarrow> P"
   657   "EX x>=y. P"   =>  "EX x. x >= y \<and> P"
   658 
   659 print_translation {*
   660 let
   661   val All_binder = Mixfix.binder_name @{const_syntax All};
   662   val Ex_binder = Mixfix.binder_name @{const_syntax Ex};
   663   val impl = @{const_syntax HOL.implies};
   664   val conj = @{const_syntax HOL.conj};
   665   val less = @{const_syntax less};
   666   val less_eq = @{const_syntax less_eq};
   667 
   668   val trans =
   669    [((All_binder, impl, less),
   670     (@{syntax_const "_All_less"}, @{syntax_const "_All_greater"})),
   671     ((All_binder, impl, less_eq),
   672     (@{syntax_const "_All_less_eq"}, @{syntax_const "_All_greater_eq"})),
   673     ((Ex_binder, conj, less),
   674     (@{syntax_const "_Ex_less"}, @{syntax_const "_Ex_greater"})),
   675     ((Ex_binder, conj, less_eq),
   676     (@{syntax_const "_Ex_less_eq"}, @{syntax_const "_Ex_greater_eq"}))];
   677 
   678   fun matches_bound v t =
   679     (case t of
   680       Const (@{syntax_const "_bound"}, _) $ Free (v', _) => v = v'
   681     | _ => false);
   682   fun contains_var v = Term.exists_subterm (fn Free (x, _) => x = v | _ => false);
   683   fun mk x c n P = Syntax.const c $ Syntax_Trans.mark_bound_body x $ n $ P;
   684 
   685   fun tr' q = (q, fn _ =>
   686     (fn [Const (@{syntax_const "_bound"}, _) $ Free (v, T),
   687         Const (c, _) $ (Const (d, _) $ t $ u) $ P] =>
   688         (case AList.lookup (op =) trans (q, c, d) of
   689           NONE => raise Match
   690         | SOME (l, g) =>
   691             if matches_bound v t andalso not (contains_var v u) then mk (v, T) l u P
   692             else if matches_bound v u andalso not (contains_var v t) then mk (v, T) g t P
   693             else raise Match)
   694       | _ => raise Match));
   695 in [tr' All_binder, tr' Ex_binder] end
   696 *}
   697 
   698 
   699 subsection {* Transitivity reasoning *}
   700 
   701 context ord
   702 begin
   703 
   704 lemma ord_le_eq_trans: "a \<le> b \<Longrightarrow> b = c \<Longrightarrow> a \<le> c"
   705   by (rule subst)
   706 
   707 lemma ord_eq_le_trans: "a = b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
   708   by (rule ssubst)
   709 
   710 lemma ord_less_eq_trans: "a < b \<Longrightarrow> b = c \<Longrightarrow> a < c"
   711   by (rule subst)
   712 
   713 lemma ord_eq_less_trans: "a = b \<Longrightarrow> b < c \<Longrightarrow> a < c"
   714   by (rule ssubst)
   715 
   716 end
   717 
   718 lemma order_less_subst2: "(a::'a::order) < b ==> f b < (c::'c::order) ==>
   719   (!!x y. x < y ==> f x < f y) ==> f a < c"
   720 proof -
   721   assume r: "!!x y. x < y ==> f x < f y"
   722   assume "a < b" hence "f a < f b" by (rule r)
   723   also assume "f b < c"
   724   finally (less_trans) show ?thesis .
   725 qed
   726 
   727 lemma order_less_subst1: "(a::'a::order) < f b ==> (b::'b::order) < c ==>
   728   (!!x y. x < y ==> f x < f y) ==> a < f c"
   729 proof -
   730   assume r: "!!x y. x < y ==> f x < f y"
   731   assume "a < f b"
   732   also assume "b < c" hence "f b < f c" by (rule r)
   733   finally (less_trans) show ?thesis .
   734 qed
   735 
   736 lemma order_le_less_subst2: "(a::'a::order) <= b ==> f b < (c::'c::order) ==>
   737   (!!x y. x <= y ==> f x <= f y) ==> f a < c"
   738 proof -
   739   assume r: "!!x y. x <= y ==> f x <= f y"
   740   assume "a <= b" hence "f a <= f b" by (rule r)
   741   also assume "f b < c"
   742   finally (le_less_trans) show ?thesis .
   743 qed
   744 
   745 lemma order_le_less_subst1: "(a::'a::order) <= f b ==> (b::'b::order) < c ==>
   746   (!!x y. x < y ==> f x < f y) ==> a < f c"
   747 proof -
   748   assume r: "!!x y. x < y ==> f x < f y"
   749   assume "a <= f b"
   750   also assume "b < c" hence "f b < f c" by (rule r)
   751   finally (le_less_trans) show ?thesis .
   752 qed
   753 
   754 lemma order_less_le_subst2: "(a::'a::order) < b ==> f b <= (c::'c::order) ==>
   755   (!!x y. x < y ==> f x < f y) ==> f a < c"
   756 proof -
   757   assume r: "!!x y. x < y ==> f x < f y"
   758   assume "a < b" hence "f a < f b" by (rule r)
   759   also assume "f b <= c"
   760   finally (less_le_trans) show ?thesis .
   761 qed
   762 
   763 lemma order_less_le_subst1: "(a::'a::order) < f b ==> (b::'b::order) <= c ==>
   764   (!!x y. x <= y ==> f x <= f y) ==> a < f c"
   765 proof -
   766   assume r: "!!x y. x <= y ==> f x <= f y"
   767   assume "a < f b"
   768   also assume "b <= c" hence "f b <= f c" by (rule r)
   769   finally (less_le_trans) show ?thesis .
   770 qed
   771 
   772 lemma order_subst1: "(a::'a::order) <= f b ==> (b::'b::order) <= c ==>
   773   (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
   774 proof -
   775   assume r: "!!x y. x <= y ==> f x <= f y"
   776   assume "a <= f b"
   777   also assume "b <= c" hence "f b <= f c" by (rule r)
   778   finally (order_trans) show ?thesis .
   779 qed
   780 
   781 lemma order_subst2: "(a::'a::order) <= b ==> f b <= (c::'c::order) ==>
   782   (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
   783 proof -
   784   assume r: "!!x y. x <= y ==> f x <= f y"
   785   assume "a <= b" hence "f a <= f b" by (rule r)
   786   also assume "f b <= c"
   787   finally (order_trans) show ?thesis .
   788 qed
   789 
   790 lemma ord_le_eq_subst: "a <= b ==> f b = c ==>
   791   (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
   792 proof -
   793   assume r: "!!x y. x <= y ==> f x <= f y"
   794   assume "a <= b" hence "f a <= f b" by (rule r)
   795   also assume "f b = c"
   796   finally (ord_le_eq_trans) show ?thesis .
   797 qed
   798 
   799 lemma ord_eq_le_subst: "a = f b ==> b <= c ==>
   800   (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
   801 proof -
   802   assume r: "!!x y. x <= y ==> f x <= f y"
   803   assume "a = f b"
   804   also assume "b <= c" hence "f b <= f c" by (rule r)
   805   finally (ord_eq_le_trans) show ?thesis .
   806 qed
   807 
   808 lemma ord_less_eq_subst: "a < b ==> f b = c ==>
   809   (!!x y. x < y ==> f x < f y) ==> f a < c"
   810 proof -
   811   assume r: "!!x y. x < y ==> f x < f y"
   812   assume "a < b" hence "f a < f b" by (rule r)
   813   also assume "f b = c"
   814   finally (ord_less_eq_trans) show ?thesis .
   815 qed
   816 
   817 lemma ord_eq_less_subst: "a = f b ==> b < c ==>
   818   (!!x y. x < y ==> f x < f y) ==> a < f c"
   819 proof -
   820   assume r: "!!x y. x < y ==> f x < f y"
   821   assume "a = f b"
   822   also assume "b < c" hence "f b < f c" by (rule r)
   823   finally (ord_eq_less_trans) show ?thesis .
   824 qed
   825 
   826 text {*
   827   Note that this list of rules is in reverse order of priorities.
   828 *}
   829 
   830 lemmas [trans] =
   831   order_less_subst2
   832   order_less_subst1
   833   order_le_less_subst2
   834   order_le_less_subst1
   835   order_less_le_subst2
   836   order_less_le_subst1
   837   order_subst2
   838   order_subst1
   839   ord_le_eq_subst
   840   ord_eq_le_subst
   841   ord_less_eq_subst
   842   ord_eq_less_subst
   843   forw_subst
   844   back_subst
   845   rev_mp
   846   mp
   847 
   848 lemmas (in order) [trans] =
   849   neq_le_trans
   850   le_neq_trans
   851 
   852 lemmas (in preorder) [trans] =
   853   less_trans
   854   less_asym'
   855   le_less_trans
   856   less_le_trans
   857   order_trans
   858 
   859 lemmas (in order) [trans] =
   860   antisym
   861 
   862 lemmas (in ord) [trans] =
   863   ord_le_eq_trans
   864   ord_eq_le_trans
   865   ord_less_eq_trans
   866   ord_eq_less_trans
   867 
   868 lemmas [trans] =
   869   trans
   870 
   871 lemmas order_trans_rules =
   872   order_less_subst2
   873   order_less_subst1
   874   order_le_less_subst2
   875   order_le_less_subst1
   876   order_less_le_subst2
   877   order_less_le_subst1
   878   order_subst2
   879   order_subst1
   880   ord_le_eq_subst
   881   ord_eq_le_subst
   882   ord_less_eq_subst
   883   ord_eq_less_subst
   884   forw_subst
   885   back_subst
   886   rev_mp
   887   mp
   888   neq_le_trans
   889   le_neq_trans
   890   less_trans
   891   less_asym'
   892   le_less_trans
   893   less_le_trans
   894   order_trans
   895   antisym
   896   ord_le_eq_trans
   897   ord_eq_le_trans
   898   ord_less_eq_trans
   899   ord_eq_less_trans
   900   trans
   901 
   902 text {* These support proving chains of decreasing inequalities
   903     a >= b >= c ... in Isar proofs. *}
   904 
   905 lemma xt1 [no_atp]:
   906   "a = b ==> b > c ==> a > c"
   907   "a > b ==> b = c ==> a > c"
   908   "a = b ==> b >= c ==> a >= c"
   909   "a >= b ==> b = c ==> a >= c"
   910   "(x::'a::order) >= y ==> y >= x ==> x = y"
   911   "(x::'a::order) >= y ==> y >= z ==> x >= z"
   912   "(x::'a::order) > y ==> y >= z ==> x > z"
   913   "(x::'a::order) >= y ==> y > z ==> x > z"
   914   "(a::'a::order) > b ==> b > a ==> P"
   915   "(x::'a::order) > y ==> y > z ==> x > z"
   916   "(a::'a::order) >= b ==> a ~= b ==> a > b"
   917   "(a::'a::order) ~= b ==> a >= b ==> a > b"
   918   "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c" 
   919   "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
   920   "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
   921   "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
   922   by auto
   923 
   924 lemma xt2 [no_atp]:
   925   "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
   926 by (subgoal_tac "f b >= f c", force, force)
   927 
   928 lemma xt3 [no_atp]: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==>
   929     (!!x y. x >= y ==> f x >= f y) ==> f a >= c"
   930 by (subgoal_tac "f a >= f b", force, force)
   931 
   932 lemma xt4 [no_atp]: "(a::'a::order) > f b ==> (b::'b::order) >= c ==>
   933   (!!x y. x >= y ==> f x >= f y) ==> a > f c"
   934 by (subgoal_tac "f b >= f c", force, force)
   935 
   936 lemma xt5 [no_atp]: "(a::'a::order) > b ==> (f b::'b::order) >= c==>
   937     (!!x y. x > y ==> f x > f y) ==> f a > c"
   938 by (subgoal_tac "f a > f b", force, force)
   939 
   940 lemma xt6 [no_atp]: "(a::'a::order) >= f b ==> b > c ==>
   941     (!!x y. x > y ==> f x > f y) ==> a > f c"
   942 by (subgoal_tac "f b > f c", force, force)
   943 
   944 lemma xt7 [no_atp]: "(a::'a::order) >= b ==> (f b::'b::order) > c ==>
   945     (!!x y. x >= y ==> f x >= f y) ==> f a > c"
   946 by (subgoal_tac "f a >= f b", force, force)
   947 
   948 lemma xt8 [no_atp]: "(a::'a::order) > f b ==> (b::'b::order) > c ==>
   949     (!!x y. x > y ==> f x > f y) ==> a > f c"
   950 by (subgoal_tac "f b > f c", force, force)
   951 
   952 lemma xt9 [no_atp]: "(a::'a::order) > b ==> (f b::'b::order) > c ==>
   953     (!!x y. x > y ==> f x > f y) ==> f a > c"
   954 by (subgoal_tac "f a > f b", force, force)
   955 
   956 lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9
   957 
   958 (* 
   959   Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands
   960   for the wrong thing in an Isar proof.
   961 
   962   The extra transitivity rules can be used as follows: 
   963 
   964 lemma "(a::'a::order) > z"
   965 proof -
   966   have "a >= b" (is "_ >= ?rhs")
   967     sorry
   968   also have "?rhs >= c" (is "_ >= ?rhs")
   969     sorry
   970   also (xtrans) have "?rhs = d" (is "_ = ?rhs")
   971     sorry
   972   also (xtrans) have "?rhs >= e" (is "_ >= ?rhs")
   973     sorry
   974   also (xtrans) have "?rhs > f" (is "_ > ?rhs")
   975     sorry
   976   also (xtrans) have "?rhs > z"
   977     sorry
   978   finally (xtrans) show ?thesis .
   979 qed
   980 
   981   Alternatively, one can use "declare xtrans [trans]" and then
   982   leave out the "(xtrans)" above.
   983 *)
   984 
   985 
   986 subsection {* Monotonicity *}
   987 
   988 context order
   989 begin
   990 
   991 definition mono :: "('a \<Rightarrow> 'b\<Colon>order) \<Rightarrow> bool" where
   992   "mono f \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> f x \<le> f y)"
   993 
   994 lemma monoI [intro?]:
   995   fixes f :: "'a \<Rightarrow> 'b\<Colon>order"
   996   shows "(\<And>x y. x \<le> y \<Longrightarrow> f x \<le> f y) \<Longrightarrow> mono f"
   997   unfolding mono_def by iprover
   998 
   999 lemma monoD [dest?]:
  1000   fixes f :: "'a \<Rightarrow> 'b\<Colon>order"
  1001   shows "mono f \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
  1002   unfolding mono_def by iprover
  1003 
  1004 lemma monoE:
  1005   fixes f :: "'a \<Rightarrow> 'b\<Colon>order"
  1006   assumes "mono f"
  1007   assumes "x \<le> y"
  1008   obtains "f x \<le> f y"
  1009 proof
  1010   from assms show "f x \<le> f y" by (simp add: mono_def)
  1011 qed
  1012 
  1013 definition strict_mono :: "('a \<Rightarrow> 'b\<Colon>order) \<Rightarrow> bool" where
  1014   "strict_mono f \<longleftrightarrow> (\<forall>x y. x < y \<longrightarrow> f x < f y)"
  1015 
  1016 lemma strict_monoI [intro?]:
  1017   assumes "\<And>x y. x < y \<Longrightarrow> f x < f y"
  1018   shows "strict_mono f"
  1019   using assms unfolding strict_mono_def by auto
  1020 
  1021 lemma strict_monoD [dest?]:
  1022   "strict_mono f \<Longrightarrow> x < y \<Longrightarrow> f x < f y"
  1023   unfolding strict_mono_def by auto
  1024 
  1025 lemma strict_mono_mono [dest?]:
  1026   assumes "strict_mono f"
  1027   shows "mono f"
  1028 proof (rule monoI)
  1029   fix x y
  1030   assume "x \<le> y"
  1031   show "f x \<le> f y"
  1032   proof (cases "x = y")
  1033     case True then show ?thesis by simp
  1034   next
  1035     case False with `x \<le> y` have "x < y" by simp
  1036     with assms strict_monoD have "f x < f y" by auto
  1037     then show ?thesis by simp
  1038   qed
  1039 qed
  1040 
  1041 end
  1042 
  1043 context linorder
  1044 begin
  1045 
  1046 lemma mono_invE:
  1047   fixes f :: "'a \<Rightarrow> 'b\<Colon>order"
  1048   assumes "mono f"
  1049   assumes "f x < f y"
  1050   obtains "x \<le> y"
  1051 proof
  1052   show "x \<le> y"
  1053   proof (rule ccontr)
  1054     assume "\<not> x \<le> y"
  1055     then have "y \<le> x" by simp
  1056     with `mono f` obtain "f y \<le> f x" by (rule monoE)
  1057     with `f x < f y` show False by simp
  1058   qed
  1059 qed
  1060 
  1061 lemma strict_mono_eq:
  1062   assumes "strict_mono f"
  1063   shows "f x = f y \<longleftrightarrow> x = y"
  1064 proof
  1065   assume "f x = f y"
  1066   show "x = y" proof (cases x y rule: linorder_cases)
  1067     case less with assms strict_monoD have "f x < f y" by auto
  1068     with `f x = f y` show ?thesis by simp
  1069   next
  1070     case equal then show ?thesis .
  1071   next
  1072     case greater with assms strict_monoD have "f y < f x" by auto
  1073     with `f x = f y` show ?thesis by simp
  1074   qed
  1075 qed simp
  1076 
  1077 lemma strict_mono_less_eq:
  1078   assumes "strict_mono f"
  1079   shows "f x \<le> f y \<longleftrightarrow> x \<le> y"
  1080 proof
  1081   assume "x \<le> y"
  1082   with assms strict_mono_mono monoD show "f x \<le> f y" by auto
  1083 next
  1084   assume "f x \<le> f y"
  1085   show "x \<le> y" proof (rule ccontr)
  1086     assume "\<not> x \<le> y" then have "y < x" by simp
  1087     with assms strict_monoD have "f y < f x" by auto
  1088     with `f x \<le> f y` show False by simp
  1089   qed
  1090 qed
  1091   
  1092 lemma strict_mono_less:
  1093   assumes "strict_mono f"
  1094   shows "f x < f y \<longleftrightarrow> x < y"
  1095   using assms
  1096     by (auto simp add: less_le Orderings.less_le strict_mono_eq strict_mono_less_eq)
  1097 
  1098 end
  1099 
  1100 
  1101 subsection {* min and max -- fundamental *}
  1102 
  1103 definition (in ord) min :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
  1104   "min a b = (if a \<le> b then a else b)"
  1105 
  1106 definition (in ord) max :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
  1107   "max a b = (if a \<le> b then b else a)"
  1108 
  1109 lemma min_absorb1: "x \<le> y \<Longrightarrow> min x y = x"
  1110   by (simp add: min_def)
  1111 
  1112 lemma max_absorb2: "x \<le> y \<Longrightarrow> max x y = y"
  1113   by (simp add: max_def)
  1114 
  1115 lemma min_absorb2: "(y\<Colon>'a\<Colon>order) \<le> x \<Longrightarrow> min x y = y"
  1116   by (simp add:min_def)
  1117 
  1118 lemma max_absorb1: "(y\<Colon>'a\<Colon>order) \<le> x \<Longrightarrow> max x y = x"
  1119   by (simp add: max_def)
  1120 
  1121 
  1122 subsection {* (Unique) top and bottom elements *}
  1123 
  1124 class bot =
  1125   fixes bot :: 'a ("\<bottom>")
  1126 
  1127 class order_bot = order + bot +
  1128   assumes bot_least: "\<bottom> \<le> a"
  1129 begin
  1130 
  1131 sublocale bot!: ordering_top greater_eq greater bot
  1132   by default (fact bot_least)
  1133 
  1134 lemma le_bot:
  1135   "a \<le> \<bottom> \<Longrightarrow> a = \<bottom>"
  1136   by (fact bot.extremum_uniqueI)
  1137 
  1138 lemma bot_unique:
  1139   "a \<le> \<bottom> \<longleftrightarrow> a = \<bottom>"
  1140   by (fact bot.extremum_unique)
  1141 
  1142 lemma not_less_bot:
  1143   "\<not> a < \<bottom>"
  1144   by (fact bot.extremum_strict)
  1145 
  1146 lemma bot_less:
  1147   "a \<noteq> \<bottom> \<longleftrightarrow> \<bottom> < a"
  1148   by (fact bot.not_eq_extremum)
  1149 
  1150 end
  1151 
  1152 class top =
  1153   fixes top :: 'a ("\<top>")
  1154 
  1155 class order_top = order + top +
  1156   assumes top_greatest: "a \<le> \<top>"
  1157 begin
  1158 
  1159 sublocale top!: ordering_top less_eq less top
  1160   by default (fact top_greatest)
  1161 
  1162 lemma top_le:
  1163   "\<top> \<le> a \<Longrightarrow> a = \<top>"
  1164   by (fact top.extremum_uniqueI)
  1165 
  1166 lemma top_unique:
  1167   "\<top> \<le> a \<longleftrightarrow> a = \<top>"
  1168   by (fact top.extremum_unique)
  1169 
  1170 lemma not_top_less:
  1171   "\<not> \<top> < a"
  1172   by (fact top.extremum_strict)
  1173 
  1174 lemma less_top:
  1175   "a \<noteq> \<top> \<longleftrightarrow> a < \<top>"
  1176   by (fact top.not_eq_extremum)
  1177 
  1178 end
  1179 
  1180 
  1181 subsection {* Dense orders *}
  1182 
  1183 class dense_order = order +
  1184   assumes dense: "x < y \<Longrightarrow> (\<exists>z. x < z \<and> z < y)"
  1185 
  1186 class dense_linorder = linorder + dense_order
  1187 begin
  1188 
  1189 lemma dense_le:
  1190   fixes y z :: 'a
  1191   assumes "\<And>x. x < y \<Longrightarrow> x \<le> z"
  1192   shows "y \<le> z"
  1193 proof (rule ccontr)
  1194   assume "\<not> ?thesis"
  1195   hence "z < y" by simp
  1196   from dense[OF this]
  1197   obtain x where "x < y" and "z < x" by safe
  1198   moreover have "x \<le> z" using assms[OF `x < y`] .
  1199   ultimately show False by auto
  1200 qed
  1201 
  1202 lemma dense_le_bounded:
  1203   fixes x y z :: 'a
  1204   assumes "x < y"
  1205   assumes *: "\<And>w. \<lbrakk> x < w ; w < y \<rbrakk> \<Longrightarrow> w \<le> z"
  1206   shows "y \<le> z"
  1207 proof (rule dense_le)
  1208   fix w assume "w < y"
  1209   from dense[OF `x < y`] obtain u where "x < u" "u < y" by safe
  1210   from linear[of u w]
  1211   show "w \<le> z"
  1212   proof (rule disjE)
  1213     assume "u \<le> w"
  1214     from less_le_trans[OF `x < u` `u \<le> w`] `w < y`
  1215     show "w \<le> z" by (rule *)
  1216   next
  1217     assume "w \<le> u"
  1218     from `w \<le> u` *[OF `x < u` `u < y`]
  1219     show "w \<le> z" by (rule order_trans)
  1220   qed
  1221 qed
  1222 
  1223 lemma dense_ge:
  1224   fixes y z :: 'a
  1225   assumes "\<And>x. z < x \<Longrightarrow> y \<le> x"
  1226   shows "y \<le> z"
  1227 proof (rule ccontr)
  1228   assume "\<not> ?thesis"
  1229   hence "z < y" by simp
  1230   from dense[OF this]
  1231   obtain x where "x < y" and "z < x" by safe
  1232   moreover have "y \<le> x" using assms[OF `z < x`] .
  1233   ultimately show False by auto
  1234 qed
  1235 
  1236 lemma dense_ge_bounded:
  1237   fixes x y z :: 'a
  1238   assumes "z < x"
  1239   assumes *: "\<And>w. \<lbrakk> z < w ; w < x \<rbrakk> \<Longrightarrow> y \<le> w"
  1240   shows "y \<le> z"
  1241 proof (rule dense_ge)
  1242   fix w assume "z < w"
  1243   from dense[OF `z < x`] obtain u where "z < u" "u < x" by safe
  1244   from linear[of u w]
  1245   show "y \<le> w"
  1246   proof (rule disjE)
  1247     assume "w \<le> u"
  1248     from `z < w` le_less_trans[OF `w \<le> u` `u < x`]
  1249     show "y \<le> w" by (rule *)
  1250   next
  1251     assume "u \<le> w"
  1252     from *[OF `z < u` `u < x`] `u \<le> w`
  1253     show "y \<le> w" by (rule order_trans)
  1254   qed
  1255 qed
  1256 
  1257 end
  1258 
  1259 class no_top = order + 
  1260   assumes gt_ex: "\<exists>y. x < y"
  1261 
  1262 class no_bot = order + 
  1263   assumes lt_ex: "\<exists>y. y < x"
  1264 
  1265 class unbounded_dense_linorder = dense_linorder + no_top + no_bot
  1266 
  1267 
  1268 subsection {* Wellorders *}
  1269 
  1270 class wellorder = linorder +
  1271   assumes less_induct [case_names less]: "(\<And>x. (\<And>y. y < x \<Longrightarrow> P y) \<Longrightarrow> P x) \<Longrightarrow> P a"
  1272 begin
  1273 
  1274 lemma wellorder_Least_lemma:
  1275   fixes k :: 'a
  1276   assumes "P k"
  1277   shows LeastI: "P (LEAST x. P x)" and Least_le: "(LEAST x. P x) \<le> k"
  1278 proof -
  1279   have "P (LEAST x. P x) \<and> (LEAST x. P x) \<le> k"
  1280   using assms proof (induct k rule: less_induct)
  1281     case (less x) then have "P x" by simp
  1282     show ?case proof (rule classical)
  1283       assume assm: "\<not> (P (LEAST a. P a) \<and> (LEAST a. P a) \<le> x)"
  1284       have "\<And>y. P y \<Longrightarrow> x \<le> y"
  1285       proof (rule classical)
  1286         fix y
  1287         assume "P y" and "\<not> x \<le> y"
  1288         with less have "P (LEAST a. P a)" and "(LEAST a. P a) \<le> y"
  1289           by (auto simp add: not_le)
  1290         with assm have "x < (LEAST a. P a)" and "(LEAST a. P a) \<le> y"
  1291           by auto
  1292         then show "x \<le> y" by auto
  1293       qed
  1294       with `P x` have Least: "(LEAST a. P a) = x"
  1295         by (rule Least_equality)
  1296       with `P x` show ?thesis by simp
  1297     qed
  1298   qed
  1299   then show "P (LEAST x. P x)" and "(LEAST x. P x) \<le> k" by auto
  1300 qed
  1301 
  1302 -- "The following 3 lemmas are due to Brian Huffman"
  1303 lemma LeastI_ex: "\<exists>x. P x \<Longrightarrow> P (Least P)"
  1304   by (erule exE) (erule LeastI)
  1305 
  1306 lemma LeastI2:
  1307   "P a \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> Q (Least P)"
  1308   by (blast intro: LeastI)
  1309 
  1310 lemma LeastI2_ex:
  1311   "\<exists>a. P a \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> Q (Least P)"
  1312   by (blast intro: LeastI_ex)
  1313 
  1314 lemma LeastI2_wellorder:
  1315   assumes "P a"
  1316   and "\<And>a. \<lbrakk> P a; \<forall>b. P b \<longrightarrow> a \<le> b \<rbrakk> \<Longrightarrow> Q a"
  1317   shows "Q (Least P)"
  1318 proof (rule LeastI2_order)
  1319   show "P (Least P)" using `P a` by (rule LeastI)
  1320 next
  1321   fix y assume "P y" thus "Least P \<le> y" by (rule Least_le)
  1322 next
  1323   fix x assume "P x" "\<forall>y. P y \<longrightarrow> x \<le> y" thus "Q x" by (rule assms(2))
  1324 qed
  1325 
  1326 lemma not_less_Least: "k < (LEAST x. P x) \<Longrightarrow> \<not> P k"
  1327 apply (simp (no_asm_use) add: not_le [symmetric])
  1328 apply (erule contrapos_nn)
  1329 apply (erule Least_le)
  1330 done
  1331 
  1332 end
  1333 
  1334 
  1335 subsection {* Order on @{typ bool} *}
  1336 
  1337 instantiation bool :: "{order_bot, order_top, linorder}"
  1338 begin
  1339 
  1340 definition
  1341   le_bool_def [simp]: "P \<le> Q \<longleftrightarrow> P \<longrightarrow> Q"
  1342 
  1343 definition
  1344   [simp]: "(P\<Colon>bool) < Q \<longleftrightarrow> \<not> P \<and> Q"
  1345 
  1346 definition
  1347   [simp]: "\<bottom> \<longleftrightarrow> False"
  1348 
  1349 definition
  1350   [simp]: "\<top> \<longleftrightarrow> True"
  1351 
  1352 instance proof
  1353 qed auto
  1354 
  1355 end
  1356 
  1357 lemma le_boolI: "(P \<Longrightarrow> Q) \<Longrightarrow> P \<le> Q"
  1358   by simp
  1359 
  1360 lemma le_boolI': "P \<longrightarrow> Q \<Longrightarrow> P \<le> Q"
  1361   by simp
  1362 
  1363 lemma le_boolE: "P \<le> Q \<Longrightarrow> P \<Longrightarrow> (Q \<Longrightarrow> R) \<Longrightarrow> R"
  1364   by simp
  1365 
  1366 lemma le_boolD: "P \<le> Q \<Longrightarrow> P \<longrightarrow> Q"
  1367   by simp
  1368 
  1369 lemma bot_boolE: "\<bottom> \<Longrightarrow> P"
  1370   by simp
  1371 
  1372 lemma top_boolI: \<top>
  1373   by simp
  1374 
  1375 lemma [code]:
  1376   "False \<le> b \<longleftrightarrow> True"
  1377   "True \<le> b \<longleftrightarrow> b"
  1378   "False < b \<longleftrightarrow> b"
  1379   "True < b \<longleftrightarrow> False"
  1380   by simp_all
  1381 
  1382 
  1383 subsection {* Order on @{typ "_ \<Rightarrow> _"} *}
  1384 
  1385 instantiation "fun" :: (type, ord) ord
  1386 begin
  1387 
  1388 definition
  1389   le_fun_def: "f \<le> g \<longleftrightarrow> (\<forall>x. f x \<le> g x)"
  1390 
  1391 definition
  1392   "(f\<Colon>'a \<Rightarrow> 'b) < g \<longleftrightarrow> f \<le> g \<and> \<not> (g \<le> f)"
  1393 
  1394 instance ..
  1395 
  1396 end
  1397 
  1398 instance "fun" :: (type, preorder) preorder proof
  1399 qed (auto simp add: le_fun_def less_fun_def
  1400   intro: order_trans antisym)
  1401 
  1402 instance "fun" :: (type, order) order proof
  1403 qed (auto simp add: le_fun_def intro: antisym)
  1404 
  1405 instantiation "fun" :: (type, bot) bot
  1406 begin
  1407 
  1408 definition
  1409   "\<bottom> = (\<lambda>x. \<bottom>)"
  1410 
  1411 instance ..
  1412 
  1413 end
  1414 
  1415 instantiation "fun" :: (type, order_bot) order_bot
  1416 begin
  1417 
  1418 lemma bot_apply [simp, code]:
  1419   "\<bottom> x = \<bottom>"
  1420   by (simp add: bot_fun_def)
  1421 
  1422 instance proof
  1423 qed (simp add: le_fun_def)
  1424 
  1425 end
  1426 
  1427 instantiation "fun" :: (type, top) top
  1428 begin
  1429 
  1430 definition
  1431   [no_atp]: "\<top> = (\<lambda>x. \<top>)"
  1432 
  1433 instance ..
  1434 
  1435 end
  1436 
  1437 instantiation "fun" :: (type, order_top) order_top
  1438 begin
  1439 
  1440 lemma top_apply [simp, code]:
  1441   "\<top> x = \<top>"
  1442   by (simp add: top_fun_def)
  1443 
  1444 instance proof
  1445 qed (simp add: le_fun_def)
  1446 
  1447 end
  1448 
  1449 lemma le_funI: "(\<And>x. f x \<le> g x) \<Longrightarrow> f \<le> g"
  1450   unfolding le_fun_def by simp
  1451 
  1452 lemma le_funE: "f \<le> g \<Longrightarrow> (f x \<le> g x \<Longrightarrow> P) \<Longrightarrow> P"
  1453   unfolding le_fun_def by simp
  1454 
  1455 lemma le_funD: "f \<le> g \<Longrightarrow> f x \<le> g x"
  1456   by (rule le_funE)
  1457 
  1458 
  1459 subsection {* Order on unary and binary predicates *}
  1460 
  1461 lemma predicate1I:
  1462   assumes PQ: "\<And>x. P x \<Longrightarrow> Q x"
  1463   shows "P \<le> Q"
  1464   apply (rule le_funI)
  1465   apply (rule le_boolI)
  1466   apply (rule PQ)
  1467   apply assumption
  1468   done
  1469 
  1470 lemma predicate1D:
  1471   "P \<le> Q \<Longrightarrow> P x \<Longrightarrow> Q x"
  1472   apply (erule le_funE)
  1473   apply (erule le_boolE)
  1474   apply assumption+
  1475   done
  1476 
  1477 lemma rev_predicate1D:
  1478   "P x \<Longrightarrow> P \<le> Q \<Longrightarrow> Q x"
  1479   by (rule predicate1D)
  1480 
  1481 lemma predicate2I:
  1482   assumes PQ: "\<And>x y. P x y \<Longrightarrow> Q x y"
  1483   shows "P \<le> Q"
  1484   apply (rule le_funI)+
  1485   apply (rule le_boolI)
  1486   apply (rule PQ)
  1487   apply assumption
  1488   done
  1489 
  1490 lemma predicate2D:
  1491   "P \<le> Q \<Longrightarrow> P x y \<Longrightarrow> Q x y"
  1492   apply (erule le_funE)+
  1493   apply (erule le_boolE)
  1494   apply assumption+
  1495   done
  1496 
  1497 lemma rev_predicate2D:
  1498   "P x y \<Longrightarrow> P \<le> Q \<Longrightarrow> Q x y"
  1499   by (rule predicate2D)
  1500 
  1501 lemma bot1E [no_atp]: "\<bottom> x \<Longrightarrow> P"
  1502   by (simp add: bot_fun_def)
  1503 
  1504 lemma bot2E: "\<bottom> x y \<Longrightarrow> P"
  1505   by (simp add: bot_fun_def)
  1506 
  1507 lemma top1I: "\<top> x"
  1508   by (simp add: top_fun_def)
  1509 
  1510 lemma top2I: "\<top> x y"
  1511   by (simp add: top_fun_def)
  1512 
  1513 
  1514 subsection {* Name duplicates *}
  1515 
  1516 lemmas order_eq_refl = preorder_class.eq_refl
  1517 lemmas order_less_irrefl = preorder_class.less_irrefl
  1518 lemmas order_less_imp_le = preorder_class.less_imp_le
  1519 lemmas order_less_not_sym = preorder_class.less_not_sym
  1520 lemmas order_less_asym = preorder_class.less_asym
  1521 lemmas order_less_trans = preorder_class.less_trans
  1522 lemmas order_le_less_trans = preorder_class.le_less_trans
  1523 lemmas order_less_le_trans = preorder_class.less_le_trans
  1524 lemmas order_less_imp_not_less = preorder_class.less_imp_not_less
  1525 lemmas order_less_imp_triv = preorder_class.less_imp_triv
  1526 lemmas order_less_asym' = preorder_class.less_asym'
  1527 
  1528 lemmas order_less_le = order_class.less_le
  1529 lemmas order_le_less = order_class.le_less
  1530 lemmas order_le_imp_less_or_eq = order_class.le_imp_less_or_eq
  1531 lemmas order_less_imp_not_eq = order_class.less_imp_not_eq
  1532 lemmas order_less_imp_not_eq2 = order_class.less_imp_not_eq2
  1533 lemmas order_neq_le_trans = order_class.neq_le_trans
  1534 lemmas order_le_neq_trans = order_class.le_neq_trans
  1535 lemmas order_antisym = order_class.antisym
  1536 lemmas order_eq_iff = order_class.eq_iff
  1537 lemmas order_antisym_conv = order_class.antisym_conv
  1538 
  1539 lemmas linorder_linear = linorder_class.linear
  1540 lemmas linorder_less_linear = linorder_class.less_linear
  1541 lemmas linorder_le_less_linear = linorder_class.le_less_linear
  1542 lemmas linorder_le_cases = linorder_class.le_cases
  1543 lemmas linorder_not_less = linorder_class.not_less
  1544 lemmas linorder_not_le = linorder_class.not_le
  1545 lemmas linorder_neq_iff = linorder_class.neq_iff
  1546 lemmas linorder_neqE = linorder_class.neqE
  1547 lemmas linorder_antisym_conv1 = linorder_class.antisym_conv1
  1548 lemmas linorder_antisym_conv2 = linorder_class.antisym_conv2
  1549 lemmas linorder_antisym_conv3 = linorder_class.antisym_conv3
  1550 
  1551 end
  1552