Fixpoint permute(ls : list A)(sigma : list nat) : list A :=
    match sigma with
      | nil => nil
      | n :: sigma' => 
        match (nth_error ls n) with
          | None => nil
          | Some a => a :: (permute ls sigma')
        end
  end.

Theorem permute_length_eq : forall (sigma : list nat)(ls : list A), (forall n, In n sigma -> n < length ls) -> length (permute ls sigma) = length sigma.