src/HOL/arith_data.ML
author wenzelm
Tue Aug 06 11:22:05 2002 +0200 (2002-08-06)
changeset 13462 56610e2ba220
parent 12949 b94843ffc0d1
child 13499 f95f5818f24f
permissions -rw-r--r--
sane interface for simprocs;
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
wenzelm@9436
    12
signature ARITH_DATA =
wenzelm@9436
    13
sig
wenzelm@9436
    14
  val nat_cancel_sums_add: simproc list
wenzelm@9436
    15
  val nat_cancel_sums: simproc list
wenzelm@9436
    16
end;
wenzelm@9436
    17
wenzelm@9436
    18
structure ArithData: ARITH_DATA =
wenzelm@9436
    19
struct
wenzelm@9436
    20
wenzelm@9436
    21
wenzelm@9436
    22
(** abstract syntax of structure nat: 0, Suc, + **)
wenzelm@9436
    23
wenzelm@9436
    24
(* mk_sum, mk_norm_sum *)
wenzelm@9436
    25
wenzelm@9436
    26
val one = HOLogic.mk_nat 1;
wenzelm@9436
    27
val mk_plus = HOLogic.mk_binop "op +";
wenzelm@9436
    28
wenzelm@9436
    29
fun mk_sum [] = HOLogic.zero
wenzelm@9436
    30
  | mk_sum [t] = t
wenzelm@9436
    31
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
wenzelm@9436
    32
wenzelm@9436
    33
(*normal form of sums: Suc (... (Suc (a + (b + ...))))*)
wenzelm@9436
    34
fun mk_norm_sum ts =
wenzelm@9436
    35
  let val (ones, sums) = partition (equal one) ts in
wenzelm@9436
    36
    funpow (length ones) HOLogic.mk_Suc (mk_sum sums)
wenzelm@9436
    37
  end;
wenzelm@9436
    38
wenzelm@9436
    39
wenzelm@9436
    40
(* dest_sum *)
wenzelm@9436
    41
wenzelm@9436
    42
val dest_plus = HOLogic.dest_bin "op +" HOLogic.natT;
wenzelm@9436
    43
wenzelm@9436
    44
fun dest_sum tm =
wenzelm@9436
    45
  if HOLogic.is_zero tm then []
wenzelm@9436
    46
  else
wenzelm@9436
    47
    (case try HOLogic.dest_Suc tm of
wenzelm@9436
    48
      Some t => one :: dest_sum t
wenzelm@9436
    49
    | None =>
wenzelm@9436
    50
        (case try dest_plus tm of
wenzelm@9436
    51
          Some (t, u) => dest_sum t @ dest_sum u
wenzelm@9436
    52
        | None => [tm]));
wenzelm@9436
    53
wenzelm@9436
    54
wenzelm@9436
    55
(** generic proof tools **)
wenzelm@9436
    56
wenzelm@9436
    57
(* prove conversions *)
wenzelm@9436
    58
wenzelm@9436
    59
val mk_eqv = HOLogic.mk_Trueprop o HOLogic.mk_eq;
wenzelm@9436
    60
wenzelm@9436
    61
fun prove_conv expand_tac norm_tac sg (t, u) =
wenzelm@9436
    62
  mk_meta_eq (prove_goalw_cterm_nocheck [] (cterm_of sg (mk_eqv (t, u)))
wenzelm@9436
    63
    (K [expand_tac, norm_tac]))
wenzelm@9436
    64
  handle ERROR => error ("The error(s) above occurred while trying to prove " ^
wenzelm@9436
    65
    (string_of_cterm (cterm_of sg (mk_eqv (t, u)))));
wenzelm@9436
    66
wenzelm@9436
    67
