huffman@24441

1 
(*

huffman@24441

2 
Author: Gerwin Klein, NICTA

huffman@24441

3 

huffman@24441

4 
Examples demonstrating and testing various word operations.

huffman@24441

5 
*)

huffman@24441

6 

kleing@25262

7 
header "Examples of word operations"

kleing@25262

8 

haftmann@29629

9 
theory WordExamples

haftmann@29629

10 
imports Word

huffman@24441

11 
begin

huffman@24441

12 

wenzelm@42463

13 
type_synonym word32 = "32 word"

wenzelm@42463

14 
type_synonym word8 = "8 word"

wenzelm@42463

15 
type_synonym byte = word8

wenzelm@29640

16 

huffman@24441

17 
 "modulus"

huffman@24441

18 

huffman@24441

19 
lemma "(27 :: 4 word) = 5" by simp

huffman@24441

20 

huffman@24441

21 
lemma "(27 :: 4 word) = 11" by simp

huffman@24441

22 

huffman@24441

23 
lemma "27 \<noteq> (11 :: 6 word)" by simp

huffman@24441

24 

huffman@24441

25 
 "signed"

huffman@24441

26 
lemma "(127 :: 6 word) = 1" by simp

huffman@24441

27 

huffman@24441

28 
 "number ring simps"

huffman@24441

29 
lemma

huffman@24465

30 
"27 + 11 = (38::'a::len word)"

huffman@24441

31 
"27 + 11 = (6::5 word)"

huffman@24465

32 
"7 * 3 = (21::'a::len word)"

huffman@24465

33 
"11  27 = (16::'a::len word)"

huffman@24465

34 
" 11 = (11::'a::len word)"

huffman@24465

35 
"40 + 1 = (39::'a::len word)"

huffman@24441

36 
by simp_all

huffman@24441

37 

huffman@24441

38 
lemma "word_pred 2 = 1" by simp

huffman@24441

39 

huffman@24441

40 
lemma "word_succ 3 = 2" by simp

huffman@24441

41 

huffman@24441

42 
lemma "23 < (27::8 word)" by simp

huffman@24441

43 
lemma "23 \<le> (27::8 word)" by simp

huffman@24441

44 
lemma "\<not> 23 < (27::2 word)" by simp

huffman@24441

45 
lemma "0 < (4::3 word)" by simp

huffman@24441

46 

huffman@24441

47 
 "ring operations"

huffman@24441

48 

huffman@24441

49 
lemma "a + 2 * b + c  b = (b + c) + (a :: 32 word)" by simp

huffman@24441

50 

huffman@24441

51 
 "casting"

huffman@24441

52 

huffman@24441

53 
lemma "uint (234567 :: 10 word) = 71" by simp

huffman@24441

54 
lemma "uint (234567 :: 10 word) = 953" by simp

huffman@24441

55 
lemma "sint (234567 :: 10 word) = 71" by simp

huffman@24441

56 
lemma "sint (234567 :: 10 word) = 71" by simp

huffman@24441

57 

huffman@24441

58 
lemma "unat (234567 :: 10 word) = 953" by simp

huffman@24441

59 

huffman@24441

60 
lemma "ucast (0b1010 :: 4 word) = (0b10 :: 2 word)" by simp

huffman@24441

61 
lemma "ucast (0b1010 :: 4 word) = (0b1010 :: 10 word)" by simp

huffman@24441

62 
lemma "scast (0b1010 :: 4 word) = (0b111010 :: 6 word)" by simp

huffman@24441

63 

huffman@24441

64 
 "reducing goals to nat or int and arith:"

huffman@24465

65 
lemma "i < x ==> i < (i + 1 :: 'a :: len word)" by unat_arith

huffman@24465

66 
lemma "i < x ==> i < (i + 1 :: 'a :: len word)" by uint_arith

huffman@24441

67 

huffman@24441

68 
 "bool lists"

huffman@24441

69 

huffman@24465

70 
lemma "of_bl [True, False, True, True] = (0b1011::'a::len word)" by simp

huffman@24441

71 

huffman@24441

72 
lemma "to_bl (0b110::4 word) = [False, True, True, False]" by simp

huffman@24441

73 

huffman@24441

74 
 "this is not exactly fast, but bearable"

huffman@24441

75 
lemma "of_bl (replicate 32 True) = (0xFFFFFFFF::32 word)" by simp

