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