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