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
[Merged by Bors] - feat(data/list/destutter): add list.destutter
to remove chained duplicates
#11934
Conversation
ericrbg
commented
Feb 9, 2022
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.
Am I blind, or are you missing the lemma that the list with no pairwise duplicates satisfies list.chain' ne
?
On that note, should this be called chain'_dedup
, since list.pairwise
isn't about adjacency?
I like |
oh, I now see why the naming is as it is. I guess I can name the |
well, that's a bug in the buildscript! we can't have |
Yes, that's #11001 |
it's a separate one from that; https://github.com/leanprover-community/mathlib/runs/5140357750?check_suite_focus=true it's to do with |
list.chain_dedup'
to remove chained duplicates
Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
I pushed a couple more golfs, and a |
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.
LGTM, let's let a fresh pair of eyes confirm.
If you're feeling brave, the following lemmas would probably be useful:
- something about
concat
(l ++ [a]
) - something about
append
l.reverse.chain'_dedup = l.chain'_dedup.reverse
src/data/list/chain_dup.lean
Outdated
/-- Removes all adjacent duplicates in `a :: l`. -/ | ||
def chain_dedup : α → list α → list α | ||
| a [] := [] | ||
| a (h :: l) := if a = h then chain_dedup h l else h :: chain_dedup h l |
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.
- This and
chain_dedup'
should be indata.list.defs
. Also could there be a version that accepts a relation? - The
chain
naming suggests that it is related tolist.chain
, but that definition takes a relation. - The use of
h :: chain_dedup h l
looks like a typo. Shouldn't it bea ::
? This will always discarda
even ifa :: l
has no duplicates.
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.
I thought about the relation generalisation, but then if !a R b
, which one would I discard?
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.
and on 3, it seems to break all my proofs in ways that make it seem genuinely different from what I'd expect - if I'm honest, I focused mainly on chain'_dedup
, so I didn't pay as close attention as to how exactly to implement chain_dedup
without it. Do you think the base case should be | a [] := [a]
? That seems reasonable, now that I think about it a little
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.
Discarding the later entry in the list is probably the right choice - the description then becomes "greedily take items from the list (in order) discarding any that do not satisfy R last_accepted candidate
", and the current definitions are just the special case when R = ne
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.
I'm busy for the next while, but when I next get a chance I'll try work on that generalisation.
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.
Note that with that generalization, this is the chain
version of list.pw_filter
list.chain_dedup'
to remove chained duplicateslist.destutter'
to remove chained duplicates
list.destutter'
to remove chained duplicateslist.destutter
to remove chained duplicates
list.destutter
to remove chained duplicateslist.destutter
to remove chained duplicates
Thanks 🎉 bors merge |
…licates (#11934) Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
Pull request successfully merged into master. Build succeeded: |
list.destutter
to remove chained duplicateslist.destutter
to remove chained duplicates