src/HOL/Library/Tools/word_lib.ML
author desharna
Mon, 13 Jun 2022 20:02:00 +0200
changeset 75560 aeb797356de0
parent 72515 c7038c397ae3
child 76183 8089593a364a
permissions -rw-r--r--
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
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
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59806
diff changeset
    20
    Type (\<^type_name>\<open>Numeral_Type.num0\<close>, _) => 0
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59806
diff changeset
    21
  | Type (\<^type_name>\<open>Numeral_Type.num1\<close>, _) => 1
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59806
diff changeset
    22
  | Type (\<^type_name>\<open>Numeral_Type.bit0\<close>, [T]) => 2 * dest_binT T
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59806
diff changeset
    23
  | Type (\<^type_name>\<open>Numeral_Type.bit1\<close>, [T]) => 1 + 2 * dest_binT T
47567
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
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59806
diff changeset
    26
fun is_wordT (Type (\<^type_name>\<open>word\<close>, _)) = true
47567
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
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59806
diff changeset
    29
fun dest_wordT (Type (\<^type_name>\<open>word\<close>, [T])) = dest_binT T
47567
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
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59806
diff changeset
    35
  then Type (\<^type_name>\<open>Numeral_Type.bit0\<close>, [T])
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59806
diff changeset
    36
  else Type (\<^type_name>\<open>Numeral_Type.bit1\<close>, [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 = 
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59806
diff changeset
    39
  if size = 0 then \<^typ>\<open>Numeral_Type.num0\<close>
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59806
diff changeset
    40
  else if size = 1 then \<^typ>\<open>Numeral_Type.num1\<close>
59806
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 =
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59806
diff changeset
    44
  if size >= 0 then Type (\<^type_name>\<open>word\<close>, [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