src/HOL/Tools/lin_arith.ML
author haftmann
Thu Aug 09 15:52:47 2007 +0200 (2007-08-09)
changeset 24196 f1dbfd7e3223
parent 24112 6c4e7d17f9b0
child 24271 499608101177
permissions -rw-r--r--
localized of_nat
wenzelm@24092
     1
(*  Title:      HOL/Tools/lin_arith.ML
wenzelm@24092
     2
    ID:         $Id$
wenzelm@24092
     3
    Author:     Tjark Weber and Tobias Nipkow
wenzelm@24092
     4
wenzelm@24092
     5
HOL setup for linear arithmetic (see Provers/Arith/fast_lin_arith.ML).
wenzelm@24092
     6
*)
wenzelm@24092
     7
wenzelm@24092
     8
signature BASIC_LIN_ARITH =
wenzelm@24092
     9
sig
wenzelm@24092
    10
  type arith_tactic
wenzelm@24092
    11
  val mk_arith_tactic: string -> (Proof.context -> int -> tactic) -> arith_tactic
wenzelm@24092
    12
  val eq_arith_tactic: arith_tactic * arith_tactic -> bool
wenzelm@24092
    13
  val arith_split_add: attribute
wenzelm@24092
    14
  val arith_discrete: string -> Context.generic -> Context.generic
wenzelm@24092
    15
  val arith_inj_const: string * typ -> Context.generic -> Context.generic
wenzelm@24092
    16
  val arith_tactic_add: arith_tactic -> Context.generic -> Context.generic
wenzelm@24112
    17
  val fast_arith_split_limit: int Config.T
wenzelm@24112
    18
  val fast_arith_neq_limit: int Config.T
wenzelm@24092
    19
  val lin_arith_pre_tac: Proof.context -> int -> tactic
wenzelm@24092
    20
  val fast_arith_tac: Proof.context -> int -> tactic
wenzelm@24092
    21
  val fast_ex_arith_tac: Proof.context -> bool -> int -> tactic
wenzelm@24092
    22
  val trace_arith: bool ref
wenzelm@24092
    23
  val lin_arith_simproc: simpset -> term -> thm option
wenzelm@24092
    24
  val fast_nat_arith_simproc: simproc
wenzelm@24092
    25
  val simple_arith_tac: Proof.context -> int -> tactic
wenzelm@24092
    26
  val arith_tac: Proof.context -> int -> tactic
wenzelm@24092
    27
  val silent_arith_tac: Proof.context -> int -> tactic
wenzelm@24092
    28
end;
wenzelm@24092
    29
wenzelm@24092
    30
signature LIN_ARITH =
wenzelm@24092
    31
sig
wenzelm@24092
    32
  include BASIC_LIN_ARITH
wenzelm@24092
    33
  val map_data:
wenzelm@24092
    34
    ({add_mono_thms: thm list, mult_mono_thms: thm list, inj_thms: thm list,
wenzelm@24092
    35
      lessD: thm list, neqE: thm list, simpset: Simplifier.simpset} ->
wenzelm@24092
    36
     {add_mono_thms: thm list, mult_mono_thms: thm list, inj_thms: thm list,
wenzelm@24092
    37
      lessD: thm list, neqE: thm list, simpset: Simplifier.simpset}) ->
wenzelm@24092
    38
    Context.generic -> Context.generic
wenzelm@24092
    39
  val setup: Context.generic -> Context.generic
wenzelm@24092
    40
end;
wenzelm@24092
    41
wenzelm@24092
    42
structure LinArith: LIN_ARITH =
wenzelm@24092
    43
struct
wenzelm@24092
    44
wenzelm@24092
    45
(* Parameters data for general linear arithmetic functor *)
wenzelm@24092
    46
wenzelm@24092
    47
structure LA_Logic: LIN_ARITH_LOGIC =
wenzelm@24092
    48
struct
wenzelm@24092
    49
wenzelm@24092
    50
val ccontr = ccontr;
wenzelm@24092
    51
val conjI = conjI;
wenzelm@24092
    52
val notI = notI;
wenzelm@24092
    53
val sym = sym;
wenzelm@24092
    54
val not_lessD = @{thm linorder_not_less} RS iffD1;
wenzelm@24092
    55
val not_leD = @{thm linorder_not_le} RS iffD1;
wenzelm@24092
    56
val le0 = thm "le0";
wenzelm@24092
    57
wenzelm@24092
    58
fun mk_Eq thm = (thm RS Eq_FalseI) handle THM _ => (thm RS Eq_TrueI);
wenzelm@24092
    59
wenzelm@24092
    60
val mk_Trueprop = HOLogic.mk_Trueprop;
wenzelm@24092
    61
wenzelm@24092
    62
fun atomize thm = case Thm.prop_of thm of
wenzelm@24092
    63
    Const("Trueprop",_) $ (Const("op &",_) $ _ $ _) =>
wenzelm@24092
    64
    atomize(thm RS conjunct1) @ atomize(thm RS conjunct2)
wenzelm@24092
    65
  | _ => [thm];
wenzelm@24092
    66
wenzelm@24092
    67
fun neg_prop ((TP as Const("Trueprop",_)) $ (Const("Not",_) $ t)) = TP $ t
wenzelm@24092
    68
  | neg_prop ((TP as Const("Trueprop",_)) $ t) = TP $ (HOLogic.Not $t)
wenzelm@24092
    69
  | neg_prop t = raise TERM ("neg_prop", [t]);
wenzelm@24092
    70
wenzelm@24092
    71
fun is_False thm =
wenzelm@24092
    72
  let val _ $ t = Thm.prop_of thm
wenzelm@24092
    73
  in t = Const("False",HOLogic.boolT) end;
wenzelm@24092
    74
wenzelm@24092
    75
fun is_nat(t) = fastype_of1 t = HOLogic.natT;
wenzelm@24092
    76
wenzelm@24092
    77
fun mk_nat_thm sg t =
wenzelm@24092
    78
  let val ct = cterm_of sg t  and cn = cterm_of sg (Var(("n",0),HOLogic.natT))
wenzelm@24092
    79
  in instantiate ([],[(cn,ct)]) le0 end;
wenzelm@24092
    80
wenzelm@24092
    81
end;
wenzelm@24092
    82
wenzelm@24092
    83
wenzelm@24092
    84
(* arith context data *)
wenzelm@24092
    85
wenzelm@24092
    86
datatype arith_tactic =
wenzelm@24092
    87
  ArithTactic of {name: string, tactic: Proof.context -> int -> tactic, id: stamp};
wenzelm@24092
    88
wenzelm@24092
    89
fun mk_arith_tactic name tactic = ArithTactic {name = name, tactic = tactic, id = stamp ()};
wenzelm@24092
    90
wenzelm@24092
    91
fun eq_arith_tactic (ArithTactic {id = id1, ...}, ArithTactic {id = id2, ...}) = (id1 = id2);
wenzelm@24092
    92
wenzelm@24092
    93
structure ArithContextData = GenericDataFun
wenzelm@24092
    94
(
wenzelm@24092
    95
  type T = {splits: thm list,
wenzelm@24092
    96
            inj_consts: (string * typ) list,
wenzelm@24092
    97
            discrete: string list,
wenzelm@24092
    98
            tactics: arith_tactic list};
wenzelm@24092
    99
  val empty = {splits = [], inj_consts = [], discrete = [], tactics = []};
wenzelm@24092
   100
  val extend = I;
wenzelm@24092
   101
  fun merge _ ({splits= splits1, inj_consts= inj_consts1, discrete= discrete1, tactics= tactics1},
wenzelm@24092
   102
             {splits= splits2, inj_consts= inj_consts2, discrete= discrete2, tactics= tactics2}) =
wenzelm@24092
   103
   {splits = Library.merge Thm.eq_thm_prop (splits1, splits2),
wenzelm@24092
   104
    inj_consts = Library.merge (op =) (inj_consts1, inj_consts2),
wenzelm@24092
   105
    discrete = Library.merge (op =) (discrete1, discrete2),
wenzelm@24092
   106
    tactics = Library.merge eq_arith_tactic (tactics1, tactics2)};
wenzelm@24092
   107
);
wenzelm@24092
   108
wenzelm@24092
   109
val get_arith_data = ArithContextData.get o Context.Proof;
wenzelm@24092
   110
wenzelm@24092
   111
val arith_split_add = Thm.declaration_attribute (fn thm =>
wenzelm@24092
   112
  ArithContextData.map (fn {splits, inj_consts, discrete, tactics} =>
wenzelm@24112
   113
    {splits = update Thm.eq_thm_prop thm splits,
wenzelm@24092
   114
     inj_consts = inj_consts, discrete = discrete, tactics = tactics}));
