src/HOL/arith_data.ML
author wenzelm
Thu Jan 19 21:22:08 2006 +0100 (2006-01-19 ago)
changeset 18708 4b3dadb4fe33
parent 18394 fa0674cd6df1
child 18728 6790126ab5f6
permissions -rw-r--r--
setup: theory -> theory;
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@18328
    62
fun simp_all_tac rules =
wenzelm@18328
    63
  let val ss0 = HOL_ss addsimps rules
wenzelm@18328
    64
  in fn ss => ALLGOALS (simp_tac (Simplifier.inherit_context ss ss0)) end;
wenzelm@9436
    65
wenzelm@9436
    66
val add_rules = [add_Suc, add_Suc_right, add_0, add_0_right];
wenzelm@9436
    67
val mult_rules = [mult_Suc, mult_Suc_right, mult_0, mult_0_right];
wenzelm@9436
    68
nipkow@13517
    69
fun prep_simproc (name, pats, proc) =
wenzelm@16834
    70
  Simplifier.simproc (the_context ()) name pats proc;
nipkow@13517
    71
nipkow@13517
    72
end;
nipkow@13517
    73
nipkow@13517
    74
signature ARITH_DATA =
nipkow@13517
    75
sig
nipkow@13517
    76
  val nat_cancel_sums_add: simproc list
nipkow@13517
    77
  val nat_cancel_sums: simproc list
nipkow@13517
    78
end;
nipkow@13517
    79
nipkow@13517
    80
structure ArithData: ARITH_DATA =
nipkow@13517
    81
struct
nipkow@13517
    82
nipkow@13517
    83
open NatArithUtils;
wenzelm@9436
    84
wenzelm@9436
    85
wenzelm@9436
    86
(** cancel common summands **)
wenzelm@9436
    87
wenzelm@9436
    88
structure Sum =
wenzelm@9436
    89
struct
wenzelm@9436
    90
  val mk_sum = mk_norm_sum;
wenzelm@9436
    91
  val dest_sum = dest_sum;
wenzelm@9436
    92
  val prove_conv = prove_conv;
wenzelm@18328
    93
  val norm_tac1 = simp_all_tac add_rules;
wenzelm@18328
    94
  val norm_tac2 = simp_all_tac add_ac;
wenzelm@18328
    95
  fun norm_tac ss = norm_tac1 ss THEN norm_tac2 ss;
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 notI = notI;
wenzelm@9436
   177
val sym = sym;
wenzelm@9436
   178
val not_lessD = linorder_not_less RS iffD1;
wenzelm@9436
   179
val not_leD = linorder_not_le RS iffD1;
wenzelm@9436
   180
wenzelm@9436
   181
wenzelm@9436
   182
fun mk_Eq thm = (thm RS Eq_FalseI) handle THM _ => (thm RS Eq_TrueI);
wenzelm@9436
   183
wenzelm@9436
   184
val mk_Trueprop = HOLogic.mk_Trueprop;
wenzelm@9436
   185
nipkow@16733
   186
fun atomize thm = case #prop(rep_thm thm) of
nipkow@16733
   187
    Const("Trueprop",_) $ (Const("op &",_) $ _ $ _) =>
nipkow@16733
   188
    atomize(thm RS conjunct1) @ atomize(thm RS conjunct2)
nipkow@16733
   189
  | _ => [thm];
nipkow@16733
   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@16424
   209
structure ArithTheoryData = TheoryDataFun
wenzelm@16424
   210
(struct
wenzelm@9436
   211
  val name = "HOL/arith";
nipkow@15185
   212
  type T = {splits: thm list, inj_consts: (string * typ)list, discrete: string  list, presburger: (int -> tactic) option};
wenzelm@9436
   213
skalberg@15531
   214
  val empty = {splits = [], inj_consts = [], discrete = [], presburger = NONE};
wenzelm@9436
   215
  val copy = I;
wenzelm@16424
   216
  val extend = I;
wenzelm@16424
   217
  fun merge _ ({splits= splits1, inj_consts= inj_consts1, discrete= discrete1, presburger= presburger1},
berghofe@13877
   218
             {splits= splits2, inj_consts= inj_consts2, discrete= discrete2, presburger= presburger2}) =
wenzelm@9436
   219
   {splits = Drule.merge_rules (splits1, splits2),
nipkow@10574
   220
    inj_consts = merge_lists inj_consts1 inj_consts2,
nipkow@15185
   221
    discrete = merge_lists discrete1 discrete2,
skalberg@15531
   222
    presburger = (case presburger1 of NONE => presburger2 | p => p)};
wenzelm@9436
   223
  fun print _ _ = ();
wenzelm@16424
   224
end);
wenzelm@9436
   225
