Section read_write.
Variable (f: file) (fptr: nat).
Let contents := get_contents f.
Fixpoint read_file_aux (nbytes sz cur: nat): list memval :=
match nbytes with
| O => nil
| S nbytes' => if eq_nat_dec sz cur then nil
else contents cur :: read_file_aux nbytes' sz (S cur)
end.
Definition read_file (nbytes: nat): list memval := read_file_aux nbytes (get_size f) fptr.
Fixpoint write_file_aux (bytes: list memval) (sz cur: nat): (nat -> memval)*nat :=
match bytes with
| nil => (contents, O)
| byte::bytes' =>
match write_file_aux bytes' sz (S cur) with (contents', nbytes) =>
(fun ofs => if eq_nat_dec ofs cur then byte else contents' ofs, S nbytes)
end
end.

Lemma write_file_aux_length (bytes: list memval) (sz cur: nat):
snd (write_file_aux bytes sz cur)=length bytes.