wenzelm@24092
   115
wenzelm@24092
   116
fun arith_discrete d = ArithContextData.map (fn {splits, inj_consts, discrete, tactics} =>
wenzelm@24092
   117
  {splits = splits, inj_consts = inj_consts,
wenzelm@24112
   118
   discrete = update (op =) d discrete, tactics = tactics});
wenzelm@24092
   119
wenzelm@24092
   120
fun arith_inj_const c = ArithContextData.map (fn {splits, inj_consts, discrete, tactics} =>
wenzelm@24112
   121
  {splits = splits, inj_consts = update (op =) c inj_consts,
wenzelm@24092
   122
   discrete = discrete, tactics= tactics});
wenzelm@24092
   123
wenzelm@24092
   124
fun arith_tactic_add tac = ArithContextData.map (fn {splits, inj_consts, discrete, tactics} =>
wenzelm@24092
   125
  {splits = splits, inj_consts = inj_consts, discrete = discrete,
wenzelm@24112
   126
   tactics = update eq_arith_tactic tac tactics});
wenzelm@24092
   127
wenzelm@24092
   128
wenzelm@24112
   129
val (fast_arith_split_limit, setup1) = Attrib.config_int "fast_arith_split_limit" 9;
wenzelm@24112
   130
val (fast_arith_neq_limit, setup2) = Attrib.config_int "fast_arith_neq_limit" 9;
wenzelm@24092
   131
val setup_options = setup1 #> setup2;
wenzelm@24092
   132
wenzelm@24092
   133
wenzelm@24092
   134
structure LA_Data_Ref =
wenzelm@24092
   135
struct
wenzelm@24092
   136
wenzelm@24092
   137
val fast_arith_neq_limit = fast_arith_neq_limit;
wenzelm@24092
   138
wenzelm@24092
   139
wenzelm@24092
   140
(* Decomposition of terms *)
wenzelm@24092
   141
wenzelm@24092
   142
(*internal representation of linear (in-)equations*)
wenzelm@24092
   143
type decompT = ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat * bool);
wenzelm@24092
   144
wenzelm@24092
   145
fun nT (Type ("fun", [N, _])) = (N = HOLogic.natT)
wenzelm@24092
   146
  | nT _                      = false;
wenzelm@24092
   147
wenzelm@24092
   148
fun add_atom (t : term) (m : Rat.rat) (p : (term * Rat.rat) list, i : Rat.rat) :
wenzelm@24092
   149
             (term * Rat.rat) list * Rat.rat =
wenzelm@24092
   150
  case AList.lookup (op =) p t of NONE   => ((t, m) :: p, i)
wenzelm@24092
   151
                                | SOME n => (AList.update (op =) (t, Rat.add n m) p, i);
wenzelm@24092
   152
wenzelm@24092
   153
exception Zero;
wenzelm@24092
   154
wenzelm@24092
   155
fun rat_of_term (numt, dent) =
wenzelm@24092
   156
  let
wenzelm@24092
   157
    val num = HOLogic.dest_numeral numt
wenzelm@24092
   158
    val den = HOLogic.dest_numeral dent
wenzelm@24092
   159
  in
wenzelm@24092
   160
    if den = 0 then raise Zero else Rat.rat_of_quotient (num, den)
wenzelm@24092
   161
  end;
wenzelm@24092
   162
wenzelm@24092
   163
(*Warning: in rare cases number_of encloses a non-numeral,
wenzelm@24092
   164
  in which case dest_numeral raises TERM; hence all the handles below.
wenzelm@24092
   165
  Same for Suc-terms that turn out not to be numerals -
wenzelm@24092
   166
  although the simplifier should eliminate those anyway ...*)
wenzelm@24092
   167
fun number_of_Sucs (Const ("Suc", _) $ n) : int =
wenzelm@24092
   168
      number_of_Sucs n + 1
wenzelm@24092
   169
  | number_of_Sucs t =
wenzelm@24092
   170
      if HOLogic.is_zero t then 0 else raise TERM ("number_of_Sucs", []);
wenzelm@24092
   171
wenzelm@24092
   172
(*decompose nested multiplications, bracketing them to the right and combining
wenzelm@24092
   173
  all their coefficients*)
wenzelm@24092
   174
fun demult (inj_consts : (string * typ) list) : term * Rat.rat -> term option * Rat.rat =
wenzelm@24092
   175
let
wenzelm@24092
   176
  fun demult ((mC as Const (@{const_name HOL.times}, _)) $ s $ t, m) = (
wenzelm@24092
   177
    (case s of
wenzelm@24092
   178
      Const ("Numeral.number_class.number_of", _) $ n =>
wenzelm@24092
   179
        demult (t, Rat.mult m (Rat.rat_of_int (HOLogic.dest_numeral n)))
wenzelm@24092
   180
    | Const (@{const_name HOL.uminus}, _) $ (Const ("Numeral.number_class.number_of", _) $ n) =>
wenzelm@24092
   181
        demult (t, Rat.mult m (Rat.rat_of_int (~(HOLogic.dest_numeral n))))
wenzelm@24092
   182
    | Const (@{const_name Suc}, _) $ _ =>
wenzelm@24092
   183
        demult (t, Rat.mult m (Rat.rat_of_int (HOLogic.dest_nat s)))
wenzelm@24092
   184
    | Const (@{const_name HOL.times}, _) $ s1 $ s2 =>
wenzelm@24092
   185
        demult (mC $ s1 $ (mC $ s2 $ t), m)
wenzelm@24092
   186
    | Const (@{const_name HOL.divide}, _) $ numt $
wenzelm@24092
   187
          (Const ("Numeral.number_class.number_of", _) $ dent) =>
wenzelm@24092
   188
        let
wenzelm@24092
   189
          val den = HOLogic.dest_numeral dent
wenzelm@24092
   190
        in
wenzelm@24092
   191
          if den = 0 then
wenzelm@24092
   192
            raise Zero
wenzelm@24092
   193
          else
wenzelm@24092
   194
            demult (mC $ numt $ t, Rat.mult m (Rat.inv (Rat.rat_of_int den)))
wenzelm@24092
   195
        end
wenzelm@24092
   196
    | _ =>
wenzelm@24092
   197
        atomult (mC, s, t, m)
wenzelm@24092
   198
    ) handle TERM _ => atomult (mC, s, t, m)
wenzelm@24092
   199
  )
wenzelm@24092
   200
    | demult (atom as Const(@{const_name HOL.divide}, _) $ t $
wenzelm@24092
   201
        (Const ("Numeral.number_class.number_of", _) $ dent), m) =
wenzelm@24092
   202
      (let
wenzelm@24092
   203
        val den = HOLogic.dest_numeral dent
wenzelm@24092
   204
      in
wenzelm@24092
   205
        if den = 0 then
wenzelm@24092
   206
          raise Zero
wenzelm@24092
   207
        else
wenzelm@24092
   208
          demult (t, Rat.mult m (Rat.inv (Rat.rat_of_int den)))
wenzelm@24092
   209
      end handle TERM _ => (SOME atom, m))
wenzelm@24092
   210
    | demult (Const (@{const_name HOL.zero}, _), m) = (NONE, Rat.zero)
wenzelm@24092
   211
    | demult (Const (@{const_name HOL.one}, _), m) = (NONE, m)
wenzelm@24092
   212
    | demult (t as Const ("Numeral.number_class.number_of", _) $ n, m) =
wenzelm@24092
   213
        ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_numeral n)))
wenzelm@24092
   214
          handle TERM _ => (SOME t, m))
wenzelm@24092
   215
    | demult (Const (@{const_name HOL.uminus}, _) $ t, m) = demult (t, Rat.neg m)
wenzelm@24092
   216
    | demult (t as Const f $ x, m) =
wenzelm@24092
   217
        (if member (op =) inj_consts f then SOME x else SOME t, m)
wenzelm@24092
   218
    | demult (atom, m) = (SOME atom, m)
wenzelm@24092
   219
and
wenzelm@24092
   220
  atomult (mC, atom, t, m) = (
wenzelm@24092
   221
    case demult (t, m) of (NONE, m')    => (SOME atom, m')
wenzelm@24092
   222
                        | (SOME t', m') => (SOME (mC $ atom $ t'), m')
wenzelm@24092
   223
  )
wenzelm@24092
   224
in demult end;
wenzelm@24092
   225
wenzelm@24092
   226
fun decomp0 (inj_consts : (string * typ) list) (rel : string, lhs : term, rhs : term) :
wenzelm@24092
   227
            ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat) option =
wenzelm@24092
   228
let
wenzelm@24092
   229
  (* Turn term into list of summand * multiplicity plus a constant *)