berghofe@13877
   226
fun arith_split_add (thy, thm) = (ArithTheoryData.map (fn {splits,inj_consts,discrete,presburger} =>
berghofe@13877
   227
  {splits= thm::splits, inj_consts= inj_consts, discrete= discrete, presburger= presburger}) thy, thm);
wenzelm@9436
   228
berghofe@13877
   229
fun arith_discrete d = ArithTheoryData.map (fn {splits,inj_consts,discrete,presburger} =>
berghofe@13877
   230
  {splits = splits, inj_consts = inj_consts, discrete = d :: discrete, presburger= presburger});
nipkow@10574
   231
berghofe@13877
   232
fun arith_inj_const c = ArithTheoryData.map (fn {splits,inj_consts,discrete,presburger} =>
berghofe@13877
   233
  {splits = splits, inj_consts = c :: inj_consts, discrete = discrete, presburger = presburger});
wenzelm@9436
   234
wenzelm@9436
   235
wenzelm@9436
   236
structure LA_Data_Ref: LIN_ARITH_DATA =
wenzelm@9436
   237
struct
wenzelm@9436
   238
wenzelm@9436
   239
(* Decomposition of terms *)
wenzelm@9436
   240
wenzelm@9436
   241
fun nT (Type("fun",[N,_])) = N = HOLogic.natT
wenzelm@9436
   242
  | nT _ = false;
wenzelm@9436
   243
haftmann@17325
   244
fun add_atom(t,m,(p,i)) = (case AList.lookup (op =) p t of NONE => ((t, m) :: p, i)
haftmann@17951
   245
                           | SOME n => (AList.update (op =) (t, Rat.add (n, m)) p, i));
nipkow@10693
   246
nipkow@10693
   247
exception Zero;
wenzelm@9436
   248
haftmann@17951
   249
fun rat_of_term (numt,dent) =
nipkow@10693
   250
  let val num = HOLogic.dest_binum numt and den = HOLogic.dest_binum dent
haftmann@17951
   251
  in if den = 0 then raise Zero else Rat.rat_of_quotient (num,den) end;
nipkow@10718
   252
nipkow@10718
   253
(* Warning: in rare cases number_of encloses a non-numeral,
nipkow@10718
   254
   in which case dest_binum raises TERM; hence all the handles below.
nipkow@11334
   255
   Same for Suc-terms that turn out not to be numerals -
nipkow@11334
   256
   although the simplifier should eliminate those anyway...
nipkow@10718
   257
*)
nipkow@10718
   258
nipkow@11334
   259
fun number_of_Sucs (Const("Suc",_) $ n) = number_of_Sucs n + 1
nipkow@11334
   260
  | number_of_Sucs t = if HOLogic.is_zero t then 0
nipkow@11334
   261
                       else raise TERM("number_of_Sucs",[])
nipkow@11334
   262
nipkow@10718
   263
(* decompose nested multiplications, bracketing them to the right and combining all
nipkow@10718
   264
   their coefficients
nipkow@10718
   265
*)
nipkow@10718
   266
nipkow@13499
   267
fun demult inj_consts =
nipkow@13499
   268
let
nipkow@10718
   269
