src/HOL/arith_data.ML
author webertj
Mon Mar 07 19:30:53 2005 +0100 (2005-03-07)
changeset 15584 3478bb4f93ff
parent 15570 8d8c70b41bab
child 15921 b6e345548913
permissions -rw-r--r--
refute_params: default value itself=1 added (for type classes)
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 =
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
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
skalberg@15531
    41
      SOME t => one :: dest_sum t
skalberg@15531
    42
    | NONE =>
wenzelm@9436
    43
        (case try dest_plus tm of
skalberg@15531
    44
          SOME (t, u) => dest_sum t @ dest_sum u
skalberg@15531
    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 =
skalberg@15531
    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;
paulson@14331
   109
  val uncancel_tac = gen_uncancel_tac nat_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;
paulson@14331
   120
  val uncancel_tac = gen_uncancel_tac nat_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;
paulson@14331
   131
  val uncancel_tac = gen_uncancel_tac nat_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@15185
   208
  type T = {splits: thm list, inj_consts: (string * typ)list, discrete: string  list, presburger: (int -> tactic) option};
wenzelm@9436
   209
skalberg@15531
   210
  val empty = {splits = [], inj_consts = [], discrete = [], presburger = NONE};
wenzelm@9436
   211
  val copy = I;
wenzelm@9436
   212
  val prep_ext = I;
berghofe@13877
   213
  fun merge ({splits= splits1, inj_consts= inj_consts1, discrete= discrete1, presburger= presburger1},
berghofe@13877
   214
             {splits= splits2, inj_consts= inj_consts2, discrete= discrete2, presburger= presburger2}) =
wenzelm@9436
   215
   {splits = Drule.merge_rules (splits1, splits2),
nipkow@10574
   216
    inj_consts = merge_lists inj_consts1 inj_consts2,
nipkow@15185
   217
    discrete = merge_lists discrete1 discrete2,
skalberg@15531
   218
    presburger = (case presburger1 of NONE => presburger2 | p => p)};
wenzelm@9436
   219
  fun print _ _ = ();
wenzelm@9436
   220
end;
wenzelm@9436
   221
wenzelm@9593
   222
structure ArithTheoryData = TheoryDataFun(ArithTheoryDataArgs);
wenzelm@9436
   223
berghofe@13877
   224
fun arith_split_add (thy, thm) = (ArithTheoryData.map (fn {splits,inj_consts,discrete,presburger} =>
berghofe@13877
   225
  {splits= thm::splits, inj_consts= inj_consts, discrete= discrete, presburger= presburger}) thy, thm);
wenzelm@9436
   226
berghofe@13877
   227
fun arith_discrete d = ArithTheoryData.map (fn {splits,inj_consts,discrete,presburger} =>
berghofe@13877
   228
  {splits = splits, inj_consts = inj_consts, discrete = d :: discrete, presburger= presburger});
nipkow@10574
   229
berghofe@13877
   230
fun arith_inj_const c = ArithTheoryData.map (fn {splits,inj_consts,discrete,presburger} =>
berghofe@13877
   231
  {splits = splits, inj_consts = c :: inj_consts, discrete = discrete, presburger = presburger});
wenzelm@9436
   232
wenzelm@9436
   233
wenzelm@9436
   234
structure LA_Data_Ref: LIN_ARITH_DATA =
wenzelm@9436
   235
struct
wenzelm@9436
   236
wenzelm@9436
   237
(* Decomposition of terms *)
wenzelm@9436
   238
wenzelm@9436
   239
fun nT (Type("fun",[N,_])) = N = HOLogic.natT
wenzelm@9436
   240
  | nT _ = false;
wenzelm@9436
   241
skalberg@15531
   242
fun add_atom(t,m,(p,i)) = (case assoc(p,t) of NONE => ((t,m)::p,i)
skalberg@15531
   243
                           | SOME n => (overwrite(p,(t,ratadd(n,m))), i));
nipkow@10693
   244
nipkow@10693
   245
exception Zero;
wenzelm@9436
   246
nipkow@10693
   247
fun rat_of_term(numt,dent) =
nipkow@10693
   248
  let val num = HOLogic.dest_binum numt and den = HOLogic.dest_binum dent
nipkow@10693
   249
  in if den = 0 then raise Zero else int_ratdiv(num,den) end;
nipkow@10718
   250
nipkow@10718
   251
(* Warning: in rare cases number_of encloses a non-numeral,
nipkow@10718
   252
   in which case dest_binum raises TERM; hence all the handles below.
nipkow@11334
   253
   Same for Suc-terms that turn out not to be numerals -
nipkow@11334
   254
   although the simplifier should eliminate those anyway...
nipkow@10718
   255
*)
nipkow@10718
   256
nipkow@11334
   257
fun number_of_Sucs (Const("Suc",_) $ n) = number_of_Sucs n + 1
nipkow@11334
   258
  | number_of_Sucs t = if HOLogic.is_zero t then 0
nipkow@11334
   259
                       else raise TERM("number_of_Sucs",[])
nipkow@11334
   260
nipkow@10718
   261
(* decompose nested multiplications, bracketing them to the right and combining all
nipkow@10718
   262
   their coefficients
nipkow@10718
   263
*)
nipkow@10718
   264
nipkow@13499
   265
fun demult inj_consts =
nipkow@13499
   266
let
nipkow@10718
   267
fun demult((mC as Const("op *",_)) $ s $ t,m) = ((case s of
nipkow@10718
   268
        Const("Numeral.number_of",_)$n
nipkow@10718
   269
        => demult(t,ratmul(m,rat_of_int(HOLogic.dest_binum n)))
nipkow@12480
   270
      | Const("uminus",_)$(Const("Numeral.number_of",_)$n)
nipkow@12480
   271
        => demult(t,ratmul(m,rat_of_int(~(HOLogic.dest_binum n))))
nipkow@11334
   272
      | Const("Suc",_) $ _
nipkow@11334
   273
        => demult(t,ratmul(m,rat_of_int(number_of_Sucs s)))
nipkow@10718
   274
      | Const("op *",_) $ s1 $ s2 => demult(mC $ s1 $ (mC $ s2 $ t),m)
nipkow@10718
   275
      | Const("HOL.divide",_) $ numt $ (Const("Numeral.number_of",_)$dent) =>
nipkow@10718
   276
          let val den = HOLogic.dest_binum dent
nipkow@10718
   277
          in if den = 0 then raise Zero
nipkow@10718
   278
             else demult(mC $ numt $ t,ratmul(m, ratinv(rat_of_int den)))
nipkow@10718
   279
          end
nipkow@10718
   280
      | _ => atomult(mC,s,t,m)
nipkow@10718
   281
      ) handle TERM _ => atomult(mC,s,t,m))
nipkow@10718
   282
  | demult(atom as Const("HOL.divide",_) $ t $ (Const("Numeral.number_of",_)$dent), m) =
nipkow@10718
   283
      (let val den = HOLogic.dest_binum dent
nipkow@10718
   284
       in if den = 0 then raise Zero else demult(t,ratmul(m, ratinv(rat_of_int den))) end
skalberg@15531
   285
       handle TERM _ => (SOME atom,m))
skalberg@15531
   286
  | demult(Const("0",_),m) = (NONE, rat_of_int 0)
skalberg@15531
   287
  | demult(Const("1",_),m) = (NONE, m)
nipkow@10718
   288
  | demult(t as Const("Numeral.number_of",_)$n,m) =
skalberg@15531
   289
      ((NONE,ratmul(m,rat_of_int(HOLogic.dest_binum n)))
skalberg@15531
   290
       handle TERM _ => (SOME t,m))
nipkow@12480
   291
  | demult(Const("uminus",_)$t, m) = demult(t,ratmul(m,rat_of_int(~1)))
nipkow@13499
   292
  | demult(t as Const f $ x, m) =
skalberg@15531
   293
      (if f mem inj_consts then SOME x else SOME t,m)
skalberg@15531
   294
  | demult(atom,m) = (SOME atom,m)
nipkow@10718
   295
skalberg@15531
   296
and atomult(mC,atom,t,m) = (case demult(t,m) of (NONE,m') => (SOME atom,m')
skalberg@15531
   297
                            | (SOME t',m') => (SOME(mC $ atom $ t'),m'))
nipkow@13499
   298
in demult end;
nipkow@10718
   299
nipkow@10574
   300
fun decomp2 inj_consts (rel,lhs,rhs) =
nipkow@10574
   301
let
wenzelm@9436
   302
(* Turn term into list of summand * multiplicity plus a constant *)
wenzelm@9436
   303
fun poly(Const("op +",_) $ s $ t, m, pi) = poly(s,m,poly(t,m,pi))
wenzelm@9436
   304
  | poly(all as Const("op -",T) $ s $ t, m, pi) =
wenzelm@9436
   305
      if nT T then add_atom(all,m,pi)
nipkow@10693
   306
      else poly(s,m,poly(t,ratneg m,pi))
nipkow@10693
   307
  | poly(Const("uminus",_) $ t, m, pi) = poly(t,ratneg m,pi)
wenzelm@9436
   308
  | poly(Const("0",_), _, pi) = pi
nipkow@11464
   309
  | poly(Const("1",_), m, (p,i)) = (p,ratadd(i,m))
nipkow@10693
   310
  | poly(Const("Suc",_)$t, m, (p,i)) = poly(t, m, (p,ratadd(i,m)))
nipkow@10718
   311
  | poly(t as Const("op *",_) $ _ $ _, m, pi as (p,i)) =
nipkow@13499
   312
      (case demult inj_consts (t,m) of
skalberg@15531
   313
         (NONE,m') => (p,ratadd(i,m))
skalberg@15531
   314
       | (SOME u,m') => add_atom(u,m',pi))
nipkow@10718
   315
  | poly(t as Const("HOL.divide",_) $ _ $ _, m, pi as (p,i)) =
nipkow@13499
   316
      (case demult inj_consts (t,m) of
skalberg@15531
   317
         (NONE,m') => (p,ratadd(i,m'))
skalberg@15531
   318
       | (SOME u,m') => add_atom(u,m',pi))
nipkow@10718
   319
  | poly(all as (Const("Numeral.number_of",_)$t,m,(p,i))) =
nipkow@10718
   320
      ((p,ratadd(i,ratmul(m,rat_of_int(HOLogic.dest_binum t))))
nipkow@10718
   321
       handle TERM _ => add_atom all)
nipkow@10574
   322
  | poly(all as Const f $ x, m, pi) =
nipkow@10574
   323
      if f mem inj_consts then poly(x,m,pi) else add_atom(all,m,pi)
wenzelm@9436
   324
  | poly x  = add_atom x;
wenzelm@9436
   325
nipkow@10718
   326
val (p,i) = poly(lhs,rat_of_int 1,([],rat_of_int 0))
nipkow@10718
   327
and (q,j) = poly(rhs,rat_of_int 1,([],rat_of_int 0))
nipkow@10693
   328
wenzelm@9436
   329
  in case rel of
skalberg@15531
   330
       "op <"  => SOME(p,i,"<",q,j)
skalberg@15531
   331
     | "op <=" => SOME(p,i,"<=",q,j)
skalberg@15531
   332
     | "op ="  => SOME(p,i,"=",q,j)
skalberg@15531
   333
     | _       => NONE
skalberg@15531
   334
  end handle Zero => NONE;
wenzelm@9436
   335
skalberg@15531
   336
fun negate(SOME(x,i,rel,y,j,d)) = SOME(x,i,"~"^rel,y,j,d)
skalberg@15531
   337
  | negate NONE = NONE;
wenzelm@9436
   338
nipkow@15121
   339
fun of_lin_arith_sort sg U =
nipkow@15121
   340
  Type.of_sort (Sign.tsig_of sg) (U,["Ring_and_Field.ordered_idom"])
nipkow@15121
   341
nipkow@15121
   342
fun allows_lin_arith sg discrete (U as Type(D,[])) =
nipkow@15121
   343
      if of_lin_arith_sort sg U
nipkow@15185
   344
      then (true, D mem discrete)
nipkow@15121
   345
      else (* special cases *)
nipkow@15185
   346
           if D mem discrete then (true,true) else (false,false)
nipkow@15121
   347
  | allows_lin_arith sg discrete U = (of_lin_arith_sort sg U, false);
nipkow@15121
   348
nipkow@15121
   349
fun decomp1 (sg,discrete,inj_consts) (T,xxx) =
wenzelm@9436
   350
  (case T of
nipkow@15121
   351
     Type("fun",[U,_]) =>
nipkow@15121
   352
       (case allows_lin_arith sg discrete U of
skalberg@15531
   353
          (true,d) => (case decomp2 inj_consts xxx of NONE => NONE
skalberg@15531
   354
                       | SOME(p,i,rel,q,j) => SOME(p,i,rel,q,j,d))
skalberg@15531
   355
        | (false,_) => NONE)
skalberg@15531
   356
   | _ => NONE);
wenzelm@9436
   357
nipkow@10574
   358
fun decomp2 data (_$(Const(rel,T)$lhs$rhs)) = decomp1 data (T,(rel,lhs,rhs))
nipkow@10574
   359
  | decomp2 data (_$(Const("Not",_)$(Const(rel,T)$lhs$rhs))) =
nipkow@10574
   360
      negate(decomp1 data (T,(rel,lhs,rhs)))
skalberg@15531
   361
  | decomp2 data _ = NONE
wenzelm@9436
   362
nipkow@10574
   363
fun decomp sg =
nipkow@10574
   364
  let val {discrete, inj_consts, ...} = ArithTheoryData.get_sg sg
nipkow@15121
   365
  in decomp2 (sg,discrete,inj_consts) end
wenzelm@9436
   366
nipkow@10693
   367
fun number_of(n,T) = HOLogic.number_of_const T $ (HOLogic.mk_bin n)
nipkow@10693
   368
wenzelm@9436
   369
end;
wenzelm@9436
   370
wenzelm@9436
   371
wenzelm@9436
   372
structure Fast_Arith =
wenzelm@9436
   373
  Fast_Lin_Arith(structure LA_Logic=LA_Logic and LA_Data=LA_Data_Ref);
wenzelm@9436
   374
nipkow@13499
   375
val fast_arith_tac    = Fast_Arith.lin_arith_tac false
nipkow@13499
   376
and fast_ex_arith_tac = Fast_Arith.lin_arith_tac
nipkow@14517
   377
and trace_arith    = Fast_Arith.trace
nipkow@14517
   378
and fast_arith_neq_limit = Fast_Arith.fast_arith_neq_limit;
wenzelm@9436
   379
wenzelm@9436
   380
local
wenzelm@9436
   381
nipkow@13902
   382
val isolateSuc =
nipkow@13902
   383
  let val thy = theory "Nat"
nipkow@13902
   384
  in prove_goal thy "Suc(i+j) = i+j + Suc 0"
nipkow@13902
   385
     (fn _ => [simp_tac (simpset_of thy) 1])
nipkow@13902
   386
  end;
nipkow@13902
   387
wenzelm@9436
   388
(* reduce contradictory <= to False.
wenzelm@9436
   389
   Most of the work is done by the cancel tactics.
wenzelm@9436
   390
*)
nipkow@12931
   391
val add_rules =
paulson@14368
   392
 [add_zero_left,add_zero_right,Zero_not_Suc,Suc_not_Zero,le_0_eq,
nipkow@15184
   393
  One_nat_def,isolateSuc,
nipkow@15184
   394
  order_less_irrefl];
wenzelm@9436
   395
paulson@14368
   396
val add_mono_thms_ordered_semiring = map (fn s => prove_goal (the_context ()) s
wenzelm@9436
   397
 (fn prems => [cut_facts_tac prems 1,
paulson@14368
   398
               blast_tac (claset() addIs [add_mono]) 1]))
nipkow@15121
   399
["(i <= j) & (k <= l) ==> i + k <= j + (l::'a::pordered_ab_semigroup_add)",
nipkow@15121
   400
 "(i  = j) & (k <= l) ==> i + k <= j + (l::'a::pordered_ab_semigroup_add)",
nipkow@15121
   401
 "(i <= j) & (k  = l) ==> i + k <= j + (l::'a::pordered_ab_semigroup_add)",
nipkow@15121
   402
 "(i  = j) & (k  = l) ==> i + k  = j + (l::'a::pordered_ab_semigroup_add)"
wenzelm@9436
   403
];
wenzelm@9436
   404
nipkow@15121
   405
val mono_ss = simpset() addsimps
nipkow@15121
   406
                [add_mono,add_strict_mono,add_less_le_mono,add_le_less_mono];
nipkow@15121
   407
nipkow@15121
   408
val add_mono_thms_ordered_field =
nipkow@15121
   409
  map (fn s => prove_goal (the_context ()) s
nipkow@15121
   410
                 (fn prems => [cut_facts_tac prems 1, asm_simp_tac mono_ss 1]))
nipkow@15121
   411
    ["(i<j) & (k=l)   ==> i+k < j+(l::'a::pordered_cancel_ab_semigroup_add)",
nipkow@15121
   412
     "(i=j) & (k<l)   ==> i+k < j+(l::'a::pordered_cancel_ab_semigroup_add)",
nipkow@15121
   413
     "(i<j) & (k<=l)  ==> i+k < j+(l::'a::pordered_cancel_ab_semigroup_add)",
nipkow@15121
   414
     "(i<=j) & (k<l)  ==> i+k < j+(l::'a::pordered_cancel_ab_semigroup_add)",
nipkow@15121
   415
     "(i<j) & (k<l)   ==> i+k < j+(l::'a::pordered_cancel_ab_semigroup_add)"];
nipkow@15121
   416
wenzelm@9436
   417
in
wenzelm@9436
   418
wenzelm@9436
   419
val init_lin_arith_data =
wenzelm@9436
   420
 Fast_Arith.setup @
nipkow@10693
   421
 [Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, simpset = _} =>
nipkow@15121
   422
   {add_mono_thms = add_mono_thms @
nipkow@15121
   423
    add_mono_thms_ordered_semiring @ add_mono_thms_ordered_field,
nipkow@10693
   424
    mult_mono_thms = mult_mono_thms,
nipkow@10574
   425
    inj_thms = inj_thms,
wenzelm@9436
   426
    lessD = lessD @ [Suc_leI],
paulson@15234
   427
    simpset = HOL_basic_ss addsimps add_rules
paulson@15234
   428
                   addsimprocs [ab_group_add_cancel.sum_conv, 
paulson@15234
   429
                                ab_group_add_cancel.rel_conv]
paulson@15234
   430
                   (*abel_cancel helps it work in abstract algebraic domains*)
paulson@15234
   431
                   addsimprocs nat_cancel_sums_add}),
nipkow@15185
   432
  ArithTheoryData.init, arith_discrete "nat"];
