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