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