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