huffman@24441

1 
(*

huffman@24441

2 
ID: $Id$

huffman@24441

3 
Author: Gerwin Klein, NICTA

huffman@24441

4 

huffman@24441

5 
Examples demonstrating and testing various word operations.

huffman@24441

6 
*)

huffman@24441

7 

kleing@25262

8 
header "Examples of word operations"

kleing@25262

9 

huffman@24441

10 
theory WordExamples imports WordMain

huffman@24441

11 
begin

huffman@24441

12 

huffman@24441

13 
 "modulus"

huffman@24441

14 

huffman@24441

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

huffman@24441

16 

huffman@24441

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

huffman@24441

18 

huffman@24441

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

huffman@24441

20 

huffman@24441

21 
 "signed"

huffman@24441

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

huffman@24441

23 

huffman@24441

24 
 "number ring simps"

huffman@24441

25 
lemma

huffman@24465

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

huffman@24441

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

huffman@24465

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

huffman@24465

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

huffman@24465

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

huffman@24465

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

huffman@24441

32 
by simp_all

huffman@24441

33 

huffman@24441

34 
lemma "word_pred 2 = 1" by simp

huffman@24441

35 

huffman@24441

36 
lemma "word_succ 3 = 2" by simp

huffman@24441

37 

huffman@24441

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

huffman@24441

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

huffman@24441

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

huffman@24441

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

huffman@24441

42 

huffman@24441

43 
 "ring operations"

huffman@24441

44 

huffman@24441

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

huffman@24441

46 

huffman@24441

47 
 "casting"

huffman@24441

48 

huffman@24441

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

huffman@24441

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

huffman@24441

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

huffman@24441

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

huffman@24441

53 

huffman@24441

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

huffman@24441

55 

huffman@24441

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

huffman@24441

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

huffman@24441

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

huffman@24441

59 

huffman@24441

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

huffman@24465

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

huffman@24465

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

huffman@24441

63 

huffman@24441

64 
 "bool lists"

huffman@24441

65 

huffman@24465

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

huffman@24441

67 

huffman@24441

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

huffman@24441

69 

huffman@24441

70 
 "this is not exactly fast, but bearable"

huffman@24441

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

huffman@24441

72 

huffman@24441

73 
 "this works only for replicate n True"

huffman@24441

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

huffman@24441

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

huffman@24441

76 

huffman@24441

77 

huffman@24441

78 
 "bit operations"

huffman@24441

79 

huffman@24441

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

huffman@24441

81 

huffman@24441

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

huffman@24441

83 

huffman@24441

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

huffman@24441

85 

huffman@24441

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

huffman@24441

87 

huffman@24441

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

huffman@24441

89 

huffman@24441

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

huffman@24441

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

huffman@24441

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

huffman@24441

93 

huffman@24441

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

huffman@26086

95 
by (auto simp add: bin_nth_Bit0 bin_nth_Bit1)

huffman@24441

96 

huffman@24465

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

huffman@24465

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

huffman@24465

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

huffman@24441

100 

huffman@24465

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

huffman@24465

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

huffman@24441

103 

huffman@24441

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

huffman@24441

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

huffman@24441

106 

huffman@24465

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

huffman@24441

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

huffman@24441

109 
by simp

huffman@24441

110 

huffman@24465

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

huffman@24441

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

huffman@24441

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

huffman@24441

114 

huffman@24441

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

huffman@24441

116 

huffman@24441

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

huffman@24441

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

huffman@24441

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

huffman@24441

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

huffman@24441

121 

huffman@24441

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

huffman@24441

123 
proof 

huffman@24441

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

huffman@24441

125 
by (simp only: word_ao_dist2)

huffman@24441

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

huffman@24441

127 
by simp

huffman@24441

128 
also have "x AND 1 = x"

huffman@24441

129 
by simp

huffman@24441

130 
finally show ?thesis .

huffman@24441

131 
qed

huffman@24441

132 

huffman@24441

133 
end
