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