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