(* ls1 is the smaller set.  This function returns a permutation of ls2 where the first elements of ls2 match the order of the elements in ls1.  This function only works if ls1 is a subset of ls2. *)
Fixpoint matchOrder (A : Set)(eqd : eq_dec A)(ls1 ls2 : list A) :=
  match ls1 with
    | nil => ls2
    | a :: ls1' => 
      a :: (matchOrder eqd ls1' (removeFirst eqd ls2 a))
  end.

(* Specifications: *)
Lemma matchOrder_In : forall (A : Set)(eqd : eq_dec A)(ls1 ls2 : list A), NoDup ls1 -> NoDup ls2 -> (forall a, In a ls1 -> In a ls2) -> (forall a, In a ls2 <-> In a (matchOrder eqd ls1 ls2)).
Lemma matchOrder_not_in_h : forall (A : Set)(eqd : eq_dec A)(ls1 ls2 : list A) a, ~In a ls1 -> ~In a ls2 -> ~In a (matchOrder eqd ls1 ls2).
Lemma matchOrder_not_in : forall (A : Set)(eqd : eq_dec A)(ls1 ls2 : list A) a, (forall a, In a ls1 -> In a ls2) -> ~In a ls2 -> ~In a (matchOrder eqd ls1 ls2).
Lemma matchOrder_NoDup : forall (A : Set)(eqd : eq_dec A)(ls1 ls2 : list A), (forall a, In a ls1 -> In a ls2) -> NoDup ls2 -> NoDup ls1 -> NoDup (matchOrder eqd ls1 ls2).
Lemma matchOrder_permutation : forall (A : Set)(eqd : eq_dec A)(ls1 ls2 : list A), NoDup ls1 -> NoDup ls2 -> (forall a, In a ls1 -> In a ls2) -> Permutation ls2 (matchOrder eqd ls1 ls2).
Lemma matchOrder_firstn : forall (A : Set)(eqd : eq_dec A)(ls1 ls2 : list A), NoDup ls1 -> NoDup ls2 -> (forall a, In a ls1 -> In a ls2) -> firstn (length ls1) (matchOrder eqd ls1 ls2) = ls1.
Lemma permutation_NoDup : forall (A : Type)(ls1 ls2 : list A), Permutation ls1 ls2 -> NoDup ls1 -> NoDup ls2.