src/HOL/arith_data.ML
author wenzelm
Mon Oct 17 23:10:10 2005 +0200 (2005-10-17)
changeset 17875 d81094515061
parent 17611 61556de6ef46
child 17951 ff954cc338c7
permissions -rw-r--r--
change_claset/simpset;
Simplifier.inherit_context instead of Simplifier.inherit_bounds;
     1 (*  Title:      HOL/arith_data.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, Stefan Berghofer and Tobias Nipkow
     4 
     5 Various arithmetic proof procedures.
     6 *)
     7 
     8 (*---------------------------------------------------------------------------*)
     9 (* 1. Cancellation of common terms                                           *)
    10 (*---------------------------------------------------------------------------*)
    11 
    12 structure NatArithUtils =
    13 struct
    14 
    15 (** abstract syntax of structure nat: 0, Suc, + **)
    16 
    17 (* mk_sum, mk_norm_sum *)
    18 
    19 val one = HOLogic.mk_nat 1;
    20 val mk_plus = HOLogic.mk_binop "op +";
    21 
    22 fun mk_sum [] = HOLogic.zero
    23   | mk_sum [t] = t
    24   | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
    25 
    26 (*normal form of sums: Suc (... (Suc (a + (b + ...))))*)
    27 fun mk_norm_sum ts =
    28   let val (ones, sums) = List.partition (equal one) ts in
    29     funpow (length ones) HOLogic.mk_Suc (mk_sum sums)
    30   end;
    31 
    32 
    33 (* dest_sum *)
    34 
    35 val dest_plus = HOLogic.dest_bin "op +" HOLogic.natT;
    36 
    37 fun dest_sum tm =
    38   if HOLogic.is_zero tm then []
    39   else
    40     (case try HOLogic.dest_Suc tm of
    41       SOME t => one :: dest_sum t
    42     | NONE =>
    43         (case try dest_plus tm of
    44           SOME (t, u) => dest_sum t @ dest_sum u
    45         | NONE => [tm]));
    46 
    47 
    48 (** generic proof tools **)
    49 
    50 (* prove conversions *)
    51 
    52 val mk_eqv = HOLogic.mk_Trueprop o HOLogic.mk_eq;
    53 
    54 fun prove_conv expand_tac norm_tac sg ss tu =
    55   mk_meta_eq (prove_goalw_cterm_nocheck [] (cterm_of sg (mk_eqv tu))
    56     (K [expand_tac, norm_tac ss]))
    57   handle ERROR => error ("The error(s) above occurred while trying to prove " ^
    58     (string_of_cterm (cterm_of sg (mk_eqv tu))));
    59 
    60 val subst_equals = prove_goal HOL.thy "[| t = s; u = t |] ==> u = s"
    61   (fn prems => [cut_facts_tac prems 1, SIMPSET' asm_simp_tac 1]);
    62 
    63 
    64 (* rewriting *)
    65 
    66 fun simp_all_tac rules ss =
    67   ALLGOALS (simp_tac (Simplifier.inherit_context ss HOL_ss addsimps rules));
    68 
    69 val add_rules = [add_Suc, add_Suc_right, add_0, add_0_right];
    70 val mult_rules = [mult_Suc, mult_Suc_right, mult_0, mult_0_right];
    71 
    72 fun prep_simproc (name, pats, proc) =
    73   Simplifier.simproc (the_context ()) name pats proc;
    74 
    75 end;
    76 
    77 signature ARITH_DATA =
    78 sig
    79   val nat_cancel_sums_add: simproc list
    80   val nat_cancel_sums: simproc list
    81 end;
    82 
    83 structure ArithData: ARITH_DATA =
    84 struct
    85 
    86 open NatArithUtils;
    87 
    88 
    89 (** cancel common summands **)
    90 
    91 structure Sum =
    92 struct
    93   val mk_sum = mk_norm_sum;
    94   val dest_sum = dest_sum;
    95   val prove_conv = prove_conv;
    96   fun norm_tac ss = simp_all_tac add_rules ss THEN simp_all_tac add_ac ss;
    97 end;
    98 
    99 fun gen_uncancel_tac rule ct =
   100   rtac (instantiate' [] [NONE, SOME ct] (rule RS subst_equals)) 1;
   101 
   102 
   103 (* nat eq *)
   104 
   105 structure EqCancelSums = CancelSumsFun
   106 (struct
   107   open Sum;
   108   val mk_bal = HOLogic.mk_eq;
   109   val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT;
   110   val uncancel_tac = gen_uncancel_tac nat_add_left_cancel;
   111 end);
   112 
   113 
   114 (* nat less *)
   115 
   116 structure LessCancelSums = CancelSumsFun
   117 (struct
   118   open Sum;
   119   val mk_bal = HOLogic.mk_binrel "op <";
   120   val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT;
   121   val uncancel_tac = gen_uncancel_tac nat_add_left_cancel_less;
   122 end);
   123 
   124 
   125 (* nat le *)
   126 
   127 structure LeCancelSums = CancelSumsFun
   128 (struct
   129   open Sum;
   130   val mk_bal = HOLogic.mk_binrel "op <=";
   131   val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT;
   132   val uncancel_tac = gen_uncancel_tac nat_add_left_cancel_le;
   133 end);
   134 
   135 
   136 (* nat diff *)
   137 
   138 structure DiffCancelSums = CancelSumsFun
   139 (struct
   140   open Sum;
   141   val mk_bal = HOLogic.mk_binop "op -";
   142   val dest_bal = HOLogic.dest_bin "op -" HOLogic.natT;
   143   val uncancel_tac = gen_uncancel_tac diff_cancel;
   144 end);
   145 
   146 
   147 
   148 (** prepare nat_cancel simprocs **)
   149 
   150 val nat_cancel_sums_add = map prep_simproc
   151   [("nateq_cancel_sums",
   152      ["(l::nat) + m = n", "(l::nat) = m + n", "Suc m = n", "m = Suc n"], EqCancelSums.proc),
   153    ("natless_cancel_sums",
   154      ["(l::nat) + m < n", "(l::nat) < m + n", "Suc m < n", "m < Suc n"], LessCancelSums.proc),
   155    ("natle_cancel_sums",
   156      ["(l::nat) + m <= n", "(l::nat) <= m + n", "Suc m <= n", "m <= Suc n"], LeCancelSums.proc)];
   157 
   158 val nat_cancel_sums = nat_cancel_sums_add @
   159   [prep_simproc ("natdiff_cancel_sums",
   160     ["((l::nat) + m) - n", "(l::nat) - (m + n)", "Suc m - n", "m - Suc n"], DiffCancelSums.proc)];
   161 
   162 end;
   163 
   164 open ArithData;
   165 
   166 
   167 (*---------------------------------------------------------------------------*)
   168 (* 2. Linear arithmetic                                                      *)
   169 (*---------------------------------------------------------------------------*)
   170 
   171 (* Parameters data for general linear arithmetic functor *)
   172 
   173 structure LA_Logic: LIN_ARITH_LOGIC =
   174 struct
   175 val ccontr = ccontr;
   176 val conjI = conjI;
   177 val notI = notI;
   178 val sym = sym;
   179 val not_lessD = linorder_not_less RS iffD1;
   180 val not_leD = linorder_not_le RS iffD1;
   181 
   182 
   183 fun mk_Eq thm = (thm RS Eq_FalseI) handle THM _ => (thm RS Eq_TrueI);
   184 
   185 val mk_Trueprop = HOLogic.mk_Trueprop;
   186 
   187 fun atomize thm = case #prop(rep_thm thm) of
   188     Const("Trueprop",_) $ (Const("op &",_) $ _ $ _) =>
   189     atomize(thm RS conjunct1) @ atomize(thm RS conjunct2)
   190   | _ => [thm];
   191 
   192 fun neg_prop(TP$(Const("Not",_)$t)) = TP$t
   193   | neg_prop(TP$t) = TP $ (Const("Not",HOLogic.boolT-->HOLogic.boolT)$t);
   194 
   195 fun is_False thm =
   196   let val _ $ t = #prop(rep_thm thm)
   197   in t = Const("False",HOLogic.boolT) end;
   198 
   199 fun is_nat(t) = fastype_of1 t = HOLogic.natT;
   200 
   201 fun mk_nat_thm sg t =
   202   let val ct = cterm_of sg t  and cn = cterm_of sg (Var(("n",0),HOLogic.natT))
   203   in instantiate ([],[(cn,ct)]) le0 end;
   204 
   205 end;
   206 
   207 
   208 (* arith theory data *)
   209 
   210 structure ArithTheoryData = TheoryDataFun
   211 (struct
   212   val name = "HOL/arith";
   213   type T = {splits: thm list, inj_consts: (string * typ)list, discrete: string  list, presburger: (int -> tactic) option};
   214 
   215   val empty = {splits = [], inj_consts = [], discrete = [], presburger = NONE};
   216   val copy = I;
   217   val extend = I;
   218   fun merge _ ({splits= splits1, inj_consts= inj_consts1, discrete= discrete1, presburger= presburger1},
   219              {splits= splits2, inj_consts= inj_consts2, discrete= discrete2, presburger= presburger2}) =
   220    {splits = Drule.merge_rules (splits1, splits2),
   221     inj_consts = merge_lists inj_consts1 inj_consts2,
   222     discrete = merge_lists discrete1 discrete2,
   223     presburger = (case presburger1 of NONE => presburger2 | p => p)};
   224   fun print _ _ = ();
   225 end);
   226 
   227 fun arith_split_add (thy, thm) = (ArithTheoryData.map (fn {splits,inj_consts,discrete,presburger} =>
   228   {splits= thm::splits, inj_consts= inj_consts, discrete= discrete, presburger= presburger}) thy, thm);
   229 
   230 fun arith_discrete d = ArithTheoryData.map (fn {splits,inj_consts,discrete,presburger} =>
   231   {splits = splits, inj_consts = inj_consts, discrete = d :: discrete, presburger= presburger});
   232 
   233 fun arith_inj_const c = ArithTheoryData.map (fn {splits,inj_consts,discrete,presburger} =>
   234   {splits = splits, inj_consts = c :: inj_consts, discrete = discrete, presburger = presburger});
   235 
   236 
   237 structure LA_Data_Ref: LIN_ARITH_DATA =
   238 struct
   239 
   240 (* Decomposition of terms *)
   241 
   242 fun nT (Type("fun",[N,_])) = N = HOLogic.natT
   243   | nT _ = false;
   244 
   245 fun add_atom(t,m,(p,i)) = (case AList.lookup (op =) p t of NONE => ((t, m) :: p, i)
   246                            | SOME n => (AList.update (op =) (t, ratadd (n, m)) p, i));
   247 
   248 exception Zero;
   249 
   250 fun rat_of_term(numt,dent) =
   251   let val num = HOLogic.dest_binum numt and den = HOLogic.dest_binum dent
   252   in if den = 0 then raise Zero else int_ratdiv(num,den) end;
   253 
   254 (* Warning: in rare cases number_of encloses a non-numeral,
   255    in which case dest_binum raises TERM; hence all the handles below.
   256    Same for Suc-terms that turn out not to be numerals -
   257    although the simplifier should eliminate those anyway...
   258 *)
   259 
   260 fun number_of_Sucs (Const("Suc",_) $ n) = number_of_Sucs n + 1
   261   | number_of_Sucs t = if HOLogic.is_zero t then 0
   262                        else raise TERM("number_of_Sucs",[])
   263 
   264 (* decompose nested multiplications, bracketing them to the right and combining all
   265    their coefficients
   266 *)
   267 
   268 fun demult inj_consts =
   269 let
   270 fun demult((mC as Const("op *",_)) $ s $ t,m) = ((case s of
   271         Const("Numeral.number_of",_)$n
   272         => demult(t,ratmul(m,rat_of_intinf(HOLogic.dest_binum n)))
   273       | Const("uminus",_)$(Const("Numeral.number_of",_)$n)
   274         => demult(t,ratmul(m,rat_of_intinf(~(HOLogic.dest_binum n))))
   275       | Const("Suc",_) $ _
   276         => demult(t,ratmul(m,rat_of_int(number_of_Sucs s)))
   277       | Const("op *",_) $ s1 $ s2 => demult(mC $ s1 $ (mC $ s2 $ t),m)
   278       | Const("HOL.divide",_) $ numt $ (Const("Numeral.number_of",_)$dent) =>
   279           let val den = HOLogic.dest_binum dent
   280           in if den = 0 then raise Zero
   281              else demult(mC $ numt $ t,ratmul(m, ratinv(rat_of_intinf den)))
   282           end
   283       | _ => atomult(mC,s,t,m)
   284       ) handle TERM _ => atomult(mC,s,t,m))
   285   | demult(atom as Const("HOL.divide",_) $ t $ (Const("Numeral.number_of",_)$dent), m) =
   286       (let val den = HOLogic.dest_binum dent
   287        in if den = 0 then raise Zero else demult(t,ratmul(m, ratinv(rat_of_intinf den))) end
   288        handle TERM _ => (SOME atom,m))
   289   | demult(Const("0",_),m) = (NONE, rat_of_int 0)
   290   | demult(Const("1",_),m) = (NONE, m)
   291   | demult(t as Const("Numeral.number_of",_)$n,m) =
   292       ((NONE,ratmul(m,rat_of_intinf(HOLogic.dest_binum n)))
   293        handle TERM _ => (SOME t,m))
   294   | demult(Const("uminus",_)$t, m) = demult(t,ratmul(m,rat_of_int(~1)))
   295   | demult(t as Const f $ x, m) =
   296       (if f mem inj_consts then SOME x else SOME t,m)
   297   | demult(atom,m) = (SOME atom,m)
   298 
   299 and atomult(mC,atom,t,m) = (case demult(t,m) of (NONE,m') => (SOME atom,m')
   300                             | (SOME t',m') => (SOME(mC $ atom $ t'),m'))
   301 in demult end;
   302 
   303 fun decomp2 inj_consts (rel,lhs,rhs) =
   304 let
   305 (* Turn term into list of summand * multiplicity plus a constant *)
   306 fun poly(Const("op +",_) $ s $ t, m, pi) = poly(s,m,poly(t,m,pi))
   307   | poly(all as Const("op -",T) $ s $ t, m, pi) =
   308       if nT T then add_atom(all,m,pi) else poly(s,m,poly(t,ratneg m,pi))
   309   | poly(all as Const("uminus",T) $ t, m, pi) =
   310       if nT T then add_atom(all,m,pi) else poly(t,ratneg m,pi)
   311   | poly(Const("0",_), _, pi) = pi
   312   | poly(Const("1",_), m, (p,i)) = (p,ratadd(i,m))
   313   | poly(Const("Suc",_)$t, m, (p,i)) = poly(t, m, (p,ratadd(i,m)))
   314   | poly(t as Const("op *",_) $ _ $ _, m, pi as (p,i)) =
   315       (case demult inj_consts (t,m) of
   316          (NONE,m') => (p,ratadd(i,m))
   317        | (SOME u,m') => add_atom(u,m',pi))
   318   | poly(t as Const("HOL.divide",_) $ _ $ _, m, pi as (p,i)) =
   319       (case demult inj_consts (t,m) of
   320          (NONE,m') => (p,ratadd(i,m'))
   321        | (SOME u,m') => add_atom(u,m',pi))
   322   | poly(all as (Const("Numeral.number_of",_)$t,m,(p,i))) =
   323       ((p,ratadd(i,ratmul(m,rat_of_intinf(HOLogic.dest_binum t))))
   324        handle TERM _ => add_atom all)
   325   | poly(all as Const f $ x, m, pi) =
   326       if f mem inj_consts then poly(x,m,pi) else add_atom(all,m,pi)
   327   | poly x  = add_atom x;
   328 
   329 val (p,i) = poly(lhs,rat_of_int 1,([],rat_of_int 0))
   330 and (q,j) = poly(rhs,rat_of_int 1,([],rat_of_int 0))
   331 
   332   in case rel of
   333        "op <"  => SOME(p,i,"<",q,j)
   334      | "op <=" => SOME(p,i,"<=",q,j)
   335      | "op ="  => SOME(p,i,"=",q,j)
   336      | _       => NONE
   337   end handle Zero => NONE;
   338 
   339 fun negate(SOME(x,i,rel,y,j,d)) = SOME(x,i,"~"^rel,y,j,d)
   340   | negate NONE = NONE;
   341 
   342 fun of_lin_arith_sort sg U =
   343   Type.of_sort (Sign.tsig_of sg) (U,["Ring_and_Field.ordered_idom"])
   344 
   345 fun allows_lin_arith sg discrete (U as Type(D,[])) =
   346       if of_lin_arith_sort sg U
   347       then (true, D mem discrete)
   348       else (* special cases *)
   349            if D mem discrete then (true,true) else (false,false)
   350   | allows_lin_arith sg discrete U = (of_lin_arith_sort sg U, false);
   351 
   352 fun decomp1 (sg,discrete,inj_consts) (T,xxx) =
   353   (case T of
   354      Type("fun",[U,_]) =>
   355        (case allows_lin_arith sg discrete U of
   356           (true,d) => (case decomp2 inj_consts xxx of NONE => NONE
   357                        | SOME(p,i,rel,q,j) => SOME(p,i,rel,q,j,d))
   358         | (false,_) => NONE)
   359    | _ => NONE);
   360 
   361 fun decomp2 data (_$(Const(rel,T)$lhs$rhs)) = decomp1 data (T,(rel,lhs,rhs))
   362   | decomp2 data (_$(Const("Not",_)$(Const(rel,T)$lhs$rhs))) =
   363       negate(decomp1 data (T,(rel,lhs,rhs)))
   364   | decomp2 data _ = NONE
   365 
   366 fun decomp sg =
   367   let val {discrete, inj_consts, ...} = ArithTheoryData.get sg
   368   in decomp2 (sg,discrete,inj_consts) end
   369 
   370 fun number_of(n,T) = HOLogic.number_of_const T $ (HOLogic.mk_bin n)
   371 
   372 end;
   373 
   374 
   375 structure Fast_Arith =
   376   Fast_Lin_Arith(structure LA_Logic=LA_Logic and LA_Data=LA_Data_Ref);
   377 
   378 val fast_arith_tac    = Fast_Arith.lin_arith_tac false
   379 and fast_ex_arith_tac = Fast_Arith.lin_arith_tac
   380 and trace_arith    = Fast_Arith.trace
   381 and fast_arith_neq_limit = Fast_Arith.fast_arith_neq_limit;
   382 
   383 local
   384 
   385 val isolateSuc =
   386   let val thy = theory "Nat"
   387   in prove_goal thy "Suc(i+j) = i+j + Suc 0"
   388      (fn _ => [simp_tac (simpset_of thy) 1])
   389   end;
   390 
   391 (* reduce contradictory <= to False.
   392    Most of the work is done by the cancel tactics.
   393 *)
   394 val add_rules =
   395  [add_zero_left,add_zero_right,Zero_not_Suc,Suc_not_Zero,le_0_eq,
   396   One_nat_def,isolateSuc,
   397   order_less_irrefl, zero_neq_one, zero_less_one, zero_le_one,
   398   zero_neq_one RS not_sym, not_one_le_zero, not_one_less_zero];
   399 
   400 val add_mono_thms_ordered_semiring = map (fn s => prove_goal (the_context ()) s
   401  (fn prems => [cut_facts_tac prems 1,
   402                blast_tac (claset() addIs [add_mono]) 1]))
   403 ["(i <= j) & (k <= l) ==> i + k <= j + (l::'a::pordered_ab_semigroup_add)",
   404  "(i  = j) & (k <= l) ==> i + k <= j + (l::'a::pordered_ab_semigroup_add)",
   405  "(i <= j) & (k  = l) ==> i + k <= j + (l::'a::pordered_ab_semigroup_add)",
   406  "(i  = j) & (k  = l) ==> i + k  = j + (l::'a::pordered_ab_semigroup_add)"
   407 ];
   408 
   409 val mono_ss = simpset() addsimps
   410                 [add_mono,add_strict_mono,add_less_le_mono,add_le_less_mono];
   411 
   412 val add_mono_thms_ordered_field =
   413   map (fn s => prove_goal (the_context ()) s
   414                  (fn prems => [cut_facts_tac prems 1, asm_simp_tac mono_ss 1]))
   415     ["(i<j) & (k=l)   ==> i+k < j+(l::'a::pordered_cancel_ab_semigroup_add)",
   416      "(i=j) & (k<l)   ==> i+k < j+(l::'a::pordered_cancel_ab_semigroup_add)",
   417      "(i<j) & (k<=l)  ==> i+k < j+(l::'a::pordered_cancel_ab_semigroup_add)",
   418      "(i<=j) & (k<l)  ==> i+k < j+(l::'a::pordered_cancel_ab_semigroup_add)",
   419      "(i<j) & (k<l)   ==> i+k < j+(l::'a::pordered_cancel_ab_semigroup_add)"];
   420 
   421 in
   422 
   423 val init_lin_arith_data =
   424  Fast_Arith.setup @
   425  [Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, ...} =>
   426    {add_mono_thms = add_mono_thms @
   427     add_mono_thms_ordered_semiring @ add_mono_thms_ordered_field,
   428     mult_mono_thms = mult_mono_thms,
   429     inj_thms = inj_thms,
   430     lessD = lessD @ [Suc_leI],
   431     neqE = [linorder_neqE_nat,
   432       get_thm (theory "Ring_and_Field") (Name "linorder_neqE_ordered_idom")],
   433     simpset = HOL_basic_ss addsimps add_rules
   434                    addsimprocs [ab_group_add_cancel.sum_conv,
   435                                 ab_group_add_cancel.rel_conv]
   436                    (*abel_cancel helps it work in abstract algebraic domains*)
   437                    addsimprocs nat_cancel_sums_add}),
   438   ArithTheoryData.init, arith_discrete "nat"];
   439 
   440 end;
   441 
   442 val fast_nat_arith_simproc =
   443   Simplifier.simproc (the_context ()) "fast_nat_arith"
   444     ["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"] Fast_Arith.lin_arith_prover;
   445 
   446 
   447 (* Because of fast_nat_arith_simproc, the arithmetic solver is really only
   448 useful to detect inconsistencies among the premises for subgoals which are
   449 *not* themselves (in)equalities, because the latter activate
   450 fast_nat_arith_simproc anyway. However, it seems cheaper to activate the
   451 solver all the time rather than add the additional check. *)
   452 
   453 
   454 (* arith proof method *)
   455 
   456 (* FIXME: K true should be replaced by a sensible test to speed things up
   457    in case there are lots of irrelevant terms involved;
   458    elimination of min/max can be optimized:
   459    (max m n + k <= r) = (m+k <= r & n+k <= r)
   460    (l <= min m n + k) = (l <= m+k & l <= n+k)
   461 *)
   462 local
   463 
   464 fun raw_arith_tac ex i st =
   465   refute_tac (K true)
   466    (REPEAT o split_tac (#splits (ArithTheoryData.get (Thm.theory_of_thm st))))
   467    ((REPEAT_DETERM o etac linorder_neqE) THEN' fast_ex_arith_tac ex)
   468    i st;
   469 
   470 fun presburger_tac i st =
   471   (case ArithTheoryData.get (Thm.theory_of_thm st) of
   472      {presburger = SOME tac, ...} =>
   473        (warning "Trying full Presburger arithmetic ..."; tac i st)
   474    | _ => no_tac st);
   475 
   476 in
   477 
   478 val simple_arith_tac = FIRST' [fast_arith_tac,
   479   ObjectLogic.atomize_tac THEN' raw_arith_tac true];
   480 
   481 val arith_tac = FIRST' [fast_arith_tac,
   482   ObjectLogic.atomize_tac THEN' raw_arith_tac true,
   483   presburger_tac];
   484 
   485 val silent_arith_tac = FIRST' [fast_arith_tac,
   486   ObjectLogic.atomize_tac THEN' raw_arith_tac false,
   487   presburger_tac];
   488 
   489 fun arith_method prems =
   490   Method.METHOD (fn facts => HEADGOAL (Method.insert_tac (prems @ facts) THEN' arith_tac));
   491 
   492 end;
   493 
   494 (* antisymmetry:
   495    combines x <= y (or ~(y < x)) and y <= x (or ~(x < y)) into x = y
   496 
   497 local
   498 val antisym = mk_meta_eq order_antisym
   499 val not_lessD = linorder_not_less RS iffD1
   500 fun prp t thm = (#prop(rep_thm thm) = t)
   501 in
   502 fun antisym_eq prems thm =
   503   let
   504     val r = #prop(rep_thm thm);
   505   in
   506     case r of
   507       Tr $ ((c as Const("op <=",T)) $ s $ t) =>
   508         let val r' = Tr $ (c $ t $ s)
   509         in
   510           case Library.find_first (prp r') prems of
   511             NONE =>
   512               let val r' = Tr $ (HOLogic.Not $ (Const("op <",T) $ s $ t))
   513               in case Library.find_first (prp r') prems of
   514                    NONE => []
   515                  | SOME thm' => [(thm' RS not_lessD) RS (thm RS antisym)]
   516               end
   517           | SOME thm' => [thm' RS (thm RS antisym)]
   518         end
   519     | Tr $ (Const("Not",_) $ (Const("op <",T) $ s $ t)) =>
   520         let val r' = Tr $ (Const("op <=",T) $ s $ t)
   521         in
   522           case Library.find_first (prp r') prems of
   523             NONE =>
   524               let val r' = Tr $ (HOLogic.Not $ (Const("op <",T) $ t $ s))
   525               in case Library.find_first (prp r') prems of
   526                    NONE => []
   527                  | SOME thm' =>
   528                      [(thm' RS not_lessD) RS ((thm RS not_lessD) RS antisym)]
   529               end
   530           | SOME thm' => [thm' RS ((thm RS not_lessD) RS antisym)]
   531         end
   532     | _ => []
   533   end
   534   handle THM _ => []
   535 end;
   536 *)
   537 
   538 (* theory setup *)
   539 
   540 val arith_setup =
   541   init_lin_arith_data @
   542   [fn thy => (Simplifier.change_simpset_of thy (fn ss => ss
   543     addsimprocs (nat_cancel_sums @ [fast_nat_arith_simproc])
   544     addSolver (mk_solver' "lin. arith." Fast_Arith.cut_lin_arith_tac)); thy),
   545   Method.add_methods
   546     [("arith", (arith_method o #2) oo Method.syntax Args.bang_facts,
   547       "decide linear arithmethic")],
   548   Attrib.add_attributes [("arith_split",
   549     (Attrib.no_args arith_split_add,
   550      Attrib.no_args Attrib.undef_local_attribute),
   551     "declaration of split rules for arithmetic procedure")]];