Section Encode_option.
Inductive sigOption (sigX: Type) : Type :=
| sigOption_X (s : sigX)
| sigOption_None
| sigOption_Some
.
Arguments sigOption_Some {sigX}. Arguments sigOption_None {sigX}. Arguments sigOption_X {sigX}.
Global Instance Retract_sigOption_X (sig tau : Type) (retr : Retract sig tau) : Retract sig (sigOption tau).
Variable (sigX: Type) (X: Type) (cX : codable sigX X).
Global Instance Encode_option : codable (sigOption sigX) (option X) :=
{|
encode o := match o with
| None => [sigOption_None]
| Some x => sigOption_Some :: Encode_map _ _ x
end;
|}.
Definition Encode_option_size (o : option X) :=
match o with
| None => 1
| Some x => S (size x)
end.
End Encode_option.
Arguments sigOption_Some {sigX}. Arguments sigOption_None {sigX}. Arguments sigOption_X {sigX}.
Hint Extern 4 (finTypeC (EqType (sigOption _))) => eapply sigOption_fin : typeclass_instances.
(* Check FinType (EqType (sigOption bool)). *)
(* Compute Encode_option Encode_bool None. *)
(* Compute Encode_option Encode_bool (Some false). *)
Variable sigX: Type.
Variable (X : Type) (cX : codable sigX X).
Fixpoint encode_list (xs : list X) : list (sigList sigX) :=
match xs with
| nil => [sigList_nil]
| x :: xs' => sigList_cons :: Encode_map _ _ x ++ encode_list xs'
end.

Lemma encode_list_concat l:
encode_list l = concat (map (fun t => sigList_cons :: map sigList_X (encode t)) l) ++[sigList_nil].