wenzelm@9436
   433
wenzelm@9436
   434
end;
wenzelm@9436
   435
wenzelm@13462
   436
val fast_nat_arith_simproc =
wenzelm@13462
   437
  Simplifier.simproc (Theory.sign_of (the_context ())) "fast_nat_arith"
wenzelm@13462
   438
    ["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"] Fast_Arith.lin_arith_prover;
wenzelm@9436
   439
wenzelm@9436
   440
wenzelm@9436
   441
(* Because of fast_nat_arith_simproc, the arithmetic solver is really only
wenzelm@9436
   442
useful to detect inconsistencies among the premises for subgoals which are
wenzelm@9436
   443
*not* themselves (in)equalities, because the latter activate
wenzelm@9436
   444
fast_nat_arith_simproc anyway. However, it seems cheaper to activate the
wenzelm@9436
   445
solver all the time rather than add the additional check. *)
wenzelm@9436
   446
wenzelm@9436
   447
wenzelm@9436
   448
(* arith proof method *)
wenzelm@9436
   449
wenzelm@9436
   450
(* FIXME: K true should be replaced by a sensible test to speed things up
wenzelm@9436
   451
   in case there are lots of irrelevant terms involved;
wenzelm@9436
   452
   elimination of min/max can be optimized:
wenzelm@9436
   453
   (max m n + k <= r) = (m+k <= r & n+k <= r)
wenzelm@9436
   454
   (l <= min m n + k) = (l <= m+k & l <= n+k)
wenzelm@9436
   455
*)
wenzelm@10516
   456
