src/HOL/Tools/lin_arith.ML
author haftmann
Mon May 11 15:57:29 2009 +0200 (2009-05-11)
changeset 31101 26c7bb764a38
parent 31100 6a2e67fe4488
child 31510 e0f2bb4b0021
permissions -rw-r--r--
qualified names for Lin_Arith tactics and simprocs
     1 (*  Title:      HOL/Tools/lin_arith.ML
     2     Author:     Tjark Weber and Tobias Nipkow, TU Muenchen
     3 
     4 HOL setup for linear arithmetic (see Provers/Arith/fast_lin_arith.ML).
     5 *)
     6 
     7 signature LIN_ARITH =
     8 sig
     9   val pre_tac: Proof.context -> int -> tactic
    10   val simple_tac: Proof.context -> int -> tactic
    11   val tac: Proof.context -> int -> tactic
    12   val simproc: simpset -> term -> thm option
    13   val add_inj_thms: thm list -> Context.generic -> Context.generic
    14   val add_lessD: thm -> Context.generic -> Context.generic
    15   val add_simps: thm list -> Context.generic -> Context.generic
    16   val add_simprocs: simproc list -> Context.generic -> Context.generic
    17   val add_inj_const: string * typ -> Context.generic -> Context.generic
    18   val add_discrete_type: string -> Context.generic -> Context.generic
    19   val setup: Context.generic -> Context.generic
    20   val global_setup: theory -> theory
    21   val split_limit: int Config.T
    22   val neq_limit: int Config.T
    23   val warning_count: int ref
    24   val trace: bool ref
    25 end;
    26 
    27 structure Lin_Arith: LIN_ARITH =
    28 struct
    29 
    30 (* Parameters data for general linear arithmetic functor *)
    31 
    32 structure LA_Logic: LIN_ARITH_LOGIC =
    33 struct
    34 
    35 val ccontr = ccontr;
    36 val conjI = conjI;
    37 val notI = notI;
    38 val sym = sym;
    39 val not_lessD = @{thm linorder_not_less} RS iffD1;
    40 val not_leD = @{thm linorder_not_le} RS iffD1;
    41 
    42 fun mk_Eq thm = thm RS Eq_FalseI handle THM _ => thm RS Eq_TrueI;
    43 
    44 val mk_Trueprop = HOLogic.mk_Trueprop;
    45 
    46 fun atomize thm = case Thm.prop_of thm of
    47     Const ("Trueprop", _) $ (Const (@{const_name "op &"}, _) $ _ $ _) =>
    48     atomize (thm RS conjunct1) @ atomize (thm RS conjunct2)
    49   | _ => [thm];
    50 
    51 fun neg_prop ((TP as Const("Trueprop", _)) $ (Const (@{const_name "Not"}, _) $ t)) = TP $ t
    52   | neg_prop ((TP as Const("Trueprop", _)) $ t) = TP $ (HOLogic.Not $t)
    53   | neg_prop t = raise TERM ("neg_prop", [t]);
    54 
    55 fun is_False thm =
    56   let val _ $ t = Thm.prop_of thm
    57   in t = HOLogic.false_const end;
    58 
    59 fun is_nat t = (fastype_of1 t = HOLogic.natT);
    60 
    61 fun mk_nat_thm thy t =
    62   let
    63     val cn = cterm_of thy (Var (("n", 0), HOLogic.natT))
    64     and ct = cterm_of thy t
    65   in instantiate ([], [(cn, ct)]) @{thm le0} end;
    66 
    67 end;
    68 
    69 
    70 (* arith context data *)
    71 
    72 structure Lin_Arith_Data = GenericDataFun
    73 (
    74   type T = {splits: thm list,
    75             inj_consts: (string * typ) list,
    76             discrete: string list};
    77   val empty = {splits = [], inj_consts = [], discrete = []};
    78   val extend = I;
    79   fun merge _
    80    ({splits= splits1, inj_consts= inj_consts1, discrete= discrete1},
    81     {splits= splits2, inj_consts= inj_consts2, discrete= discrete2}) : T =
    82    {splits = Library.merge Thm.eq_thm_prop (splits1, splits2),
    83     inj_consts = Library.merge (op =) (inj_consts1, inj_consts2),
    84     discrete = Library.merge (op =) (discrete1, discrete2)};
    85 );
    86 
    87 val get_arith_data = Lin_Arith_Data.get o Context.Proof;
    88 
    89 fun add_split thm = Lin_Arith_Data.map (fn {splits, inj_consts, discrete} =>
    90   {splits = update Thm.eq_thm_prop thm splits,
    91    inj_consts = inj_consts, discrete = discrete});
    92 
    93 fun add_discrete_type d = Lin_Arith_Data.map (fn {splits, inj_consts, discrete} =>
    94   {splits = splits, inj_consts = inj_consts,
    95    discrete = update (op =) d discrete});
    96 
    97 fun add_inj_const c = Lin_Arith_Data.map (fn {splits, inj_consts, discrete} =>
    98   {splits = splits, inj_consts = update (op =) c inj_consts,
    99    discrete = discrete});
   100 
   101 val (split_limit, setup_split_limit) = Attrib.config_int "linarith_split_limit" 9;
   102 val (neq_limit, setup_neq_limit) = Attrib.config_int "linarith_neq_limit" 9;
   103 
   104 
   105 structure LA_Data =
   106 struct
   107 
   108 val fast_arith_neq_limit = neq_limit;
   109 
   110 
   111 (* Decomposition of terms *)
   112 
   113 (*internal representation of linear (in-)equations*)
   114 type decomp =
   115   ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat * bool);
   116 
   117 fun nT (Type ("fun", [N, _])) = (N = HOLogic.natT)
   118   | nT _                      = false;
   119 
   120 fun add_atom (t : term) (m : Rat.rat) (p : (term * Rat.rat) list, i : Rat.rat) :
   121              (term * Rat.rat) list * Rat.rat =
   122   case AList.lookup Pattern.aeconv p t of
   123       NONE   => ((t, m) :: p, i)
   124     | SOME n => (AList.update Pattern.aeconv (t, Rat.add n m) p, i);
   125 
   126 (* decompose nested multiplications, bracketing them to the right and combining
   127    all their coefficients
   128 
   129    inj_consts: list of constants to be ignored when encountered
   130                (e.g. arithmetic type conversions that preserve value)
   131 
   132    m: multiplicity associated with the entire product
   133 
   134    returns either (SOME term, associated multiplicity) or (NONE, constant)
   135 *)
   136 fun demult (inj_consts : (string * typ) list) : term * Rat.rat -> term option * Rat.rat =
   137 let
   138   fun demult ((mC as Const (@{const_name HOL.times}, _)) $ s $ t, m) =
   139       (case s of Const (@{const_name HOL.times}, _) $ s1 $ s2 =>
   140         (* bracketing to the right: '(s1 * s2) * t' becomes 's1 * (s2 * t)' *)
   141         demult (mC $ s1 $ (mC $ s2 $ t), m)
   142       | _ =>
   143         (* product 's * t', where either factor can be 'NONE' *)
   144         (case demult (s, m) of
   145           (SOME s', m') =>
   146             (case demult (t, m') of
   147               (SOME t', m'') => (SOME (mC $ s' $ t'), m'')
   148             | (NONE,    m'') => (SOME s', m''))
   149         | (NONE,    m') => demult (t, m')))
   150     | demult ((mC as Const (@{const_name HOL.divide}, _)) $ s $ t, m) =
   151       (* FIXME: Shouldn't we simplify nested quotients, e.g. '(s/t)/u' could
   152          become 's/(t*u)', and '(s*t)/u' could become 's*(t/u)' ?   Note that
   153          if we choose to do so here, the simpset used by arith must be able to
   154          perform the same simplifications. *)
   155       (* FIXME: Currently we treat the numerator as atomic unless the
   156          denominator can be reduced to a numeric constant.  It might be better
   157          to demult the numerator in any case, and invent a new term of the form
   158          '1 / t' if the numerator can be reduced, but the denominator cannot. *)
   159       (* FIXME: Currently we even treat the whole fraction as atomic unless the
   160          denominator can be reduced to a numeric constant.  It might be better
   161          to use the partially reduced denominator (i.e. 's / (2*t)' could be
   162          demult'ed to 's / t' with multiplicity .5).   This would require a
   163          very simple change only below, but it breaks existing proofs. *)
   164       (* quotient 's / t', where the denominator t can be NONE *)
   165       (* Note: will raise Rat.DIVZERO iff m' is Rat.zero *)
   166       (case demult (t, Rat.one) of
   167         (SOME _, _) => (SOME (mC $ s $ t), m)
   168       | (NONE,  m') => apsnd (Rat.mult (Rat.inv m')) (demult (s, m)))
   169     (* terms that evaluate to numeric constants *)
   170     | demult (Const (@{const_name HOL.uminus}, _) $ t, m) = demult (t, Rat.neg m)
   171     | demult (Const (@{const_name HOL.zero}, _), m) = (NONE, Rat.zero)
   172     | demult (Const (@{const_name HOL.one}, _), m) = (NONE, m)
   173     (*Warning: in rare cases number_of encloses a non-numeral,
   174       in which case dest_numeral raises TERM; hence all the handles below.
   175       Same for Suc-terms that turn out not to be numerals -
   176       although the simplifier should eliminate those anyway ...*)
   177     | demult (t as Const ("Int.number_class.number_of", _) $ n, m) =
   178       ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_numeral n)))
   179         handle TERM _ => (SOME t, m))
   180     | demult (t as Const (@{const_name Suc}, _) $ _, m) =
   181       ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_nat t)))
   182         handle TERM _ => (SOME t, m))
   183     (* injection constants are ignored *)
   184     | demult (t as Const f $ x, m) =
   185       if member (op =) inj_consts f then demult (x, m) else (SOME t, m)
   186     (* everything else is considered atomic *)
   187     | demult (atom, m) = (SOME atom, m)
   188 in demult end;
   189 
   190 fun decomp0 (inj_consts : (string * typ) list) (rel : string, lhs : term, rhs : term) :
   191             ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat) option =
   192 let
   193   (* Turns a term 'all' and associated multiplicity 'm' into a list 'p' of
   194      summands and associated multiplicities, plus a constant 'i' (with implicit
   195      multiplicity 1) *)
   196   fun poly (Const (@{const_name HOL.plus}, _) $ s $ t,
   197         m : Rat.rat, pi : (term * Rat.rat) list * Rat.rat) = poly (s, m, poly (t, m, pi))
   198     | poly (all as Const (@{const_name HOL.minus}, T) $ s $ t, m, pi) =
   199         if nT T then add_atom all m pi else poly (s, m, poly (t, Rat.neg m, pi))
   200     | poly (all as Const (@{const_name HOL.uminus}, T) $ t, m, pi) =
   201         if nT T then add_atom all m pi else poly (t, Rat.neg m, pi)
   202     | poly (Const (@{const_name HOL.zero}, _), _, pi) =
   203         pi
   204     | poly (Const (@{const_name HOL.one}, _), m, (p, i)) =
   205         (p, Rat.add i m)
   206     | poly (Const (@{const_name Suc}, _) $ t, m, (p, i)) =
   207         poly (t, m, (p, Rat.add i m))
   208     | poly (all as Const (@{const_name HOL.times}, _) $ _ $ _, m, pi as (p, i)) =
   209         (case demult inj_consts (all, m) of
   210            (NONE,   m') => (p, Rat.add i m')
   211          | (SOME u, m') => add_atom u m' pi)
   212     | poly (all as Const (@{const_name HOL.divide}, _) $ _ $ _, m, pi as (p, i)) =
   213         (case demult inj_consts (all, m) of
   214            (NONE,   m') => (p, Rat.add i m')
   215          | (SOME u, m') => add_atom u m' pi)
   216     | poly (all as Const ("Int.number_class.number_of", Type(_,[_,T])) $ t, m, pi as (p, i)) =
   217         (let val k = HOLogic.dest_numeral t
   218             val k2 = if k < 0 andalso T = HOLogic.natT then 0 else k
   219         in (p, Rat.add i (Rat.mult m (Rat.rat_of_int k2))) end
   220         handle TERM _ => add_atom all m pi)
   221     | poly (all as Const f $ x, m, pi) =
   222         if f mem inj_consts then poly (x, m, pi) else add_atom all m pi
   223     | poly (all, m, pi) =
   224         add_atom all m pi
   225   val (p, i) = poly (lhs, Rat.one, ([], Rat.zero))
   226   val (q, j) = poly (rhs, Rat.one, ([], Rat.zero))
   227 in
   228   case rel of
   229     @{const_name HOL.less}    => SOME (p, i, "<", q, j)
   230   | @{const_name HOL.less_eq} => SOME (p, i, "<=", q, j)
   231   | "op ="              => SOME (p, i, "=", q, j)
   232   | _                   => NONE
   233 end handle Rat.DIVZERO => NONE;
   234 
   235 fun of_lin_arith_sort thy U =
   236   Sign.of_sort thy (U, @{sort Ring_and_Field.ordered_idom});
   237 
   238 fun allows_lin_arith thy (discrete : string list) (U as Type (D, [])) : bool * bool =
   239       if of_lin_arith_sort thy U then (true, member (op =) discrete D)
   240       else if member (op =) discrete D then (true, true) else (false, false)
   241   | allows_lin_arith sg discrete U = (of_lin_arith_sort sg U, false);
   242 
   243 fun decomp_typecheck (thy, discrete, inj_consts) (T : typ, xxx) : decomp option =
   244   case T of
   245     Type ("fun", [U, _]) =>
   246       (case allows_lin_arith thy discrete U of
   247         (true, d) =>
   248           (case decomp0 inj_consts xxx of
   249             NONE                   => NONE
   250           | SOME (p, i, rel, q, j) => SOME (p, i, rel, q, j, d))
   251       | (false, _) =>
   252           NONE)
   253   | _ => NONE;
   254 
   255 fun negate (SOME (x, i, rel, y, j, d)) = SOME (x, i, "~" ^ rel, y, j, d)
   256   | negate NONE                        = NONE;
   257 
   258 fun decomp_negation data
   259   ((Const ("Trueprop", _)) $ (Const (rel, T) $ lhs $ rhs)) : decomp option =
   260       decomp_typecheck data (T, (rel, lhs, rhs))
   261   | decomp_negation data ((Const ("Trueprop", _)) $
   262   (Const ("Not", _) $ (Const (rel, T) $ lhs $ rhs))) =
   263       negate (decomp_typecheck data (T, (rel, lhs, rhs)))
   264   | decomp_negation data _ =
   265       NONE;
   266 
   267 fun decomp ctxt : term -> decomp option =
   268   let
   269     val thy = ProofContext.theory_of ctxt
   270     val {discrete, inj_consts, ...} = get_arith_data ctxt
   271   in decomp_negation (thy, discrete, inj_consts) end;
   272 
   273 fun domain_is_nat (_ $ (Const (_, T) $ _ $ _))                      = nT T
   274   | domain_is_nat (_ $ (Const ("Not", _) $ (Const (_, T) $ _ $ _))) = nT T
   275   | domain_is_nat _                                                 = false;
   276 
   277 val mk_number = HOLogic.mk_number;
   278 
   279 (*---------------------------------------------------------------------------*)
   280 (* the following code performs splitting of certain constants (e.g. min,     *)
   281 (* max) in a linear arithmetic problem; similar to what split_tac later does *)
   282 (* to the proof state                                                        *)
   283 (*---------------------------------------------------------------------------*)
   284 
   285 (* checks if splitting with 'thm' is implemented                             *)
   286 
   287 fun is_split_thm (thm : thm) : bool =
   288   case concl_of thm of _ $ (_ $ (_ $ lhs) $ _) => (
   289     (* Trueprop $ ((op =) $ (?P $ lhs) $ rhs) *)
   290     case head_of lhs of
   291       Const (a, _) => member (op =) [@{const_name Orderings.max},
   292                                     @{const_name Orderings.min},
   293                                     @{const_name HOL.abs},
   294                                     @{const_name HOL.minus},
   295                                     "Int.nat",
   296                                     "Divides.div_class.mod",
   297                                     "Divides.div_class.div"] a
   298     | _            => (warning ("Lin. Arith.: wrong format for split rule " ^
   299                                  Display.string_of_thm thm);
   300                        false))
   301   | _ => (warning ("Lin. Arith.: wrong format for split rule " ^
   302                    Display.string_of_thm thm);
   303           false);
   304 
   305 (* substitute new for occurrences of old in a term, incrementing bound       *)
   306 (* variables as needed when substituting inside an abstraction               *)
   307 
   308 fun subst_term ([] : (term * term) list) (t : term) = t
   309   | subst_term pairs                     t          =
   310       (case AList.lookup Pattern.aeconv pairs t of
   311         SOME new =>
   312           new
   313       | NONE     =>
   314           (case t of Abs (a, T, body) =>
   315             let val pairs' = map (pairself (incr_boundvars 1)) pairs
   316             in  Abs (a, T, subst_term pairs' body)  end
   317           | t1 $ t2                   =>
   318             subst_term pairs t1 $ subst_term pairs t2
   319           | _ => t));
   320 
   321 (* approximates the effect of one application of split_tac (followed by NNF  *)
   322 (* normalization) on the subgoal represented by '(Ts, terms)'; returns a     *)
   323 (* list of new subgoals (each again represented by a typ list for bound      *)
   324 (* variables and a term list for premises), or NONE if split_tac would fail  *)
   325 (* on the subgoal                                                            *)
   326 
   327 (* FIXME: currently only the effect of certain split theorems is reproduced  *)
   328 (*        (which is why we need 'is_split_thm').  A more canonical           *)
   329 (*        implementation should analyze the right-hand side of the split     *)
   330 (*        theorem that can be applied, and modify the subgoal accordingly.   *)
   331 (*        Or even better, the splitter should be extended to provide         *)
   332 (*        splitting on terms as well as splitting on theorems (where the     *)
   333 (*        former can have a faster implementation as it does not need to be  *)
   334 (*        proof-producing).                                                  *)
   335 
   336 fun split_once_items ctxt (Ts : typ list, terms : term list) :
   337                      (typ list * term list) list option =
   338 let
   339   val thy = ProofContext.theory_of ctxt
   340   (* takes a list  [t1, ..., tn]  to the term                                *)
   341   (*   tn' --> ... --> t1' --> False  ,                                      *)
   342   (* where ti' = HOLogic.dest_Trueprop ti                                    *)
   343   fun REPEAT_DETERM_etac_rev_mp terms' =
   344     fold (curry HOLogic.mk_imp) (map HOLogic.dest_Trueprop terms') HOLogic.false_const
   345   val split_thms = filter is_split_thm (#splits (get_arith_data ctxt))
   346   val cmap       = Splitter.cmap_of_split_thms split_thms
   347   val splits     = Splitter.split_posns cmap thy Ts (REPEAT_DETERM_etac_rev_mp terms)
   348   val split_limit = Config.get ctxt split_limit
   349 in
   350   if length splits > split_limit then
   351    (tracing ("linarith_split_limit exceeded (current value is " ^
   352       string_of_int split_limit ^ ")"); NONE)
   353   else (
   354   case splits of [] =>
   355     (* split_tac would fail: no possible split *)
   356     NONE
   357   | ((_, _, _, split_type, split_term) :: _) => (
   358     (* ignore all but the first possible split *)
   359     case strip_comb split_term of
   360     (* ?P (max ?i ?j) = ((?i <= ?j --> ?P ?j) & (~ ?i <= ?j --> ?P ?i)) *)
   361       (Const (@{const_name Orderings.max}, _), [t1, t2]) =>
   362       let
   363         val rev_terms     = rev terms
   364         val terms1        = map (subst_term [(split_term, t1)]) rev_terms
   365         val terms2        = map (subst_term [(split_term, t2)]) rev_terms
   366         val t1_leq_t2     = Const (@{const_name HOL.less_eq},
   367                                     split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
   368         val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
   369         val not_false     = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   370         val subgoal1      = (HOLogic.mk_Trueprop t1_leq_t2) :: terms2 @ [not_false]
   371         val subgoal2      = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms1 @ [not_false]
   372       in
   373         SOME [(Ts, subgoal1), (Ts, subgoal2)]
   374       end
   375     (* ?P (min ?i ?j) = ((?i <= ?j --> ?P ?i) & (~ ?i <= ?j --> ?P ?j)) *)
   376     | (Const (@{const_name Orderings.min}, _), [t1, t2]) =>
   377       let
   378         val rev_terms     = rev terms
   379         val terms1        = map (subst_term [(split_term, t1)]) rev_terms
   380         val terms2        = map (subst_term [(split_term, t2)]) rev_terms
   381         val t1_leq_t2     = Const (@{const_name HOL.less_eq},
   382                                     split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
   383         val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
   384         val not_false     = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   385         val subgoal1      = (HOLogic.mk_Trueprop t1_leq_t2) :: terms1 @ [not_false]
   386         val subgoal2      = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms2 @ [not_false]
   387       in
   388         SOME [(Ts, subgoal1), (Ts, subgoal2)]
   389       end
   390     (* ?P (abs ?a) = ((0 <= ?a --> ?P ?a) & (?a < 0 --> ?P (- ?a))) *)
   391     | (Const (@{const_name HOL.abs}, _), [t1]) =>
   392       let
   393         val rev_terms   = rev terms
   394         val terms1      = map (subst_term [(split_term, t1)]) rev_terms
   395         val terms2      = map (subst_term [(split_term, Const (@{const_name HOL.uminus},
   396                             split_type --> split_type) $ t1)]) rev_terms
   397         val zero        = Const (@{const_name HOL.zero}, split_type)
   398         val zero_leq_t1 = Const (@{const_name HOL.less_eq},
   399                             split_type --> split_type --> HOLogic.boolT) $ zero $ t1
   400         val t1_lt_zero  = Const (@{const_name HOL.less},
   401                             split_type --> split_type --> HOLogic.boolT) $ t1 $ zero
   402         val not_false   = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   403         val subgoal1    = (HOLogic.mk_Trueprop zero_leq_t1) :: terms1 @ [not_false]
   404         val subgoal2    = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
   405       in
   406         SOME [(Ts, subgoal1), (Ts, subgoal2)]
   407       end
   408     (* ?P (?a - ?b) = ((?a < ?b --> ?P 0) & (ALL d. ?a = ?b + d --> ?P d)) *)
   409     | (Const (@{const_name HOL.minus}, _), [t1, t2]) =>
   410       let
   411         (* "d" in the above theorem becomes a new bound variable after NNF   *)
   412         (* transformation, therefore some adjustment of indices is necessary *)
   413         val rev_terms       = rev terms
   414         val zero            = Const (@{const_name HOL.zero}, split_type)
   415         val d               = Bound 0
   416         val terms1          = map (subst_term [(split_term, zero)]) rev_terms
   417         val terms2          = map (subst_term [(incr_boundvars 1 split_term, d)])
   418                                 (map (incr_boundvars 1) rev_terms)
   419         val t1'             = incr_boundvars 1 t1
   420         val t2'             = incr_boundvars 1 t2
   421         val t1_lt_t2        = Const (@{const_name HOL.less},
   422                                 split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
   423         val t1_eq_t2_plus_d = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
   424                                 (Const (@{const_name HOL.plus},
   425                                   split_type --> split_type --> split_type) $ t2' $ d)
   426         val not_false       = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   427         val subgoal1        = (HOLogic.mk_Trueprop t1_lt_t2) :: terms1 @ [not_false]
   428         val subgoal2        = (HOLogic.mk_Trueprop t1_eq_t2_plus_d) :: terms2 @ [not_false]
   429       in
   430         SOME [(Ts, subgoal1), (split_type :: Ts, subgoal2)]
   431       end
   432     (* ?P (nat ?i) = ((ALL n. ?i = int n --> ?P n) & (?i < 0 --> ?P 0)) *)
   433     | (Const ("Int.nat", _), [t1]) =>
   434       let
   435         val rev_terms   = rev terms
   436         val zero_int    = Const (@{const_name HOL.zero}, HOLogic.intT)
   437         val zero_nat    = Const (@{const_name HOL.zero}, HOLogic.natT)
   438         val n           = Bound 0
   439         val terms1      = map (subst_term [(incr_boundvars 1 split_term, n)])
   440                             (map (incr_boundvars 1) rev_terms)
   441         val terms2      = map (subst_term [(split_term, zero_nat)]) rev_terms
   442         val t1'         = incr_boundvars 1 t1
   443         val t1_eq_int_n = Const ("op =", HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1' $
   444                             (Const (@{const_name of_nat}, HOLogic.natT --> HOLogic.intT) $ n)
   445         val t1_lt_zero  = Const (@{const_name HOL.less},
   446                             HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1 $ zero_int
   447         val not_false   = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   448         val subgoal1    = (HOLogic.mk_Trueprop t1_eq_int_n) :: terms1 @ [not_false]
   449         val subgoal2    = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
   450       in
   451         SOME [(HOLogic.natT :: Ts, subgoal1), (Ts, subgoal2)]
   452       end
   453     (* "?P ((?n::nat) mod (number_of ?k)) =
   454          ((number_of ?k = 0 --> ?P ?n) & (~ (number_of ?k = 0) -->
   455            (ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P j))) *)
   456     | (Const ("Divides.div_class.mod", Type ("fun", [Type ("nat", []), _])), [t1, t2]) =>
   457       let
   458         val rev_terms               = rev terms
   459         val zero                    = Const (@{const_name HOL.zero}, split_type)
   460         val i                       = Bound 1
   461         val j                       = Bound 0
   462         val terms1                  = map (subst_term [(split_term, t1)]) rev_terms
   463         val terms2                  = map (subst_term [(incr_boundvars 2 split_term, j)])
   464                                         (map (incr_boundvars 2) rev_terms)
   465         val t1'                     = incr_boundvars 2 t1
   466         val t2'                     = incr_boundvars 2 t2
   467         val t2_eq_zero              = Const ("op =",
   468                                         split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
   469         val t2_neq_zero             = HOLogic.mk_not (Const ("op =",
   470                                         split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
   471         val j_lt_t2                 = Const (@{const_name HOL.less},
   472                                         split_type --> split_type--> HOLogic.boolT) $ j $ t2'
   473         val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
   474                                        (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
   475                                          (Const (@{const_name HOL.times},
   476                                            split_type --> split_type --> split_type) $ t2' $ i) $ j)
   477         val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   478         val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
   479         val subgoal2                = (map HOLogic.mk_Trueprop
   480                                         [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
   481                                           @ terms2 @ [not_false]
   482       in
   483         SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
   484       end
   485     (* "?P ((?n::nat) div (number_of ?k)) =
   486          ((number_of ?k = 0 --> ?P 0) & (~ (number_of ?k = 0) -->
   487            (ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P i))) *)
   488     | (Const ("Divides.div_class.div", Type ("fun", [Type ("nat", []), _])), [t1, t2]) =>
   489       let
   490         val rev_terms               = rev terms
   491         val zero                    = Const (@{const_name HOL.zero}, split_type)
   492         val i                       = Bound 1
   493         val j                       = Bound 0
   494         val terms1                  = map (subst_term [(split_term, zero)]) rev_terms
   495         val terms2                  = map (subst_term [(incr_boundvars 2 split_term, i)])
   496                                         (map (incr_boundvars 2) rev_terms)
   497         val t1'                     = incr_boundvars 2 t1
   498         val t2'                     = incr_boundvars 2 t2
   499         val t2_eq_zero              = Const ("op =",
   500                                         split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
   501         val t2_neq_zero             = HOLogic.mk_not (Const ("op =",
   502                                         split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
   503         val j_lt_t2                 = Const (@{const_name HOL.less},
   504                                         split_type --> split_type--> HOLogic.boolT) $ j $ t2'
   505         val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
   506                                        (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
   507                                          (Const (@{const_name HOL.times},
   508                                            split_type --> split_type --> split_type) $ t2' $ i) $ j)
   509         val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   510         val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
   511         val subgoal2                = (map HOLogic.mk_Trueprop
   512                                         [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
   513                                           @ terms2 @ [not_false]
   514       in
   515         SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
   516       end
   517     (* "?P ((?n::int) mod (number_of ?k)) =
   518          ((iszero (number_of ?k) --> ?P ?n) &
   519           (neg (number_of (uminus ?k)) -->
   520             (ALL i j. 0 <= j & j < number_of ?k & ?n = number_of ?k * i + j --> ?P j)) &
   521           (neg (number_of ?k) -->
   522             (ALL i j. number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j --> ?P j))) *)
   523     | (Const ("Divides.div_class.mod",
   524         Type ("fun", [Type ("Int.int", []), _])), [t1, t2 as (number_of $ k)]) =>
   525       let
   526         val rev_terms               = rev terms
   527         val zero                    = Const (@{const_name HOL.zero}, split_type)
   528         val i                       = Bound 1
   529         val j                       = Bound 0
   530         val terms1                  = map (subst_term [(split_term, t1)]) rev_terms
   531         val terms2_3                = map (subst_term [(incr_boundvars 2 split_term, j)])
   532                                         (map (incr_boundvars 2) rev_terms)
   533         val t1'                     = incr_boundvars 2 t1
   534         val (t2' as (_ $ k'))       = incr_boundvars 2 t2
   535         val iszero_t2               = Const ("Int.iszero", split_type --> HOLogic.boolT) $ t2
   536         val neg_minus_k             = Const ("Int.neg", split_type --> HOLogic.boolT) $
   537                                         (number_of $
   538                                           (Const (@{const_name HOL.uminus},
   539                                             HOLogic.intT --> HOLogic.intT) $ k'))
   540         val zero_leq_j              = Const (@{const_name HOL.less_eq},
   541                                         split_type --> split_type --> HOLogic.boolT) $ zero $ j
   542         val j_lt_t2                 = Const (@{const_name HOL.less},
   543                                         split_type --> split_type--> HOLogic.boolT) $ j $ t2'
   544         val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
   545                                        (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
   546                                          (Const (@{const_name HOL.times},
   547                                            split_type --> split_type --> split_type) $ t2' $ i) $ j)
   548         val neg_t2                  = Const ("Int.neg", split_type --> HOLogic.boolT) $ t2'
   549         val t2_lt_j                 = Const (@{const_name HOL.less},
   550                                         split_type --> split_type--> HOLogic.boolT) $ t2' $ j
   551         val j_leq_zero              = Const (@{const_name HOL.less_eq},
   552                                         split_type --> split_type --> HOLogic.boolT) $ j $ zero
   553         val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   554         val subgoal1                = (HOLogic.mk_Trueprop iszero_t2) :: terms1 @ [not_false]
   555         val subgoal2                = (map HOLogic.mk_Trueprop [neg_minus_k, zero_leq_j])
   556                                         @ hd terms2_3
   557                                         :: (if tl terms2_3 = [] then [not_false] else [])
   558                                         @ (map HOLogic.mk_Trueprop [j_lt_t2, t1_eq_t2_times_i_plus_j])
   559                                         @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
   560         val subgoal3                = (map HOLogic.mk_Trueprop [neg_t2, t2_lt_j])
   561                                         @ hd terms2_3
   562                                         :: (if tl terms2_3 = [] then [not_false] else [])
   563                                         @ (map HOLogic.mk_Trueprop [j_leq_zero, t1_eq_t2_times_i_plus_j])
   564                                         @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
   565         val Ts'                     = split_type :: split_type :: Ts
   566       in
   567         SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
   568       end
   569     (* "?P ((?n::int) div (number_of ?k)) =
   570          ((iszero (number_of ?k) --> ?P 0) &
   571           (neg (number_of (uminus ?k)) -->
   572             (ALL i. (EX j. 0 <= j & j < number_of ?k & ?n = number_of ?k * i + j) --> ?P i)) &
   573           (neg (number_of ?k) -->
   574             (ALL i. (EX j. number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j) --> ?P i))) *)
   575     | (Const ("Divides.div_class.div",
   576         Type ("fun", [Type ("Int.int", []), _])), [t1, t2 as (number_of $ k)]) =>
   577       let
   578         val rev_terms               = rev terms
   579         val zero                    = Const (@{const_name HOL.zero}, split_type)
   580         val i                       = Bound 1
   581         val j                       = Bound 0
   582         val terms1                  = map (subst_term [(split_term, zero)]) rev_terms
   583         val terms2_3                = map (subst_term [(incr_boundvars 2 split_term, i)])
   584                                         (map (incr_boundvars 2) rev_terms)
   585         val t1'                     = incr_boundvars 2 t1
   586         val (t2' as (_ $ k'))       = incr_boundvars 2 t2
   587         val iszero_t2               = Const ("Int.iszero", split_type --> HOLogic.boolT) $ t2
   588         val neg_minus_k             = Const ("Int.neg", split_type --> HOLogic.boolT) $
   589                                         (number_of $
   590                                           (Const (@{const_name HOL.uminus},
   591                                             HOLogic.intT --> HOLogic.intT) $ k'))
   592         val zero_leq_j              = Const (@{const_name HOL.less_eq},
   593                                         split_type --> split_type --> HOLogic.boolT) $ zero $ j
   594         val j_lt_t2                 = Const (@{const_name HOL.less},
   595                                         split_type --> split_type--> HOLogic.boolT) $ j $ t2'
   596         val t1_eq_t2_times_i_plus_j = Const ("op =",
   597                                         split_type --> split_type --> HOLogic.boolT) $ t1' $
   598                                        (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
   599                                          (Const (@{const_name HOL.times},
   600                                            split_type --> split_type --> split_type) $ t2' $ i) $ j)
   601         val neg_t2                  = Const ("Int.neg", split_type --> HOLogic.boolT) $ t2'
   602         val t2_lt_j                 = Const (@{const_name HOL.less},
   603                                         split_type --> split_type--> HOLogic.boolT) $ t2' $ j
   604         val j_leq_zero              = Const (@{const_name HOL.less_eq},
   605                                         split_type --> split_type --> HOLogic.boolT) $ j $ zero
   606         val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   607         val subgoal1                = (HOLogic.mk_Trueprop iszero_t2) :: terms1 @ [not_false]
   608         val subgoal2                = (HOLogic.mk_Trueprop neg_minus_k)
   609                                         :: terms2_3
   610                                         @ not_false
   611                                         :: (map HOLogic.mk_Trueprop
   612                                              [zero_leq_j, j_lt_t2, t1_eq_t2_times_i_plus_j])
   613         val subgoal3                = (HOLogic.mk_Trueprop neg_t2)
   614                                         :: terms2_3
   615                                         @ not_false
   616                                         :: (map HOLogic.mk_Trueprop
   617                                              [t2_lt_j, j_leq_zero, t1_eq_t2_times_i_plus_j])
   618         val Ts'                     = split_type :: split_type :: Ts
   619       in
   620         SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
   621       end
   622     (* this will only happen if a split theorem can be applied for which no  *)
   623     (* code exists above -- in which case either the split theorem should be *)
   624     (* implemented above, or 'is_split_thm' should be modified to filter it  *)
   625     (* out                                                                   *)
   626     | (t, ts) => (
   627       warning ("Lin. Arith.: split rule for " ^ Syntax.string_of_term ctxt t ^
   628                " (with " ^ string_of_int (length ts) ^
   629                " argument(s)) not implemented; proof reconstruction is likely to fail");
   630       NONE
   631     ))
   632   )
   633 end;
   634 
   635 (* remove terms that do not satisfy 'p'; change the order of the remaining   *)
   636 (* terms in the same way as filter_prems_tac does                            *)
   637 
   638 fun filter_prems_tac_items (p : term -> bool) (terms : term list) : term list =
   639 let
   640   fun filter_prems (t, (left, right)) =
   641     if  p t  then  (left, right @ [t])  else  (left @ right, [])
   642   val (left, right) = List.foldl filter_prems ([], []) terms
   643 in
   644   right @ left
   645 end;
   646 
   647 (* return true iff TRY (etac notE) THEN eq_assume_tac would succeed on a     *)
   648 (* subgoal that has 'terms' as premises                                      *)
   649 
   650 fun negated_term_occurs_positively (terms : term list) : bool =
   651   List.exists
   652     (fn (Trueprop $ (Const ("Not", _) $ t)) => member Pattern.aeconv terms (Trueprop $ t)
   653       | _                                   => false)
   654     terms;
   655 
   656 fun pre_decomp ctxt (Ts : typ list, terms : term list) : (typ list * term list) list =
   657 let
   658   (* repeatedly split (including newly emerging subgoals) until no further   *)
   659   (* splitting is possible                                                   *)
   660   fun split_loop ([] : (typ list * term list) list) = ([] : (typ list * term list) list)
   661     | split_loop (subgoal::subgoals)                = (
   662         case split_once_items ctxt subgoal of
   663           SOME new_subgoals => split_loop (new_subgoals @ subgoals)
   664         | NONE              => subgoal :: split_loop subgoals
   665       )
   666   fun is_relevant t  = isSome (decomp ctxt t)
   667   (* filter_prems_tac is_relevant: *)
   668   val relevant_terms = filter_prems_tac_items is_relevant terms
   669   (* split_tac, NNF normalization: *)
   670   val split_goals    = split_loop [(Ts, relevant_terms)]
   671   (* necessary because split_once_tac may normalize terms: *)
   672   val beta_eta_norm  = map (apsnd (map (Envir.eta_contract o Envir.beta_norm))) split_goals
   673   (* TRY (etac notE) THEN eq_assume_tac: *)
   674   val result         = List.filter (not o negated_term_occurs_positively o snd) beta_eta_norm
   675 in
   676   result
   677 end;
   678 
   679 (* takes the i-th subgoal  [| A1; ...; An |] ==> B  to                       *)
   680 (* An --> ... --> A1 --> B,  performs splitting with the given 'split_thms'  *)
   681 (* (resulting in a different subgoal P), takes  P  to  ~P ==> False,         *)
   682 (* performs NNF-normalization of ~P, and eliminates conjunctions,            *)
   683 (* disjunctions and existential quantifiers from the premises, possibly (in  *)
   684 (* the case of disjunctions) resulting in several new subgoals, each of the  *)
   685 (* general form  [| Q1; ...; Qm |] ==> False.  Fails if more than            *)
   686 (* !split_limit splits are possible.                              *)
   687 
   688 local
   689   val nnf_simpset =
   690     empty_ss setmkeqTrue mk_eq_True
   691     setmksimps (mksimps mksimps_pairs)
   692     addsimps [imp_conv_disj, iff_conv_conj_imp, de_Morgan_disj, de_Morgan_conj,
   693       not_all, not_ex, not_not]
   694   fun prem_nnf_tac i st =
   695     full_simp_tac (Simplifier.theory_context (Thm.theory_of_thm st) nnf_simpset) i st
   696 in
   697 
   698 fun split_once_tac ctxt split_thms =
   699   let
   700     val thy = ProofContext.theory_of ctxt
   701     val cond_split_tac = SUBGOAL (fn (subgoal, i) =>
   702       let
   703         val Ts = rev (map snd (Logic.strip_params subgoal))
   704         val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl subgoal)
   705         val cmap = Splitter.cmap_of_split_thms split_thms
   706         val splits = Splitter.split_posns cmap thy Ts concl
   707         val split_limit = Config.get ctxt split_limit
   708       in
   709         if length splits > split_limit then no_tac
   710         else split_tac split_thms i
   711       end)
   712   in
   713     EVERY' [
   714       REPEAT_DETERM o etac rev_mp,
   715       cond_split_tac,
   716       rtac ccontr,
   717       prem_nnf_tac,
   718       TRY o REPEAT_ALL_NEW (DETERM o (eresolve_tac [conjE, exE] ORELSE' etac disjE))
   719     ]
   720   end;
   721 
   722 end;  (* local *)
   723 
   724 (* remove irrelevant premises, then split the i-th subgoal (and all new      *)
   725 (* subgoals) by using 'split_once_tac' repeatedly.  Beta-eta-normalize new   *)
   726 (* subgoals and finally attempt to solve them by finding an immediate        *)
   727 (* contradiction (i.e. a term and its negation) in their premises.           *)
   728 
   729 fun pre_tac ctxt i =
   730 let
   731   val split_thms = filter is_split_thm (#splits (get_arith_data ctxt))
   732   fun is_relevant t = isSome (decomp ctxt t)
   733 in
   734   DETERM (
   735     TRY (filter_prems_tac is_relevant i)
   736       THEN (
   737         (TRY o REPEAT_ALL_NEW (split_once_tac ctxt split_thms))
   738           THEN_ALL_NEW
   739             (CONVERSION Drule.beta_eta_conversion
   740               THEN'
   741             (TRY o (etac notE THEN' eq_assume_tac)))
   742       ) i
   743   )
   744 end;
   745 
   746 end;  (* LA_Data *)
   747 
   748 
   749 val pre_tac = LA_Data.pre_tac;
   750 
   751 structure Fast_Arith = Fast_Lin_Arith(structure LA_Logic = LA_Logic and LA_Data = LA_Data);
   752 
   753 val map_data = Fast_Arith.map_data;
   754 
   755 fun map_inj_thms f {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =
   756   {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms, inj_thms = f inj_thms,
   757     lessD = lessD, neqE = neqE, simpset = simpset};
   758 
   759 fun map_lessD f {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =
   760   {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms, inj_thms = inj_thms,
   761     lessD = f lessD, neqE = neqE, simpset = simpset};
   762 
   763 fun map_simpset f {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =
   764   {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms, inj_thms = inj_thms,
   765     lessD = lessD, neqE = neqE, simpset = f simpset};
   766 
   767 fun add_inj_thms thms = Fast_Arith.map_data (map_inj_thms (append thms));
   768 fun add_lessD thm = Fast_Arith.map_data (map_lessD (fn thms => thms @ [thm]));
   769 fun add_simps thms = Fast_Arith.map_data (map_simpset (fn simpset => simpset addsimps thms));
   770 fun add_simprocs procs = Fast_Arith.map_data (map_simpset (fn simpset => simpset addsimprocs procs));
   771 
   772 fun simple_tac ctxt = Fast_Arith.lin_arith_tac ctxt false;
   773 val lin_arith_tac = Fast_Arith.lin_arith_tac;
   774 val trace = Fast_Arith.trace;
   775 val warning_count = Fast_Arith.warning_count;
   776 
   777 (* reduce contradictory <= to False.
   778    Most of the work is done by the cancel tactics. *)
   779 
   780 val init_arith_data =
   781   Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, ...} =>
   782    {add_mono_thms = @{thms add_mono_thms_ordered_semiring} @ @{thms add_mono_thms_ordered_field} @ add_mono_thms,
   783     mult_mono_thms = @{thm mult_strict_left_mono} :: @{thm mult_left_mono} :: mult_mono_thms,
   784     inj_thms = inj_thms,
   785     lessD = lessD @ [@{thm "Suc_leI"}],
   786     neqE = [@{thm linorder_neqE_nat}, @{thm linorder_neqE_ordered_idom}],
   787     simpset = HOL_basic_ss
   788       addsimps
   789        [@{thm "monoid_add_class.add_0_left"},
   790         @{thm "monoid_add_class.add_0_right"},
   791         @{thm "Zero_not_Suc"}, @{thm "Suc_not_Zero"}, @{thm "le_0_eq"}, @{thm "One_nat_def"},
   792         @{thm "order_less_irrefl"}, @{thm "zero_neq_one"}, @{thm "zero_less_one"},
   793         @{thm "zero_le_one"}, @{thm "zero_neq_one"} RS not_sym, @{thm "not_one_le_zero"},
   794         @{thm "not_one_less_zero"}]
   795       addsimprocs [ab_group_add_cancel.sum_conv, ab_group_add_cancel.rel_conv]
   796        (*abel_cancel helps it work in abstract algebraic domains*)
   797       addsimprocs Nat_Arith.nat_cancel_sums_add
   798       addcongs [if_weak_cong]}) #>
   799   add_discrete_type @{type_name nat};
   800 
   801 fun add_arith_facts ss =
   802   add_prems (Arith_Data.get_arith_facts (MetaSimplifier.the_context ss)) ss;
   803 
   804 val simproc = add_arith_facts #> Fast_Arith.lin_arith_simproc;
   805 
   806 
   807 (* generic refutation procedure *)
   808 
   809 (* parameters:
   810 
   811    test: term -> bool
   812    tests if a term is at all relevant to the refutation proof;
   813    if not, then it can be discarded. Can improve performance,
   814    esp. if disjunctions can be discarded (no case distinction needed!).
   815 
   816    prep_tac: int -> tactic
   817    A preparation tactic to be applied to the goal once all relevant premises
   818    have been moved to the conclusion.
   819 
   820    ref_tac: int -> tactic
   821    the actual refutation tactic. Should be able to deal with goals
   822    [| A1; ...; An |] ==> False
   823    where the Ai are atomic, i.e. no top-level &, | or EX
   824 *)
   825 
   826 local
   827   val nnf_simpset =
   828     empty_ss setmkeqTrue mk_eq_True
   829     setmksimps (mksimps mksimps_pairs)
   830     addsimps [@{thm imp_conv_disj}, @{thm iff_conv_conj_imp}, @{thm de_Morgan_disj},
   831       @{thm de_Morgan_conj}, @{thm not_all}, @{thm not_ex}, @{thm not_not}];
   832   fun prem_nnf_tac i st =
   833     full_simp_tac (Simplifier.theory_context (Thm.theory_of_thm st) nnf_simpset) i st;
   834 in
   835 fun refute_tac test prep_tac ref_tac =
   836   let val refute_prems_tac =
   837         REPEAT_DETERM
   838               (eresolve_tac [@{thm conjE}, @{thm exE}] 1 ORELSE
   839                filter_prems_tac test 1 ORELSE
   840                etac @{thm disjE} 1) THEN
   841         (DETERM (etac @{thm notE} 1 THEN eq_assume_tac 1) ORELSE
   842          ref_tac 1);
   843   in EVERY'[TRY o filter_prems_tac test,
   844             REPEAT_DETERM o etac @{thm rev_mp}, prep_tac, rtac @{thm ccontr}, prem_nnf_tac,
   845             SELECT_GOAL (DEPTH_SOLVE refute_prems_tac)]
   846   end;
   847 end;
   848 
   849 
   850 (* arith proof method *)
   851 
   852 local
   853 
   854 fun raw_tac ctxt ex =
   855   (* FIXME: K true should be replaced by a sensible test (perhaps "isSome o
   856      decomp sg"? -- but note that the test is applied to terms already before
   857      they are split/normalized) to speed things up in case there are lots of
   858      irrelevant terms involved; elimination of min/max can be optimized:
   859      (max m n + k <= r) = (m+k <= r & n+k <= r)
   860      (l <= min m n + k) = (l <= m+k & l <= n+k)
   861   *)
   862   refute_tac (K true)
   863     (* Splitting is also done inside simple_tac, but not completely --   *)
   864     (* split_tac may use split theorems that have not been implemented in    *)
   865     (* simple_tac (cf. pre_decomp and split_once_items above), and       *)
   866     (* split_limit may trigger.                                   *)
   867     (* Therefore splitting outside of simple_tac may allow us to prove   *)
   868     (* some goals that simple_tac alone would fail on.                   *)
   869     (REPEAT_DETERM o split_tac (#splits (get_arith_data ctxt)))
   870     (lin_arith_tac ctxt ex);
   871 
   872 in
   873 
   874 fun gen_tac ex ctxt = FIRST' [simple_tac ctxt,
   875   ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_tac ctxt ex];
   876 
   877 val tac = gen_tac true;
   878 
   879 end;
   880 
   881 
   882 (* context setup *)
   883 
   884 val setup =
   885   init_arith_data #>
   886   Simplifier.map_ss (fn ss => ss addsimprocs [Simplifier.simproc (@{theory}) "fast_nat_arith"
   887     ["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"] (K simproc)]
   888     (* Because of fast_nat_arith_simproc, the arithmetic solver is really only
   889     useful to detect inconsistencies among the premises for subgoals which are
   890     *not* themselves (in)equalities, because the latter activate
   891     fast_nat_arith_simproc anyway. However, it seems cheaper to activate the
   892     solver all the time rather than add the additional check. *)
   893     addSolver (mk_solver' "lin_arith"
   894       (add_arith_facts #> Fast_Arith.cut_lin_arith_tac)))
   895 
   896 val global_setup =
   897   setup_split_limit #> setup_neq_limit #>
   898   Attrib.setup @{binding arith_split} (Scan.succeed (Thm.declaration_attribute add_split))
   899     "declaration of split rules for arithmetic procedure" #>
   900   Method.setup @{binding linarith}
   901     (Args.bang_facts >> (fn prems => fn ctxt =>
   902       METHOD (fn facts =>
   903         HEADGOAL (Method.insert_tac (prems @ Arith_Data.get_arith_facts ctxt @ facts)
   904           THEN' tac ctxt)))) "linear arithmetic" #>
   905   Arith_Data.add_tactic "linear arithmetic" gen_tac;
   906 
   907 end;