Module fin_OT (Import N : NatValue) <: UsualOrderedType.
Definition t := fin n.
Definition eq := @eq (fin n).
Definition eq_equiv := @eq_equivalence (fin n).
Definition lt := @fin_lt n.
Definition lt_strorder := fin_lt_strorder n.
Definition lt_compat := fin_lt_lt_compat n.
Definition compare := fun x y => proj1_sig (fin_compare n x y).
Definition compare_spec := fun x y => proj2_sig (fin_compare n x y).
Definition eq_dec := fin_eq_dec n.
End fin_OT.
Fixpoint fin_of_nat (m n : nat) : fin n + {exists p, m = n + p} :=
match n with
| 0 => inright (ex_intro _ _ eq_refl)
| S n' =>
match m with
| 0 => inleft None
| S m' =>
match fin_of_nat m' n' with
| inleft f => inleft (Some f)
| inright pf =>
inright (match pf with
| ex_intro _ x H => ex_intro _ x (f_equal _ H)
end)
end
end
end.

Lemma fin_of_nat_fin_to_nat :
forall (n : nat) (a : fin n),
fin_of_nat (fin_to_nat a) n = inleft a.