src/HOL/Tools/lin_arith.ML
author huffman
Sun Mar 25 20:15:39 2012 +0200 (2012-03-25)
changeset 47108 2a1953f0d20d
parent 46709 65a9b30bff00
child 48556 62a3fbf9d35b
permissions -rw-r--r--
merged fork with new numeral representation (see NEWS)
wenzelm@24092
     1
(*  Title:      HOL/Tools/lin_arith.ML
wenzelm@29288
     2
    Author:     Tjark Weber and Tobias Nipkow, TU Muenchen
wenzelm@24092
     3
wenzelm@24092
     4
HOL setup for linear arithmetic (see Provers/Arith/fast_lin_arith.ML).
wenzelm@24092
     5
*)
wenzelm@24092
     6
wenzelm@24092
     7
signature LIN_ARITH =
wenzelm@24092
     8
sig
wenzelm@35230
     9
  val pre_tac: simpset -> int -> tactic
haftmann@31101
    10
  val simple_tac: Proof.context -> int -> tactic
haftmann@31101
    11
  val tac: Proof.context -> int -> tactic
haftmann@31101
    12
  val simproc: simpset -> term -> thm option
haftmann@31100
    13
  val add_inj_thms: thm list -> Context.generic -> Context.generic
haftmann@31100
    14
  val add_lessD: thm -> Context.generic -> Context.generic
haftmann@31100
    15
  val add_simps: thm list -> Context.generic -> Context.generic
haftmann@31100
    16
  val add_simprocs: simproc list -> Context.generic -> Context.generic
haftmann@31082
    17
  val add_inj_const: string * typ -> Context.generic -> Context.generic
haftmann@31100
    18
  val add_discrete_type: string -> Context.generic -> Context.generic
wenzelm@38763
    19
  val set_number_of: (theory -> typ -> int -> cterm) -> Context.generic -> Context.generic
haftmann@31082
    20
  val setup: Context.generic -> Context.generic
haftmann@31100
    21
  val global_setup: theory -> theory
haftmann@31082
    22
  val split_limit: int Config.T
haftmann@31082
    23
  val neq_limit: int Config.T
boehmes@43607
    24
  val verbose: bool Config.T
wenzelm@44654
    25
  val trace: bool Config.T
wenzelm@24092
    26
end;
wenzelm@24092
    27
haftmann@30686
    28
structure Lin_Arith: LIN_ARITH =
wenzelm@24092
    29
struct
wenzelm@24092
    30
wenzelm@24092
    31
(* Parameters data for general linear arithmetic functor *)
wenzelm@24092
    32
wenzelm@24092
    33
structure LA_Logic: LIN_ARITH_LOGIC =
wenzelm@24092
    34
struct
wenzelm@24092
    35
wenzelm@24092
    36
val ccontr = ccontr;
wenzelm@24092
    37
val conjI = conjI;
wenzelm@24092
    38
val notI = notI;
wenzelm@24092
    39
val sym = sym;
boehmes@31510
    40
val trueI = TrueI;
wenzelm@24092
    41
val not_lessD = @{thm linorder_not_less} RS iffD1;
wenzelm@24092
    42
val not_leD = @{thm linorder_not_le} RS iffD1;
wenzelm@24092
    43
wenzelm@35410
    44
fun mk_Eq thm = thm RS @{thm Eq_FalseI} handle THM _ => thm RS @{thm Eq_TrueI};
wenzelm@24092
    45
wenzelm@24092
    46
val mk_Trueprop = HOLogic.mk_Trueprop;
wenzelm@24092
    47
wenzelm@24092
    48
fun atomize thm = case Thm.prop_of thm of
haftmann@38795
    49
    Const (@{const_name Trueprop}, _) $ (Const (@{const_name HOL.conj}, _) $ _ $ _) =>
haftmann@31100
    50
    atomize (thm RS conjunct1) @ atomize (thm RS conjunct2)
wenzelm@24092
    51
  | _ => [thm];
wenzelm@24092
    52
haftmann@38558
    53
fun neg_prop ((TP as Const(@{const_name Trueprop}, _)) $ (Const (@{const_name Not}, _) $ t)) = TP $ t
haftmann@38558
    54
  | neg_prop ((TP as Const(@{const_name Trueprop}, _)) $ t) = TP $ (HOLogic.Not $t)
wenzelm@24092
    55
  | neg_prop t = raise TERM ("neg_prop", [t]);
wenzelm@24092
    56
wenzelm@24092
    57
fun is_False thm =
wenzelm@24092
    58
  let val _ $ t = Thm.prop_of thm
wenzelm@45740
    59
  in t = @{term False} end;
wenzelm@24092
    60
haftmann@30686
    61
fun is_nat t = (fastype_of1 t = HOLogic.natT);
wenzelm@24092
    62
haftmann@31100
    63
fun mk_nat_thm thy t =
haftmann@31100
    64
  let
haftmann@31100
    65
    val cn = cterm_of thy (Var (("n", 0), HOLogic.natT))
haftmann@31100
    66
    and ct = cterm_of thy t
wenzelm@43333
    67
  in Drule.instantiate_normalize ([], [(cn, ct)]) @{thm le0} end;
wenzelm@24092
    68
wenzelm@43333
    69
end;
wenzelm@24092
    70
wenzelm@24092
    71
wenzelm@24092
    72
(* arith context data *)
wenzelm@24092
    73
wenzelm@33519
    74
structure Lin_Arith_Data = Generic_Data
wenzelm@24092
    75
(
wenzelm@24092
    76
  type T = {splits: thm list,
wenzelm@24092
    77
            inj_consts: (string * typ) list,
haftmann@30686
    78
            discrete: string list};
haftmann@30686
    79
  val empty = {splits = [], inj_consts = [], discrete = []};
wenzelm@24092
    80
  val extend = I;
wenzelm@33519
    81
  fun merge
haftmann@44946
    82
   ({splits = splits1, inj_consts = inj_consts1, discrete = discrete1},
haftmann@44946
    83
    {splits = splits2, inj_consts = inj_consts2, discrete = discrete2}) : T =
wenzelm@33520
    84
   {splits = Thm.merge_thms (splits1, splits2),
wenzelm@24092
    85
    inj_consts = Library.merge (op =) (inj_consts1, inj_consts2),
haftmann@30686
    86
    discrete = Library.merge (op =) (discrete1, discrete2)};
wenzelm@24092
    87
);
wenzelm@24092
    88
haftmann@31100
    89
val get_arith_data = Lin_Arith_Data.get o Context.Proof;
wenzelm@24092
    90
haftmann@31100
    91
fun add_split thm = Lin_Arith_Data.map (fn {splits, inj_consts, discrete} =>
haftmann@31100
    92
  {splits = update Thm.eq_thm_prop thm splits,
haftmann@31100
    93
   inj_consts = inj_consts, discrete = discrete});
wenzelm@24092
    94
haftmann@31100
    95
fun add_discrete_type d = Lin_Arith_Data.map (fn {splits, inj_consts, discrete} =>
haftmann@30686
    96
  {splits = splits, inj_consts = inj_consts,
haftmann@30686
    97
   discrete = update (op =) d discrete});
wenzelm@24092
    98
haftmann@31100
    99
fun add_inj_const c = Lin_Arith_Data.map (fn {splits, inj_consts, discrete} =>
haftmann@30686
   100
  {splits = splits, inj_consts = update (op =) c inj_consts,
haftmann@30686
   101
   discrete = discrete});
wenzelm@24092
   102
wenzelm@42616
   103
val split_limit = Attrib.setup_config_int @{binding linarith_split_limit} (K 9);
wenzelm@42616
   104
val neq_limit = Attrib.setup_config_int @{binding linarith_neq_limit} (K 9);
wenzelm@44654
   105
val verbose = Attrib.setup_config_bool @{binding linarith_verbose} (K true);
wenzelm@44654
   106
val trace = Attrib.setup_config_bool @{binding linarith_trace} (K false);
wenzelm@24092
   107
wenzelm@24092
   108
haftmann@31100
   109
structure LA_Data =
wenzelm@24092
   110
struct
wenzelm@24092
   111
wenzelm@44654
   112
val neq_limit = neq_limit;
wenzelm@44654
   113
val verbose = verbose;
wenzelm@44654
   114
val trace = trace;
wenzelm@24092
   115
wenzelm@24092
   116
wenzelm@24092
   117
(* Decomposition of terms *)
wenzelm@24092
   118
wenzelm@24092
   119
(*internal representation of linear (in-)equations*)
wenzelm@26942
   120
type decomp =
webertj@24328
   121
  ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat * bool);
wenzelm@24092
   122
wenzelm@24092
   123
fun nT (Type ("fun", [N, _])) = (N = HOLogic.natT)
wenzelm@24092
   124
  | nT _                      = false;
wenzelm@24092
   125
wenzelm@24092
   126