fun demult((mC as Const("op *",_)) $ s $ t,m) = ((case s of
nipkow@10718
   270
        Const("Numeral.number_of",_)$n
haftmann@17951
   271
        => demult(t,Rat.mult(m,Rat.rat_of_intinf(HOLogic.dest_binum n)))
nipkow@12480
   272
      | Const("uminus",_)$(Const("Numeral.number_of",_)$n)
haftmann@17951
   273
        => demult(t,Rat.mult(m,Rat.rat_of_intinf(~(HOLogic.dest_binum n))))
nipkow@11334
   274
      | Const("Suc",_) $ _
haftmann@17951
   275
        => demult(t,Rat.mult(m,Rat.rat_of_int(number_of_Sucs s)))
nipkow@10718
   276
      | Const("op *",_) $ s1 $ s2 => demult(mC $ s1 $ (mC $ s2 $ t),m)
nipkow@10718
   277
      | Const("HOL.divide",_) $ numt $ (Const("Numeral.number_of",_)$dent) =>
nipkow@10718
   278
          let val den = HOLogic.dest_binum dent
nipkow@10718
   279
          in if den = 0 then raise Zero
haftmann@17951
   280
             else demult(mC $ numt $ t,Rat.mult(m, Rat.inv(Rat.rat_of_intinf den)))
nipkow@10718
   281
          end
nipkow@10718
   282
      | _ => atomult(mC,s,t,m)
nipkow@10718
   283
      ) handle TERM _ => atomult(mC,s,t,m))
nipkow@10718
   284
  | demult(atom as Const("HOL.divide",_) $ t $ (Const("Numeral.number_of",_)$dent), m) =
nipkow@10718
   285
      (let val den = HOLogic.dest_binum dent
haftmann@17951
   286
       in if den = 0 then raise Zero else demult(t,Rat.mult(m, Rat.inv(Rat.rat_of_intinf den))) end
skalberg@15531
   287
       handle TERM _ => (SOME atom,m))
haftmann@17951
   288
  | demult(Const("0",_),m) = (NONE, Rat.rat_of_int 0)
skalberg@15531
   289
  | demult(Const("1",_),m) = (NONE, m)
nipkow@10718
   290
  | demult(t as Const("Numeral.number_of",_)$n,m) =
haftmann@17951
   291
      ((NONE,Rat.mult(m,Rat.rat_of_intinf(HOLogic.dest_binum n)))
skalberg@15531
   292
       handle TERM _ => (SOME t,m))
haftmann@17951
   293
  | demult(Const("uminus",_)$t, m) = demult(t,Rat.mult(m,Rat.rat_of_int(~1)))
nipkow@13499
   294
  | demult(t as Const f $ x, m) =
skalberg@15531
   295
      (if f mem inj_consts then SOME x else SOME t,m)
skalberg@15531
   296
  | demult(atom,m) = (SOME atom,m)
nipkow@10718
   297
skalberg@15531
   298
