src/HOL/arith_data.ML
author haftmann
Fri Jul 20 14:28:25 2007 +0200 (2007-07-20)
changeset 23881 851c74f1bb69
parent 23530 438c5d2db482
child 24076 ae946f751c44
permissions -rw-r--r--
moved class ord from Orderings.thy to HOL.thy
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
haftmann@22997
    19
val mk_plus = HOLogic.mk_binop @{const_name HOL.plus};
wenzelm@9436
    20
wenzelm@9436
    21
fun mk_sum [] = HOLogic.zero
wenzelm@9436
    22
  | mk_sum [t] = t
wenzelm@9436
    23
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
wenzelm@9436
    24
wenzelm@9436
    25
(*normal form of sums: Suc (... (Suc (a + (b + ...))))*)
wenzelm@9436
    26
fun mk_norm_sum ts =
haftmann@21621
    27
  let val (ones, sums) = List.partition (equal HOLogic.Suc_zero) ts in
wenzelm@9436
    28
    funpow (length ones) HOLogic.mk_Suc (mk_sum sums)
wenzelm@9436
    29
  end;
wenzelm@9436
    30
wenzelm@9436
    31
(* dest_sum *)
wenzelm@9436
    32
haftmann@22997
    33
val dest_plus = HOLogic.dest_bin @{const_name HOL.plus} HOLogic.natT;
wenzelm@9436
    34
wenzelm@9436
    35
fun dest_sum tm =
wenzelm@9436
    36
  if HOLogic.is_zero tm then []
wenzelm@9436
    37
  else
wenzelm@9436
    38
    (case try HOLogic.dest_Suc tm of
haftmann@21621
    39
      SOME t => HOLogic.Suc_zero :: dest_sum t
skalberg@15531
    40
    | NONE =>
wenzelm@9436
    41
        (case try dest_plus tm of
skalberg@15531
    42
          SOME (t, u) => dest_sum t @ dest_sum u
skalberg@15531
    43
        | NONE => [tm]));
wenzelm@9436
    44
wenzelm@9436
    45
(** generic proof tools **)
wenzelm@9436
    46
wenzelm@9436
    47
(* prove conversions *)
wenzelm@9436
    48
wenzelm@20044
    49
fun prove_conv expand_tac norm_tac ss tu =  (* FIXME avoid standard *)
wenzelm@20044
    50
  mk_meta_eq (standard (Goal.prove (Simplifier.the_context ss) [] []
wenzelm@20044
    51
      (HOLogic.mk_Trueprop (HOLogic.mk_eq tu))
wenzelm@17989
    52
    (K (EVERY [expand_tac, norm_tac ss]))));
wenzelm@9436
    53
wenzelm@9436
    54
wenzelm@9436
    55
(* rewriting *)
wenzelm@9436
    56
wenzelm@18328
    57
fun simp_all_tac rules =
wenzelm@18328
    58
  let val ss0 = HOL_ss addsimps rules
wenzelm@18328
    59
  in fn ss => ALLGOALS (simp_tac (Simplifier.inherit_context ss ss0)) end;
wenzelm@9436
    60
nipkow@13517
    61
fun prep_simproc (name, pats, proc) =
wenzelm@16834
    62
  Simplifier.simproc (the_context ()) name pats proc;
nipkow@13517
    63
webertj@20217
    64
end;  (* NatArithUtils *)
webertj@20217
    65
nipkow@13517
    66
nipkow@13517
    67
signature ARITH_DATA =
nipkow@13517
    68
sig
nipkow@13517
    69
  val nat_cancel_sums_add: simproc list
nipkow@13517
    70
  val nat_cancel_sums: simproc list
nipkow@13517
    71
end;
nipkow@13517
    72
webertj@20217
    73
nipkow@13517
    74
structure ArithData: ARITH_DATA =
nipkow@13517
    75
struct
nipkow@13517
    76
nipkow@13517
    77
open NatArithUtils;
wenzelm@9436
    78
wenzelm@9436
    79
(** cancel common summands **)
wenzelm@9436
    80
wenzelm@9436
    81
structure Sum =
wenzelm@9436
    82
struct
wenzelm@9436
    83
  val mk_sum = mk_norm_sum;
wenzelm@9436
    84
  val dest_sum = dest_sum;
wenzelm@9436
    85
  val prove_conv = prove_conv;
haftmann@22838
    86
  val norm_tac1 = simp_all_tac [@{thm "add_Suc"}, @{thm "add_Suc_right"},
haftmann@22838
    87
    @{thm "add_0"}, @{thm "add_0_right"}];
haftmann@22548
    88
  val norm_tac2 = simp_all_tac @{thms add_ac};
wenzelm@18328
    89
  fun norm_tac ss = norm_tac1 ss THEN norm_tac2 ss;
wenzelm@9436
    90
end;
wenzelm@9436
    91
wenzelm@9436
    92
fun gen_uncancel_tac rule ct =
haftmann@22838
    93
  rtac (instantiate' [] [NONE, SOME ct] (rule RS @{thm subst_equals})) 1;
wenzelm@9436
    94
wenzelm@9436
    95
(* nat eq *)
wenzelm@9436
    96
wenzelm@9436
    97
structure EqCancelSums = CancelSumsFun
wenzelm@9436
    98
(struct
wenzelm@9436
    99
  open Sum;
wenzelm@9436
   100
  val mk_bal = HOLogic.mk_eq;
wenzelm@9436
   101
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT;
haftmann@22838
   102
  val uncancel_tac = gen_uncancel_tac @{thm "nat_add_left_cancel"};
wenzelm@9436
   103
end);
wenzelm@9436
   104
wenzelm@9436
   105
(* nat less *)
wenzelm@9436
   106
wenzelm@9436
   107
structure LessCancelSums = CancelSumsFun
wenzelm@9436
   108
(struct
wenzelm@9436
   109
  open Sum;
haftmann@23881
   110
  val mk_bal = HOLogic.mk_binrel @{const_name HOL.less};
haftmann@23881
   111
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less} HOLogic.natT;
haftmann@22838
   112
  val uncancel_tac = gen_uncancel_tac @{thm "nat_add_left_cancel_less"};
wenzelm@9436
   113
end);
wenzelm@9436
   114
wenzelm@9436
   115
(* nat le *)
wenzelm@9436
   116
wenzelm@9436
   117
structure LeCancelSums = CancelSumsFun
wenzelm@9436
   118
(struct
wenzelm@9436
   119
  open Sum;
haftmann@23881
   120
  val mk_bal = HOLogic.mk_binrel @{const_name HOL.less_eq};
haftmann@23881
   121
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less_eq} HOLogic.natT;
haftmann@22838
   122
  val uncancel_tac = gen_uncancel_tac @{thm "nat_add_left_cancel_le"};
wenzelm@9436
   123
end);
wenzelm@9436
   124
wenzelm@9436
   125
(* nat diff *)
wenzelm@9436
   126
wenzelm@9436
   127
structure DiffCancelSums = CancelSumsFun
wenzelm@9436
   128
(struct
wenzelm@9436
   129
  open Sum;
haftmann@22997
   130
  val mk_bal = HOLogic.mk_binop @{const_name HOL.minus};
haftmann@22997
   131
  val dest_bal = HOLogic.dest_bin @{const_name HOL.minus} HOLogic.natT;
haftmann@22838
   132
  val uncancel_tac = gen_uncancel_tac @{thm "diff_cancel"};
wenzelm@9436
   133
end);
wenzelm@9436
   134
wenzelm@9436
   135
(** prepare nat_cancel simprocs **)
wenzelm@9436
   136
wenzelm@9436
   137
val nat_cancel_sums_add = map prep_simproc
wenzelm@13462
   138
  [("nateq_cancel_sums",
webertj@20268
   139
     ["(l::nat) + m = n", "(l::nat) = m + n", "Suc m = n", "m = Suc n"],
webertj@20268
   140
     K EqCancelSums.proc),
wenzelm@13462
   141
   ("natless_cancel_sums",
webertj@20268
   142
     ["(l::nat) + m < n", "(l::nat) < m + n", "Suc m < n", "m < Suc n"],
webertj@20268
   143
     K LessCancelSums.proc),
wenzelm@13462
   144
   ("natle_cancel_sums",
webertj@20268
   145
     ["(l::nat) + m <= n", "(l::nat) <= m + n", "Suc m <= n", "m <= Suc n"],
webertj@20268
   146
     K LeCancelSums.proc)];
wenzelm@9436
   147
wenzelm@9436
   148
val nat_cancel_sums = nat_cancel_sums_add @
wenzelm@13462
   149
  [prep_simproc ("natdiff_cancel_sums",
webertj@20268
   150
    ["((l::nat) + m) - n", "(l::nat) - (m + n)", "Suc m - n", "m - Suc n"],
webertj@20268
   151
    K DiffCancelSums.proc)];
wenzelm@9436
   152
webertj@20217
   153
end;  (* ArithData *)
wenzelm@9436
   154
wenzelm@9436
   155
open ArithData;
wenzelm@9436
   156
wenzelm@9436
   157
wenzelm@9436
   158
(*---------------------------------------------------------------------------*)
wenzelm@9436
   159
(* 2. Linear arithmetic                                                      *)
wenzelm@9436
   160
(*---------------------------------------------------------------------------*)
wenzelm@9436
   161
wenzelm@9436
   162
(* Parameters data for general linear arithmetic functor *)
wenzelm@9436
   163
wenzelm@9436
   164
