src/HOL/Tools/int_arith.ML
author haftmann
Fri Feb 05 14:33:50 2010 +0100 (2010-02-05)
changeset 35028 108662d50512
parent 34974 18b41bba42b5
child 35092 cfe605c54e50
permissions -rw-r--r--
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
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@34974
    52
fun check (Const (@{const_name Algebras.one}, @{typ int})) = false
haftmann@34974
    53
  | check (Const (@{const_name Algebras.one}, _)) = true
haftmann@31100
    54
  | check (Const (s, _)) = member (op =) [@{const_name "op ="},
haftmann@34974
    55
      @{const_name Algebras.times}, @{const_name Algebras.uminus},
haftmann@34974
    56
      @{const_name Algebras.minus}, @{const_name Algebras.plus},
haftmann@34974
    57
      @{const_name Algebras.zero},
haftmann@34974
    58
      @{const_name Algebras.less}, @{const_name Algebras.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)"},
haftmann@35028
    74
   @{cpat "(?x::?'a::linordered_idom) < (?y::?'a)"},
haftmann@35028
    75
   @{cpat "(?x::?'a::linordered_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"
haftmann@35028
    83
     ["(m::'a::{linordered_idom,number_ring}) < n",
haftmann@35028
    84
      "(m::'a::{linordered_idom,number_ring}) <= n",
haftmann@35028
    85
      "(m::'a::{linordered_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@33296
   101
  #> Lin_Arith.add_simprocs [zero_one_idom_simproc]
boehmes@31510
   102
  #> Lin_Arith.set_number_of number_of
haftmann@31100
   103
  #> Lin_Arith.add_inj_const (@{const_name of_nat}, HOLogic.natT --> HOLogic.intT)
haftmann@31100
   104
  #> Lin_Arith.add_discrete_type @{type_name Int.int}
haftmann@31100
   105
haftmann@31100
   106
end;