val subst_equals = prove_goal HOL.thy "[| t = s; u = t |] ==> u = s"
wenzelm@9436
    68
  (fn prems => [cut_facts_tac prems 1, SIMPSET' asm_simp_tac 1]);
wenzelm@9436
    69
wenzelm@9436
    70
wenzelm@9436
    71
(* rewriting *)
wenzelm@9436
    72
wenzelm@9436
    73
fun simp_all rules = ALLGOALS (simp_tac (HOL_ss addsimps rules));
wenzelm@9436
    74
wenzelm@9436
    75
val add_rules = [add_Suc, add_Suc_right, add_0, add_0_right];
wenzelm@9436
    76
val mult_rules = [mult_Suc, mult_Suc_right, mult_0, mult_0_right];
wenzelm@9436
    77
wenzelm@9436
    78
wenzelm@9436
    79
wenzelm@9436
    80
(** cancel common summands **)
wenzelm@9436
    81
wenzelm@9436
    82
structure Sum =
wenzelm@9436
    83
struct
wenzelm@9436
    84
  val mk_sum = mk_norm_sum;
wenzelm@9436
    85
  val dest_sum = dest_sum;
wenzelm@9436
    86
  val prove_conv = prove_conv;
wenzelm@9436
    87
  val norm_tac = simp_all add_rules THEN simp_all add_ac;
wenzelm@9436
    88
end;
wenzelm@9436
    89
wenzelm@9436
    90
fun gen_uncancel_tac rule ct =
wenzelm@9436
    91
  rtac (instantiate' [] [None, Some ct] (rule RS subst_equals)) 1;
wenzelm@9436
    92
wenzelm@9436
    93
wenzelm@9436
    94
(* nat eq *)
wenzelm@9436
    95
wenzelm@9436
    96
structure EqCancelSums = CancelSumsFun
wenzelm@9436
    97
(struct
wenzelm@9436
    98
  open Sum;
wenzelm@9436
    99
  val mk_bal = HOLogic.mk_eq;
wenzelm@9436
   100
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT;
wenzelm@9436
   101
  val uncancel_tac = gen_uncancel_tac add_left_cancel;
wenzelm@9436
   102
end);
wenzelm@9436
   103
wenzelm@9436
   104
wenzelm@9436
   105
(* nat less *)
wenzelm@9436
   106
wenzelm@9436
   107
structure LessCancelSums = CancelSumsFun
wenzelm@9436
   108
(struct
wenzelm@9436
   109
  open Sum;
wenzelm@9436
   110
  val mk_bal = HOLogic.mk_binrel "op <";
wenzelm@9436
   111
  val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT;
wenzelm@9436
   112
  val uncancel_tac = gen_uncancel_tac add_left_cancel_less;
wenzelm@9436
   113
end);
wenzelm@9436
   114
wenzelm@9436
   115
wenzelm@9436
   116
(* nat le *)
wenzelm@9436
   117
wenzelm@9436
   118
structure LeCancelSums = CancelSumsFun
wenzelm@9436
   119
(struct
wenzelm@9436
   120
  open Sum;
wenzelm@9436
   121
  val mk_bal = HOLogic.mk_binrel "op <=";
wenzelm@9436
   122
  val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT;
wenzelm@9436
   123
  val uncancel_tac = gen_uncancel_tac add_left_cancel_le;
wenzelm@9436
   124
end);
wenzelm@9436
   125
wenzelm@9436
   126
wenzelm@9436
   127
(* nat diff *)
wenzelm@9436
   128
wenzelm@9436
   129
structure DiffCancelSums = CancelSumsFun
wenzelm@9436
   130
(struct
wenzelm@9436
   131
  open Sum;
wenzelm@9436
   132
  val mk_bal = HOLogic.mk_binop "op -";
wenzelm@9436
   133
  val dest_bal = HOLogic.dest_bin "op -" HOLogic.natT;
wenzelm@9436
   134
  val uncancel_tac = gen_uncancel_tac diff_cancel;
wenzelm@9436
   135
end);
wenzelm@9436
   136
wenzelm@9436
   137
wenzelm@9436
   138
wenzelm@9436
   139
(** prepare nat_cancel simprocs **)
wenzelm@9436
   140
wenzelm@13462
   141
fun prep_simproc (name, pats, proc) =
wenzelm@13462
   142
  Simplifier.simproc (Theory.sign_of (the_context ())) name pats proc;
wenzelm@9436
   143
wenzelm@9436
   144
val nat_cancel_sums_add = map prep_simproc
wenzelm@13462
   145
  [("nateq_cancel_sums",
wenzelm@13462
   146
     ["(l::nat) + m = n", "(l::nat) = m + n", "Suc m = n", "m = Suc n"], EqCancelSums.proc),
wenzelm@13462
   147
   ("natless_cancel_sums",
wenzelm@13462
   148
     ["(l::nat) + m < n", "(l::nat) < m + n", "Suc m < n", "m < Suc n"], LessCancelSums.proc),
wenzelm@13462
   149
   ("natle_cancel_sums",
wenzelm@13462
   150
     ["(l::nat) + m <= n", "(l::nat) <= m + n", "Suc m <= n", "m <= Suc n"], LeCancelSums.proc)];
wenzelm@9436
   151
wenzelm@9436
   152
val nat_cancel_sums = nat_cancel_sums_add @
wenzelm@13462
   153
  [prep_simproc ("natdiff_cancel_sums",
wenzelm@13462
   154
    ["((l::nat) + m) - n", "(l::nat) - (m + n)", "Suc m - n", "m - Suc n"], DiffCancelSums.proc)];
wenzelm@9436
   155
wenzelm@9436
   156
end;
wenzelm@9436
   157
wenzelm@9436
   158
open ArithData;
wenzelm@9436
   159
wenzelm@9436
   160
wenzelm@9436
   161
(*---------------------------------------------------------------------------*)
wenzelm@9436
   162
(* 2. Linear arithmetic                                                      *)
wenzelm@9436
   163
(*---------------------------------------------------------------------------*)
wenzelm@9436
   164
wenzelm@9436
   165
(* Parameters data for general linear arithmetic functor *)
wenzelm@9436
   166
wenzelm@9436
   167
structure LA_Logic: LIN_ARITH_LOGIC =
wenzelm@9436
   168
struct
wenzelm@9436
   169
val ccontr = ccontr;
wenzelm@9436
   170
val conjI = conjI;
wenzelm@9436
   171
val neqE = linorder_neqE;
wenzelm@9436
   172
val notI = notI;
wenzelm@9436
   173
val sym = sym;
wenzelm@9436
   174
val not_lessD = linorder_not_less RS iffD1;
wenzelm@9436
   175
val not_leD = linorder_not_le RS iffD1;
wenzelm@9436
   176
wenzelm@9436
   177
wenzelm@9436
   178
fun mk_Eq thm = (thm RS Eq_FalseI) handle THM _ => (thm RS Eq_TrueI);
wenzelm@9436
   179
wenzelm@9436
   180
val mk_Trueprop = HOLogic.mk_Trueprop;
wenzelm@9436
   181
wenzelm@9436
   182
fun neg_prop(TP$(Const("Not",_)$t)) = TP$t
wenzelm@9436
   183
  | neg_prop(TP$t) = TP $ (Const("Not",HOLogic.boolT-->HOLogic.boolT)$t);
wenzelm@9436
   184
wenzelm@9436
   185
fun is_False thm =
wenzelm@9436
   186
  let val _ $ t = #prop(rep_thm thm)
wenzelm@9436
   187
  in t = Const("False",HOLogic.boolT) end;
wenzelm@9436
   188
wenzelm@9436
   189
fun is_nat(t) = fastype_of1 t = HOLogic.natT;
wenzelm@9436
   190
wenzelm@9436
   191
fun mk_nat_thm sg t =
wenzelm@9436
   192
  let val ct = cterm_of sg t  and cn = cterm_of sg (Var(("n",0),HOLogic.natT))
wenzelm@9436
   193
  in instantiate ([],[(cn,ct)]) le0 end;
wenzelm@9436
   194
wenzelm@9436
   195
end;
wenzelm@9436
   196
wenzelm@9436
   197
wenzelm@9436
   198
(* arith theory data *)
wenzelm@9436
   199
wenzelm@9593
   200
structure ArithTheoryDataArgs =
wenzelm@9436
   201
struct
wenzelm@9436
   202
  val name = "HOL/arith";
nipkow@10574
   203
  type T = {splits: thm list, inj_consts: (string * typ)list, discrete: (string * bool) list};
wenzelm@9436
   204
nipkow@10574
   205
  val empty = {splits = [], inj_consts = [], discrete = []};
wenzelm@9436
   206
  val copy = I;
wenzelm@9436
   207
  val prep_ext = I;
nipkow@10574
   208
  fun merge ({splits= splits1, inj_consts= inj_consts1, discrete= discrete1},
nipkow@10574
   209
             {splits= splits2, inj_consts= inj_consts2, discrete= discrete2}) =
wenzelm@9436
   210
   {splits = Drule.merge_rules (splits1, splits2),
nipkow@10574
   211
    inj_consts = merge_lists inj_consts1 inj_consts2,
wenzelm@9436
   212
    discrete = merge_alists discrete1 discrete2};
wenzelm@9436
   213
  fun print _ _ = ();
wenzelm@9436
   214
end;
wenzelm@9436
   215
wenzelm@9593
   216
structure ArithTheoryData = TheoryDataFun(ArithTheoryDataArgs);
wenzelm@9436
   217
nipkow@10574
   218
fun arith_split_add (thy, thm) = (ArithTheoryData.map (fn {splits,inj_consts,discrete} =>
nipkow@10574
   219
  {splits= thm::splits, inj_consts= inj_consts, discrete= discrete}) thy, thm);
wenzelm@9436
   220
nipkow@10574
   221
fun arith_discrete d = ArithTheoryData.map (fn {splits,inj_consts,discrete} =>
nipkow@10574
   222
  {splits = splits, inj_consts = inj_consts, discrete = d :: discrete});
nipkow@10574
   223
nipkow@10574
   224
fun arith_inj_const c = ArithTheoryData.map (fn {splits,inj_consts,discrete} =>
nipkow@10574
   225
  {splits = splits, inj_consts = c :: inj_consts, discrete = discrete});
wenzelm@9436
   226
wenzelm@9436
   227
wenzelm@9436
   228
structure LA_Data_Ref: LIN_ARITH_DATA =
wenzelm@9436
   229
struct
wenzelm@9436
   230
wenzelm@9436
   231
(* Decomposition of terms *)
wenzelm@9436
   232
wenzelm@9436
   233
fun nT (Type("fun",[N,_])) = N = HOLogic.natT
wenzelm@9436
   234
  | nT _ = false;
wenzelm@9436
   235
wenzelm@9436
   236
fun add_atom(t,m,(p,i)) = (case assoc(p,t) of None => ((t,m)::p,i)
nipkow@10693
   237
                           | Some n => (overwrite(p,(t,ratadd(n,m))), i));
nipkow@10693
   238
nipkow@10693
   239
exception Zero;
wenzelm@9436
   240
nipkow@10693
   241
fun rat_of_term(numt,dent) =
nipkow@10693
   242
  let val num = HOLogic.dest_binum numt and den = HOLogic.dest_binum dent
nipkow@10693
   243
  in if den = 0 then raise Zero else int_ratdiv(num,den) end;
nipkow@10718
   244
nipkow@10718
   245
(* Warning: in rare cases number_of encloses a non-numeral,
nipkow@10718
   246
   in which case dest_binum raises TERM; hence all the handles below.
nipkow@11334
   247
   Same for Suc-terms that turn out not to be numerals -
nipkow@11334
   248
   although the simplifier should eliminate those anyway...
nipkow@10718
   249
*)
nipkow@10718
   250
nipkow@11334
   251
fun number_of_Sucs (Const("Suc",_) $ n) = number_of_Sucs n + 1
nipkow@11334
   252
  | number_of_Sucs t = if HOLogic.is_zero t then 0
nipkow@11334
   253
                       else raise TERM("number_of_Sucs",[])
nipkow@11334
   254
nipkow@10718
   255
(* decompose nested multiplications, bracketing them to the right and combining all
nipkow@10718
   256
   their coefficients
nipkow@10718
   257
*)
nipkow@10718
   258
nipkow@10718
   259
fun demult((mC as Const("op *",_)) $ s $ t,m) = ((case s of
nipkow@10718
   260
        Const("Numeral.number_of",_)$n
nipkow@10718
   261
        => demult(t,ratmul(m,rat_of_int(HOLogic.dest_binum n)))
nipkow@12480
   262
      | Const("uminus",_)$(Const("Numeral.number_of",_)$n)
nipkow@12480
   263
        => demult(t,ratmul(m,rat_of_int(~(HOLogic.dest_binum n))))
nipkow@11334
   264
      | Const("Suc",_) $ _
nipkow@11334
   265
        => demult(t,ratmul(m,rat_of_int(number_of_Sucs s)))
nipkow@10718
   266
      | Const("op *",_) $ s1 $ s2 => demult(mC $ s1 $ (mC $ s2 $ t),m)
nipkow@10718
   267
      | Const("HOL.divide",_) $ numt $ (Const("Numeral.number_of",_)$dent) =>
nipkow@10718
   268
          let val den = HOLogic.dest_binum dent
nipkow@10718
   269
          in if den = 0 then raise Zero
nipkow@10718
   270
             else demult(mC $ numt $ t,ratmul(m, ratinv(rat_of_int den)))
nipkow@10718
   271
          end
nipkow@10718
   272
      | _ => atomult(mC,s,t,m)
nipkow@10718
   273
      ) handle TERM _ => atomult(mC,s,t,m))
nipkow@10718
   274
  | demult(atom as Const("HOL.divide",_) $ t $ (Const("Numeral.number_of",_)$dent), m) =
nipkow@10718
   275
      (let val den = HOLogic.dest_binum dent
nipkow@10718
   276
       in if den = 0 then raise Zero else demult(t,ratmul(m, ratinv(rat_of_int den))) end
nipkow@10718
   277
       handle TERM _ => (Some atom,m))
nipkow@10718
   278
  | demult(t as Const("Numeral.number_of",_)$n,m) =
nipkow@10718
   279
      ((None,ratmul(m,rat_of_int(HOLogic.dest_binum n)))
nipkow@10718
   280
       handle TERM _ => (Some t,m))
nipkow@12480
   281
  | demult(Const("uminus",_)$t, m) = demult(t,ratmul(m,rat_of_int(~1)))
nipkow@10718
   282
  | demult(atom,m) = (Some atom,m)
nipkow@10718
   283
nipkow@10718
   284
and atomult(mC,atom,t,m) = (case demult(t,m) of (None,m') => (Some atom,m')
nipkow@10718
   285
                            | (Some t',m') => (Some(mC $ atom $ t'),m'))
nipkow@10718
   286
nipkow@10574
   287
fun decomp2 inj_consts (rel,lhs,rhs) =
nipkow@10574
   288
let
wenzelm@9436
   289
(* Turn term into list of summand * multiplicity plus a constant *)
wenzelm@9436
   290
fun poly(Const("op +",_) $ s $ t, m, pi) = poly(s,m,poly(t,m,pi))
wenzelm@9436
   291
  | poly(all as Const("op -",T) $ s $ t, m, pi) =
wenzelm@9436
   292
      if nT T then add_atom(all,m,pi)
nipkow@10693
   293
      else poly(s,m,poly(t,ratneg m,pi))
nipkow@10693
   294
  | poly(Const("uminus",_) $ t, m, pi) = poly(t,ratneg m,pi)
wenzelm@9436
   295
  | poly(Const("0",_), _, pi) = pi
nipkow@11464
   296
  | poly(Const("1",_), m, (p,i)) = (p,ratadd(i,m))
nipkow@10693
   297
  | poly(Const("Suc",_)$t, m, (p,i)) = poly(t, m, (p,ratadd(i,m)))
nipkow@10718
   298
  | poly(t as Const("op *",_) $ _ $ _, m, pi as (p,i)) =
nipkow@10718
   299
      (case demult(t,m) of
nipkow@10718
   300
         (None,m') => (p,ratadd(i,m))
nipkow@10718
   301
       | (Some u,m') => add_atom(u,m',pi))
nipkow@10718
   302
  | poly(t as Const("HOL.divide",_) $ _ $ _, m, pi as (p,i)) =
nipkow@10718
   303
      (case demult(t,m) of
nipkow@10718
   304
         (None,m') => (p,ratadd(i,m))
nipkow@10718
   305
       | (Some u,m') => add_atom(u,m',pi))
nipkow@10718
   306
  | poly(all as (Const("Numeral.number_of",_)$t,m,(p,i))) =
nipkow@10718
   307
      ((p,ratadd(i,ratmul(m,rat_of_int(HOLogic.dest_binum t))))
nipkow@10718
   308
       handle TERM _ => add_atom all)
nipkow@10574
   309
  | poly(all as Const f $ x, m, pi) =
nipkow@10574
   310
      if f mem inj_consts then poly(x,m,pi) else add_atom(all,m,pi)
wenzelm@9436
   311
  | poly x  = add_atom x;
wenzelm@9436
   312
nipkow@10718
   313
val (p,i) = poly(lhs,rat_of_int 1,([],rat_of_int 0))
nipkow@10718
   314
and (q,j) = poly(rhs,rat_of_int 1,([],rat_of_int 0))
nipkow@10693
   315
wenzelm@9436
   316
  in case rel of
wenzelm@9436
   317
       "op <"  => Some(p,i,"<",q,j)
wenzelm@9436
   318
     | "op <=" => Some(p,i,"<=",q,j)
wenzelm@9436
   319
     | "op ="  => Some(p,i,"=",q,j)
wenzelm@9436
   320
     | _       => None
nipkow@10693
   321
  end handle Zero => None;
wenzelm@9436
   322
wenzelm@9436
   323
fun negate(Some(x,i,rel,y,j,d)) = Some(x,i,"~"^rel,y,j,d)
wenzelm@9436
   324
  | negate None = None;
wenzelm@9436
   325
nipkow@10574
   326
fun decomp1 (discrete,inj_consts) (T,xxx) =
wenzelm@9436
   327
  (case T of
wenzelm@9436
   328
     Type("fun",[Type(D,[]),_]) =>
wenzelm@9436
   329
       (case assoc(discrete,D) of
wenzelm@9436
   330
          None => None
nipkow@10574
   331
        | Some d => (case decomp2 inj_consts xxx of
wenzelm@9436
   332
                       None => None
wenzelm@9436
   333
                     | Some(p,i,rel,q,j) => Some(p,i,rel,q,j,d)))
wenzelm@9436
   334
   | _ => None);
wenzelm@9436
   335
nipkow@10574
   336
fun decomp2 data (_$(Const(rel,T)$lhs$rhs)) = decomp1 data (T,(rel,lhs,rhs))
nipkow@10574
   337
  | decomp2 data (_$(Const("Not",_)$(Const(rel,T)$lhs$rhs))) =
nipkow@10574
   338
      negate(decomp1 data (T,(rel,lhs,rhs)))
nipkow@10574
   339
  | decomp2 data _ = None
wenzelm@9436
   340
nipkow@10574
   341
fun decomp sg =
nipkow@10574
   342
  let val {discrete, inj_consts, ...} = ArithTheoryData.get_sg sg
nipkow@10574
   343
  in decomp2 (discrete,inj_consts) end
wenzelm@9436
   344
nipkow@10693
   345
fun number_of(n,T) = HOLogic.number_of_const T $ (HOLogic.mk_bin n)
nipkow@10693
   346
wenzelm@9436
   347
end;
wenzelm@9436
   348
wenzelm@9436
   349
wenzelm@9436
   350
structure Fast_Arith =
wenzelm@9436
   351
  Fast_Lin_Arith(structure LA_Logic=LA_Logic and LA_Data=LA_Data_Ref);
wenzelm@9436
   352
wenzelm@9436
   353
val fast_arith_tac = Fast_Arith.lin_arith_tac
wenzelm@9436
   354
and trace_arith    = Fast_Arith.trace;
wenzelm@9436
   355
wenzelm@9436
   356
local
wenzelm@9436
   357
wenzelm@9436
   358
(* reduce contradictory <= to False.
wenzelm@9436
   359
   Most of the work is done by the cancel tactics.
wenzelm@9436
   360
*)
nipkow@12931
   361
val add_rules =
nipkow@12931
   362
 [add_0,add_0_right,Zero_not_Suc,Suc_not_Zero,le_0_eq,
nipkow@12931
   363
  One_nat_def];
wenzelm@9436
   364
wenzelm@9436
   365
val add_mono_thms_nat = map (fn s => prove_goal (the_context ()) s
wenzelm@9436
   366
 (fn prems => [cut_facts_tac prems 1,
wenzelm@9436
   367
               blast_tac (claset() addIs [add_le_mono]) 1]))
wenzelm@9436
   368
["(i <= j) & (k <= l) ==> i + k <= j + (l::nat)",
wenzelm@9436
   369
 "(i  = j) & (k <= l) ==> i + k <= j + (l::nat)",
wenzelm@9436
   370
 "(i <= j) & (k  = l) ==> i + k <= j + (l::nat)",
wenzelm@9436
   371
 "(i  = j) & (k  = l) ==> i + k  = j + (l::nat)"
wenzelm@9436
   372
];
wenzelm@9436
   373
wenzelm@9436
   374
in
wenzelm@9436
   375
wenzelm@9436
   376
val init_lin_arith_data =
wenzelm@9436
   377
 Fast_Arith.setup @
nipkow@10693
   378
 [Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, simpset = _} =>
wenzelm@9436
   379
   {add_mono_thms = add_mono_thms @ add_mono_thms_nat,
nipkow@10693
   380
    mult_mono_thms = mult_mono_thms,
nipkow@10574
   381
    inj_thms = inj_thms,
wenzelm@9436
   382
    lessD = lessD @ [Suc_leI],
wenzelm@9436
   383
    simpset = HOL_basic_ss addsimps add_rules addsimprocs nat_cancel_sums_add}),
wenzelm@9593
   384
  ArithTheoryData.init, arith_discrete ("nat", true)];
wenzelm@9436
   385
wenzelm@9436
   386
end;
wenzelm@9436
   387
wenzelm@13462
   388
val fast_nat_arith_simproc =
wenzelm@13462
   389
  Simplifier.simproc (Theory.sign_of (the_context ())) "fast_nat_arith"
wenzelm@13462
   390
    ["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"] Fast_Arith.lin_arith_prover;
wenzelm@9436
   391
wenzelm@9436
   392
wenzelm@9436
   393
(* Because of fast_nat_arith_simproc, the arithmetic solver is really only
wenzelm@9436
   394
useful to detect inconsistencies among the premises for subgoals which are
wenzelm@9436
   395
*not* themselves (in)equalities, because the latter activate
wenzelm@9436
   396
fast_nat_arith_simproc anyway. However, it seems cheaper to activate the
wenzelm@9436
   397
solver all the time rather than add the additional check. *)
wenzelm@9436
   398
wenzelm@9436
   399
wenzelm@9436
   400
(* arith proof method *)
wenzelm@9436
   401
wenzelm@9436
   402
(* FIXME: K true should be replaced by a sensible test to speed things up
wenzelm@9436
   403
   in case there are lots of irrelevant terms involved;
wenzelm@9436
   404
   elimination of min/max can be optimized:
wenzelm@9436
   405
   (max m n + k <= r) = (m+k <= r & n+k <= r)
wenzelm@9436
   406
   (l <= min m n + k) = (l <= m+k & l <= n+k)
wenzelm@9436
   407
*)
wenzelm@10516
   408
local
wenzelm@10516
   409
wenzelm@10516
   410
fun raw_arith_tac i st =
wenzelm@9593
   411
  refute_tac (K true) (REPEAT o split_tac (#splits (ArithTheoryData.get_sg (Thm.sign_of_thm st))))
wenzelm@9436
   412
             ((REPEAT_DETERM o etac linorder_neqE) THEN' fast_arith_tac) i st;
wenzelm@9436
   413
wenzelm@10516
   414
in
wenzelm@10516
   415
wenzelm@11753
   416
val arith_tac = fast_arith_tac ORELSE' (ObjectLogic.atomize_tac THEN' raw_arith_tac);
wenzelm@10516
   417
wenzelm@9436
   418
fun arith_method prems =
wenzelm@9436
   419
  Method.METHOD (fn facts => HEADGOAL (Method.insert_tac (prems @ facts) THEN' arith_tac));
wenzelm@9436
   420
wenzelm@10516
   421
end;
wenzelm@10516
   422
wenzelm@9436
   423
wenzelm@9436
   424
(* theory setup *)
wenzelm@9436
   425
wenzelm@9436
   426
val arith_setup =
paulson@10766
   427
 [Simplifier.change_simpset_of (op addsimprocs) nat_cancel_sums] @
wenzelm@9436
   428
  init_lin_arith_data @
wenzelm@9436
   429
  [Simplifier.change_simpset_of (op addSolver)
wenzelm@9436
   430
   (mk_solver "lin. arith." Fast_Arith.cut_lin_arith_tac),
wenzelm@9436
   431
  Simplifier.change_simpset_of (op addsimprocs) [fast_nat_arith_simproc],
wenzelm@11704
   432
  Method.add_methods [("arith", (arith_method o #2) oo Method.syntax Args.bang_facts,
wenzelm@9436
   433
    "decide linear arithmethic")],
wenzelm@9436
   434
  Attrib.add_attributes [("arith_split",
wenzelm@9436
   435
    (Attrib.no_args arith_split_add, Attrib.no_args Attrib.undef_local_attribute),
wenzelm@9893
   436
    "declaration of split rules for arithmetic procedure")]];