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