structure LA_Logic: LIN_ARITH_LOGIC =
wenzelm@9436
   165
struct
webertj@20217
   166
wenzelm@9436
   167
val ccontr = ccontr;
wenzelm@9436
   168
val conjI = conjI;
wenzelm@9436
   169
val notI = notI;
wenzelm@9436
   170
val sym = sym;
haftmann@22548
   171
val not_lessD = @{thm linorder_not_less} RS iffD1;
haftmann@22548
   172
val not_leD = @{thm linorder_not_le} RS iffD1;
wenzelm@21243
   173
val le0 = thm "le0";
wenzelm@9436
   174
wenzelm@9436
   175
fun mk_Eq thm = (thm RS Eq_FalseI) handle THM _ => (thm RS Eq_TrueI);
wenzelm@9436
   176
wenzelm@9436
   177
val mk_Trueprop = HOLogic.mk_Trueprop;
wenzelm@9436
   178
nipkow@16733
   179
fun atomize thm = case #prop(rep_thm thm) of
nipkow@16733
   180
    Const("Trueprop",_) $ (Const("op &",_) $ _ $ _) =>
nipkow@16733
   181
    atomize(thm RS conjunct1) @ atomize(thm RS conjunct2)
nipkow@16733
   182
  | _ => [thm];
nipkow@16733
   183
webertj@23190
   184
fun neg_prop ((TP as Const("Trueprop",_)) $ (Const("Not",_) $ t)) = TP $ t
webertj@23190
   185
  | neg_prop ((TP as Const("Trueprop",_)) $ t) = TP $ (HOLogic.Not $t)
webertj@23190
   186
  | neg_prop t = raise TERM ("neg_prop", [t]);
wenzelm@9436
   187
wenzelm@9436
   188
fun is_False thm =
wenzelm@9436
   189
  let val _ $ t = #prop(rep_thm thm)
wenzelm@9436
   190
  in t = Const("False",HOLogic.boolT) end;
wenzelm@9436
   191
wenzelm@9436
   192
fun is_nat(t) = fastype_of1 t = HOLogic.natT;
wenzelm@9436
   193
wenzelm@9436
   194
fun mk_nat_thm sg t =
wenzelm@9436
   195
  let val ct = cterm_of sg t  and cn = cterm_of sg (Var(("n",0),HOLogic.natT))
wenzelm@9436
   196
  in instantiate ([],[(cn,ct)]) le0 end;
wenzelm@9436
   197
webertj@20217
   198
end;  (* LA_Logic *)
wenzelm@9436
   199
wenzelm@9436
   200
wenzelm@9436
   201
(* arith theory data *)
wenzelm@9436
   202
webertj@20412
   203
datatype arithtactic = ArithTactic of {name: string, tactic: int -> tactic, id: stamp};
webertj@20412
   204
webertj@20412
   205
fun mk_arith_tactic name tactic = ArithTactic {name = name, tactic = tactic, id = stamp ()};
webertj@20412
   206
webertj@20412
   207
fun eq_arith_tactic (ArithTactic {id = id1, ...}, ArithTactic {id = id2, ...}) = (id1 = id2);
webertj@20412
   208
wenzelm@16424
   209
structure ArithTheoryData = TheoryDataFun
wenzelm@22846
   210
(
webertj@20268
   211
  type T = {splits: thm list,
webertj@20268
   212
            inj_consts: (string * typ) list,
webertj@20268
   213
            discrete: string list,
webertj@20412
   214
            tactics: arithtactic list};
webertj@20412
   215
  val empty = {splits = [], inj_consts = [], discrete = [], tactics = []};
wenzelm@9436
   216
  val copy = I;
wenzelm@16424
   217
  val extend = I;
webertj@20412
   218
  fun merge _ ({splits= splits1, inj_consts= inj_consts1, discrete= discrete1, tactics= tactics1},
webertj@20412
   219
             {splits= splits2, inj_consts= inj_consts2, discrete= discrete2, tactics= tactics2}) =
haftmann@22634
   220
   {splits = Library.merge Thm.eq_thm_prop (splits1, splits2),
haftmann@22634
   221
    inj_consts = Library.merge (op =) (inj_consts1, inj_consts2),
haftmann@22634
   222
    discrete = Library.merge (op =) (discrete1, discrete2),
haftmann@22634
   223
    tactics = Library.merge eq_arith_tactic (tactics1, tactics2)};
wenzelm@22846
   224
);
wenzelm@9436
   225
wenzelm@18728
   226
val arith_split_add = Thm.declaration_attribute (fn thm =>
wenzelm@20897
   227
  Context.mapping (ArithTheoryData.map (fn {splits,inj_consts,discrete,tactics} =>
haftmann@22634
   228
    {splits= insert Thm.eq_thm_prop thm splits, inj_consts= inj_consts, discrete= discrete, tactics= tactics})) I);
webertj@20412
   229
webertj@20412
   230
fun arith_discrete d = ArithTheoryData.map (fn {splits,inj_consts,discrete,tactics} =>
haftmann@22634
   231
  {splits = splits, inj_consts = inj_consts, discrete = insert (op =) d discrete, tactics= tactics});
wenzelm@9436
   232
webertj@20412
   233
fun arith_inj_const c = ArithTheoryData.map (fn {splits,inj_consts,discrete,tactics} =>
haftmann@22634
   234
  {splits = splits, inj_consts = insert (op =) c inj_consts, discrete = discrete, tactics= tactics});
nipkow@10574
   235
webertj@20412
   236
fun arith_tactic_add tac = ArithTheoryData.map (fn {splits,inj_consts,discrete,tactics} =>
haftmann@22634
   237
  {splits= splits, inj_consts= inj_consts, discrete= discrete, tactics= insert eq_arith_tactic tac tactics});
wenzelm@9436
   238
wenzelm@9436
   239
webertj@20217
   240
signature HOL_LIN_ARITH_DATA =
webertj@20217
   241
sig
webertj@20217
   242
  include LIN_ARITH_DATA
webertj@20217
   243
  val fast_arith_split_limit : int ref
webertj@20217
   244
end;
webertj@20217
   245
webertj@20217
   246
structure LA_Data_Ref: HOL_LIN_ARITH_DATA =
wenzelm@9436
   247
struct
wenzelm@9436
   248
webertj@20217
   249
(* internal representation of linear (in-)equations *)
webertj@20217
   250
type decompT = ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat * bool);
webertj@20217
   251
wenzelm@9436
   252
(* Decomposition of terms *)
wenzelm@9436
   253
webertj@20254
   254
fun nT (Type ("fun", [N, _])) = (N = HOLogic.natT)
webertj@20254
   255
  | nT _                      = false;
webertj@20254
   256
webertj@20271
   257
fun add_atom (t : term) (m : Rat.rat) (p : (term * Rat.rat) list, i : Rat.rat) :
webertj@20271
   258
             (term * Rat.rat) list * Rat.rat =
webertj@20254
   259
  case AList.lookup (op =) p t of NONE   => ((t, m) :: p, i)
haftmann@22947
   260
                                | SOME n => (AList.update (op =) (t, Rat.add n m) p, i);
nipkow@10693
   261
nipkow@10693
   262
exception Zero;
wenzelm@9436
   263
haftmann@21820
   264
fun rat_of_term (numt, dent) =
haftmann@21820
   265
  let
haftmann@21820
   266
    val num = HOLogic.dest_numeral numt
haftmann@21820
   267
    val den = HOLogic.dest_numeral dent
haftmann@21820
   268
  in
haftmann@21820
   269
    if den = 0 then raise Zero else Rat.rat_of_quotient (num, den)
haftmann@21820
   270
  end;
nipkow@10718
   271
nipkow@10718
   272
(* Warning: in rare cases number_of encloses a non-numeral,
haftmann@21820
   273
   in which case dest_numeral raises TERM; hence all the handles below.
nipkow@11334
   274
   Same for Suc-terms that turn out not to be numerals -
webertj@20271
   275
   although the simplifier should eliminate those anyway ...
nipkow@10718
   276
*)
webertj@20271
   277
fun number_of_Sucs (Const ("Suc", _) $ n) : int =
webertj@20271
   278
      number_of_Sucs n + 1
webertj@20271
   279
  | number_of_Sucs t =
webertj@20271
   280
      if HOLogic.is_zero t then 0 else raise TERM ("number_of_Sucs", []);
nipkow@10718
   281
webertj@20271
   282
(* decompose nested multiplications, bracketing them to the right and combining
webertj@20271
   283
   all their coefficients
nipkow@10718
   284
*)
webertj@20271
   285
fun demult (inj_consts : (string * typ) list) : term * Rat.rat -> term option * Rat.rat =
nipkow@13499
   286
