haftmann@28685: (* Title: HOL/Orderings.thy nipkow@15524: Author: Tobias Nipkow, Markus Wenzel, and Larry Paulson nipkow@15524: *) nipkow@15524: haftmann@25614: header {* Abstract orderings *} nipkow@15524: nipkow@15524: theory Orderings haftmann@35301: imports HOL wenzelm@32215: uses wenzelm@32215: "~~/src/Provers/order.ML" wenzelm@32215: "~~/src/Provers/quasi.ML" (* FIXME unused? *) nipkow@15524: begin nipkow@15524: haftmann@35092: subsection {* Syntactic orders *} haftmann@35092: haftmann@35092: class ord = haftmann@35092: fixes less_eq :: "'a \ 'a \ bool" haftmann@35092: and less :: "'a \ 'a \ bool" haftmann@35092: begin haftmann@35092: haftmann@35092: notation haftmann@35092: less_eq ("op <=") and haftmann@35092: less_eq ("(_/ <= _)" [51, 51] 50) and haftmann@35092: less ("op <") and haftmann@35092: less ("(_/ < _)" [51, 51] 50) haftmann@35092: haftmann@35092: notation (xsymbols) haftmann@35092: less_eq ("op \") and haftmann@35092: less_eq ("(_/ \ _)" [51, 51] 50) haftmann@35092: haftmann@35092: notation (HTML output) haftmann@35092: less_eq ("op \") and haftmann@35092: less_eq ("(_/ \ _)" [51, 51] 50) haftmann@35092: haftmann@35092: abbreviation (input) haftmann@35092: greater_eq (infix ">=" 50) where haftmann@35092: "x >= y \ y <= x" haftmann@35092: haftmann@35092: notation (input) haftmann@35092: greater_eq (infix "\" 50) haftmann@35092: haftmann@35092: abbreviation (input) haftmann@35092: greater (infix ">" 50) where haftmann@35092: "x > y \ y < x" haftmann@35092: haftmann@35092: end haftmann@35092: haftmann@35092: haftmann@27682: subsection {* Quasi orders *} nipkow@15524: haftmann@27682: class preorder = ord + haftmann@27682: assumes less_le_not_le: "x < y \ x \ y \ \ (y \ x)" haftmann@25062: and order_refl [iff]: "x \ x" haftmann@25062: and order_trans: "x \ y \ y \ z \ x \ z" haftmann@21248: begin haftmann@21248: nipkow@15524: text {* Reflexivity. *} nipkow@15524: haftmann@25062: lemma eq_refl: "x = y \ x \ y" nipkow@15524: -- {* This form is useful with the classical reasoner. *} nipkow@23212: by (erule ssubst) (rule order_refl) nipkow@15524: haftmann@25062: lemma less_irrefl [iff]: "\ x < x" haftmann@27682: by (simp add: less_le_not_le) haftmann@27682: haftmann@27682: lemma less_imp_le: "x < y \ x \ y" haftmann@27682: unfolding less_le_not_le by blast haftmann@27682: haftmann@27682: haftmann@27682: text {* Asymmetry. *} haftmann@27682: haftmann@27682: lemma less_not_sym: "x < y \ \ (y < x)" haftmann@27682: by (simp add: less_le_not_le) haftmann@27682: haftmann@27682: lemma less_asym: "x < y \ (\ P \ y < x) \ P" haftmann@27682: by (drule less_not_sym, erule contrapos_np) simp haftmann@27682: haftmann@27682: haftmann@27682: text {* Transitivity. *} haftmann@27682: haftmann@27682: lemma less_trans: "x < y \ y < z \ x < z" haftmann@27682: by (auto simp add: less_le_not_le intro: order_trans) haftmann@27682: haftmann@27682: lemma le_less_trans: "x \ y \ y < z \ x < z" haftmann@27682: by (auto simp add: less_le_not_le intro: order_trans) haftmann@27682: haftmann@27682: lemma less_le_trans: "x < y \ y \ z \ x < z" haftmann@27682: by (auto simp add: less_le_not_le intro: order_trans) haftmann@27682: haftmann@27682: haftmann@27682: text {* Useful for simplification, but too risky to include by default. *} haftmann@27682: haftmann@27682: lemma less_imp_not_less: "x < y \ (\ y < x) \ True" haftmann@27682: by (blast elim: less_asym) haftmann@27682: haftmann@27682: lemma less_imp_triv: "x < y \ (y < x \ P) \ True" haftmann@27682: by (blast elim: less_asym) haftmann@27682: haftmann@27682: haftmann@27682: text {* Transitivity rules for calculational reasoning *} haftmann@27682: haftmann@27682: lemma less_asym': "a < b \ b < a \ P" haftmann@27682: by (rule less_asym) haftmann@27682: haftmann@27682: haftmann@27682: text {* Dual order *} haftmann@27682: haftmann@27682: lemma dual_preorder: haftmann@36635: "class.preorder (op \) (op >)" haftmann@28823: proof qed (auto simp add: less_le_not_le intro: order_trans) haftmann@27682: haftmann@27682: end haftmann@27682: haftmann@27682: haftmann@27682: subsection {* Partial orders *} haftmann@27682: haftmann@27682: class order = preorder + haftmann@27682: assumes antisym: "x \ y \ y \ x \ x = y" haftmann@27682: begin haftmann@27682: haftmann@27682: text {* Reflexivity. *} haftmann@27682: haftmann@27682: lemma less_le: "x < y \ x \ y \ x \ y" haftmann@27682: by (auto simp add: less_le_not_le intro: antisym) nipkow@15524: haftmann@25062: lemma le_less: "x \ y \ x < y \ x = y" nipkow@15524: -- {* NOT suitable for iff, since it can cause PROOF FAILED. *} nipkow@23212: by (simp add: less_le) blast nipkow@15524: haftmann@25062: lemma le_imp_less_or_eq: "x \ y \ x < y \ x = y" nipkow@23212: unfolding less_le by blast nipkow@15524: haftmann@21329: haftmann@21329: text {* Useful for simplification, but too risky to include by default. *} haftmann@21329: haftmann@25062: lemma less_imp_not_eq: "x < y \ (x = y) \ False" nipkow@23212: by auto haftmann@21329: haftmann@25062: lemma less_imp_not_eq2: "x < y \ (y = x) \ False" nipkow@23212: by auto haftmann@21329: haftmann@21329: haftmann@21329: text {* Transitivity rules for calculational reasoning *} haftmann@21329: haftmann@25062: lemma neq_le_trans: "a \ b \ a \ b \ a < b" nipkow@23212: by (simp add: less_le) haftmann@21329: haftmann@25062: lemma le_neq_trans: "a \ b \ a \ b \ a < b" nipkow@23212: by (simp add: less_le) haftmann@21329: nipkow@15524: nipkow@15524: text {* Asymmetry. *} nipkow@15524: haftmann@25062: lemma eq_iff: "x = y \ x \ y \ y \ x" nipkow@23212: by (blast intro: antisym) nipkow@15524: haftmann@25062: lemma antisym_conv: "y \ x \ x \ y \ x = y" nipkow@23212: by (blast intro: antisym) nipkow@15524: haftmann@25062: lemma less_imp_neq: "x < y \ x \ y" nipkow@23212: by (erule contrapos_pn, erule subst, rule less_irrefl) haftmann@21248: haftmann@21083: haftmann@27107: text {* Least value operator *} haftmann@27107: haftmann@27299: definition (in ord) haftmann@27107: Least :: "('a \ bool) \ 'a" (binder "LEAST " 10) where haftmann@27107: "Least P = (THE x. P x \ (\y. P y \ x \ y))" haftmann@27107: haftmann@27107: lemma Least_equality: haftmann@27107: assumes "P x" haftmann@27107: and "\y. P y \ x \ y" haftmann@27107: shows "Least P = x" haftmann@27107: unfolding Least_def by (rule the_equality) haftmann@27107: (blast intro: assms antisym)+ haftmann@27107: haftmann@27107: lemma LeastI2_order: haftmann@27107: assumes "P x" haftmann@27107: and "\y. P y \ x \ y" haftmann@27107: and "\x. P x \ \y. P y \ x \ y \ Q x" haftmann@27107: shows "Q (Least P)" haftmann@27107: unfolding Least_def by (rule theI2) haftmann@27107: (blast intro: assms antisym)+ haftmann@27107: haftmann@27107: haftmann@26014: text {* Dual order *} haftmann@22916: haftmann@26014: lemma dual_order: haftmann@36635: "class.order (op \) (op >)" haftmann@27682: by (intro_locales, rule dual_preorder) (unfold_locales, rule antisym) haftmann@22916: haftmann@21248: end nipkow@15524: haftmann@21329: haftmann@21329: subsection {* Linear (total) orders *} haftmann@21329: haftmann@22316: class linorder = order + haftmann@25207: assumes linear: "x \ y \ y \ x" haftmann@21248: begin haftmann@21248: haftmann@25062: lemma less_linear: "x < y \ x = y \ y < x" nipkow@23212: unfolding less_le using less_le linear by blast haftmann@21248: haftmann@25062: lemma le_less_linear: "x \ y \ y < x" nipkow@23212: by (simp add: le_less less_linear) haftmann@21248: haftmann@21248: lemma le_cases [case_names le ge]: haftmann@25062: "(x \ y \ P) \ (y \ x \ P) \ P" nipkow@23212: using linear by blast haftmann@21248: haftmann@22384: lemma linorder_cases [case_names less equal greater]: haftmann@25062: "(x < y \ P) \ (x = y \ P) \ (y < x \ P) \ P" nipkow@23212: using less_linear by blast haftmann@21248: haftmann@25062: lemma not_less: "\ x < y \ y \ x" nipkow@23212: apply (simp add: less_le) nipkow@23212: using linear apply (blast intro: antisym) nipkow@23212: done nipkow@23212: nipkow@23212: lemma not_less_iff_gr_or_eq: haftmann@25062: "\(x < y) \ (x > y | x = y)" nipkow@23212: apply(simp add:not_less le_less) nipkow@23212: apply blast nipkow@23212: done nipkow@15524: haftmann@25062: lemma not_le: "\ x \ y \ y < x" nipkow@23212: apply (simp add: less_le) nipkow@23212: using linear apply (blast intro: antisym) nipkow@23212: done nipkow@15524: haftmann@25062: lemma neq_iff: "x \ y \ x < y \ y < x" nipkow@23212: by (cut_tac x = x and y = y in less_linear, auto) nipkow@15524: haftmann@25062: lemma neqE: "x \ y \ (x < y \ R) \ (y < x \ R) \ R" nipkow@23212: by (simp add: neq_iff) blast nipkow@15524: haftmann@25062: lemma antisym_conv1: "\ x < y \ x \ y \ x = y" nipkow@23212: by (blast intro: antisym dest: not_less [THEN iffD1]) nipkow@15524: haftmann@25062: lemma antisym_conv2: "x \ y \ \ x < y \ x = y" nipkow@23212: by (blast intro: antisym dest: not_less [THEN iffD1]) nipkow@15524: haftmann@25062: lemma antisym_conv3: "\ y < x \ \ x < y \ x = y" nipkow@23212: by (blast intro: antisym dest: not_less [THEN iffD1]) nipkow@15524: haftmann@25062: lemma leI: "\ x < y \ y \ x" nipkow@23212: unfolding not_less . paulson@16796: haftmann@25062: lemma leD: "y \ x \ \ x < y" nipkow@23212: unfolding not_less . paulson@16796: paulson@16796: (*FIXME inappropriate name (or delete altogether)*) haftmann@25062: lemma not_leE: "\ y \ x \ x < y" nipkow@23212: unfolding not_le . haftmann@21248: haftmann@22916: haftmann@26014: text {* Dual order *} haftmann@22916: haftmann@26014: lemma dual_linorder: haftmann@36635: "class.linorder (op \) (op >)" haftmann@36635: by (rule class.linorder.intro, rule dual_order) (unfold_locales, rule linear) haftmann@22916: haftmann@22916: haftmann@23881: text {* min/max *} haftmann@23881: haftmann@27299: definition (in ord) min :: "'a \ 'a \ 'a" where haftmann@37767: "min a b = (if a \ b then a else b)" haftmann@23881: haftmann@27299: definition (in ord) max :: "'a \ 'a \ 'a" where haftmann@37767: "max a b = (if a \ b then b else a)" haftmann@22384: haftmann@21383: lemma min_le_iff_disj: haftmann@25062: "min x y \ z \ x \ z \ y \ z" nipkow@23212: unfolding min_def using linear by (auto intro: order_trans) haftmann@21383: haftmann@21383: lemma le_max_iff_disj: haftmann@25062: "z \ max x y \ z \ x \ z \ y" nipkow@23212: unfolding max_def using linear by (auto intro: order_trans) haftmann@21383: haftmann@21383: lemma min_less_iff_disj: haftmann@25062: "min x y < z \ x < z \ y < z" nipkow@23212: unfolding min_def le_less using less_linear by (auto intro: less_trans) haftmann@21383: haftmann@21383: lemma less_max_iff_disj: haftmann@25062: "z < max x y \ z < x \ z < y" nipkow@23212: unfolding max_def le_less using less_linear by (auto intro: less_trans) haftmann@21383: haftmann@21383: lemma min_less_iff_conj [simp]: haftmann@25062: "z < min x y \ z < x \ z < y" nipkow@23212: unfolding min_def le_less using less_linear by (auto intro: less_trans) haftmann@21383: haftmann@21383: lemma max_less_iff_conj [simp]: haftmann@25062: "max x y < z \ x < z \ y < z" nipkow@23212: unfolding max_def le_less using less_linear by (auto intro: less_trans) haftmann@21383: blanchet@35828: lemma split_min [no_atp]: haftmann@25062: "P (min i j) \ (i \ j \ P i) \ (\ i \ j \ P j)" nipkow@23212: by (simp add: min_def) haftmann@21383: blanchet@35828: lemma split_max [no_atp]: haftmann@25062: "P (max i j) \ (i \ j \ P j) \ (\ i \ j \ P i)" nipkow@23212: by (simp add: max_def) haftmann@21383: haftmann@21248: end haftmann@21248: haftmann@23948: haftmann@21083: subsection {* Reasoning tools setup *} haftmann@21083: haftmann@21091: ML {* haftmann@21091: ballarin@24641: signature ORDERS = ballarin@24641: sig ballarin@24641: val print_structures: Proof.context -> unit ballarin@24641: val setup: theory -> theory wenzelm@32215: val order_tac: Proof.context -> thm list -> int -> tactic ballarin@24641: end; haftmann@21091: ballarin@24641: structure Orders: ORDERS = haftmann@21248: struct ballarin@24641: ballarin@24641: (** Theory and context data **) ballarin@24641: ballarin@24641: fun struct_eq ((s1: string, ts1), (s2, ts2)) = ballarin@24641: (s1 = s2) andalso eq_list (op aconv) (ts1, ts2); ballarin@24641: wenzelm@33519: structure Data = Generic_Data ballarin@24641: ( ballarin@24641: type T = ((string * term list) * Order_Tac.less_arith) list; ballarin@24641: (* Order structures: ballarin@24641: identifier of the structure, list of operations and record of theorems ballarin@24641: needed to set up the transitivity reasoner, ballarin@24641: identifier and operations identify the structure uniquely. *) ballarin@24641: val empty = []; ballarin@24641: val extend = I; wenzelm@33519: fun merge data = AList.join struct_eq (K fst) data; ballarin@24641: ); ballarin@24641: ballarin@24641: fun print_structures ctxt = ballarin@24641: let ballarin@24641: val structs = Data.get (Context.Proof ctxt); ballarin@24641: fun pretty_term t = Pretty.block wenzelm@24920: [Pretty.quote (Syntax.pretty_term ctxt t), Pretty.brk 1, ballarin@24641: Pretty.str "::", Pretty.brk 1, wenzelm@24920: Pretty.quote (Syntax.pretty_typ ctxt (type_of t))]; ballarin@24641: fun pretty_struct ((s, ts), _) = Pretty.block ballarin@24641: [Pretty.str s, Pretty.str ":", Pretty.brk 1, ballarin@24641: Pretty.enclose "(" ")" (Pretty.breaks (map pretty_term ts))]; ballarin@24641: in ballarin@24641: Pretty.writeln (Pretty.big_list "Order structures:" (map pretty_struct structs)) ballarin@24641: end; ballarin@24641: ballarin@24641: ballarin@24641: (** Method **) haftmann@21091: wenzelm@32215: fun struct_tac ((s, [eq, le, less]), thms) ctxt prems = ballarin@24641: let berghofe@30107: fun decomp thy (@{const Trueprop} $ t) = ballarin@24641: let ballarin@24641: fun excluded t = ballarin@24641: (* exclude numeric types: linear arithmetic subsumes transitivity *) ballarin@24641: let val T = type_of t ballarin@24641: in wenzelm@32960: T = HOLogic.natT orelse T = HOLogic.intT orelse T = HOLogic.realT ballarin@24641: end; wenzelm@32960: fun rel (bin_op $ t1 $ t2) = ballarin@24641: if excluded t1 then NONE ballarin@24641: else if Pattern.matches thy (eq, bin_op) then SOME (t1, "=", t2) ballarin@24641: else if Pattern.matches thy (le, bin_op) then SOME (t1, "<=", t2) ballarin@24641: else if Pattern.matches thy (less, bin_op) then SOME (t1, "<", t2) ballarin@24641: else NONE wenzelm@32960: | rel _ = NONE; wenzelm@32960: fun dec (Const (@{const_name Not}, _) $ t) = (case rel t wenzelm@32960: of NONE => NONE wenzelm@32960: | SOME (t1, rel, t2) => SOME (t1, "~" ^ rel, t2)) ballarin@24741: | dec x = rel x; berghofe@30107: in dec t end berghofe@30107: | decomp thy _ = NONE; ballarin@24641: in ballarin@24641: case s of wenzelm@32215: "order" => Order_Tac.partial_tac decomp thms ctxt prems wenzelm@32215: | "linorder" => Order_Tac.linear_tac decomp thms ctxt prems ballarin@24641: | _ => error ("Unknown kind of order `" ^ s ^ "' encountered in transitivity reasoner.") ballarin@24641: end ballarin@24641: wenzelm@32215: fun order_tac ctxt prems = wenzelm@32215: FIRST' (map (fn s => CHANGED o struct_tac s ctxt prems) (Data.get (Context.Proof ctxt))); ballarin@24641: ballarin@24641: ballarin@24641: (** Attribute **) ballarin@24641: ballarin@24641: fun add_struct_thm s tag = ballarin@24641: Thm.declaration_attribute ballarin@24641: (fn thm => Data.map (AList.map_default struct_eq (s, Order_Tac.empty TrueI) (Order_Tac.update tag thm))); ballarin@24641: fun del_struct s = ballarin@24641: Thm.declaration_attribute ballarin@24641: (fn _ => Data.map (AList.delete struct_eq s)); ballarin@24641: wenzelm@30722: val attrib_setup = wenzelm@30722: Attrib.setup @{binding order} wenzelm@30722: (Scan.lift ((Args.add -- Args.name >> (fn (_, s) => SOME s) || Args.del >> K NONE) --| wenzelm@30722: Args.colon (* FIXME || Scan.succeed true *) ) -- Scan.lift Args.name -- wenzelm@30722: Scan.repeat Args.term wenzelm@30722: >> (fn ((SOME tag, n), ts) => add_struct_thm (n, ts) tag wenzelm@30722: | ((NONE, n), ts) => del_struct (n, ts))) wenzelm@30722: "theorems controlling transitivity reasoner"; ballarin@24641: ballarin@24641: ballarin@24641: (** Diagnostic command **) ballarin@24641: wenzelm@24867: val _ = wenzelm@36960: Outer_Syntax.improper_command "print_orders" wenzelm@36960: "print order structures available to transitivity reasoner" Keyword.diag wenzelm@30806: (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_context o wenzelm@30806: Toplevel.keep (print_structures o Toplevel.context_of))); ballarin@24641: ballarin@24641: ballarin@24641: (** Setup **) ballarin@24641: wenzelm@24867: val setup = wenzelm@32215: Method.setup @{binding order} (Scan.succeed (fn ctxt => SIMPLE_METHOD' (order_tac ctxt []))) wenzelm@30722: "transitivity reasoner" #> wenzelm@30722: attrib_setup; haftmann@21091: haftmann@21091: end; ballarin@24641: haftmann@21091: *} haftmann@21091: ballarin@24641: setup Orders.setup ballarin@24641: ballarin@24641: ballarin@24641: text {* Declarations to set up transitivity reasoner of partial and linear orders. *} ballarin@24641: haftmann@25076: context order haftmann@25076: begin haftmann@25076: ballarin@24641: (* The type constraint on @{term op =} below is necessary since the operation ballarin@24641: is not a parameter of the locale. *) haftmann@25076: haftmann@27689: declare less_irrefl [THEN notE, order add less_reflE: order "op = :: 'a \ 'a \ bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare order_refl [order add le_refl: order "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare less_imp_le [order add less_imp_le: order "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare antisym [order add eqI: order "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare eq_refl [order add eqD1: order "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare sym [THEN eq_refl, order add eqD2: order "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare less_trans [order add less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare less_le_trans [order add less_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare le_less_trans [order add le_less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare order_trans [order add le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare le_neq_trans [order add le_neq_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare neq_le_trans [order add neq_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare less_imp_neq [order add less_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare not_sym [order add not_sym: order "op = :: 'a => 'a => bool" "op <=" "op <"] ballarin@24641: haftmann@25076: end haftmann@25076: haftmann@25076: context linorder haftmann@25076: begin ballarin@24641: haftmann@27689: declare [[order del: order "op = :: 'a => 'a => bool" "op <=" "op <"]] haftmann@27689: haftmann@27689: declare less_irrefl [THEN notE, order add less_reflE: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare order_refl [order add le_refl: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare less_imp_le [order add less_imp_le: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare not_less [THEN iffD2, order add not_lessI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare not_le [THEN iffD2, order add not_leI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare not_less [THEN iffD1, order add not_lessD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare not_le [THEN iffD1, order add not_leD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare antisym [order add eqI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare eq_refl [order add eqD1: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@25076: haftmann@27689: declare sym [THEN eq_refl, order add eqD2: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare less_trans [order add less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare less_le_trans [order add less_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare le_less_trans [order add le_less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare order_trans [order add le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare le_neq_trans [order add le_neq_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare neq_le_trans [order add neq_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare less_imp_neq [order add less_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] haftmann@27689: haftmann@27689: declare not_sym [order add not_sym: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] ballarin@24641: haftmann@25076: end haftmann@25076: ballarin@24641: haftmann@21083: setup {* haftmann@21083: let haftmann@21083: wenzelm@44058: fun prp t thm = Thm.prop_of thm = t; (* FIXME aconv!? *) nipkow@15524: haftmann@21083: fun prove_antisym_le sg ss ((le as Const(_,T)) $ r $ s) = wenzelm@43597: let val prems = Simplifier.prems_of ss; haftmann@22916: val less = Const (@{const_name less}, T); haftmann@21083: val t = HOLogic.mk_Trueprop(le $ s $ r); haftmann@21083: in case find_first (prp t) prems of haftmann@21083: NONE => haftmann@21083: let val t = HOLogic.mk_Trueprop(HOLogic.Not $ (less $ r $ s)) haftmann@21083: in case find_first (prp t) prems of haftmann@21083: NONE => NONE haftmann@24422: | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv1})) haftmann@21083: end haftmann@24422: | SOME thm => SOME(mk_meta_eq(thm RS @{thm order_class.antisym_conv})) haftmann@21083: end haftmann@21083: handle THM _ => NONE; nipkow@15524: haftmann@21083: fun prove_antisym_less sg ss (NotC $ ((less as Const(_,T)) $ r $ s)) = wenzelm@43597: let val prems = Simplifier.prems_of ss; haftmann@22916: val le = Const (@{const_name less_eq}, T); haftmann@21083: val t = HOLogic.mk_Trueprop(le $ r $ s); haftmann@21083: in case find_first (prp t) prems of haftmann@21083: NONE => haftmann@21083: let val t = HOLogic.mk_Trueprop(NotC $ (less $ s $ r)) haftmann@21083: in case find_first (prp t) prems of haftmann@21083: NONE => NONE haftmann@24422: | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv3})) haftmann@21083: end haftmann@24422: | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv2})) haftmann@21083: end haftmann@21083: handle THM _ => NONE; nipkow@15524: haftmann@21248: fun add_simprocs procs thy = wenzelm@42795: Simplifier.map_simpset_global (fn ss => ss haftmann@21248: addsimprocs (map (fn (name, raw_ts, proc) => wenzelm@38715: Simplifier.simproc_global thy name raw_ts proc) procs)) thy; wenzelm@42795: wenzelm@26496: fun add_solver name tac = wenzelm@42795: Simplifier.map_simpset_global (fn ss => ss addSolver wenzelm@43597: mk_solver name (fn ss => tac (Simplifier.the_context ss) (Simplifier.prems_of ss))); haftmann@21083: haftmann@21083: in haftmann@21248: add_simprocs [ haftmann@21248: ("antisym le", ["(x::'a::order) <= y"], prove_antisym_le), haftmann@21248: ("antisym less", ["~ (x::'a::linorder) < y"], prove_antisym_less) haftmann@21248: ] ballarin@24641: #> add_solver "Transitivity" Orders.order_tac haftmann@21248: (* Adding the transitivity reasoners also as safe solvers showed a slight haftmann@21248: speed up, but the reasoning strength appears to be not higher (at least haftmann@21248: no breaking of additional proofs in the entire HOL distribution, as haftmann@21248: of 5 March 2004, was observed). *) haftmann@21083: end haftmann@21083: *} nipkow@15524: nipkow@15524: haftmann@21083: subsection {* Bounded quantifiers *} haftmann@21083: haftmann@21083: syntax wenzelm@21180: "_All_less" :: "[idt, 'a, bool] => bool" ("(3ALL _<_./ _)" [0, 0, 10] 10) wenzelm@21180: "_Ex_less" :: "[idt, 'a, bool] => bool" ("(3EX _<_./ _)" [0, 0, 10] 10) wenzelm@21180: "_All_less_eq" :: "[idt, 'a, bool] => bool" ("(3ALL _<=_./ _)" [0, 0, 10] 10) wenzelm@21180: "_Ex_less_eq" :: "[idt, 'a, bool] => bool" ("(3EX _<=_./ _)" [0, 0, 10] 10) haftmann@21083: wenzelm@21180: "_All_greater" :: "[idt, 'a, bool] => bool" ("(3ALL _>_./ _)" [0, 0, 10] 10) wenzelm@21180: "_Ex_greater" :: "[idt, 'a, bool] => bool" ("(3EX _>_./ _)" [0, 0, 10] 10) wenzelm@21180: "_All_greater_eq" :: "[idt, 'a, bool] => bool" ("(3ALL _>=_./ _)" [0, 0, 10] 10) wenzelm@21180: "_Ex_greater_eq" :: "[idt, 'a, bool] => bool" ("(3EX _>=_./ _)" [0, 0, 10] 10) haftmann@21083: haftmann@21083: syntax (xsymbols) wenzelm@21180: "_All_less" :: "[idt, 'a, bool] => bool" ("(3\_<_./ _)" [0, 0, 10] 10) wenzelm@21180: "_Ex_less" :: "[idt, 'a, bool] => bool" ("(3\_<_./ _)" [0, 0, 10] 10) wenzelm@21180: "_All_less_eq" :: "[idt, 'a, bool] => bool" ("(3\_\_./ _)" [0, 0, 10] 10) wenzelm@21180: "_Ex_less_eq" :: "[idt, 'a, bool] => bool" ("(3\_\_./ _)" [0, 0, 10] 10) haftmann@21083: wenzelm@21180: "_All_greater" :: "[idt, 'a, bool] => bool" ("(3\_>_./ _)" [0, 0, 10] 10) wenzelm@21180: "_Ex_greater" :: "[idt, 'a, bool] => bool" ("(3\_>_./ _)" [0, 0, 10] 10) wenzelm@21180: "_All_greater_eq" :: "[idt, 'a, bool] => bool" ("(3\_\_./ _)" [0, 0, 10] 10) wenzelm@21180: "_Ex_greater_eq" :: "[idt, 'a, bool] => bool" ("(3\_\_./ _)" [0, 0, 10] 10) haftmann@21083: haftmann@21083: syntax (HOL) wenzelm@21180: "_All_less" :: "[idt, 'a, bool] => bool" ("(3! _<_./ _)" [0, 0, 10] 10) wenzelm@21180: "_Ex_less" :: "[idt, 'a, bool] => bool" ("(3? _<_./ _)" [0, 0, 10] 10) wenzelm@21180: "_All_less_eq" :: "[idt, 'a, bool] => bool" ("(3! _<=_./ _)" [0, 0, 10] 10) wenzelm@21180: "_Ex_less_eq" :: "[idt, 'a, bool] => bool" ("(3? _<=_./ _)" [0, 0, 10] 10) haftmann@21083: haftmann@21083: syntax (HTML output) wenzelm@21180: "_All_less" :: "[idt, 'a, bool] => bool" ("(3\_<_./ _)" [0, 0, 10] 10) wenzelm@21180: "_Ex_less" :: "[idt, 'a, bool] => bool" ("(3\_<_./ _)" [0, 0, 10] 10) wenzelm@21180: "_All_less_eq" :: "[idt, 'a, bool] => bool" ("(3\_\_./ _)" [0, 0, 10] 10) wenzelm@21180: "_Ex_less_eq" :: "[idt, 'a, bool] => bool" ("(3\_\_./ _)" [0, 0, 10] 10) haftmann@21083: wenzelm@21180: "_All_greater" :: "[idt, 'a, bool] => bool" ("(3\_>_./ _)" [0, 0, 10] 10) wenzelm@21180: "_Ex_greater" :: "[idt, 'a, bool] => bool" ("(3\_>_./ _)" [0, 0, 10] 10) wenzelm@21180: "_All_greater_eq" :: "[idt, 'a, bool] => bool" ("(3\_\_./ _)" [0, 0, 10] 10) wenzelm@21180: "_Ex_greater_eq" :: "[idt, 'a, bool] => bool" ("(3\_\_./ _)" [0, 0, 10] 10) haftmann@21083: haftmann@21083: translations haftmann@21083: "ALL x "ALL x. x < y \ P" haftmann@21083: "EX x "EX x. x < y \ P" haftmann@21083: "ALL x<=y. P" => "ALL x. x <= y \ P" haftmann@21083: "EX x<=y. P" => "EX x. x <= y \ P" haftmann@21083: "ALL x>y. P" => "ALL x. x > y \ P" haftmann@21083: "EX x>y. P" => "EX x. x > y \ P" haftmann@21083: "ALL x>=y. P" => "ALL x. x >= y \ P" haftmann@21083: "EX x>=y. P" => "EX x. x >= y \ P" haftmann@21083: haftmann@21083: print_translation {* haftmann@21083: let wenzelm@42287: val All_binder = Mixfix.binder_name @{const_syntax All}; wenzelm@42287: val Ex_binder = Mixfix.binder_name @{const_syntax Ex}; haftmann@38786: val impl = @{const_syntax HOL.implies}; haftmann@38795: val conj = @{const_syntax HOL.conj}; haftmann@22916: val less = @{const_syntax less}; haftmann@22916: val less_eq = @{const_syntax less_eq}; wenzelm@21180: wenzelm@21180: val trans = wenzelm@35115: [((All_binder, impl, less), wenzelm@35115: (@{syntax_const "_All_less"}, @{syntax_const "_All_greater"})), wenzelm@35115: ((All_binder, impl, less_eq), wenzelm@35115: (@{syntax_const "_All_less_eq"}, @{syntax_const "_All_greater_eq"})), wenzelm@35115: ((Ex_binder, conj, less), wenzelm@35115: (@{syntax_const "_Ex_less"}, @{syntax_const "_Ex_greater"})), wenzelm@35115: ((Ex_binder, conj, less_eq), wenzelm@35115: (@{syntax_const "_Ex_less_eq"}, @{syntax_const "_Ex_greater_eq"}))]; wenzelm@21180: wenzelm@35115: fun matches_bound v t = wenzelm@35115: (case t of wenzelm@35364: Const (@{syntax_const "_bound"}, _) $ Free (v', _) => v = v' wenzelm@35115: | _ => false); wenzelm@35115: fun contains_var v = Term.exists_subterm (fn Free (x, _) => x = v | _ => false); wenzelm@42284: fun mk v c n P = Syntax.const c $ Syntax_Trans.mark_bound v $ n $ P; wenzelm@21180: wenzelm@21180: fun tr' q = (q, wenzelm@35364: fn [Const (@{syntax_const "_bound"}, _) $ Free (v, _), wenzelm@35364: Const (c, _) $ (Const (d, _) $ t $ u) $ P] => wenzelm@35115: (case AList.lookup (op =) trans (q, c, d) of wenzelm@35115: NONE => raise Match wenzelm@35115: | SOME (l, g) => wenzelm@35115: if matches_bound v t andalso not (contains_var v u) then mk v l u P wenzelm@35115: else if matches_bound v u andalso not (contains_var v t) then mk v g t P wenzelm@35115: else raise Match) wenzelm@21180: | _ => raise Match); wenzelm@21524: in [tr' All_binder, tr' Ex_binder] end haftmann@21083: *} haftmann@21083: haftmann@21083: haftmann@21383: subsection {* Transitivity reasoning *} haftmann@21383: haftmann@25193: context ord haftmann@25193: begin haftmann@21383: haftmann@25193: lemma ord_le_eq_trans: "a \ b \ b = c \ a \ c" haftmann@25193: by (rule subst) haftmann@21383: haftmann@25193: lemma ord_eq_le_trans: "a = b \ b \ c \ a \ c" haftmann@25193: by (rule ssubst) haftmann@21383: haftmann@25193: lemma ord_less_eq_trans: "a < b \ b = c \ a < c" haftmann@25193: by (rule subst) haftmann@25193: haftmann@25193: lemma ord_eq_less_trans: "a = b \ b < c \ a < c" haftmann@25193: by (rule ssubst) haftmann@25193: haftmann@25193: end haftmann@21383: haftmann@21383: lemma order_less_subst2: "(a::'a::order) < b ==> f b < (c::'c::order) ==> haftmann@21383: (!!x y. x < y ==> f x < f y) ==> f a < c" haftmann@21383: proof - haftmann@21383: assume r: "!!x y. x < y ==> f x < f y" haftmann@21383: assume "a < b" hence "f a < f b" by (rule r) haftmann@21383: also assume "f b < c" haftmann@34250: finally (less_trans) show ?thesis . haftmann@21383: qed haftmann@21383: haftmann@21383: lemma order_less_subst1: "(a::'a::order) < f b ==> (b::'b::order) < c ==> haftmann@21383: (!!x y. x < y ==> f x < f y) ==> a < f c" haftmann@21383: proof - haftmann@21383: assume r: "!!x y. x < y ==> f x < f y" haftmann@21383: assume "a < f b" haftmann@21383: also assume "b < c" hence "f b < f c" by (rule r) haftmann@34250: finally (less_trans) show ?thesis . haftmann@21383: qed haftmann@21383: haftmann@21383: lemma order_le_less_subst2: "(a::'a::order) <= b ==> f b < (c::'c::order) ==> haftmann@21383: (!!x y. x <= y ==> f x <= f y) ==> f a < c" haftmann@21383: proof - haftmann@21383: assume r: "!!x y. x <= y ==> f x <= f y" haftmann@21383: assume "a <= b" hence "f a <= f b" by (rule r) haftmann@21383: also assume "f b < c" haftmann@34250: finally (le_less_trans) show ?thesis . haftmann@21383: qed haftmann@21383: haftmann@21383: lemma order_le_less_subst1: "(a::'a::order) <= f b ==> (b::'b::order) < c ==> haftmann@21383: (!!x y. x < y ==> f x < f y) ==> a < f c" haftmann@21383: proof - haftmann@21383: assume r: "!!x y. x < y ==> f x < f y" haftmann@21383: assume "a <= f b" haftmann@21383: also assume "b < c" hence "f b < f c" by (rule r) haftmann@34250: finally (le_less_trans) show ?thesis . haftmann@21383: qed haftmann@21383: haftmann@21383: lemma order_less_le_subst2: "(a::'a::order) < b ==> f b <= (c::'c::order) ==> haftmann@21383: (!!x y. x < y ==> f x < f y) ==> f a < c" haftmann@21383: proof - haftmann@21383: assume r: "!!x y. x < y ==> f x < f y" haftmann@21383: assume "a < b" hence "f a < f b" by (rule r) haftmann@21383: also assume "f b <= c" haftmann@34250: finally (less_le_trans) show ?thesis . haftmann@21383: qed haftmann@21383: haftmann@21383: lemma order_less_le_subst1: "(a::'a::order) < f b ==> (b::'b::order) <= c ==> haftmann@21383: (!!x y. x <= y ==> f x <= f y) ==> a < f c" haftmann@21383: proof - haftmann@21383: assume r: "!!x y. x <= y ==> f x <= f y" haftmann@21383: assume "a < f b" haftmann@21383: also assume "b <= c" hence "f b <= f c" by (rule r) haftmann@34250: finally (less_le_trans) show ?thesis . haftmann@21383: qed haftmann@21383: haftmann@21383: lemma order_subst1: "(a::'a::order) <= f b ==> (b::'b::order) <= c ==> haftmann@21383: (!!x y. x <= y ==> f x <= f y) ==> a <= f c" haftmann@21383: proof - haftmann@21383: assume r: "!!x y. x <= y ==> f x <= f y" haftmann@21383: assume "a <= f b" haftmann@21383: also assume "b <= c" hence "f b <= f c" by (rule r) haftmann@21383: finally (order_trans) show ?thesis . haftmann@21383: qed haftmann@21383: haftmann@21383: lemma order_subst2: "(a::'a::order) <= b ==> f b <= (c::'c::order) ==> haftmann@21383: (!!x y. x <= y ==> f x <= f y) ==> f a <= c" haftmann@21383: proof - haftmann@21383: assume r: "!!x y. x <= y ==> f x <= f y" haftmann@21383: assume "a <= b" hence "f a <= f b" by (rule r) haftmann@21383: also assume "f b <= c" haftmann@21383: finally (order_trans) show ?thesis . haftmann@21383: qed haftmann@21383: haftmann@21383: lemma ord_le_eq_subst: "a <= b ==> f b = c ==> haftmann@21383: (!!x y. x <= y ==> f x <= f y) ==> f a <= c" haftmann@21383: proof - haftmann@21383: assume r: "!!x y. x <= y ==> f x <= f y" haftmann@21383: assume "a <= b" hence "f a <= f b" by (rule r) haftmann@21383: also assume "f b = c" haftmann@21383: finally (ord_le_eq_trans) show ?thesis . haftmann@21383: qed haftmann@21383: haftmann@21383: lemma ord_eq_le_subst: "a = f b ==> b <= c ==> haftmann@21383: (!!x y. x <= y ==> f x <= f y) ==> a <= f c" haftmann@21383: proof - haftmann@21383: assume r: "!!x y. x <= y ==> f x <= f y" haftmann@21383: assume "a = f b" haftmann@21383: also assume "b <= c" hence "f b <= f c" by (rule r) haftmann@21383: finally (ord_eq_le_trans) show ?thesis . haftmann@21383: qed haftmann@21383: haftmann@21383: lemma ord_less_eq_subst: "a < b ==> f b = c ==> haftmann@21383: (!!x y. x < y ==> f x < f y) ==> f a < c" haftmann@21383: proof - haftmann@21383: assume r: "!!x y. x < y ==> f x < f y" haftmann@21383: assume "a < b" hence "f a < f b" by (rule r) haftmann@21383: also assume "f b = c" haftmann@21383: finally (ord_less_eq_trans) show ?thesis . haftmann@21383: qed haftmann@21383: haftmann@21383: lemma ord_eq_less_subst: "a = f b ==> b < c ==> haftmann@21383: (!!x y. x < y ==> f x < f y) ==> a < f c" haftmann@21383: proof - haftmann@21383: assume r: "!!x y. x < y ==> f x < f y" haftmann@21383: assume "a = f b" haftmann@21383: also assume "b < c" hence "f b < f c" by (rule r) haftmann@21383: finally (ord_eq_less_trans) show ?thesis . haftmann@21383: qed haftmann@21383: haftmann@21383: text {* haftmann@21383: Note that this list of rules is in reverse order of priorities. haftmann@21383: *} haftmann@21383: haftmann@27682: lemmas [trans] = haftmann@21383: order_less_subst2 haftmann@21383: order_less_subst1 haftmann@21383: order_le_less_subst2 haftmann@21383: order_le_less_subst1 haftmann@21383: order_less_le_subst2 haftmann@21383: order_less_le_subst1 haftmann@21383: order_subst2 haftmann@21383: order_subst1 haftmann@21383: ord_le_eq_subst haftmann@21383: ord_eq_le_subst haftmann@21383: ord_less_eq_subst haftmann@21383: ord_eq_less_subst haftmann@21383: forw_subst haftmann@21383: back_subst haftmann@21383: rev_mp haftmann@21383: mp haftmann@27682: haftmann@27682: lemmas (in order) [trans] = haftmann@27682: neq_le_trans haftmann@27682: le_neq_trans haftmann@27682: haftmann@27682: lemmas (in preorder) [trans] = haftmann@27682: less_trans haftmann@27682: less_asym' haftmann@27682: le_less_trans haftmann@27682: less_le_trans haftmann@21383: order_trans haftmann@27682: haftmann@27682: lemmas (in order) [trans] = haftmann@27682: antisym haftmann@27682: haftmann@27682: lemmas (in ord) [trans] = haftmann@27682: ord_le_eq_trans haftmann@27682: ord_eq_le_trans haftmann@27682: ord_less_eq_trans haftmann@27682: ord_eq_less_trans haftmann@27682: haftmann@27682: lemmas [trans] = haftmann@27682: trans haftmann@27682: haftmann@27682: lemmas order_trans_rules = haftmann@27682: order_less_subst2 haftmann@27682: order_less_subst1 haftmann@27682: order_le_less_subst2 haftmann@27682: order_le_less_subst1 haftmann@27682: order_less_le_subst2 haftmann@27682: order_less_le_subst1 haftmann@27682: order_subst2 haftmann@27682: order_subst1 haftmann@27682: ord_le_eq_subst haftmann@27682: ord_eq_le_subst haftmann@27682: ord_less_eq_subst haftmann@27682: ord_eq_less_subst haftmann@27682: forw_subst haftmann@27682: back_subst haftmann@27682: rev_mp haftmann@27682: mp haftmann@27682: neq_le_trans haftmann@27682: le_neq_trans haftmann@27682: less_trans haftmann@27682: less_asym' haftmann@27682: le_less_trans haftmann@27682: less_le_trans haftmann@27682: order_trans haftmann@27682: antisym haftmann@21383: ord_le_eq_trans haftmann@21383: ord_eq_le_trans haftmann@21383: ord_less_eq_trans haftmann@21383: ord_eq_less_trans haftmann@21383: trans haftmann@21383: haftmann@21083: text {* These support proving chains of decreasing inequalities haftmann@21083: a >= b >= c ... in Isar proofs. *} haftmann@21083: blanchet@45221: lemma xt1 [no_atp]: haftmann@21083: "a = b ==> b > c ==> a > c" haftmann@21083: "a > b ==> b = c ==> a > c" haftmann@21083: "a = b ==> b >= c ==> a >= c" haftmann@21083: "a >= b ==> b = c ==> a >= c" haftmann@21083: "(x::'a::order) >= y ==> y >= x ==> x = y" haftmann@21083: "(x::'a::order) >= y ==> y >= z ==> x >= z" haftmann@21083: "(x::'a::order) > y ==> y >= z ==> x > z" haftmann@21083: "(x::'a::order) >= y ==> y > z ==> x > z" wenzelm@23417: "(a::'a::order) > b ==> b > a ==> P" haftmann@21083: "(x::'a::order) > y ==> y > z ==> x > z" haftmann@21083: "(a::'a::order) >= b ==> a ~= b ==> a > b" haftmann@21083: "(a::'a::order) ~= b ==> a >= b ==> a > b" haftmann@21083: "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c" haftmann@21083: "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c" haftmann@21083: "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c" haftmann@21083: "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c" haftmann@25076: by auto haftmann@21083: blanchet@45221: lemma xt2 [no_atp]: haftmann@21083: "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c" haftmann@21083: by (subgoal_tac "f b >= f c", force, force) haftmann@21083: blanchet@45221: lemma xt3 [no_atp]: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==> haftmann@21083: (!!x y. x >= y ==> f x >= f y) ==> f a >= c" haftmann@21083: by (subgoal_tac "f a >= f b", force, force) haftmann@21083: blanchet@45221: lemma xt4 [no_atp]: "(a::'a::order) > f b ==> (b::'b::order) >= c ==> haftmann@21083: (!!x y. x >= y ==> f x >= f y) ==> a > f c" haftmann@21083: by (subgoal_tac "f b >= f c", force, force) haftmann@21083: blanchet@45221: lemma xt5 [no_atp]: "(a::'a::order) > b ==> (f b::'b::order) >= c==> haftmann@21083: (!!x y. x > y ==> f x > f y) ==> f a > c" haftmann@21083: by (subgoal_tac "f a > f b", force, force) haftmann@21083: blanchet@45221: lemma xt6 [no_atp]: "(a::'a::order) >= f b ==> b > c ==> haftmann@21083: (!!x y. x > y ==> f x > f y) ==> a > f c" haftmann@21083: by (subgoal_tac "f b > f c", force, force) haftmann@21083: blanchet@45221: lemma xt7 [no_atp]: "(a::'a::order) >= b ==> (f b::'b::order) > c ==> haftmann@21083: (!!x y. x >= y ==> f x >= f y) ==> f a > c" haftmann@21083: by (subgoal_tac "f a >= f b", force, force) haftmann@21083: blanchet@45221: lemma xt8 [no_atp]: "(a::'a::order) > f b ==> (b::'b::order) > c ==> haftmann@21083: (!!x y. x > y ==> f x > f y) ==> a > f c" haftmann@21083: by (subgoal_tac "f b > f c", force, force) haftmann@21083: blanchet@45221: lemma xt9 [no_atp]: "(a::'a::order) > b ==> (f b::'b::order) > c ==> haftmann@21083: (!!x y. x > y ==> f x > f y) ==> f a > c" haftmann@21083: by (subgoal_tac "f a > f b", force, force) haftmann@21083: blanchet@45221: lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9 [no_atp] haftmann@21083: haftmann@21083: (* haftmann@21083: Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands haftmann@21083: for the wrong thing in an Isar proof. haftmann@21083: haftmann@21083: The extra transitivity rules can be used as follows: haftmann@21083: haftmann@21083: lemma "(a::'a::order) > z" haftmann@21083: proof - haftmann@21083: have "a >= b" (is "_ >= ?rhs") haftmann@21083: sorry haftmann@21083: also have "?rhs >= c" (is "_ >= ?rhs") haftmann@21083: sorry haftmann@21083: also (xtrans) have "?rhs = d" (is "_ = ?rhs") haftmann@21083: sorry haftmann@21083: also (xtrans) have "?rhs >= e" (is "_ >= ?rhs") haftmann@21083: sorry haftmann@21083: also (xtrans) have "?rhs > f" (is "_ > ?rhs") haftmann@21083: sorry haftmann@21083: also (xtrans) have "?rhs > z" haftmann@21083: sorry haftmann@21083: finally (xtrans) show ?thesis . haftmann@21083: qed haftmann@21083: haftmann@21083: Alternatively, one can use "declare xtrans [trans]" and then haftmann@21083: leave out the "(xtrans)" above. haftmann@21083: *) haftmann@21083: haftmann@23881: haftmann@23881: subsection {* Monotonicity, least value operator and min/max *} haftmann@21083: haftmann@25076: context order haftmann@25076: begin haftmann@25076: haftmann@30298: definition mono :: "('a \ 'b\order) \ bool" where haftmann@25076: "mono f \ (\x y. x \ y \ f x \ f y)" haftmann@25076: haftmann@25076: lemma monoI [intro?]: haftmann@25076: fixes f :: "'a \ 'b\order" haftmann@25076: shows "(\x y. x \ y \ f x \ f y) \ mono f" haftmann@25076: unfolding mono_def by iprover haftmann@21216: haftmann@25076: lemma monoD [dest?]: haftmann@25076: fixes f :: "'a \ 'b\order" haftmann@25076: shows "mono f \ x \ y \ f x \ f y" haftmann@25076: unfolding mono_def by iprover haftmann@25076: haftmann@30298: definition strict_mono :: "('a \ 'b\order) \ bool" where haftmann@30298: "strict_mono f \ (\x y. x < y \ f x < f y)" haftmann@30298: haftmann@30298: lemma strict_monoI [intro?]: haftmann@30298: assumes "\x y. x < y \ f x < f y" haftmann@30298: shows "strict_mono f" haftmann@30298: using assms unfolding strict_mono_def by auto haftmann@30298: haftmann@30298: lemma strict_monoD [dest?]: haftmann@30298: "strict_mono f \ x < y \ f x < f y" haftmann@30298: unfolding strict_mono_def by auto haftmann@30298: haftmann@30298: lemma strict_mono_mono [dest?]: haftmann@30298: assumes "strict_mono f" haftmann@30298: shows "mono f" haftmann@30298: proof (rule monoI) haftmann@30298: fix x y haftmann@30298: assume "x \ y" haftmann@30298: show "f x \ f y" haftmann@30298: proof (cases "x = y") haftmann@30298: case True then show ?thesis by simp haftmann@30298: next haftmann@30298: case False with `x \ y` have "x < y" by simp haftmann@30298: with assms strict_monoD have "f x < f y" by auto haftmann@30298: then show ?thesis by simp haftmann@30298: qed haftmann@30298: qed haftmann@30298: haftmann@25076: end haftmann@25076: haftmann@25076: context linorder haftmann@25076: begin haftmann@25076: haftmann@30298: lemma strict_mono_eq: haftmann@30298: assumes "strict_mono f" haftmann@30298: shows "f x = f y \ x = y" haftmann@30298: proof haftmann@30298: assume "f x = f y" haftmann@30298: show "x = y" proof (cases x y rule: linorder_cases) haftmann@30298: case less with assms strict_monoD have "f x < f y" by auto haftmann@30298: with `f x = f y` show ?thesis by simp haftmann@30298: next haftmann@30298: case equal then show ?thesis . haftmann@30298: next haftmann@30298: case greater with assms strict_monoD have "f y < f x" by auto haftmann@30298: with `f x = f y` show ?thesis by simp haftmann@30298: qed haftmann@30298: qed simp haftmann@30298: haftmann@30298: lemma strict_mono_less_eq: haftmann@30298: assumes "strict_mono f" haftmann@30298: shows "f x \ f y \ x \ y" haftmann@30298: proof haftmann@30298: assume "x \ y" haftmann@30298: with assms strict_mono_mono monoD show "f x \ f y" by auto haftmann@30298: next haftmann@30298: assume "f x \ f y" haftmann@30298: show "x \ y" proof (rule ccontr) haftmann@30298: assume "\ x \ y" then have "y < x" by simp haftmann@30298: with assms strict_monoD have "f y < f x" by auto haftmann@30298: with `f x \ f y` show False by simp haftmann@30298: qed haftmann@30298: qed haftmann@30298: haftmann@30298: lemma strict_mono_less: haftmann@30298: assumes "strict_mono f" haftmann@30298: shows "f x < f y \ x < y" haftmann@30298: using assms haftmann@30298: by (auto simp add: less_le Orderings.less_le strict_mono_eq strict_mono_less_eq) haftmann@30298: haftmann@25076: lemma min_of_mono: haftmann@25076: fixes f :: "'a \ 'b\linorder" wenzelm@25377: shows "mono f \ min (f m) (f n) = f (min m n)" haftmann@25076: by (auto simp: mono_def Orderings.min_def min_def intro: Orderings.antisym) haftmann@25076: haftmann@25076: lemma max_of_mono: haftmann@25076: fixes f :: "'a \ 'b\linorder" wenzelm@25377: shows "mono f \ max (f m) (f n) = f (max m n)" haftmann@25076: by (auto simp: mono_def Orderings.max_def max_def intro: Orderings.antisym) haftmann@25076: haftmann@25076: end haftmann@21083: haftmann@21383: lemma min_leastL: "(!!x. least <= x) ==> min least x = least" nipkow@23212: by (simp add: min_def) haftmann@21383: haftmann@21383: lemma max_leastL: "(!!x. least <= x) ==> max least x = x" nipkow@23212: by (simp add: max_def) haftmann@21383: haftmann@21383: lemma min_leastR: "(\x\'a\order. least \ x) \ min x least = least" nipkow@23212: apply (simp add: min_def) haftmann@34250: apply (blast intro: antisym) nipkow@23212: done haftmann@21383: haftmann@21383: lemma max_leastR: "(\x\'a\order. least \ x) \ max x least = x" nipkow@23212: apply (simp add: max_def) haftmann@34250: apply (blast intro: antisym) nipkow@23212: done haftmann@21383: haftmann@27823: haftmann@43813: subsection {* (Unique) top and bottom elements *} haftmann@28685: haftmann@43813: class bot = order + haftmann@43853: fixes bot :: 'a ("\") haftmann@43853: assumes bot_least [simp]: "\ \ a" haftmann@43814: begin haftmann@43814: haftmann@43853: lemma le_bot: haftmann@43853: "a \ \ \ a = \" haftmann@43853: by (auto intro: antisym) haftmann@43853: haftmann@43816: lemma bot_unique: haftmann@43853: "a \ \ \ a = \" haftmann@43853: by (auto intro: antisym) haftmann@43853: haftmann@43853: lemma not_less_bot [simp]: haftmann@43853: "\ (a < \)" haftmann@43853: using bot_least [of a] by (auto simp: le_less) haftmann@43816: haftmann@43814: lemma bot_less: haftmann@43853: "a \ \ \ \ < a" haftmann@43814: by (auto simp add: less_le_not_le intro!: antisym) haftmann@43814: haftmann@43814: end haftmann@41082: haftmann@43813: class top = order + haftmann@43853: fixes top :: 'a ("\") haftmann@43853: assumes top_greatest [simp]: "a \ \" haftmann@43814: begin haftmann@43814: haftmann@43853: lemma top_le: haftmann@43853: "\ \ a \ a = \" haftmann@43853: by (rule antisym) auto haftmann@43853: haftmann@43816: lemma top_unique: haftmann@43853: "\ \ a \ a = \" haftmann@43853: by (auto intro: antisym) haftmann@43853: haftmann@43853: lemma not_top_less [simp]: "\ (\ < a)" haftmann@43853: using top_greatest [of a] by (auto simp: le_less) haftmann@43816: haftmann@43814: lemma less_top: haftmann@43853: "a \ \ \ a < \" haftmann@43814: by (auto simp add: less_le_not_le intro!: antisym) haftmann@43814: haftmann@43814: end haftmann@28685: haftmann@43853: no_notation haftmann@43853: bot ("\") and haftmann@43853: top ("\") haftmann@43853: haftmann@28685: haftmann@27823: subsection {* Dense orders *} haftmann@27823: haftmann@35028: class dense_linorder = linorder + haftmann@27823: assumes gt_ex: "\y. x < y" haftmann@27823: and lt_ex: "\y. y < x" haftmann@27823: and dense: "x < y \ (\z. x < z \ z < y)" hoelzl@35579: begin haftmann@27823: hoelzl@35579: lemma dense_le: hoelzl@35579: fixes y z :: 'a hoelzl@35579: assumes "\x. x < y \ x \ z" hoelzl@35579: shows "y \ z" hoelzl@35579: proof (rule ccontr) hoelzl@35579: assume "\ ?thesis" hoelzl@35579: hence "z < y" by simp hoelzl@35579: from dense[OF this] hoelzl@35579: obtain x where "x < y" and "z < x" by safe hoelzl@35579: moreover have "x \ z" using assms[OF `x < y`] . hoelzl@35579: ultimately show False by auto hoelzl@35579: qed hoelzl@35579: hoelzl@35579: lemma dense_le_bounded: hoelzl@35579: fixes x y z :: 'a hoelzl@35579: assumes "x < y" hoelzl@35579: assumes *: "\w. \ x < w ; w < y \ \ w \ z" hoelzl@35579: shows "y \ z" hoelzl@35579: proof (rule dense_le) hoelzl@35579: fix w assume "w < y" hoelzl@35579: from dense[OF `x < y`] obtain u where "x < u" "u < y" by safe hoelzl@35579: from linear[of u w] hoelzl@35579: show "w \ z" hoelzl@35579: proof (rule disjE) hoelzl@35579: assume "u \ w" hoelzl@35579: from less_le_trans[OF `x < u` `u \ w`] `w < y` hoelzl@35579: show "w \ z" by (rule *) hoelzl@35579: next hoelzl@35579: assume "w \ u" hoelzl@35579: from `w \ u` *[OF `x < u` `u < y`] hoelzl@35579: show "w \ z" by (rule order_trans) hoelzl@35579: qed hoelzl@35579: qed hoelzl@35579: hoelzl@35579: end haftmann@27823: haftmann@27823: subsection {* Wellorders *} haftmann@27823: haftmann@27823: class wellorder = linorder + haftmann@27823: assumes less_induct [case_names less]: "(\x. (\y. y < x \ P y) \ P x) \ P a" haftmann@27823: begin haftmann@27823: haftmann@27823: lemma wellorder_Least_lemma: haftmann@27823: fixes k :: 'a haftmann@27823: assumes "P k" haftmann@34250: shows LeastI: "P (LEAST x. P x)" and Least_le: "(LEAST x. P x) \ k" haftmann@27823: proof - haftmann@27823: have "P (LEAST x. P x) \ (LEAST x. P x) \ k" haftmann@27823: using assms proof (induct k rule: less_induct) haftmann@27823: case (less x) then have "P x" by simp haftmann@27823: show ?case proof (rule classical) haftmann@27823: assume assm: "\ (P (LEAST a. P a) \ (LEAST a. P a) \ x)" haftmann@27823: have "\y. P y \ x \ y" haftmann@27823: proof (rule classical) haftmann@27823: fix y hoelzl@38705: assume "P y" and "\ x \ y" haftmann@27823: with less have "P (LEAST a. P a)" and "(LEAST a. P a) \ y" haftmann@27823: by (auto simp add: not_le) haftmann@27823: with assm have "x < (LEAST a. P a)" and "(LEAST a. P a) \ y" haftmann@27823: by auto haftmann@27823: then show "x \ y" by auto haftmann@27823: qed haftmann@27823: with `P x` have Least: "(LEAST a. P a) = x" haftmann@27823: by (rule Least_equality) haftmann@27823: with `P x` show ?thesis by simp haftmann@27823: qed haftmann@27823: qed haftmann@27823: then show "P (LEAST x. P x)" and "(LEAST x. P x) \ k" by auto haftmann@27823: qed haftmann@27823: haftmann@27823: -- "The following 3 lemmas are due to Brian Huffman" haftmann@27823: lemma LeastI_ex: "\x. P x \ P (Least P)" haftmann@27823: by (erule exE) (erule LeastI) haftmann@27823: haftmann@27823: lemma LeastI2: haftmann@27823: "P a \ (\x. P x \ Q x) \ Q (Least P)" haftmann@27823: by (blast intro: LeastI) haftmann@27823: haftmann@27823: lemma LeastI2_ex: haftmann@27823: "\a. P a \ (\x. P x \ Q x) \ Q (Least P)" haftmann@27823: by (blast intro: LeastI_ex) haftmann@27823: hoelzl@38705: lemma LeastI2_wellorder: hoelzl@38705: assumes "P a" hoelzl@38705: and "\a. \ P a; \b. P b \ a \ b \ \ Q a" hoelzl@38705: shows "Q (Least P)" hoelzl@38705: proof (rule LeastI2_order) hoelzl@38705: show "P (Least P)" using `P a` by (rule LeastI) hoelzl@38705: next hoelzl@38705: fix y assume "P y" thus "Least P \ y" by (rule Least_le) hoelzl@38705: next hoelzl@38705: fix x assume "P x" "\y. P y \ x \ y" thus "Q x" by (rule assms(2)) hoelzl@38705: qed hoelzl@38705: haftmann@27823: lemma not_less_Least: "k < (LEAST x. P x) \ \ P k" haftmann@27823: apply (simp (no_asm_use) add: not_le [symmetric]) haftmann@27823: apply (erule contrapos_nn) haftmann@27823: apply (erule Least_le) haftmann@27823: done haftmann@27823: hoelzl@38705: end haftmann@27823: haftmann@28685: haftmann@28685: subsection {* Order on bool *} haftmann@28685: haftmann@44025: instantiation bool :: "{bot, top}" haftmann@28685: begin haftmann@28685: haftmann@28685: definition haftmann@41080: le_bool_def [simp]: "P \ Q \ P \ Q" haftmann@28685: haftmann@28685: definition haftmann@41080: [simp]: "(P\bool) < Q \ \ P \ Q" haftmann@28685: haftmann@28685: definition haftmann@41082: [simp]: "bot \ False" haftmann@28685: haftmann@28685: definition haftmann@41082: [simp]: "top \ True" haftmann@28685: haftmann@28685: instance proof haftmann@41080: qed auto haftmann@28685: nipkow@15524: end haftmann@28685: haftmann@28685: lemma le_boolI: "(P \ Q) \ P \ Q" haftmann@41080: by simp haftmann@28685: haftmann@28685: lemma le_boolI': "P \ Q \ P \ Q" haftmann@41080: by simp haftmann@28685: haftmann@28685: lemma le_boolE: "P \ Q \ P \ (Q \ R) \ R" haftmann@41080: by simp haftmann@28685: haftmann@28685: lemma le_boolD: "P \ Q \ P \ Q" haftmann@41080: by simp haftmann@32899: haftmann@32899: lemma bot_boolE: "bot \ P" haftmann@41080: by simp haftmann@32899: haftmann@32899: lemma top_boolI: top haftmann@41080: by simp haftmann@28685: haftmann@28685: lemma [code]: haftmann@28685: "False \ b \ True" haftmann@28685: "True \ b \ b" haftmann@28685: "False < b \ b" haftmann@28685: "True < b \ False" haftmann@41080: by simp_all haftmann@28685: haftmann@28685: haftmann@28685: subsection {* Order on functions *} haftmann@28685: haftmann@28685: instantiation "fun" :: (type, ord) ord haftmann@28685: begin haftmann@28685: haftmann@28685: definition haftmann@37767: le_fun_def: "f \ g \ (\x. f x \ g x)" haftmann@28685: haftmann@28685: definition haftmann@41080: "(f\'a \ 'b) < g \ f \ g \ \ (g \ f)" haftmann@28685: haftmann@28685: instance .. haftmann@28685: haftmann@28685: end haftmann@28685: haftmann@28685: instance "fun" :: (type, preorder) preorder proof haftmann@28685: qed (auto simp add: le_fun_def less_fun_def huffman@44921: intro: order_trans antisym) haftmann@28685: haftmann@28685: instance "fun" :: (type, order) order proof huffman@44921: qed (auto simp add: le_fun_def intro: antisym) haftmann@28685: haftmann@41082: instantiation "fun" :: (type, bot) bot haftmann@41082: begin haftmann@41082: haftmann@41082: definition haftmann@41082: "bot = (\x. bot)" haftmann@41082: haftmann@41082: lemma bot_apply: haftmann@41082: "bot x = bot" haftmann@41082: by (simp add: bot_fun_def) haftmann@41082: haftmann@41082: instance proof haftmann@41082: qed (simp add: le_fun_def bot_apply) haftmann@41082: haftmann@41082: end haftmann@41082: haftmann@28685: instantiation "fun" :: (type, top) top haftmann@28685: begin haftmann@28685: haftmann@28685: definition haftmann@41080: [no_atp]: "top = (\x. top)" haftmann@41075: declare top_fun_def_raw [no_atp] haftmann@28685: haftmann@41080: lemma top_apply: haftmann@41080: "top x = top" haftmann@41080: by (simp add: top_fun_def) haftmann@41080: haftmann@28685: instance proof haftmann@41080: qed (simp add: le_fun_def top_apply) haftmann@28685: haftmann@28685: end haftmann@28685: haftmann@28685: lemma le_funI: "(\x. f x \ g x) \ f \ g" haftmann@28685: unfolding le_fun_def by simp haftmann@28685: haftmann@28685: lemma le_funE: "f \ g \ (f x \ g x \ P) \ P" haftmann@28685: unfolding le_fun_def by simp haftmann@28685: haftmann@28685: lemma le_funD: "f \ g \ f x \ g x" haftmann@28685: unfolding le_fun_def by simp haftmann@28685: haftmann@34250: haftmann@34250: subsection {* Name duplicates *} haftmann@34250: haftmann@34250: lemmas order_eq_refl = preorder_class.eq_refl haftmann@34250: lemmas order_less_irrefl = preorder_class.less_irrefl haftmann@34250: lemmas order_less_imp_le = preorder_class.less_imp_le haftmann@34250: lemmas order_less_not_sym = preorder_class.less_not_sym haftmann@34250: lemmas order_less_asym = preorder_class.less_asym haftmann@34250: lemmas order_less_trans = preorder_class.less_trans haftmann@34250: lemmas order_le_less_trans = preorder_class.le_less_trans haftmann@34250: lemmas order_less_le_trans = preorder_class.less_le_trans haftmann@34250: lemmas order_less_imp_not_less = preorder_class.less_imp_not_less haftmann@34250: lemmas order_less_imp_triv = preorder_class.less_imp_triv haftmann@34250: lemmas order_less_asym' = preorder_class.less_asym' haftmann@34250: haftmann@34250: lemmas order_less_le = order_class.less_le haftmann@34250: lemmas order_le_less = order_class.le_less haftmann@34250: lemmas order_le_imp_less_or_eq = order_class.le_imp_less_or_eq haftmann@34250: lemmas order_less_imp_not_eq = order_class.less_imp_not_eq haftmann@34250: lemmas order_less_imp_not_eq2 = order_class.less_imp_not_eq2 haftmann@34250: lemmas order_neq_le_trans = order_class.neq_le_trans haftmann@34250: lemmas order_le_neq_trans = order_class.le_neq_trans haftmann@34250: lemmas order_antisym = order_class.antisym haftmann@34250: lemmas order_eq_iff = order_class.eq_iff haftmann@34250: lemmas order_antisym_conv = order_class.antisym_conv haftmann@34250: haftmann@34250: lemmas linorder_linear = linorder_class.linear haftmann@34250: lemmas linorder_less_linear = linorder_class.less_linear haftmann@34250: lemmas linorder_le_less_linear = linorder_class.le_less_linear haftmann@34250: lemmas linorder_le_cases = linorder_class.le_cases haftmann@34250: lemmas linorder_not_less = linorder_class.not_less haftmann@34250: lemmas linorder_not_le = linorder_class.not_le haftmann@34250: lemmas linorder_neq_iff = linorder_class.neq_iff haftmann@34250: lemmas linorder_neqE = linorder_class.neqE haftmann@34250: lemmas linorder_antisym_conv1 = linorder_class.antisym_conv1 haftmann@34250: lemmas linorder_antisym_conv2 = linorder_class.antisym_conv2 haftmann@34250: lemmas linorder_antisym_conv3 = linorder_class.antisym_conv3 haftmann@34250: haftmann@28685: end