src/HOL/arith_data.ML
author wenzelm
Tue Oct 25 18:38:21 2005 +0200 (2005-10-25)
changeset 17989 fa751791be4d
parent 17985 d5d576b72371
child 18328 841261f303a1
permissions -rw-r--r--
EVERY;
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@17985
    52
fun prove_conv expand_tac norm_tac sg ss tu =  (* FIXME avoid standard *)
wenzelm@17985
    53
  mk_meta_eq (standard (Goal.prove sg [] [] (HOLogic.mk_Trueprop (HOLogic.mk_eq tu))
wenzelm@17989
    54
    (K (EVERY [expand_tac, norm_tac ss]))));
wenzelm@9436
    55
wenzelm@9436
    56
val subst_equals = prove_goal HOL.thy "[| t = s; u = t |] ==> u = s"
wenzelm@9436
    57
  (fn prems => [cut_facts_tac prems 1, SIMPSET' asm_simp_tac 1]);
wenzelm@9436
    58
wenzelm@9436
    59
wenzelm@9436
    60
(* rewriting *)
wenzelm@9436
    61
wenzelm@17611
    62
fun simp_all_tac rules ss =
wenzelm@17875
    63
  ALLGOALS (simp_tac (Simplifier.inherit_context ss HOL_ss addsimps rules));
wenzelm@9436
    64
wenzelm@9436
    65
val add_rules = [add_Suc, add_Suc_right, add_0, add_0_right];
wenzelm@9436
    66
val mult_rules = [mult_Suc, mult_Suc_right, mult_0, mult_0_right];
wenzelm@9436
    67
nipkow@13517
    68
fun prep_simproc (name, pats, proc) =
wenzelm@16834
    69
  Simplifier.simproc (the_context ()) name pats proc;
nipkow@13517
    70
nipkow@13517
    71
end;
nipkow@13517
    72
nipkow@13517
    73
signature ARITH_DATA =
nipkow@13517
    74
sig
nipkow@13517
    75
  val nat_cancel_sums_add: simproc list
nipkow@13517
    76
  val nat_cancel_sums: simproc list
nipkow@13517
    77
end;
nipkow@13517
    78
nipkow@13517
    79
structure ArithData: ARITH_DATA =
nipkow@13517
    80
struct
nipkow@13517
    81
nipkow@13517
    82
open NatArithUtils;
wenzelm@9436
    83
wenzelm@9436
    84
wenzelm@9436
    85
(** cancel common summands **)
wenzelm@9436
    86
wenzelm@9436
    87
structure Sum =
wenzelm@9436
    88
struct
wenzelm@9436
    89
  val mk_sum = mk_norm_sum;
wenzelm@9436
    90
  val dest_sum = dest_sum;
wenzelm@9436
    91
  val prove_conv = prove_conv;
wenzelm@17611
    92
  fun norm_tac ss = simp_all_tac add_rules ss THEN simp_all_tac add_ac ss;
wenzelm@9436
    93
end;
wenzelm@9436
    94
wenzelm@9436
    95
fun gen_uncancel_tac rule ct =
skalberg@15531
    96
  rtac (instantiate' [] [NONE, SOME ct] (rule RS subst_equals)) 1;
wenzelm@9436
    97
wenzelm@9436
    98
wenzelm@9436
    99
(* nat eq *)
wenzelm@9436
   100
wenzelm@9436
   101
structure EqCancelSums = CancelSumsFun
wenzelm@9436
   102
(struct
wenzelm@9436
   103
  open Sum;
wenzelm@9436
   104
  val mk_bal = HOLogic.mk_eq;
wenzelm@9436
   105
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT;
paulson@14331
   106
  val uncancel_tac = gen_uncancel_tac nat_add_left_cancel;
wenzelm@9436
   107
end);
wenzelm@9436
   108
wenzelm@9436
   109
wenzelm@9436
   110
(* nat less *)
wenzelm@9436
   111
wenzelm@9436
   112
structure LessCancelSums = CancelSumsFun
wenzelm@9436
   113
(struct
wenzelm@9436
   114
  open Sum;
wenzelm@9436
   115
  val mk_bal = HOLogic.mk_binrel "op <";
wenzelm@9436
   116
  val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT;
paulson@14331
   117
  val uncancel_tac = gen_uncancel_tac nat_add_left_cancel_less;
wenzelm@9436
   118
end);
wenzelm@9436
   119
wenzelm@9436
   120
wenzelm@9436
   121
(* nat le *)
wenzelm@9436
   122
wenzelm@9436
   123
structure LeCancelSums = CancelSumsFun
wenzelm@9436
   124
(struct
wenzelm@9436
   125
  open Sum;
wenzelm@9436
   126
  val mk_bal = HOLogic.mk_binrel "op <=";
wenzelm@9436
   127
  val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT;
paulson@14331
   128
  val uncancel_tac = gen_uncancel_tac nat_add_left_cancel_le;
wenzelm@9436
   129
end);
wenzelm@9436
   130
wenzelm@9436
   131
wenzelm@9436
   132
(* nat diff *)
wenzelm@9436
   133
wenzelm@9436
   134
structure DiffCancelSums = CancelSumsFun
wenzelm@9436
   135
(struct
wenzelm@9436
   136
  open Sum;
wenzelm@9436
   137
  val mk_bal = HOLogic.mk_binop "op -";
wenzelm@9436
   138
  val dest_bal = HOLogic.dest_bin "op -" HOLogic.natT;
wenzelm@9436
   139
  val uncancel_tac = gen_uncancel_tac diff_cancel;
wenzelm@9436
   140
end);
wenzelm@9436
   141
wenzelm@9436
   142
wenzelm@9436
   143
wenzelm@9436
   144
(** prepare nat_cancel simprocs **)
wenzelm@9436
   145
wenzelm@9436
   146
val nat_cancel_sums_add = map prep_simproc
wenzelm@13462
   147
  [("nateq_cancel_sums",
wenzelm@13462
   148
     ["(l::nat) + m = n", "(l::nat) = m + n", "Suc m = n", "m = Suc n"], EqCancelSums.proc),
wenzelm@13462
   149
   ("natless_cancel_sums",
wenzelm@13462
   150
     ["(l::nat) + m < n", "(l::nat) < m + n", "Suc m < n", "m < Suc n"], LessCancelSums.proc),
wenzelm@13462
   151
   ("natle_cancel_sums",
wenzelm@13462
   152
     ["(l::nat) + m <= n", "(l::nat) <= m + n", "Suc m <= n", "m <= Suc n"], LeCancelSums.proc)];
wenzelm@9436
   153
wenzelm@9436
   154
val nat_cancel_sums = nat_cancel_sums_add @
wenzelm@13462
   155
  [prep_simproc ("natdiff_cancel_sums",
wenzelm@13462
   156
    ["((l::nat) + m) - n", "(l::nat) - (m + n)", "Suc m - n", "m - Suc n"], DiffCancelSums.proc)];
wenzelm@9436
   157
wenzelm@9436
   158
end;
wenzelm@9436
   159
wenzelm@9436
   160
open ArithData;
wenzelm@9436
   161
wenzelm@9436
   162
wenzelm@9436
   163
(*---------------------------------------------------------------------------*)
wenzelm@9436
   164
(* 2. Linear arithmetic                                                      *)
wenzelm@9436
   165
(*---------------------------------------------------------------------------*)
wenzelm@9436
   166
wenzelm@9436
   167
(* Parameters data for general linear arithmetic functor *)
wenzelm@9436
   168
wenzelm@9436
   169
structure LA_Logic: LIN_ARITH_LOGIC =
wenzelm@9436
   170
struct
wenzelm@9436
   171
val ccontr = ccontr;
wenzelm@9436
   172
val conjI = conjI;
wenzelm@9436
   173
val notI = notI;
wenzelm@9436
   174
val sym = sym;
wenzelm@9436
   175
val not_lessD = linorder_not_less RS iffD1;
wenzelm@9436
   176
val not_leD = linorder_not_le RS iffD1;
wenzelm@9436
   177
wenzelm@9436
   178
wenzelm@9436
   179
fun mk_Eq thm = (thm RS Eq_FalseI) handle THM _ => (thm RS Eq_TrueI);
wenzelm@9436
   180
wenzelm@9436
   181
val mk_Trueprop = HOLogic.mk_Trueprop;
wenzelm@9436
   182
nipkow@16733
   183
fun atomize thm = case #prop(rep_thm thm) of
nipkow@16733
   184
    Const("Trueprop",_) $ (Const("op &",_) $ _ $ _) =>
nipkow@16733
   185
    atomize(thm RS conjunct1) @ atomize(thm RS conjunct2)
nipkow@16733
   186
  | _ => [thm];
nipkow@16733
   187
wenzelm@9436
   188
fun neg_prop(TP$(Const("Not",_)$t)) = TP$t
wenzelm@9436
   189
  | neg_prop(TP$t) = TP $ (Const("Not",HOLogic.boolT-->HOLogic.boolT)$t);
wenzelm@9436
   190
wenzelm@9436
   191
fun is_False thm =
wenzelm@9436
   192
  let val _ $ t = #prop(rep_thm thm)
wenzelm@9436
   193
  in t = Const("False",HOLogic.boolT) end;
wenzelm@9436
   194
wenzelm@9436
   195
fun is_nat(t) = fastype_of1 t = HOLogic.natT;
wenzelm@9436
   196
wenzelm@9436
   197
fun mk_nat_thm sg t =
wenzelm@9436
   198
  let val ct = cterm_of sg t  and cn = cterm_of sg (Var(("n",0),HOLogic.natT))
wenzelm@9436
   199
  in instantiate ([],[(cn,ct)]) le0 end;
wenzelm@9436
   200
wenzelm@9436
   201
end;
wenzelm@9436
   202
wenzelm@9436
   203
wenzelm@9436
   204
(* arith theory data *)
wenzelm@9436
   205
wenzelm@16424
   206
structure ArithTheoryData = TheoryDataFun
wenzelm@16424
   207
(struct
wenzelm@9436
   208
  val name = "HOL/arith";
nipkow@15185
   209
  type T = {splits: thm list, inj_consts: (string * typ)list, discrete: string  list, presburger: (int -> tactic) option};
wenzelm@9436
   210
skalberg@15531
   211
  val empty = {splits = [], inj_consts = [], discrete = [], presburger = NONE};
wenzelm@9436
   212
  val copy = I;
wenzelm@16424
   213
  val extend = I;
wenzelm@16424
   214
  fun merge _ ({splits= splits1, inj_consts= inj_consts1, discrete= discrete1, presburger= presburger1},
berghofe@13877
   215
             {splits= splits2, inj_consts= inj_consts2, discrete= discrete2, presburger= presburger2}) =
wenzelm@9436
   216
   {splits = Drule.merge_rules (splits1, splits2),
nipkow@10574
   217
    inj_consts = merge_lists inj_consts1 inj_consts2,
nipkow@15185
   218
    discrete = merge_lists discrete1 discrete2,
skalberg@15531
   219
    presburger = (case presburger1 of NONE => presburger2 | p => p)};
wenzelm@9436
   220
  fun print _ _ = ();
wenzelm@16424
   221
end);
wenzelm@9436
   222
berghofe@13877
   223
fun arith_split_add (thy, thm) = (ArithTheoryData.map (fn {splits,inj_consts,discrete,presburger} =>
berghofe@13877
   224
  {splits= thm::splits, inj_consts= inj_consts, discrete= discrete, presburger= presburger}) thy, thm);
wenzelm@9436
   225
berghofe@13877
   226
fun arith_discrete d = ArithTheoryData.map (fn {splits,inj_consts,discrete,presburger} =>
berghofe@13877
   227
  {splits = splits, inj_consts = inj_consts, discrete = d :: discrete, presburger= presburger});
nipkow@10574
   228
berghofe@13877
   229
fun arith_inj_const c = ArithTheoryData.map (fn {splits,inj_consts,discrete,presburger} =>
berghofe@13877
   230
  {splits = splits, inj_consts = c :: inj_consts, discrete = discrete, presburger = presburger});
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
haftmann@17325
   241
fun add_atom(t,m,(p,i)) = (case AList.lookup (op =) p t of NONE => ((t, m) :: p, i)
haftmann@17951
   242
                           | SOME n => (AList.update (op =) (t, Rat.add (n, m)) p, i));
nipkow@10693
   243
nipkow@10693
   244
exception Zero;
wenzelm@9436
   245
haftmann@17951
   246
fun rat_of_term (numt,dent) =
nipkow@10693
   247
  let val num = HOLogic.dest_binum numt and den = HOLogic.dest_binum dent
haftmann@17951
   248
  in if den = 0 then raise Zero else Rat.rat_of_quotient (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
haftmann@17951
   268
        => demult(t,Rat.mult(m,Rat.rat_of_intinf(HOLogic.dest_binum n)))
nipkow@12480
   269
      | Const("uminus",_)$(Const("Numeral.number_of",_)$n)
haftmann@17951
   270
        => demult(t,Rat.mult(m,Rat.rat_of_intinf(~(HOLogic.dest_binum n))))
nipkow@11334
   271
      | Const("Suc",_) $ _
haftmann@17951
   272
        => demult(t,Rat.mult(m,Rat.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
haftmann@17951
   277
             else demult(mC $ numt $ t,Rat.mult(m, Rat.inv(Rat.rat_of_intinf 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
haftmann@17951
   283
       in if den = 0 then raise Zero else demult(t,Rat.mult(m, Rat.inv(Rat.rat_of_intinf den))) end
skalberg@15531
   284
       handle TERM _ => (SOME atom,m))
haftmann@17951
   285
  | demult(Const("0",_),m) = (NONE, Rat.rat_of_int 0)
skalberg@15531
   286
  | demult(Const("1",_),m) = (NONE, m)
nipkow@10718
   287
  | demult(t as Const("Numeral.number_of",_)$n,m) =
haftmann@17951
   288
      ((NONE,Rat.mult(m,Rat.rat_of_intinf(HOLogic.dest_binum n)))
skalberg@15531
   289
       handle TERM _ => (SOME t,m))
haftmann@17951
   290
  | demult(Const("uminus",_)$t, m) = demult(t,Rat.mult(m,Rat.rat_of_int(~1)))
nipkow@13499
   291
  | demult(t as Const f $ x, m) =
skalberg@15531
   292
      (if f mem inj_consts then SOME x else SOME t,m)
skalberg@15531
   293
  | demult(atom,m) = (SOME atom,m)
nipkow@10718
   294
skalberg@15531
   295
and atomult(mC,atom,t,m) = (case demult(t,m) of (NONE,m') => (SOME atom,m')
skalberg@15531
   296
                            | (SOME t',m') => (SOME(mC $ atom $ t'),m'))
nipkow@13499
   297
in demult end;
nipkow@10718
   298
nipkow@10574
   299
fun decomp2 inj_consts (rel,lhs,rhs) =
nipkow@10574
   300
let
wenzelm@9436
   301
(* Turn term into list of summand * multiplicity plus a constant *)
wenzelm@9436
   302
fun poly(Const("op +",_) $ s $ t, m, pi) = poly(s,m,poly(t,m,pi))
wenzelm@9436
   303
  | poly(all as Const("op -",T) $ s $ t, m, pi) =
haftmann@17951
   304
      if nT T then add_atom(all,m,pi) else poly(s,m,poly(t,Rat.neg m,pi))
nipkow@15958
   305
  | poly(all as Const("uminus",T) $ t, m, pi) =
haftmann@17951
   306
      if nT T then add_atom(all,m,pi) else poly(t,Rat.neg m,pi)
wenzelm@9436
   307
  | poly(Const("0",_), _, pi) = pi
haftmann@17951
   308
  | poly(Const("1",_), m, (p,i)) = (p,Rat.add(i,m))
haftmann@17951
   309
  | poly(Const("Suc",_)$t, m, (p,i)) = poly(t, m, (p,Rat.add(i,m)))
nipkow@10718
   310
  | poly(t as Const("op *",_) $ _ $ _, m, pi as (p,i)) =
nipkow@13499
   311
      (case demult inj_consts (t,m) of
haftmann@17951
   312
         (NONE,m') => (p,Rat.add(i,m))
skalberg@15531
   313
       | (SOME u,m') => add_atom(u,m',pi))
nipkow@10718
   314
  | poly(t as Const("HOL.divide",_) $ _ $ _, m, pi as (p,i)) =
nipkow@13499
   315
      (case demult inj_consts (t,m) of
haftmann@17951
   316
         (NONE,m') => (p,Rat.add(i,m'))
skalberg@15531
   317
       | (SOME u,m') => add_atom(u,m',pi))
nipkow@10718
   318
  | poly(all as (Const("Numeral.number_of",_)$t,m,(p,i))) =
haftmann@17951
   319
      ((p,Rat.add(i,Rat.mult(m,Rat.rat_of_intinf(HOLogic.dest_binum t))))
nipkow@10718
   320
       handle TERM _ => add_atom all)
nipkow@10574
   321
  | poly(all as Const f $ x, m, pi) =
nipkow@10574
   322
      if f mem inj_consts then poly(x,m,pi) else add_atom(all,m,pi)
wenzelm@9436
   323
  | poly x  = add_atom x;
wenzelm@9436
   324
haftmann@17951
   325
val (p,i) = poly(lhs,Rat.rat_of_int 1,([],Rat.rat_of_int 0))
haftmann@17951
   326
and (q,j) = poly(rhs,Rat.rat_of_int 1,([],Rat.rat_of_int 0))
nipkow@10693
   327
wenzelm@9436
   328
  in case rel of
skalberg@15531
   329
       "op <"  => SOME(p,i,"<",q,j)
skalberg@15531
   330
     | "op <=" => SOME(p,i,"<=",q,j)
skalberg@15531
   331
     | "op ="  => SOME(p,i,"=",q,j)
skalberg@15531
   332
     | _       => NONE
skalberg@15531
   333
  end handle Zero => NONE;
wenzelm@9436
   334
skalberg@15531
   335
fun negate(SOME(x,i,rel,y,j,d)) = SOME(x,i,"~"^rel,y,j,d)
skalberg@15531
   336
  | negate NONE = NONE;
wenzelm@9436
   337
nipkow@15121
   338
fun of_lin_arith_sort sg U =
nipkow@15121
   339
  Type.of_sort (Sign.tsig_of sg) (U,["Ring_and_Field.ordered_idom"])
nipkow@15121
   340
nipkow@15121
   341
fun allows_lin_arith sg discrete (U as Type(D,[])) =
nipkow@15121
   342
      if of_lin_arith_sort sg U
nipkow@15185
   343
      then (true, D mem discrete)
nipkow@15121
   344
      else (* special cases *)
nipkow@15185
   345
           if D mem discrete then (true,true) else (false,false)
nipkow@15121
   346
  | allows_lin_arith sg discrete U = (of_lin_arith_sort sg U, false);
nipkow@15121
   347
nipkow@15121
   348
fun decomp1 (sg,discrete,inj_consts) (T,xxx) =
wenzelm@9436
   349
  (case T of
nipkow@15121
   350
     Type("fun",[U,_]) =>
nipkow@15121
   351
       (case allows_lin_arith sg discrete U of
skalberg@15531
   352
          (true,d) => (case decomp2 inj_consts xxx of NONE => NONE
skalberg@15531
   353
                       | SOME(p,i,rel,q,j) => SOME(p,i,rel,q,j,d))
skalberg@15531
   354
        | (false,_) => NONE)
skalberg@15531
   355
   | _ => NONE);
wenzelm@9436
   356
nipkow@10574
   357
fun decomp2 data (_$(Const(rel,T)$lhs$rhs)) = decomp1 data (T,(rel,lhs,rhs))
nipkow@10574
   358
  | decomp2 data (_$(Const("Not",_)$(Const(rel,T)$lhs$rhs))) =
nipkow@10574
   359
      negate(decomp1 data (T,(rel,lhs,rhs)))
skalberg@15531
   360
  | decomp2 data _ = NONE
wenzelm@9436
   361
nipkow@10574
   362
fun decomp sg =
wenzelm@16424
   363
  let val {discrete, inj_consts, ...} = ArithTheoryData.get sg
nipkow@15121
   364
  in decomp2 (sg,discrete,inj_consts) end
wenzelm@9436
   365
nipkow@16358
   366
fun number_of(n,T) = HOLogic.number_of_const T $ (HOLogic.mk_bin n)
nipkow@10693
   367
wenzelm@9436
   368
end;
wenzelm@9436
   369
wenzelm@9436
   370
wenzelm@9436
   371
structure Fast_Arith =
wenzelm@9436
   372
  Fast_Lin_Arith(structure LA_Logic=LA_Logic and LA_Data=LA_Data_Ref);
wenzelm@9436
   373
nipkow@13499
   374
val fast_arith_tac    = Fast_Arith.lin_arith_tac false
nipkow@13499
   375
and fast_ex_arith_tac = Fast_Arith.lin_arith_tac
nipkow@14517
   376
and trace_arith    = Fast_Arith.trace
nipkow@14517
   377
and fast_arith_neq_limit = Fast_Arith.fast_arith_neq_limit;
wenzelm@9436
   378
wenzelm@9436
   379
local
wenzelm@9436
   380
nipkow@13902
   381
val isolateSuc =
nipkow@13902
   382
  let val thy = theory "Nat"
nipkow@13902
   383
  in prove_goal thy "Suc(i+j) = i+j + Suc 0"
nipkow@13902
   384
     (fn _ => [simp_tac (simpset_of thy) 1])
nipkow@13902
   385
  end;
nipkow@13902
   386
wenzelm@9436
   387
(* reduce contradictory <= to False.
wenzelm@9436
   388
   Most of the work is done by the cancel tactics.
wenzelm@9436
   389
*)
nipkow@12931
   390
val add_rules =
paulson@14368
   391
 [add_zero_left,add_zero_right,Zero_not_Suc,Suc_not_Zero,le_0_eq,
nipkow@15184
   392
  One_nat_def,isolateSuc,
wenzelm@17875
   393
  order_less_irrefl, zero_neq_one, zero_less_one, zero_le_one,
paulson@16473
   394
  zero_neq_one RS not_sym, not_one_le_zero, not_one_less_zero];
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@15921
   421
 [Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, ...} =>
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],
nipkow@15923
   427
    neqE = [linorder_neqE_nat,
wenzelm@16485
   428
      get_thm (theory "Ring_and_Field") (Name "linorder_neqE_ordered_idom")],
paulson@15234
   429
    simpset = HOL_basic_ss addsimps add_rules
wenzelm@17875
   430
                   addsimprocs [ab_group_add_cancel.sum_conv,
paulson@15234
   431
                                ab_group_add_cancel.rel_conv]
paulson@15234
   432
                   (*abel_cancel helps it work in abstract algebraic domains*)
paulson@15234
   433
                   addsimprocs nat_cancel_sums_add}),
nipkow@15185
   434
  ArithTheoryData.init, arith_discrete "nat"];
wenzelm@9436
   435
wenzelm@9436
   436
end;
wenzelm@9436
   437
wenzelm@13462
   438
val fast_nat_arith_simproc =
wenzelm@16834
   439
  Simplifier.simproc (the_context ()) "fast_nat_arith"
wenzelm@13462
   440
    ["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"] Fast_Arith.lin_arith_prover;
wenzelm@9436
   441
wenzelm@9436
   442
wenzelm@9436
   443
(* Because of fast_nat_arith_simproc, the arithmetic solver is really only
wenzelm@9436
   444
useful to detect inconsistencies among the premises for subgoals which are
wenzelm@9436
   445
*not* themselves (in)equalities, because the latter activate
wenzelm@9436
   446
fast_nat_arith_simproc anyway. However, it seems cheaper to activate the
wenzelm@9436
   447
solver all the time rather than add the additional check. *)
wenzelm@9436
   448
wenzelm@9436
   449
wenzelm@9436
   450
(* arith proof method *)
wenzelm@9436
   451
wenzelm@9436
   452
(* FIXME: K true should be replaced by a sensible test to speed things up
wenzelm@9436
   453
   in case there are lots of irrelevant terms involved;
wenzelm@9436
   454
   elimination of min/max can be optimized:
wenzelm@9436
   455
   (max m n + k <= r) = (m+k <= r & n+k <= r)
wenzelm@9436
   456
   (l <= min m n + k) = (l <= m+k & l <= n+k)
wenzelm@9436
   457
*)
wenzelm@10516
   458
local
wenzelm@10516
   459
nipkow@13499
   460
fun raw_arith_tac ex i st =
nipkow@13499
   461
  refute_tac (K true)
wenzelm@16834
   462
   (REPEAT o split_tac (#splits (ArithTheoryData.get (Thm.theory_of_thm st))))
nipkow@14509
   463
   ((REPEAT_DETERM o etac linorder_neqE) THEN' fast_ex_arith_tac ex)
nipkow@14509
   464
   i st;
wenzelm@9436
   465
berghofe@13877
   466
fun presburger_tac i st =
wenzelm@16834
   467
  (case ArithTheoryData.get (Thm.theory_of_thm st) of
skalberg@15531
   468
     {presburger = SOME tac, ...} =>
wenzelm@16970
   469
       (warning "Trying full Presburger arithmetic ..."; tac i st)
berghofe@13877
   470
   | _ => no_tac st);
berghofe@13877
   471
wenzelm@10516
   472
in
wenzelm@10516
   473
berghofe@13877
   474
val simple_arith_tac = FIRST' [fast_arith_tac,
berghofe@13877
   475
  ObjectLogic.atomize_tac THEN' raw_arith_tac true];
berghofe@13877
   476
berghofe@13877
   477
val arith_tac = FIRST' [fast_arith_tac,
berghofe@13877
   478
  ObjectLogic.atomize_tac THEN' raw_arith_tac true,
berghofe@13877
   479
  presburger_tac];
berghofe@13877
   480
berghofe@13877
   481
val silent_arith_tac = FIRST' [fast_arith_tac,
berghofe@13877
   482
  ObjectLogic.atomize_tac THEN' raw_arith_tac false,
berghofe@13877
   483
  presburger_tac];
wenzelm@10516
   484
wenzelm@9436
   485
fun arith_method prems =
wenzelm@9436
   486
  Method.METHOD (fn facts => HEADGOAL (Method.insert_tac (prems @ facts) THEN' arith_tac));
wenzelm@9436
   487
wenzelm@10516
   488
end;
wenzelm@10516
   489
nipkow@15195
   490
(* antisymmetry:
nipkow@15197
   491
   combines x <= y (or ~(y < x)) and y <= x (or ~(x < y)) into x = y
nipkow@15195
   492
nipkow@15195
   493
local
nipkow@15195
   494
val antisym = mk_meta_eq order_antisym
nipkow@15195
   495
val not_lessD = linorder_not_less RS iffD1
nipkow@15195
   496
fun prp t thm = (#prop(rep_thm thm) = t)
nipkow@15195
   497
in
nipkow@15195
   498
fun antisym_eq prems thm =
nipkow@15195
   499
  let
nipkow@15195
   500
    val r = #prop(rep_thm thm);
nipkow@15195
   501
  in
nipkow@15195
   502
    case r of
nipkow@15195
   503
      Tr $ ((c as Const("op <=",T)) $ s $ t) =>
nipkow@15195
   504
        let val r' = Tr $ (c $ t $ s)
nipkow@15195
   505
        in
nipkow@15195
   506
          case Library.find_first (prp r') prems of
skalberg@15531
   507
            NONE =>
wenzelm@16834
   508
              let val r' = Tr $ (HOLogic.Not $ (Const("op <",T) $ s $ t))
nipkow@15195
   509
              in case Library.find_first (prp r') prems of
skalberg@15531
   510
                   NONE => []
skalberg@15531
   511
                 | SOME thm' => [(thm' RS not_lessD) RS (thm RS antisym)]
nipkow@15195
   512
              end
skalberg@15531
   513
          | SOME thm' => [thm' RS (thm RS antisym)]
nipkow@15195
   514
        end
nipkow@15195
   515
    | Tr $ (Const("Not",_) $ (Const("op <",T) $ s $ t)) =>
nipkow@15195
   516
        let val r' = Tr $ (Const("op <=",T) $ s $ t)
nipkow@15195
   517
        in
nipkow@15195
   518
          case Library.find_first (prp r') prems of
skalberg@15531
   519
            NONE =>
wenzelm@16834
   520
              let val r' = Tr $ (HOLogic.Not $ (Const("op <",T) $ t $ s))
nipkow@15195
   521
              in case Library.find_first (prp r') prems of
skalberg@15531
   522
                   NONE => []
skalberg@15531
   523
                 | SOME thm' =>
nipkow@15195
   524
                     [(thm' RS not_lessD) RS ((thm RS not_lessD) RS antisym)]
nipkow@15195
   525
              end
skalberg@15531
   526
          | SOME thm' => [thm' RS ((thm RS not_lessD) RS antisym)]
nipkow@15195
   527
        end
nipkow@15195
   528
    | _ => []
nipkow@15195
   529
  end
nipkow@15195
   530
  handle THM _ => []
nipkow@15195
   531
end;
nipkow@15197
   532
*)
wenzelm@9436
   533
wenzelm@9436
   534
(* theory setup *)
wenzelm@9436
   535
wenzelm@9436
   536
val arith_setup =
wenzelm@9436
   537
  init_lin_arith_data @
wenzelm@17875
   538
  [fn thy => (Simplifier.change_simpset_of thy (fn ss => ss
wenzelm@17875
   539
    addsimprocs (nat_cancel_sums @ [fast_nat_arith_simproc])
wenzelm@17875
   540
    addSolver (mk_solver' "lin. arith." Fast_Arith.cut_lin_arith_tac)); thy),
paulson@15221
   541
  Method.add_methods
wenzelm@17875
   542
    [("arith", (arith_method o #2) oo Method.syntax Args.bang_facts,
wenzelm@17875
   543
      "decide linear arithmethic")],
wenzelm@9436
   544
  Attrib.add_attributes [("arith_split",
wenzelm@17875
   545
    (Attrib.no_args arith_split_add,
paulson@15221
   546
     Attrib.no_args Attrib.undef_local_attribute),
wenzelm@9893
   547
    "declaration of split rules for arithmetic procedure")]];