Skip to content

Commit e2da77f

Browse files
authored
Prove alternative set_byte and word_of_bytes thms (#1680)
If we know the byte being set is zero, then set_byte is equivalent to just a bitwise-or of the byte (cast and shifted into place) and the original contents. In particular, word_of_bytes can be shown to be just a bitwise-or of all the bytes being inserted, assuming we're not in a funny overflow case. This makes at least some downstream proofs about word_of_bytes much easier.
1 parent e0dfa14 commit e2da77f

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/n-bit/byteScript.sml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ Proof
9696
rw[set_byte_def,byte_index_def]
9797
QED
9898

99+
Theorem set_byte_eq_or:
100+
(!j. j < 8n ==> ~ w ' (byte_index ix bige + j)) ==>
101+
set_byte ix b w bige = (w || (w2w b << byte_index ix bige))
102+
Proof
103+
simp [set_byte_bit_field_insert, wordsTheory.bit_field_insert_def,
104+
wordsTheory.word_modify_def, wordsTheory.word_or_def, wordsTheory.word_lsl_def]
105+
\\ simp_tac (std_ss ++ fcpLib.FCP_ss) [wordsTheory.w2w]
106+
\\ rw []
107+
\\ iff_tac \\ CCONTR_TAC \\ gs [wordsTheory.w2w]
108+
\\ first_x_assum (qspec_then `i - byte_index ix bige` assume_tac)
109+
\\ gs []
110+
QED
111+
99112
Triviality lt_or_gt:
100113
(a: num) <> b ==> a < b \/ b < a
101114
Proof
@@ -202,6 +215,64 @@ Proof
202215
>> rfs [w2n_add_2, num_bytes_nonzero])
203216
QED
204217

218+
Triviality word_of_bytes_eq_or_helper:
219+
!bs n w. (!j. j < LENGTH bs ==> ~ (f (EL j bs) (j + n) ' ix)) /\
220+
~ ((w : 'a word) ' ix) /\ ix < dimindex (: 'a) ==>
221+
~ FOLDRi (\x b w. w || (f b (n + x))) w bs ' ix
222+
Proof
223+
Induct
224+
\\ simp []
225+
\\ rw []
226+
\\ ONCE_REWRITE_TAC [wordsTheory.word_or_def]
227+
\\ simp [fcpTheory.FCP_BETA]
228+
\\ conj_tac
229+
>- (
230+
first_x_assum (qspec_then `0n` mp_tac)
231+
\\ simp []
232+
)
233+
\\ simp [combinTheory.o_DEF]
234+
\\ last_x_assum (qspec_then `SUC n` mp_tac)
235+
\\ simp [arithmeticTheory.ADD1]
236+
\\ disch_then irule
237+
\\ rw []
238+
\\ first_x_assum (qspec_then `SUC j` mp_tac)
239+
\\ simp [arithmeticTheory.ADD1]
240+
QED
241+
242+
Triviality word_of_bytes_eq_or_helper2 =
243+
word_of_bytes_eq_or_helper |> Q.SPECL [`bs`, `0n`]
244+
|> SIMP_RULE std_ss []
245+
246+
Triviality w2n_increment:
247+
w2n (x + 1w) = (if x = UINT_MAXw then 0n else w2n x + 1)
248+
Proof
249+
qspec_then `x` mp_tac wordsTheory.w2n_plus1
250+
\\ rw []
251+
QED
252+
253+
Theorem word_of_bytes_eq_or:
254+
!bs ix.
255+
(w2n ix + LENGTH bs) * 8 <= dimindex (: 'a) /\
256+
EVERYi (\i _. ix + n2w i <> -1w) bs ==>
257+
word_of_bytes F ix bs =
258+
FOLDRi (\i b w. (w2w b << ((w2n ix + i) * 8)) || w) (0w : 'a word) bs
259+
Proof
260+
Induct
261+
\\ simp [word_of_bytes_def]
262+
\\ rw []
263+
\\ first_x_assum (qspec_then `ix + 1w` mp_tac)
264+
\\ fs [listTheory.EVERYi_def, w2n_increment]
265+
\\ fs [combinTheory.o_DEF, wordsTheory.n2w_SUC]
266+
\\ rw []
267+
\\ dep_rewrite.DEP_ONCE_REWRITE_TAC [set_byte_eq_or]
268+
\\ simp [byte_index_def]
269+
\\ simp [arithmeticTheory.LESS_MOD, arithmeticTheory.X_LT_DIV]
270+
\\ rw [arithmeticTheory.ADD1]
271+
\\ ho_match_mp_tac word_of_bytes_eq_or_helper2
272+
\\ rw [wordsTheory.word_0]
273+
\\ simp [wordsTheory.word_lsl_def, wordsTheory.w2w, fcpTheory.FCP_BETA]
274+
QED
275+
205276
Definition words_of_bytes_def:
206277
(words_of_bytes be [] = ([]:'a word list)) /\
207278
(words_of_bytes be bytes =

0 commit comments

Comments
 (0)