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