Fixpoint fold {S A}
           (f : byte -> S -> fold_state S A) (s : S) (l : list byte) :=
    match l with
    | [] => None
    | b :: l => match f b s with
                | Done a => Some (a, l)
                | More s => fold f s l
                | Error => None
                end
    end.

(* Specifications: *)
Lemma ret_unwrap : forall {A} (x: A) bytes, unwrap (ret x) bytes = Some (x, bytes).
Lemma map_unwrap: forall A B (f: A -> B) (d: t A) bytes, unwrap (map f d) bytes = match (unwrap d bytes) with | None => None | Some (v, bytes) => Some (f v, bytes) end.
Lemma fold_unwrap : forall {S A : Type} (f : byte -> S -> fold_state S A) (s : S) l, unwrap (fold f s) l = match l with | [] => None | b :: l => match f b s with | Done a => Some (a, l) | More s => unwrap (fold f s) l | Error => None end end.