(* * Encoding for binary numbers *)
From Undecidability.Shared.Libs.PSL Require Export Bijection.
Require Export BinNums.
(* We use Coq's standard types [positive] and [N] *)
(* It is important to note that the least-significant bit is the last symbol in the encoding *)
Inductive sigPos : Type :=
| sigPos_xI : sigPos
| sigPos_xO : sigPos
| sigPos_xH : sigPos.
Global Instance sigPos_eq : eq_dec sigPos.
(* Compute encode (42 % positive). *)
(* With this alphabet it is easier to derive the machines for [N] using the machines for [positive] *)
Notation sigN := (sigOption sigPos).
Definition N_to_optionPos : BinNums.N -> option positive :=
fun n => match n with
| N0 => None
| Npos p => Some p
end.
Instance Encode_N : codable sigN BinNums.N :=
{|
encode n := encode (N_to_optionPos n);
|}.
Definition Encode_N_size (n : N) : nat :=
match n with
| N0 => 1
| Npos p => S (Pos.size_nat p)
end.

Lemma Encode_N_hasSize (n : N) :
size n = Encode_N_size n.