wenzelm@24092
   230
  fun poly (Const (@{const_name HOL.plus}, _) $ s $ t,
wenzelm@24092
   231
        m : Rat.rat, pi : (term * Rat.rat) list * Rat.rat) = poly (s, m, poly (t, m, pi))
wenzelm@24092
   232
    | poly (all as Const (@{const_name HOL.minus}, T) $ s $ t, m, pi) =
wenzelm@24092
   233
        if nT T then add_atom all m pi else poly (s, m, poly (t, Rat.neg m, pi))
wenzelm@24092
   234
    | poly (all as Const (@{const_name HOL.uminus}, T) $ t, m, pi) =
wenzelm@24092
   235
        if nT T then add_atom all m pi else poly (t, Rat.neg m, pi)
wenzelm@24092
   236
    | poly (Const (@{const_name HOL.zero}, _), _, pi) =
wenzelm@24092
   237
        pi
wenzelm@24092
   238
    | poly (Const (@{const_name HOL.one}, _), m, (p, i)) =
wenzelm@24092
   239
        (p, Rat.add i m)
wenzelm@24092
   240
    | poly (Const (@{const_name Suc}, _) $ t, m, (p, i)) =
wenzelm@24092
   241
        poly (t, m, (p, Rat.add i m))
wenzelm@24092
   242
    | poly (all as Const (@{const_name HOL.times}, _) $ _ $ _, m, pi as (p, i)) =
