src/HOL/Orderings.thy
author nipkow
Sat May 19 14:05:05 2007 +0200 (2007-05-19)
changeset 23032 b6cb6a131511
parent 23018 1d29bc31b0cb
child 23087 ad7244663431
permissions -rw-r--r--
unfold min/max in Stefans code generator
     1 (*  Title:      HOL/Orderings.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow, Markus Wenzel, and Larry Paulson
     4 *)
     5 
     6 header {* Syntactic and abstract orders *}
     7 
     8 theory Orderings
     9 imports Code_Generator
    10 begin
    11 
    12 subsection {* Order syntax *}
    13 
    14 class ord = type +
    15   fixes less_eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"  (infix "\<sqsubseteq>" 50)
    16     and less :: "'a \<Rightarrow> 'a \<Rightarrow> bool"  (infix "\<sqsubset>" 50)
    17 begin
    18 
    19 notation
    20   less_eq  ("op \<^loc><=") and
    21   less_eq  ("(_/ \<^loc><= _)" [51, 51] 50) and
    22   less  ("op \<^loc><") and
    23   less  ("(_/ \<^loc>< _)"  [51, 51] 50)
    24   
    25 notation (xsymbols)
    26   less_eq  ("op \<^loc>\<le>") and
    27   less_eq  ("(_/ \<^loc>\<le> _)"  [51, 51] 50)
    28 
    29 notation (HTML output)
    30   less_eq  ("op \<^loc>\<le>") and
    31   less_eq  ("(_/ \<^loc>\<le> _)"  [51, 51] 50)
    32 
    33 abbreviation (input)
    34   greater  (infix "\<^loc>>" 50) where
    35   "x \<^loc>> y \<equiv> y \<^loc>< x"
    36 
    37 abbreviation (input)
    38   greater_eq  (infix "\<^loc>>=" 50) where
    39   "x \<^loc>>= y \<equiv> y \<^loc><= x"
    40 
    41 notation (input)
    42   greater_eq  (infix "\<^loc>\<ge>" 50)
    43 
    44 text {*
    45   syntactic min/max -- these definitions reach
    46   their usual semantics in class linorder ahead.
    47 *}
    48 
    49 definition
    50   min :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
    51   "min a b = (if a \<^loc>\<le> b then a else b)"
    52 
    53 definition
    54   max :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
    55   "max a b = (if a \<^loc>\<le> b then b else a)"
    56 
    57 end
    58 
    59 notation
    60   less_eq  ("op <=") and
    61   less_eq  ("(_/ <= _)" [51, 51] 50) and
    62   less  ("op <") and
    63   less  ("(_/ < _)"  [51, 51] 50)
    64   
    65 notation (xsymbols)
    66   less_eq  ("op \<le>") and
    67   less_eq  ("(_/ \<le> _)"  [51, 51] 50)
    68 
    69 notation (HTML output)
    70   less_eq  ("op \<le>") and
    71   less_eq  ("(_/ \<le> _)"  [51, 51] 50)
    72 
    73 abbreviation (input)
    74   greater  (infix ">" 50) where
    75   "x > y \<equiv> y < x"
    76 
    77 abbreviation (input)
    78   greater_eq  (infix ">=" 50) where
    79   "x >= y \<equiv> y <= x"
    80 
    81 notation (input)
    82   greater_eq  (infix "\<ge>" 50)
    83 
    84 hide const min max
    85 
    86 definition
    87   min :: "'a\<Colon>ord \<Rightarrow> 'a \<Rightarrow> 'a" where
    88   "min a b = (if a \<le> b then a else b)"
    89 
    90 definition
    91   max :: "'a\<Colon>ord \<Rightarrow> 'a \<Rightarrow> 'a" where
    92   "max a b = (if a \<le> b then b else a)"
    93 
    94 declare min_def[code unfold, code inline del]
    95         max_def[code unfold, code inline del]
    96 
    97 lemma linorder_class_min:
    98   "ord.min (op \<le> \<Colon> 'a\<Colon>ord \<Rightarrow> 'a \<Rightarrow> bool) = min"
    99   by rule+ (simp add: min_def ord_class.min_def)
   100 
   101 lemma linorder_class_max:
   102   "ord.max (op \<le> \<Colon> 'a\<Colon>ord \<Rightarrow> 'a \<Rightarrow> bool) = max"
   103   by rule+ (simp add: max_def ord_class.max_def)
   104 
   105 
   106 subsection {* Partial orders *}
   107 
   108 class order = ord +
   109   assumes less_le: "x \<sqsubset> y \<longleftrightarrow> x \<sqsubseteq> y \<and> x \<noteq> y"
   110   and order_refl [iff]: "x \<sqsubseteq> x"
   111   and order_trans: "x \<sqsubseteq> y \<Longrightarrow> y \<sqsubseteq> z \<Longrightarrow> x \<sqsubseteq> z"
   112   assumes antisym: "x \<sqsubseteq> y \<Longrightarrow> y \<sqsubseteq> x \<Longrightarrow> x = y"
   113 
   114 begin
   115 
   116 text {* Reflexivity. *}
   117 
   118 lemma eq_refl: "x = y \<Longrightarrow> x \<^loc>\<le> y"
   119     -- {* This form is useful with the classical reasoner. *}
   120   by (erule ssubst) (rule order_refl)
   121 
   122 lemma less_irrefl [iff]: "\<not> x \<^loc>< x"
   123   by (simp add: less_le)
   124 
   125 lemma le_less: "x \<^loc>\<le> y \<longleftrightarrow> x \<^loc>< y \<or> x = y"
   126     -- {* NOT suitable for iff, since it can cause PROOF FAILED. *}
   127   by (simp add: less_le) blast
   128 
   129 lemma le_imp_less_or_eq: "x \<^loc>\<le> y \<Longrightarrow> x \<^loc>< y \<or> x = y"
   130   unfolding less_le by blast
   131 
   132 lemma less_imp_le: "x \<^loc>< y \<Longrightarrow> x \<^loc>\<le> y"
   133   unfolding less_le by blast
   134 
   135 lemma less_imp_neq: "x \<^loc>< y \<Longrightarrow> x \<noteq> y"
   136   by (erule contrapos_pn, erule subst, rule less_irrefl)
   137 
   138 
   139 text {* Useful for simplification, but too risky to include by default. *}
   140 
   141 lemma less_imp_not_eq: "x \<^loc>< y \<Longrightarrow> (x = y) \<longleftrightarrow> False"
   142   by auto
   143 
   144 lemma less_imp_not_eq2: "x \<^loc>< y \<Longrightarrow> (y = x) \<longleftrightarrow> False"
   145   by auto
   146 
   147 
   148 text {* Transitivity rules for calculational reasoning *}
   149 
   150 lemma neq_le_trans: "a \<noteq> b \<Longrightarrow> a \<^loc>\<le> b \<Longrightarrow> a \<^loc>< b"
   151   by (simp add: less_le)
   152 
   153 lemma le_neq_trans: "a \<^loc>\<le> b \<Longrightarrow> a \<noteq> b \<Longrightarrow> a \<^loc>< b"
   154   by (simp add: less_le)
   155 
   156 
   157 text {* Asymmetry. *}
   158 
   159 lemma less_not_sym: "x \<^loc>< y \<Longrightarrow> \<not> (y \<^loc>< x)"
   160   by (simp add: less_le antisym)
   161 
   162 lemma less_asym: "x \<^loc>< y \<Longrightarrow> (\<not> P \<Longrightarrow> y \<^loc>< x) \<Longrightarrow> P"
   163   by (drule less_not_sym, erule contrapos_np) simp
   164 
   165 lemma eq_iff: "x = y \<longleftrightarrow> x \<^loc>\<le> y \<and> y \<^loc>\<le> x"
   166   by (blast intro: antisym)
   167 
   168 lemma antisym_conv: "y \<^loc>\<le> x \<Longrightarrow> x \<^loc>\<le> y \<longleftrightarrow> x = y"
   169   by (blast intro: antisym)
   170 
   171 lemma less_imp_neq: "x \<^loc>< y \<Longrightarrow> x \<noteq> y"
   172   by (erule contrapos_pn, erule subst, rule less_irrefl)
   173 
   174 
   175 text {* Transitivity. *}
   176 
   177 lemma less_trans: "x \<^loc>< y \<Longrightarrow> y \<^loc>< z \<Longrightarrow> x \<^loc>< z"
   178   by (simp add: less_le) (blast intro: order_trans antisym)
   179 
   180 lemma le_less_trans: "x \<^loc>\<le> y \<Longrightarrow> y \<^loc>< z \<Longrightarrow> x \<^loc>< z"
   181   by (simp add: less_le) (blast intro: order_trans antisym)
   182 
   183 lemma less_le_trans: "x \<^loc>< y \<Longrightarrow> y \<^loc>\<le> z \<Longrightarrow> x \<^loc>< z"
   184   by (simp add: less_le) (blast intro: order_trans antisym)
   185 
   186 
   187 text {* Useful for simplification, but too risky to include by default. *}
   188 
   189 lemma less_imp_not_less: "x \<^loc>< y \<Longrightarrow> (\<not> y \<^loc>< x) \<longleftrightarrow> True"
   190   by (blast elim: less_asym)
   191 
   192 lemma less_imp_triv: "x \<^loc>< y \<Longrightarrow> (y \<^loc>< x \<longrightarrow> P) \<longleftrightarrow> True"
   193   by (blast elim: less_asym)
   194 
   195 
   196 text {* Transitivity rules for calculational reasoning *}
   197 
   198 lemma less_asym': "a \<^loc>< b \<Longrightarrow> b \<^loc>< a \<Longrightarrow> P"
   199   by (rule less_asym)
   200 
   201 
   202 text {* Reverse order *}
   203 
   204 lemma order_reverse:
   205   "order (\<lambda>x y. y \<^loc>\<le> x) (\<lambda>x y. y \<^loc>< x)"
   206   by unfold_locales
   207     (simp add: less_le, auto intro: antisym order_trans)
   208 
   209 end
   210 
   211 
   212 subsection {* Linear (total) orders *}
   213 
   214 class linorder = order +
   215   assumes linear: "x \<sqsubseteq> y \<or> y \<sqsubseteq> x"
   216 begin
   217 
   218 lemma less_linear: "x \<^loc>< y \<or> x = y \<or> y \<^loc>< x"
   219   unfolding less_le using less_le linear by blast 
   220 
   221 lemma le_less_linear: "x \<^loc>\<le> y \<or> y \<^loc>< x"
   222   by (simp add: le_less less_linear)
   223 
   224 lemma le_cases [case_names le ge]:
   225   "(x \<^loc>\<le> y \<Longrightarrow> P) \<Longrightarrow> (y \<^loc>\<le> x \<Longrightarrow> P) \<Longrightarrow> P"
   226   using linear by blast
   227 
   228 lemma linorder_cases [case_names less equal greater]:
   229     "(x \<^loc>< y \<Longrightarrow> P) \<Longrightarrow> (x = y \<Longrightarrow> P) \<Longrightarrow> (y \<^loc>< x \<Longrightarrow> P) \<Longrightarrow> P"
   230   using less_linear by blast
   231 
   232 lemma not_less: "\<not> x \<^loc>< y \<longleftrightarrow> y \<^loc>\<le> x"
   233   apply (simp add: less_le)
   234   using linear apply (blast intro: antisym)
   235   done
   236 
   237 lemma not_le: "\<not> x \<^loc>\<le> y \<longleftrightarrow> y \<^loc>< x"
   238   apply (simp add: less_le)
   239   using linear apply (blast intro: antisym)
   240   done
   241 
   242 lemma neq_iff: "x \<noteq> y \<longleftrightarrow> x \<^loc>< y \<or> y \<^loc>< x"
   243   by (cut_tac x = x and y = y in less_linear, auto)
   244 
   245 lemma neqE: "x \<noteq> y \<Longrightarrow> (x \<^loc>< y \<Longrightarrow> R) \<Longrightarrow> (y \<^loc>< x \<Longrightarrow> R) \<Longrightarrow> R"
   246   by (simp add: neq_iff) blast
   247 
   248 lemma antisym_conv1: "\<not> x \<^loc>< y \<Longrightarrow> x \<^loc>\<le> y \<longleftrightarrow> x = y"
   249   by (blast intro: antisym dest: not_less [THEN iffD1])
   250 
   251 lemma antisym_conv2: "x \<^loc>\<le> y \<Longrightarrow> \<not> x \<^loc>< y \<longleftrightarrow> x = y"
   252   by (blast intro: antisym dest: not_less [THEN iffD1])
   253 
   254 lemma antisym_conv3: "\<not> y \<^loc>< x \<Longrightarrow> \<not> x \<^loc>< y \<longleftrightarrow> x = y"
   255   by (blast intro: antisym dest: not_less [THEN iffD1])
   256 
   257 text{*Replacing the old Nat.leI*}
   258 lemma leI: "\<not> x \<^loc>< y \<Longrightarrow> y \<^loc>\<le> x"
   259   unfolding not_less .
   260 
   261 lemma leD: "y \<^loc>\<le> x \<Longrightarrow> \<not> x \<^loc>< y"
   262   unfolding not_less .
   263 
   264 (*FIXME inappropriate name (or delete altogether)*)
   265 lemma not_leE: "\<not> y \<^loc>\<le> x \<Longrightarrow> x \<^loc>< y"
   266   unfolding not_le .
   267 
   268 
   269 text {* Reverse order *}
   270 
   271 lemma linorder_reverse:
   272   "linorder (\<lambda>x y. y \<^loc>\<le> x) (\<lambda>x y. y \<^loc>< x)"
   273   by unfold_locales
   274     (simp add: less_le, auto intro: antisym order_trans simp add: linear)
   275 
   276 
   277 text {* min/max properties *}
   278 
   279 lemma min_le_iff_disj:
   280   "min x y \<^loc>\<le> z \<longleftrightarrow> x \<^loc>\<le> z \<or> y \<^loc>\<le> z"
   281   unfolding min_def using linear by (auto intro: order_trans)
   282 
   283 lemma le_max_iff_disj:
   284   "z \<^loc>\<le> max x y \<longleftrightarrow> z \<^loc>\<le> x \<or> z \<^loc>\<le> y"
   285   unfolding max_def using linear by (auto intro: order_trans)
   286 
   287 lemma min_less_iff_disj:
   288   "min x y \<^loc>< z \<longleftrightarrow> x \<^loc>< z \<or> y \<^loc>< z"
   289   unfolding min_def le_less using less_linear by (auto intro: less_trans)
   290 
   291 lemma less_max_iff_disj:
   292   "z \<^loc>< max x y \<longleftrightarrow> z \<^loc>< x \<or> z \<^loc>< y"
   293   unfolding max_def le_less using less_linear by (auto intro: less_trans)
   294 
   295 lemma min_less_iff_conj [simp]:
   296   "z \<^loc>< min x y \<longleftrightarrow> z \<^loc>< x \<and> z \<^loc>< y"
   297   unfolding min_def le_less using less_linear by (auto intro: less_trans)
   298 
   299 lemma max_less_iff_conj [simp]:
   300   "max x y \<^loc>< z \<longleftrightarrow> x \<^loc>< z \<and> y \<^loc>< z"
   301   unfolding max_def le_less using less_linear by (auto intro: less_trans)
   302 
   303 lemma split_min:
   304   "P (min i j) \<longleftrightarrow> (i \<^loc>\<le> j \<longrightarrow> P i) \<and> (\<not> i \<^loc>\<le> j \<longrightarrow> P j)"
   305   by (simp add: min_def)
   306 
   307 lemma split_max:
   308   "P (max i j) \<longleftrightarrow> (i \<^loc>\<le> j \<longrightarrow> P j) \<and> (\<not> i \<^loc>\<le> j \<longrightarrow> P i)"
   309   by (simp add: max_def)
   310 
   311 end
   312 
   313 subsection {* Name duplicates -- including min/max interpretation *}
   314 
   315 lemmas order_less_le = less_le
   316 lemmas order_eq_refl = order_class.eq_refl
   317 lemmas order_less_irrefl = order_class.less_irrefl
   318 lemmas order_le_less = order_class.le_less
   319 lemmas order_le_imp_less_or_eq = order_class.le_imp_less_or_eq
   320 lemmas order_less_imp_le = order_class.less_imp_le
   321 lemmas order_less_imp_not_eq = order_class.less_imp_not_eq
   322 lemmas order_less_imp_not_eq2 = order_class.less_imp_not_eq2
   323 lemmas order_neq_le_trans = order_class.neq_le_trans
   324 lemmas order_le_neq_trans = order_class.le_neq_trans
   325 
   326 lemmas order_antisym = antisym
   327 lemmas order_less_not_sym = order_class.less_not_sym
   328 lemmas order_less_asym = order_class.less_asym
   329 lemmas order_eq_iff = order_class.eq_iff
   330 lemmas order_antisym_conv = order_class.antisym_conv
   331 lemmas less_imp_neq = order_class.less_imp_neq
   332 lemmas order_less_trans = order_class.less_trans
   333 lemmas order_le_less_trans = order_class.le_less_trans
   334 lemmas order_less_le_trans = order_class.less_le_trans
   335 lemmas order_less_imp_not_less = order_class.less_imp_not_less
   336 lemmas order_less_imp_triv = order_class.less_imp_triv
   337 lemmas order_less_asym' = order_class.less_asym'
   338 
   339 lemmas linorder_linear = linear
   340 lemmas linorder_less_linear = linorder_class.less_linear
   341 lemmas linorder_le_less_linear = linorder_class.le_less_linear
   342 lemmas linorder_le_cases = linorder_class.le_cases
   343 lemmas linorder_not_less = linorder_class.not_less
   344 lemmas linorder_not_le = linorder_class.not_le
   345 lemmas linorder_neq_iff = linorder_class.neq_iff
   346 lemmas linorder_neqE = linorder_class.neqE
   347 lemmas linorder_antisym_conv1 = linorder_class.antisym_conv1
   348 lemmas linorder_antisym_conv2 = linorder_class.antisym_conv2
   349 lemmas linorder_antisym_conv3 = linorder_class.antisym_conv3
   350 lemmas leI = linorder_class.leI
   351 lemmas leD = linorder_class.leD
   352 lemmas not_leE = linorder_class.not_leE
   353 
   354 lemmas min_le_iff_disj = linorder_class.min_le_iff_disj [unfolded linorder_class_min]
   355 lemmas le_max_iff_disj = linorder_class.le_max_iff_disj [unfolded linorder_class_max]
   356 lemmas min_less_iff_disj = linorder_class.min_less_iff_disj [unfolded linorder_class_min]
   357 lemmas less_max_iff_disj = linorder_class.less_max_iff_disj [unfolded linorder_class_max]
   358 lemmas min_less_iff_conj [simp] = linorder_class.min_less_iff_conj [unfolded linorder_class_min]
   359 lemmas max_less_iff_conj [simp] = linorder_class.max_less_iff_conj [unfolded linorder_class_max]
   360 lemmas split_min = linorder_class.split_min [unfolded linorder_class_min]
   361 lemmas split_max = linorder_class.split_max [unfolded linorder_class_max]
   362 
   363 
   364 subsection {* Reasoning tools setup *}
   365 
   366 ML {*
   367 local
   368 
   369 fun decomp_gen sort thy (Trueprop $ t) =
   370   let
   371     fun of_sort t =
   372       let
   373         val T = type_of t
   374       in
   375         (* exclude numeric types: linear arithmetic subsumes transitivity *)
   376         T <> HOLogic.natT andalso T <> HOLogic.intT
   377           andalso T <> HOLogic.realT andalso Sign.of_sort thy (T, sort)
   378       end;
   379     fun dec (Const (@{const_name Not}, _) $ t) = (case dec t
   380           of NONE => NONE
   381            | SOME (t1, rel, t2) => SOME (t1, "~" ^ rel, t2))
   382       | dec (Const (@{const_name "op ="},  _) $ t1 $ t2) =
   383           if of_sort t1
   384           then SOME (t1, "=", t2)
   385           else NONE
   386       | dec (Const (@{const_name Orderings.less_eq},  _) $ t1 $ t2) =
   387           if of_sort t1
   388           then SOME (t1, "<=", t2)
   389           else NONE
   390       | dec (Const (@{const_name Orderings.less},  _) $ t1 $ t2) =
   391           if of_sort t1
   392           then SOME (t1, "<", t2)
   393           else NONE
   394       | dec _ = NONE;
   395   in dec t end;
   396 
   397 in
   398 
   399 (* sorry - there is no preorder class
   400 structure Quasi_Tac = Quasi_Tac_Fun (
   401 struct
   402   val le_trans = thm "order_trans";
   403   val le_refl = thm "order_refl";
   404   val eqD1 = thm "order_eq_refl";
   405   val eqD2 = thm "sym" RS thm "order_eq_refl";
   406   val less_reflE = thm "order_less_irrefl" RS thm "notE";
   407   val less_imp_le = thm "order_less_imp_le";
   408   val le_neq_trans = thm "order_le_neq_trans";
   409   val neq_le_trans = thm "order_neq_le_trans";
   410   val less_imp_neq = thm "less_imp_neq";
   411   val decomp_trans = decomp_gen ["Orderings.preorder"];
   412   val decomp_quasi = decomp_gen ["Orderings.preorder"];
   413 end);*)
   414 
   415 structure Order_Tac = Order_Tac_Fun (
   416 struct
   417   val less_reflE = thm "order_less_irrefl" RS thm "notE";
   418   val le_refl = thm "order_refl";
   419   val less_imp_le = thm "order_less_imp_le";
   420   val not_lessI = thm "linorder_not_less" RS thm "iffD2";
   421   val not_leI = thm "linorder_not_le" RS thm "iffD2";
   422   val not_lessD = thm "linorder_not_less" RS thm "iffD1";
   423   val not_leD = thm "linorder_not_le" RS thm "iffD1";
   424   val eqI = thm "order_antisym";
   425   val eqD1 = thm "order_eq_refl";
   426   val eqD2 = thm "sym" RS thm "order_eq_refl";
   427   val less_trans = thm "order_less_trans";
   428   val less_le_trans = thm "order_less_le_trans";
   429   val le_less_trans = thm "order_le_less_trans";
   430   val le_trans = thm "order_trans";
   431   val le_neq_trans = thm "order_le_neq_trans";
   432   val neq_le_trans = thm "order_neq_le_trans";
   433   val less_imp_neq = thm "less_imp_neq";
   434   val eq_neq_eq_imp_neq = thm "eq_neq_eq_imp_neq";
   435   val not_sym = thm "not_sym";
   436   val decomp_part = decomp_gen ["Orderings.order"];
   437   val decomp_lin = decomp_gen ["Orderings.linorder"];
   438 end);
   439 
   440 end;
   441 *}
   442 
   443 setup {*
   444 let
   445 
   446 fun prp t thm = (#prop (rep_thm thm) = t);
   447 
   448 fun prove_antisym_le sg ss ((le as Const(_,T)) $ r $ s) =
   449   let val prems = prems_of_ss ss;
   450       val less = Const (@{const_name less}, T);
   451       val t = HOLogic.mk_Trueprop(le $ s $ r);
   452   in case find_first (prp t) prems of
   453        NONE =>
   454          let val t = HOLogic.mk_Trueprop(HOLogic.Not $ (less $ r $ s))
   455          in case find_first (prp t) prems of
   456               NONE => NONE
   457             | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_antisym_conv1}))
   458          end
   459      | SOME thm => SOME(mk_meta_eq(thm RS @{thm order_antisym_conv}))
   460   end
   461   handle THM _ => NONE;
   462 
   463 fun prove_antisym_less sg ss (NotC $ ((less as Const(_,T)) $ r $ s)) =
   464   let val prems = prems_of_ss ss;
   465       val le = Const (@{const_name less_eq}, T);
   466       val t = HOLogic.mk_Trueprop(le $ r $ s);
   467   in case find_first (prp t) prems of
   468        NONE =>
   469          let val t = HOLogic.mk_Trueprop(NotC $ (less $ s $ r))
   470          in case find_first (prp t) prems of
   471               NONE => NONE
   472             | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_antisym_conv3}))
   473          end
   474      | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_antisym_conv2}))
   475   end
   476   handle THM _ => NONE;
   477 
   478 fun add_simprocs procs thy =
   479   (Simplifier.change_simpset_of thy (fn ss => ss
   480     addsimprocs (map (fn (name, raw_ts, proc) =>
   481       Simplifier.simproc thy name raw_ts proc)) procs); thy);
   482 fun add_solver name tac thy =
   483   (Simplifier.change_simpset_of thy (fn ss => ss addSolver
   484     (mk_solver name (K tac))); thy);
   485 
   486 in
   487   add_simprocs [
   488        ("antisym le", ["(x::'a::order) <= y"], prove_antisym_le),
   489        ("antisym less", ["~ (x::'a::linorder) < y"], prove_antisym_less)
   490      ]
   491   #> add_solver "Trans_linear" Order_Tac.linear_tac
   492   #> add_solver "Trans_partial" Order_Tac.partial_tac
   493   (* Adding the transitivity reasoners also as safe solvers showed a slight
   494      speed up, but the reasoning strength appears to be not higher (at least
   495      no breaking of additional proofs in the entire HOL distribution, as
   496      of 5 March 2004, was observed). *)
   497 end
   498 *}
   499 
   500 
   501 subsection {* Bounded quantifiers *}
   502 
   503 syntax
   504   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3ALL _<_./ _)"  [0, 0, 10] 10)
   505   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3EX _<_./ _)"  [0, 0, 10] 10)
   506   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _<=_./ _)" [0, 0, 10] 10)
   507   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _<=_./ _)" [0, 0, 10] 10)
   508 
   509   "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3ALL _>_./ _)"  [0, 0, 10] 10)
   510   "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3EX _>_./ _)"  [0, 0, 10] 10)
   511   "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _>=_./ _)" [0, 0, 10] 10)
   512   "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _>=_./ _)" [0, 0, 10] 10)
   513 
   514 syntax (xsymbols)
   515   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
   516   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
   517   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
   518   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
   519 
   520   "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
   521   "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
   522   "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
   523   "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
   524 
   525 syntax (HOL)
   526   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3! _<_./ _)"  [0, 0, 10] 10)
   527   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3? _<_./ _)"  [0, 0, 10] 10)
   528   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3! _<=_./ _)" [0, 0, 10] 10)
   529   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3? _<=_./ _)" [0, 0, 10] 10)
   530 
   531 syntax (HTML output)
   532   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
   533   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
   534   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
   535   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
   536 
   537   "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
   538   "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
   539   "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
   540   "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
   541 
   542 translations
   543   "ALL x<y. P"   =>  "ALL x. x < y \<longrightarrow> P"
   544   "EX x<y. P"    =>  "EX x. x < y \<and> P"
   545   "ALL x<=y. P"  =>  "ALL x. x <= y \<longrightarrow> P"
   546   "EX x<=y. P"   =>  "EX x. x <= y \<and> P"
   547   "ALL x>y. P"   =>  "ALL x. x > y \<longrightarrow> P"
   548   "EX x>y. P"    =>  "EX x. x > y \<and> P"
   549   "ALL x>=y. P"  =>  "ALL x. x >= y \<longrightarrow> P"
   550   "EX x>=y. P"   =>  "EX x. x >= y \<and> P"
   551 
   552 print_translation {*
   553 let
   554   val All_binder = Syntax.binder_name @{const_syntax All};
   555   val Ex_binder = Syntax.binder_name @{const_syntax Ex};
   556   val impl = @{const_syntax "op -->"};
   557   val conj = @{const_syntax "op &"};
   558   val less = @{const_syntax less};
   559   val less_eq = @{const_syntax less_eq};
   560 
   561   val trans =
   562    [((All_binder, impl, less), ("_All_less", "_All_greater")),
   563     ((All_binder, impl, less_eq), ("_All_less_eq", "_All_greater_eq")),
   564     ((Ex_binder, conj, less), ("_Ex_less", "_Ex_greater")),
   565     ((Ex_binder, conj, less_eq), ("_Ex_less_eq", "_Ex_greater_eq"))];
   566 
   567   fun matches_bound v t = 
   568      case t of (Const ("_bound", _) $ Free (v', _)) => (v = v')
   569               | _ => false
   570   fun contains_var v = Term.exists_subterm (fn Free (x, _) => x = v | _ => false)
   571   fun mk v c n P = Syntax.const c $ Syntax.mark_bound v $ n $ P
   572 
   573   fun tr' q = (q,
   574     fn [Const ("_bound", _) $ Free (v, _), Const (c, _) $ (Const (d, _) $ t $ u) $ P] =>
   575       (case AList.lookup (op =) trans (q, c, d) of
   576         NONE => raise Match
   577       | SOME (l, g) =>
   578           if matches_bound v t andalso not (contains_var v u) then mk v l u P
   579           else if matches_bound v u andalso not (contains_var v t) then mk v g t P
   580           else raise Match)
   581      | _ => raise Match);
   582 in [tr' All_binder, tr' Ex_binder] end
   583 *}
   584 
   585 
   586 subsection {* Transitivity reasoning *}
   587 
   588 lemma ord_le_eq_trans: "a <= b ==> b = c ==> a <= c"
   589   by (rule subst)
   590 
   591 lemma ord_eq_le_trans: "a = b ==> b <= c ==> a <= c"
   592   by (rule ssubst)
   593 
   594 lemma ord_less_eq_trans: "a < b ==> b = c ==> a < c"
   595   by (rule subst)
   596 
   597 lemma ord_eq_less_trans: "a = b ==> b < c ==> a < c"
   598   by (rule ssubst)
   599 
   600 lemma order_less_subst2: "(a::'a::order) < b ==> f b < (c::'c::order) ==>
   601   (!!x y. x < y ==> f x < f y) ==> f a < c"
   602 proof -
   603   assume r: "!!x y. x < y ==> f x < f y"
   604   assume "a < b" hence "f a < f b" by (rule r)
   605   also assume "f b < c"
   606   finally (order_less_trans) show ?thesis .
   607 qed
   608 
   609 lemma order_less_subst1: "(a::'a::order) < f b ==> (b::'b::order) < c ==>
   610   (!!x y. x < y ==> f x < f y) ==> a < f c"
   611 proof -
   612   assume r: "!!x y. x < y ==> f x < f y"
   613   assume "a < f b"
   614   also assume "b < c" hence "f b < f c" by (rule r)
   615   finally (order_less_trans) show ?thesis .
   616 qed
   617 
   618 lemma order_le_less_subst2: "(a::'a::order) <= b ==> f b < (c::'c::order) ==>
   619   (!!x y. x <= y ==> f x <= f y) ==> f a < c"
   620 proof -
   621   assume r: "!!x y. x <= y ==> f x <= f y"
   622   assume "a <= b" hence "f a <= f b" by (rule r)
   623   also assume "f b < c"
   624   finally (order_le_less_trans) show ?thesis .
   625 qed
   626 
   627 lemma order_le_less_subst1: "(a::'a::order) <= f b ==> (b::'b::order) < c ==>
   628   (!!x y. x < y ==> f x < f y) ==> a < f c"
   629 proof -
   630   assume r: "!!x y. x < y ==> f x < f y"
   631   assume "a <= f b"
   632   also assume "b < c" hence "f b < f c" by (rule r)
   633   finally (order_le_less_trans) show ?thesis .
   634 qed
   635 
   636 lemma order_less_le_subst2: "(a::'a::order) < b ==> f b <= (c::'c::order) ==>
   637   (!!x y. x < y ==> f x < f y) ==> f a < c"
   638 proof -
   639   assume r: "!!x y. x < y ==> f x < f y"
   640   assume "a < b" hence "f a < f b" by (rule r)
   641   also assume "f b <= c"
   642   finally (order_less_le_trans) show ?thesis .
   643 qed
   644 
   645 lemma order_less_le_subst1: "(a::'a::order) < f b ==> (b::'b::order) <= c ==>
   646   (!!x y. x <= y ==> f x <= f y) ==> a < f c"
   647 proof -
   648   assume r: "!!x y. x <= y ==> f x <= f y"
   649   assume "a < f b"
   650   also assume "b <= c" hence "f b <= f c" by (rule r)
   651   finally (order_less_le_trans) show ?thesis .
   652 qed
   653 
   654 lemma order_subst1: "(a::'a::order) <= f b ==> (b::'b::order) <= c ==>
   655   (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
   656 proof -
   657   assume r: "!!x y. x <= y ==> f x <= f y"
   658   assume "a <= f b"
   659   also assume "b <= c" hence "f b <= f c" by (rule r)
   660   finally (order_trans) show ?thesis .
   661 qed
   662 
   663 lemma order_subst2: "(a::'a::order) <= b ==> f b <= (c::'c::order) ==>
   664   (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
   665 proof -
   666   assume r: "!!x y. x <= y ==> f x <= f y"
   667   assume "a <= b" hence "f a <= f b" by (rule r)
   668   also assume "f b <= c"
   669   finally (order_trans) show ?thesis .
   670 qed
   671 
   672 lemma ord_le_eq_subst: "a <= b ==> f b = c ==>
   673   (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
   674 proof -
   675   assume r: "!!x y. x <= y ==> f x <= f y"
   676   assume "a <= b" hence "f a <= f b" by (rule r)
   677   also assume "f b = c"
   678   finally (ord_le_eq_trans) show ?thesis .
   679 qed
   680 
   681 lemma ord_eq_le_subst: "a = f b ==> b <= c ==>
   682   (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
   683 proof -
   684   assume r: "!!x y. x <= y ==> f x <= f y"
   685   assume "a = f b"
   686   also assume "b <= c" hence "f b <= f c" by (rule r)
   687   finally (ord_eq_le_trans) show ?thesis .
   688 qed
   689 
   690 lemma ord_less_eq_subst: "a < b ==> f b = c ==>
   691   (!!x y. x < y ==> f x < f y) ==> f a < c"
   692 proof -
   693   assume r: "!!x y. x < y ==> f x < f y"
   694   assume "a < b" hence "f a < f b" by (rule r)
   695   also assume "f b = c"
   696   finally (ord_less_eq_trans) show ?thesis .
   697 qed
   698 
   699 lemma ord_eq_less_subst: "a = f b ==> b < c ==>
   700   (!!x y. x < y ==> f x < f y) ==> a < f c"
   701 proof -
   702   assume r: "!!x y. x < y ==> f x < f y"
   703   assume "a = f b"
   704   also assume "b < c" hence "f b < f c" by (rule r)
   705   finally (ord_eq_less_trans) show ?thesis .
   706 qed
   707 
   708 text {*
   709   Note that this list of rules is in reverse order of priorities.
   710 *}
   711 
   712 lemmas order_trans_rules [trans] =
   713   order_less_subst2
   714   order_less_subst1
   715   order_le_less_subst2
   716   order_le_less_subst1
   717   order_less_le_subst2
   718   order_less_le_subst1
   719   order_subst2
   720   order_subst1
   721   ord_le_eq_subst
   722   ord_eq_le_subst
   723   ord_less_eq_subst
   724   ord_eq_less_subst
   725   forw_subst
   726   back_subst
   727   rev_mp
   728   mp
   729   order_neq_le_trans
   730   order_le_neq_trans
   731   order_less_trans
   732   order_less_asym'
   733   order_le_less_trans
   734   order_less_le_trans
   735   order_trans
   736   order_antisym
   737   ord_le_eq_trans
   738   ord_eq_le_trans
   739   ord_less_eq_trans
   740   ord_eq_less_trans
   741   trans
   742 
   743 
   744 (* FIXME cleanup *)
   745 
   746 text {* These support proving chains of decreasing inequalities
   747     a >= b >= c ... in Isar proofs. *}
   748 
   749 lemma xt1:
   750   "a = b ==> b > c ==> a > c"
   751   "a > b ==> b = c ==> a > c"
   752   "a = b ==> b >= c ==> a >= c"
   753   "a >= b ==> b = c ==> a >= c"
   754   "(x::'a::order) >= y ==> y >= x ==> x = y"
   755   "(x::'a::order) >= y ==> y >= z ==> x >= z"
   756   "(x::'a::order) > y ==> y >= z ==> x > z"
   757   "(x::'a::order) >= y ==> y > z ==> x > z"
   758   "(a::'a::order) > b ==> b > a ==> ?P"
   759   "(x::'a::order) > y ==> y > z ==> x > z"
   760   "(a::'a::order) >= b ==> a ~= b ==> a > b"
   761   "(a::'a::order) ~= b ==> a >= b ==> a > b"
   762   "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c" 
   763   "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
   764   "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
   765   "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
   766 by auto
   767 
   768 lemma xt2:
   769   "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
   770 by (subgoal_tac "f b >= f c", force, force)
   771 
   772 lemma xt3: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==> 
   773     (!!x y. x >= y ==> f x >= f y) ==> f a >= c"
   774 by (subgoal_tac "f a >= f b", force, force)
   775 
   776 lemma xt4: "(a::'a::order) > f b ==> (b::'b::order) >= c ==>
   777   (!!x y. x >= y ==> f x >= f y) ==> a > f c"
   778 by (subgoal_tac "f b >= f c", force, force)
   779 
   780 lemma xt5: "(a::'a::order) > b ==> (f b::'b::order) >= c==>
   781     (!!x y. x > y ==> f x > f y) ==> f a > c"
   782 by (subgoal_tac "f a > f b", force, force)
   783 
   784 lemma xt6: "(a::'a::order) >= f b ==> b > c ==>
   785     (!!x y. x > y ==> f x > f y) ==> a > f c"
   786 by (subgoal_tac "f b > f c", force, force)
   787 
   788 lemma xt7: "(a::'a::order) >= b ==> (f b::'b::order) > c ==>
   789     (!!x y. x >= y ==> f x >= f y) ==> f a > c"
   790 by (subgoal_tac "f a >= f b", force, force)
   791 
   792 lemma xt8: "(a::'a::order) > f b ==> (b::'b::order) > c ==>
   793     (!!x y. x > y ==> f x > f y) ==> a > f c"
   794 by (subgoal_tac "f b > f c", force, force)
   795 
   796 lemma xt9: "(a::'a::order) > b ==> (f b::'b::order) > c ==>
   797     (!!x y. x > y ==> f x > f y) ==> f a > c"
   798 by (subgoal_tac "f a > f b", force, force)
   799 
   800 lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9
   801 
   802 (* 
   803   Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands
   804   for the wrong thing in an Isar proof.
   805 
   806   The extra transitivity rules can be used as follows: 
   807 
   808 lemma "(a::'a::order) > z"
   809 proof -
   810   have "a >= b" (is "_ >= ?rhs")
   811     sorry
   812   also have "?rhs >= c" (is "_ >= ?rhs")
   813     sorry
   814   also (xtrans) have "?rhs = d" (is "_ = ?rhs")
   815     sorry
   816   also (xtrans) have "?rhs >= e" (is "_ >= ?rhs")
   817     sorry
   818   also (xtrans) have "?rhs > f" (is "_ > ?rhs")
   819     sorry
   820   also (xtrans) have "?rhs > z"
   821     sorry
   822   finally (xtrans) show ?thesis .
   823 qed
   824 
   825   Alternatively, one can use "declare xtrans [trans]" and then
   826   leave out the "(xtrans)" above.
   827 *)
   828 
   829 subsection {* Order on bool *}
   830 
   831 instance bool :: order 
   832   le_bool_def: "P \<le> Q \<equiv> P \<longrightarrow> Q"
   833   less_bool_def: "P < Q \<equiv> P \<le> Q \<and> P \<noteq> Q"
   834   by intro_classes (auto simp add: le_bool_def less_bool_def)
   835 
   836 lemma le_boolI: "(P \<Longrightarrow> Q) \<Longrightarrow> P \<le> Q"
   837   by (simp add: le_bool_def)
   838 
   839 lemma le_boolI': "P \<longrightarrow> Q \<Longrightarrow> P \<le> Q"
   840   by (simp add: le_bool_def)
   841 
   842 lemma le_boolE: "P \<le> Q \<Longrightarrow> P \<Longrightarrow> (Q \<Longrightarrow> R) \<Longrightarrow> R"
   843   by (simp add: le_bool_def)
   844 
   845 lemma le_boolD: "P \<le> Q \<Longrightarrow> P \<longrightarrow> Q"
   846   by (simp add: le_bool_def)
   847 
   848 lemma [code func]:
   849   "False \<le> b \<longleftrightarrow> True"
   850   "True \<le> b \<longleftrightarrow> b"
   851   "False < b \<longleftrightarrow> b"
   852   "True < b \<longleftrightarrow> False"
   853   unfolding le_bool_def less_bool_def by simp_all
   854 
   855 
   856 subsection {* Monotonicity, syntactic least value operator and min/max *}
   857 
   858 locale mono =
   859   fixes f
   860   assumes mono: "A \<le> B \<Longrightarrow> f A \<le> f B"
   861 
   862 lemmas monoI [intro?] = mono.intro
   863   and monoD [dest?] = mono.mono
   864 
   865 constdefs
   866   Least :: "('a::ord => bool) => 'a"               (binder "LEAST " 10)
   867   "Least P == THE x. P x & (ALL y. P y --> x <= y)"
   868     -- {* We can no longer use LeastM because the latter requires Hilbert-AC. *}
   869 
   870 lemma LeastI2_order:
   871   "[| P (x::'a::order);
   872       !!y. P y ==> x <= y;
   873       !!x. [| P x; ALL y. P y --> x \<le> y |] ==> Q x |]
   874    ==> Q (Least P)"
   875   apply (unfold Least_def)
   876   apply (rule theI2)
   877     apply (blast intro: order_antisym)+
   878   done
   879 
   880 lemma Least_equality:
   881     "[| P (k::'a::order); !!x. P x ==> k <= x |] ==> (LEAST x. P x) = k"
   882   apply (simp add: Least_def)
   883   apply (rule the_equality)
   884   apply (auto intro!: order_antisym)
   885   done
   886 
   887 lemma min_leastL: "(!!x. least <= x) ==> min least x = least"
   888   by (simp add: min_def)
   889 
   890 lemma max_leastL: "(!!x. least <= x) ==> max least x = x"
   891   by (simp add: max_def)
   892 
   893 lemma min_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> min x least = least"
   894   apply (simp add: min_def)
   895   apply (blast intro: order_antisym)
   896   done
   897 
   898 lemma max_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> max x least = x"
   899   apply (simp add: max_def)
   900   apply (blast intro: order_antisym)
   901   done
   902 
   903 lemma min_of_mono:
   904     "(!!x y. (f x <= f y) = (x <= y)) ==> min (f m) (f n) = f (min m n)"
   905   by (simp add: min_def)
   906 
   907 lemma max_of_mono:
   908     "(!!x y. (f x <= f y) = (x <= y)) ==> max (f m) (f n) = f (max m n)"
   909   by (simp add: max_def)
   910 
   911 
   912 subsection {* legacy ML bindings *}
   913 
   914 ML {*
   915 val monoI = @{thm monoI};
   916 *}
   917 
   918 end