and atomult(mC,atom,t,m) = (case demult(t,m) of (NONE,m') => (SOME atom,m')
skalberg@15531
   299
                            | (SOME t',m') => (SOME(mC $ atom $ t'),m'))
nipkow@13499
   300
in demult end;
nipkow@10718
   301
nipkow@10574
   302
fun decomp2 inj_consts (rel,lhs,rhs) =
nipkow@10574
   303
let
wenzelm@9436
   304
(* Turn term into list of summand * multiplicity plus a constant *)
wenzelm@9436
   305
fun poly(Const("op +",_) $ s $ t, m, pi) = poly(s,m,poly(t,m,pi))
wenzelm@9436
   306
  | poly(all as Const("op -",T) $ s $ t, m, pi) =
haftmann@17951
   307
      if nT T then add_atom(all,m,pi) else poly(s,m,poly(t,Rat.neg m,pi))
nipkow@15958
   308
  | poly(all as Const("uminus",T) $ t, m, pi) =
haftmann@17951
   309
      if nT T then add_atom(all,m,pi) else poly(t,Rat.neg m,pi)
wenzelm@9436
   310
  | poly(Const("0",_), _, pi) = pi
haftmann@17951
   311
  | poly(Const("1",_), m, (p,i)) = (p,Rat.add(i,m))
haftmann@17951
   312
  | poly(Const("Suc",_)$t, m, (p,i)) = poly(t, m, (p,Rat.add(i,m)))
nipkow@10718
   313
  | poly(t as Const("op *",_) $ _ $ _, m, pi as (p,i)) =
nipkow@13499
   314
      (case demult inj_consts (t,m) of
haftmann@17951
   315
         (NONE,m') => (p,Rat.add(i,m))
skalberg@15531
   316
       | (SOME u,m') => add_atom(u,m',pi))
nipkow@10718
   317
  | poly(t as Const("HOL.divide",_) $ _ $ _, m, pi as (p,i)) =
nipkow@13499
   318
      (case demult inj_consts (t,m) of
haftmann@17951
   319
         (NONE,m') => (p,Rat.add(i,m'))
skalberg@15531
   320
       | (SOME u,m') => add_atom(u,m',pi))
nipkow@10718
   321
  | poly(all as (Const("Numeral.number_of",_)$t,m,(p,i))) =
haftmann@17951
   322
      ((p,Rat.add(i,Rat.mult(m,Rat.rat_of_intinf(HOLogic.dest_binum t))))
nipkow@10718
   323
       handle TERM _ => add_atom all)
nipkow@10574
   324
  | poly(all as Const f $ x, m, pi) =
nipkow@10574
   325
      if f mem inj_consts then poly(x,m,pi) else add_atom(all,m,pi)
wenzelm@9436
   326
  | poly x  = add_atom x;
wenzelm@9436
   327
haftmann@17951
   328
val (p,i) = poly(lhs,Rat.rat_of_int 1,([],Rat.rat_of_int 0))
haftmann@17951
   329
and (q,j) = poly(rhs,Rat.rat_of_int 1,([],Rat.rat_of_int 0))
nipkow@10693
   330
wenzelm@9436
   331
  in case rel of
skalberg@15531
   332
       "op <"  => SOME(p,i,"<",q,j)
skalberg@15531
   333
     | "op <=" => SOME(p,i,"<=",q,j)
skalberg@15531
   334
     | "op ="  => SOME(p,i,"=",q,j)
skalberg@15531
   335
     | _       => NONE
skalberg@15531
   336
  end handle Zero => NONE;
wenzelm@9436
   337
skalberg@15531
   338
fun negate(SOME(x,i,rel,y,j,d)) = SOME(x,i,"~"^rel,y,j,d)
skalberg@15531
   339
  | negate NONE = NONE;
wenzelm@9436
   340
nipkow@15121
   341
fun of_lin_arith_sort sg U =
nipkow@15121
   342
  Type.of_sort (Sign.tsig_of sg) (U,["Ring_and_Field.ordered_idom"])
nipkow@15121
   343
nipkow@15121
   344
fun allows_lin_arith sg discrete (U as Type(D,[])) =
nipkow@15121
   345
      if of_lin_arith_sort sg U
nipkow@15185
   346
      then (true, D mem discrete)
nipkow@15121
   347
      else (* special cases *)
nipkow@15185
   348
           if D mem discrete then (true,true) else (false,false)
nipkow@15121
   349
  | allows_lin_arith sg discrete U = (of_lin_arith_sort sg U, false);
nipkow@15121
   350
nipkow@15121
   351
fun decomp1 (sg,discrete,inj_consts) (T,xxx) =
wenzelm@9436
   352
  (case T of
nipkow@15121
   353
     Type("fun",[U,_]) =>
nipkow@15121
   354
       (case allows_lin_arith sg discrete U of
skalberg@15531
   355
          (true,d) => (case decomp2 inj_consts xxx of NONE => NONE
skalberg@15531
   356
                       | SOME(p,i,rel,q,j) => SOME(p,i,rel,q,j,d))
skalberg@15531
   357
        | (false,_) => NONE)
skalberg@15531
   358
   | _ => NONE);
wenzelm@9436
   359
nipkow@10574
   360
fun decomp2 data (_$(Const(rel,T)$lhs$rhs)) = decomp1 data (T,(rel,lhs,rhs))
nipkow@10574
   361
  | decomp2 data (_$(Const("Not",_)$(Const(rel,T)$lhs$rhs))) =
nipkow@10574
   362
      negate(decomp1 data (T,(rel,lhs,rhs)))
skalberg@15531
   363
  | decomp2 data _ = NONE
wenzelm@9436
   364
nipkow@10574
   365
fun decomp sg =
wenzelm@16424
   366
  let val {discrete, inj_consts, ...} = ArithTheoryData.get sg
nipkow@15121
   367
  in decomp2 (sg,discrete,inj_consts) end
wenzelm@9436
   368
nipkow@16358
   369
fun number_of(n,T) = HOLogic.number_of_const T $ (HOLogic.mk_bin n)
nipkow@10693
   370
wenzelm@9436
   371
end;
wenzelm@9436
   372
wenzelm@9436
   373
wenzelm@9436
   374
structure Fast_Arith =
wenzelm@9436
   375
  Fast_Lin_Arith(structure LA_Logic=LA_Logic and LA_Data=LA_Data_Ref);
wenzelm@9436
   376
nipkow@13499
   377
val fast_arith_tac    = Fast_Arith.lin_arith_tac false
nipkow@13499
   378
and fast_ex_arith_tac = Fast_Arith.lin_arith_tac
nipkow@14517
   379
and trace_arith    = Fast_Arith.trace
nipkow@14517
   380
and fast_arith_neq_limit = Fast_Arith.fast_arith_neq_limit;
wenzelm@9436
   381
wenzelm@9436
   382
local
wenzelm@9436
   383
nipkow@13902
   384
val isolateSuc =
nipkow@13902
   385
  let val thy = theory "Nat"
nipkow@13902
   386
  in prove_goal thy "Suc(i+j) = i+j + Suc 0"
nipkow@13902
   387
     (fn _ => [simp_tac (simpset_of thy) 1])
nipkow@13902
   388
  end;
nipkow@13902
   389
wenzelm@9436
   390
(* reduce contradictory <= to False.
wenzelm@9436
   391
   Most of the work is done by the cancel tactics.
wenzelm@9436
   392
*)
nipkow@12931
   393
val add_rules =
paulson@14368
   394
 [add_zero_left,add_zero_right,Zero_not_Suc,Suc_not_Zero,le_0_eq,
nipkow@15184
   395
  One_nat_def,isolateSuc,
wenzelm@17875
   396
  order_less_irrefl, zero_neq_one, zero_less_one, zero_le_one,
paulson@16473
   397
  zero_neq_one RS not_sym, not_one_le_zero, not_one_less_zero];
wenzelm@9436
   398
paulson@14368
   399
val add_mono_thms_ordered_semiring = map (fn s => prove_goal (the_context ()) s
wenzelm@9436
   400
 (fn prems => [cut_facts_tac prems 1,
paulson@14368
   401
               blast_tac (claset() addIs [add_mono]) 1]))
nipkow@15121
   402
["(i <= j) & (k <= l) ==> i + k <= j + (l::'a::pordered_ab_semigroup_add)",
nipkow@15121
   403
 "(i  = j) & (k <= l) ==> i + k <= j + (l::'a::pordered_ab_semigroup_add)",
nipkow@15121
   404
 "(i <= j) & (k  = l) ==> i + k <= j + (l::'a::pordered_ab_semigroup_add)",
nipkow@15121
   405
 "(i  = j) & (k  = l) ==> i + k  = j + (l::'a::pordered_ab_semigroup_add)"
wenzelm@9436
   406
];
wenzelm@9436
   407
nipkow@15121
   408
val mono_ss = simpset() addsimps
nipkow@15121
   409
                [add_mono,add_strict_mono,add_less_le_mono,add_le_less_mono];
nipkow@15121
   410
nipkow@15121
   411
val add_mono_thms_ordered_field =
nipkow@15121
   412
  map (fn s => prove_goal (the_context ()) s
nipkow@15121
   413
                 (fn prems => [cut_facts_tac prems 1, asm_simp_tac mono_ss 1]))
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
     "(i<j) & (k<=l)  ==> i+k < j+(l::'a::pordered_cancel_ab_semigroup_add)",
nipkow@15121
   417
     "(i<=j) & (k<l)  ==> i+k < j+(l::'a::pordered_cancel_ab_semigroup_add)",
nipkow@15121
   418
     "(i<j) & (k<l)   ==> i+k < j+(l::'a::pordered_cancel_ab_semigroup_add)"];