let
haftmann@22997
   287
  fun demult ((mC as Const (@{const_name HOL.times}, _)) $ s $ t, m) = (
webertj@20271
   288
    (case s of
haftmann@22997
   289
      Const ("Numeral.number_class.number_of", _) $ n =>
haftmann@22947
   290
        demult (t, Rat.mult m (Rat.rat_of_int (HOLogic.dest_numeral n)))
haftmann@22997
   291
    | Const (@{const_name HOL.uminus}, _) $ (Const ("Numeral.number_class.number_of", _) $ n) =>
haftmann@22947
   292
        demult (t, Rat.mult m (Rat.rat_of_int (~(HOLogic.dest_numeral n))))
haftmann@22997
   293
    | Const (@{const_name Suc}, _) $ _ =>
haftmann@22947
   294
        demult (t, Rat.mult m (Rat.rat_of_int (HOLogic.dest_nat s)))
haftmann@22997
   295
    | Const (@{const_name HOL.times}, _) $ s1 $ s2 =>
webertj@20271
   296
        demult (mC $ s1 $ (mC $ s2 $ t), m)
haftmann@22997
   297
    | Const (@{const_name HOL.divide}, _) $ numt $ (Const ("Numeral.number_class.number_of", _) $ dent) =>
webertj@20271
   298
        let
haftmann@21820
   299
          val den = HOLogic.dest_numeral dent
webertj@20271
   300
        in
webertj@20271
   301
          if den = 0 then
webertj@20271
   302
            raise Zero
webertj@20271
   303
          else
haftmann@22947
   304
            demult (mC $ numt $ t, Rat.mult m (Rat.inv (Rat.rat_of_int den)))
webertj@20271
   305
        end
webertj@20271
   306
    | _ =>
webertj@20271
   307
        atomult (mC, s, t, m)
webertj@20271
   308
    ) handle TERM _ => atomult (mC, s, t, m)
webertj@20271
   309
  )
haftmann@22997
   310
    | demult (atom as Const(@{const_name HOL.divide}, _) $ t $ (Const ("Numeral.number_class.number_of", _) $ dent), m) =
webertj@20268
   311
      (let
haftmann@21820
   312
        val den = HOLogic.dest_numeral dent
webertj@20254
   313
      in
webertj@20268
   314
        if den = 0 then
webertj@20268
   315
          raise Zero
webertj@20268
   316
        else
haftmann@22947
   317
          demult (t, Rat.mult m (Rat.inv (Rat.rat_of_int den)))
webertj@20254
   318
      end
webertj@20268
   319
        handle TERM _ => (SOME atom, m))
haftmann@22997
   320
    | demult (Const (@{const_name HOL.zero}, _), m) = (NONE, Rat.zero)
haftmann@22997
   321
    | demult (Const (@{const_name HOL.one}, _), m) = (NONE, m)
haftmann@22997
   322
    | demult (t as Const ("Numeral.number_class.number_of", _) $ n, m) =
haftmann@22947
   323
        ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_numeral n)))
haftmann@22947
   324
          handle TERM _ => (SOME t, m))
haftmann@22997
   325
    | demult (Const (@{const_name HOL.uminus}, _) $ t, m) = demult (t, Rat.neg m)
webertj@20268
   326
    | demult (t as Const f $ x, m) =
haftmann@22997
   327
        (if member (op =) inj_consts f then SOME x else SOME t, m)
webertj@20268
   328
    | demult (atom, m) = (SOME atom, m)
webertj@20254
   329
