Section Nth'.
(* In this implementation of [nth_error], instead of encoding an option to the output tape, we use the finite parameter to indicate whether the result is [Some] or [None]. The advantage is that the client doesn't have to add the option to its alphabet. *)
Variable (sig sigX : finType) (X : Type) (cX : codable sigX X).
(* Hypothesis (defX: inhabitedC sigX). *)
Variable (retr1 : Retract (sigList sigX) sig) (retr2 : Retract sigNat sig).
Local Instance retr_X_list' : Retract sigX sig := ComposeRetract retr1 (Retract_sigList_X _).
(* Check _ : codable sig (list X). *)
(* Check _ : codable sig nat. *)
Definition Nth'_Step_size {sigX X : Type} {cX : codable sigX X} (n : nat) (l : list X) : Vector.t (nat -> nat) 3 :=
match n, l with
| S n', x :: l' =>
[| CaseList_size0 x; S; CaseList_size1 x >> Reset_size x|]
| 0, x :: x' =>
[|fun s0 => CaseList_size0 x s0; id; fun s2 => CaseList_size1 x s2|]
| 0, nil => [|id; id; id|]
| S n', nil => [|id; fun s1 => S s1; id|]
end.
Definition Nth'_Step : pTM sig^+ (option bool) 3 :=
If (LiftTapes (ChangeAlphabet CaseNat _) [|Fin1|])
(If (LiftTapes (ChangeAlphabet (CaseList sigX) _) [|Fin0; Fin2|]) (* n = S n' *)
(Return (LiftTapes (Reset _) [|Fin2|]) None) (* l = x :: l'; continue *)
(Return Nop (Some false))) (* l = nil; return false *)
(Relabel (LiftTapes (ChangeAlphabet (CaseList sigX) _) [|Fin0; Fin2|]) Some) (* n = 0 *)
.
Definition Nth'_Step_steps {sigX X : Type} {cX : codable sigX X} (l : list X) (n : nat) :=
match n, l with
| S n', x :: l' =>
2 + CaseNat_steps + CaseList_steps_cons x + Reset_steps x
| S n', nil =>
2 + CaseNat_steps + CaseList_steps_nil
| O, x :: l' =>
1 + CaseNat_steps + CaseList_steps_cons x
| O, nil =>
1 + CaseNat_steps + CaseList_steps_nil
end.
Definition Nth'_Step_steps_CaseList (xs : list X) :=
match xs with
| x :: xs' => 1 + CaseList_steps_cons x + Reset_steps x
| nil => 1 + CaseList_steps_nil
end.

Lemma Nth'_Step_SpecT_size (n : nat) (xs : list X) (ss : Vector.t nat 3) :
TripleT
(≃≃([],withSpace ([|Contains _ xs; Contains _ n; Void|]) ss))
(Nth'_Step_steps xs n) Nth'_Step
(fun yout =>
≃≃([yout = match xs,n with [],_ => Some false | _ , 0 => Some true | _,__ => None end ],
withSpace
match xs,n with
| nil,_ => [|Contains _ xs; Contains _ (pred n); Void|]
| x::xs', S n' => [|Contains _ xs'; Contains _ n'; Void|]
| x::xs', 0 => [|Contains _ xs'; Contains _ 0;  Contains _ x|]
end (appSize (Nth'_Step_size n xs) ss))).