src/HOL/arith_data.ML
author oheimb
Wed Jan 31 10:15:55 2001 +0100 (2001-01-31)
changeset 11008 f7333f055ef6
parent 10906 de95ba2760fe
child 11334 a16eaf2a1edd
permissions -rw-r--r--
improved theory reference in comment
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
paulson@10766
   141
fun prep_pat s = Thm.read_cterm (Theory.sign_of (the_context ())) 
paulson@10766
   142
                                (s, HOLogic.termT);
wenzelm@9436
   143
val prep_pats = map prep_pat;
wenzelm@9436
   144
wenzelm@9436
   145
fun prep_simproc (name, pats, proc) = Simplifier.mk_simproc name pats proc;
wenzelm@9436
   146
paulson@10766
   147
val eq_pats = prep_pats ["(l::nat) + m = n", "(l::nat) = m + n", "Suc m = n", 
paulson@10766
   148
                         "m = Suc n"];
paulson@10766
   149
val less_pats = prep_pats ["(l::nat) + m < n", "(l::nat) < m + n", "Suc m < n",
paulson@10766
   150
                           "m < Suc n"];
paulson@10766
   151
val le_pats = prep_pats ["(l::nat) + m <= n", "(l::nat) <= m + n", 
paulson@10766
   152
                         "Suc m <= n", "m <= Suc n"];
paulson@10766
   153
val diff_pats = prep_pats ["((l::nat) + m) - n", "(l::nat) - (m + n)", 
paulson@10766
   154
                           "Suc m - n", "m - Suc n"];
wenzelm@9436
   155
wenzelm@9436
   156
val nat_cancel_sums_add = map prep_simproc
paulson@10766
   157
  [("nateq_cancel_sums",   eq_pats,   EqCancelSums.proc),
wenzelm@9436
   158
   ("natless_cancel_sums", less_pats, LessCancelSums.proc),
paulson@10766
   159
   ("natle_cancel_sums",   le_pats,   LeCancelSums.proc)];
wenzelm@9436
   160
wenzelm@9436
   161
val nat_cancel_sums = nat_cancel_sums_add @
wenzelm@9436
   162
  [prep_simproc("natdiff_cancel_sums", diff_pats, DiffCancelSums.proc)];
wenzelm@9436
   163
wenzelm@9436
   164
wenzelm@9436
   165
end;
wenzelm@9436
   166
wenzelm@9436
   167
open ArithData;
wenzelm@9436
   168
wenzelm@9436
   169
wenzelm@9436
   170
(*---------------------------------------------------------------------------*)
wenzelm@9436
   171
(* 2. Linear arithmetic                                                      *)
wenzelm@9436
   172
(*---------------------------------------------------------------------------*)
wenzelm@9436
   173
wenzelm@9436
   174
(* Parameters data for general linear arithmetic functor *)
wenzelm@9436
   175
wenzelm@9436
   176
structure LA_Logic: LIN_ARITH_LOGIC =
wenzelm@9436
   177
struct
wenzelm@9436
   178
val ccontr = ccontr;
wenzelm@9436
   179
val conjI = conjI;
wenzelm@9436
   180
val neqE = linorder_neqE;
wenzelm@9436
   181
val notI = notI;
wenzelm@9436
   182
val sym = sym;
wenzelm@9436
   183
val not_lessD = linorder_not_less RS iffD1;
wenzelm@9436
   184
val not_leD = linorder_not_le RS iffD1;
wenzelm@9436
   185
wenzelm@9436
   186
wenzelm@9436
   187
fun mk_Eq thm = (thm RS Eq_FalseI) handle THM _ => (thm RS Eq_TrueI);
wenzelm@9436
   188
wenzelm@9436
   189
val mk_Trueprop = HOLogic.mk_Trueprop;
wenzelm@9436
   190
wenzelm@9436
   191
fun neg_prop(TP$(Const("Not",_)$t)) = TP$t
wenzelm@9436
   192
  | neg_prop(TP$t) = TP $ (Const("Not",HOLogic.boolT-->HOLogic.boolT)$t);
wenzelm@9436
   193
wenzelm@9436
   194
fun is_False thm =
wenzelm@9436
   195
  let val _ $ t = #prop(rep_thm thm)
wenzelm@9436
   196
  in t = Const("False",HOLogic.boolT) end;
wenzelm@9436
   197
wenzelm@9436
   198
fun is_nat(t) = fastype_of1 t = HOLogic.natT;
wenzelm@9436
   199
wenzelm@9436
   200
fun mk_nat_thm sg t =
wenzelm@9436
   201
  let val ct = cterm_of sg t  and cn = cterm_of sg (Var(("n",0),HOLogic.natT))
wenzelm@9436
   202
  in instantiate ([],[(cn,ct)]) le0 end;
