src/HOL/Word/Bit_Operations.thy
author wenzelm
Wed, 29 Dec 2010 17:34:41 +0100
changeset 41413 64cd30d6b0b8
parent 37655 f4d616d41a59
child 53062 3af1a6020014
permissions -rw-r--r--
explicit file specifications -- avoid secondary load path;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37655
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
     1
(*  Title:      HOL/Word/Bit_Operations.thy
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
     2
    Author:     Author: Brian Huffman, PSU and Gerwin Klein, NICTA
24334
22863f364531 added header
kleing
parents: 24333
diff changeset
     3
*)
22863f364531 added header
kleing
parents: 24333
diff changeset
     4
26559
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
     5
header {* Syntactic classes for bitwise operations *}
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     6
37655
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
     7
theory Bit_Operations
41413
64cd30d6b0b8 explicit file specifications -- avoid secondary load path;
wenzelm
parents: 37655
diff changeset
     8
imports "~~/src/HOL/Library/Bit"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     9
begin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    10
29608
564ea783ace8 no base sort in class import
haftmann
parents: 26559
diff changeset
    11
class bit =
26559
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    12
  fixes bitNOT :: "'a \<Rightarrow> 'a"       ("NOT _" [70] 71)
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    13
    and bitAND :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "AND" 64)
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    14
    and bitOR  :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "OR"  59)
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    15
    and bitXOR :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "XOR" 59)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    16
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    17
text {*
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    18
  We want the bitwise operations to bind slightly weaker
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    19
  than @{text "+"} and @{text "-"}, but @{text "~~"} to 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    20
  bind slightly stronger than @{text "*"}.
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    21
*}
24352
eda777a2785b use class instead of axclass
huffman
parents: 24334
diff changeset
    22
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    23
text {*
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    24
  Testing and shifting operations.
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    25
*}
26559
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    26
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    27
class bits = bit +
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    28
  fixes test_bit :: "'a \<Rightarrow> nat \<Rightarrow> bool" (infixl "!!" 100)
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    29
    and lsb      :: "'a \<Rightarrow> bool"
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    30
    and set_bit  :: "'a \<Rightarrow> nat \<Rightarrow> bool \<Rightarrow> 'a"
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    31
    and set_bits :: "(nat \<Rightarrow> bool) \<Rightarrow> 'a" (binder "BITS " 10)
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    32
    and shiftl   :: "'a \<Rightarrow> nat \<Rightarrow> 'a" (infixl "<<" 55)
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    33
    and shiftr   :: "'a \<Rightarrow> nat \<Rightarrow> 'a" (infixl ">>" 55)
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    34
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    35
class bitss = bits +
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    36
  fixes msb      :: "'a \<Rightarrow> bool"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    37
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    38
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25762
diff changeset
    39
subsection {* Bitwise operations on @{typ bit} *}
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    40
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    41
instantiation bit :: bit
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    42
begin
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    43
26559
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    44
primrec bitNOT_bit where
37655
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    45
  "NOT 0 = (1::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    46
  | "NOT 1 = (0::bit)"
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    47
26559
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    48
primrec bitAND_bit where
37655
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    49
  "0 AND y = (0::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    50
  | "1 AND y = (y::bit)"
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    51
26559
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    52
primrec bitOR_bit where
37655
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    53
  "0 OR y = (y::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    54
  | "1 OR y = (1::bit)"
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    55
26559
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    56
primrec bitXOR_bit where
37655
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    57
  "0 XOR y = (y::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    58
  | "1 XOR y = (NOT y :: bit)"
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    59
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    60
instance  ..
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    61
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    62
end
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    63
26559
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    64
lemmas bit_simps =
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    65
  bitNOT_bit.simps bitAND_bit.simps bitOR_bit.simps bitXOR_bit.simps
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    66
24366
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    67
lemma bit_extra_simps [simp]: 
37655
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    68
  "x AND 0 = (0::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    69
  "x AND 1 = (x::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    70
  "x OR 1 = (1::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    71
  "x OR 0 = (x::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    72
  "x XOR 1 = NOT (x::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    73
  "x XOR 0 = (x::bit)"
24366
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    74
  by (cases x, auto)+
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    75
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    76
lemma bit_ops_comm: 
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    77
  "(x::bit) AND y = y AND x"
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    78
  "(x::bit) OR y = y OR x"
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    79
  "(x::bit) XOR y = y XOR x"
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    80
  by (cases y, auto)+
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    81
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    82
lemma bit_ops_same [simp]: 
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    83
  "(x::bit) AND x = x"
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    84
  "(x::bit) OR x = x"
37655
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    85
  "(x::bit) XOR x = 0"
24366
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    86
  by (cases x, auto)+
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    87
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    88
lemma bit_not_not [simp]: "NOT (NOT (x::bit)) = x"
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    89
  by (cases x) auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    90
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    91
end