Definition kfilter (p : pred K) (s : fmap) := FinMap (sorted_kfilter p s).

(* Specifications: *)
Lemma supp_kfilt (p : pred K) (s : fmap) : supp (kfilter p s) = filter p (supp s).
Lemma kfilt_nil (p : pred K) : kfilter p nil = nil.
Lemma fnd_kfilt (p : pred K) k (s : fmap) : fnd k (kfilter p s) = if p k then fnd k s else None.
Lemma kfilt_ins (p : pred K) k v (s : fmap) : kfilter p (ins k v s) = if p k then ins k v (kfilter p s) else kfilter p s.
Lemma rem_kfilt (p : pred K) k (s : fmap) : rem k (kfilter p s) = if p k then kfilter p (rem k s) else kfilter p s.
Lemma kfilt_rem (p : pred K) k (s : fmap) : kfilter p (rem k s) = if p k then rem k (kfilter p s) else kfilter p s.
Lemma kfilt_fcat (p : pred K) (s1 s2 : fmap) : kfilter p (fcat s1 s2) = fcat (kfilter p s1) (kfilter p s2).
Lemma kfilter_pred0 (s : fmap) : kfilter pred0 s = nil.
Lemma kfilter_predT (s : fmap) : kfilter predT s = s.
Lemma kfilter_predI p1 p2 (s : fmap) : kfilter (predI p1 p2) s = kfilter p1 (kfilter p2 s).
Lemma kfilter_predU p1 p2 (s : fmap) : kfilter (predU p1 p2) s = fcat (kfilter p1 s) (kfilter p2 s).
Lemma eq_in_kfilter p1 p2 s : {in supp s, p1 =1 p2} -> kfilter p1 s = kfilter p2 s.