local
wenzelm@10516
   457
nipkow@13499
   458
fun raw_arith_tac ex i st =
nipkow@13499
   459
  refute_tac (K true)
nipkow@13499
   460
   (REPEAT o split_tac (#splits (ArithTheoryData.get_sg (Thm.sign_of_thm st))))
nipkow@14509
   461
   ((REPEAT_DETERM o etac linorder_neqE) THEN' fast_ex_arith_tac ex)
nipkow@14509
   462
   i st;
wenzelm@9436
   463
berghofe@13877
   464
fun presburger_tac i st =
berghofe@13877
   465
  (case ArithTheoryData.get_sg (sign_of_thm st) of
skalberg@15531
   466
     {presburger = SOME tac, ...} =>
berghofe@13877
   467
       (tracing "Simple arithmetic decision procedure failed.\nNow trying full Presburger arithmetic..."; tac i st)
berghofe@13877
   468
   | _ => no_tac st);
berghofe@13877
   469
wenzelm@10516
   470
in
wenzelm@10516
   471
berghofe@13877
   472
val simple_arith_tac = FIRST' [fast_arith_tac,
berghofe@13877
   473
  ObjectLogic.atomize_tac THEN' raw_arith_tac true];
berghofe@13877
   474
berghofe@13877
   475
val arith_tac = FIRST' [fast_arith_tac,
berghofe@13877
   476
  ObjectLogic.atomize_tac THEN' raw_arith_tac true,
berghofe@13877
   477
  presburger_tac];
berghofe@13877
   478
berghofe@13877
   479
val silent_arith_tac = FIRST' [fast_arith_tac,
berghofe@13877
   480
  ObjectLogic.atomize_tac THEN' raw_arith_tac false,
berghofe@13877
   481
  presburger_tac];
wenzelm@10516
   482
wenzelm@9436
   483
fun arith_method prems =
wenzelm@9436
   484
  Method.METHOD (fn facts => HEADGOAL (Method.insert_tac (prems @ facts) THEN' arith_tac));
wenzelm@9436
   485
wenzelm@10516
   486
end;
wenzelm@10516
   487
nipkow@15195
   488
(* antisymmetry:
nipkow@15197
   489
   combines x <= y (or ~(y < x)) and y <= x (or ~(x < y)) into x = y
nipkow@15195
   490
nipkow@15195
   491
local
nipkow@15195
   492
val antisym = mk_meta_eq order_antisym
nipkow@15195
   493
val not_lessD = linorder_not_less RS iffD1
nipkow@15195
   494
fun prp t thm = (#prop(rep_thm thm) = t)
nipkow@15195
   495
in
nipkow@15195
   496
fun antisym_eq prems thm =
nipkow@15195
   497
  let
nipkow@15195
   498
    val r = #prop(rep_thm thm);
nipkow@15195
   499
  in
nipkow@15195
   500
    case r of
nipkow@15195
   501
      Tr $ ((c as Const("op <=",T)) $ s $ t) =>
nipkow@15195
   502
        let val r' = Tr $ (c $ t $ s)
nipkow@15195
   503
        in
nipkow@15195
   504
          case Library.find_first (prp r') prems of
skalberg@15531
   505
            NONE =>
nipkow@15195
   506
              let val r' = Tr $ (HOLogic.not_const $ (Const("op <",T) $ s $ t))
nipkow@15195
   507
              in case Library.find_first (prp r') prems of
skalberg@15531
   508
                   NONE => []
skalberg@15531
   509
                 | SOME thm' => [(thm' RS not_lessD) RS (thm RS antisym)]
nipkow@15195
   510
              end
skalberg@15531
   511
          | SOME thm' => [thm' RS (thm RS antisym)]
nipkow@15195
   512
        end
nipkow@15195
   513
    | Tr $ (Const("Not",_) $ (Const("op <",T) $ s $ t)) =>
nipkow@15195
   514
        let val r' = Tr $ (Const("op <=",T) $ s $ t)
nipkow@15195
   515
        in
nipkow@15195
   516
          case Library.find_first (prp r') prems of
skalberg@15531
   517
            NONE =>
nipkow@15195
   518
              let val r' = Tr $ (HOLogic.not_const $ (Const("op <",T) $ t $ s))
nipkow@15195
   519
              in case Library.find_first (prp r') prems of
skalberg@15531
   520
                   NONE => []
skalberg@15531
   521
                 | SOME thm' =>
nipkow@15195
   522
                     [(thm' RS not_lessD) RS ((thm RS not_lessD) RS antisym)]
nipkow@15195
   523
              end
skalberg@15531
   524
          | SOME thm' => [thm' RS ((thm RS not_lessD) RS antisym)]
nipkow@15195
   525
        end
nipkow@15195
   526
    | _ => []
nipkow@15195
   527
  end
nipkow@15195
   528
  handle THM _ => []
nipkow@15195
   529
end;
nipkow@15197
   530
*)
wenzelm@9436
   531
wenzelm@9436
   532
(* theory setup *)
wenzelm@9436
   533
wenzelm@9436
   534
val arith_setup =
nipkow@15197
   535
 [Simplifier.change_simpset_of (op addsimprocs) nat_cancel_sums] @
wenzelm@9436
   536
  init_lin_arith_data @
wenzelm@9436
   537
  [Simplifier.change_simpset_of (op addSolver)
wenzelm@9436
   538
   (mk_solver "lin. arith." Fast_Arith.cut_lin_arith_tac),
wenzelm@9436
   539
  Simplifier.change_simpset_of (op addsimprocs) [fast_nat_arith_simproc],
paulson@15221
   540
  Method.add_methods
paulson@15221
   541
      [("arith", (arith_method o #2) oo Method.syntax Args.bang_facts,
paulson@15221
   542
	"decide linear arithmethic")],
wenzelm@9436
   543
  Attrib.add_attributes [("arith_split",
paulson@15221
   544
    (Attrib.no_args arith_split_add, 
paulson@15221
   545
     Attrib.no_args Attrib.undef_local_attribute),
wenzelm@9893
   546
    "declaration of split rules for arithmetic procedure")]];