src/HOL/Tools/lin_arith.ML
author wenzelm
Fri Mar 06 15:58:56 2015 +0100 (2015-03-06)
changeset 59621 291934bac95e
parent 59582 0fbed69ff081
child 59656 ddc5411c1cb9
permissions -rw-r--r--
Thm.cterm_of and Thm.ctyp_of operate on local context;
     1 (*  Title:      HOL/Tools/lin_arith.ML
     2     Author:     Tjark Weber and Tobias Nipkow, TU Muenchen
     3 
     4 HOL setup for linear arithmetic (see Provers/Arith/fast_lin_arith.ML).
     5 *)
     6 
     7 signature LIN_ARITH =
     8 sig
     9   val pre_tac: Proof.context -> int -> tactic
    10   val simple_tac: Proof.context -> int -> tactic
    11   val tac: Proof.context -> int -> tactic
    12   val simproc: Proof.context -> 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 = @{thm 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 = Thm.global_cterm_of thy (Var (("n", 0), HOLogic.natT))
    66     and ct = Thm.global_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 Envir.aeconv p t of
   129       NONE   => ((t, m) :: p, i)
   130     | SOME n => (AList.update Envir.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       (* quotient 's / t', where the denominator t can be NONE *)
   162       (* Note: will raise Rat.DIVZERO iff m' is Rat.zero *)
   163       let val (os',m') = demult (s, m);
   164           val (ot',p) = demult (t, Rat.one)
   165       in (case (os',ot') of
   166             (SOME s', SOME t') => SOME (mC $ s' $ t')
   167           | (SOME s', NONE) => SOME s'
   168           | (NONE, SOME t') =>
   169                let val Const(_,T) = mC
   170                in SOME (mC $ Const (@{const_name Groups.one}, domain_type T) $ t') end
   171           | (NONE, NONE) => NONE,
   172           Rat.mult m' (Rat.inv p))
   173       end
   174     (* terms that evaluate to numeric constants *)
   175     | demult (Const (@{const_name Groups.uminus}, _) $ t, m) = demult (t, Rat.neg m)
   176     | demult (Const (@{const_name Groups.zero}, _), _) = (NONE, Rat.zero)
   177     | demult (Const (@{const_name Groups.one}, _), m) = (NONE, m)
   178     (*Warning: in rare cases (neg_)numeral encloses a non-numeral,
   179       in which case dest_num raises TERM; hence all the handles below.
   180       Same for Suc-terms that turn out not to be numerals -
   181       although the simplifier should eliminate those anyway ...*)
   182     | demult (t as Const ("Num.numeral_class.numeral", _) $ n, m) =
   183       ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_num n)))
   184         handle TERM _ => (SOME t, m))
   185     | demult (t as Const (@{const_name Suc}, _) $ _, m) =
   186       ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_nat t)))
   187         handle TERM _ => (SOME t, m))
   188     (* injection constants are ignored *)
   189     | demult (t as Const f $ x, m) =
   190       if member (op =) inj_consts f then demult (x, m) else (SOME t, m)
   191     (* everything else is considered atomic *)
   192     | demult (atom, m) = (SOME atom, m)
   193 in demult end;
   194 
   195 fun decomp0 (inj_consts : (string * typ) list) (rel : string, lhs : term, rhs : term) :
   196             ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat) option =
   197 let
   198   (* Turns a term 'all' and associated multiplicity 'm' into a list 'p' of
   199      summands and associated multiplicities, plus a constant 'i' (with implicit
   200      multiplicity 1) *)
   201   fun poly (Const (@{const_name Groups.plus}, _) $ s $ t,
   202         m : Rat.rat, pi : (term * Rat.rat) list * Rat.rat) = poly (s, m, poly (t, m, pi))
   203     | poly (all as Const (@{const_name Groups.minus}, T) $ s $ t, m, pi) =
   204         if nT T then add_atom all m pi else poly (s, m, poly (t, Rat.neg m, pi))
   205     | poly (all as Const (@{const_name Groups.uminus}, T) $ t, m, pi) =
   206         if nT T then add_atom all m pi else poly (t, Rat.neg m, pi)
   207     | poly (Const (@{const_name Groups.zero}, _), _, pi) =
   208         pi
   209     | poly (Const (@{const_name Groups.one}, _), m, (p, i)) =
   210         (p, Rat.add i m)
   211     | poly (all as Const ("Num.numeral_class.numeral", Type(_,[_,_])) $ t, m, pi as (p, i)) =
   212         (let val k = HOLogic.dest_num t
   213         in (p, Rat.add i (Rat.mult m (Rat.rat_of_int k))) end
   214         handle TERM _ => add_atom all m pi)
   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 f $ x, m, pi) =
   226         if member (op =) inj_consts f then poly (x, m, pi) else add_atom all m pi
   227     | poly (all, m, pi) =
   228         add_atom all m pi
   229   val (p, i) = poly (lhs, Rat.one, ([], Rat.zero))
   230   val (q, j) = poly (rhs, Rat.one, ([], Rat.zero))
   231 in
   232   case rel of
   233     @{const_name Orderings.less}    => SOME (p, i, "<", q, j)
   234   | @{const_name Orderings.less_eq} => SOME (p, i, "<=", q, j)
   235   | @{const_name HOL.eq}            => SOME (p, i, "=", q, j)
   236   | _                   => NONE
   237 end handle Rat.DIVZERO => NONE;
   238 
   239 fun of_lin_arith_sort thy U =
   240   Sign.of_sort thy (U, @{sort Rings.linordered_idom});
   241 
   242 fun allows_lin_arith thy (discrete : string list) (U as Type (D, [])) : bool * bool =
   243       if of_lin_arith_sort thy U then (true, member (op =) discrete D)
   244       else if member (op =) discrete D then (true, true) else (false, false)
   245   | allows_lin_arith sg discrete U = (of_lin_arith_sort sg U, false);
   246 
   247 fun decomp_typecheck (thy, discrete, inj_consts) (T : typ, xxx) : decomp option =
   248   case T of
   249     Type ("fun", [U, _]) =>
   250       (case allows_lin_arith thy discrete U of
   251         (true, d) =>
   252           (case decomp0 inj_consts xxx of
   253             NONE                   => NONE
   254           | SOME (p, i, rel, q, j) => SOME (p, i, rel, q, j, d))
   255       | (false, _) =>
   256           NONE)
   257   | _ => NONE;
   258 
   259 fun negate (SOME (x, i, rel, y, j, d)) = SOME (x, i, "~" ^ rel, y, j, d)
   260   | negate NONE                        = NONE;
   261 
   262 fun decomp_negation data
   263   ((Const (@{const_name Trueprop}, _)) $ (Const (rel, T) $ lhs $ rhs)) : decomp option =
   264       decomp_typecheck data (T, (rel, lhs, rhs))
   265   | decomp_negation data ((Const (@{const_name Trueprop}, _)) $
   266   (Const (@{const_name Not}, _) $ (Const (rel, T) $ lhs $ rhs))) =
   267       negate (decomp_typecheck data (T, (rel, lhs, rhs)))
   268   | decomp_negation data _ =
   269       NONE;
   270 
   271 fun decomp ctxt : term -> decomp option =
   272   let
   273     val thy = Proof_Context.theory_of ctxt
   274     val {discrete, inj_consts, ...} = get_arith_data ctxt
   275   in decomp_negation (thy, discrete, inj_consts) end;
   276 
   277 fun domain_is_nat (_ $ (Const (_, T) $ _ $ _)) = nT T
   278   | domain_is_nat (_ $ (Const (@{const_name Not}, _) $ (Const (_, T) $ _ $ _))) = nT T
   279   | domain_is_nat _ = false;
   280 
   281 
   282 (*---------------------------------------------------------------------------*)
   283 (* the following code performs splitting of certain constants (e.g., min,    *)
   284 (* max) in a linear arithmetic problem; similar to what split_tac later does *)
   285 (* to the proof state                                                        *)
   286 (*---------------------------------------------------------------------------*)
   287 
   288 (* checks if splitting with 'thm' is implemented                             *)
   289 
   290 fun is_split_thm ctxt thm =
   291   (case Thm.concl_of thm of _ $ (_ $ (_ $ lhs) $ _) =>
   292     (* Trueprop $ ((op =) $ (?P $ lhs) $ rhs) *)
   293     (case head_of lhs of
   294       Const (a, _) =>
   295         member (op =)
   296          [@{const_name Orderings.max},
   297           @{const_name Orderings.min},
   298           @{const_name Groups.abs},
   299           @{const_name Groups.minus},
   300           "Int.nat" (*DYNAMIC BINDING!*),
   301           "Divides.div_class.mod" (*DYNAMIC BINDING!*),
   302           "Divides.div_class.div" (*DYNAMIC BINDING!*)] a
   303     | _ =>
   304       (if Context_Position.is_visible ctxt then
   305         warning ("Lin. Arith.: wrong format for split rule " ^ Display.string_of_thm ctxt thm)
   306        else (); false))
   307   | _ =>
   308     (if Context_Position.is_visible ctxt then
   309       warning ("Lin. Arith.: wrong format for split rule " ^ Display.string_of_thm ctxt thm)
   310      else (); false));
   311 
   312 (* substitute new for occurrences of old in a term, incrementing bound       *)
   313 (* variables as needed when substituting inside an abstraction               *)
   314 
   315 fun subst_term ([] : (term * term) list) (t : term) = t
   316   | subst_term pairs                     t          =
   317       (case AList.lookup Envir.aeconv pairs t of
   318         SOME new =>
   319           new
   320       | NONE     =>
   321           (case t of Abs (a, T, body) =>
   322             let val pairs' = map (apply2 (incr_boundvars 1)) pairs
   323             in  Abs (a, T, subst_term pairs' body)  end
   324           | t1 $ t2 => subst_term pairs t1 $ subst_term pairs t2
   325           | _ => t));
   326 
   327 (* approximates the effect of one application of split_tac (followed by NNF  *)
   328 (* normalization) on the subgoal represented by '(Ts, terms)'; returns a     *)
   329 (* list of new subgoals (each again represented by a typ list for bound      *)
   330 (* variables and a term list for premises), or NONE if split_tac would fail  *)
   331 (* on the subgoal                                                            *)
   332 
   333 (* FIXME: currently only the effect of certain split theorems is reproduced  *)
   334 (*        (which is why we need 'is_split_thm').  A more canonical           *)
   335 (*        implementation should analyze the right-hand side of the split     *)
   336 (*        theorem that can be applied, and modify the subgoal accordingly.   *)
   337 (*        Or even better, the splitter should be extended to provide         *)
   338 (*        splitting on terms as well as splitting on theorems (where the     *)
   339 (*        former can have a faster implementation as it does not need to be  *)
   340 (*        proof-producing).                                                  *)
   341 
   342 fun split_once_items ctxt (Ts : typ list, terms : term list) :
   343                      (typ list * term list) list option =
   344 let
   345   val thy = Proof_Context.theory_of ctxt
   346   (* takes a list  [t1, ..., tn]  to the term                                *)
   347   (*   tn' --> ... --> t1' --> False  ,                                      *)
   348   (* where ti' = HOLogic.dest_Trueprop ti                                    *)
   349   fun REPEAT_DETERM_etac_rev_mp tms =
   350     fold (curry HOLogic.mk_imp) (map HOLogic.dest_Trueprop tms)
   351       @{term False}
   352   val split_thms  = filter (is_split_thm ctxt) (#splits (get_arith_data ctxt))
   353   val cmap        = Splitter.cmap_of_split_thms split_thms
   354   val goal_tm     = REPEAT_DETERM_etac_rev_mp terms
   355   val splits      = Splitter.split_posns cmap thy Ts goal_tm
   356   val split_limit = Config.get ctxt split_limit
   357 in
   358   if length splits > split_limit then (
   359     tracing ("linarith_split_limit exceeded (current value is " ^
   360       string_of_int split_limit ^ ")");
   361     NONE
   362   ) else case splits of
   363     [] =>
   364     (* split_tac would fail: no possible split *)
   365     NONE
   366   | (_, _::_, _, _, _) :: _ =>
   367     (* disallow a split that involves non-locally bound variables (except    *)
   368     (* when bound by outermost meta-quantifiers)                             *)
   369     NONE
   370   | (_, [], _, split_type, split_term) :: _ =>
   371     (* ignore all but the first possible split                               *)
   372     (case strip_comb split_term of
   373     (* ?P (max ?i ?j) = ((?i <= ?j --> ?P ?j) & (~ ?i <= ?j --> ?P ?i)) *)
   374       (Const (@{const_name Orderings.max}, _), [t1, t2]) =>
   375       let
   376         val rev_terms     = rev terms
   377         val terms1        = map (subst_term [(split_term, t1)]) rev_terms
   378         val terms2        = map (subst_term [(split_term, t2)]) rev_terms
   379         val t1_leq_t2     = Const (@{const_name Orderings.less_eq},
   380                                     split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
   381         val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
   382         val not_false     = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
   383         val subgoal1      = (HOLogic.mk_Trueprop t1_leq_t2) :: terms2 @ [not_false]
   384         val subgoal2      = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms1 @ [not_false]
   385       in
   386         SOME [(Ts, subgoal1), (Ts, subgoal2)]
   387       end
   388     (* ?P (min ?i ?j) = ((?i <= ?j --> ?P ?i) & (~ ?i <= ?j --> ?P ?j)) *)
   389     | (Const (@{const_name Orderings.min}, _), [t1, t2]) =>
   390       let
   391         val rev_terms     = rev terms
   392         val terms1        = map (subst_term [(split_term, t1)]) rev_terms
   393         val terms2        = map (subst_term [(split_term, t2)]) rev_terms
   394         val t1_leq_t2     = Const (@{const_name Orderings.less_eq},
   395                                     split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
   396         val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
   397         val not_false     = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
   398         val subgoal1      = (HOLogic.mk_Trueprop t1_leq_t2) :: terms1 @ [not_false]
   399         val subgoal2      = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms2 @ [not_false]
   400       in
   401         SOME [(Ts, subgoal1), (Ts, subgoal2)]
   402       end
   403     (* ?P (abs ?a) = ((0 <= ?a --> ?P ?a) & (?a < 0 --> ?P (- ?a))) *)
   404     | (Const (@{const_name Groups.abs}, _), [t1]) =>
   405       let
   406         val rev_terms   = rev terms
   407         val terms1      = map (subst_term [(split_term, t1)]) rev_terms
   408         val terms2      = map (subst_term [(split_term, Const (@{const_name Groups.uminus},
   409                             split_type --> split_type) $ t1)]) rev_terms
   410         val zero        = Const (@{const_name Groups.zero}, split_type)
   411         val zero_leq_t1 = Const (@{const_name Orderings.less_eq},
   412                             split_type --> split_type --> HOLogic.boolT) $ zero $ t1
   413         val t1_lt_zero  = Const (@{const_name Orderings.less},
   414                             split_type --> split_type --> HOLogic.boolT) $ t1 $ zero
   415         val not_false   = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
   416         val subgoal1    = (HOLogic.mk_Trueprop zero_leq_t1) :: terms1 @ [not_false]
   417         val subgoal2    = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
   418       in
   419         SOME [(Ts, subgoal1), (Ts, subgoal2)]
   420       end
   421     (* ?P (?a - ?b) = ((?a < ?b --> ?P 0) & (ALL d. ?a = ?b + d --> ?P d)) *)
   422     | (Const (@{const_name Groups.minus}, _), [t1, t2]) =>
   423       let
   424         (* "d" in the above theorem becomes a new bound variable after NNF   *)
   425         (* transformation, therefore some adjustment of indices is necessary *)
   426         val rev_terms       = rev terms
   427         val zero            = Const (@{const_name Groups.zero}, split_type)
   428         val d               = Bound 0
   429         val terms1          = map (subst_term [(split_term, zero)]) rev_terms
   430         val terms2          = map (subst_term [(incr_boundvars 1 split_term, d)])
   431                                 (map (incr_boundvars 1) rev_terms)
   432         val t1'             = incr_boundvars 1 t1
   433         val t2'             = incr_boundvars 1 t2
   434         val t1_lt_t2        = Const (@{const_name Orderings.less},
   435                                 split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
   436         val t1_eq_t2_plus_d = Const (@{const_name HOL.eq}, split_type --> split_type --> HOLogic.boolT) $ t1' $
   437                                 (Const (@{const_name Groups.plus},
   438                                   split_type --> split_type --> split_type) $ t2' $ d)
   439         val not_false       = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
   440         val subgoal1        = (HOLogic.mk_Trueprop t1_lt_t2) :: terms1 @ [not_false]
   441         val subgoal2        = (HOLogic.mk_Trueprop t1_eq_t2_plus_d) :: terms2 @ [not_false]
   442       in
   443         SOME [(Ts, subgoal1), (split_type :: Ts, subgoal2)]
   444       end
   445     (* ?P (nat ?i) = ((ALL n. ?i = of_nat n --> ?P n) & (?i < 0 --> ?P 0)) *)
   446     | (Const ("Int.nat", _), [t1]) =>
   447       let
   448         val rev_terms   = rev terms
   449         val zero_int    = Const (@{const_name Groups.zero}, HOLogic.intT)
   450         val zero_nat    = Const (@{const_name Groups.zero}, HOLogic.natT)
   451         val n           = Bound 0
   452         val terms1      = map (subst_term [(incr_boundvars 1 split_term, n)])
   453                             (map (incr_boundvars 1) rev_terms)
   454         val terms2      = map (subst_term [(split_term, zero_nat)]) rev_terms
   455         val t1'         = incr_boundvars 1 t1
   456         val t1_eq_nat_n = Const (@{const_name HOL.eq}, HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1' $
   457                             (Const (@{const_name of_nat}, HOLogic.natT --> HOLogic.intT) $ n)
   458         val t1_lt_zero  = Const (@{const_name Orderings.less},
   459                             HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1 $ zero_int
   460         val not_false   = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
   461         val subgoal1    = (HOLogic.mk_Trueprop t1_eq_nat_n) :: terms1 @ [not_false]
   462         val subgoal2    = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
   463       in
   464         SOME [(HOLogic.natT :: Ts, subgoal1), (Ts, subgoal2)]
   465       end
   466     (* ?P ((?n::nat) mod (numeral ?k)) =
   467          ((numeral ?k = 0 --> ?P ?n) & (~ (numeral ?k = 0) -->
   468            (ALL i j. j < numeral ?k --> ?n = numeral ?k * i + j --> ?P j))) *)
   469     | (Const ("Divides.div_class.mod", Type ("fun", [@{typ nat}, _])), [t1, t2]) =>
   470       let
   471         val rev_terms               = rev terms
   472         val zero                    = Const (@{const_name Groups.zero}, split_type)
   473         val i                       = Bound 1
   474         val j                       = Bound 0
   475         val terms1                  = map (subst_term [(split_term, t1)]) rev_terms
   476         val terms2                  = map (subst_term [(incr_boundvars 2 split_term, j)])
   477                                         (map (incr_boundvars 2) rev_terms)
   478         val t1'                     = incr_boundvars 2 t1
   479         val t2'                     = incr_boundvars 2 t2
   480         val t2_eq_zero              = Const (@{const_name HOL.eq},
   481                                         split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
   482         val t2_neq_zero             = HOLogic.mk_not (Const (@{const_name HOL.eq},
   483                                         split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
   484         val j_lt_t2                 = Const (@{const_name Orderings.less},
   485                                         split_type --> split_type--> HOLogic.boolT) $ j $ t2'
   486         val t1_eq_t2_times_i_plus_j = Const (@{const_name HOL.eq}, split_type --> split_type --> HOLogic.boolT) $ t1' $
   487                                        (Const (@{const_name Groups.plus}, split_type --> split_type --> split_type) $
   488                                          (Const (@{const_name Groups.times},
   489                                            split_type --> split_type --> split_type) $ t2' $ i) $ j)
   490         val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
   491         val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
   492         val subgoal2                = (map HOLogic.mk_Trueprop
   493                                         [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
   494                                           @ terms2 @ [not_false]
   495       in
   496         SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
   497       end
   498     (* ?P ((?n::nat) div (numeral ?k)) =
   499          ((numeral ?k = 0 --> ?P 0) & (~ (numeral ?k = 0) -->
   500            (ALL i j. j < numeral ?k --> ?n = numeral ?k * i + j --> ?P i))) *)
   501     | (Const ("Divides.div_class.div", Type ("fun", [@{typ nat}, _])), [t1, t2]) =>
   502       let
   503         val rev_terms               = rev terms
   504         val zero                    = Const (@{const_name Groups.zero}, split_type)
   505         val i                       = Bound 1
   506         val j                       = Bound 0
   507         val terms1                  = map (subst_term [(split_term, zero)]) rev_terms
   508         val terms2                  = map (subst_term [(incr_boundvars 2 split_term, i)])
   509                                         (map (incr_boundvars 2) rev_terms)
   510         val t1'                     = incr_boundvars 2 t1
   511         val t2'                     = incr_boundvars 2 t2
   512         val t2_eq_zero              = Const (@{const_name HOL.eq},
   513                                         split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
   514         val t2_neq_zero             = HOLogic.mk_not (Const (@{const_name HOL.eq},
   515                                         split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
   516         val j_lt_t2                 = Const (@{const_name Orderings.less},
   517                                         split_type --> split_type--> HOLogic.boolT) $ j $ t2'
   518         val t1_eq_t2_times_i_plus_j = Const (@{const_name HOL.eq}, split_type --> split_type --> HOLogic.boolT) $ t1' $
   519                                        (Const (@{const_name Groups.plus}, split_type --> split_type --> split_type) $
   520                                          (Const (@{const_name Groups.times},
   521                                            split_type --> split_type --> split_type) $ t2' $ i) $ j)
   522         val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
   523         val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
   524         val subgoal2                = (map HOLogic.mk_Trueprop
   525                                         [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
   526                                           @ terms2 @ [not_false]
   527       in
   528         SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
   529       end
   530     (* ?P ((?n::int) mod (numeral ?k)) =
   531          ((numeral ?k = 0 --> ?P ?n) &
   532           (0 < numeral ?k -->
   533             (ALL i j.
   534               0 <= j & j < numeral ?k & ?n = numeral ?k * i + j --> ?P j)) &
   535           (numeral ?k < 0 -->
   536             (ALL i j.
   537               numeral ?k < j & j <= 0 & ?n = numeral ?k * i + j --> ?P j))) *)
   538     | (Const ("Divides.div_class.mod",
   539         Type ("fun", [Type ("Int.int", []), _])), [t1, t2]) =>
   540       let
   541         val rev_terms               = rev terms
   542         val zero                    = Const (@{const_name Groups.zero}, split_type)
   543         val i                       = Bound 1
   544         val j                       = Bound 0
   545         val terms1                  = map (subst_term [(split_term, t1)]) rev_terms
   546         val terms2_3                = map (subst_term [(incr_boundvars 2 split_term, j)])
   547                                         (map (incr_boundvars 2) rev_terms)
   548         val t1'                     = incr_boundvars 2 t1
   549         val t2'                     = incr_boundvars 2 t2
   550         val t2_eq_zero              = Const (@{const_name HOL.eq},
   551                                         split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
   552         val zero_lt_t2              = Const (@{const_name Orderings.less},
   553                                         split_type --> split_type --> HOLogic.boolT) $ zero $ t2'
   554         val t2_lt_zero              = Const (@{const_name Orderings.less},
   555                                         split_type --> split_type --> HOLogic.boolT) $ t2' $ zero
   556         val zero_leq_j              = Const (@{const_name Orderings.less_eq},
   557                                         split_type --> split_type --> HOLogic.boolT) $ zero $ j
   558         val j_leq_zero              = Const (@{const_name Orderings.less_eq},
   559                                         split_type --> split_type --> HOLogic.boolT) $ j $ zero
   560         val j_lt_t2                 = Const (@{const_name Orderings.less},
   561                                         split_type --> split_type--> HOLogic.boolT) $ j $ t2'
   562         val t2_lt_j                 = Const (@{const_name Orderings.less},
   563                                         split_type --> split_type--> HOLogic.boolT) $ t2' $ j
   564         val t1_eq_t2_times_i_plus_j = Const (@{const_name HOL.eq}, split_type --> split_type --> HOLogic.boolT) $ t1' $
   565                                        (Const (@{const_name Groups.plus}, split_type --> split_type --> split_type) $
   566                                          (Const (@{const_name Groups.times},
   567                                            split_type --> split_type --> split_type) $ t2' $ i) $ j)
   568         val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
   569         val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
   570         val subgoal2                = (map HOLogic.mk_Trueprop [zero_lt_t2, zero_leq_j])
   571                                         @ hd terms2_3
   572                                         :: (if tl terms2_3 = [] then [not_false] else [])
   573                                         @ (map HOLogic.mk_Trueprop [j_lt_t2, t1_eq_t2_times_i_plus_j])
   574                                         @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
   575         val subgoal3                = (map HOLogic.mk_Trueprop [t2_lt_zero, t2_lt_j])
   576                                         @ hd terms2_3
   577                                         :: (if tl terms2_3 = [] then [not_false] else [])
   578                                         @ (map HOLogic.mk_Trueprop [j_leq_zero, t1_eq_t2_times_i_plus_j])
   579                                         @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
   580         val Ts'                     = split_type :: split_type :: Ts
   581       in
   582         SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
   583       end
   584     (* ?P ((?n::int) div (numeral ?k)) =
   585          ((numeral ?k = 0 --> ?P 0) &
   586           (0 < numeral ?k -->
   587             (ALL i j.
   588               0 <= j & j < numeral ?k & ?n = numeral ?k * i + j --> ?P i)) &
   589           (numeral ?k < 0 -->
   590             (ALL i j.
   591               numeral ?k < j & j <= 0 & ?n = numeral ?k * i + j --> ?P i))) *)
   592     | (Const ("Divides.div_class.div",
   593         Type ("fun", [Type ("Int.int", []), _])), [t1, t2]) =>
   594       let
   595         val rev_terms               = rev terms
   596         val zero                    = Const (@{const_name Groups.zero}, split_type)
   597         val i                       = Bound 1
   598         val j                       = Bound 0
   599         val terms1                  = map (subst_term [(split_term, zero)]) rev_terms
   600         val terms2_3                = map (subst_term [(incr_boundvars 2 split_term, i)])
   601                                         (map (incr_boundvars 2) rev_terms)
   602         val t1'                     = incr_boundvars 2 t1
   603         val t2'                     = incr_boundvars 2 t2
   604         val t2_eq_zero              = Const (@{const_name HOL.eq},
   605                                         split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
   606         val zero_lt_t2              = Const (@{const_name Orderings.less},
   607                                         split_type --> split_type --> HOLogic.boolT) $ zero $ t2'
   608         val t2_lt_zero              = Const (@{const_name Orderings.less},
   609                                         split_type --> split_type --> HOLogic.boolT) $ t2' $ zero
   610         val zero_leq_j              = Const (@{const_name Orderings.less_eq},
   611                                         split_type --> split_type --> HOLogic.boolT) $ zero $ j
   612         val j_leq_zero              = Const (@{const_name Orderings.less_eq},
   613                                         split_type --> split_type --> HOLogic.boolT) $ j $ zero
   614         val j_lt_t2                 = Const (@{const_name Orderings.less},
   615                                         split_type --> split_type--> HOLogic.boolT) $ j $ t2'
   616         val t2_lt_j                 = Const (@{const_name Orderings.less},
   617                                         split_type --> split_type--> HOLogic.boolT) $ t2' $ j
   618         val t1_eq_t2_times_i_plus_j = Const (@{const_name HOL.eq}, split_type --> split_type --> HOLogic.boolT) $ t1' $
   619                                        (Const (@{const_name Groups.plus}, split_type --> split_type --> split_type) $
   620                                          (Const (@{const_name Groups.times},
   621                                            split_type --> split_type --> split_type) $ t2' $ i) $ j)
   622         val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
   623         val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
   624         val subgoal2                = (map HOLogic.mk_Trueprop [zero_lt_t2, zero_leq_j])
   625                                         @ hd terms2_3
   626                                         :: (if tl terms2_3 = [] then [not_false] else [])
   627                                         @ (map HOLogic.mk_Trueprop [j_lt_t2, t1_eq_t2_times_i_plus_j])
   628                                         @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
   629         val subgoal3                = (map HOLogic.mk_Trueprop [t2_lt_zero, t2_lt_j])
   630                                         @ hd terms2_3
   631                                         :: (if tl terms2_3 = [] then [not_false] else [])
   632                                         @ (map HOLogic.mk_Trueprop [j_leq_zero, t1_eq_t2_times_i_plus_j])
   633                                         @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
   634         val Ts'                     = split_type :: split_type :: Ts
   635       in
   636         SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
   637       end
   638     (* this will only happen if a split theorem can be applied for which no  *)
   639     (* code exists above -- in which case either the split theorem should be *)
   640     (* implemented above, or 'is_split_thm' should be modified to filter it  *)
   641     (* out                                                                   *)
   642     | (t, ts) =>
   643       (if Context_Position.is_visible ctxt then
   644         warning ("Lin. Arith.: split rule for " ^ Syntax.string_of_term ctxt t ^
   645           " (with " ^ string_of_int (length ts) ^
   646           " argument(s)) not implemented; proof reconstruction is likely to fail")
   647        else (); NONE))
   648 end;  (* split_once_items *)
   649 
   650 (* remove terms that do not satisfy 'p'; change the order of the remaining   *)
   651 (* terms in the same way as filter_prems_tac does                            *)
   652 
   653 fun filter_prems_tac_items (p : term -> bool) (terms : term list) : term list =
   654   let
   655     fun filter_prems t (left, right) =
   656       if p t then (left, right @ [t]) else (left @ right, [])
   657     val (left, right) = fold filter_prems terms ([], [])
   658   in
   659     right @ left
   660   end;
   661 
   662 (* return true iff TRY (etac notE) THEN eq_assume_tac would succeed on a     *)
   663 (* subgoal that has 'terms' as premises                                      *)
   664 
   665 fun negated_term_occurs_positively (terms : term list) : bool =
   666   exists
   667     (fn (Trueprop $ (Const (@{const_name Not}, _) $ t)) =>
   668       member Envir.aeconv terms (Trueprop $ t)
   669       | _ => false)
   670     terms;
   671 
   672 fun pre_decomp ctxt (Ts : typ list, terms : term list) : (typ list * term list) list =
   673   let
   674     (* repeatedly split (including newly emerging subgoals) until no further   *)
   675     (* splitting is possible                                                   *)
   676     fun split_loop ([] : (typ list * term list) list) = ([] : (typ list * term list) list)
   677       | split_loop (subgoal::subgoals) =
   678           (case split_once_items ctxt subgoal of
   679             SOME new_subgoals => split_loop (new_subgoals @ subgoals)
   680           | NONE => subgoal :: split_loop subgoals)
   681     fun is_relevant t  = is_some (decomp ctxt t)
   682     (* filter_prems_tac is_relevant: *)
   683     val relevant_terms = filter_prems_tac_items is_relevant terms
   684     (* split_tac, NNF normalization: *)
   685     val split_goals = split_loop [(Ts, relevant_terms)]
   686     (* necessary because split_once_tac may normalize terms: *)
   687     val beta_eta_norm = map (apsnd (map (Envir.eta_contract o Envir.beta_norm)))
   688       split_goals
   689     (* TRY (etac notE) THEN eq_assume_tac: *)
   690     val result = filter_out (negated_term_occurs_positively o snd) beta_eta_norm
   691   in
   692     result
   693   end;
   694 
   695 (* takes the i-th subgoal  [| A1; ...; An |] ==> B  to                       *)
   696 (* An --> ... --> A1 --> B,  performs splitting with the given 'split_thms'  *)
   697 (* (resulting in a different subgoal P), takes  P  to  ~P ==> False,         *)
   698 (* performs NNF-normalization of ~P, and eliminates conjunctions,            *)
   699 (* disjunctions and existential quantifiers from the premises, possibly (in  *)
   700 (* the case of disjunctions) resulting in several new subgoals, each of the  *)
   701 (* general form  [| Q1; ...; Qm |] ==> False.  Fails if more than            *)
   702 (* !split_limit splits are possible.                              *)
   703 
   704 local
   705   fun nnf_simpset ctxt =
   706     (empty_simpset ctxt
   707       |> Simplifier.set_mkeqTrue mk_eq_True
   708       |> Simplifier.set_mksimps (mksimps mksimps_pairs))
   709     addsimps [@{thm imp_conv_disj}, @{thm iff_conv_conj_imp}, @{thm de_Morgan_disj},
   710       @{thm de_Morgan_conj}, not_all, not_ex, not_not]
   711   fun prem_nnf_tac ctxt = full_simp_tac (nnf_simpset ctxt)
   712 in
   713 
   714 fun split_once_tac ctxt split_thms =
   715   let
   716     val thy = Proof_Context.theory_of ctxt
   717     val cond_split_tac = SUBGOAL (fn (subgoal, i) =>
   718       let
   719         val Ts = rev (map snd (Logic.strip_params subgoal))
   720         val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl subgoal)
   721         val cmap = Splitter.cmap_of_split_thms split_thms
   722         val splits = Splitter.split_posns cmap thy Ts concl
   723       in
   724         if null splits orelse length splits > Config.get ctxt split_limit then
   725           no_tac
   726         else if null (#2 (hd splits)) then
   727           split_tac ctxt split_thms i
   728         else
   729           (* disallow a split that involves non-locally bound variables      *)
   730           (* (except when bound by outermost meta-quantifiers)               *)
   731           no_tac
   732       end)
   733   in
   734     EVERY' [
   735       REPEAT_DETERM o eresolve_tac ctxt [rev_mp],
   736       cond_split_tac,
   737       resolve_tac ctxt @{thms ccontr},
   738       prem_nnf_tac ctxt,
   739       TRY o REPEAT_ALL_NEW
   740         (DETERM o (eresolve_tac ctxt [conjE, exE] ORELSE' eresolve_tac ctxt [disjE]))
   741     ]
   742   end;
   743 
   744 end;  (* local *)
   745 
   746 (* remove irrelevant premises, then split the i-th subgoal (and all new      *)
   747 (* subgoals) by using 'split_once_tac' repeatedly.  Beta-eta-normalize new   *)
   748 (* subgoals and finally attempt to solve them by finding an immediate        *)
   749 (* contradiction (i.e., a term and its negation) in their premises.          *)
   750 
   751 fun pre_tac ctxt i =
   752   let
   753     val split_thms = filter (is_split_thm ctxt) (#splits (get_arith_data ctxt))
   754     fun is_relevant t = is_some (decomp ctxt t)
   755   in
   756     DETERM (
   757       TRY (filter_prems_tac ctxt is_relevant i)
   758         THEN (
   759           (TRY o REPEAT_ALL_NEW (split_once_tac ctxt split_thms))
   760             THEN_ALL_NEW
   761               (CONVERSION Drule.beta_eta_conversion
   762                 THEN'
   763               (TRY o (eresolve_tac ctxt [notE] THEN' eq_assume_tac)))
   764         ) i
   765     )
   766   end;
   767 
   768 end;  (* LA_Data *)
   769 
   770 
   771 val pre_tac = LA_Data.pre_tac;
   772 
   773 structure Fast_Arith = Fast_Lin_Arith(structure LA_Logic = LA_Logic and LA_Data = LA_Data);
   774 
   775 val add_inj_thms = Fast_Arith.add_inj_thms;
   776 val add_lessD = Fast_Arith.add_lessD;
   777 val add_simps = Fast_Arith.add_simps;
   778 val add_simprocs = Fast_Arith.add_simprocs;
   779 val set_number_of = Fast_Arith.set_number_of;
   780 
   781 fun simple_tac ctxt = Fast_Arith.lin_arith_tac ctxt false;
   782 val lin_arith_tac = Fast_Arith.lin_arith_tac;
   783 
   784 (* reduce contradictory <= to False.
   785    Most of the work is done by the cancel tactics. *)
   786 
   787 val init_arith_data =
   788   Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, number_of, ...} =>
   789    {add_mono_thms = @{thms add_mono_thms_linordered_semiring}
   790       @ @{thms add_mono_thms_linordered_field} @ add_mono_thms,
   791     mult_mono_thms = @{thm mult_strict_left_mono} :: @{thm mult_left_mono}
   792       :: @{lemma "a = b ==> c * a = c * b" by (rule arg_cong)} :: mult_mono_thms,
   793     inj_thms = inj_thms,
   794     lessD = lessD,
   795     neqE = @{thm linorder_neqE_nat} :: @{thm linorder_neqE_linordered_idom} :: neqE,
   796     simpset = put_simpset HOL_basic_ss @{context} |> Simplifier.add_cong @{thm if_weak_cong} |> simpset_of,
   797     number_of = number_of});
   798 
   799 (* FIXME !?? *)
   800 fun add_arith_facts ctxt =
   801   Simplifier.add_prems (rev (Named_Theorems.get ctxt @{named_theorems arith})) ctxt;
   802 
   803 val simproc = add_arith_facts #> Fast_Arith.lin_arith_simproc;
   804 
   805 
   806 (* generic refutation procedure *)
   807 
   808 (* parameters:
   809 
   810    test: term -> bool
   811    tests if a term is at all relevant to the refutation proof;
   812    if not, then it can be discarded. Can improve performance,
   813    esp. if disjunctions can be discarded (no case distinction needed!).
   814 
   815    prep_tac: int -> tactic
   816    A preparation tactic to be applied to the goal once all relevant premises
   817    have been moved to the conclusion.
   818 
   819    ref_tac: int -> tactic
   820    the actual refutation tactic. Should be able to deal with goals
   821    [| A1; ...; An |] ==> False
   822    where the Ai are atomic, i.e. no top-level &, | or EX
   823 *)
   824 
   825 local
   826   fun nnf_simpset ctxt =
   827     (empty_simpset ctxt
   828       |> Simplifier.set_mkeqTrue mk_eq_True
   829       |> Simplifier.set_mksimps (mksimps mksimps_pairs))
   830     addsimps [@{thm imp_conv_disj}, @{thm iff_conv_conj_imp}, @{thm de_Morgan_disj},
   831       @{thm de_Morgan_conj}, @{thm not_all}, @{thm not_ex}, @{thm not_not}];
   832   fun prem_nnf_tac ctxt = full_simp_tac (nnf_simpset ctxt);
   833 in
   834 
   835 fun refute_tac ctxt test prep_tac ref_tac =
   836   let val refute_prems_tac =
   837         REPEAT_DETERM
   838               (eresolve_tac ctxt [@{thm conjE}, @{thm exE}] 1 ORELSE
   839                filter_prems_tac ctxt test 1 ORELSE
   840                eresolve_tac ctxt @{thms disjE} 1) THEN
   841         (DETERM (eresolve_tac ctxt @{thms notE} 1 THEN eq_assume_tac 1) ORELSE
   842          ref_tac 1);
   843   in EVERY'[TRY o filter_prems_tac ctxt test,
   844             REPEAT_DETERM o eresolve_tac ctxt @{thms rev_mp}, prep_tac,
   845               resolve_tac ctxt @{thms ccontr}, prem_nnf_tac ctxt,
   846             SELECT_GOAL (DEPTH_SOLVE refute_prems_tac)]
   847   end;
   848 
   849 end;
   850 
   851 
   852 (* arith proof method *)
   853 
   854 local
   855 
   856 fun raw_tac ctxt ex =
   857   (* FIXME: K true should be replaced by a sensible test (perhaps "is_some o
   858      decomp sg"? -- but note that the test is applied to terms already before
   859      they are split/normalized) to speed things up in case there are lots of
   860      irrelevant terms involved; elimination of min/max can be optimized:
   861      (max m n + k <= r) = (m+k <= r & n+k <= r)
   862      (l <= min m n + k) = (l <= m+k & l <= n+k)
   863   *)
   864   refute_tac ctxt (K true)
   865     (* Splitting is also done inside simple_tac, but not completely --    *)
   866     (* split_tac may use split theorems that have not been implemented in *)
   867     (* simple_tac (cf. pre_decomp and split_once_items above), and        *)
   868     (* split_limit may trigger.                                           *)
   869     (* Therefore splitting outside of simple_tac may allow us to prove    *)
   870     (* some goals that simple_tac alone would fail on.                    *)
   871     (REPEAT_DETERM o split_tac ctxt (#splits (get_arith_data ctxt)))
   872     (lin_arith_tac ctxt ex);
   873 
   874 in
   875 
   876 fun gen_tac ex ctxt =
   877   FIRST' [simple_tac ctxt,
   878     Object_Logic.full_atomize_tac ctxt THEN'
   879     (REPEAT_DETERM o resolve_tac ctxt [impI]) THEN' raw_tac ctxt ex];
   880 
   881 val tac = gen_tac true;
   882 
   883 end;
   884 
   885 
   886 (* context setup *)
   887 
   888 val global_setup =
   889   map_theory_simpset (fn ctxt => ctxt
   890     addSolver (mk_solver "lin_arith" (add_arith_facts #> Fast_Arith.prems_lin_arith_tac))) #>
   891   Attrib.setup @{binding arith_split} (Scan.succeed (Thm.declaration_attribute add_split))
   892     "declaration of split rules for arithmetic procedure" #>
   893   Method.setup @{binding linarith}
   894     (Scan.succeed (fn ctxt =>
   895       METHOD (fn facts =>
   896         HEADGOAL (Method.insert_tac (rev (Named_Theorems.get ctxt @{named_theorems arith}) @ facts)
   897           THEN' tac ctxt)))) "linear arithmetic" #>
   898   Arith_Data.add_tactic "linear arithmetic" gen_tac;
   899 
   900 val setup =
   901   init_arith_data
   902   #> add_discrete_type @{type_name nat}
   903   #> add_lessD @{thm Suc_leI}
   904   #> add_simps (@{thms simp_thms} @ @{thms ring_distribs} @ [@{thm if_True}, @{thm if_False},
   905       @{thm add_0_left}, @{thm add_0_right}, @{thm order_less_irrefl},
   906       @{thm zero_neq_one}, @{thm zero_less_one}, @{thm zero_le_one},
   907       @{thm zero_neq_one} RS not_sym, @{thm not_one_le_zero}, @{thm not_one_less_zero}])
   908   #> add_simps [@{thm add_Suc}, @{thm add_Suc_right}, @{thm nat.inject},
   909       @{thm Suc_le_mono}, @{thm Suc_less_eq}, @{thm Zero_not_Suc},
   910       @{thm Suc_not_Zero}, @{thm le_0_eq}, @{thm One_nat_def}]
   911   #> add_simprocs [@{simproc group_cancel_add}, @{simproc group_cancel_diff},
   912       @{simproc group_cancel_eq}, @{simproc group_cancel_le},
   913       @{simproc group_cancel_less}]
   914      (*abel_cancel helps it work in abstract algebraic domains*)
   915   #> add_simprocs [@{simproc nateq_cancel_sums},@{simproc natless_cancel_sums},
   916       @{simproc natle_cancel_sums}];
   917 
   918 end;