Fixpoint Fin n : Type :=
  match n with
  | 0%nat => Empty_set
  | S m => unit + Fin m
  end.Global Instance Fin_EqDec {n} : EqDec (Fin n).Class Finite X := {
  card : nat;
  to_fin : X -> Fin card;
  from_fin : Fin card -> X;
  to_from : forall x, to_fin (from_fin x) = x;
  from_to : forall x, from_fin (to_fin x) = x
  }.Class Finite X := {
  card : nat;
  to_fin : X -> Fin card;
  from_fin : Fin card -> X;
  to_from : forall x, to_fin (from_fin x) = x;
  from_to : forall x, from_fin (to_fin x) = x
  }.Class Finite X := {
  card : nat;
  to_fin : X -> Fin card;
  from_fin : Fin card -> X;
  to_from : forall x, to_fin (from_fin x) = x;
  from_to : forall x, from_fin (to_fin x) = x
  }.Class Finite X := {
  card : nat;
  to_fin : X -> Fin card;
  from_fin : Fin card -> X;
  to_from : forall x, to_fin (from_fin x) = x;
  from_to : forall x, from_fin (to_fin x) = x
  }.Class Finite X := {
  card : nat;
  to_fin : X -> Fin card;
  from_fin : Fin card -> X;
  to_from : forall x, to_fin (from_fin x) = x;
  from_to : forall x, from_fin (to_fin x) = x
  }.Class Finite X := {
  card : nat;
  to_fin : X -> Fin card;
  from_fin : Fin card -> X;
  to_from : forall x, to_fin (from_fin x) = x;
  from_to : forall x, from_fin (to_fin x) = x
  }.Global Instance Finite_EqDec {X} `{Finite X} : EqDec X.Fixpoint to_fin_sum {m n} : Fin m + Fin n -> Fin (m + n) :=
  match m with
  | 0 => fun s =>
    match s with
    | inl e =>
      match e with
      end
    | inr i => i
    end
  | S k => fun s =>
    match s with
    | inl (inl tt) => inl tt
    | inl (inr i) => inr (to_fin_sum (inl i))
    | inr i => inr (to_fin_sum (inr i))
    end
  end.Fixpoint from_fin_sum {m n} : Fin (m + n) -> Fin m + Fin n :=
  match m with
  | 0 => fun i => inr i
  | S k => fun i =>
    match i with
    | inl tt => inl (inl tt)
    | inr j =>
      match from_fin_sum j with
      | inl j' => inl (inr j')
      | inr j' => inr j'
      end
    end
  end.

Lemma from_to_fin_sum {m n} : forall s : Fin m + Fin n, from_fin_sum (to_fin_sum s) = s.