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