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