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.

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.