src/HOL/Word/BitSyntax.thy
author kleing
Mon, 20 Aug 2007 04:34:31 +0200
changeset 24333 e77ea0ea7f2c
child 24334 22863f364531
permissions -rw-r--r--
* HOL-Word: New extensive library and type for generic, fixed size machine words, with arithemtic, bit-wise, shifting and rotating operations, reflection into int, nat, and bool lists, automation for linear arithmetic (by automatic reflection into nat or int), including lemmas on overflow and monotonicity. Instantiated to all appropriate arithmetic type classes, supporting automatic simplification of numerals on all operations. Jointly developed by NICTA, Galois, and PSU. * still to do: README.html/document + moving some of the generic lemmas to appropriate place in distribution
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     1
header {* Syntactic class for bitwise operations *}
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     2
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     3
theory BitSyntax
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     4
imports Main
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     5
begin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     6
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     7
axclass bit < type
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     8
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     9
text {*
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    10
  We want the bitwise operations to bind slightly weaker
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    11
  than @{text "+"} and @{text "-"}, but @{text "~~"} to 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    12
  bind slightly stronger than @{text "*"}.
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    13
*}
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    14
consts
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    15
  bitNOT  :: "'a::bit \<Rightarrow> 'a"       ("NOT _" [70] 71)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    16
  bitAND  :: "'a::bit \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "AND" 64)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    17
  bitOR   :: "'a::bit \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "OR"  59)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    18
  bitXOR  :: "'a::bit \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "XOR" 59)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    19
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    20
text {*
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    21
  Testing and shifting operations.
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    22
*}
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    23
consts
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    24
  test_bit :: "'a::bit \<Rightarrow> nat \<Rightarrow> bool" (infixl "!!" 100)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    25
  lsb      :: "'a::bit \<Rightarrow> bool"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    26
  msb      :: "'a::bit \<Rightarrow> bool"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    27
  set_bit  :: "'a::bit \<Rightarrow> nat \<Rightarrow> bool \<Rightarrow> 'a"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    28
  set_bits :: "(nat \<Rightarrow> bool) \<Rightarrow> 'a::bit" (binder "BITS " 10)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    29
  shiftl   :: "'a::bit \<Rightarrow> nat \<Rightarrow> 'a" (infixl "<<" 55)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    30
  shiftr   :: "'a::bit \<Rightarrow> nat \<Rightarrow> 'a" (infixl ">>" 55)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    31
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    32
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    33
subsection {* Bitwise operations on type @{typ bit} *}
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    34
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    35
instance bit :: bit ..
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    36
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    37
defs (overloaded)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    38
  NOT_bit_def: "NOT x \<equiv> case x of bit.B0 \<Rightarrow> bit.B1 | bit.B1 \<Rightarrow> bit.B0"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    39
  AND_bit_def: "x AND y \<equiv> case x of bit.B0 \<Rightarrow> bit.B0 | bit.B1 \<Rightarrow> y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    40
  OR_bit_def: "x OR y :: bit \<equiv> NOT (NOT x AND NOT y)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    41
  XOR_bit_def: "x XOR y :: bit \<equiv> (x AND NOT y) OR (NOT x AND y)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    42
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    43
lemma bit_simps [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    44
  "NOT bit.B0 = bit.B1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    45
  "NOT bit.B1 = bit.B0"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    46
  "bit.B0 AND y = bit.B0"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    47
  "bit.B1 AND y = y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    48
  "bit.B0 OR y = y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    49
  "bit.B1 OR y = bit.B1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    50
  "bit.B0 XOR y = y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    51
  "bit.B1 XOR y = NOT y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    52
by (simp_all add: NOT_bit_def AND_bit_def OR_bit_def
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    53
                  XOR_bit_def split: bit.split)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    54
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    55
lemma bit_NOT_NOT: "NOT (NOT (b::bit)) = b"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    56
by (induct b) simp_all
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    57
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    58
end