fun add_atom (t : term) (m : Rat.rat) (p : (term * Rat.rat) list, i : Rat.rat) :
wenzelm@24092
   127
             (term * Rat.rat) list * Rat.rat =
nipkow@29548
   128
  case AList.lookup Pattern.aeconv p t of
webertj@24328
   129
      NONE   => ((t, m) :: p, i)
nipkow@29548
   130
    | SOME n => (AList.update Pattern.aeconv (t, Rat.add n m) p, i);
wenzelm@24092
   131
webertj@24328
   132
(* decompose nested multiplications, bracketing them to the right and combining
webertj@24328
   133
   all their coefficients
wenzelm@24092
   134
webertj@24328
   135
   inj_consts: list of constants to be ignored when encountered
webertj@24328
   136
               (e.g. arithmetic type conversions that preserve value)
wenzelm@24092
   137
webertj@24328
   138
   m: multiplicity associated with the entire product
wenzelm@24092
   139
webertj@24328
   140
   returns either (SOME term, associated multiplicity) or (NONE, constant)
webertj@24328
   141
*)
wenzelm@24092
   142
fun demult (inj_consts : (string * typ) list) : term * Rat.rat -> term option * Rat.rat =
wenzelm@24092
   143
let
haftmann@35267
   144
  fun demult ((mC as Const (@{const_name Groups.times}, _)) $ s $ t, m) =
haftmann@35267
   145
      (case s of Const (@{const_name Groups.times}, _) $ s1 $ s2 =>
webertj@24328
   146
        (* bracketing to the right: '(s1 * s2) * t' becomes 's1 * (s2 * t)' *)
wenzelm@24092
   147
        demult (mC $ s1 $ (mC $ s2 $ t), m)
webertj@24328
   148
      | _ =>
webertj@24328
   149
        (* product 's * t', where either factor can be 'NONE' *)
webertj@24328
   150
        (case demult (s, m) of
webertj@24328
   151
          (SOME s', m') =>
webertj@24328
   152
            (case demult (t, m') of
webertj@24328
   153
              (SOME t', m'') => (SOME (mC $ s' $ t'), m'')
webertj@24328
   154
            | (NONE,    m'') => (SOME s', m''))
webertj@24328
   155
        | (NONE,    m') => demult (t, m')))
huffman@44064
   156
    | demult ((mC as Const (@{const_name Fields.divide}, _)) $ s $ t, m) =
webertj@24328
   157
      (* FIXME: Shouldn't we simplify nested quotients, e.g. '(s/t)/u' could
webertj@24328
   158
         become 's/(t*u)', and '(s*t)/u' could become 's*(t/u)' ?   Note that
webertj@24328
   159
         if we choose to do so here, the simpset used by arith must be able to
webertj@24328
   160
         perform the same simplifications. *)
webertj@24328
   161
      (* FIXME: Currently we treat the numerator as atomic unless the
webertj@24328
   162
         denominator can be reduced to a numeric constant.  It might be better
webertj@24328
   163
         to demult the numerator in any case, and invent a new term of the form
webertj@24328
   164
         '1 / t' if the numerator can be reduced, but the denominator cannot. *)
webertj@24328
   165
      (* FIXME: Currently we even treat the whole fraction as atomic unless the
webertj@24328
   166
         denominator can be reduced to a numeric constant.  It might be better
webertj@25015
   167
         to use the partially reduced denominator (i.e. 's / (2*t)' could be
webertj@24328
   168
         demult'ed to 's / t' with multiplicity .5).   This would require a
webertj@24328
   169
         very simple change only below, but it breaks existing proofs. *)
webertj@24328
   170
      (* quotient 's / t', where the denominator t can be NONE *)
webertj@24328
   171
      (* Note: will raise Rat.DIVZERO iff m' is Rat.zero *)
webertj@24328
   172
      (case demult (t, Rat.one) of
webertj@24328
   173
        (SOME _, _) => (SOME (mC $ s $ t), m)
webertj@24328
   174
      | (NONE,  m') => apsnd (Rat.mult (Rat.inv m')) (demult (s, m)))
webertj@24328
   175
    (* terms that evaluate to numeric constants *)
haftmann@35267
   176
    | demult (Const (@{const_name Groups.uminus}, _) $ t, m) = demult (t, Rat.neg m)
huffman@47108
   177
    | demult (Const (@{const_name Groups.zero}, _), _) = (NONE, Rat.zero)
haftmann@35267
   178
    | demult (Const (@{const_name Groups.one}, _), m) = (NONE, m)
huffman@47108
   179
    (*Warning: in rare cases (neg_)numeral encloses a non-numeral,
huffman@47108
   180
      in which case dest_num raises TERM; hence all the handles below.
webertj@24328
   181
      Same for Suc-terms that turn out not to be numerals -
webertj@24328
   182
      although the simplifier should eliminate those anyway ...*)
huffman@47108
   183
    | demult (t as Const ("Num.numeral_class.numeral", _) $ n, m) =
huffman@47108
   184
      ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_num n)))
huffman@47108
   185
        handle TERM _ => (SOME t, m))
huffman@47108
   186
    | demult (t as Const ("Num.neg_numeral_class.neg_numeral", _) $ n, m) =
huffman@47108
   187
      ((NONE, Rat.mult m (Rat.rat_of_int (~ (HOLogic.dest_num n))))
webertj@24328
   188
        handle TERM _ => (SOME t, m))
webertj@24328
   189
    | demult (t as Const (@{const_name Suc}, _) $ _, m) =
webertj@24328
   190
      ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_nat t)))
webertj@24328
   191
        handle TERM _ => (SOME t, m))
webertj@24328
   192
    (* injection constants are ignored *)
wenzelm@24092
   193
    | demult (t as Const f $ x, m) =
webertj@24328
   194
      if member (op =) inj_consts f then demult (x, m) else (SOME t, m)
webertj@24328
   195
    (* everything else is considered atomic *)
wenzelm@24092
   196
    | demult (atom, m) = (SOME atom, m)
wenzelm@24092
   197
in demult end;
wenzelm@24092
   198
wenzelm@24092
   199
fun decomp0 (inj_consts : (string * typ) list) (rel : string, lhs : term, rhs : term) :
wenzelm@24092
   200
            ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat) option =
wenzelm@24092
   201
let
webertj@24328
   202
  (* Turns a term 'all' and associated multiplicity 'm' into a list 'p' of
webertj@24328
   203
     summands and associated multiplicities, plus a constant 'i' (with implicit
webertj@24328
   204
     multiplicity 1) *)
haftmann@35267
   205
  fun poly (Const (@{const_name Groups.plus}, _) $ s $ t,
wenzelm@24092
   206
        m : Rat.rat, pi : (term * Rat.rat) list * Rat.rat) = poly (s, m, poly (t, m, pi))
haftmann@35267
   207
    | poly (all as Const (@{const_name Groups.minus}, T) $ s $ t, m, pi) =
wenzelm@24092
   208
        if nT T then add_atom all m pi else poly (s, m, poly (t, Rat.neg m, pi))
haftmann@35267
   209
    | poly (all as Const (@{const_name Groups.uminus}, T) $ t, m, pi) =
wenzelm@24092
   210
        if nT T then add_atom all m pi else poly (t, Rat.neg m, pi)
haftmann@35267
   211
    | poly (Const (@{const_name Groups.zero}, _), _, pi) =
wenzelm@24092
   212
        pi
haftmann@35267
   213
    | poly (Const (@{const_name Groups.one}, _), m, (p, i)) =
wenzelm@24092
   214
        (p, Rat.add i m)
wenzelm@24092
   215
    | poly (Const (@{const_name Suc}, _) $ t, m, (p, i)) =
wenzelm@24092
   216
        poly (t, m, (p, Rat.add i m))
haftmann@35267
   217
    | poly (all as Const (@{const_name Groups.times}, _) $ _ $ _, m, pi as (p, i)) =