wenzelm@9436
   203
wenzelm@9436
   204
end;
wenzelm@9436
   205
wenzelm@9436
   206
wenzelm@9436
   207
(* arith theory data *)
wenzelm@9436
   208
wenzelm@9593
   209
structure ArithTheoryDataArgs =
wenzelm@9436
   210
struct
wenzelm@9436
   211
  val name = "HOL/arith";
nipkow@10574
   212
  type T = {splits: thm list, inj_consts: (string * typ)list, discrete: (string * bool) list};
wenzelm@9436
   213
nipkow@10574
   214
  val empty = {splits = [], inj_consts = [], discrete = []};
wenzelm@9436
   215
  val copy = I;
wenzelm@9436
   216
  val prep_ext = I;
nipkow@10574
   217
  fun merge ({splits= splits1, inj_consts= inj_consts1, discrete= discrete1},
nipkow@10574
   218
             {splits= splits2, inj_consts= inj_consts2, discrete= discrete2}) =
wenzelm@9436
   219
   {splits = Drule.merge_rules (splits1, splits2),
nipkow@10574
   220
    inj_consts = merge_lists inj_consts1 inj_consts2,
wenzelm@9436
   221
    discrete = merge_alists discrete1 discrete2};
wenzelm@9436
   222
  fun print _ _ = ();
wenzelm@9436
   223
end;
wenzelm@9436
   224
wenzelm@9593
   225
structure ArithTheoryData = TheoryDataFun(ArithTheoryDataArgs);
wenzelm@9436
   226
nipkow@10574
   227
fun arith_split_add (thy, thm) = (ArithTheoryData.map (fn {splits,inj_consts,discrete} =>
nipkow@10574
   228
  {splits= thm::splits, inj_consts= inj_consts, discrete= discrete}) thy, thm);
wenzelm@9436
   229
nipkow@10574
   230
fun arith_discrete d = ArithTheoryData.map (fn {splits,inj_consts,discrete} =>
nipkow@10574
   231
  {splits = splits, inj_consts = inj_consts, discrete = d :: discrete});
nipkow@10574
   232
nipkow@10574
   233
fun arith_inj_const c = ArithTheoryData.map (fn {splits,inj_consts,discrete} =>
nipkow@10574
   234
  {splits = splits, inj_consts = c :: inj_consts, discrete = discrete});
wenzelm@9436
   235
wenzelm@9436
   236
wenzelm@9436
   237
structure LA_Data_Ref: LIN_ARITH_DATA =
wenzelm@9436
   238
struct
wenzelm@9436
   239
wenzelm@9436
   240
(* Decomposition of terms *)
wenzelm@9436
   241
wenzelm@9436
   242
fun nT (Type("fun",[N,_])) = N = HOLogic.natT
wenzelm@9436
   243
  | nT _ = false;
wenzelm@9436
   244
wenzelm@9436
   245
fun add_atom(t,m,(p,i)) = (case assoc(p,t) of None => ((t,m)::p,i)
nipkow@10693
   246
                           | Some n => (overwrite(p,(t,ratadd(n,m))), i));
nipkow@10693
   247
nipkow@10693
   248
exception Zero;
wenzelm@9436
   249
nipkow@10693
   250
fun rat_of_term(numt,dent) =
nipkow@10693
   251
  let val num = HOLogic.dest_binum numt and den = HOLogic.dest_binum dent
nipkow@10693
   252
  in if den = 0 then raise Zero else int_ratdiv(num,den) end;
nipkow@10718
   253
nipkow@10718
   254
(* Warning: in rare cases number_of encloses a non-numeral,
nipkow@10718
   255
   in which case dest_binum raises TERM; hence all the handles below.
nipkow@10718
   256
*)
nipkow@10718
   257
nipkow@10718
   258
(* decompose nested multiplications, bracketing them to the right and combining all
nipkow@10718
   259
   their coefficients
nipkow@10718
   260
*)
nipkow@10718
   261
nipkow@10718
   262
fun demult((mC as Const("op *",_)) $ s $ t,m) = ((case s of
nipkow@10718
   263
        Const("Numeral.number_of",_)$n
nipkow@10718
   264
        => demult(t,ratmul(m,rat_of_int(HOLogic.dest_binum n)))
nipkow@10718
   265
      | Const("op *",_) $ s1 $ s2 => demult(mC $ s1 $ (mC $ s2 $ t),m)
nipkow@10718
   266
      | Const("HOL.divide",_) $ numt $ (Const("Numeral.number_of",_)$dent) =>
nipkow@10718
   267
          let val den = HOLogic.dest_binum dent
nipkow@10718
   268
          in if den = 0 then raise Zero
nipkow@10718
   269
             else demult(mC $ numt $ t,ratmul(m, ratinv(rat_of_int den)))
nipkow@10718
   270
          end
nipkow@10718
   271
      | _ => atomult(mC,s,t,m)
nipkow@10718
   272
      ) handle TERM _ => atomult(mC,s,t,m))