nipkow@15121
   419
wenzelm@9436
   420
in
wenzelm@9436
   421
wenzelm@9436
   422
val init_lin_arith_data =
wenzelm@18708
   423
 Fast_Arith.setup #>
wenzelm@18708
   424
 Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, ...} =>
nipkow@15121
   425
   {add_mono_thms = add_mono_thms @
nipkow@15121
   426
    add_mono_thms_ordered_semiring @ add_mono_thms_ordered_field,
nipkow@10693
   427
    mult_mono_thms = mult_mono_thms,
nipkow@10574
   428
    inj_thms = inj_thms,
wenzelm@9436
   429
    lessD = lessD @ [Suc_leI],
nipkow@15923
   430
    neqE = [linorder_neqE_nat,
wenzelm@16485
   431
      get_thm (theory "Ring_and_Field") (Name "linorder_neqE_ordered_idom")],
paulson@15234
   432
    simpset = HOL_basic_ss addsimps add_rules
wenzelm@17875
   433
                   addsimprocs [ab_group_add_cancel.sum_conv,
paulson@15234
   434
                                ab_group_add_cancel.rel_conv]
paulson@15234
   435
                   (*abel_cancel helps it work in abstract algebraic domains*)
wenzelm@18708
   436
                   addsimprocs nat_cancel_sums_add}) #>
