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