wenzelm@24092
   218
        (case demult inj_consts (all, m) of
wenzelm@24092
   219
           (NONE,   m') => (p, Rat.add i m')
wenzelm@24092
   220
         | (SOME u, m') => add_atom u m' pi)
huffman@44064
   221
    | poly (all as Const (@{const_name Fields.divide}, _) $ _ $ _, m, pi as (p, i)) =
wenzelm@24092
   222
        (case demult inj_consts (all, m) of
wenzelm@24092
   223
           (NONE,   m') => (p, Rat.add i m')
wenzelm@24092
   224
         | (SOME u, m') => add_atom u m' pi)
huffman@47108
   225
    | poly (all as Const ("Num.numeral_class.numeral", Type(_,[_,_])) $ t, m, pi as (p, i)) =
huffman@47108
   226
        (let val k = HOLogic.dest_num t
huffman@47108
   227
        in (p, Rat.add i (Rat.mult m (Rat.rat_of_int k))) end
huffman@47108
   228
        handle TERM _ => add_atom all m pi)
huffman@47108
   229
    | poly (all as Const ("Num.neg_numeral_class.neg_numeral", Type(_,[_,_])) $ t, m, pi as (p, i)) =
huffman@47108
   230
        (let val k = HOLogic.dest_num t
huffman@47108
   231
        in (p, Rat.add i (Rat.mult m (Rat.rat_of_int (~ k)))) end
wenzelm@24092
   232
        handle TERM _ => add_atom all m pi)
wenzelm@24092
   233
    | poly (all as Const f $ x, m, pi) =
haftmann@36692
   234
        if member (op =) inj_consts f then poly (x, m, pi) else add_atom all m pi
wenzelm@24092
   235
    | poly (all, m, pi) =
wenzelm@24092
   236
        add_atom all m pi
wenzelm@24092
   237
  val (p, i) = poly (lhs, Rat.one, ([], Rat.zero))
wenzelm@24092
   238
  val (q, j) = poly (rhs, Rat.one, ([], Rat.zero))
wenzelm@24092
   239
in
wenzelm@24092
   240
  case rel of
haftmann@35092
   241
    @{const_name Orderings.less}    => SOME (p, i, "<", q, j)
haftmann@35092
   242
  | @{const_name Orderings.less_eq} => SOME (p, i, "<=", q, j)
haftmann@38864
   243
  | @{const_name HOL.eq}            => SOME (p, i, "=", q, j)
wenzelm@24092
   244
  | _                   => NONE
webertj@24328
   245
end handle Rat.DIVZERO => NONE;
wenzelm@24092
   246
wenzelm@24271
   247
fun of_lin_arith_sort thy U =
haftmann@35050
   248
  Sign.of_sort thy (U, @{sort Rings.linordered_idom});
wenzelm@24092
   249
haftmann@31101
   250
fun allows_lin_arith thy (discrete : string list) (U as Type (D, [])) : bool * bool =
haftmann@31101
   251
      if of_lin_arith_sort thy U then (true, member (op =) discrete D)
haftmann@31101
   252
      else if member (op =) discrete D then (true, true) else (false, false)
haftmann@31101
   253
  | allows_lin_arith sg discrete U = (of_lin_arith_sort sg U, false);
wenzelm@24092
   254
wenzelm@26942
   255
fun decomp_typecheck (thy, discrete, inj_consts) (T : typ, xxx) : decomp option =
wenzelm@24092
   256
  case T of
wenzelm@24092
   257
    Type ("fun", [U, _]) =>
wenzelm@24092
   258
      (case allows_lin_arith thy discrete U of
wenzelm@24092
   259
        (true, d) =>
wenzelm@24092
   260
          (case decomp0 inj_consts xxx of
wenzelm@24092
   261
            NONE                   => NONE
wenzelm@24092
   262
          | SOME (p, i, rel, q, j) => SOME (p, i, rel, q, j, d))
wenzelm@24092
   263
      | (false, _) =>
wenzelm@24092
   264
          NONE)
wenzelm@24092
   265
  | _ => NONE;
wenzelm@24092
   266
wenzelm@24092
   267
fun negate (SOME (x, i, rel, y, j, d)) = SOME (x, i, "~" ^ rel, y, j, d)
wenzelm@24092
   268
  | negate NONE                        = NONE;
wenzelm@24092
   269
wenzelm@24092
   270
fun decomp_negation data
haftmann@38558
   271
  ((Const (@{const_name Trueprop}, _)) $ (Const (rel, T) $ lhs $ rhs)) : decomp option =
wenzelm@24092
   272
      decomp_typecheck data (T, (rel, lhs, rhs))
haftmann@38558
   273
  | decomp_negation data ((Const (@{const_name Trueprop}, _)) $
haftmann@38558
   274
  (Const (@{const_name Not}, _) $ (Const (rel, T) $ lhs $ rhs))) =
wenzelm@24092
   275
      negate (decomp_typecheck data (T, (rel, lhs, rhs)))
wenzelm@24092
   276
  | decomp_negation data _ =
wenzelm@24092
   277
      NONE;
wenzelm@24092
   278
wenzelm@26942
   279
fun decomp ctxt : term -> decomp option =
wenzelm@24092
   280
  let
wenzelm@42361
   281
    val thy = Proof_Context.theory_of ctxt
wenzelm@24092
   282
    val {discrete, inj_consts, ...} = get_arith_data ctxt
wenzelm@24092
   283
  in decomp_negation (thy, discrete, inj_consts) end;
wenzelm@24092
   284
wenzelm@42439
   285
fun domain_is_nat (_ $ (Const (_, T) $ _ $ _)) = nT T
haftmann@38558
   286
  | domain_is_nat (_ $ (Const (@{const_name Not}, _) $ (Const (_, T) $ _ $ _))) = nT T
wenzelm@42439
   287
  | domain_is_nat _ = false;
wenzelm@24092
   288
wenzelm@24092
   289
wenzelm@24092
   290
(*---------------------------------------------------------------------------*)
webertj@32369
   291
(* the following code performs splitting of certain constants (e.g., min,    *)
wenzelm@24092
   292
(* max) in a linear arithmetic problem; similar to what split_tac later does *)
wenzelm@24092
   293
(* to the proof state                                                        *)
wenzelm@24092
   294
(*---------------------------------------------------------------------------*)
wenzelm@24092
   295
wenzelm@24092
   296
(* checks if splitting with 'thm' is implemented                             *)
wenzelm@24092
   297
wenzelm@42439
   298
fun is_split_thm ctxt thm =
wenzelm@42439
   299
  (case concl_of thm of _ $ (_ $ (_ $ lhs) $ _) =>
wenzelm@24092
   300
    (* Trueprop $ ((op =) $ (?P $ lhs) $ rhs) *)
wenzelm@42439
   301
    (case head_of lhs of
wenzelm@42439
   302
      Const (a, _) =>
wenzelm@42439
   303
        member (op =)
wenzelm@42439
   304
         [@{const_name Orderings.max},
wenzelm@42439
   305
          @{const_name Orderings.min},
wenzelm@42439
   306
          @{const_name Groups.abs},
wenzelm@42439
   307
          @{const_name Groups.minus},
wenzelm@42439
   308
          "Int.nat" (*DYNAMIC BINDING!*),
wenzelm@42439
   309
          "Divides.div_class.mod" (*DYNAMIC BINDING!*),
wenzelm@42439
   310
          "Divides.div_class.div" (*DYNAMIC BINDING!*)] a
wenzelm@42439
   311
    | _ =>
wenzelm@42439
   312
      (warning ("Lin. Arith.: wrong format for split rule " ^ Display.string_of_thm ctxt thm);
wenzelm@42439
   313
        false))
wenzelm@42439
   314
  | _ =>
wenzelm@42439
   315
    (warning ("Lin. Arith.: wrong format for split rule " ^ Display.string_of_thm ctxt thm);
wenzelm@42439
   316
      false));
wenzelm@24092
   317
wenzelm@24092
   318
(* substitute new for occurrences of old in a term, incrementing bound       *)
wenzelm@24092
   319
(* variables as needed when substituting inside an abstraction               *)
wenzelm@24092
   320
wenzelm@24092
   321
fun subst_term ([] : (term * term) list) (t : term) = t
wenzelm@24092
   322
  | subst_term pairs                     t          =
nipkow@29528
   323
      (case AList.lookup Pattern.aeconv pairs t of
wenzelm@24092
   324
        SOME new =>
wenzelm@24092
   325
          new
wenzelm@24092
   326
      | NONE     =>
wenzelm@24092
   327
          (case t of Abs (a, T, body) =>
wenzelm@24092
   328
            let val pairs' = map (pairself (incr_boundvars 1)) pairs
wenzelm@24092
   329
            in  Abs (a, T, subst_term pairs' body)  end
wenzelm@24092
   330
          | t1 $ t2                   =>
wenzelm@24092
   331
            subst_term pairs t1 $ subst_term pairs t2
wenzelm@24092
   332
          | _ => t));
wenzelm@24092
   333
wenzelm@24092
   334
(* approximates the effect of one application of split_tac (followed by NNF  *)
wenzelm@24092
   335
(* normalization) on the subgoal represented by '(Ts, terms)'; returns a     *)
wenzelm@24092
   336
(* list of new subgoals (each again represented by a typ list for bound      *)
wenzelm@24092
   337
(* variables and a term list for premises), or NONE if split_tac would fail  *)
wenzelm@24092
   338
(* on the subgoal                                                            *)
wenzelm@24092
   339
wenzelm@24092
   340
(* FIXME: currently only the effect of certain split theorems is reproduced  *)
wenzelm@24092
   341
(*        (which is why we need 'is_split_thm').  A more canonical           *)
wenzelm@24092
   342
(*        implementation should analyze the right-hand side of the split     *)
wenzelm@24092
   343
(*        theorem that can be applied, and modify the subgoal accordingly.   *)
wenzelm@24092
   344
(*        Or even better, the splitter should be extended to provide         *)
wenzelm@24092
   345
(*        splitting on terms as well as splitting on theorems (where the     *)
wenzelm@24092
   346
(*        former can have a faster implementation as it does not need to be  *)
wenzelm@24092
   347
(*        proof-producing).                                                  *)
wenzelm@24092
   348
wenzelm@24092
   349
fun split_once_items ctxt (Ts : typ list, terms : term list) :
wenzelm@24092
   350
                     (typ list * term list) list option =
wenzelm@24092
   351
let
wenzelm@42361
   352
  val thy = Proof_Context.theory_of ctxt
wenzelm@24092
   353
  (* takes a list  [t1, ..., tn]  to the term                                *)
wenzelm@24092
   354
  (*   tn' --> ... --> t1' --> False  ,                                      *)
wenzelm@24092
   355
  (* where ti' = HOLogic.dest_Trueprop ti                                    *)
webertj@32369
   356
  fun REPEAT_DETERM_etac_rev_mp tms =
webertj@32369
   357
    fold (curry HOLogic.mk_imp) (map HOLogic.dest_Trueprop tms)
wenzelm@45740
   358
      @{term False}
wenzelm@42439
   359
  val split_thms  = filter (is_split_thm ctxt) (#splits (get_arith_data ctxt))
webertj@32369
   360
  val cmap        = Splitter.cmap_of_split_thms split_thms
webertj@32369
   361
  val goal_tm     = REPEAT_DETERM_etac_rev_mp terms
webertj@32369
   362
  val splits      = Splitter.split_posns cmap thy Ts goal_tm
haftmann@31082
   363
  val split_limit = Config.get ctxt split_limit
wenzelm@24092
   364
in
webertj@32369
   365
  if length splits > split_limit then (
webertj@32369
   366
    tracing ("linarith_split_limit exceeded (current value is " ^
webertj@32369
   367
      string_of_int split_limit ^ ")");
webertj@32369
   368
    NONE
webertj@32369
   369
  ) else case splits of
webertj@32369
   370
    [] =>
wenzelm@24092
   371
    (* split_tac would fail: no possible split *)
wenzelm@24092
   372
    NONE
webertj@32369
   373
  | (_, _::_, _, _, _) :: _ =>
webertj@32369
   374
    (* disallow a split that involves non-locally bound variables (except    *)
webertj@32369
   375
    (* when bound by outermost meta-quantifiers)                             *)
webertj@32369
   376
    NONE
webertj@32369
   377
  | (_, [], _, split_type, split_term) :: _ =>
webertj@32369
   378
    (* ignore all but the first possible split                               *)
webertj@32369
   379
    (case strip_comb split_term of
wenzelm@24092
   380
    (* ?P (max ?i ?j) = ((?i <= ?j --> ?P ?j) & (~ ?i <= ?j --> ?P ?i)) *)
wenzelm@24092
   381
      (Const (@{const_name Orderings.max}, _), [t1, t2]) =>
wenzelm@24092
   382
      let
wenzelm@24092
   383
        val rev_terms     = rev terms
wenzelm@24092
   384
        val terms1        = map (subst_term [(split_term, t1)]) rev_terms
wenzelm@24092
   385
        val terms2        = map (subst_term [(split_term, t2)]) rev_terms
haftmann@35092
   386
        val t1_leq_t2     = Const (@{const_name Orderings.less_eq},
wenzelm@24092
   387
                                    split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
wenzelm@24092
   388
        val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
wenzelm@45740
   389
        val not_false     = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
wenzelm@24092
   390
        val subgoal1      = (HOLogic.mk_Trueprop t1_leq_t2) :: terms2 @ [not_false]
wenzelm@24092
   391
        val subgoal2      = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms1 @ [not_false]
wenzelm@24092
   392
      in
wenzelm@24092
   393
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
wenzelm@24092
   394
      end
wenzelm@24092
   395
    (* ?P (min ?i ?j) = ((?i <= ?j --> ?P ?i) & (~ ?i <= ?j --> ?P ?j)) *)
wenzelm@24092
   396
    | (Const (@{const_name Orderings.min}, _), [t1, t2]) =>
wenzelm@24092
   397
      let
wenzelm@24092
   398
        val rev_terms     = rev terms
wenzelm@24092
   399
        val terms1        = map (subst_term [(split_term, t1)]) rev_terms
wenzelm@24092
   400
        val terms2        = map (subst_term [(split_term, t2)]) rev_terms
haftmann@35092
   401
        val t1_leq_t2     = Const (@{const_name Orderings.less_eq},
wenzelm@24092
   402
                                    split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
wenzelm@24092
   403
        val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
wenzelm@45740
   404
        val not_false     = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
wenzelm@24092
   405
        val subgoal1      = (HOLogic.mk_Trueprop t1_leq_t2) :: terms1 @ [not_false]
wenzelm@24092
   406
        val subgoal2      = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms2 @ [not_false]
wenzelm@24092
   407
      in
wenzelm@24092
   408
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
wenzelm@24092
   409
      end
wenzelm@24092
   410
    (* ?P (abs ?a) = ((0 <= ?a --> ?P ?a) & (?a < 0 --> ?P (- ?a))) *)
haftmann@35092
   411
    | (Const (@{const_name Groups.abs}, _), [t1]) =>
wenzelm@24092
   412
      let
wenzelm@24092
   413
        val rev_terms   = rev terms
wenzelm@24092
   414
        val terms1      = map (subst_term [(split_term, t1)]) rev_terms
haftmann@35267
   415
        val terms2      = map (subst_term [(split_term, Const (@{const_name Groups.uminus},
wenzelm@24092
   416
                            split_type --> split_type) $ t1)]) rev_terms
haftmann@35267
   417
        val zero        = Const (@{const_name Groups.zero}, split_type)
haftmann@35092
   418
        val zero_leq_t1 = Const (@{const_name Orderings.less_eq},
wenzelm@24092
   419
                            split_type --> split_type --> HOLogic.boolT) $ zero $ t1
haftmann@35092
   420
        val t1_lt_zero  = Const (@{const_name Orderings.less},
wenzelm@24092
   421
                            split_type --> split_type --> HOLogic.boolT) $ t1 $ zero
wenzelm@45740
   422
        val not_false   = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
wenzelm@24092
   423
        val subgoal1    = (HOLogic.mk_Trueprop zero_leq_t1) :: terms1 @ [not_false]
wenzelm@24092
   424
        val subgoal2    = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
wenzelm@24092
   425
      in
wenzelm@24092
   426
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
wenzelm@24092
   427
      end
wenzelm@24092
   428
    (* ?P (?a - ?b) = ((?a < ?b --> ?P 0) & (ALL d. ?a = ?b + d --> ?P d)) *)
haftmann@35267
   429
    | (Const (@{const_name Groups.minus}, _), [t1, t2]) =>
wenzelm@24092
   430
      let
wenzelm@24092
   431
        (* "d" in the above theorem becomes a new bound variable after NNF   *)
wenzelm@24092
   432
        (* transformation, therefore some adjustment of indices is necessary *)
wenzelm@24092
   433
        val rev_terms       = rev terms
haftmann@35267
   434
        val zero            = Const (@{const_name Groups.zero}, split_type)
wenzelm@24092
   435
        val d               = Bound 0
wenzelm@24092
   436
        val terms1          = map (subst_term [(split_term, zero)]) rev_terms
wenzelm@24092
   437
        val terms2          = map (subst_term [(incr_boundvars 1 split_term, d)])
wenzelm@24092
   438
                                (map (incr_boundvars 1) rev_terms)
wenzelm@24092
   439
        val t1'             = incr_boundvars 1 t1
wenzelm@24092
   440
        val t2'             = incr_boundvars 1 t2
haftmann@35092
   441
        val t1_lt_t2        = Const (@{const_name Orderings.less},
wenzelm@24092
   442
                                split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
haftmann@38864
   443
        val t1_eq_t2_plus_d = Const (@{const_name HOL.eq}, split_type --> split_type --> HOLogic.boolT) $ t1' $
haftmann@35267
   444
                                (Const (@{const_name Groups.plus},
wenzelm@24092
   445
                                  split_type --> split_type --> split_type) $ t2' $ d)
wenzelm@45740
   446
        val not_false       = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
wenzelm@24092
   447
        val subgoal1        = (HOLogic.mk_Trueprop t1_lt_t2) :: terms1 @ [not_false]
wenzelm@24092
   448
        val subgoal2        = (HOLogic.mk_Trueprop t1_eq_t2_plus_d) :: terms2 @ [not_false]
wenzelm@24092
   449
      in
wenzelm@24092
   450
        SOME [(Ts, subgoal1), (split_type :: Ts, subgoal2)]
wenzelm@24092
   451
      end
webertj@33728
   452
    (* ?P (nat ?i) = ((ALL n. ?i = of_nat n --> ?P n) & (?i < 0 --> ?P 0)) *)
haftmann@25919
   453
    | (Const ("Int.nat", _), [t1]) =>
wenzelm@24092
   454
      let
wenzelm@24092
   455
        val rev_terms   = rev terms
haftmann@35267
   456
        val zero_int    = Const (@{const_name Groups.zero}, HOLogic.intT)
haftmann@35267
   457
        val zero_nat    = Const (@{const_name Groups.zero}, HOLogic.natT)
wenzelm@24092
   458
        val n           = Bound 0
wenzelm@24092
   459
        val terms1      = map (subst_term [(incr_boundvars 1 split_term, n)])
wenzelm@24092
   460
                            (map (incr_boundvars 1) rev_terms)
wenzelm@24092
   461
        val terms2      = map (subst_term [(split_term, zero_nat)]) rev_terms
wenzelm@24092
   462
        val t1'         = incr_boundvars 1 t1
haftmann@38864
   463
        val t1_eq_nat_n = Const (@{const_name HOL.eq}, HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1' $
haftmann@24196
   464
                            (Const (@{const_name of_nat}, HOLogic.natT --> HOLogic.intT) $ n)
haftmann@35092
   465
        val t1_lt_zero  = Const (@{const_name Orderings.less},
wenzelm@24092
   466
                            HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1 $ zero_int
wenzelm@45740
   467
        val not_false   = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
webertj@33728
   468
        val subgoal1    = (HOLogic.mk_Trueprop t1_eq_nat_n) :: terms1 @ [not_false]
wenzelm@24092
   469
        val subgoal2    = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
wenzelm@24092
   470
      in
wenzelm@24092
   471
        SOME [(HOLogic.natT :: Ts, subgoal1), (Ts, subgoal2)]
wenzelm@24092
   472
      end
huffman@47108
   473
    (* ?P ((?n::nat) mod (numeral ?k)) =
huffman@47108
   474
         ((numeral ?k = 0 --> ?P ?n) & (~ (numeral ?k = 0) -->
huffman@47108
   475
           (ALL i j. j < numeral ?k --> ?n = numeral ?k * i + j --> ?P j))) *)
haftmann@37388
   476
    | (Const ("Divides.div_class.mod", Type ("fun", [@{typ nat}, _])), [t1, t2]) =>
wenzelm@24092
   477
      let
wenzelm@24092
   478
        val rev_terms               = rev terms
haftmann@35267
   479
        val zero                    = Const (@{const_name Groups.zero}, split_type)
wenzelm@24092
   480
        val i                       = Bound 1
wenzelm@24092
   481
        val j                       = Bound 0
wenzelm@24092
   482
        val terms1                  = map (subst_term [(split_term, t1)]) rev_terms
wenzelm@24092
   483
        val terms2                  = map (subst_term [(incr_boundvars 2 split_term, j)])
wenzelm@24092
   484
                                        (map (incr_boundvars 2) rev_terms)
wenzelm@24092
   485
        val t1'                     = incr_boundvars 2 t1
wenzelm@24092
   486
        val t2'                     = incr_boundvars 2 t2
haftmann@38864
   487
        val t2_eq_zero              = Const (@{const_name HOL.eq},
wenzelm@24092
   488
                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
haftmann@38864
   489
        val t2_neq_zero             = HOLogic.mk_not (Const (@{const_name HOL.eq},
wenzelm@24092
   490
                                        split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
haftmann@35092
   491
        val j_lt_t2                 = Const (@{const_name Orderings.less},
wenzelm@24092
   492
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
haftmann@38864
   493
        val t1_eq_t2_times_i_plus_j = Const (@{const_name HOL.eq}, split_type --> split_type --> HOLogic.boolT) $ t1' $
haftmann@35267
   494
                                       (Const (@{const_name Groups.plus}, split_type --> split_type --> split_type) $
haftmann@35267
   495
                                         (Const (@{const_name Groups.times},
wenzelm@24092
   496
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
wenzelm@45740
   497
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
wenzelm@24092
   498
        val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
wenzelm@24092
   499
        val subgoal2                = (map HOLogic.mk_Trueprop
wenzelm@24092
   500
                                        [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
wenzelm@24092
   501
                                          @ terms2 @ [not_false]
wenzelm@24092
   502
      in
wenzelm@24092
   503
        SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
wenzelm@24092
   504
      end
huffman@47108
   505
    (* ?P ((?n::nat) div (numeral ?k)) =
huffman@47108
   506
         ((numeral ?k = 0 --> ?P 0) & (~ (numeral ?k = 0) -->
huffman@47108
   507
           (ALL i j. j < numeral ?k --> ?n = numeral ?k * i + j --> ?P i))) *)
haftmann@37388
   508
    | (Const ("Divides.div_class.div", Type ("fun", [@{typ nat}, _])), [t1, t2]) =>
wenzelm@24092
   509
      let
wenzelm@24092
   510
        val rev_terms               = rev terms
haftmann@35267
   511
        val zero                    = Const (@{const_name Groups.zero}, split_type)
wenzelm@24092
   512
        val i                       = Bound 1
wenzelm@24092
   513
        val j                       = Bound 0
wenzelm@24092
   514
        val terms1                  = map (subst_term [(split_term, zero)]) rev_terms
wenzelm@24092
   515
        val terms2                  = map (subst_term [(incr_boundvars 2 split_term, i)])
wenzelm@24092
   516
                                        (map (incr_boundvars 2) rev_terms)
wenzelm@24092
   517
        val t1'                     = incr_boundvars 2 t1
wenzelm@24092
   518
        val t2'                     = incr_boundvars 2 t2
haftmann@38864
   519
        val t2_eq_zero              = Const (@{const_name HOL.eq},
wenzelm@24092
   520
                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
haftmann@38864
   521
        val t2_neq_zero             = HOLogic.mk_not (Const (@{const_name HOL.eq},
wenzelm@24092
   522
                                        split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
haftmann@35092
   523
        val j_lt_t2                 = Const (@{const_name Orderings.less},
wenzelm@24092
   524
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
haftmann@38864
   525
        val t1_eq_t2_times_i_plus_j = Const (@{const_name HOL.eq}, split_type --> split_type --> HOLogic.boolT) $ t1' $
haftmann@35267
   526
                                       (Const (@{const_name Groups.plus}, split_type --> split_type --> split_type) $
haftmann@35267
   527
                                         (Const (@{const_name Groups.times},
wenzelm@24092
   528
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
wenzelm@45740
   529
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
wenzelm@24092
   530
        val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
wenzelm@24092
   531
        val subgoal2                = (map HOLogic.mk_Trueprop
wenzelm@24092
   532
                                        [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
wenzelm@24092
   533
                                          @ terms2 @ [not_false]
wenzelm@24092
   534
      in
wenzelm@24092
   535
        SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
wenzelm@24092
   536
      end
huffman@47108
   537
    (* ?P ((?n::int) mod (numeral ?k)) =
huffman@47108
   538
         ((numeral ?k = 0 --> ?P ?n) &
huffman@47108
   539
          (0 < numeral ?k -->
webertj@33728
   540
            (ALL i j.
huffman@47108
   541
              0 <= j & j < numeral ?k & ?n = numeral ?k * i + j --> ?P j)) &
huffman@47108
   542
          (numeral ?k < 0 -->
webertj@33728
   543
            (ALL i j.
huffman@47108
   544
              numeral ?k < j & j <= 0 & ?n = numeral ?k * i + j --> ?P j))) *)
wenzelm@24092
   545
    | (Const ("Divides.div_class.mod",
webertj@33728
   546
        Type ("fun", [Type ("Int.int", []), _])), [t1, t2]) =>
wenzelm@24092
   547
      let
wenzelm@24092
   548
        val rev_terms               = rev terms
haftmann@35267
   549
        val zero                    = Const (@{const_name Groups.zero}, split_type)
wenzelm@24092
   550
        val i                       = Bound 1
wenzelm@24092
   551
        val j                       = Bound 0
wenzelm@24092
   552
        val terms1                  = map (subst_term [(split_term, t1)]) rev_terms
wenzelm@24092
   553
        val terms2_3                = map (subst_term [(incr_boundvars 2 split_term, j)])
wenzelm@24092
   554
                                        (map (incr_boundvars 2) rev_terms)
wenzelm@24092
   555
        val t1'                     = incr_boundvars 2 t1
webertj@33728
   556
        val t2'                     = incr_boundvars 2 t2
haftmann@38864
   557
        val t2_eq_zero              = Const (@{const_name HOL.eq},
webertj@33728
   558
                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
haftmann@35092
   559
        val zero_lt_t2              = Const (@{const_name Orderings.less},
webertj@33728
   560
                                        split_type --> split_type --> HOLogic.boolT) $ zero $ t2'
haftmann@35092
   561
        val t2_lt_zero              = Const (@{const_name Orderings.less},
webertj@33728
   562
                                        split_type --> split_type --> HOLogic.boolT) $ t2' $ zero
haftmann@35092
   563
        val zero_leq_j              = Const (@{const_name Orderings.less_eq},
wenzelm@24092
   564
                                        split_type --> split_type --> HOLogic.boolT) $ zero $ j
haftmann@35092
   565
        val j_leq_zero              = Const (@{const_name Orderings.less_eq},
webertj@33728
   566
                                        split_type --> split_type --> HOLogic.boolT) $ j $ zero
haftmann@35092
   567
        val j_lt_t2                 = Const (@{const_name Orderings.less},
wenzelm@24092
   568
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
haftmann@35092
   569
        val t2_lt_j                 = Const (@{const_name Orderings.less},
webertj@33728
   570
                                        split_type --> split_type--> HOLogic.boolT) $ t2' $ j
haftmann@38864
   571
        val t1_eq_t2_times_i_plus_j = Const (@{const_name HOL.eq}, split_type --> split_type --> HOLogic.boolT) $ t1' $
haftmann@35267
   572
                                       (Const (@{const_name Groups.plus}, split_type --> split_type --> split_type) $
haftmann@35267
   573
                                         (Const (@{const_name Groups.times},
wenzelm@24092
   574
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
wenzelm@45740
   575
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
webertj@33728
   576
        val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
webertj@33728
   577
        val subgoal2                = (map HOLogic.mk_Trueprop [zero_lt_t2, zero_leq_j])
wenzelm@24092
   578
                                        @ hd terms2_3
wenzelm@24092
   579
                                        :: (if tl terms2_3 = [] then [not_false] else [])
wenzelm@24092
   580
                                        @ (map HOLogic.mk_Trueprop [j_lt_t2, t1_eq_t2_times_i_plus_j])
wenzelm@24092
   581
                                        @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
webertj@33728
   582
        val subgoal3                = (map HOLogic.mk_Trueprop [t2_lt_zero, t2_lt_j])
wenzelm@24092
   583
                                        @ hd terms2_3
wenzelm@24092
   584
                                        :: (if tl terms2_3 = [] then [not_false] else [])
wenzelm@24092
   585
                                        @ (map HOLogic.mk_Trueprop [j_leq_zero, t1_eq_t2_times_i_plus_j])
wenzelm@24092
   586
                                        @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
wenzelm@24092
   587
        val Ts'                     = split_type :: split_type :: Ts
wenzelm@24092
   588
      in
wenzelm@24092
   589
        SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
wenzelm@24092
   590
      end
huffman@47108
   591
    (* ?P ((?n::int) div (numeral ?k)) =
huffman@47108
   592
         ((numeral ?k = 0 --> ?P 0) &
huffman@47108
   593
          (0 < numeral ?k -->
webertj@33728
   594
            (ALL i j.
huffman@47108
   595
              0 <= j & j < numeral ?k & ?n = numeral ?k * i + j --> ?P i)) &
huffman@47108
   596
          (numeral ?k < 0 -->
webertj@33728
   597
            (ALL i j.
huffman@47108
   598
              numeral ?k < j & j <= 0 & ?n = numeral ?k * i + j --> ?P i))) *)
wenzelm@24092
   599
    | (Const ("Divides.div_class.div",
webertj@33728
   600
        Type ("fun", [Type ("Int.int", []), _])), [t1, t2]) =>
wenzelm@24092
   601
      let
wenzelm@24092
   602
        val rev_terms               = rev terms
haftmann@35267
   603
        val zero                    = Const (@{const_name Groups.zero}, split_type)
wenzelm@24092
   604
        val i                       = Bound 1
wenzelm@24092
   605
        val j                       = Bound 0
wenzelm@24092
   606
        val terms1                  = map (subst_term [(split_term, zero)]) rev_terms
wenzelm@24092
   607
        val terms2_3                = map (subst_term [(incr_boundvars 2 split_term, i)])
wenzelm@24092
   608
                                        (map (incr_boundvars 2) rev_terms)
wenzelm@24092
   609
        val t1'                     = incr_boundvars 2 t1
webertj@33728
   610
        val t2'                     = incr_boundvars 2 t2
haftmann@38864
   611
        val t2_eq_zero              = Const (@{const_name HOL.eq},
webertj@33728
   612
                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
haftmann@35092
   613
        val zero_lt_t2              = Const (@{const_name Orderings.less},
webertj@33728
   614
                                        split_type --> split_type --> HOLogic.boolT) $ zero $ t2'
haftmann@35092
   615
        val t2_lt_zero              = Const (@{const_name Orderings.less},
webertj@33728
   616
                                        split_type --> split_type --> HOLogic.boolT) $ t2' $ zero
haftmann@35092
   617
        val zero_leq_j              = Const (@{const_name Orderings.less_eq},
wenzelm@24092
   618
                                        split_type --> split_type --> HOLogic.boolT) $ zero $ j
haftmann@35092
   619
        val j_leq_zero              = Const (@{const_name Orderings.less_eq},
webertj@33728
   620
                                        split_type --> split_type --> HOLogic.boolT) $ j $ zero
haftmann@35092
   621
        val j_lt_t2                 = Const (@{const_name Orderings.less},
wenzelm@24092
   622
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
haftmann@35092
   623
        val t2_lt_j                 = Const (@{const_name Orderings.less},
webertj@33728
   624
                                        split_type --> split_type--> HOLogic.boolT) $ t2' $ j
haftmann@38864
   625
        val t1_eq_t2_times_i_plus_j = Const (@{const_name HOL.eq}, split_type --> split_type --> HOLogic.boolT) $ t1' $
haftmann@35267
   626
                                       (Const (@{const_name Groups.plus}, split_type --> split_type --> split_type) $
haftmann@35267
   627
                                         (Const (@{const_name Groups.times},
wenzelm@24092
   628
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
wenzelm@45740
   629
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ @{term False})
webertj@33728
   630
        val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
webertj@33728
   631
        val subgoal2                = (map HOLogic.mk_Trueprop [zero_lt_t2, zero_leq_j])
webertj@33728
   632
                                        @ hd terms2_3
webertj@33728
   633
                                        :: (if tl terms2_3 = [] then [not_false] else [])
webertj@33728
   634
                                        @ (map HOLogic.mk_Trueprop [j_lt_t2, t1_eq_t2_times_i_plus_j])
webertj@33728
   635
                                        @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
webertj@33728
   636
        val subgoal3                = (map HOLogic.mk_Trueprop [t2_lt_zero, t2_lt_j])
webertj@33728
   637
                                        @ hd terms2_3
webertj@33728
   638
                                        :: (if tl terms2_3 = [] then [not_false] else [])
webertj@33728
   639
                                        @ (map HOLogic.mk_Trueprop [j_leq_zero, t1_eq_t2_times_i_plus_j])
webertj@33728
   640
                                        @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
wenzelm@24092
   641
        val Ts'                     = split_type :: split_type :: Ts
wenzelm@24092
   642
      in
wenzelm@24092
   643
        SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
wenzelm@24092
   644
      end
wenzelm@24092
   645
    (* this will only happen if a split theorem can be applied for which no  *)
wenzelm@24092
   646
    (* code exists above -- in which case either the split theorem should be *)
wenzelm@24092
   647
    (* implemented above, or 'is_split_thm' should be modified to filter it  *)
wenzelm@24092
   648
    (* out                                                                   *)
wenzelm@24092
   649
    | (t, ts) => (
wenzelm@24920
   650
      warning ("Lin. Arith.: split rule for " ^ Syntax.string_of_term ctxt t ^
webertj@32369
   651
        " (with " ^ string_of_int (length ts) ^
webertj@32369
   652
        " argument(s)) not implemented; proof reconstruction is likely to fail");
wenzelm@24092
   653
      NONE
wenzelm@24092
   654
    ))
webertj@32369
   655
end;  (* split_once_items *)
wenzelm@24092
   656
wenzelm@24092
   657
(* remove terms that do not satisfy 'p'; change the order of the remaining   *)
wenzelm@24092
   658
(* terms in the same way as filter_prems_tac does                            *)
wenzelm@24092
   659
wenzelm@24092
   660
fun filter_prems_tac_items (p : term -> bool) (terms : term list) : term list =
wenzelm@42439
   661
  let
wenzelm@42439
   662
    fun filter_prems t (left, right) =
wenzelm@42439
   663
      if p t then (left, right @ [t]) else (left @ right, [])
wenzelm@42439
   664
    val (left, right) = fold filter_prems terms ([], [])
wenzelm@42439
   665
  in
wenzelm@42439
   666
    right @ left
wenzelm@42439
   667
  end;
wenzelm@24092
   668
wenzelm@24092
   669
(* return true iff TRY (etac notE) THEN eq_assume_tac would succeed on a     *)
wenzelm@24092
   670
(* subgoal that has 'terms' as premises                                      *)
wenzelm@24092
   671
wenzelm@24092
   672
fun negated_term_occurs_positively (terms : term list) : bool =
wenzelm@24092
   673
  List.exists
haftmann@38558
   674
    (fn (Trueprop $ (Const (@{const_name Not}, _) $ t)) =>
webertj@32369
   675
      member Pattern.aeconv terms (Trueprop $ t)
webertj@32369
   676
      | _ => false)
wenzelm@24092
   677
    terms;
wenzelm@24092
   678
wenzelm@24092
   679
fun pre_decomp ctxt (Ts : typ list, terms : term list) : (typ list * term list) list =
wenzelm@42439
   680
  let
wenzelm@42439
   681
    (* repeatedly split (including newly emerging subgoals) until no further   *)
wenzelm@42439
   682
    (* splitting is possible                                                   *)
wenzelm@42439
   683
    fun split_loop ([] : (typ list * term list) list) = ([] : (typ list * term list) list)
wenzelm@42439
   684
      | split_loop (subgoal::subgoals) =
wenzelm@42439
   685
          (case split_once_items ctxt subgoal of
wenzelm@42439
   686
            SOME new_subgoals => split_loop (new_subgoals @ subgoals)
wenzelm@42439
   687
          | NONE => subgoal :: split_loop subgoals)
wenzelm@42439
   688
    fun is_relevant t  = is_some (decomp ctxt t)
wenzelm@42439
   689
    (* filter_prems_tac is_relevant: *)
wenzelm@42439
   690
    val relevant_terms = filter_prems_tac_items is_relevant terms
wenzelm@42439
   691
    (* split_tac, NNF normalization: *)
wenzelm@42439
   692
    val split_goals = split_loop [(Ts, relevant_terms)]
wenzelm@42439
   693
    (* necessary because split_once_tac may normalize terms: *)
wenzelm@42439
   694
    val beta_eta_norm = map (apsnd (map (Envir.eta_contract o Envir.beta_norm)))
wenzelm@42439
   695
      split_goals
wenzelm@42439
   696
    (* TRY (etac notE) THEN eq_assume_tac: *)
wenzelm@42439
   697
    val result = filter_out (negated_term_occurs_positively o snd) beta_eta_norm
wenzelm@42439
   698
  in
wenzelm@42439
   699
    result
wenzelm@42439
   700
  end;
wenzelm@24092
   701
wenzelm@24092
   702
(* takes the i-th subgoal  [| A1; ...; An |] ==> B  to                       *)
wenzelm@24092
   703
(* An --> ... --> A1 --> B,  performs splitting with the given 'split_thms'  *)
wenzelm@24092
   704
(* (resulting in a different subgoal P), takes  P  to  ~P ==> False,         *)
wenzelm@24092
   705
(* performs NNF-normalization of ~P, and eliminates conjunctions,            *)
wenzelm@24092
   706
(* disjunctions and existential quantifiers from the premises, possibly (in  *)
wenzelm@24092
   707
(* the case of disjunctions) resulting in several new subgoals, each of the  *)
wenzelm@24092
   708
(* general form  [| Q1; ...; Qm |] ==> False.  Fails if more than            *)
haftmann@31082
   709
(* !split_limit splits are possible.                              *)
wenzelm@24092
   710
wenzelm@24092
   711
local
wenzelm@24092
   712
  val nnf_simpset =
wenzelm@45625
   713
    (empty_ss
wenzelm@45625
   714
      |> Simplifier.set_mkeqTrue mk_eq_True
wenzelm@45625
   715
      |> Simplifier.set_mksimps (mksimps mksimps_pairs))
wenzelm@35410
   716
    addsimps [@{thm imp_conv_disj}, @{thm iff_conv_conj_imp}, @{thm de_Morgan_disj},
wenzelm@35410
   717
      @{thm de_Morgan_conj}, not_all, not_ex, not_not]
wenzelm@35230
   718
  fun prem_nnf_tac ss = full_simp_tac (Simplifier.inherit_context ss nnf_simpset)
wenzelm@24092
   719
in
wenzelm@24092
   720
wenzelm@35230
   721
fun split_once_tac ss split_thms =
wenzelm@24092
   722
  let
wenzelm@35230
   723
    val ctxt = Simplifier.the_context ss
wenzelm@42361
   724
    val thy = Proof_Context.theory_of ctxt
wenzelm@24092
   725
    val cond_split_tac = SUBGOAL (fn (subgoal, i) =>
wenzelm@24092
   726
      let
wenzelm@24092
   727
        val Ts = rev (map snd (Logic.strip_params subgoal))
wenzelm@24092
   728
        val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl subgoal)
wenzelm@24092
   729
        val cmap = Splitter.cmap_of_split_thms split_thms
wenzelm@24092
   730
        val splits = Splitter.split_posns cmap thy Ts concl
wenzelm@24092
   731
      in
webertj@32369
   732
        if null splits orelse length splits > Config.get ctxt split_limit then
webertj@32369
   733
          no_tac
webertj@32369
   734
        else if null (#2 (hd splits)) then
webertj@32369
   735
          split_tac split_thms i
webertj@32369
   736
        else
webertj@32369
   737
          (* disallow a split that involves non-locally bound variables      *)
webertj@32369
   738
          (* (except when bound by outermost meta-quantifiers)               *)
webertj@32369
   739
          no_tac
wenzelm@24092
   740
      end)
wenzelm@24092
   741
  in
wenzelm@24092
   742
    EVERY' [
wenzelm@24092
   743
      REPEAT_DETERM o etac rev_mp,
wenzelm@24092
   744
      cond_split_tac,
wenzelm@24092
   745
      rtac ccontr,
wenzelm@35230
   746
      prem_nnf_tac ss,
wenzelm@24092
   747
      TRY o REPEAT_ALL_NEW (DETERM o (eresolve_tac [conjE, exE] ORELSE' etac disjE))
wenzelm@24092
   748
    ]
wenzelm@24092
   749
  end;
wenzelm@24092
   750
wenzelm@24092
   751
end;  (* local *)
wenzelm@24092
   752
wenzelm@24092
   753
(* remove irrelevant premises, then split the i-th subgoal (and all new      *)
wenzelm@24092
   754
(* subgoals) by using 'split_once_tac' repeatedly.  Beta-eta-normalize new   *)
wenzelm@24092
   755
(* subgoals and finally attempt to solve them by finding an immediate        *)
webertj@32369
   756
(* contradiction (i.e., a term and its negation) in their premises.          *)
wenzelm@24092
   757
wenzelm@35230
   758
fun pre_tac ss i =
wenzelm@42439
   759
  let
wenzelm@42439
   760
    val ctxt = Simplifier.the_context ss;
wenzelm@42439
   761
    val split_thms = filter (is_split_thm ctxt) (#splits (get_arith_data ctxt))
wenzelm@42439
   762
    fun is_relevant t = is_some (decomp ctxt t)
wenzelm@42439
   763
  in
wenzelm@42439
   764
    DETERM (
wenzelm@42439
   765
      TRY (filter_prems_tac is_relevant i)
wenzelm@42439
   766
        THEN (
wenzelm@42439
   767
          (TRY o REPEAT_ALL_NEW (split_once_tac ss split_thms))
wenzelm@42439
   768
            THEN_ALL_NEW
wenzelm@42439
   769
              (CONVERSION Drule.beta_eta_conversion
wenzelm@42439
   770
                THEN'
wenzelm@42439
   771
              (TRY o (etac notE THEN' eq_assume_tac)))
wenzelm@42439
   772
        ) i
wenzelm@42439
   773
    )
wenzelm@42439
   774
  end;
wenzelm@24092
   775
haftmann@31100
   776
end;  (* LA_Data *)
wenzelm@24092
   777
wenzelm@24092
   778
haftmann@31100
   779
val pre_tac = LA_Data.pre_tac;
wenzelm@24092
   780
haftmann@31100
   781
structure Fast_Arith = Fast_Lin_Arith(structure LA_Logic = LA_Logic and LA_Data = LA_Data);
wenzelm@24092
   782
wenzelm@38762
   783
val add_inj_thms = Fast_Arith.add_inj_thms;
wenzelm@38762
   784
val add_lessD = Fast_Arith.add_lessD;
wenzelm@38762
   785
val add_simps = Fast_Arith.add_simps;
wenzelm@38762
   786
val add_simprocs = Fast_Arith.add_simprocs;
wenzelm@38762
   787
val set_number_of = Fast_Arith.set_number_of;
boehmes@31510
   788
haftmann@31101
   789
fun simple_tac ctxt = Fast_Arith.lin_arith_tac ctxt false;
haftmann@31101
   790
val lin_arith_tac = Fast_Arith.lin_arith_tac;
wenzelm@24092
   791
wenzelm@24092
   792
(* reduce contradictory <= to False.
wenzelm@24092
   793
   Most of the work is done by the cancel tactics. *)
wenzelm@24092
   794
wenzelm@24092
   795
val init_arith_data =
boehmes@31510
   796
  Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, number_of, ...} =>
wenzelm@42439
   797
   {add_mono_thms = @{thms add_mono_thms_linordered_semiring} @
wenzelm@42439
   798
      @{thms add_mono_thms_linordered_field} @ add_mono_thms,
boehmes@31510
   799
    mult_mono_thms = @{thm mult_strict_left_mono} :: @{thm mult_left_mono} ::
boehmes@31510
   800
      @{lemma "a = b ==> c*a = c*b" by (rule arg_cong)} :: mult_mono_thms,
wenzelm@24092
   801
    inj_thms = inj_thms,
haftmann@31082
   802
    lessD = lessD @ [@{thm "Suc_leI"}],
haftmann@35028
   803
    neqE = [@{thm linorder_neqE_nat}, @{thm linorder_neqE_linordered_idom}],
wenzelm@24092
   804
    simpset = HOL_basic_ss
boehmes@31510
   805
      addsimps @{thms ring_distribs}
boehmes@31510
   806
      addsimps [@{thm if_True}, @{thm if_False}]
wenzelm@24092
   807
      addsimps
haftmann@35050
   808
       [@{thm add_0_left},
haftmann@35050
   809
        @{thm add_0_right},
wenzelm@24092
   810
        @{thm "Zero_not_Suc"}, @{thm "Suc_not_Zero"}, @{thm "le_0_eq"}, @{thm "One_nat_def"},
wenzelm@24092
   811
        @{thm "order_less_irrefl"}, @{thm "zero_neq_one"}, @{thm "zero_less_one"},
wenzelm@24092
   812
        @{thm "zero_le_one"}, @{thm "zero_neq_one"} RS not_sym, @{thm "not_one_le_zero"},
wenzelm@24092
   813
        @{thm "not_one_less_zero"}]
haftmann@37890
   814
      addsimprocs [@{simproc abel_cancel_sum}, @{simproc abel_cancel_relation}]
wenzelm@24092
   815
       (*abel_cancel helps it work in abstract algebraic domains*)
haftmann@31082
   816
      addsimprocs Nat_Arith.nat_cancel_sums_add
wenzelm@45620
   817
      |> Simplifier.add_cong @{thm if_weak_cong},
boehmes@31510
   818
    number_of = number_of}) #>
haftmann@31082
   819
  add_discrete_type @{type_name nat};
wenzelm@24092
   820
nipkow@29849
   821
fun add_arith_facts ss =
wenzelm@41225
   822
  Simplifier.add_prems (Arith_Data.get_arith_facts (Simplifier.the_context ss)) ss;
nipkow@29849
   823
haftmann@31101
   824
val simproc = add_arith_facts #> Fast_Arith.lin_arith_simproc;
wenzelm@24092
   825
wenzelm@24092
   826
haftmann@26110
   827
(* generic refutation procedure *)
haftmann@26110
   828
haftmann@26110
   829
(* parameters:
haftmann@26110
   830
haftmann@26110
   831
   test: term -> bool
haftmann@26110
   832
   tests if a term is at all relevant to the refutation proof;
haftmann@26110
   833
   if not, then it can be discarded. Can improve performance,
haftmann@26110
   834
   esp. if disjunctions can be discarded (no case distinction needed!).
haftmann@26110
   835
haftmann@26110
   836
   prep_tac: int -> tactic
haftmann@26110
   837
   A preparation tactic to be applied to the goal once all relevant premises
haftmann@26110
   838
   have been moved to the conclusion.
haftmann@26110
   839
haftmann@26110
   840
   ref_tac: int -> tactic
haftmann@26110
   841
   the actual refutation tactic. Should be able to deal with goals
haftmann@26110
   842
   [| A1; ...; An |] ==> False
haftmann@26110
   843
   where the Ai are atomic, i.e. no top-level &, | or EX
haftmann@26110
   844
*)
haftmann@26110
   845
haftmann@26110
   846
local
haftmann@26110
   847
  val nnf_simpset =
wenzelm@45625
   848
    (empty_ss
wenzelm@45625
   849
      |> Simplifier.set_mkeqTrue mk_eq_True
wenzelm@45625
   850
      |> Simplifier.set_mksimps (mksimps mksimps_pairs))
haftmann@26110
   851
    addsimps [@{thm imp_conv_disj}, @{thm iff_conv_conj_imp}, @{thm de_Morgan_disj},
haftmann@26110
   852
      @{thm de_Morgan_conj}, @{thm not_all}, @{thm not_ex}, @{thm not_not}];
haftmann@26110
   853
  fun prem_nnf_tac i st =
wenzelm@35232
   854
    full_simp_tac (Simplifier.global_context (Thm.theory_of_thm st) nnf_simpset) i st;
haftmann@26110
   855
in
wenzelm@42439
   856
haftmann@26110
   857
fun refute_tac test prep_tac ref_tac =
haftmann@26110
   858
  let val refute_prems_tac =
haftmann@26110
   859
        REPEAT_DETERM
haftmann@26110
   860
              (eresolve_tac [@{thm conjE}, @{thm exE}] 1 ORELSE
haftmann@26110
   861
               filter_prems_tac test 1 ORELSE
haftmann@26110
   862
               etac @{thm disjE} 1) THEN
haftmann@26110
   863
        (DETERM (etac @{thm notE} 1 THEN eq_assume_tac 1) ORELSE
haftmann@26110
   864
         ref_tac 1);
haftmann@26110
   865
  in EVERY'[TRY o filter_prems_tac test,
haftmann@26110
   866
            REPEAT_DETERM o etac @{thm rev_mp}, prep_tac, rtac @{thm ccontr}, prem_nnf_tac,
haftmann@26110
   867
            SELECT_GOAL (DEPTH_SOLVE refute_prems_tac)]
haftmann@26110
   868
  end;
wenzelm@42439
   869
haftmann@26110
   870
end;
haftmann@26110
   871
haftmann@26110
   872
wenzelm@24092
   873
(* arith proof method *)
wenzelm@24092
   874
wenzelm@24092
   875
local
wenzelm@24092
   876
haftmann@31101
   877
fun raw_tac ctxt ex =
wenzelm@33035
   878
  (* FIXME: K true should be replaced by a sensible test (perhaps "is_some o
wenzelm@24092
   879
     decomp sg"? -- but note that the test is applied to terms already before
wenzelm@24092
   880
     they are split/normalized) to speed things up in case there are lots of
wenzelm@24092
   881
     irrelevant terms involved; elimination of min/max can be optimized:
wenzelm@24092
   882
     (max m n + k <= r) = (m+k <= r & n+k <= r)
wenzelm@24092
   883
     (l <= min m n + k) = (l <= m+k & l <= n+k)
wenzelm@24092
   884
  *)
wenzelm@24092
   885
  refute_tac (K true)
webertj@33728
   886
    (* Splitting is also done inside simple_tac, but not completely --    *)
webertj@33728
   887
    (* split_tac may use split theorems that have not been implemented in *)
webertj@33728
   888
    (* simple_tac (cf. pre_decomp and split_once_items above), and        *)
webertj@33728
   889
    (* split_limit may trigger.                                           *)
webertj@33728
   890
    (* Therefore splitting outside of simple_tac may allow us to prove    *)
webertj@33728
   891
    (* some goals that simple_tac alone would fail on.                    *)
wenzelm@24092
   892
    (REPEAT_DETERM o split_tac (#splits (get_arith_data ctxt)))
haftmann@31101
   893
    (lin_arith_tac ctxt ex);
wenzelm@24092
   894
wenzelm@24092
   895
in
wenzelm@24092
   896
haftmann@31101
   897
fun gen_tac ex ctxt = FIRST' [simple_tac ctxt,
wenzelm@35625
   898
  Object_Logic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_tac ctxt ex];
wenzelm@24092
   899
haftmann@31101
   900
val tac = gen_tac true;
wenzelm@24092
   901
wenzelm@24092
   902
end;
wenzelm@24092
   903
wenzelm@24092
   904
wenzelm@24092
   905
(* context setup *)
wenzelm@24092
   906
wenzelm@24092
   907
val setup =
wenzelm@24092
   908
  init_arith_data #>
wenzelm@43595
   909
  Simplifier.map_ss (fn ss => ss
wenzelm@46709
   910
    addSolver (mk_solver "lin_arith" (add_arith_facts #> Fast_Arith.prems_lin_arith_tac)));
haftmann@31100
   911
haftmann@31100
   912
val global_setup =
haftmann@31100
   913
  Attrib.setup @{binding arith_split} (Scan.succeed (Thm.declaration_attribute add_split))
haftmann@31100
   914
    "declaration of split rules for arithmetic procedure" #>
haftmann@31100
   915
  Method.setup @{binding linarith}
wenzelm@33554
   916
    (Scan.succeed (fn ctxt =>
haftmann@31100
   917
      METHOD (fn facts =>
wenzelm@33554
   918
        HEADGOAL (Method.insert_tac (Arith_Data.get_arith_facts ctxt @ facts)
haftmann@31101
   919
          THEN' tac ctxt)))) "linear arithmetic" #>
haftmann@31101
   920
  Arith_Data.add_tactic "linear arithmetic" gen_tac;
wenzelm@24092
   921
wenzelm@24092
   922
end;