wenzelm@18708
   437
  ArithTheoryData.init #>
wenzelm@18708
   438
  arith_discrete "nat";
wenzelm@9436
   439
wenzelm@9436
   440
end;
wenzelm@9436
   441
wenzelm@13462
   442
val fast_nat_arith_simproc =
wenzelm@16834
   443
  Simplifier.simproc (the_context ()) "fast_nat_arith"
wenzelm@13462
   444
    ["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"] Fast_Arith.lin_arith_prover;
wenzelm@9436
   445
wenzelm@9436
   446
wenzelm@9436
   447
(* Because of fast_nat_arith_simproc, the arithmetic solver is really only
wenzelm@9436
   448
useful to detect inconsistencies among the premises for subgoals which are
wenzelm@9436
   449
*not* themselves (in)equalities, because the latter activate
wenzelm@9436
   450
fast_nat_arith_simproc anyway. However, it seems cheaper to activate the
wenzelm@9436
   451
solver all the time rather than add the additional check. *)
wenzelm@9436
   452
wenzelm@9436
   453
wenzelm@9436
   454
(* arith proof method *)
wenzelm@9436
   455
wenzelm@9436
   456
(* FIXME: K true should be replaced by a sensible test to speed things up
wenzelm@9436
   457
   in case there are lots of irrelevant terms involved;
wenzelm@9436
   458
   elimination of min/max can be optimized:
wenzelm@9436
   459
   (max m n + k <= r) = (m+k <= r & n+k <= r)
wenzelm@9436
   460
   (l <= min m n + k) = (l <= m+k & l <= n+k)
wenzelm@9436
   461
*)
wenzelm@10516
   462
local
chaieb@18394
   463
(* a simpset for computations subject to optimazation !!! *)
chaieb@18394
   464
