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