src/HOL/Word/Tools/word_lib.ML
author wenzelm
Wed, 25 Mar 2015 10:41:53 +0100
changeset 59806 d3d4ec6c21ef
parent 47567 407cabf66f21
child 69597 ff784d5a5bfb
permissions -rw-r--r--
proper signature; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47567
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
     1
(*  Title:      HOL/Word/Tools/word_lib.ML
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
     2
    Author:     Sascha Boehme, TU Muenchen and Thomas Sewell, NICTA
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
     3
59806
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
     4
Helper routines for operating on the word type.
47567
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
     5
*)
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
     6
59806
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
     7
signature WORD_LIB =
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
     8
sig
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
     9
  val dest_binT: typ -> int
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    10
  val is_wordT: typ -> bool
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    11
  val dest_wordT: typ -> int
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    12
  val mk_wordT: int -> typ
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    13
end
47567
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    14
59806
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    15
structure Word_Lib: WORD_LIB =
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    16
struct
47567
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    17
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    18
fun dest_binT T =
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    19
  (case T of
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    20
    Type (@{type_name "Numeral_Type.num0"}, _) => 0
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    21
  | Type (@{type_name "Numeral_Type.num1"}, _) => 1
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    22
  | Type (@{type_name "Numeral_Type.bit0"}, [T]) => 2 * dest_binT T
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    23
  | Type (@{type_name "Numeral_Type.bit1"}, [T]) => 1 + 2 * dest_binT T
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    24
  | _ => raise TYPE ("dest_binT", [T], []))
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    25
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    26
fun is_wordT (Type (@{type_name word}, _)) = true
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    27
  | is_wordT _ = false
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    28
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    29
fun dest_wordT (Type (@{type_name word}, [T])) = dest_binT T
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    30
  | dest_wordT T = raise TYPE ("dest_wordT", [T], [])
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    31
59806
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    32
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    33
fun mk_bitT i T =
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    34
  if i = 0
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    35
  then Type (@{type_name "Numeral_Type.bit0"}, [T])
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    36
  else Type (@{type_name "Numeral_Type.bit1"}, [T])
47567
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    37
59806
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    38
fun mk_binT size = 
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    39
  if size = 0 then @{typ "Numeral_Type.num0"}
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    40
  else if size = 1 then @{typ "Numeral_Type.num1"}
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    41
  else let val (q, r) = Integer.div_mod size 2 in mk_bitT r (mk_binT q) end
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    42
47567
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    43
fun mk_wordT size =
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    44
  if size >= 0 then Type (@{type_name "word"}, [mk_binT size])
59806
d3d4ec6c21ef proper signature;
wenzelm
parents: 47567
diff changeset
    45
  else raise TYPE ("mk_wordT: " ^ string_of_int size, [], [])
47567
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    46
407cabf66f21 New tactic "word_bitwise" expands word equalities/inequalities into logic.
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
diff changeset
    47
end