(*
chaieb@18394
   465
val binarith = map thm
chaieb@18394
   466
  ["Pls_0_eq", "Min_1_eq",
chaieb@18394
   467
 "bin_pred_Pls","bin_pred_Min","bin_pred_1","bin_pred_0",
chaieb@18394
   468
  "bin_succ_Pls", "bin_succ_Min", "bin_succ_1", "bin_succ_0",
chaieb@18394
   469
  "bin_add_Pls", "bin_add_Min", "bin_add_BIT_0", "bin_add_BIT_10",
chaieb@18394
   470
  "bin_add_BIT_11", "bin_minus_Pls", "bin_minus_Min", "bin_minus_1", 
chaieb@18394
   471
  "bin_minus_0", "bin_mult_Pls", "bin_mult_Min", "bin_mult_1", "bin_mult_0", 
chaieb@18394
   472
  "bin_add_Pls_right", "bin_add_Min_right"];
chaieb@18394
   473
 val intarithrel = 
chaieb@18394
   474
     (map thm ["int_eq_number_of_eq","int_neg_number_of_BIT", 
chaieb@18394
   475
		"int_le_number_of_eq","int_iszero_number_of_0",
chaieb@18394
   476
		"int_less_number_of_eq_neg"]) @
chaieb@18394
   477
     (map (fn s => thm s RS thm "lift_bool") 
chaieb@18394
   478
	  ["int_iszero_number_of_Pls","int_iszero_number_of_1",
chaieb@18394
   479
	   "int_neg_number_of_Min"])@
chaieb@18394
   480
     (map (fn s => thm s RS thm "nlift_bool") 
chaieb@18394
   481
	  ["int_nonzero_number_of_Min","int_not_neg_number_of_Pls"]);
chaieb@18394
   482
     
chaieb@18394
   483
val intarith = map thm ["int_number_of_add_sym", "int_number_of_minus_sym",
chaieb@18394
   484
			"int_number_of_diff_sym", "int_number_of_mult_sym"];
chaieb@18394
   485
val natarith = map thm ["add_nat_number_of", "diff_nat_number_of",
chaieb@18394
   486
			"mult_nat_number_of", "eq_nat_number_of",
chaieb@18394
   487
			"less_nat_number_of"]
chaieb@18394
   488
val powerarith = 
chaieb@18394
   489
    (map thm ["nat_number_of", "zpower_number_of_even", 
chaieb@18394
   490
	      "zpower_Pls", "zpower_Min"]) @ 
chaieb@18394
   491
    [(Tactic.simplify true [thm "zero_eq_Numeral0_nring", 
chaieb@18394
   492
			   thm "one_eq_Numeral1_nring"] 
chaieb@18394
   493
  (thm "zpower_number_of_odd"))]
wenzelm@10516
   494
chaieb@18394
   495
val comp_arith = binarith @ intarith @ intarithrel @ natarith 
chaieb@18394
   496
	    @ powerarith @[thm"not_false_eq_true", thm "not_true_eq_false"];
chaieb@18394
   497
chaieb@18394
   498
val comp_ss = HOL_basic_ss addsimps comp_arith addsimps simp_thms;
chaieb@18394
   499
*)
nipkow@13499
   500
fun raw_arith_tac ex i st =
nipkow@13499
   501
  refute_tac (K true)
