src/HOL/Word/Bit_Operations.thy
author haftmann
Sun, 18 Aug 2013 15:29:50 +0200
changeset 53062 3af1a6020014
parent 41413 64cd30d6b0b8
child 53438 6301ed01e34d
permissions -rw-r--r--
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
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
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 41413
diff changeset
    11
subsection {* Abstract syntactic bit operations *}
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 41413
diff changeset
    12
29608
564ea783ace8 no base sort in class import
haftmann
parents: 26559
diff changeset
    13
class bit =
26559
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    14
  fixes bitNOT :: "'a \<Rightarrow> 'a"       ("NOT _" [70] 71)
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    15
    and bitAND :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "AND" 64)
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    16
    and bitOR  :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "OR"  59)
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    17
    and bitXOR :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "XOR" 59)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    18
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    19
text {*
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    20
  We want the bitwise operations to bind slightly weaker
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    21
  than @{text "+"} and @{text "-"}, but @{text "~~"} to 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    22
  bind slightly stronger than @{text "*"}.
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    23
*}
24352
eda777a2785b use class instead of axclass
huffman
parents: 24334
diff changeset
    24
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    25
text {*
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    26
  Testing and shifting operations.
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    27
*}
26559
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    28
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    29
class bits = bit +
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    30
  fixes test_bit :: "'a \<Rightarrow> nat \<Rightarrow> bool" (infixl "!!" 100)
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    31
    and lsb      :: "'a \<Rightarrow> bool"
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    32
    and set_bit  :: "'a \<Rightarrow> nat \<Rightarrow> bool \<Rightarrow> 'a"
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    33
    and set_bits :: "(nat \<Rightarrow> bool) \<Rightarrow> 'a" (binder "BITS " 10)
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    34
    and shiftl   :: "'a \<Rightarrow> nat \<Rightarrow> 'a" (infixl "<<" 55)
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    35
    and shiftr   :: "'a \<Rightarrow> nat \<Rightarrow> 'a" (infixl ">>" 55)
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    36
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    37
class bitss = bits +
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    38
  fixes msb      :: "'a \<Rightarrow> bool"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    39
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 41413
diff changeset
    40
  
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25762
diff changeset
    41
subsection {* Bitwise operations on @{typ bit} *}
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    42
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    43
instantiation bit :: bit
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    44
begin
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    45
26559
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    46
primrec bitNOT_bit where
37655
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    47
  "NOT 0 = (1::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    48
  | "NOT 1 = (0::bit)"
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    49
26559
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    50
primrec bitAND_bit where
37655
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    51
  "0 AND y = (0::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    52
  | "1 AND y = (y::bit)"
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    53
26559
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    54
primrec bitOR_bit where
37655
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    55
  "0 OR y = (y::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    56
  | "1 OR y = (1::bit)"
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25382
diff changeset
    57
26559
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    58
primrec bitXOR_bit where
37655
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    59
  "0 XOR y = (y::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    60
  | "1 XOR y = (NOT y :: bit)"
25762
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
instance  ..
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
end
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    65
26559
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    66
lemmas bit_simps =
799983936aad syntactic classes for bit operations
haftmann
parents: 26086
diff changeset
    67
  bitNOT_bit.simps bitAND_bit.simps bitOR_bit.simps bitXOR_bit.simps
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    68
24366
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    69
lemma bit_extra_simps [simp]: 
37655
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    70
  "x AND 0 = (0::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    71
  "x AND 1 = (x::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    72
  "x OR 1 = (1::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    73
  "x OR 0 = (x::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    74
  "x XOR 1 = NOT (x::bit)"
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    75
  "x XOR 0 = (x::bit)"
24366
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    76
  by (cases x, auto)+
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    77
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    78
lemma bit_ops_comm: 
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    79
  "(x::bit) AND y = y AND x"
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    80
  "(x::bit) OR y = y OR x"
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    81
  "(x::bit) XOR y = y XOR x"
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    82
  by (cases y, auto)+
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    83
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    84
lemma bit_ops_same [simp]: 
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    85
  "(x::bit) AND x = x"
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    86
  "(x::bit) OR x = x"
37655
f4d616d41a59 more speaking theory names
haftmann
parents: 29631
diff changeset
    87
  "(x::bit) XOR x = 0"
24366
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    88
  by (cases x, auto)+
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    89
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    90
lemma bit_not_not [simp]: "NOT (NOT (x::bit)) = x"
08b116049547 move bit simps from BinOperations to BitSyntax
huffman
parents: 24352
diff changeset
    91
  by (cases x) auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    92
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    93
end