src/HOL/Tools/int_arith.ML
author boehmes
Mon Jun 08 22:29:37 2009 +0200 (2009-06-08)
changeset 31510 e0f2bb4b0021
parent 31101 26c7bb764a38
child 32603 e08fdd615333
permissions -rw-r--r--
fast_lin_arith uses proper multiplication instead of unfolding to additions
haftmann@31068
     1
(* Author: Tobias Nipkow
huffman@30802
     2
wenzelm@23164
     3
Instantiation of the generic linear arithmetic package for int.
wenzelm@23164
     4
*)
wenzelm@23164
     5
haftmann@31068
     6
signature INT_ARITH =
haftmann@31068
     7
sig
haftmann@31068
     8
  val setup: Context.generic -> Context.generic
haftmann@31100
     9
  val global_setup: theory -> theory
haftmann@31068
    10
end
haftmann@31068
    11
haftmann@31068
    12
structure Int_Arith : INT_ARITH =
haftmann@30496
    13
struct
haftmann@30496
    14
wenzelm@23164
    15
(* Update parameters of arithmetic prover *)
wenzelm@23164
    16
nipkow@24266
    17
(* reduce contradictory =/</<= to False *)
nipkow@24266
    18
nipkow@24266
    19
(* Evaluation of terms of the form "m R n" where R is one of "=", "<=" or "<",
nipkow@24266
    20
   and m and n are ground terms over rings (roughly speaking).
nipkow@24266
    21
   That is, m and n consist only of 1s combined with "+", "-" and "*".
nipkow@24266
    22
*)
haftmann@30496
    23
nipkow@24266
    24
val zeroth = (symmetric o mk_meta_eq) @{thm of_int_0};
haftmann@30496
    25
nipkow@24266
    26
val lhss0 = [@{cpat "0::?'a::ring"}];
haftmann@30496
    27
nipkow@24266
    28
fun proc0 phi ss ct =
nipkow@24266
    29
  let val T = ctyp_of_term ct
nipkow@24266
    30
  in if typ_of T = @{typ int} then NONE else
