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