New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[stdlib] several NoDup lemmas #18172
Conversation
Fyi, there already is a Injective_map_NoDup. |
@mrhaandi Definition insert_at i x l :=
firstn i l ++ x :: skipn i l.
Definition additions x l :=
map (fun i => insert_at i x l) (seq 0 (S (length l))).
Definition permutations :=
fix F l :=
match l with
| nil => [ nil ]
| x::l' => flat_map (additions x) (F l')
end. The function |
For your use-case, what about: Lemma ForallPairs_inj_map_NoDup [A B] (f: A->B) (l: list A) :
ForallPairs (fun x y => f x = f y -> x = y) l -> NoDup l -> NoDup (map f l).
Proof.
intros Hinj Hl.
induction Hl as [|x ?? _ IH]; cbn; constructor.
- intros [y [??]]%in_map_iff.
destruct (Hinj y x); cbn; auto.
- apply IH.
intros x' y' Hx' Hy'.
now apply Hinj; right.
Qed. |
@mrhaandi Nice, I like it! Should I replace |
Yes, that would be my suggestion (and remove |
Fyi, here are different proofs of the above statements. Feel free to ignore or copy (this is entirely stylistic choice). Lemma NoDup_app (l1 l2 : list A):
NoDup l1 -> NoDup l2 -> (forall a, In a l1 -> ~ In a l2) ->
NoDup (l1 ++ l2).
Proof.
intros H ?. induction H; [easy|].
cbn. intros H'. constructor; [|now auto].
intros [?|?%H']%in_app_or; now auto.
Qed.
Lemma NoDup_concat [A] (L: list (list A)):
Forall (@NoDup A) L ->
ForallOrdPairs (fun l1 l2 => forall a, In a l1 -> ~ In a l2) L ->
NoDup (concat L).
Proof.
intros H' H. revert H'.
induction H as [|l L HL _ IH]; [constructor|].
cbn. intros [??%IH]%Forall_cons_iff.
apply NoDup_app; [easy..|].
intros ??[?[??]]%in_concat.
rewrite Forall_forall in HL.
eapply HL; eassumption.
Qed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are welcome additions. It's a pity that this PR has fallen through the cracks.
@andres-erbsen I changed the names accordingly and added a changelog. |
@coqbot run full ci |
@coqbot merge now |
@andres-erbsen: You cannot merge this PR because:
|
@coqbot merge now |
This PR adds following lemmas about
NoDup
:The last lemma was useful to me for arguing about
NoDup (flat_map ...)
usingflat_map_concat_map
.