and
webertj@20254
   330
  atomult (mC, atom, t, m) = (
webertj@20254
   331
    case demult (t, m) of (NONE, m')    => (SOME atom, m')
webertj@20254
   332
                        | (SOME t', m') => (SOME (mC $ atom $ t'), m')
webertj@20254
   333
  )
nipkow@13499
   334
in demult end;
nipkow@10718
   335
webertj@20271
   336
fun decomp0 (inj_consts : (string * typ) list) (rel : string, lhs : term, rhs : term) :
webertj@20271
   337
            ((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat) option =
nipkow@10574
   338
let
webertj@20254
   339
  (* Turn term into list of summand * multiplicity plus a constant *)
haftmann@22997
   340
  fun poly (Const (@{const_name HOL.plus}, _) $ s $ t, m : Rat.rat, pi : (term * Rat.rat) list * Rat.rat) =
webertj@20271
   341
        poly (s, m, poly (t, m, pi))
haftmann@22997
   342
    | poly (all as Const (@{const_name HOL.minus}, T) $ s $ t, m, pi) =
webertj@20271
   343
        if nT T then add_atom all m pi else poly (s, m, poly (t, Rat.neg m, pi))
haftmann@22997
   344
    | poly (all as Const (@{const_name HOL.uminus}, T) $ t, m, pi) =
webertj@20271
   345
        if nT T then add_atom all m pi else poly (t, Rat.neg m, pi)
haftmann@22997
   346
    | poly (Const (@{const_name HOL.zero}, _), _, pi) =
webertj@20271
   347
        pi
haftmann@22997
   348
    | poly (Const (@{const_name HOL.one}, _), m, (p, i)) =
haftmann@22947
   349
        (p, Rat.add i m)
haftmann@22997
   350
    | poly (Const (@{const_name Suc}, _) $ t, m, (p, i)) =
haftmann@22947
   351
        poly (t, m, (p, Rat.add i m))
haftmann@22997
   352
    | poly (all as Const (@{const_name HOL.times}, _) $ _ $ _, m, pi as (p, i)) =
webertj@20271
   353
        (case demult inj_consts (all, m) of
haftmann@22947
   354
           (NONE,   m') => (p, Rat.add i m')
webertj@20271
   355
         | (SOME u, m') => add_atom u m' pi)
haftmann@22997
   356
    | poly (all as Const (@{const_name HOL.divide}, _) $ _ $ _, m, pi as (p, i)) =
webertj@20271
   357
        (case demult inj_consts (all, m) of
haftmann@22947
   358
           (NONE,   m') => (p, Rat.add i m')
webertj@20271
   359
         | (SOME u, m') => add_atom u m' pi)
haftmann@22997
   360
    | poly (all as Const ("Numeral.number_class.number_of", Type(_,[_,T])) $ t, m, pi as (p, i)) =
haftmann@21820
   361
        (let val k = HOLogic.dest_numeral t
nipkow@20859
   362
            val k2 = if k < 0 andalso T = HOLogic.natT then 0 else k
haftmann@22947
   363
        in (p, Rat.add i (Rat.mult m (Rat.rat_of_int k2))) end
nipkow@20859
   364
        handle TERM _ => add_atom all m pi)
webertj@20271
   365
    | poly (all as Const f $ x, m, pi) =
webertj@20271
   366
        if f mem inj_consts then poly (x, m, pi) else add_atom all m pi
webertj@20271
   367
    | poly (all, m, pi) =
webertj@20271
   368
        add_atom all m pi
haftmann@22947
   369
  val (p, i) = poly (lhs, Rat.one, ([], Rat.zero))
haftmann@22947
   370
  val (q, j) = poly (rhs, Rat.one, ([], Rat.zero))
webertj@20254
   371
in
webertj@20254
   372
  case rel of
haftmann@23881
   373
    @{const_name HOL.less}    => SOME (p, i, "<", q, j)
haftmann@23881
   374
  | @{const_name HOL.less_eq} => SOME (p, i, "<=", q, j)
webertj@20254
   375
  | "op ="              => SOME (p, i, "=", q, j)
webertj@20254
   376
  | _                   => NONE
webertj@20254
   377
end handle Zero => NONE;
wenzelm@9436
   378
webertj@20271
   379
fun of_lin_arith_sort sg (U : typ) : bool =
webertj@20254
   380
  Type.of_sort (Sign.tsig_of sg) (U, ["Ring_and_Field.ordered_idom"])
nipkow@15121
   381
webertj@20271
   382
fun allows_lin_arith sg (discrete : string list) (U as Type (D, [])) : bool * bool =
webertj@20254
   383
  if of_lin_arith_sort sg U then
webertj@20254
   384
    (true, D mem discrete)
webertj@20254
   385
  else (* special cases *)
webertj@20271
   386
    if D mem discrete then  (true, true)  else  (false, false)
webertj@20254
   387
  | allows_lin_arith sg discrete U =
webertj@20254
   388
  (of_lin_arith_sort sg U, false);
nipkow@15121
   389
webertj@20271
   390
fun decomp_typecheck (sg, discrete, inj_consts) (T : typ, xxx) : decompT option =
webertj@20271
   391
  case T of
webertj@20271
   392
    Type ("fun", [U, _]) =>
webertj@20271
   393
      (case allows_lin_arith sg discrete U of
webertj@20271
   394
        (true, d) =>
webertj@20271
   395
          (case decomp0 inj_consts xxx of
webertj@20271
   396
            NONE                   => NONE
webertj@20271
   397
          | SOME (p, i, rel, q, j) => SOME (p, i, rel, q, j, d))
webertj@20271
   398
      | (false, _) =>
webertj@20271
   399
          NONE)
webertj@20271
   400
  | _ => NONE;
wenzelm@9436
   401
webertj@20271
   402
fun negate (SOME (x, i, rel, y, j, d)) = SOME (x, i, "~" ^ rel, y, j, d)
webertj@20271
   403
  | negate NONE                        = NONE;
wenzelm@9436
   404
webertj@23190
   405
fun decomp_negation data
webertj@23190
   406
  ((Const ("Trueprop", _)) $ (Const (rel, T) $ lhs $ rhs)) : decompT option =
webertj@20271
   407
      decomp_typecheck data (T, (rel, lhs, rhs))
webertj@23190
   408
  | decomp_negation data ((Const ("Trueprop", _)) $
webertj@23190
   409
  (Const ("Not", _) $ (Const (rel, T) $ lhs $ rhs))) =
webertj@20271
   410
      negate (decomp_typecheck data (T, (rel, lhs, rhs)))
webertj@20271
   411
  | decomp_negation data _ =
webertj@20271
   412
      NONE;
webertj@20271
   413
webertj@20271
   414
fun decomp sg : term -> decompT option =
webertj@20254
   415
let
webertj@20254
   416
  val {discrete, inj_consts, ...} = ArithTheoryData.get sg
webertj@20254
   417
in
webertj@20271
   418
  decomp_negation (sg, discrete, inj_consts)
webertj@20254
   419
end;
wenzelm@9436
   420
webertj@20276
   421
fun domain_is_nat (_ $ (Const (_, T) $ _ $ _))                      = nT T
webertj@20276
   422
  | domain_is_nat (_ $ (Const ("Not", _) $ (Const (_, T) $ _ $ _))) = nT T
webertj@20276
   423
  | domain_is_nat _                                                 = false;
webertj@20276
   424
haftmann@21820
   425
fun number_of (n, T) = HOLogic.mk_number T n;
nipkow@10693
   426
webertj@20217
   427
(*---------------------------------------------------------------------------*)
webertj@20217
   428
(* code that performs certain goal transformations for linear arithmetic     *)
webertj@20217
   429
(*---------------------------------------------------------------------------*)
webertj@20217
   430
webertj@20217
   431
(* A "do nothing" variant of pre_decomp and pre_tac:
webertj@20217
   432
webertj@20217
   433
fun pre_decomp sg Ts termitems = [termitems];
webertj@20217
   434
fun pre_tac i = all_tac;
webertj@20217
   435
*)
webertj@20217
   436
webertj@20217
   437
(*---------------------------------------------------------------------------*)
webertj@20217
   438
(* the following code performs splitting of certain constants (e.g. min,     *)
webertj@20217
   439
(* max) in a linear arithmetic problem; similar to what split_tac later does *)
webertj@20217
   440
(* to the proof state                                                        *)
webertj@20217
   441
(*---------------------------------------------------------------------------*)
webertj@20217
   442
webertj@20217
   443
val fast_arith_split_limit = ref 9;
webertj@20217
   444
webertj@20268
   445
(* checks if splitting with 'thm' is implemented                             *)
webertj@20217
   446
webertj@20268
   447
fun is_split_thm (thm : thm) : bool =
webertj@20268
   448
  case concl_of thm of _ $ (_ $ (_ $ lhs) $ _) => (
webertj@20268
   449
    (* Trueprop $ ((op =) $ (?P $ lhs) $ rhs) *)
webertj@20268
   450
    case head_of lhs of
haftmann@22997
   451
      Const (a, _) => member (op =) [@{const_name Orderings.max},
haftmann@22997
   452
                                    @{const_name Orderings.min},
haftmann@22997
   453
                                    @{const_name HOL.abs},
haftmann@22997
   454
                                    @{const_name HOL.minus},
webertj@20268
   455
                                    "IntDef.nat",
haftmann@22997
   456
                                    "Divides.div_class.mod",
haftmann@22997
   457
                                    "Divides.div_class.div"] a
webertj@20268
   458
    | _            => (warning ("Lin. Arith.: wrong format for split rule " ^
webertj@20268
   459
                                 Display.string_of_thm thm);
webertj@20268
   460
                       false))
webertj@20268
   461
  | _ => (warning ("Lin. Arith.: wrong format for split rule " ^
webertj@20268
   462
                   Display.string_of_thm thm);
webertj@20268
   463
          false);
webertj@20217
   464
webertj@20217
   465
(* substitute new for occurrences of old in a term, incrementing bound       *)
webertj@20217
   466
(* variables as needed when substituting inside an abstraction               *)
webertj@20217
   467
webertj@20268
   468
fun subst_term ([] : (term * term) list) (t : term) = t
webertj@20268
   469
  | subst_term pairs                     t          =
webertj@20217
   470
      (case AList.lookup (op aconv) pairs t of
webertj@20217
   471
        SOME new =>
webertj@20217
   472
          new
webertj@20217
   473
      | NONE     =>
webertj@20217
   474
          (case t of Abs (a, T, body) =>
webertj@20217
   475
            let val pairs' = map (pairself (incr_boundvars 1)) pairs
webertj@20217
   476
            in  Abs (a, T, subst_term pairs' body)  end
webertj@20217
   477
          | t1 $ t2                   =>
webertj@20217
   478
            subst_term pairs t1 $ subst_term pairs t2
webertj@20217
   479
          | _ => t));
webertj@20217
   480
webertj@20217
   481
(* approximates the effect of one application of split_tac (followed by NNF  *)
webertj@20217
   482
(* normalization) on the subgoal represented by '(Ts, terms)'; returns a     *)
webertj@20217
   483
(* list of new subgoals (each again represented by a typ list for bound      *)
webertj@20217
   484
(* variables and a term list for premises), or NONE if split_tac would fail  *)
webertj@20217
   485
(* on the subgoal                                                            *)
webertj@20217
   486
webertj@20217
   487
(* FIXME: currently only the effect of certain split theorems is reproduced  *)
webertj@20217
   488
(*        (which is why we need 'is_split_thm').  A more canonical           *)
webertj@20217
   489
(*        implementation should analyze the right-hand side of the split     *)
webertj@20217
   490
(*        theorem that can be applied, and modify the subgoal accordingly.   *)
webertj@20268
   491
(*        Or even better, the splitter should be extended to provide         *)
webertj@20268
   492
(*        splitting on terms as well as splitting on theorems (where the     *)
webertj@20268
   493
(*        former can have a faster implementation as it does not need to be  *)
webertj@20268
   494
(*        proof-producing).                                                  *)
webertj@20217
   495
webertj@20268
   496
fun split_once_items (sg : theory) (Ts : typ list, terms : term list) :
webertj@20268
   497
                     (typ list * term list) list option =
webertj@20217
   498
let
webertj@20217
   499
  (* takes a list  [t1, ..., tn]  to the term                                *)
webertj@20217
   500
  (*   tn' --> ... --> t1' --> False  ,                                      *)
webertj@20217
   501
  (* where ti' = HOLogic.dest_Trueprop ti                                    *)
webertj@20217
   502
  (* term list -> term *)
webertj@20217
   503
  fun REPEAT_DETERM_etac_rev_mp terms' =
webertj@20217
   504
    fold (curry HOLogic.mk_imp) (map HOLogic.dest_Trueprop terms') HOLogic.false_const
webertj@20217
   505
  val split_thms = filter is_split_thm (#splits (ArithTheoryData.get sg))
webertj@20217
   506
  val cmap       = Splitter.cmap_of_split_thms split_thms
webertj@20217
   507
  val splits     = Splitter.split_posns cmap sg Ts (REPEAT_DETERM_etac_rev_mp terms)
webertj@20217
   508
in
webertj@20217
   509
  if length splits > !fast_arith_split_limit then (
webertj@20268
   510
    tracing ("fast_arith_split_limit exceeded (current value is " ^
webertj@20268
   511
              string_of_int (!fast_arith_split_limit) ^ ")");
webertj@20217
   512
    NONE
webertj@20217
   513
  ) else (
webertj@20217
   514
  case splits of [] =>
webertj@20268
   515
    (* split_tac would fail: no possible split *)
webertj@20268
   516
    NONE
webertj@20268
   517
  | ((_, _, _, split_type, split_term) :: _) => (
webertj@20268
   518
    (* ignore all but the first possible split *)
webertj@20217
   519
    case strip_comb split_term of
webertj@20217
   520
    (* ?P (max ?i ?j) = ((?i <= ?j --> ?P ?j) & (~ ?i <= ?j --> ?P ?i)) *)
haftmann@22997
   521
      (Const (@{const_name Orderings.max}, _), [t1, t2]) =>
webertj@20217
   522
      let
webertj@20217
   523
        val rev_terms     = rev terms
webertj@20217
   524
        val terms1        = map (subst_term [(split_term, t1)]) rev_terms
webertj@20217
   525
        val terms2        = map (subst_term [(split_term, t2)]) rev_terms
haftmann@23881
   526
        val t1_leq_t2     = Const (@{const_name HOL.less_eq},
webertj@20268
   527
                                    split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
webertj@20217
   528
        val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
webertj@20217
   529
        val not_false     = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
webertj@20217
   530
        val subgoal1      = (HOLogic.mk_Trueprop t1_leq_t2) :: terms2 @ [not_false]
webertj@20217
   531
        val subgoal2      = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms1 @ [not_false]
webertj@20217
   532
      in
webertj@20217
   533
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
webertj@20217
   534
      end
webertj@20217
   535
    (* ?P (min ?i ?j) = ((?i <= ?j --> ?P ?i) & (~ ?i <= ?j --> ?P ?j)) *)
haftmann@22997
   536
    | (Const (@{const_name Orderings.min}, _), [t1, t2]) =>
webertj@20217
   537
      let
webertj@20217
   538
        val rev_terms     = rev terms
webertj@20217
   539
        val terms1        = map (subst_term [(split_term, t1)]) rev_terms
webertj@20217
   540
        val terms2        = map (subst_term [(split_term, t2)]) rev_terms
haftmann@23881
   541
        val t1_leq_t2     = Const (@{const_name HOL.less_eq},
webertj@20268
   542
                                    split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
webertj@20217
   543
        val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
webertj@20217
   544
        val not_false     = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
webertj@20217
   545
        val subgoal1      = (HOLogic.mk_Trueprop t1_leq_t2) :: terms1 @ [not_false]
webertj@20217
   546
        val subgoal2      = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms2 @ [not_false]
webertj@20217
   547
      in
webertj@20217
   548
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
webertj@20217
   549
      end
webertj@20217
   550
    (* ?P (abs ?a) = ((0 <= ?a --> ?P ?a) & (?a < 0 --> ?P (- ?a))) *)
haftmann@22997
   551
    | (Const (@{const_name HOL.abs}, _), [t1]) =>
webertj@20217
   552
      let
webertj@20268
   553
        val rev_terms   = rev terms
webertj@20268
   554
        val terms1      = map (subst_term [(split_term, t1)]) rev_terms
haftmann@22997
   555
        val terms2      = map (subst_term [(split_term, Const (@{const_name HOL.uminus},
webertj@20268
   556
                            split_type --> split_type) $ t1)]) rev_terms
haftmann@22997
   557
        val zero        = Const (@{const_name HOL.zero}, split_type)
haftmann@23881
   558
        val zero_leq_t1 = Const (@{const_name HOL.less_eq},
webertj@20268
   559
                            split_type --> split_type --> HOLogic.boolT) $ zero $ t1
haftmann@23881
   560
        val t1_lt_zero  = Const (@{const_name HOL.less},
webertj@20268
   561
                            split_type --> split_type --> HOLogic.boolT) $ t1 $ zero
webertj@20268
   562
        val not_false   = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
webertj@20268
   563
        val subgoal1    = (HOLogic.mk_Trueprop zero_leq_t1) :: terms1 @ [not_false]
webertj@20268
   564
        val subgoal2    = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
webertj@20217
   565
      in
webertj@20217
   566
        SOME [(Ts, subgoal1), (Ts, subgoal2)]
webertj@20217
   567
      end
webertj@20217
   568
    (* ?P (?a - ?b) = ((?a < ?b --> ?P 0) & (ALL d. ?a = ?b + d --> ?P d)) *)
haftmann@22997
   569
    | (Const (@{const_name HOL.minus}, _), [t1, t2]) =>
webertj@20217
   570
      let
webertj@20217
   571
        (* "d" in the above theorem becomes a new bound variable after NNF   *)
webertj@20217
   572
        (* transformation, therefore some adjustment of indices is necessary *)
webertj@20217
   573
        val rev_terms       = rev terms
haftmann@22997
   574
        val zero            = Const (@{const_name HOL.zero}, split_type)
webertj@20217
   575
        val d               = Bound 0
webertj@20217
   576
        val terms1          = map (subst_term [(split_term, zero)]) rev_terms
webertj@20268
   577
        val terms2          = map (subst_term [(incr_boundvars 1 split_term, d)])
webertj@20268
   578
                                (map (incr_boundvars 1) rev_terms)
webertj@20217
   579
        val t1'             = incr_boundvars 1 t1
webertj@20217
   580
        val t2'             = incr_boundvars 1 t2
haftmann@23881
   581
        val t1_lt_t2        = Const (@{const_name HOL.less},
webertj@20268
   582
                                split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
webertj@20268
   583
        val t1_eq_t2_plus_d = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
haftmann@22997
   584
                                (Const (@{const_name HOL.plus},
webertj@20268
   585
                                  split_type --> split_type --> split_type) $ t2' $ d)
webertj@20217
   586
        val not_false       = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
webertj@20217
   587
        val subgoal1        = (HOLogic.mk_Trueprop t1_lt_t2) :: terms1 @ [not_false]
webertj@20217
   588
        val subgoal2        = (HOLogic.mk_Trueprop t1_eq_t2_plus_d) :: terms2 @ [not_false]
webertj@20217
   589
      in
webertj@20217
   590
        SOME [(Ts, subgoal1), (split_type :: Ts, subgoal2)]
webertj@20217
   591
      end
webertj@20217
   592
    (* ?P (nat ?i) = ((ALL n. ?i = int n --> ?P n) & (?i < 0 --> ?P 0)) *)
webertj@20217
   593
    | (Const ("IntDef.nat", _), [t1]) =>
webertj@20217
   594
      let
webertj@20217
   595
        val rev_terms   = rev terms
haftmann@22997
   596
        val zero_int    = Const (@{const_name HOL.zero}, HOLogic.intT)
haftmann@22997
   597
        val zero_nat    = Const (@{const_name HOL.zero}, HOLogic.natT)
webertj@20217
   598
        val n           = Bound 0
webertj@20268
   599
        val terms1      = map (subst_term [(incr_boundvars 1 split_term, n)])
webertj@20268
   600
                            (map (incr_boundvars 1) rev_terms)
webertj@20217
   601
        val terms2      = map (subst_term [(split_term, zero_nat)]) rev_terms
webertj@20217
   602
        val t1'         = incr_boundvars 1 t1
webertj@20268
   603
        val t1_eq_int_n = Const ("op =", HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1' $
huffman@23365
   604
                            (Const ("Nat.of_nat", HOLogic.natT --> HOLogic.intT) $ n)
haftmann@23881
   605
        val t1_lt_zero  = Const (@{const_name HOL.less},
webertj@20268
   606
                            HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1 $ zero_int
webertj@20217
   607
        val not_false   = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
webertj@20217
   608
        val subgoal1    = (HOLogic.mk_Trueprop t1_eq_int_n) :: terms1 @ [not_false]
webertj@20217
   609
        val subgoal2    = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
webertj@20217
   610
      in
webertj@20217
   611
        SOME [(HOLogic.natT :: Ts, subgoal1), (Ts, subgoal2)]
webertj@20217
   612
      end
webertj@20268
   613
    (* "?P ((?n::nat) mod (number_of ?k)) =
webertj@20268
   614
         ((number_of ?k = 0 --> ?P ?n) & (~ (number_of ?k = 0) -->
webertj@20268
   615
           (ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P j))) *)
haftmann@22997
   616
    | (Const ("Divides.div_class.mod", Type ("fun", [Type ("nat", []), _])), [t1, t2]) =>
webertj@20217
   617
      let
webertj@20217
   618
        val rev_terms               = rev terms
haftmann@22997
   619
        val zero                    = Const (@{const_name HOL.zero}, split_type)
webertj@20217
   620
        val i                       = Bound 1
webertj@20217
   621
        val j                       = Bound 0
webertj@20217
   622
        val terms1                  = map (subst_term [(split_term, t1)]) rev_terms
webertj@20268
   623
        val terms2                  = map (subst_term [(incr_boundvars 2 split_term, j)])
webertj@20268
   624
                                        (map (incr_boundvars 2) rev_terms)
webertj@20217
   625
        val t1'                     = incr_boundvars 2 t1
webertj@20217
   626
        val t2'                     = incr_boundvars 2 t2
webertj@20268
   627
        val t2_eq_zero              = Const ("op =",
webertj@20268
   628
                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
webertj@20268
   629
        val t2_neq_zero             = HOLogic.mk_not (Const ("op =",
webertj@20268
   630
                                        split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
haftmann@23881
   631
        val j_lt_t2                 = Const (@{const_name HOL.less},
webertj@20268
   632
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
webertj@20217
   633
        val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
haftmann@22997
   634
                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
haftmann@22997
   635
                                         (Const (@{const_name HOL.times},
webertj@20268
   636
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
webertj@20217
   637
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
webertj@20217
   638
        val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
webertj@20268
   639
        val subgoal2                = (map HOLogic.mk_Trueprop
webertj@20268
   640
                                        [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
webertj@20268
   641
                                          @ terms2 @ [not_false]
webertj@20217
   642
      in
webertj@20217
   643
        SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
webertj@20217
   644
      end
webertj@20268
   645
    (* "?P ((?n::nat) div (number_of ?k)) =
webertj@20268
   646
         ((number_of ?k = 0 --> ?P 0) & (~ (number_of ?k = 0) -->
webertj@20268
   647
           (ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P i))) *)
haftmann@22997
   648
    | (Const ("Divides.div_class.div", Type ("fun", [Type ("nat", []), _])), [t1, t2]) =>
webertj@20217
   649
      let
webertj@20217
   650
        val rev_terms               = rev terms
haftmann@22997
   651
        val zero                    = Const (@{const_name HOL.zero}, split_type)
webertj@20217
   652
        val i                       = Bound 1
webertj@20217
   653
        val j                       = Bound 0
webertj@20217
   654
        val terms1                  = map (subst_term [(split_term, zero)]) rev_terms
webertj@20268
   655
        val terms2                  = map (subst_term [(incr_boundvars 2 split_term, i)])
webertj@20268
   656
                                        (map (incr_boundvars 2) rev_terms)
webertj@20217
   657
        val t1'                     = incr_boundvars 2 t1
webertj@20217
   658
        val t2'                     = incr_boundvars 2 t2
webertj@20268
   659
        val t2_eq_zero              = Const ("op =",
webertj@20268
   660
                                        split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
webertj@20268
   661
        val t2_neq_zero             = HOLogic.mk_not (Const ("op =",
webertj@20268
   662
                                        split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
haftmann@23881
   663
        val j_lt_t2                 = Const (@{const_name HOL.less},
webertj@20268
   664
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
webertj@20217
   665
        val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
haftmann@22997
   666
                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
haftmann@22997
   667
                                         (Const (@{const_name HOL.times},
webertj@20268
   668
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
webertj@20217
   669
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
webertj@20217
   670
        val subgoal1                = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
webertj@20268
   671
        val subgoal2                = (map HOLogic.mk_Trueprop
webertj@20268
   672
                                        [t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
webertj@20268
   673
                                          @ terms2 @ [not_false]
webertj@20217
   674
      in
webertj@20217
   675
        SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
webertj@20217
   676
      end
webertj@20268
   677
    (* "?P ((?n::int) mod (number_of ?k)) =
webertj@20268
   678
         ((iszero (number_of ?k) --> ?P ?n) &
haftmann@20485
   679
          (neg (number_of (uminus ?k)) -->
webertj@20268
   680
            (ALL i j. 0 <= j & j < number_of ?k & ?n = number_of ?k * i + j --> ?P j)) &
webertj@20268
   681
          (neg (number_of ?k) -->
webertj@20268
   682
            (ALL i j. number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j --> ?P j))) *)
haftmann@22997
   683
    | (Const ("Divides.div_class.mod",
webertj@20268
   684
        Type ("fun", [Type ("IntDef.int", []), _])), [t1, t2 as (number_of $ k)]) =>
webertj@20217
   685
      let
webertj@20217
   686
        val rev_terms               = rev terms
haftmann@22997
   687
        val zero                    = Const (@{const_name HOL.zero}, split_type)
webertj@20217
   688
        val i                       = Bound 1
webertj@20217
   689
        val j                       = Bound 0
webertj@20217
   690
        val terms1                  = map (subst_term [(split_term, t1)]) rev_terms
webertj@20268
   691
        val terms2_3                = map (subst_term [(incr_boundvars 2 split_term, j)])
webertj@20268
   692
                                        (map (incr_boundvars 2) rev_terms)
webertj@20217
   693
        val t1'                     = incr_boundvars 2 t1
webertj@20217
   694
        val (t2' as (_ $ k'))       = incr_boundvars 2 t2
webertj@20217
   695
        val iszero_t2               = Const ("IntDef.iszero", split_type --> HOLogic.boolT) $ t2
webertj@20217
   696
        val neg_minus_k             = Const ("IntDef.neg", split_type --> HOLogic.boolT) $
webertj@20268
   697
                                        (number_of $
haftmann@22997
   698
                                          (Const (@{const_name HOL.uminus},
haftmann@20485
   699
                                            HOLogic.intT --> HOLogic.intT) $ k'))
haftmann@23881
   700
        val zero_leq_j              = Const (@{const_name HOL.less_eq},
webertj@20268
   701
                                        split_type --> split_type --> HOLogic.boolT) $ zero $ j
haftmann@23881
   702
        val j_lt_t2                 = Const (@{const_name HOL.less},
webertj@20268
   703
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
webertj@20217
   704
        val t1_eq_t2_times_i_plus_j = Const ("op =", split_type --> split_type --> HOLogic.boolT) $ t1' $
haftmann@22997
   705
                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
haftmann@22997
   706
                                         (Const (@{const_name HOL.times},
webertj@20268
   707
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
webertj@20217
   708
        val neg_t2                  = Const ("IntDef.neg", split_type --> HOLogic.boolT) $ t2'
haftmann@23881
   709
        val t2_lt_j                 = Const (@{const_name HOL.less},
webertj@20268
   710
                                        split_type --> split_type--> HOLogic.boolT) $ t2' $ j
haftmann@23881
   711
        val j_leq_zero              = Const (@{const_name HOL.less_eq},
webertj@20268
   712
                                        split_type --> split_type --> HOLogic.boolT) $ j $ zero
webertj@20217
   713
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
webertj@20217
   714
        val subgoal1                = (HOLogic.mk_Trueprop iszero_t2) :: terms1 @ [not_false]
webertj@20217
   715
        val subgoal2                = (map HOLogic.mk_Trueprop [neg_minus_k, zero_leq_j])
webertj@20217
   716
                                        @ hd terms2_3
webertj@20217
   717
                                        :: (if tl terms2_3 = [] then [not_false] else [])
webertj@20217
   718
                                        @ (map HOLogic.mk_Trueprop [j_lt_t2, t1_eq_t2_times_i_plus_j])
webertj@20217
   719
                                        @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
webertj@20217
   720
        val subgoal3                = (map HOLogic.mk_Trueprop [neg_t2, t2_lt_j])
webertj@20217
   721
                                        @ hd terms2_3
webertj@20217
   722
                                        :: (if tl terms2_3 = [] then [not_false] else [])
webertj@20217
   723
                                        @ (map HOLogic.mk_Trueprop [j_leq_zero, t1_eq_t2_times_i_plus_j])
webertj@20217
   724
                                        @ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
webertj@20217
   725
        val Ts'                     = split_type :: split_type :: Ts
webertj@20217
   726
      in
webertj@20217
   727
        SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
webertj@20217
   728
      end
webertj@20268
   729
    (* "?P ((?n::int) div (number_of ?k)) =
webertj@20268
   730
         ((iszero (number_of ?k) --> ?P 0) &
haftmann@20485
   731
          (neg (number_of (uminus ?k)) -->
webertj@20268
   732
            (ALL i. (EX j. 0 <= j & j < number_of ?k & ?n = number_of ?k * i + j) --> ?P i)) &
webertj@20268
   733
          (neg (number_of ?k) -->
webertj@20268
   734
            (ALL i. (EX j. number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j) --> ?P i))) *)
haftmann@22997
   735
    | (Const ("Divides.div_class.div",
webertj@20268
   736
        Type ("fun", [Type ("IntDef.int", []), _])), [t1, t2 as (number_of $ k)]) =>
webertj@20217
   737
      let
webertj@20217
   738
        val rev_terms               = rev terms
haftmann@22997
   739
        val zero                    = Const (@{const_name HOL.zero}, split_type)
webertj@20217
   740
        val i                       = Bound 1
webertj@20217
   741
        val j                       = Bound 0
webertj@20217
   742
        val terms1                  = map (subst_term [(split_term, zero)]) rev_terms
webertj@20268
   743
        val terms2_3                = map (subst_term [(incr_boundvars 2 split_term, i)])
webertj@20268
   744
                                        (map (incr_boundvars 2) rev_terms)
webertj@20217
   745
        val t1'                     = incr_boundvars 2 t1
webertj@20217
   746
        val (t2' as (_ $ k'))       = incr_boundvars 2 t2
webertj@20217
   747
        val iszero_t2               = Const ("IntDef.iszero", split_type --> HOLogic.boolT) $ t2
webertj@20217
   748
        val neg_minus_k             = Const ("IntDef.neg", split_type --> HOLogic.boolT) $
webertj@20268
   749
                                        (number_of $
haftmann@22997
   750
                                          (Const (@{const_name HOL.uminus},
haftmann@20485
   751
                                            HOLogic.intT --> HOLogic.intT) $ k'))
haftmann@23881
   752
        val zero_leq_j              = Const (@{const_name HOL.less_eq},
webertj@20268
   753
                                        split_type --> split_type --> HOLogic.boolT) $ zero $ j
haftmann@23881
   754
        val j_lt_t2                 = Const (@{const_name HOL.less},
webertj@20268
   755
                                        split_type --> split_type--> HOLogic.boolT) $ j $ t2'
webertj@20268
   756
        val t1_eq_t2_times_i_plus_j = Const ("op =",
webertj@20268
   757
                                        split_type --> split_type --> HOLogic.boolT) $ t1' $
haftmann@22997
   758
                                       (Const (@{const_name HOL.plus}, split_type --> split_type --> split_type) $
haftmann@22997
   759
                                         (Const (@{const_name HOL.times},
webertj@20268
   760
                                           split_type --> split_type --> split_type) $ t2' $ i) $ j)
webertj@20217
   761
        val neg_t2                  = Const ("IntDef.neg", split_type --> HOLogic.boolT) $ t2'
haftmann@23881
   762
        val t2_lt_j                 = Const (@{const_name HOL.less},
webertj@20268
   763
                                        split_type --> split_type--> HOLogic.boolT) $ t2' $ j
haftmann@23881
   764
        val j_leq_zero              = Const (@{const_name HOL.less_eq},
webertj@20268
   765
                                        split_type --> split_type --> HOLogic.boolT) $ j $ zero
webertj@20217
   766
        val not_false               = HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.false_const)
webertj@20217
   767
        val subgoal1                = (HOLogic.mk_Trueprop iszero_t2) :: terms1 @ [not_false]
webertj@20217
   768
        val subgoal2                = (HOLogic.mk_Trueprop neg_minus_k)
webertj@20217
   769
                                        :: terms2_3
webertj@20217
   770
                                        @ not_false
webertj@20268
   771
                                        :: (map HOLogic.mk_Trueprop
webertj@20268
   772
                                             [zero_leq_j, j_lt_t2, t1_eq_t2_times_i_plus_j])
webertj@20217
   773
        val subgoal3                = (HOLogic.mk_Trueprop neg_t2)
webertj@20217
   774
                                        :: terms2_3
webertj@20217
   775
                                        @ not_false
webertj@20268
   776
                                        :: (map HOLogic.mk_Trueprop
webertj@20268
   777
                                             [t2_lt_j, j_leq_zero, t1_eq_t2_times_i_plus_j])
webertj@20217
   778
        val Ts'                     = split_type :: split_type :: Ts
webertj@20217
   779
      in
webertj@20217
   780
        SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
webertj@20217
   781
      end
webertj@20268
   782
    (* this will only happen if a split theorem can be applied for which no  *)
webertj@20268
   783
    (* code exists above -- in which case either the split theorem should be *)
webertj@20268
   784
    (* implemented above, or 'is_split_thm' should be modified to filter it  *)
webertj@20268
   785
    (* out                                                                   *)
webertj@20217
   786
    | (t, ts) => (
webertj@20268
   787
      warning ("Lin. Arith.: split rule for " ^ Sign.string_of_term sg t ^
webertj@20268
   788
               " (with " ^ Int.toString (length ts) ^
webertj@20217
   789
               " argument(s)) not implemented; proof reconstruction is likely to fail");
webertj@20217
   790
      NONE
webertj@20217
   791
    ))
webertj@20217
   792
  )
wenzelm@9436
   793
end;
wenzelm@9436
   794
webertj@20268
   795
(* remove terms that do not satisfy 'p'; change the order of the remaining   *)
webertj@20217
   796
(* terms in the same way as filter_prems_tac does                            *)
webertj@20217
   797
webertj@20268
   798
fun filter_prems_tac_items (p : term -> bool) (terms : term list) : term list =
webertj@20217
   799
let
webertj@20217
   800
  fun filter_prems (t, (left, right)) =
webertj@20217
   801
    if  p t  then  (left, right @ [t])  else  (left @ right, [])
webertj@20217
   802
  val (left, right) = foldl filter_prems ([], []) terms
webertj@20217
   803
in
webertj@20217
   804
  right @ left
webertj@20217
   805
end;
webertj@20217
   806
webertj@20217
   807
(* return true iff TRY (etac notE) THEN eq_assume_tac would succeed on a     *)
webertj@20217
   808
(* subgoal that has 'terms' as premises                                      *)
webertj@20217
   809
webertj@20268
   810
fun negated_term_occurs_positively (terms : term list) : bool =
webertj@20268
   811
  List.exists
webertj@20268
   812
    (fn (Trueprop $ (Const ("Not", _) $ t)) => member (op aconv) terms (Trueprop $ t)
webertj@20268
   813
      | _                                   => false)
webertj@20268
   814
    terms;
webertj@20217
   815
webertj@20268
   816
fun pre_decomp sg (Ts : typ list, terms : term list) : (typ list * term list) list =
webertj@20217
   817
let
webertj@20217
   818
  (* repeatedly split (including newly emerging subgoals) until no further   *)
webertj@20217
   819
  (* splitting is possible                                                   *)
webertj@20271
   820
  fun split_loop ([] : (typ list * term list) list) = ([] : (typ list * term list) list)
webertj@20268
   821
    | split_loop (subgoal::subgoals)                = (
webertj@20217
   822
        case split_once_items sg subgoal of
webertj@20217
   823
          SOME new_subgoals => split_loop (new_subgoals @ subgoals)
webertj@20217
   824
        | NONE              => subgoal :: split_loop subgoals
webertj@20217
   825
      )
webertj@20217
   826
  fun is_relevant t  = isSome (decomp sg t)
webertj@20268
   827
  (* filter_prems_tac is_relevant: *)
webertj@20268
   828
  val relevant_terms = filter_prems_tac_items is_relevant terms
webertj@20268
   829
  (* split_tac, NNF normalization: *)
webertj@20268
   830
  val split_goals    = split_loop [(Ts, relevant_terms)]
webertj@20268
   831
  (* necessary because split_once_tac may normalize terms: *)
webertj@20268
   832
  val beta_eta_norm  = map (apsnd (map (Envir.eta_contract o Envir.beta_norm))) split_goals
webertj@20268
   833
  (* TRY (etac notE) THEN eq_assume_tac: *)
webertj@20268
   834
  val result         = List.filter (not o negated_term_occurs_positively o snd) beta_eta_norm
webertj@20217
   835
in
webertj@20217
   836
  result
webertj@20217
   837
end;
webertj@20217
   838
webertj@20217
   839
(* takes the i-th subgoal  [| A1; ...; An |] ==> B  to                       *)
webertj@20217
   840
(* An --> ... --> A1 --> B,  performs splitting with the given 'split_thms'  *)
webertj@20217
   841
(* (resulting in a different subgoal P), takes  P  to  ~P ==> False,         *)
webertj@20217
   842
(* performs NNF-normalization of ~P, and eliminates conjunctions,            *)
webertj@20217
   843
(* disjunctions and existential quantifiers from the premises, possibly (in  *)
webertj@20217
   844
(* the case of disjunctions) resulting in several new subgoals, each of the  *)
webertj@20217
   845
(* general form  [| Q1; ...; Qm |] ==> False.  Fails if more than            *)
webertj@20217
   846
(* !fast_arith_split_limit splits are possible.                              *)
webertj@20217
   847
webertj@20850
   848
local
webertj@20217
   849
  val nnf_simpset =
webertj@20217
   850
    empty_ss setmkeqTrue mk_eq_True
webertj@20217
   851
    setmksimps (mksimps mksimps_pairs)
webertj@20850
   852
    addsimps [imp_conv_disj, iff_conv_conj_imp, de_Morgan_disj, de_Morgan_conj,
webertj@20217
   853
      not_all, not_ex, not_not]
webertj@20217
   854
  fun prem_nnf_tac i st =
webertj@20217
   855
    full_simp_tac (Simplifier.theory_context (Thm.theory_of_thm st) nnf_simpset) i st
webertj@20850
   856
in
webertj@20850
   857
webertj@20850
   858
fun split_once_tac (split_thms : thm list) (i : int) : tactic =
webertj@20850
   859
let
webertj@20217
   860
  fun cond_split_tac i st =
webertj@20217
   861
    let
webertj@20217
   862
      val subgoal = Logic.nth_prem (i, Thm.prop_of st)
webertj@20217
   863
      val Ts      = rev (map snd (Logic.strip_params subgoal))
webertj@20217
   864
      val concl   = HOLogic.dest_Trueprop (Logic.strip_assums_concl subgoal)
webertj@20217
   865
      val cmap    = Splitter.cmap_of_split_thms split_thms
webertj@20217
   866
      val splits  = Splitter.split_posns cmap (theory_of_thm st) Ts concl
webertj@20217
   867
    in
webertj@20217
   868
      if length splits > !fast_arith_split_limit then
webertj@20217
   869
        no_tac st
webertj@20217
   870
      else
webertj@20217
   871
        split_tac split_thms i st
webertj@20217
   872
    end
webertj@20217
   873
in
webertj@20217
   874
  EVERY' [
webertj@20217
   875
    REPEAT_DETERM o etac rev_mp,
webertj@20217
   876
    cond_split_tac,
webertj@20217
   877
    rtac ccontr,
webertj@20217
   878
    prem_nnf_tac,
webertj@20217
   879
    TRY o REPEAT_ALL_NEW (DETERM o (eresolve_tac [conjE, exE] ORELSE' etac disjE))
webertj@20217
   880
  ] i
webertj@20850
   881
end
webertj@20850
   882
webertj@20850
   883
end;  (* local *)
webertj@20217
   884
webertj@20217
   885
(* remove irrelevant premises, then split the i-th subgoal (and all new      *)
webertj@20217
   886
(* subgoals) by using 'split_once_tac' repeatedly.  Beta-eta-normalize new   *)
webertj@20217
   887
(* subgoals and finally attempt to solve them by finding an immediate        *)
webertj@20217
   888
(* contradiction (i.e. a term and its negation) in their premises.           *)
webertj@20217
   889
webertj@20217
   890
fun pre_tac i st =
webertj@20217
   891
let
webertj@20217
   892
  val sg            = theory_of_thm st
webertj@20217
   893
  val split_thms    = filter is_split_thm (#splits (ArithTheoryData.get sg))
webertj@20217
   894
  fun is_relevant t = isSome (decomp sg t)
webertj@20217
   895
in
webertj@20217
   896
  DETERM (
webertj@20217
   897
    TRY (filter_prems_tac is_relevant i)
webertj@20217
   898
      THEN (
webertj@20217
   899
        (TRY o REPEAT_ALL_NEW (split_once_tac split_thms))
webertj@20217
   900
          THEN_ALL_NEW
wenzelm@23530
   901
            (CONVERSION Drule.beta_eta_conversion
webertj@20217
   902
              THEN'
webertj@20217
   903
            (TRY o (etac notE THEN' eq_assume_tac)))
webertj@20217
   904
      ) i
webertj@20217
   905
  ) st
webertj@20217
   906
end;
webertj@20217
   907
webertj@20217
   908
end;  (* LA_Data_Ref *)
webertj@20217
   909
wenzelm@9436
   910
wenzelm@9436
   911
structure Fast_Arith =
wenzelm@9436
   912
  Fast_Lin_Arith(structure LA_Logic=LA_Logic and LA_Data=LA_Data_Ref);
wenzelm@9436
   913
webertj@20217
   914
val fast_arith_tac         = Fast_Arith.lin_arith_tac false;
webertj@20217
   915
val fast_ex_arith_tac      = Fast_Arith.lin_arith_tac;
webertj@20217
   916
val trace_arith            = Fast_Arith.trace;
webertj@20217
   917
val fast_arith_neq_limit   = Fast_Arith.fast_arith_neq_limit;
webertj@20217
   918
val fast_arith_split_limit = LA_Data_Ref.fast_arith_split_limit;
wenzelm@9436
   919
wenzelm@9436
   920
(* reduce contradictory <= to False.
haftmann@22838
   921
   Most of the work is done by the cancel tactics. *)
wenzelm@9436
   922
wenzelm@9436
   923
val init_lin_arith_data =
wenzelm@18708
   924
 Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, ...} =>
nipkow@15121
   925
   {add_mono_thms = add_mono_thms @
haftmann@22838
   926
    @{thms add_mono_thms_ordered_semiring} @ @{thms add_mono_thms_ordered_field},
nipkow@10693
   927
    mult_mono_thms = mult_mono_thms,
nipkow@10574
   928
    inj_thms = inj_thms,
wenzelm@21243
   929
    lessD = lessD @ [thm "Suc_leI"],
haftmann@22887
   930
    neqE = [@{thm linorder_neqE_nat}, @{thm linorder_neqE_ordered_idom}],
haftmann@22838
   931
    simpset = HOL_basic_ss
nipkow@23085
   932
      addsimps [@{thm "monoid_add_class.zero_plus.add_0_left"}, @{thm "monoid_add_class.zero_plus.add_0_right"},
haftmann@22838
   933
        @{thm "Zero_not_Suc"}, @{thm "Suc_not_Zero"}, @{thm "le_0_eq"}, @{thm "One_nat_def"},
haftmann@22838
   934
        @{thm "order_less_irrefl"}, @{thm "zero_neq_one"}, @{thm "zero_less_one"},
haftmann@22838
   935
        @{thm "zero_le_one"}, @{thm "zero_neq_one"} RS not_sym, @{thm "not_one_le_zero"},
haftmann@22838
   936
        @{thm "not_one_less_zero"}]
haftmann@22838
   937
      addsimprocs [ab_group_add_cancel.sum_conv, ab_group_add_cancel.rel_conv]
haftmann@22838
   938
       (*abel_cancel helps it work in abstract algebraic domains*)
haftmann@22838
   939
      addsimprocs nat_cancel_sums_add}) #>
wenzelm@18708
   940
  arith_discrete "nat";
wenzelm@9436
   941
wenzelm@13462
   942
val fast_nat_arith_simproc =
wenzelm@16834
   943
  Simplifier.simproc (the_context ()) "fast_nat_arith"
wenzelm@13462
   944
    ["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"] Fast_Arith.lin_arith_prover;
wenzelm@9436
   945
wenzelm@9436
   946
(* Because of fast_nat_arith_simproc, the arithmetic solver is really only
wenzelm@9436
   947
useful to detect inconsistencies among the premises for subgoals which are
wenzelm@9436
   948
*not* themselves (in)equalities, because the latter activate
wenzelm@9436
   949
fast_nat_arith_simproc anyway. However, it seems cheaper to activate the
wenzelm@9436
   950
solver all the time rather than add the additional check. *)
wenzelm@9436
   951
wenzelm@9436
   952
wenzelm@9436
   953
(* arith proof method *)
wenzelm@9436
   954
wenzelm@10516
   955
local
wenzelm@10516
   956
nipkow@13499
   957
fun raw_arith_tac ex i st =
webertj@20217
   958
  (* FIXME: K true should be replaced by a sensible test (perhaps "isSome o
webertj@23200
   959
     decomp sg"? -- but note that the test is applied to terms already before
webertj@23200
   960
     they are split/normalized) to speed things up in case there are lots of
webertj@23200
   961
     irrelevant terms involved; elimination of min/max can be optimized:
webertj@20217
   962
     (max m n + k <= r) = (m+k <= r & n+k <= r)
webertj@20217
   963
     (l <= min m n + k) = (l <= m+k & l <= n+k)
webertj@20217
   964
  *)
nipkow@13499
   965
  refute_tac (K true)
webertj@20217
   966
    (* Splitting is also done inside fast_arith_tac, but not completely --   *)
webertj@20217
   967
    (* split_tac may use split theorems that have not been implemented in    *)
webertj@20268
   968
    (* fast_arith_tac (cf. pre_decomp and split_once_items above), and       *)
webertj@20268
   969
    (* fast_arith_split_limit may trigger.                                   *)
webertj@20217
   970
    (* Therefore splitting outside of fast_arith_tac may allow us to prove   *)
webertj@20217
   971
    (* some goals that fast_arith_tac alone would fail on.                   *)
webertj@20217
   972
    (REPEAT_DETERM o split_tac (#splits (ArithTheoryData.get (Thm.theory_of_thm st))))
webertj@20217
   973
    (fast_ex_arith_tac ex)
webertj@20217
   974
    i st;
wenzelm@9436
   975
webertj@20412
   976
fun arith_theory_tac i st =
webertj@20412
   977
let
webertj@20412
   978
  val tactics = #tactics (ArithTheoryData.get (Thm.theory_of_thm st))
webertj@20412
   979
in
webertj@20412
   980
  FIRST' (map (fn ArithTactic {tactic, ...} => tactic) tactics) i st
webertj@20412
   981
end;
berghofe@13877
   982
wenzelm@10516
   983
in
wenzelm@10516
   984
webertj@20217
   985
  val simple_arith_tac = FIRST' [fast_arith_tac,
webertj@23190
   986
    ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_arith_tac true];
berghofe@13877
   987
webertj@20217
   988
  val arith_tac = FIRST' [fast_arith_tac,
webertj@23190
   989
    ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_arith_tac true,
webertj@20412
   990
    arith_theory_tac];
berghofe@13877
   991
webertj@20217
   992
  val silent_arith_tac = FIRST' [fast_arith_tac,
webertj@23190
   993
    ObjectLogic.full_atomize_tac THEN' (REPEAT_DETERM o rtac impI) THEN' raw_arith_tac false,
webertj@20412
   994
    arith_theory_tac];
wenzelm@10516
   995
webertj@20217
   996
  fun arith_method prems =
webertj@20217
   997
    Method.METHOD (fn facts => HEADGOAL (Method.insert_tac (prems @ facts) THEN' arith_tac));
wenzelm@9436
   998
wenzelm@10516
   999
end;
wenzelm@10516
  1000
nipkow@15195
  1001
(* antisymmetry:
nipkow@15197
  1002
   combines x <= y (or ~(y < x)) and y <= x (or ~(x < y)) into x = y
nipkow@15195
  1003
nipkow@15195
  1004
local
nipkow@15195
  1005
val antisym = mk_meta_eq order_antisym
haftmann@22548
  1006
val not_lessD = @{thm linorder_not_less} RS iffD1
nipkow@15195
  1007
fun prp t thm = (#prop(rep_thm thm) = t)
nipkow@15195
  1008
in
nipkow@15195
  1009
fun antisym_eq prems thm =
nipkow@15195
  1010
  let
nipkow@15195
  1011
    val r = #prop(rep_thm thm);
nipkow@15195
  1012
  in
nipkow@15195
  1013
    case r of
haftmann@23881
  1014
      Tr $ ((c as Const(@{const_name HOL.less_eq},T)) $ s $ t) =>
nipkow@15195
  1015
        let val r' = Tr $ (c $ t $ s)
nipkow@15195
  1016
        in
nipkow@15195
  1017
          case Library.find_first (prp r') prems of
skalberg@15531
  1018
            NONE =>
haftmann@23881
  1019
              let val r' = Tr $ (HOLogic.Not $ (Const(@{const_name HOL.less},T) $ s $ t))
nipkow@15195
  1020
              in case Library.find_first (prp r') prems of
skalberg@15531
  1021
                   NONE => []
skalberg@15531
  1022
                 | SOME thm' => [(thm' RS not_lessD) RS (thm RS antisym)]
nipkow@15195
  1023
              end
skalberg@15531
  1024
          | SOME thm' => [thm' RS (thm RS antisym)]
nipkow@15195
  1025
        end
haftmann@23881
  1026
    | Tr $ (Const("Not",_) $ (Const(@{const_name HOL.less},T) $ s $ t)) =>
haftmann@23881
  1027
        let val r' = Tr $ (Const(@{const_name HOL.less_eq},T) $ s $ t)
nipkow@15195
  1028
        in
nipkow@15195
  1029
          case Library.find_first (prp r') prems of
skalberg@15531
  1030
            NONE =>
haftmann@23881
  1031
              let val r' = Tr $ (HOLogic.Not $ (Const(@{const_name HOL.less},T) $ t $ s))
nipkow@15195
  1032
              in case Library.find_first (prp r') prems of
skalberg@15531
  1033
                   NONE => []
skalberg@15531
  1034
                 | SOME thm' =>
nipkow@15195
  1035
                     [(thm' RS not_lessD) RS ((thm RS not_lessD) RS antisym)]
nipkow@15195
  1036
              end
skalberg@15531
  1037
          | SOME thm' => [thm' RS ((thm RS not_lessD) RS antisym)]
nipkow@15195
  1038
        end
nipkow@15195
  1039
    | _ => []
nipkow@15195
  1040
  end
nipkow@15195
  1041
  handle THM _ => []
nipkow@15195
  1042
end;
nipkow@15197
  1043
*)
wenzelm@9436
  1044
wenzelm@9436
  1045
(* theory setup *)
wenzelm@9436
  1046
wenzelm@9436
  1047
val arith_setup =
wenzelm@18708
  1048
  init_lin_arith_data #>
wenzelm@18708
  1049
  (fn thy => (Simplifier.change_simpset_of thy (fn ss => ss
wenzelm@17875
  1050
    addsimprocs (nat_cancel_sums @ [fast_nat_arith_simproc])
wenzelm@18708
  1051
    addSolver (mk_solver' "lin. arith." Fast_Arith.cut_lin_arith_tac)); thy)) #>
paulson@15221
  1052
  Method.add_methods
haftmann@21879
  1053
    [("arith", (arith_method o fst) oo Method.syntax Args.bang_facts,
wenzelm@18708
  1054
      "decide linear arithmethic")] #>
wenzelm@18728
  1055
  Attrib.add_attributes [("arith_split", Attrib.no_args arith_split_add,
wenzelm@18708
  1056
    "declaration of split rules for arithmetic procedure")];