HOL setup for linear arithmetic -- moved here from arith_data.ML;
authorwenzelm
Tue Jul 31 19:40:23 2007 +0200 (2007-07-31)
changeset 2409271c27b320610
parent 24091 109f19a13872
child 24093 5d0ecd0c8f3c
HOL setup for linear arithmetic -- moved here from arith_data.ML;
src/HOL/Tools/lin_arith.ML
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Tools/lin_arith.ML	Tue Jul 31 19:40:23 2007 +0200
     1.3 @@ -0,0 +1,897 @@
     1.4 +(*  Title:      HOL/Tools/lin_arith.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Tjark Weber and Tobias Nipkow
     1.7 +
     1.8 +HOL setup for linear arithmetic (see Provers/Arith/fast_lin_arith.ML).
     1.9 +*)
    1.10 +
    1.11 +signature BASIC_LIN_ARITH =
    1.12 +sig
    1.13 +  type arith_tactic
    1.14 +  val mk_arith_tactic: string -> (Proof.context -> int -> tactic) -> arith_tactic
    1.15 +  val eq_arith_tactic: arith_tactic * arith_tactic -> bool
    1.16 +  val arith_split_add: attribute
    1.17 +  val arith_discrete: string -> Context.generic -> Context.generic
    1.18 +  val arith_inj_const: string * typ -> Context.generic -> Context.generic
    1.19 +  val arith_tactic_add: arith_tactic -> Context.generic -> Context.generic
    1.20 +  val fast_arith_split_limit: int ConfigOption.T
    1.21 +  val fast_arith_neq_limit: int ConfigOption.T
    1.22 +  val lin_arith_pre_tac: Proof.context -> int -> tactic
    1.23 +  val fast_arith_tac: Proof.context -> int -> tactic
    1.24 +  val fast_ex_arith_tac: Proof.context -> bool -> int -> tactic
    1.25 +  val trace_arith: bool ref
    1.26 +  val lin_arith_simproc: simpset -> term -> thm option
    1.27 +  val fast_nat_arith_simproc: simproc
    1.28 +  val simple_arith_tac: Proof.context -> int -> tactic
    1.29 +  val arith_tac: Proof.context -> int -> tactic
    1.30 +  val silent_arith_tac: Proof.context -> int -> tactic
    1.31 +end;
    1.32 +
    1.33 +signature LIN_ARITH =
    1.34 +sig
    1.35 +  include BASIC_LIN_ARITH
    1.36 +  val map_data:
    1.37 +    ({add_mono_thms: thm list, mult_mono_thms: thm list, inj_thms: thm list,
    1.38 +      lessD: thm list, neqE: thm list, simpset: Simplifier.simpset} ->
    1.39 +     {add_mono_thms: thm list, mult_mono_thms: thm list, inj_thms: thm list,
    1.40 +      lessD: thm list, neqE: thm list, simpset: Simplifier.simpset}) ->
    1.41 +    Context.generic -> Context.generic
    1.42 +  val setup: Context.generic -> Context.generic
    1.43 +end;
    1.44 +
    1.45 +structure LinArith: LIN_ARITH =
    1.46 +struct
    1.47 +
    1.48 +(* Parameters data for general linear arithmetic functor *)
    1.49 +
    1.50 +structure LA_Logic: LIN_ARITH_LOGIC =
    1.51 +struct
    1.52 +
    1.53 +val ccontr = ccontr;
    1.54 +val conjI = conjI;
    1.55 +val notI = notI;
    1.56 +val sym = sym;
    1.57 +val not_lessD = @{thm linorder_not_less} RS iffD1;
    1.58 +val not_leD = @{thm linorder_not_le} RS iffD1;
    1.59 +val le0 = thm "le0";
    1.60 +
    1.61 +fun mk_Eq thm = (thm RS Eq_FalseI) handle THM _ => (thm RS Eq_TrueI);
    1.62 +
    1.63 +val mk_Trueprop = HOLogic.mk_Trueprop;
    1.64 +
    1.65 +fun atomize thm = case Thm.prop_of thm of
    1.66 +    Const("Trueprop",_) $ (Const("op &",_) $ _ $ _) =>
    1.67 +    atomize(thm RS conjunct1) @ atomize(thm RS conjunct2)
    1.68 +  | _ => [thm];
    1.69 +
    1.70 +fun neg_prop ((TP as Const("Trueprop",_)) $ (Const("Not",_) $ t)) = TP $ t
    1.71 +  | neg_prop ((TP as Const("Trueprop",_)) $ t) = TP $ (HOLogic.Not $t)
    1.72 +  | neg_prop t = raise TERM ("neg_prop", [t]);
    1.73 +
    1.74 +fun is_False thm =
    1.75 +  let val _ $ t = Thm.prop_of thm
    1.76 +  in t = Const("False",HOLogic.boolT) end;
    1.77 +
    1.78 +fun is_nat(t) = fastype_of1 t = HOLogic.natT;
    1.79 +
    1.80 +fun mk_nat_thm sg t =
    1.81 +  let val ct = cterm_of sg t  and cn = cterm_of sg (Var(("n",0),HOLogic.natT))
    1.82 +  in instantiate ([],[(cn,ct)]) le0 end;
    1.83 +
    1.84 +end;
    1.85 +
    1.86 +
    1.87 +(* arith context data *)
    1.88 +
    1.89 +datatype arith_tactic =
    1.90 +  ArithTactic of {name: string, tactic: Proof.context -> int -> tactic, id: stamp};
    1.91 +
    1.92 +fun mk_arith_tactic name tactic = ArithTactic {name = name, tactic = tactic, id = stamp ()};
    1.93 +
    1.94 +fun eq_arith_tactic (ArithTactic {id = id1, ...}, ArithTactic {id = id2, ...}) = (id1 = id2);
    1.95 +
    1.96 +structure ArithContextData = GenericDataFun
    1.97 +(
    1.98 +  type T = {splits: thm list,
    1.99 +            inj_consts: (string * typ) list,
   1.100 +            discrete: string list,
   1.101 +            tactics: arith_tactic list};
   1.102 +  val empty = {splits = [], inj_consts = [], discrete = [], tactics = []};
   1.103 +  val extend = I;
   1.104 +  fun merge _ ({splits= splits1, inj_consts= inj_consts1, discrete= discrete1, tactics= tactics1},
   1.105 +             {splits= splits2, inj_consts= inj_consts2, discrete= discrete2, tactics= tactics2}) =
   1.106 +   {splits = Library.merge Thm.eq_thm_prop (splits1, splits2),
   1.107 +    inj_consts = Library.merge (op =) (inj_consts1, inj_consts2),
   1.108 +    discrete = Library.merge (op =) (discrete1, discrete2),
   1.109 +    tactics = Library.merge eq_arith_tactic (tactics1, tactics2)};
   1.110 +);
   1.111 +
   1.112 +val get_arith_data = ArithContextData.get o Context.Proof;
   1.113 +
   1.114 +val arith_split_add = Thm.declaration_attribute (fn thm =>
   1.115 +  ArithContextData.map (fn {splits, inj_consts, discrete, tactics} =>
   1.116 +    {splits = insert Thm.eq_thm_prop thm splits,
   1.117 +     inj_consts = inj_consts, discrete = discrete, tactics = tactics}));
   1.118 +
   1.119 +fun arith_discrete d = ArithContextData.map (fn {splits, inj_consts, discrete, tactics} =>
   1.120 +  {splits = splits, inj_consts = inj_consts,
   1.121 +   discrete = insert (op =) d discrete, tactics = tactics});
   1.122 +
   1.123 +fun arith_inj_const c = ArithContextData.map (fn {splits, inj_consts, discrete, tactics} =>
   1.124 +  {splits = splits, inj_consts = insert (op =) c inj_consts,
   1.125 +   discrete = discrete, tactics= tactics});
   1.126 +
   1.127 +fun arith_tactic_add tac = ArithContextData.map (fn {splits, inj_consts, discrete, tactics} =>
   1.128 +  {splits = splits, inj_consts = inj_consts, discrete = discrete,
   1.129 +   tactics = insert eq_arith_tactic tac tactics});
   1.130 +
   1.131 +
   1.132 +val (fast_arith_split_limit, setup1) = ConfigOption.int "fast_arith_split_limit" 9;
   1.133 +val (fast_arith_neq_limit, setup2) = ConfigOption.int "fast_arith_neq_limit" 9;
   1.134 +val setup_options = setup1 #> setup2;
   1.135 +
   1.136 +
   1.137 +structure LA_Data_Ref =
   1.138 +struct
   1.139 +
   1.140 +val fast_arith_neq_limit = fast_arith_neq_limit;
   1.141 +
   1.142 +
   1.143 +(* Decomposition of terms *)
   1.144 +
   1.145 +(*internal representation of linear (in-)equations*)
   1.146 +type decompT = ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat * bool);
   1.147 +
   1.148 +fun nT (Type ("fun", [N, _])) = (N = HOLogic.natT)
   1.149 +  | nT _                      = false;
   1.150 +
   1.151 +fun add_atom (t : term) (m : Rat.rat) (p : (term * Rat.rat) list, i : Rat.rat) :
   1.152 +             (term * Rat.rat) list * Rat.rat =
   1.153 +  case AList.lookup (op =) p t of NONE   => ((t, m) :: p, i)
   1.154 +                                | SOME n => (AList.update (op =) (t, Rat.add n m) p, i);
   1.155 +
   1.156 +exception Zero;
   1.157 +
   1.158 +fun rat_of_term (numt, dent) =
   1.159 +  let
   1.160 +    val num = HOLogic.dest_numeral numt
   1.161 +    val den = HOLogic.dest_numeral dent
   1.162 +  in
   1.163 +    if den = 0 then raise Zero else Rat.rat_of_quotient (num, den)
   1.164 +  end;
   1.165 +
   1.166 +(*Warning: in rare cases number_of encloses a non-numeral,
   1.167 +  in which case dest_numeral raises TERM; hence all the handles below.
   1.168 +  Same for Suc-terms that turn out not to be numerals -
   1.169 +  although the simplifier should eliminate those anyway ...*)
   1.170 +fun number_of_Sucs (Const ("Suc", _) $ n) : int =
   1.171 +      number_of_Sucs n + 1
   1.172 +  | number_of_Sucs t =
   1.173 +      if HOLogic.is_zero t then 0 else raise TERM ("number_of_Sucs", []);
   1.174 +
   1.175 +(*decompose nested multiplications, bracketing them to the right and combining
   1.176 +  all their coefficients*)
   1.177 +fun demult (inj_consts : (string * typ) list) : term * Rat.rat -> term option * Rat.rat =
   1.178 +let
   1.179 +  fun demult ((mC as Const (@{const_name HOL.times}, _)) $ s $ t, m) = (
   1.180 +    (case s of
   1.181 +      Const ("Numeral.number_class.number_of", _) $ n =>
   1.182 +        demult (t, Rat.mult m (Rat.rat_of_int (HOLogic.dest_numeral n)))
   1.183 +    | Const (@{const_name HOL.uminus}, _) $ (Const ("Numeral.number_class.number_of", _) $ n) =>
   1.184 +        demult (t, Rat.mult m (Rat.rat_of_int (~(HOLogic.dest_numeral n))))
   1.185 +    | Const (@{const_name Suc}, _) $ _ =>
   1.186 +        demult (t, Rat.mult m (Rat.rat_of_int (HOLogic.dest_nat s)))
   1.187 +    | Const (@{const_name HOL.times}, _) $ s1 $ s2 =>
   1.188 +        demult (mC $ s1 $ (mC $ s2 $ t), m)
   1.189 +    | Const (@{const_name HOL.divide}, _) $ numt $
   1.190 +          (Const ("Numeral.number_class.number_of", _) $ dent) =>
   1.191 +        let
   1.192 +          val den = HOLogic.dest_numeral dent
   1.193 +        in
   1.194 +          if den = 0 then
   1.195 +            raise Zero
   1.196 +          else
   1.197 +            demult (mC $ numt $ t, Rat.mult m (Rat.inv (Rat.rat_of_int den)))
   1.198 +        end
   1.199 +    | _ =>
   1.200 +        atomult (mC, s, t, m)
   1.201 +    ) handle TERM _ => atomult (mC, s, t, m)
   1.202 +  )
   1.203 +    | demult (atom as Const(@{const_name HOL.divide}, _) $ t $
   1.204 +        (Const ("Numeral.number_class.number_of", _) $ dent), m) =
   1.205 +      (let
   1.206 +        val den = HOLogic.dest_numeral dent
   1.207 +      in
   1.208 +        if den = 0 then
   1.209 +          raise Zero
   1.210 +        else
   1.211 +          demult (t, Rat.mult m (Rat.inv (Rat.rat_of_int den)))
   1.212 +      end handle TERM _ => (SOME atom, m))
   1.213 +    | demult (Const (@{const_name HOL.zero}, _), m) = (NONE, Rat.zero)
   1.214 +    | demult (Const (@{const_name HOL.one}, _), m) = (NONE, m)
   1.215 +    | demult (t as Const ("Numeral.number_class.number_of", _) $ n, m) =
   1.216 +        ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_numeral n)))
   1.217 +          handle TERM _ => (SOME t, m))
   1.218 +    | demult (Const (@{const_name HOL.uminus}, _) $ t, m) = demult (t, Rat.neg m)
   1.219 +    | demult (t as Const f $ x, m) =
   1.220 +        (if member (op =) inj_consts f then SOME x else SOME t, m)
   1.221 +    | demult (atom, m) = (SOME atom, m)
   1.222 +and
   1.223 +  atomult (mC, atom, t, m) = (
   1.224 +    case demult (t, m) of (NONE, m')    => (SOME atom, m')
   1.225 +                        | (SOME t', m') => (SOME (mC $ atom $ t'), m')
   1.226 +  )
   1.227 +in demult end;
   1.228 +
   1.229 +fun decomp0 (inj_consts : (string * typ) list) (rel : string, lhs : term, rhs : term) :
   1.230 +            ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat) option =
   1.231 +let
   1.232 +  (* Turn term into list of summand * multiplicity plus a constant *)
   1.233 +  fun poly (Const (@{const_name HOL.plus}, _) $ s $ t,
   1.234 +        m : Rat.rat, pi : (term * Rat.rat) list * Rat.rat) = poly (s, m, poly (t, m, pi))
   1.235 +    | poly (all as Const (@{const_name HOL.minus}, T) $ s $ t, m, pi) =
   1.236 +        if nT T then add_atom all m pi else poly (s, m, poly (t, Rat.neg m, pi))
   1.237 +    | poly (all as Const (@{const_name HOL.uminus}, T) $ t, m, pi) =
   1.238 +        if nT T then add_atom all m pi else poly (t, Rat.neg m, pi)
   1.239 +    | poly (Const (@{const_name HOL.zero}, _), _, pi) =
   1.240 +        pi
   1.241 +    | poly (Const (@{const_name HOL.one}, _), m, (p, i)) =
   1.242 +        (p, Rat.add i m)
   1.243 +    | poly (Const (@{const_name Suc}, _) $ t, m, (p, i)) =
   1.244 +        poly (t, m, (p, Rat.add i m))
   1.245 +    | poly (all as Const (@{const_name HOL.times}, _) $ _ $ _, m, pi as (p, i)) =
   1.246 +        (case demult inj_consts (all, m) of
   1.247 +           (NONE,   m') => (p, Rat.add i m')
   1.248 +         | (SOME u, m') => add_atom u m' pi)
   1.249 +    | poly (all as Const (@{const_name HOL.divide}, _) $ _ $ _, m, pi as (p, i)) =
   1.250 +        (case demult inj_consts (all, m) of
   1.251 +           (NONE,   m') => (p, Rat.add i m')
   1.252 +         | (SOME u, m') => add_atom u m' pi)
   1.253 +    | poly (all as Const ("Numeral.number_class.number_of", Type(_,[_,T])) $ t, m, pi as (p, i)) =
   1.254 +        (let val k = HOLogic.dest_numeral t
   1.255 +            val k2 = if k < 0 andalso T = HOLogic.natT then 0 else k
   1.256 +        in (p, Rat.add i (Rat.mult m (Rat.rat_of_int k2))) end
   1.257 +        handle TERM _ => add_atom all m pi)
   1.258 +    | poly (all as Const f $ x, m, pi) =
   1.259 +        if f mem inj_consts then poly (x, m, pi) else add_atom all m pi
   1.260 +    | poly (all, m, pi) =
   1.261 +        add_atom all m pi
   1.262 +  val (p, i) = poly (lhs, Rat.one, ([], Rat.zero))
   1.263 +  val (q, j) = poly (rhs, Rat.one, ([], Rat.zero))
   1.264 +in
   1.265 +  case rel of
   1.266 +    @{const_name HOL.less}    => SOME (p, i, "<", q, j)
   1.267 +  | @{const_name HOL.less_eq} => SOME (p, i, "<=", q, j)
   1.268 +  | "op ="              => SOME (p, i, "=", q, j)
   1.269 +  | _                   => NONE
   1.270 +end handle Zero => NONE;
   1.271 +
   1.272 +fun of_lin_arith_sort sg (U : typ) : bool =
   1.273 +  Type.of_sort (Sign.tsig_of sg) (U, ["Ring_and_Field.ordered_idom"])
   1.274 +
   1.275 +fun allows_lin_arith sg (discrete : string list) (U as Type (D, [])) : bool * bool =
   1.276 +  if of_lin_arith_sort sg U then
   1.277 +    (true, D mem discrete)
   1.278 +  else (* special cases *)
   1.279 +    if D mem discrete then  (true, true)  else  (false, false)
   1.280 +  | allows_lin_arith sg discrete U =
   1.281 +  (of_lin_arith_sort sg U, false);
   1.282 +
   1.283 +fun decomp_typecheck (thy, discrete, inj_consts) (T : typ, xxx) : decompT option =
   1.284 +  case T of
   1.285 +    Type ("fun", [U, _]) =>
   1.286 +      (case allows_lin_arith thy discrete U of
   1.287 +        (true, d) =>
   1.288 +          (case decomp0 inj_consts xxx of
   1.289 +            NONE                   => NONE
   1.290 +          | SOME (p, i, rel, q, j) => SOME (p, i, rel, q, j, d))
   1.291 +      | (false, _) =>
   1.292 +          NONE)
   1.293 +  | _ => NONE;
   1.294 +
   1.295 +fun negate (SOME (x, i, rel, y, j, d)) = SOME (x, i, "~" ^ rel, y, j, d)
   1.296 +  | negate NONE                        = NONE;
   1.297 +
   1.298 +fun decomp_negation data
   1.299 +  ((Const ("Trueprop", _)) $ (Const (rel, T) $ lhs $ rhs)) : decompT option =
   1.300 +      decomp_typecheck data (T, (rel, lhs, rhs))
   1.301 +  | decomp_negation data ((Const ("Trueprop", _)) $
   1.302 +  (Const ("Not", _) $ (Const (rel, T) $ lhs $ rhs))) =
   1.303 +      negate (decomp_typecheck data (T, (rel, lhs, rhs)))
   1.304 +  | decomp_negation data _ =
   1.305 +      NONE;
   1.306 +
   1.307 +fun decomp ctxt : term -> decompT option =
   1.308 +  let
   1.309 +    val thy = ProofContext.theory_of ctxt
   1.310 +    val {discrete, inj_consts, ...} = get_arith_data ctxt
   1.311 +  in decomp_negation (thy, discrete, inj_consts) end;
   1.312 +
   1.313 +fun domain_is_nat (_ $ (Const (_, T) $ _ $ _))                      = nT T
   1.314 +  | domain_is_nat (_ $ (Const ("Not", _) $ (Const (_, T) $ _ $ _))) = nT T
   1.315 +  | domain_is_nat _                                                 = false;
   1.316 +
   1.317 +fun number_of (n, T) = HOLogic.mk_number T n;
   1.318 +
   1.319 +(*---------------------------------------------------------------------------*)
   1.320 +(* the following code performs splitting of certain constants (e.g. min,     *)
   1.321 +(* max) in a linear arithmetic problem; similar to what split_tac later does *)
   1.322 +(* to the proof state                                                        *)
   1.323 +(*---------------------------------------------------------------------------*)
   1.324 +
   1.325 +(* checks if splitting with 'thm' is implemented                             *)
   1.326 +
   1.327 +fun is_split_thm (thm : thm) : bool =
   1.328 +  case concl_of thm of _ $ (_ $ (_ $ lhs) $ _) => (
   1.329 +    (* Trueprop $ ((op =) $ (?P $ lhs) $ rhs) *)
   1.330 +    case head_of lhs of
   1.331 +      Const (a, _) => member (op =) [@{const_name Orderings.max},
   1.332 +                                    @{const_name Orderings.min},
   1.333 +                                    @{const_name HOL.abs},
   1.334 +                                    @{const_name HOL.minus},
   1.335 +                                    "IntDef.nat",
   1.336 +                                    "Divides.div_class.mod",
   1.337 +                                    "Divides.div_class.div"] a
   1.338 +    | _            => (warning ("Lin. Arith.: wrong format for split rule " ^
   1.339 +                                 Display.string_of_thm thm);
   1.340 +                       false))
   1.341 +  | _ => (warning ("Lin. Arith.: wrong format for split rule " ^
   1.342 +                   Display.string_of_thm thm);
   1.343 +          false);
   1.344 +
   1.345 +(* substitute new for occurrences of old in a term, incrementing bound       *)
   1.346 +(* variables as needed when substituting inside an abstraction               *)
   1.347 +
   1.348 +fun subst_term ([] : (term * term) list) (t : term) = t
   1.349 +  | subst_term pairs                     t          =
   1.350 +      (case AList.lookup (op aconv) pairs t of
   1.351 +        SOME new =>
   1.352 +          new
   1.353 +      | NONE     =>
   1.354 +          (case t of Abs (a, T, body) =>
   1.355 +            let val pairs' = map (pairself (incr_boundvars 1)) pairs
   1.356 +            in  Abs (a, T, subst_term pairs' body)  end
   1.357 +          | t1 $ t2                   =>
   1.358 +            subst_term pairs t1 $ subst_term pairs t2
   1.359 +          | _ => t));
   1.360 +
   1.361 +(* approximates the effect of one application of split_tac (followed by NNF  *)
   1.362 +(* normalization) on the subgoal represented by '(Ts, terms)'; returns a     *)
   1.363 +(* list of new subgoals (each again represented by a typ list for bound      *)
   1.364 +(* variables and a term list for premises), or NONE if split_tac would fail  *)
   1.365 +(* on the subgoal                                                            *)
   1.366 +
   1.367 +(* FIXME: currently only the effect of certain split theorems is reproduced  *)
   1.368 +(*        (which is why we need 'is_split_thm').  A more canonical           *)
   1.369 +(*        implementation should analyze the right-hand side of the split     *)
   1.370 +(*        theorem that can be applied, and modify the subgoal accordingly.   *)
   1.371 +(*        Or even better, the splitter should be extended to provide         *)
   1.372 +(*        splitting on terms as well as splitting on theorems (where the     *)
   1.373 +(*        former can have a faster implementation as it does not need to be  *)
   1.374 +(*        proof-producing).                                                  *)
   1.375 +
   1.376 +fun split_once_items ctxt (Ts : typ list, terms : term list) :
   1.377 +                     (typ list * term list) list option =
   1.378 +let
   1.379 +  val thy = ProofContext.theory_of ctxt
   1.380 +  (* takes a list  [t1, ..., tn]  to the term                                *)
   1.381 +  (*   tn' --> ... --> t1' --> False  ,                                      *)
   1.382 +  (* where ti' = HOLogic.dest_Trueprop ti                                    *)
   1.383 +  fun REPEAT_DETERM_etac_rev_mp terms' =
   1.384 +    fold (curry HOLogic.mk_imp) (map HOLogic.dest_Trueprop terms') HOLogic.false_const
   1.385 +  val split_thms = filter is_split_thm (#splits (get_arith_data ctxt))
   1.386 +  val cmap       = Splitter.cmap_of_split_thms split_thms
   1.387 +  val splits     = Splitter.split_posns cmap thy Ts (REPEAT_DETERM_etac_rev_mp terms)
   1.388 +  val split_limit = ConfigOption.get ctxt fast_arith_split_limit
   1.389 +in
   1.390 +  if length splits > split_limit then
   1.391 +   (tracing ("fast_arith_split_limit exceeded (current value is " ^
   1.392 +      string_of_int split_limit ^ ")"); NONE)
   1.393 +  else (
   1.394 +  case splits of [] =>
   1.395 +    (* split_tac would fail: no possible split *)
   1.396 +    NONE
   1.397 +  | ((_, _, _, split_type, split_term) :: _) => (
   1.398 +    (* ignore all but the first possible split *)
   1.399 +    case strip_comb split_term of
   1.400 +    (* ?P (max ?i ?j) = ((?i <= ?j --> ?P ?j) & (~ ?i <= ?j --> ?P ?i)) *)
   1.401 +      (Const (@{const_name Orderings.max}, _), [t1, t2]) =>
   1.402 +      let
   1.403 +        val rev_terms     = rev terms
   1.404 +        val terms1        = map (subst_term [(split_term, t1)]) rev_terms
   1.405 +        val terms2        = map (subst_term [(split_term, t2)]) rev_terms
   1.406 +        val t1_leq_t2     = Const (@{const_name HOL.less_eq},
   1.407 +                                    split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
   1.408 +        val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
   1.409 +        val not_false     = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   1.410 +        val subgoal1      = (HOLogic.mk_Trueprop t1_leq_t2) :: terms2 @ [not_false]
   1.411 +        val subgoal2      = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms1 @ [not_false]
   1.412 +      in
   1.413 +        SOME [(Ts, subgoal1), (Ts, subgoal2)]
   1.414 +      end
   1.415 +    (* ?P (min ?i ?j) = ((?i <= ?j --> ?P ?i) & (~ ?i <= ?j --> ?P ?j)) *)
   1.416 +    | (Const (@{const_name Orderings.min}, _), [t1, t2]) =>
   1.417 +      let
   1.418 +        val rev_terms     = rev terms
   1.419 +        val terms1        = map (subst_term [(split_term, t1)]) rev_terms
   1.420 +        val terms2        = map (subst_term [(split_term, t2)]) rev_terms
   1.421 +        val t1_leq_t2     = Const (@{const_name HOL.less_eq},
   1.422 +                                    split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
   1.423 +        val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
   1.424 +        val not_false     = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   1.425 +        val subgoal1      = (HOLogic.mk_Trueprop t1_leq_t2) :: terms1 @ [not_false]
   1.426 +        val subgoal2      = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms2 @ [not_false]
   1.427 +      in
   1.428 +        SOME [(Ts, subgoal1), (Ts, subgoal2)]
   1.429 +      end
   1.430 +    (* ?P (abs ?a) = ((0 <= ?a --> ?P ?a) & (?a < 0 --> ?P (- ?a))) *)
   1.431 +    | (Const (@{const_name HOL.abs}, _), [t1]) =>
   1.432 +      let
   1.433 +        val rev_terms   = rev terms
   1.434 +        val terms1      = map (subst_term [(split_term, t1)]) rev_terms
   1.435 +        val terms2      = map (subst_term [(split_term, Const (@{const_name HOL.uminus},
   1.436 +                            split_type --> split_type) $ t1)]) rev_terms
   1.437 +        val zero        = Const (@{const_name HOL.zero}, split_type)
   1.438 +        val zero_leq_t1 = Const (@{const_name HOL.less_eq},
   1.439 +                            split_type --> split_type --> HOLogic.boolT) $ zero $ t1
   1.440 +        val t1_lt_zero  = Const (@{const_name HOL.less},
   1.441 +                            split_type --> split_type --> HOLogic.boolT) $ t1 $ zero
   1.442 +        val not_false   = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   1.443 +        val subgoal1    = (HOLogic.mk_Trueprop zero_leq_t1) :: terms1 @ [not_false]
   1.444 +        val subgoal2    = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
   1.445 +      in
   1.446 +        SOME [(Ts, subgoal1), (Ts, subgoal2)]
   1.447 +      end
   1.448 +    (* ?P (?a - ?b) = ((?a < ?b --> ?P 0) & (ALL d. ?a = ?b + d --> ?P d)) *)
   1.449 +    | (Const (@{const_name HOL.minus}, _), [t1, t2]) =>
   1.450 +      let
   1.451 +        (* "d" in the above theorem becomes a new bound variable after NNF   *)
   1.452 +        (* transformation, therefore some adjustment of indices is necessary *)
   1.453 +        val rev_terms       = rev terms
   1.454 +        val zero            = Const (@{const_name HOL.zero}, split_type)
   1.455 +        val d               = Bound 0
   1.456 +        val terms1          = map (subst_term [(split_term, zero)]) rev_terms
   1.457 +        val terms2          = map (subst_term [(incr_boundvars 1 split_term, d)])
   1.458 +                                (map (incr_boundvars 1) rev_terms)
   1.459 +        val t1'             = incr_boundvars 1 t1
   1.460 +        val t2'             = incr_boundvars 1 t2
   1.461 +        val t1_lt_t2        = Const (@{const_name HOL.less},
   1.462 +                                split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
   1.463 +        val t1_eq_t2_plus_d = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
   1.464 +                                (Const (@{const_name HOL.plus},
   1.465 +                                  split_type --> split_type --> split_type) $ t2' $ d)
   1.466 +        val not_false       = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   1.467 +        val subgoal1        = (HOLogic.mk_Trueprop t1_lt_t2) :: terms1 @ [not_false]
   1.468 +        val subgoal2        = (HOLogic.mk_Trueprop t1_eq_t2_plus_d) :: terms2 @ [not_false]
   1.469 +      in
   1.470 +        SOME [(Ts, subgoal1), (split_type :: Ts, subgoal2)]
   1.471 +      end
   1.472 +    (* ?P (nat ?i) = ((ALL n. ?i = int n --> ?P n) & (?i < 0 --> ?P 0)) *)
   1.473 +    | (Const ("IntDef.nat", _), [t1]) =>
   1.474 +      let
   1.475 +        val rev_terms   = rev terms
   1.476 +        val zero_int    = Const (@{const_name HOL.zero}, HOLogic.intT)
   1.477 +        val zero_nat    = Const (@{const_name HOL.zero}, HOLogic.natT)
   1.478 +        val n           = Bound 0
   1.479 +        val terms1      = map (subst_term [(incr_boundvars 1 split_term, n)])
   1.480 +                            (map (incr_boundvars 1) rev_terms)
   1.481 +        val terms2      = map (subst_term [(split_term, zero_nat)]) rev_terms
   1.482 +        val t1'         = incr_boundvars 1 t1
   1.483 +        val t1_eq_int_n = Const ("op =", HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1' $
   1.484 +                            (Const ("Nat.of_nat", HOLogic.natT --> HOLogic.intT) $ n)
   1.485 +        val t1_lt_zero  = Const (@{const_name HOL.less},
   1.486 +                            HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1 $ zero_int
   1.487 +        val not_false   = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   1.488 +        val subgoal1    = (HOLogic.mk_Trueprop t1_eq_int_n) :: terms1 @ [not_false]
   1.489 +        val subgoal2    = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
   1.490 +      in
   1.491 +        SOME [(HOLogic.natT :: Ts, subgoal1), (Ts, subgoal2)]
   1.492 +      end
   1.493 +    (* "?P ((?n::nat) mod (number_of ?k)) =
   1.494 +         ((number_of ?k = 0 --> ?P ?n) & (~ (number_of ?k = 0) -->
   1.495 +           (ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P j))) *)
   1.496 +    | (Const ("Divides.div_class.mod", Type ("fun", [Type ("nat", []), _])), [t1, t2]) =>
   1.497 +      let
   1.498 +        val rev_terms               = rev terms
   1.499 +        val zero                    = Const (@{const_name HOL.zero}, split_type)
   1.500 +        val i                       = Bound 1
   1.501 +        val j                       = Bound 0
   1.502 +        val terms1                  = map (subst_term [(split_term, t1)]) rev_terms
   1.503 +        val terms2                  = map (subst_term [(incr_boundvars 2 split_term, j)])
   1.504 +                                        (map (incr_boundvars 2) rev_terms)
   1.505 +        val t1'                     = incr_boundvars 2 t1
   1.506 +        val t2'                     = incr_boundvars 2 t2
   1.507 +        val t2_eq_zero              = Const ("op =",
   1.508 +                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
   1.509 +        val t2_neq_zero             = HOLogic.mk_not (Const ("op =",
   1.510 +                                        split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
   1.511 +        val j_lt_t2                 = Const (@{const_name HOL.less},
   1.512 +                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
   1.513 +        val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
   1.514 +                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
   1.515 +                                         (Const (@{const_name HOL.times},
   1.516 +                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
   1.517 +        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   1.518 +        val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
   1.519 +        val subgoal2                = (map HOLogic.mk_Trueprop
   1.520 +                                        [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
   1.521 +                                          @ terms2 @ [not_false]
   1.522 +      in
   1.523 +        SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
   1.524 +      end
   1.525 +    (* "?P ((?n::nat) div (number_of ?k)) =
   1.526 +         ((number_of ?k = 0 --> ?P 0) & (~ (number_of ?k = 0) -->
   1.527 +           (ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P i))) *)
   1.528 +    | (Const ("Divides.div_class.div", Type ("fun", [Type ("nat", []), _])), [t1, t2]) =>
   1.529 +      let
   1.530 +        val rev_terms               = rev terms
   1.531 +        val zero                    = Const (@{const_name HOL.zero}, split_type)
   1.532 +        val i                       = Bound 1
   1.533 +        val j                       = Bound 0
   1.534 +        val terms1                  = map (subst_term [(split_term, zero)]) rev_terms
   1.535 +        val terms2                  = map (subst_term [(incr_boundvars 2 split_term, i)])
   1.536 +                                        (map (incr_boundvars 2) rev_terms)
   1.537 +        val t1'                     = incr_boundvars 2 t1
   1.538 +        val t2'                     = incr_boundvars 2 t2
   1.539 +        val t2_eq_zero              = Const ("op =",
   1.540 +                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
   1.541 +        val t2_neq_zero             = HOLogic.mk_not (Const ("op =",
   1.542 +                                        split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
   1.543 +        val j_lt_t2                 = Const (@{const_name HOL.less},
   1.544 +                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
   1.545 +        val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
   1.546 +                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
   1.547 +                                         (Const (@{const_name HOL.times},
   1.548 +                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
   1.549 +        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   1.550 +        val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
   1.551 +        val subgoal2                = (map HOLogic.mk_Trueprop
   1.552 +                                        [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
   1.553 +                                          @ terms2 @ [not_false]
   1.554 +      in
   1.555 +        SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
   1.556 +      end
   1.557 +    (* "?P ((?n::int) mod (number_of ?k)) =
   1.558 +         ((iszero (number_of ?k) --> ?P ?n) &
   1.559 +          (neg (number_of (uminus ?k)) -->
   1.560 +            (ALL i j. 0 <= j & j < number_of ?k & ?n = number_of ?k * i + j --> ?P j)) &
   1.561 +          (neg (number_of ?k) -->
   1.562 +            (ALL i j. number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j --> ?P j))) *)
   1.563 +    | (Const ("Divides.div_class.mod",
   1.564 +        Type ("fun", [Type ("IntDef.int", []), _])), [t1, t2 as (number_of $ k)]) =>
   1.565 +      let
   1.566 +        val rev_terms               = rev terms
   1.567 +        val zero                    = Const (@{const_name HOL.zero}, split_type)
   1.568 +        val i                       = Bound 1
   1.569 +        val j                       = Bound 0
   1.570 +        val terms1                  = map (subst_term [(split_term, t1)]) rev_terms
   1.571 +        val terms2_3                = map (subst_term [(incr_boundvars 2 split_term, j)])
   1.572 +                                        (map (incr_boundvars 2) rev_terms)
   1.573 +        val t1'                     = incr_boundvars 2 t1
   1.574 +        val (t2' as (_ $ k'))       = incr_boundvars 2 t2
   1.575 +        val iszero_t2               = Const ("IntDef.iszero", split_type --> HOLogic.boolT) $ t2
   1.576 +        val neg_minus_k             = Const ("IntDef.neg", split_type --> HOLogic.boolT) $
   1.577 +                                        (number_of $
   1.578 +                                          (Const (@{const_name HOL.uminus},
   1.579 +                                            HOLogic.intT --> HOLogic.intT) $ k'))
   1.580 +        val zero_leq_j              = Const (@{const_name HOL.less_eq},
   1.581 +                                        split_type --> split_type --> HOLogic.boolT) $ zero $ j
   1.582 +        val j_lt_t2                 = Const (@{const_name HOL.less},
   1.583 +                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
   1.584 +        val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
   1.585 +                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
   1.586 +                                         (Const (@{const_name HOL.times},
   1.587 +                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
   1.588 +        val neg_t2                  = Const ("IntDef.neg", split_type --> HOLogic.boolT) $ t2'
   1.589 +        val t2_lt_j                 = Const (@{const_name HOL.less},
   1.590 +                                        split_type --> split_type--> HOLogic.boolT) $ t2' $ j
   1.591 +        val j_leq_zero              = Const (@{const_name HOL.less_eq},
   1.592 +                                        split_type --> split_type --> HOLogic.boolT) $ j $ zero
   1.593 +        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   1.594 +        val subgoal1                = (HOLogic.mk_Trueprop iszero_t2) :: terms1 @ [not_false]
   1.595 +        val subgoal2                = (map HOLogic.mk_Trueprop [neg_minus_k, zero_leq_j])
   1.596 +                                        @ hd terms2_3
   1.597 +                                        :: (if tl terms2_3 = [] then [not_false] else [])
   1.598 +                                        @ (map HOLogic.mk_Trueprop [j_lt_t2, t1_eq_t2_times_i_plus_j])
   1.599 +                                        @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
   1.600 +        val subgoal3                = (map HOLogic.mk_Trueprop [neg_t2, t2_lt_j])
   1.601 +                                        @ hd terms2_3
   1.602 +                                        :: (if tl terms2_3 = [] then [not_false] else [])
   1.603 +                                        @ (map HOLogic.mk_Trueprop [j_leq_zero, t1_eq_t2_times_i_plus_j])
   1.604 +                                        @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
   1.605 +        val Ts'                     = split_type :: split_type :: Ts
   1.606 +      in
   1.607 +        SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
   1.608 +      end
   1.609 +    (* "?P ((?n::int) div (number_of ?k)) =
   1.610 +         ((iszero (number_of ?k) --> ?P 0) &
   1.611 +          (neg (number_of (uminus ?k)) -->
   1.612 +            (ALL i. (EX j. 0 <= j & j < number_of ?k & ?n = number_of ?k * i + j) --> ?P i)) &
   1.613 +          (neg (number_of ?k) -->
   1.614 +            (ALL i. (EX j. number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j) --> ?P i))) *)
   1.615 +    | (Const ("Divides.div_class.div",
   1.616 +        Type ("fun", [Type ("IntDef.int", []), _])), [t1, t2 as (number_of $ k)]) =>
   1.617 +      let
   1.618 +        val rev_terms               = rev terms
   1.619 +        val zero                    = Const (@{const_name HOL.zero}, split_type)
   1.620 +        val i                       = Bound 1
   1.621 +        val j                       = Bound 0
   1.622 +        val terms1                  = map (subst_term [(split_term, zero)]) rev_terms
   1.623 +        val terms2_3                = map (subst_term [(incr_boundvars 2 split_term, i)])
   1.624 +                                        (map (incr_boundvars 2) rev_terms)
   1.625 +        val t1'                     = incr_boundvars 2 t1
   1.626 +        val (t2' as (_ $ k'))       = incr_boundvars 2 t2
   1.627 +        val iszero_t2               = Const ("IntDef.iszero", split_type --> HOLogic.boolT) $ t2
   1.628 +        val neg_minus_k             = Const ("IntDef.neg", split_type --> HOLogic.boolT) $
   1.629 +                                        (number_of $
   1.630 +                                          (Const (@{const_name HOL.uminus},
   1.631 +                                            HOLogic.intT --> HOLogic.intT) $ k'))
   1.632 +        val zero_leq_j              = Const (@{const_name HOL.less_eq},
   1.633 +                                        split_type --> split_type --> HOLogic.boolT) $ zero $ j
   1.634 +        val j_lt_t2                 = Const (@{const_name HOL.less},
   1.635 +                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
   1.636 +        val t1_eq_t2_times_i_plus_j = Const ("op =",
   1.637 +                                        split_type --> split_type --> HOLogic.boolT) $ t1' $
   1.638 +                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
   1.639 +                                         (Const (@{const_name HOL.times},
   1.640 +                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
   1.641 +        val neg_t2                  = Const ("IntDef.neg", split_type --> HOLogic.boolT) $ t2'
   1.642 +        val t2_lt_j                 = Const (@{const_name HOL.less},
   1.643 +                                        split_type --> split_type--> HOLogic.boolT) $ t2' $ j
   1.644 +        val j_leq_zero              = Const (@{const_name HOL.less_eq},
   1.645 +                                        split_type --> split_type --> HOLogic.boolT) $ j $ zero
   1.646 +        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
   1.647 +        val subgoal1                = (HOLogic.mk_Trueprop iszero_t2) :: terms1 @ [not_false]
   1.648 +        val subgoal2                = (HOLogic.mk_Trueprop neg_minus_k)
   1.649 +                                        :: terms2_3
   1.650 +                                        @ not_false
   1.651 +                                        :: (map HOLogic.mk_Trueprop
   1.652 +                                             [zero_leq_j, j_lt_t2, t1_eq_t2_times_i_plus_j])
   1.653 +        val subgoal3                = (HOLogic.mk_Trueprop neg_t2)
   1.654 +                                        :: terms2_3
   1.655 +                                        @ not_false
   1.656 +                                        :: (map HOLogic.mk_Trueprop
   1.657 +                                             [t2_lt_j, j_leq_zero, t1_eq_t2_times_i_plus_j])
   1.658 +        val Ts'                     = split_type :: split_type :: Ts
   1.659 +      in
   1.660 +        SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
   1.661 +      end
   1.662 +    (* this will only happen if a split theorem can be applied for which no  *)
   1.663 +    (* code exists above -- in which case either the split theorem should be *)
   1.664 +    (* implemented above, or 'is_split_thm' should be modified to filter it  *)
   1.665 +    (* out                                                                   *)
   1.666 +    | (t, ts) => (
   1.667 +      warning ("Lin. Arith.: split rule for " ^ ProofContext.string_of_term ctxt t ^
   1.668 +               " (with " ^ string_of_int (length ts) ^
   1.669 +               " argument(s)) not implemented; proof reconstruction is likely to fail");
   1.670 +      NONE
   1.671 +    ))
   1.672 +  )
   1.673 +end;
   1.674 +
   1.675 +(* remove terms that do not satisfy 'p'; change the order of the remaining   *)
   1.676 +(* terms in the same way as filter_prems_tac does                            *)
   1.677 +
   1.678 +fun filter_prems_tac_items (p : term -> bool) (terms : term list) : term list =
   1.679 +let
   1.680 +  fun filter_prems (t, (left, right)) =
   1.681 +    if  p t  then  (left, right @ [t])  else  (left @ right, [])
   1.682 +  val (left, right) = foldl filter_prems ([], []) terms
   1.683 +in
   1.684 +  right @ left
   1.685 +end;
   1.686 +
   1.687 +(* return true iff TRY (etac notE) THEN eq_assume_tac would succeed on a     *)
   1.688 +(* subgoal that has 'terms' as premises                                      *)
   1.689 +
   1.690 +fun negated_term_occurs_positively (terms : term list) : bool =
   1.691 +  List.exists
   1.692 +    (fn (Trueprop $ (Const ("Not", _) $ t)) => member (op aconv) terms (Trueprop $ t)
   1.693 +      | _                                   => false)
   1.694 +    terms;
   1.695 +
   1.696 +fun pre_decomp ctxt (Ts : typ list, terms : term list) : (typ list * term list) list =
   1.697 +let
   1.698 +  (* repeatedly split (including newly emerging subgoals) until no further   *)
   1.699 +  (* splitting is possible                                                   *)
   1.700 +  fun split_loop ([] : (typ list * term list) list) = ([] : (typ list * term list) list)
   1.701 +    | split_loop (subgoal::subgoals)                = (
   1.702 +        case split_once_items ctxt subgoal of
   1.703 +          SOME new_subgoals => split_loop (new_subgoals @ subgoals)
   1.704 +        | NONE              => subgoal :: split_loop subgoals
   1.705 +      )
   1.706 +  fun is_relevant t  = isSome (decomp ctxt t)
   1.707 +  (* filter_prems_tac is_relevant: *)
   1.708 +  val relevant_terms = filter_prems_tac_items is_relevant terms
   1.709 +  (* split_tac, NNF normalization: *)
   1.710 +  val split_goals    = split_loop [(Ts, relevant_terms)]
   1.711 +  (* necessary because split_once_tac may normalize terms: *)
   1.712 +  val beta_eta_norm  = map (apsnd (map (Envir.eta_contract o Envir.beta_norm))) split_goals
   1.713 +  (* TRY (etac notE) THEN eq_assume_tac: *)
   1.714 +  val result         = List.filter (not o negated_term_occurs_positively o snd) beta_eta_norm
   1.715 +in
   1.716 +  result
   1.717 +end;
   1.718 +
   1.719 +(* takes the i-th subgoal  [| A1; ...; An |] ==> B  to                       *)
   1.720 +(* An --> ... --> A1 --> B,  performs splitting with the given 'split_thms'  *)
   1.721 +(* (resulting in a different subgoal P), takes  P  to  ~P ==> False,         *)
   1.722 +(* performs NNF-normalization of ~P, and eliminates conjunctions,            *)
   1.723 +(* disjunctions and existential quantifiers from the premises, possibly (in  *)
   1.724 +(* the case of disjunctions) resulting in several new subgoals, each of the  *)
   1.725 +(* general form  [| Q1; ...; Qm |] ==> False.  Fails if more than            *)
   1.726 +(* !fast_arith_split_limit splits are possible.                              *)
   1.727 +
   1.728 +local
   1.729 +  val nnf_simpset =
   1.730 +    empty_ss setmkeqTrue mk_eq_True
   1.731 +    setmksimps (mksimps mksimps_pairs)
   1.732 +    addsimps [imp_conv_disj, iff_conv_conj_imp, de_Morgan_disj, de_Morgan_conj,
   1.733 +      not_all, not_ex, not_not]
   1.734 +  fun prem_nnf_tac i st =
   1.735 +    full_simp_tac (Simplifier.theory_context (Thm.theory_of_thm st) nnf_simpset) i st
   1.736 +in
   1.737 +
   1.738 +fun split_once_tac ctxt split_thms =
   1.739 +  let
   1.740 +    val thy = ProofContext.theory_of ctxt
   1.741 +    val cond_split_tac = SUBGOAL (fn (subgoal, i) =>
   1.742 +      let
   1.743 +        val Ts = rev (map snd (Logic.strip_params subgoal))
   1.744 +        val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl subgoal)
   1.745 +        val cmap = Splitter.cmap_of_split_thms split_thms
   1.746 +        val splits = Splitter.split_posns cmap thy Ts concl
   1.747 +        val split_limit = ConfigOption.get ctxt fast_arith_split_limit
   1.748 +      in
   1.749 +        if length splits > split_limit then no_tac
   1.750 +        else split_tac split_thms i
   1.751 +      end)
   1.752 +  in
   1.753 +    EVERY' [
   1.754 +      REPEAT_DETERM o etac rev_mp,
   1.755 +      cond_split_tac,
   1.756 +      rtac ccontr,
   1.757 +      prem_nnf_tac,
   1.758 +      TRY o REPEAT_ALL_NEW (DETERM o (eresolve_tac [conjE, exE] ORELSE' etac disjE))
   1.759 +    ]
   1.760 +  end;
   1.761 +
   1.762 +end;  (* local *)
   1.763 +
   1.764 +(* remove irrelevant premises, then split the i-th subgoal (and all new      *)
   1.765 +(* subgoals) by using 'split_once_tac' repeatedly.  Beta-eta-normalize new   *)
   1.766 +(* subgoals and finally attempt to solve them by finding an immediate        *)
   1.767 +(* contradiction (i.e. a term and its negation) in their premises.           *)
   1.768 +
   1.769 +fun pre_tac ctxt i =
   1.770 +let
   1.771 +  val split_thms = filter is_split_thm (#splits (get_arith_data ctxt))
   1.772 +  fun is_relevant t = isSome (decomp ctxt t)
   1.773 +in
   1.774 +  DETERM (
   1.775 +    TRY (filter_prems_tac is_relevant i)
   1.776 +      THEN (
   1.777 +        (TRY o REPEAT_ALL_NEW (split_once_tac ctxt split_thms))
   1.778 +          THEN_ALL_NEW
   1.779 +            (CONVERSION Drule.beta_eta_conversion
   1.780 +              THEN'
   1.781 +            (TRY o (etac notE THEN' eq_assume_tac)))
   1.782 +      ) i
   1.783 +  )
   1.784 +end;
   1.785 +
   1.786 +end;  (* LA_Data_Ref *)
   1.787 +
   1.788 +
   1.789 +val lin_arith_pre_tac = LA_Data_Ref.pre_tac;
   1.790 +
   1.791 +structure Fast_Arith =
   1.792 +  Fast_Lin_Arith(structure LA_Logic=LA_Logic and LA_Data=LA_Data_Ref);
   1.793 +
   1.794 +val map_data = Fast_Arith.map_data;
   1.795 +
   1.796 +fun fast_arith_tac ctxt    = Fast_Arith.lin_arith_tac ctxt false;
   1.797 +val fast_ex_arith_tac      = Fast_Arith.lin_arith_tac;
   1.798 +val trace_arith            = Fast_Arith.trace;
   1.799 +
   1.800 +(* reduce contradictory <= to False.
   1.801 +   Most of the work is done by the cancel tactics. *)
   1.802 +
   1.803 +val init_arith_data =
   1.804 + Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, ...} =>
   1.805 +   {add_mono_thms = add_mono_thms @
   1.806 +    @{thms add_mono_thms_ordered_semiring} @ @{thms add_mono_thms_ordered_field},
   1.807 +    mult_mono_thms = mult_mono_thms,
   1.808 +    inj_thms = inj_thms,
   1.809 +    lessD = lessD @ [thm "Suc_leI"],
   1.810 +    neqE = [@{thm linorder_neqE_nat}, @{thm linorder_neqE_ordered_idom}],
   1.811 +    simpset = HOL_basic_ss
   1.812 +      addsimps
   1.813 +       [@{thm "monoid_add_class.zero_plus.add_0_left"},
   1.814 +        @{thm "monoid_add_class.zero_plus.add_0_right"},
   1.815 +        @{thm "Zero_not_Suc"}, @{thm "Suc_not_Zero"}, @{thm "le_0_eq"}, @{thm "One_nat_def"},
   1.816 +        @{thm "order_less_irrefl"}, @{thm "zero_neq_one"}, @{thm "zero_less_one"},
   1.817 +        @{thm "zero_le_one"}, @{thm "zero_neq_one"} RS not_sym, @{thm "not_one_le_zero"},
   1.818 +        @{thm "not_one_less_zero"}]
   1.819 +      addsimprocs [ab_group_add_cancel.sum_conv, ab_group_add_cancel.rel_conv]
   1.820 +       (*abel_cancel helps it work in abstract algebraic domains*)
   1.821 +      addsimprocs nat_cancel_sums_add}) #>
   1.822 +  arith_discrete "nat";
   1.823 +
   1.824 +val lin_arith_simproc = Fast_Arith.lin_arith_simproc;
   1.825 +
   1.826 +val fast_nat_arith_simproc =
   1.827 +  Simplifier.simproc (the_context ()) "fast_nat_arith"
   1.828 +    ["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"] (K Fast_Arith.lin_arith_simproc);
   1.829 +
   1.830 +(* Because of fast_nat_arith_simproc, the arithmetic solver is really only
   1.831 +useful to detect inconsistencies among the premises for subgoals which are
   1.832 +*not* themselves (in)equalities, because the latter activate
   1.833 +fast_nat_arith_simproc anyway. However, it seems cheaper to activate the
   1.834 +solver all the time rather than add the additional check. *)
   1.835 +
   1.836 +
   1.837 +(* arith proof method *)
   1.838 +
   1.839 +local
   1.840 +
   1.841 +fun raw_arith_tac ctxt ex =
   1.842 +  (* FIXME: K true should be replaced by a sensible test (perhaps "isSome o
   1.843 +     decomp sg"? -- but note that the test is applied to terms already before
   1.844 +     they are split/normalized) to speed things up in case there are lots of
   1.845 +     irrelevant terms involved; elimination of min/max can be optimized:
   1.846 +     (max m n + k <= r) = (m+k <= r & n+k <= r)
   1.847 +     (l <= min m n + k) = (l <= m+k & l <= n+k)
   1.848 +  *)
   1.849 +  refute_tac (K true)
   1.850 +    (* Splitting is also done inside fast_arith_tac, but not completely --   *)
   1.851 +    (* split_tac may use split theorems that have not been implemented in    *)
   1.852 +    (* fast_arith_tac (cf. pre_decomp and split_once_items above), and       *)
   1.853 +    (* fast_arith_split_limit may trigger.                                   *)
   1.854 +    (* Therefore splitting outside of fast_arith_tac may allow us to prove   *)
   1.855 +    (* some goals that fast_arith_tac alone would fail on.                   *)
   1.856 +    (REPEAT_DETERM o split_tac (#splits (get_arith_data ctxt)))
   1.857 +    (fast_ex_arith_tac ctxt ex);
   1.858 +
   1.859 +fun more_arith_tacs ctxt =
   1.860 +  let val tactics = #tactics (get_arith_data ctxt)
   1.861 +  in FIRST' (map (fn ArithTactic {tactic, ...} => tactic ctxt) tactics) end;
   1.862 +
   1.863 +in
   1.864 +
   1.865 +fun simple_arith_tac ctxt = FIRST' [fast_arith_tac ctxt,
   1.866 +  ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_arith_tac ctxt true];
   1.867 +
   1.868 +fun arith_tac ctxt = FIRST' [fast_arith_tac ctxt,
   1.869 +  ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_arith_tac ctxt true,
   1.870 +  more_arith_tacs ctxt];
   1.871 +
   1.872 +fun silent_arith_tac ctxt = FIRST' [fast_arith_tac ctxt,
   1.873 +  ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_arith_tac ctxt false,
   1.874 +  more_arith_tacs ctxt];
   1.875 +
   1.876 +fun arith_method src =
   1.877 +  Method.syntax Args.bang_facts src
   1.878 +  #> (fn (prems, ctxt) => Method.METHOD (fn facts =>
   1.879 +      HEADGOAL (Method.insert_tac (prems @ facts) THEN' arith_tac ctxt)));
   1.880 +
   1.881 +end;
   1.882 +
   1.883 +
   1.884 +(* context setup *)
   1.885 +
   1.886 +val setup =
   1.887 +  init_arith_data #>
   1.888 +  Simplifier.map_ss (fn ss => ss addsimprocs [fast_nat_arith_simproc]
   1.889 +    addSolver (mk_solver' "lin_arith" Fast_Arith.cut_lin_arith_tac)) #>
   1.890 +  Context.mapping
   1.891 +   (setup_options #>
   1.892 +    Method.add_methods
   1.893 +      [("arith", arith_method, "decide linear arithmethic")] #>
   1.894 +    Attrib.add_attributes [("arith_split", Attrib.no_args arith_split_add,
   1.895 +      "declaration of split rules for arithmetic procedure")]) I;
   1.896 +
   1.897 +end;
   1.898 +
   1.899 +structure BasicLinArith: BASIC_LIN_ARITH = LinArith;
   1.900 +open BasicLinArith;