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