wenzelm@24092
   243
        (case demult inj_consts (all, m) of
wenzelm@24092
   244
           (NONE,   m') => (p, Rat.add i m')
wenzelm@24092
   245
         | (SOME u, m') => add_atom u m' pi)
wenzelm@24092
   246
    | poly (all as Const (@{const_name HOL.divide}, _) $ _ $ _, m, pi as (p, i)) =
wenzelm@24092
   247
        (case demult inj_consts (all, m) of
wenzelm@24092
   248
           (NONE,   m') => (p, Rat.add i m')
wenzelm@24092
   249
         | (SOME u, m') => add_atom u m' pi)
wenzelm@24092
   250
    | poly (all as Const ("Numeral.number_class.number_of", Type(_,[_,T])) $ t, m, pi as (p, i)) =
wenzelm@24092
   251
        (let val k = HOLogic.dest_numeral t
wenzelm@24092
   252
            val k2 = if k < 0 andalso T = HOLogic.natT then 0 else k
wenzelm@24092
   253
        in (p, Rat.add i (Rat.mult m (Rat.rat_of_int k2))) end
wenzelm@24092
   254
        handle TERM _ => add_atom all m pi)
wenzelm@24092
   255
    | poly (all as Const f $ x, m, pi) =
wenzelm@24092
   256
        if f mem inj_consts then poly (x, m, pi) else add_atom all m pi
wenzelm@24092
   257
    | poly (all, m, pi) =
wenzelm@24092
   258
        add_atom all m pi
wenzelm@24092
   259
  val (p, i) = poly (lhs, Rat.one, ([], Rat.zero))
wenzelm@24092
   260
  val (q, j) = poly (rhs, Rat.one, ([], Rat.zero))
wenzelm@24092
   261
in
wenzelm@24092
   262
  case rel of
wenzelm@24092
   263
    @{const_name HOL.less}    => SOME (p, i, "<", q, j)
wenzelm@24092
   264
  | @{const_name HOL.less_eq} => SOME (p, i, "<=", q, j)
wenzelm@24092
   265
  | "op ="              => SOME (p, i, "=", q, j)
wenzelm@24092
   266
  | _                   => NONE
wenzelm@24092
   267
end handle Zero => NONE;
wenzelm@24092
   268
wenzelm@24092
   269
fun of_lin_arith_sort sg (U : typ) : bool =
wenzelm@24092
   270
  Type.of_sort (Sign.tsig_of sg) (U, ["Ring_and_Field.ordered_idom"])
wenzelm@24092
   271
wenzelm@24092
   272
fun allows_lin_arith sg (discrete : string list) (U as Type (D, [])) : bool * bool =
wenzelm@24092
   273
  if of_lin_arith_sort sg U then
wenzelm@24092
   274
    (true, D mem discrete)
wenzelm@24092
   275
  else (* special cases *)
wenzelm@24092
   276
    if D mem discrete then  (true, true)  else  (false, false)
wenzelm@24092
   277
  | allows_lin_arith sg discrete U =
wenzelm@24092
   278
  (of_lin_arith_sort sg U, false);
wenzelm@24092
   279
wenzelm@24092
   280
fun decomp_typecheck (thy, discrete, inj_consts) (T : typ, xxx) : decompT option =
wenzelm@24092
   281
  case T of
wenzelm@24092
   282
    Type ("fun", [U, _]) =>
wenzelm@24092
   283
      (case allows_lin_arith thy discrete U of
wenzelm@24092
   284
        (true, d) =>
wenzelm@24092
   285
          (case decomp0 inj_consts xxx of
wenzelm@24092
   286
            NONE                   => NONE
wenzelm@24092
   287
          | SOME (p, i, rel, q, j) => SOME (p, i, rel, q, j, d))
wenzelm@24092
   288
      | (false, _) =>
wenzelm@24092
   289
          NONE)
wenzelm@24092
   290
  | _ => NONE;
wenzelm@24092
   291
wenzelm@24092
   292
fun negate (SOME (x, i, rel, y, j, d)) = SOME (x, i, "~" ^ rel, y, j, d)
wenzelm@24092
   293
  | negate NONE                        = NONE;
wenzelm@24092
   294
wenzelm@24092
   295
fun decomp_negation data
wenzelm@24092
   296
  ((Const ("Trueprop", _)) $ (Const (rel, T) $ lhs $ rhs)) : decompT option =
wenzelm@24092
   297
      decomp_typecheck data (T, (rel, lhs, rhs))
wenzelm@24092
   298
  | decomp_negation data ((Const ("Trueprop", _)) $
wenzelm@24092
   299
  (Const ("Not", _) $ (Const (rel, T) $ lhs $ rhs))) =
wenzelm@24092
   300
      negate (decomp_typecheck data (T, (rel, lhs, rhs)))
wenzelm@24092
   301
  | decomp_negation data _ =
wenzelm@24092
   302
      NONE;
wenzelm@24092
   303
wenzelm@24092
   304
fun decomp ctxt : term -> decompT option =
wenzelm@24092
   305
  let
wenzelm@24092
   306
    val thy = ProofContext.theory_of ctxt
wenzelm@24092
   307
    val {discrete, inj_consts, ...} = get_arith_data ctxt
wenzelm@24092
   308
  in decomp_negation (thy, discrete, inj_consts) end;
wenzelm@24092
   309
wenzelm@24092
   310
fun domain_is_nat (_ $ (Const (_, T) $ _ $ _))                      = nT T
wenzelm@24092
   311
  | domain_is_nat (_ $ (Const ("Not", _) $ (Const (_, T) $ _ $ _))) = nT T
wenzelm@24092
   312
  | domain_is_nat _                                                 = false;
wenzelm@24092
   313
wenzelm@24092
   314
fun number_of (n, T) = HOLogic.mk_number T n;
wenzelm@24092
   315
wenzelm@24092
   316
(*---------------------------------------------------------------------------*)
wenzelm@24092
   317
(* the following code performs splitting of certain constants (e.g. min,     *)
wenzelm@24092
   318
(* max) in a linear arithmetic problem; similar to what split_tac later does *)
wenzelm@24092
   319
(* to the proof state                                                        *)
wenzelm@24092
   320
(*---------------------------------------------------------------------------*)
wenzelm@24092
   321
wenzelm@24092
   322
(* checks if splitting with 'thm' is implemented                             *)
wenzelm@24092
   323
wenzelm@24092
   324
fun is_split_thm (thm : thm) : bool =
wenzelm@24092
   325
  case concl_of thm of _ $ (_ $ (_ $ lhs) $ _) => (
wenzelm@24092
   326
    (* Trueprop $ ((op =) $ (?P $ lhs) $ rhs) *)
wenzelm@24092
   327
    case head_of lhs of
wenzelm@24092
   328
      Const (a, _) => member (op =) [@{const_name Orderings.max},
wenzelm@24092
   329
                                    @{const_name Orderings.min},
wenzelm@24092
   330
                                    @{const_name HOL.abs},
wenzelm@24092
   331
                                    @{const_name HOL.minus},
wenzelm@24092
   332
                                    "IntDef.nat",
wenzelm@24092
   333
                                    "Divides.div_class.mod",
wenzelm@24092
   334
                                    "Divides.div_class.div"] a
wenzelm@24092
   335
    | _            => (warning ("Lin. Arith.: wrong format for split rule " ^
wenzelm@24092
   336
                                 Display.string_of_thm thm);
wenzelm@24092
   337
                       false))
wenzelm@24092
   338
  | _ => (warning ("Lin. Arith.: wrong format for split rule " ^
wenzelm@24092
   339
                   Display.string_of_thm thm);
wenzelm@24092
   340
          false);
wenzelm@24092
   341
wenzelm@24092
   342
(* substitute new for occurrences of old in a term, incrementing bound       *)
wenzelm@24092
   343
(* variables as needed when substituting inside an abstraction               *)
wenzelm@24092
   344
wenzelm@24092
   345
fun subst_term ([] : (term * term) list) (t : term) = t
wenzelm@24092
   346
  | subst_term pairs                     t          =
wenzelm@24092
   347
      (case AList.lookup (op aconv) pairs t of
wenzelm@24092
   348
        SOME new =>
wenzelm@24092
   349
          new
wenzelm@24092
   350
      | NONE     =>
wenzelm@24092
   351
          (case t of Abs (a, T, body) =>
wenzelm@24092
   352
            let val pairs' = map (pairself (incr_boundvars 1)) pairs
wenzelm@24092
   353
            in  Abs (a, T, subst_term pairs' body)  end
wenzelm@24092
   354
          | t1 $ t2                   =>
wenzelm@24092
   355
            subst_term pairs t1 $ subst_term pairs t2
wenzelm@24092
   356
          | _ => t));
wenzelm@24092
   357
wenzelm@24092
   358
(* approximates the effect of one application of split_tac (followed by NNF  *)
wenzelm@24092
   359
(* normalization) on the subgoal represented by '(Ts, terms)'; returns a     *)
wenzelm@24092
   360
(* list of new subgoals (each again represented by a typ list for bound      *)
wenzelm@24092
   361
(* variables and a term list for premises), or NONE if split_tac would fail  *)
wenzelm@24092
   362
(* on the subgoal                                                            *)
wenzelm@24092
   363
wenzelm@24092
   364
(* FIXME: currently only the effect of certain split theorems is reproduced  *)
wenzelm@24092
   365
(*        (which is why we need 'is_split_thm').  A more canonical           *)
wenzelm@24092
   366
(*        implementation should analyze the right-hand side of the split     *)
wenzelm@24092
   367
(*        theorem that can be applied, and modify the subgoal accordingly.   *)
wenzelm@24092
   368
(*        Or even better, the splitter should be extended to provide         *)
wenzelm@24092
   369
(*        splitting on terms as well as splitting on theorems (where the     *)
wenzelm@24092
   370
(*        former can have a faster implementation as it does not need to be  *)
wenzelm@24092
   371
(*        proof-producing).                                                  *)
wenzelm@24092
   372
wenzelm@24092
   373
fun split_once_items ctxt (Ts : typ list, terms : term list) :
wenzelm@24092
   374
                     (typ list * term list) list option =
wenzelm@24092
   375
let
wenzelm@24092
   376
  val thy = ProofContext.theory_of ctxt
wenzelm@24092
   377
  (* takes a list  [t1, ..., tn]  to the term                                *)
wenzelm@24092
   378
  (*   tn' --> ... --> t1' --> False  ,                                      *)
wenzelm@24092
   379
  (* where ti' = HOLogic.dest_Trueprop ti                                    *)
wenzelm@24092
   380
  fun REPEAT_DETERM_etac_rev_mp terms' =
wenzelm@24092
   381
    fold (curry HOLogic.mk_imp) (map HOLogic.dest_Trueprop terms') HOLogic.false_const
wenzelm@24092
   382
  val split_thms = filter is_split_thm (#splits (get_arith_data ctxt))
wenzelm@24092
   383
  val cmap       = Splitter.cmap_of_split_thms split_thms
wenzelm@24092
   384
  val splits     = Splitter.split_posns cmap thy Ts (REPEAT_DETERM_etac_rev_mp terms)
wenzelm@24112
   385
  val split_limit = Config.get ctxt fast_arith_split_limit
wenzelm@24092
   386
in
wenzelm@24092
   387
  if length splits > split_limit then
wenzelm@24092
   388
   (tracing ("fast_arith_split_limit exceeded (current value is " ^
wenzelm@24092
   389
      string_of_int split_limit ^ ")"); NONE)
wenzelm@24092
   390
  else (
wenzelm@24092
   391
  case splits of [] =>
wenzelm@24092
   392
    (* split_tac would fail: no possible split *)
wenzelm@24092
   393
    NONE
wenzelm@24092
   394
  | ((_, _, _, split_type, split_term) :: _) => (
wenzelm@24092
   395
    (* ignore all but the first possible split *)
wenzelm@24092
   396
    case strip_comb split_term of
wenzelm@24092
   397
    (* ?P (max ?i ?j) = ((?i <= ?j --> ?P ?j) & (~ ?i <= ?j --> ?P ?i)) *)
wenzelm@24092
   398
      (Const (@{const_name Orderings.max}, _), [t1, t2]) =>
wenzelm@24092
   399
      let
wenzelm@24092
   400
        val rev_terms     = rev terms
wenzelm@24092
   401
        val terms1        = map (subst_term [(split_term, t1)]) rev_terms
wenzelm@24092
   402
        val terms2        = map (subst_term [(split_term, t2)]) rev_terms
wenzelm@24092
   403
        val t1_leq_t2     = Const (@{const_name HOL.less_eq},
wenzelm@24092
   404
                                    split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
wenzelm@24092
   405
        val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
wenzelm@24092
   406
        val not_false     = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
wenzelm@24092
   407
        val subgoal1      = (HOLogic.mk_Trueprop t1_leq_t2) :: terms2 @ [not_false]
wenzelm@24092
   408
        val subgoal2      = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms1 @ [not_false]
wenzelm@24092
   409
      in
wenzelm@24092
   410
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
wenzelm@24092
   411
      end
wenzelm@24092
   412
    (* ?P (min ?i ?j) = ((?i <= ?j --> ?P ?i) & (~ ?i <= ?j --> ?P ?j)) *)
wenzelm@24092
   413
    | (Const (@{const_name Orderings.min}, _), [t1, t2]) =>
wenzelm@24092
   414
      let
wenzelm@24092
   415
        val rev_terms     = rev terms
wenzelm@24092
   416
        val terms1        = map (subst_term [(split_term, t1)]) rev_terms
wenzelm@24092
   417
        val terms2        = map (subst_term [(split_term, t2)]) rev_terms
wenzelm@24092
   418
        val t1_leq_t2     = Const (@{const_name HOL.less_eq},
wenzelm@24092
   419
                                    split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
wenzelm@24092
   420
        val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
wenzelm@24092
   421
        val not_false     = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
wenzelm@24092
   422
        val subgoal1      = (HOLogic.mk_Trueprop t1_leq_t2) :: terms1 @ [not_false]
wenzelm@24092
   423
        val subgoal2      = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms2 @ [not_false]
wenzelm@24092
   424
      in
wenzelm@24092
   425
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
wenzelm@24092
   426
      end
wenzelm@24092
   427
    (* ?P (abs ?a) = ((0 <= ?a --> ?P ?a) & (?a < 0 --> ?P (- ?a))) *)
wenzelm@24092
   428
    | (Const (@{const_name HOL.abs}, _), [t1]) =>
wenzelm@24092
   429
      let
wenzelm@24092
   430
        val rev_terms   = rev terms
wenzelm@24092
   431
        val terms1      = map (subst_term [(split_term, t1)]) rev_terms
wenzelm@24092
   432
        val terms2      = map (subst_term [(split_term, Const (@{const_name HOL.uminus},
wenzelm@24092
   433
                            split_type --> split_type) $ t1)]) rev_terms
wenzelm@24092
   434
        val zero        = Const (@{const_name HOL.zero}, split_type)
wenzelm@24092
   435
        val zero_leq_t1 = Const (@{const_name HOL.less_eq},
wenzelm@24092
   436
                            split_type --> split_type --> HOLogic.boolT) $ zero $ t1
wenzelm@24092
   437
        val t1_lt_zero  = Const (@{const_name HOL.less},
wenzelm@24092
   438
                            split_type --> split_type --> HOLogic.boolT) $ t1 $ zero
wenzelm@24092
   439
        val not_false   = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
wenzelm@24092
   440
        val subgoal1    = (HOLogic.mk_Trueprop zero_leq_t1) :: terms1 @ [not_false]
wenzelm@24092
   441
        val subgoal2    = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
wenzelm@24092
   442
      in
wenzelm@24092
   443
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
wenzelm@24092
   444
      end
wenzelm@24092
   445
    (* ?P (?a - ?b) = ((?a < ?b --> ?P 0) & (ALL d. ?a = ?b + d --> ?P d)) *)
wenzelm@24092
   446
    | (Const (@{const_name HOL.minus}, _), [t1, t2]) =>
wenzelm@24092
   447
      let
wenzelm@24092
   448
        (* "d" in the above theorem becomes a new bound variable after NNF   *)
wenzelm@24092
   449
        (* transformation, therefore some adjustment of indices is necessary *)
wenzelm@24092
   450
        val rev_terms       = rev terms
wenzelm@24092
   451
        val zero            = Const (@{const_name HOL.zero}, split_type)
wenzelm@24092
   452
        val d               = Bound 0
wenzelm@24092
   453
        val terms1          = map (subst_term [(split_term, zero)]) rev_terms
wenzelm@24092
   454
        val terms2          = map (subst_term [(incr_boundvars 1 split_term, d)])
wenzelm@24092
   455
                                (map (incr_boundvars 1) rev_terms)
wenzelm@24092
   456
        val t1'             = incr_boundvars 1 t1
wenzelm@24092
   457
        val t2'             = incr_boundvars 1 t2
wenzelm@24092
   458
        val t1_lt_t2        = Const (@{const_name HOL.less},
wenzelm@24092
   459
                                split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
wenzelm@24092
   460
        val t1_eq_t2_plus_d = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
wenzelm@24092
   461
                                (Const (@{const_name HOL.plus},
wenzelm@24092
   462
                                  split_type --> split_type --> split_type) $ t2' $ d)
wenzelm@24092
   463
        val not_false       = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
wenzelm@24092
   464
        val subgoal1        = (HOLogic.mk_Trueprop t1_lt_t2) :: terms1 @ [not_false]
wenzelm@24092
   465
        val subgoal2        = (HOLogic.mk_Trueprop t1_eq_t2_plus_d) :: terms2 @ [not_false]
wenzelm@24092
   466
      in
wenzelm@24092
   467
        SOME [(Ts, subgoal1), (split_type :: Ts, subgoal2)]
wenzelm@24092
   468
      end
wenzelm@24092
   469
    (* ?P (nat ?i) = ((ALL n. ?i = int n --> ?P n) & (?i < 0 --> ?P 0)) *)
wenzelm@24092
   470
    | (Const ("IntDef.nat", _), [t1]) =>
wenzelm@24092
   471
      let
wenzelm@24092
   472
        val rev_terms   = rev terms
wenzelm@24092
   473
        val zero_int    = Const (@{const_name HOL.zero}, HOLogic.intT)
wenzelm@24092
   474
        val zero_nat    = Const (@{const_name HOL.zero}, HOLogic.natT)
wenzelm@24092
   475
        val n           = Bound 0
wenzelm@24092
   476
        val terms1      = map (subst_term [(incr_boundvars 1 split_term, n)])
wenzelm@24092
   477
                            (map (incr_boundvars 1) rev_terms)
wenzelm@24092
   478
        val terms2      = map (subst_term [(split_term, zero_nat)]) rev_terms
wenzelm@24092
   479
        val t1'         = incr_boundvars 1 t1
wenzelm@24092
   480
        val t1_eq_int_n = Const ("op =", HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1' $
haftmann@24196
   481
                            (Const (@{const_name of_nat}, HOLogic.natT --> HOLogic.intT) $ n)
wenzelm@24092
   482
        val t1_lt_zero  = Const (@{const_name HOL.less},
wenzelm@24092
   483
                            HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1 $ zero_int
wenzelm@24092
   484
        val not_false   = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
wenzelm@24092
   485
        val subgoal1    = (HOLogic.mk_Trueprop t1_eq_int_n) :: terms1 @ [not_false]
wenzelm@24092
   486
        val subgoal2    = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
wenzelm@24092
   487
      in
wenzelm@24092
   488
        SOME [(HOLogic.natT :: Ts, subgoal1), (Ts, subgoal2)]
wenzelm@24092
   489
      end
wenzelm@24092
   490
    (* "?P ((?n::nat) mod (number_of ?k)) =
wenzelm@24092
   491
         ((number_of ?k = 0 --> ?P ?n) & (~ (number_of ?k = 0) -->
wenzelm@24092
   492
           (ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P j))) *)
wenzelm@24092
   493
    | (Const ("Divides.div_class.mod", Type ("fun", [Type ("nat", []), _])), [t1, t2]) =>
wenzelm@24092
   494
      let
wenzelm@24092
   495
        val rev_terms               = rev terms
wenzelm@24092
   496
        val zero                    = Const (@{const_name HOL.zero}, split_type)
wenzelm@24092
   497
        val i                       = Bound 1
wenzelm@24092
   498
        val j                       = Bound 0
wenzelm@24092
   499
        val terms1                  = map (subst_term [(split_term, t1)]) rev_terms
wenzelm@24092
   500
        val terms2                  = map (subst_term [(incr_boundvars 2 split_term, j)])
wenzelm@24092
   501
                                        (map (incr_boundvars 2) rev_terms)
wenzelm@24092
   502
        val t1'                     = incr_boundvars 2 t1
wenzelm@24092
   503
        val t2'                     = incr_boundvars 2 t2
wenzelm@24092
   504
        val t2_eq_zero              = Const ("op =",
wenzelm@24092
   505
                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
wenzelm@24092
   506
        val t2_neq_zero             = HOLogic.mk_not (Const ("op =",
wenzelm@24092
   507
                                        split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
wenzelm@24092
   508
        val j_lt_t2                 = Const (@{const_name HOL.less},
wenzelm@24092
   509
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
wenzelm@24092
   510
        val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
wenzelm@24092
   511
                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
wenzelm@24092
   512
                                         (Const (@{const_name HOL.times},
wenzelm@24092
   513
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
wenzelm@24092
   514
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
wenzelm@24092
   515
        val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
wenzelm@24092
   516
        val subgoal2                = (map HOLogic.mk_Trueprop
wenzelm@24092
   517
                                        [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
wenzelm@24092
   518
                                          @ terms2 @ [not_false]
wenzelm@24092
   519
      in
wenzelm@24092
   520
        SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
wenzelm@24092
   521
      end
wenzelm@24092
   522
    (* "?P ((?n::nat) div (number_of ?k)) =
wenzelm@24092
   523
         ((number_of ?k = 0 --> ?P 0) & (~ (number_of ?k = 0) -->
wenzelm@24092
   524
           (ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P i))) *)
wenzelm@24092
   525
    | (Const ("Divides.div_class.div", Type ("fun", [Type ("nat", []), _])), [t1, t2]) =>
wenzelm@24092
   526
      let
wenzelm@24092
   527
        val rev_terms               = rev terms
wenzelm@24092
   528
        val zero                    = Const (@{const_name HOL.zero}, split_type)
wenzelm@24092
   529
        val i                       = Bound 1
wenzelm@24092
   530
        val j                       = Bound 0
wenzelm@24092
   531
        val terms1                  = map (subst_term [(split_term, zero)]) rev_terms
wenzelm@24092
   532
        val terms2                  = map (subst_term [(incr_boundvars 2 split_term, i)])
wenzelm@24092
   533
                                        (map (incr_boundvars 2) rev_terms)
wenzelm@24092
   534
        val t1'                     = incr_boundvars 2 t1
wenzelm@24092
   535
        val t2'                     = incr_boundvars 2 t2
wenzelm@24092
   536
        val t2_eq_zero              = Const ("op =",
wenzelm@24092
   537
                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
wenzelm@24092
   538
        val t2_neq_zero             = HOLogic.mk_not (Const ("op =",
wenzelm@24092
   539
                                        split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
wenzelm@24092
   540
        val j_lt_t2                 = Const (@{const_name HOL.less},
wenzelm@24092
   541
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
wenzelm@24092
   542
        val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
wenzelm@24092
   543
                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
wenzelm@24092
   544
                                         (Const (@{const_name HOL.times},
wenzelm@24092
   545
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
wenzelm@24092
   546
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
wenzelm@24092
   547
        val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
wenzelm@24092
   548
        val subgoal2                = (map HOLogic.mk_Trueprop
wenzelm@24092
   549
                                        [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
wenzelm@24092
   550
                                          @ terms2 @ [not_false]
wenzelm@24092
   551
      in
wenzelm@24092
   552
        SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
wenzelm@24092
   553
      end
wenzelm@24092
   554
    (* "?P ((?n::int) mod (number_of ?k)) =
wenzelm@24092
   555
         ((iszero (number_of ?k) --> ?P ?n) &
wenzelm@24092
   556
          (neg (number_of (uminus ?k)) -->
wenzelm@24092
   557
            (ALL i j. 0 <= j & j < number_of ?k & ?n = number_of ?k * i + j --> ?P j)) &
wenzelm@24092
   558
          (neg (number_of ?k) -->
wenzelm@24092
   559
            (ALL i j. number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j --> ?P j))) *)
wenzelm@24092
   560
    | (Const ("Divides.div_class.mod",
wenzelm@24092
   561
        Type ("fun", [Type ("IntDef.int", []), _])), [t1, t2 as (number_of $ k)]) =>
wenzelm@24092
   562
      let
wenzelm@24092
   563
        val rev_terms               = rev terms
wenzelm@24092
   564
        val zero                    = Const (@{const_name HOL.zero}, split_type)
wenzelm@24092
   565
        val i                       = Bound 1
wenzelm@24092
   566
        val j                       = Bound 0
wenzelm@24092
   567
        val terms1                  = map (subst_term [(split_term, t1)]) rev_terms
wenzelm@24092
   568
        val terms2_3                = map (subst_term [(incr_boundvars 2 split_term, j)])
wenzelm@24092
   569
                                        (map (incr_boundvars 2) rev_terms)
wenzelm@24092
   570
        val t1'                     = incr_boundvars 2 t1
wenzelm@24092
   571
        val (t2' as (_ $ k'))       = incr_boundvars 2 t2
wenzelm@24092
   572
        val iszero_t2               = Const ("IntDef.iszero", split_type --> HOLogic.boolT) $ t2
wenzelm@24092
   573
        val neg_minus_k             = Const ("IntDef.neg", split_type --> HOLogic.boolT) $
wenzelm@24092
   574
                                        (number_of $
wenzelm@24092
   575
                                          (Const (@{const_name HOL.uminus},
wenzelm@24092
   576
                                            HOLogic.intT --> HOLogic.intT) $ k'))
wenzelm@24092
   577
        val zero_leq_j              = Const (@{const_name HOL.less_eq},
wenzelm@24092
   578
                                        split_type --> split_type --> HOLogic.boolT) $ zero $ j
wenzelm@24092
   579
        val j_lt_t2                 = Const (@{const_name HOL.less},
wenzelm@24092
   580
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
wenzelm@24092
   581
        val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
wenzelm@24092
   582
                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
wenzelm@24092
   583
                                         (Const (@{const_name HOL.times},
wenzelm@24092
   584
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
wenzelm@24092
   585
        val neg_t2                  = Const ("IntDef.neg", split_type --> HOLogic.boolT) $ t2'
wenzelm@24092
   586
        val t2_lt_j                 = Const (@{const_name HOL.less},
wenzelm@24092
   587
                                        split_type --> split_type--> HOLogic.boolT) $ t2' $ j
wenzelm@24092
   588
        val j_leq_zero              = Const (@{const_name HOL.less_eq},
wenzelm@24092
   589
                                        split_type --> split_type --> HOLogic.boolT) $ j $ zero
wenzelm@24092
   590
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
wenzelm@24092
   591
        val subgoal1                = (HOLogic.mk_Trueprop iszero_t2) :: terms1 @ [not_false]
wenzelm@24092
   592
        val subgoal2                = (map HOLogic.mk_Trueprop [neg_minus_k, zero_leq_j])
wenzelm@24092
   593
                                        @ hd terms2_3
wenzelm@24092
   594
                                        :: (if tl terms2_3 = [] then [not_false] else [])
wenzelm@24092
   595
                                        @ (map HOLogic.mk_Trueprop [j_lt_t2, t1_eq_t2_times_i_plus_j])
wenzelm@24092
   596
                                        @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
wenzelm@24092
   597
        val subgoal3                = (map HOLogic.mk_Trueprop [neg_t2, t2_lt_j])
wenzelm@24092
   598
                                        @ hd terms2_3
wenzelm@24092
   599
                                        :: (if tl terms2_3 = [] then [not_false] else [])
wenzelm@24092
   600
                                        @ (map HOLogic.mk_Trueprop [j_leq_zero, t1_eq_t2_times_i_plus_j])
wenzelm@24092
   601
                                        @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
wenzelm@24092
   602
        val Ts'                     = split_type :: split_type :: Ts
wenzelm@24092
   603
      in
wenzelm@24092
   604
        SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
wenzelm@24092
   605
      end
wenzelm@24092
   606
    (* "?P ((?n::int) div (number_of ?k)) =
wenzelm@24092
   607
         ((iszero (number_of ?k) --> ?P 0) &
wenzelm@24092
   608
          (neg (number_of (uminus ?k)) -->
wenzelm@24092
   609
            (ALL i. (EX j. 0 <= j & j < number_of ?k & ?n = number_of ?k * i + j) --> ?P i)) &
wenzelm@24092
   610
          (neg (number_of ?k) -->
wenzelm@24092
   611
            (ALL i. (EX j. number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j) --> ?P i))) *)
wenzelm@24092
   612
    | (Const ("Divides.div_class.div",
wenzelm@24092
   613
        Type ("fun", [Type ("IntDef.int", []), _])), [t1, t2 as (number_of $ k)]) =>
wenzelm@24092
   614
      let
wenzelm@24092
   615
        val rev_terms               = rev terms
wenzelm@24092
   616
        val zero                    = Const (@{const_name HOL.zero}, split_type)
wenzelm@24092
   617
        val i                       = Bound 1
wenzelm@24092
   618
        val j                       = Bound 0
wenzelm@24092
   619
        val terms1                  = map (subst_term [(split_term, zero)]) rev_terms
wenzelm@24092
   620
        val terms2_3                = map (subst_term [(incr_boundvars 2 split_term, i)])
wenzelm@24092
   621
                                        (map (incr_boundvars 2) rev_terms)
wenzelm@24092
   622
        val t1'                     = incr_boundvars 2 t1
wenzelm@24092
   623
        val (t2' as (_ $ k'))       = incr_boundvars 2 t2
wenzelm@24092
   624
        val iszero_t2               = Const ("IntDef.iszero", split_type --> HOLogic.boolT) $ t2
wenzelm@24092
   625
        val neg_minus_k             = Const ("IntDef.neg", split_type --> HOLogic.boolT) $
wenzelm@24092
   626
                                        (number_of $
wenzelm@24092
   627
                                          (Const (@{const_name HOL.uminus},
wenzelm@24092
   628
                                            HOLogic.intT --> HOLogic.intT) $ k'))
wenzelm@24092
   629
        val zero_leq_j              = Const (@{const_name HOL.less_eq},
wenzelm@24092
   630
                                        split_type --> split_type --> HOLogic.boolT) $ zero $ j
wenzelm@24092
   631
        val j_lt_t2                 = Const (@{const_name HOL.less},
wenzelm@24092
   632
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
wenzelm@24092
   633
        val t1_eq_t2_times_i_plus_j = Const ("op =",
wenzelm@24092
   634
                                        split_type --> split_type --> HOLogic.boolT) $ t1' $
wenzelm@24092
   635
                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
wenzelm@24092
   636
                                         (Const (@{const_name HOL.times},
wenzelm@24092
   637
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
wenzelm@24092
   638
        val neg_t2                  = Const ("IntDef.neg", split_type --> HOLogic.boolT) $ t2'
wenzelm@24092
   639
        val t2_lt_j                 = Const (@{const_name HOL.less},
wenzelm@24092
   640
                                        split_type --> split_type--> HOLogic.boolT) $ t2' $ j
wenzelm@24092
   641
        val j_leq_zero              = Const (@{const_name HOL.less_eq},
wenzelm@24092
   642
                                        split_type --> split_type --> HOLogic.boolT) $ j $ zero
wenzelm@24092
   643
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
wenzelm@24092
   644
        val subgoal1                = (HOLogic.mk_Trueprop iszero_t2) :: terms1 @ [not_false]
wenzelm@24092
   645
        val subgoal2                = (HOLogic.mk_Trueprop neg_minus_k)
wenzelm@24092
   646
                                        :: terms2_3
wenzelm@24092
   647
                                        @ not_false
wenzelm@24092
   648
                                        :: (map HOLogic.mk_Trueprop
wenzelm@24092
   649
                                             [zero_leq_j, j_lt_t2, t1_eq_t2_times_i_plus_j])
wenzelm@24092
   650
        val subgoal3                = (HOLogic.mk_Trueprop neg_t2)
wenzelm@24092
   651
                                        :: terms2_3
wenzelm@24092
   652
                                        @ not_false
wenzelm@24092
   653
                                        :: (map HOLogic.mk_Trueprop
wenzelm@24092
   654
                                             [t2_lt_j, j_leq_zero, t1_eq_t2_times_i_plus_j])
wenzelm@24092
   655
        val Ts'                     = split_type :: split_type :: Ts
wenzelm@24092
   656
      in
wenzelm@24092
   657
        SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
wenzelm@24092
   658
      end
wenzelm@24092
   659
    (* this will only happen if a split theorem can be applied for which no  *)
wenzelm@24092
   660
    (* code exists above -- in which case either the split theorem should be *)
wenzelm@24092
   661
    (* implemented above, or 'is_split_thm' should be modified to filter it  *)
wenzelm@24092
   662
    (* out                                                                   *)
wenzelm@24092
   663
    | (t, ts) => (
wenzelm@24092
   664
      warning ("Lin. Arith.: split rule for " ^ ProofContext.string_of_term ctxt t ^
wenzelm@24092
   665
               " (with " ^ string_of_int (length ts) ^
wenzelm@24092
   666
               " argument(s)) not implemented; proof reconstruction is likely to fail");
wenzelm@24092
   667
      NONE
wenzelm@24092
   668
    ))
wenzelm@24092
   669
  )
wenzelm@24092
   670
end;
wenzelm@24092
   671
wenzelm@24092
   672
(* remove terms that do not satisfy 'p'; change the order of the remaining   *)
wenzelm@24092
   673
(* terms in the same way as filter_prems_tac does                            *)
wenzelm@24092
   674
wenzelm@24092
   675
fun filter_prems_tac_items (p : term -> bool) (terms : term list) : term list =
wenzelm@24092
   676
let
wenzelm@24092
   677
  fun filter_prems (t, (left, right)) =
wenzelm@24092
   678
    if  p t  then  (left, right @ [t])  else  (left @ right, [])
wenzelm@24092
   679
  val (left, right) = foldl filter_prems ([], []) terms
wenzelm@24092
   680
in
wenzelm@24092
   681
  right @ left
wenzelm@24092
   682
end;
wenzelm@24092
   683
wenzelm@24092
   684
(* return true iff TRY (etac notE) THEN eq_assume_tac would succeed on a     *)
wenzelm@24092
   685
(* subgoal that has 'terms' as premises                                      *)
wenzelm@24092
   686
wenzelm@24092
   687
fun negated_term_occurs_positively (terms : term list) : bool =
wenzelm@24092
   688
  List.exists
wenzelm@24092
   689
    (fn (Trueprop $ (Const ("Not", _) $ t)) => member (op aconv) terms (Trueprop $ t)
wenzelm@24092
   690
      | _                                   => false)
wenzelm@24092
   691
    terms;
wenzelm@24092
   692
wenzelm@24092
   693
fun pre_decomp ctxt (Ts : typ list, terms : term list) : (typ list * term list) list =
wenzelm@24092
   694
let
wenzelm@24092
   695
  (* repeatedly split (including newly emerging subgoals) until no further   *)
wenzelm@24092
   696
  (* splitting is possible                                                   *)
wenzelm@24092
   697
  fun split_loop ([] : (typ list * term list) list) = ([] : (typ list * term list) list)
wenzelm@24092
   698
    | split_loop (subgoal::subgoals)                = (
wenzelm@24092
   699
        case split_once_items ctxt subgoal of
wenzelm@24092
   700
          SOME new_subgoals => split_loop (new_subgoals @ subgoals)
wenzelm@24092
   701
        | NONE              => subgoal :: split_loop subgoals
wenzelm@24092
   702
      )
wenzelm@24092
   703
  fun is_relevant t  = isSome (decomp ctxt t)
wenzelm@24092
   704
  (* filter_prems_tac is_relevant: *)
wenzelm@24092
   705
  val relevant_terms = filter_prems_tac_items is_relevant terms
wenzelm@24092
   706
  (* split_tac, NNF normalization: *)
wenzelm@24092
   707
  val split_goals    = split_loop [(Ts, relevant_terms)]
wenzelm@24092
   708
  (* necessary because split_once_tac may normalize terms: *)
wenzelm@24092
   709
  val beta_eta_norm  = map (apsnd (map (Envir.eta_contract o Envir.beta_norm))) split_goals
wenzelm@24092
   710
  (* TRY (etac notE) THEN eq_assume_tac: *)
wenzelm@24092
   711
  val result         = List.filter (not o negated_term_occurs_positively o snd) beta_eta_norm
wenzelm@24092
   712
in
wenzelm@24092
   713
  result
wenzelm@24092
   714
end;
wenzelm@24092
   715
wenzelm@24092
   716
(* takes the i-th subgoal  [| A1; ...; An |] ==> B  to                       *)
wenzelm@24092
   717
(* An --> ... --> A1 --> B,  performs splitting with the given 'split_thms'  *)
wenzelm@24092
   718
(* (resulting in a different subgoal P), takes  P  to  ~P ==> False,         *)
wenzelm@24092
   719
(* performs NNF-normalization of ~P, and eliminates conjunctions,            *)
wenzelm@24092
   720
(* disjunctions and existential quantifiers from the premises, possibly (in  *)
wenzelm@24092
   721
(* the case of disjunctions) resulting in several new subgoals, each of the  *)
wenzelm@24092
   722
(* general form  [| Q1; ...; Qm |] ==> False.  Fails if more than            *)
wenzelm@24092
   723
(* !fast_arith_split_limit splits are possible.                              *)
wenzelm@24092
   724
wenzelm@24092
   725
local
wenzelm@24092
   726
  val nnf_simpset =
wenzelm@24092
   727
    empty_ss setmkeqTrue mk_eq_True
wenzelm@24092
   728
    setmksimps (mksimps mksimps_pairs)
wenzelm@24092
   729
    addsimps [imp_conv_disj, iff_conv_conj_imp, de_Morgan_disj, de_Morgan_conj,
wenzelm@24092
   730
      not_all, not_ex, not_not]
wenzelm@24092
   731
  fun prem_nnf_tac i st =
wenzelm@24092
   732
    full_simp_tac (Simplifier.theory_context (Thm.theory_of_thm st) nnf_simpset) i st
wenzelm@24092
   733
in
wenzelm@24092
   734
wenzelm@24092
   735
fun split_once_tac ctxt split_thms =
wenzelm@24092
   736
  let
wenzelm@24092
   737
    val thy = ProofContext.theory_of ctxt
wenzelm@24092
   738
    val cond_split_tac = SUBGOAL (fn (subgoal, i) =>
wenzelm@24092
   739
      let
wenzelm@24092
   740
        val Ts = rev (map snd (Logic.strip_params subgoal))
wenzelm@24092
   741
        val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl subgoal)
wenzelm@24092
   742
        val cmap = Splitter.cmap_of_split_thms split_thms
wenzelm@24092
   743
        val splits = Splitter.split_posns cmap thy Ts concl
wenzelm@24112
   744
        val split_limit = Config.get ctxt fast_arith_split_limit
wenzelm@24092
   745
      in
wenzelm@24092
   746
        if length splits > split_limit then no_tac
wenzelm@24092
   747
        else split_tac split_thms i
wenzelm@24092
   748
      end)
wenzelm@24092
   749
  in
wenzelm@24092
   750
    EVERY' [
wenzelm@24092
   751
      REPEAT_DETERM o etac rev_mp,
wenzelm@24092
   752
      cond_split_tac,
wenzelm@24092
   753
      rtac ccontr,
wenzelm@24092
   754
      prem_nnf_tac,
wenzelm@24092
   755
      TRY o REPEAT_ALL_NEW (DETERM o (eresolve_tac [conjE, exE] ORELSE' etac disjE))
wenzelm@24092
   756
    ]
wenzelm@24092
   757
  end;
wenzelm@24092
   758
wenzelm@24092
   759
end;  (* local *)
wenzelm@24092
   760
wenzelm@24092
   761
(* remove irrelevant premises, then split the i-th subgoal (and all new      *)
wenzelm@24092
   762
(* subgoals) by using 'split_once_tac' repeatedly.  Beta-eta-normalize new   *)
wenzelm@24092
   763
(* subgoals and finally attempt to solve them by finding an immediate        *)
wenzelm@24092
   764
(* contradiction (i.e. a term and its negation) in their premises.           *)
wenzelm@24092
   765
wenzelm@24092
   766
fun pre_tac ctxt i =
wenzelm@24092
   767
let
wenzelm@24092
   768
  val split_thms = filter is_split_thm (#splits (get_arith_data ctxt))
wenzelm@24092
   769
  fun is_relevant t = isSome (decomp ctxt t)
wenzelm@24092
   770
in
wenzelm@24092
   771
  DETERM (
wenzelm@24092
   772
    TRY (filter_prems_tac is_relevant i)
wenzelm@24092
   773
      THEN (
wenzelm@24092
   774
        (TRY o REPEAT_ALL_NEW (split_once_tac ctxt split_thms))
wenzelm@24092
   775
          THEN_ALL_NEW
wenzelm@24092
   776
            (CONVERSION Drule.beta_eta_conversion
wenzelm@24092
   777
              THEN'
wenzelm@24092
   778
            (TRY o (etac notE THEN' eq_assume_tac)))
wenzelm@24092
   779
      ) i
wenzelm@24092
   780
  )
wenzelm@24092
   781
end;
wenzelm@24092
   782
wenzelm@24092
   783
end;  (* LA_Data_Ref *)
wenzelm@24092
   784
wenzelm@24092
   785
wenzelm@24092
   786
val lin_arith_pre_tac = LA_Data_Ref.pre_tac;
wenzelm@24092
   787
wenzelm@24092
   788
structure Fast_Arith =
wenzelm@24092
   789
  Fast_Lin_Arith(structure LA_Logic=LA_Logic and LA_Data=LA_Data_Ref);
wenzelm@24092
   790
wenzelm@24092
   791
val map_data = Fast_Arith.map_data;
wenzelm@24092
   792
wenzelm@24092
   793
fun fast_arith_tac ctxt    = Fast_Arith.lin_arith_tac ctxt false;
wenzelm@24092
   794
val fast_ex_arith_tac      = Fast_Arith.lin_arith_tac;
wenzelm@24092
   795
val trace_arith            = Fast_Arith.trace;
wenzelm@24092
   796
wenzelm@24092
   797
(* reduce contradictory <= to False.
wenzelm@24092
   798
   Most of the work is done by the cancel tactics. *)
wenzelm@24092
   799
wenzelm@24092
   800
val init_arith_data =
wenzelm@24092
   801
 Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, ...} =>
wenzelm@24092
   802
   {add_mono_thms = add_mono_thms @
wenzelm@24092
   803
    @{thms add_mono_thms_ordered_semiring} @ @{thms add_mono_thms_ordered_field},
wenzelm@24092
   804
    mult_mono_thms = mult_mono_thms,
wenzelm@24092
   805
    inj_thms = inj_thms,
wenzelm@24092
   806
    lessD = lessD @ [thm "Suc_leI"],
wenzelm@24092
   807
    neqE = [@{thm linorder_neqE_nat}, @{thm linorder_neqE_ordered_idom}],
wenzelm@24092
   808
    simpset = HOL_basic_ss
wenzelm@24092
   809
      addsimps
wenzelm@24092
   810
       [@{thm "monoid_add_class.zero_plus.add_0_left"},
wenzelm@24092
   811
        @{thm "monoid_add_class.zero_plus.add_0_right"},
wenzelm@24092
   812
        @{thm "Zero_not_Suc"}, @{thm "Suc_not_Zero"}, @{thm "le_0_eq"}, @{thm "One_nat_def"},
wenzelm@24092
   813
        @{thm "order_less_irrefl"}, @{thm "zero_neq_one"}, @{thm "zero_less_one"},
wenzelm@24092
   814
        @{thm "zero_le_one"}, @{thm "zero_neq_one"} RS not_sym, @{thm "not_one_le_zero"},
wenzelm@24092
   815
        @{thm "not_one_less_zero"}]
wenzelm@24092
   816
      addsimprocs [ab_group_add_cancel.sum_conv, ab_group_add_cancel.rel_conv]
wenzelm@24092
   817
       (*abel_cancel helps it work in abstract algebraic domains*)
wenzelm@24092
   818
      addsimprocs nat_cancel_sums_add}) #>
wenzelm@24092
   819
  arith_discrete "nat";
wenzelm@24092
   820
wenzelm@24092
   821
val lin_arith_simproc = Fast_Arith.lin_arith_simproc;
wenzelm@24092
   822
wenzelm@24092
   823
val fast_nat_arith_simproc =
wenzelm@24092
   824
  Simplifier.simproc (the_context ()) "fast_nat_arith"
wenzelm@24092
   825
    ["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"] (K Fast_Arith.lin_arith_simproc);
wenzelm@24092
   826
wenzelm@24092
   827
(* Because of fast_nat_arith_simproc, the arithmetic solver is really only
wenzelm@24092
   828
useful to detect inconsistencies among the premises for subgoals which are
wenzelm@24092
   829
*not* themselves (in)equalities, because the latter activate
wenzelm@24092
   830
fast_nat_arith_simproc anyway. However, it seems cheaper to activate the
wenzelm@24092
   831
solver all the time rather than add the additional check. *)
wenzelm@24092
   832
wenzelm@24092
   833
wenzelm@24092
   834
(* arith proof method *)
wenzelm@24092
   835
wenzelm@24092
   836
local
wenzelm@24092
   837
wenzelm@24092
   838
fun raw_arith_tac ctxt ex =
wenzelm@24092
   839
  (* FIXME: K true should be replaced by a sensible test (perhaps "isSome o
wenzelm@24092
   840
     decomp sg"? -- but note that the test is applied to terms already before
wenzelm@24092
   841
     they are split/normalized) to speed things up in case there are lots of
wenzelm@24092
   842
     irrelevant terms involved; elimination of min/max can be optimized:
wenzelm@24092
   843
     (max m n + k <= r) = (m+k <= r & n+k <= r)
wenzelm@24092
   844
     (l <= min m n + k) = (l <= m+k & l <= n+k)
wenzelm@24092
   845
  *)
wenzelm@24092
   846
  refute_tac (K true)
wenzelm@24092
   847
    (* Splitting is also done inside fast_arith_tac, but not completely --   *)
wenzelm@24092
   848
    (* split_tac may use split theorems that have not been implemented in    *)
wenzelm@24092
   849
    (* fast_arith_tac (cf. pre_decomp and split_once_items above), and       *)
wenzelm@24092
   850
    (* fast_arith_split_limit may trigger.                                   *)
wenzelm@24092
   851
    (* Therefore splitting outside of fast_arith_tac may allow us to prove   *)
wenzelm@24092
   852
    (* some goals that fast_arith_tac alone would fail on.                   *)
wenzelm@24092
   853
    (REPEAT_DETERM o split_tac (#splits (get_arith_data ctxt)))
wenzelm@24092
   854
    (fast_ex_arith_tac ctxt ex);
wenzelm@24092
   855
wenzelm@24092
   856
fun more_arith_tacs ctxt =
wenzelm@24092
   857
  let val tactics = #tactics (get_arith_data ctxt)
wenzelm@24092
   858
  in FIRST' (map (fn ArithTactic {tactic, ...} => tactic ctxt) tactics) end;
wenzelm@24092
   859
wenzelm@24092
   860
in
wenzelm@24092
   861
wenzelm@24092
   862
fun simple_arith_tac ctxt = FIRST' [fast_arith_tac ctxt,
wenzelm@24092
   863
  ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_arith_tac ctxt true];
wenzelm@24092
   864
wenzelm@24092
   865
fun arith_tac ctxt = FIRST' [fast_arith_tac ctxt,
wenzelm@24092
   866
  ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_arith_tac ctxt true,
wenzelm@24092
   867
  more_arith_tacs ctxt];
wenzelm@24092
   868
wenzelm@24092
   869
fun silent_arith_tac ctxt = FIRST' [fast_arith_tac ctxt,
wenzelm@24092
   870
  ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_arith_tac ctxt false,
wenzelm@24092
   871
  more_arith_tacs ctxt];
wenzelm@24092
   872
wenzelm@24092
   873
fun arith_method src =
wenzelm@24092
   874
  Method.syntax Args.bang_facts src
wenzelm@24092
   875
  #> (fn (prems, ctxt) => Method.METHOD (fn facts =>
wenzelm@24092
   876
      HEADGOAL (Method.insert_tac (prems @ facts) THEN' arith_tac ctxt)));
wenzelm@24092
   877
wenzelm@24092
   878
end;
wenzelm@24092
   879
wenzelm@24092
   880
wenzelm@24092
   881
(* context setup *)
wenzelm@24092
   882
wenzelm@24092
   883
val setup =
wenzelm@24092
   884
  init_arith_data #>
wenzelm@24092
   885
  Simplifier.map_ss (fn ss => ss addsimprocs [fast_nat_arith_simproc]
wenzelm@24092
   886
    addSolver (mk_solver' "lin_arith" Fast_Arith.cut_lin_arith_tac)) #>
wenzelm@24092
   887
  Context.mapping
wenzelm@24092
   888
   (setup_options #>
wenzelm@24092
   889
    Method.add_methods
wenzelm@24092
   890
      [("arith", arith_method, "decide linear arithmethic")] #>
wenzelm@24092
   891
    Attrib.add_attributes [("arith_split", Attrib.no_args arith_split_add,
wenzelm@24092
   892
      "declaration of split rules for arithmetic procedure")]) I;
wenzelm@24092
   893
wenzelm@24092
   894
end;
wenzelm@24092
   895
wenzelm@24092
   896
structure BasicLinArith: BASIC_LIN_ARITH = LinArith;
wenzelm@24092
   897
open BasicLinArith;