wenzelm@16834
   502
   (REPEAT o split_tac (#splits (ArithTheoryData.get (Thm.theory_of_thm st))))
chaieb@18394
   503
(*   (REPEAT o 
chaieb@18394
   504
    (fn i =>(split_tac (#splits (ArithTheoryData.get(Thm.theory_of_thm st))) i)
chaieb@18394
   505
		THEN (simp_tac comp_ss i))) *)
nipkow@14509
   506
   ((REPEAT_DETERM o etac linorder_neqE) THEN' fast_ex_arith_tac ex)
nipkow@14509
   507
   i st;
wenzelm@9436
   508
berghofe@13877
   509
fun presburger_tac i st =
wenzelm@16834
   510
  (case ArithTheoryData.get (Thm.theory_of_thm st) of
skalberg@15531
   511
     {presburger = SOME tac, ...} =>
wenzelm@16970
   512
       (warning "Trying full Presburger arithmetic ..."; tac i st)
berghofe@13877
   513
   | _ => no_tac st);
berghofe@13877
   514
wenzelm@10516
   515
in
wenzelm@10516
   516
berghofe@13877
   517
val simple_arith_tac = FIRST' [fast_arith_tac,
berghofe@13877
   518
  ObjectLogic.atomize_tac THEN' raw_arith_tac true];
berghofe@13877
   519
berghofe@13877
   520
val arith_tac = FIRST' [fast_arith_tac,
berghofe@13877
   521
  ObjectLogic.atomize_tac THEN' raw_arith_tac true,
berghofe@13877
   522
  presburger_tac];
berghofe@13877
   523
berghofe@13877
   524
val silent_arith_tac = FIRST' [fast_arith_tac,
berghofe@13877
   525
  ObjectLogic.atomize_tac THEN' raw_arith_tac false,
berghofe@13877
   526
  presburger_tac];
wenzelm@10516
   527
wenzelm@9436
   528
fun arith_method prems =
wenzelm@9436
   529
  Method.METHOD (fn facts => HEADGOAL (Method.insert_tac (prems @ facts) THEN' arith_tac));
wenzelm@9436
   530
wenzelm@10516
   531
end;
wenzelm@10516
   532
nipkow@15195
   533
(* antisymmetry:
nipkow@15197
   534
   combines x <= y (or ~(y < x)) and y <= x (or ~(x < y)) into x = y
nipkow@15195
   535
nipkow@15195
   536
local
nipkow@15195
   537
val antisym = mk_meta_eq order_antisym
nipkow@15195
   538
val not_lessD = linorder_not_less RS iffD1
nipkow@15195
   539
fun prp t thm = (#prop(rep_thm thm) = t)
nipkow@15195
   540
in
nipkow@15195
   541
fun antisym_eq prems thm =
nipkow@15195
   542
  let
nipkow@15195
   543
    val r = #prop(rep_thm thm);
nipkow@15195
   544
  in
nipkow@15195
   545
    case r of
nipkow@15195
   546
      Tr $ ((c as Const("op <=",T)) $ s $ t) =>
nipkow@15195
   547
        let val r' = Tr $ (c $ t $ s)
nipkow@15195
   548
        in
nipkow@15195
   549
          case Library.find_first (prp r') prems of
skalberg@15531
   550
            NONE =>
wenzelm@16834
   551
              let val r' = Tr $ (HOLogic.Not $ (Const("op <",T) $ s $ t))
nipkow@15195
   552
              in case Library.find_first (prp r') prems of
skalberg@15531
   553
                   NONE => []
skalberg@15531
   554
                 | SOME thm' => [(thm' RS not_lessD) RS (thm RS antisym)]
nipkow@15195
   555
              end
skalberg@15531
   556
          | SOME thm' => [thm' RS (thm RS antisym)]
nipkow@15195
   557
        end
nipkow@15195
   558
    | Tr $ (Const("Not",_) $ (Const("op <",T) $ s $ t)) =>
nipkow@15195
   559
        let val r' = Tr $ (Const("op <=",T) $ s $ t)
nipkow@15195
   560
        in
nipkow@15195
   561
          case Library.find_first (prp r') prems of
skalberg@15531
   562
            NONE =>
wenzelm@16834
   563
              let val r' = Tr $ (HOLogic.Not $ (Const("op <",T) $ t $ s))
nipkow@15195
   564
              in case Library.find_first (prp r') prems of
skalberg@15531
   565
                   NONE => []
skalberg@15531
   566
                 | SOME thm' =>
nipkow@15195
   567
                     [(thm' RS not_lessD) RS ((thm RS not_lessD) RS antisym)]
nipkow@15195
   568
              end
skalberg@15531
   569
          | SOME thm' => [thm' RS ((thm RS not_lessD) RS antisym)]
nipkow@15195
   570
        end
nipkow@15195
   571
    | _ => []
nipkow@15195
   572
  end
nipkow@15195
   573
  handle THM _ => []
nipkow@15195
   574
end;
nipkow@15197
   575
*)
wenzelm@9436
   576
wenzelm@9436
   577
(* theory setup *)
wenzelm@9436
   578
wenzelm@9436
   579
val arith_setup =
wenzelm@18708
   580
  init_lin_arith_data #>
wenzelm@18708
   581
  (fn thy => (Simplifier.change_simpset_of thy (fn ss => ss
wenzelm@17875
   582
    addsimprocs (nat_cancel_sums @ [fast_nat_arith_simproc])
wenzelm@18708
   583
    addSolver (mk_solver' "lin. arith." Fast_Arith.cut_lin_arith_tac)); thy)) #>
paulson@15221
   584
  Method.add_methods
wenzelm@17875
   585
    [("arith", (arith_method o #2) oo Method.syntax Args.bang_facts,
wenzelm@18708
   586
      "decide linear arithmethic")] #>
wenzelm@9436
   587
  Attrib.add_attributes [("arith_split",
wenzelm@17875
   588
    (Attrib.no_args arith_split_add,
paulson@15221
   589
     Attrib.no_args Attrib.undef_local_attribute),
wenzelm@18708
   590
    "declaration of split rules for arithmetic procedure")];