nipkow@10718
   273
  | demult(atom as Const("HOL.divide",_) $ t $ (Const("Numeral.number_of",_)$dent), m) =
nipkow@10718
   274
      (let val den = HOLogic.dest_binum dent
nipkow@10718
   275
       in if den = 0 then raise Zero else demult(t,ratmul(m, ratinv(rat_of_int den))) end
nipkow@10718
   276
       handle TERM _ => (Some atom,m))
nipkow@10718
   277
  | demult(t as Const("Numeral.number_of",_)$n,m) =
nipkow@10718
   278
      ((None,ratmul(m,rat_of_int(HOLogic.dest_binum n)))
nipkow@10718
   279
       handle TERM _ => (Some t,m))
nipkow@10718
   280
  | demult(atom,m) = (Some atom,m)
nipkow@10718
   281
nipkow@10718
   282
and atomult(mC,atom,t,m) = (case demult(t,m) of (None,m') => (Some atom,m')
nipkow@10718
   283
                            | (Some t',m') => (Some(mC $ atom $ t'),m'))
nipkow@10718
   284
nipkow@10574
   285
fun decomp2 inj_consts (rel,lhs,rhs) =
nipkow@10574
   286
let
wenzelm@9436
   287
(* Turn term into list of summand * multiplicity plus a constant *)
wenzelm@9436
   288
fun poly(Const("op +",_) $ s $ t, m, pi) = poly(s,m,poly(t,m,pi))
wenzelm@9436
   289
  | poly(all as Const("op -",T) $ s $ t, m, pi) =
wenzelm@9436
   290
      if nT T then add_atom(all,m,pi)
nipkow@10693
   291
      else poly(s,m,poly(t,ratneg m,pi))
nipkow@10693
   292
  | poly(Const("uminus",_) $ t, m, pi) = poly(t,ratneg m,pi)
wenzelm@9436
   293
  | poly(Const("0",_), _, pi) = pi
nipkow@10693
   294
  | poly(Const("Suc",_)$t, m, (p,i)) = poly(t, m, (p,ratadd(i,m)))
nipkow@10718
   295
  | poly(t as Const("op *",_) $ _ $ _, m, pi as (p,i)) =
nipkow@10718
   296
      (case demult(t,m) of
nipkow@10718
   297
         (None,m') => (p,ratadd(i,m))
nipkow@10718
   298
       | (Some u,m') => add_atom(u,m',pi))
nipkow@10718
   299
  | poly(t as Const("HOL.divide",_) $ _ $ _, m, pi as (p,i)) =
nipkow@10718
   300
      (case demult(t,m) of
nipkow@10718
   301
         (None,m') => (p,ratadd(i,m))
nipkow@10718
   302
       | (Some u,m') => add_atom(u,m',pi))
nipkow@10718
   303
  | poly(all as (Const("Numeral.number_of",_)$t,m,(p,i))) =
nipkow@10718
   304
      ((p,ratadd(i,ratmul(m,rat_of_int(HOLogic.dest_binum t))))
nipkow@10718
   305
       handle TERM _ => add_atom all)
nipkow@10574
   306
  | poly(all as Const f $ x, m, pi) =
nipkow@10574
   307
      if f mem inj_consts then poly(x,m,pi) else add_atom(all,m,pi)
wenzelm@9436
   308
  | poly x  = add_atom x;
wenzelm@9436
   309
nipkow@10718
   310
val (p,i) = poly(lhs,rat_of_int 1,([],rat_of_int 0))
nipkow@10718
   311
and (q,j) = poly(rhs,rat_of_int 1,([],rat_of_int 0))
nipkow@10693
   312
wenzelm@9436
   313
  in case rel of
wenzelm@9436
   314
       "op <"  => Some(p,i,"<",q,j)
wenzelm@9436
   315
     | "op <=" => Some(p,i,"<=",q,j)
wenzelm@9436
   316
     | "op ="  => Some(p,i,"=",q,j)
wenzelm@9436
   317
     | _       => None
nipkow@10693
   318
  end handle Zero => None;
wenzelm@9436
   319
wenzelm@9436
   320
fun negate(Some(x,i,rel,y,j,d)) = Some(x,i,"~"^rel,y,j,d)
wenzelm@9436
   321
  | negate None = None;
wenzelm@9436
   322
nipkow@10574
   323
fun decomp1 (discrete,inj_consts) (T,xxx) =
wenzelm@9436
   324
  (case T of
wenzelm@9436
   325
     Type("fun",[Type(D,[]),_]) =>
wenzelm@9436
   326
       (case assoc(discrete,D) of
wenzelm@9436
   327
          None => None
nipkow@10574
   328
        | Some d => (case decomp2 inj_consts xxx of
wenzelm@9436
   329
                       None => None
wenzelm@9436
   330
                     | Some(p,i,rel,q,j) => Some(p,i,rel,q,j,d)))
wenzelm@9436
   331
   | _ => None);
wenzelm@9436
   332
nipkow@10574
   333
fun decomp2 data (_$(Const(rel,T)$lhs$rhs)) = decomp1 data (T,(rel,lhs,rhs))
nipkow@10574
   334
  | decomp2 data (_$(Const("Not",_)$(Const(rel,T)$lhs$rhs))) =
nipkow@10574
   335
      negate(decomp1 data (T,(rel,lhs,rhs)))
nipkow@10574
   336
  | decomp2 data _ = None
wenzelm@9436
   337
nipkow@10574
   338
fun decomp sg =
nipkow@10574
   339
  let val {discrete, inj_consts, ...} = ArithTheoryData.get_sg sg
nipkow@10574
   340
  in decomp2 (discrete,inj_consts) end
wenzelm@9436
   341
nipkow@10693
   342
fun number_of(n,T) = HOLogic.number_of_const T $ (HOLogic.mk_bin n)
nipkow@10693
   343
wenzelm@9436
   344
end;
wenzelm@9436
   345
wenzelm@9436
   346
wenzelm@9436
   347
structure Fast_Arith =
wenzelm@9436
   348
  Fast_Lin_Arith(structure LA_Logic=LA_Logic and LA_Data=LA_Data_Ref);
wenzelm@9436
   349
wenzelm@9436
   350
val fast_arith_tac = Fast_Arith.lin_arith_tac
wenzelm@9436
   351
and trace_arith    = Fast_Arith.trace;
wenzelm@9436
   352
wenzelm@9436
   353
local
wenzelm@9436
   354
wenzelm@9436
   355
(* reduce contradictory <= to False.
wenzelm@9436
   356
   Most of the work is done by the cancel tactics.
wenzelm@9436
   357
*)
wenzelm@9436
   358
val add_rules = [add_0,add_0_right,Zero_not_Suc,Suc_not_Zero,le_0_eq];
wenzelm@9436
   359
wenzelm@9436
   360
val add_mono_thms_nat = map (fn s => prove_goal (the_context ()) s
wenzelm@9436
   361
 (fn prems => [cut_facts_tac prems 1,
wenzelm@9436
   362
               blast_tac (claset() addIs [add_le_mono]) 1]))
wenzelm@9436
   363
["(i <= j) & (k <= l) ==> i + k <= j + (l::nat)",
wenzelm@9436
   364
 "(i  = j) & (k <= l) ==> i + k <= j + (l::nat)",
wenzelm@9436
   365
 "(i <= j) & (k  = l) ==> i + k <= j + (l::nat)",
wenzelm@9436
   366
 "(i  = j) & (k  = l) ==> i + k  = j + (l::nat)"
wenzelm@9436
   367
];
wenzelm@9436
   368
wenzelm@9436
   369
in
wenzelm@9436
   370
wenzelm@9436
   371
val init_lin_arith_data =
wenzelm@9436
   372
 Fast_Arith.setup @
nipkow@10693
   373
 [Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, simpset = _} =>
wenzelm@9436
   374
   {add_mono_thms = add_mono_thms @ add_mono_thms_nat,
nipkow@10693
   375
    mult_mono_thms = mult_mono_thms,
nipkow@10574
   376
    inj_thms = inj_thms,
wenzelm@9436
   377
    lessD = lessD @ [Suc_leI],
wenzelm@9436
   378
    simpset = HOL_basic_ss addsimps add_rules addsimprocs nat_cancel_sums_add}),
wenzelm@9593
   379
  ArithTheoryData.init, arith_discrete ("nat", true)];
wenzelm@9436
   380
wenzelm@9436
   381
end;
wenzelm@9436
   382
wenzelm@9436
   383
wenzelm@9436
   384
local
wenzelm@9436
   385
val nat_arith_simproc_pats =
wenzelm@9436
   386
  map (fn s => Thm.read_cterm (Theory.sign_of (the_context ())) (s, HOLogic.boolT))
wenzelm@9436
   387
      ["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"];
wenzelm@9436
   388
in
wenzelm@9436
   389
val fast_nat_arith_simproc = mk_simproc
wenzelm@9436
   390
  "fast_nat_arith" nat_arith_simproc_pats Fast_Arith.lin_arith_prover;
wenzelm@9436
   391
end;
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
nipkow@10574
   416
val arith_tac = fast_arith_tac ORELSE' (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@9436
   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")]];