huffman@24441

76 

huffman@24441

77 
 "this works only for replicate n True"

huffman@24441

78 
lemma "of_bl (replicate 32 True) = (0xFFFFFFFF::32 word)"

huffman@24441

79 
by (unfold mask_bl [symmetric]) (simp add: mask_def)

huffman@24441

80 

huffman@24441

81 

huffman@24441

82 
 "bit operations"

huffman@24441

83 

huffman@24441

84 
lemma "0b110 AND 0b101 = (0b100 :: 32 word)" by simp

huffman@24441

85 

huffman@24441

86 
lemma "0b110 OR 0b011 = (0b111 :: 8 word)" by simp

huffman@24441

87 

huffman@24441

88 
lemma "0xF0 XOR 0xFF = (0x0F :: byte)" by simp

huffman@24441

89 

huffman@24441

90 
lemma "NOT (0xF0 :: 16 word) = 0xFF0F" by simp

huffman@24441

91 

huffman@24441

92 
lemma "(1 :: 32 word) = 0xFFFFFFFF" by simp

huffman@24441

93 

huffman@24441

94 
lemma "(0b0010 :: 4 word) !! 1" by simp

huffman@24441

95 
lemma "\<not> (0b0010 :: 4 word) !! 0" by simp

huffman@24441

96 
lemma "\<not> (0b1000 :: 3 word) !! 4" by simp

huffman@24441

97 

huffman@24441

98 
lemma "(0b11000 :: 10 word) !! n = (n = 4 \<or> n = 3)"

huffman@26086

99 
by (auto simp add: bin_nth_Bit0 bin_nth_Bit1)

huffman@24441

100 

huffman@24465

101 
lemma "set_bit 55 7 True = (183::'a::len0 word)" by simp

huffman@24465

102 
lemma "set_bit 0b0010 7 True = (0b10000010::'a::len0 word)" by simp

huffman@24465

103 
lemma "set_bit 0b0010 1 False = (0::'a::len0 word)" by simp

huffman@24441

104 

huffman@24465

105 
lemma "lsb (0b0101::'a::len word)" by simp

huffman@24465

106 
lemma "\<not> lsb (0b1000::'a::len word)" by simp

huffman@24441

107 

huffman@24441

108 
lemma "\<not> msb (0b0101::4 word)" by simp

huffman@24441

109 
lemma "msb (0b1000::4 word)" by simp

huffman@24441

110 

huffman@24465

111 
lemma "word_cat (27::4 word) (27::8 word) = (2843::'a::len word)" by simp

huffman@24441

112 
lemma "word_cat (0b0011::4 word) (0b1111::6word) = (0b0011001111 :: 10 word)"

huffman@24441

113 
by simp

huffman@24441

114 

huffman@24465

115 
lemma "0b1011 << 2 = (0b101100::'a::len0 word)" by simp

huffman@24441

116 
lemma "0b1011 >> 2 = (0b10::8 word)" by simp

huffman@24441

117 
lemma "0b1011 >>> 2 = (0b10::8 word)" by simp

huffman@24441

118 

huffman@24441

119 
lemma "slice 3 (0b101111::6 word) = (0b101::3 word)" by simp

huffman@24441

120 

huffman@24441

121 
lemma "word_rotr 2 0b0110 = (0b1001::4 word)" by simp

huffman@24441

122 
lemma "word_rotl 1 0b1110 = (0b1101::4 word)" by simp

huffman@24441

123 
lemma "word_roti 2 0b1110 = (0b1011::4 word)" by simp

huffman@24441

124 
lemma "word_roti 2 0b0110 = (0b1001::4 word)" by simp

huffman@24441

125 

huffman@24441

126 
lemma "(x AND 0xff00) OR (x AND 0x00ff) = (x::16 word)"

huffman@24441

127 
proof 

huffman@24441

128 
have "(x AND 0xff00) OR (x AND 0x00ff) = x AND (0xff00 OR 0x00ff)"

huffman@24441

129 
by (simp only: word_ao_dist2)

huffman@24441

130 
also have "0xff00 OR 0x00ff = (1::16 word)"

huffman@24441

131 
by simp

huffman@24441

132 
also have "x AND 1 = x"

huffman@24441

133 
by simp

huffman@24441

134 
finally show ?thesis .

huffman@24441

135 
qed

huffman@24441

136 

huffman@24441

137 
end
