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