nipkow@24266
    31
     SOME (instantiate' [SOME T] [] zeroth)
nipkow@24266
    32
  end;
haftmann@30496
    33
nipkow@24266
    34
val zero_to_of_int_zero_simproc =
nipkow@24266
    35
  make_simproc {lhss = lhss0, name = "zero_to_of_int_zero_simproc",
nipkow@24266
    36
  proc = proc0, identifier = []};
nipkow@24266
    37
nipkow@24266
    38
val oneth = (symmetric o mk_meta_eq) @{thm of_int_1};
haftmann@30496
    39
nipkow@24266
    40
val lhss1 = [@{cpat "1::?'a::ring_1"}];
haftmann@30496
    41
nipkow@24266
    42
fun proc1 phi ss ct =
nipkow@24266
    43
  let val T = ctyp_of_term ct
nipkow@24266
    44
  in if typ_of T = @{typ int} then NONE else
nipkow@24266
    45
     SOME (instantiate' [SOME T] [] oneth)
nipkow@24266
    46
  end;
haftmann@30496
    47
nipkow@24266
    48
val one_to_of_int_one_simproc =
nipkow@24266
    49
  make_simproc {lhss = lhss1, name = "one_to_of_int_one_simproc",
nipkow@24266
    50
  proc = proc1, identifier = []};
nipkow@24266
    51
haftmann@31100
    52
fun check (Const (@{const_name "HOL.one"}, @{typ int})) = false
haftmann@31100
    53
  | check (Const (@{const_name "HOL.one"}, _)) = true
haftmann@31100
    54
  | check (Const (s, _)) = member (op =) [@{const_name "op ="},
haftmann@31100
    55
      @{const_name "HOL.times"}, @{const_name "HOL.uminus"},
haftmann@31100
    56
      @{const_name "HOL.minus"}, @{const_name "HOL.plus"},
haftmann@31100
    57
      @{const_name "HOL.zero"},
haftmann@31100
    58
      @{const_name "HOL.less"}, @{const_name "HOL.less_eq"}] s
haftmann@31100
    59
  | check (a $ b) = check a andalso check b
haftmann@31100
    60
  | check _ = false;
nipkow@24266
    61
nipkow@24266
    62
val conv =
nipkow@24266
    63
  Simplifier.rewrite
nipkow@24266
    64
   (HOL_basic_ss addsimps
nipkow@24266
    65
     ((map (fn th => th RS sym) [@{thm of_int_add}, @{thm of_int_mult},
nipkow@24266
    66
             @{thm of_int_diff},  @{thm of_int_minus}])@
nipkow@24266
    67
      [@{thm of_int_less_iff}, @{thm of_int_le_iff}, @{thm of_int_eq_iff}])
nipkow@24266
    68
     addsimprocs [zero_to_of_int_zero_simproc,one_to_of_int_one_simproc]);
nipkow@24266
    69
nipkow@24266
    70
fun sproc phi ss ct = if check (term_of ct) then SOME (conv ct) else NONE
haftmann@30496
    71
nipkow@24266
    72
val lhss' =
nipkow@24266
    73
  [@{cpat "(?x::?'a::ring_char_0) = (?y::?'a)"},
nipkow@24266
    74
   @{cpat "(?x::?'a::ordered_idom) < (?y::?'a)"},
nipkow@24266
    75
   @{cpat "(?x::?'a::ordered_idom) <= (?y::?'a)"}]
haftmann@30496
    76
nipkow@24266
    77
val zero_one_idom_simproc =
nipkow@24266
    78
  make_simproc {lhss = lhss' , name = "zero_one_idom_simproc",
nipkow@24266
    79
  proc = sproc, identifier = []}
nipkow@24266
    80
wenzelm@23164
    81
val fast_int_arith_simproc =
haftmann@31100
    82
  Simplifier.simproc @{theory} "fast_int_arith"
wenzelm@23164
    83
     ["(m::'a::{ordered_idom,number_ring}) < n",
wenzelm@23164
    84
      "(m::'a::{ordered_idom,number_ring}) <= n",
haftmann@31101
    85
      "(m::'a::{ordered_idom,number_ring}) = n"] (K Lin_Arith.simproc);
wenzelm@23164
    86
haftmann@31100
    87
val global_setup = Simplifier.map_simpset
haftmann@31100
    88
  (fn simpset => simpset addsimprocs [fast_int_arith_simproc]);
haftmann@30496
    89
boehmes@31510
    90
boehmes@31510
    91
fun number_of thy T n =
boehmes@31510
    92
  if not (Sign.of_sort thy (T, @{sort number}))
boehmes@31510
    93
  then raise CTERM ("number_of", [])
boehmes@31510
    94
  else Numeral.mk_cnumber (Thm.ctyp_of thy T) n
boehmes@31510
    95
haftmann@31100
    96
val setup =
haftmann@31100
    97
  Lin_Arith.add_inj_thms [@{thm zle_int} RS iffD2, @{thm int_int_eq} RS iffD2]
haftmann@31100
    98
  #> Lin_Arith.add_lessD @{thm zless_imp_add1_zle}
haftmann@31100
    99
  #> Lin_Arith.add_simps (@{thms simp_thms} @ @{thms arith_simps} @ @{thms rel_simps}
haftmann@31100
   100
      @ @{thms arith_special} @ @{thms int_arith_rules})
haftmann@31100
   101
  #> Lin_Arith.add_simprocs (Numeral_Simprocs.assoc_fold_simproc :: zero_one_idom_simproc
haftmann@31100
   102
      :: Numeral_Simprocs.combine_numerals
haftmann@31100
   103
      :: Numeral_Simprocs.cancel_numerals)
boehmes@31510
   104
  #> Lin_Arith.set_number_of number_of
haftmann@31100
   105
  #> Lin_Arith.add_inj_const (@{const_name of_nat}, HOLogic.natT --> HOLogic.intT)
haftmann@31100
   106
  #> Lin_Arith.add_discrete_type @{type_name Int.int}
haftmann@31100
   107
haftmann@31100
   108
end;