Fixpoint apply_hist S h :=
match h with
| [] => S
| Add e :: h' => if in_dec Z_eq_dec e S then apply_hist S h' else apply_hist (sort (e :: S)) h'
| Remove e :: h' => apply_hist (remove Z_eq_dec e S) h'
end.

Lemma apply_hist_app : forall h1 S h2, apply_hist S (h1 ++ h2) = apply_hist (apply_hist S h1) h2.