Skip to content
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

Lists: porting several lemmas from other libraries #2479

Merged
merged 13 commits into from
Sep 25, 2024

Conversation

omelkonian
Copy link
Contributor

Porting useful list properties that we needed in the Cardano ledger project, several of which are themselves ported from my personal prelude.

I am totally fine with some of those not being merged due to the 'Fairbairn threshold' or just because it is too niche of a utility to be included in the standard library; I'll let the reviewer(s) decide.

NB: each commit is somewhat independent, so reviews can proceed in a per-commit fashion (unless a change is requested globally, e.g. cosmetic changes w.r.t. to the style guide).

Copy link
Contributor

@jamesmckinna jamesmckinna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for a huge contribution!

Lots of nitpicky feedback, but I think quite a few changes (removing redundancy etc.) would make this a lot tighter.

CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated
* In `Data.List.Relation.Unary.Unique.Setoid.Properties`:
```agda
Unique-dropSnd : Unique S (x ∷ y ∷ xs) → Unique S (x ∷ xs)
Unique∷⇒head∉tail : Unique S (x ∷ xs) → x ∉ xs
Copy link
Contributor

@jamesmckinna jamesmckinna Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR introduces a lot of new names, some of which, like this one, are pretty hard to read to begin with... so maybe an alternative is preferable?

Suggested change
Unique∷⇒head∉tail : Unique S (x ∷ xs) → x ∉ xs
Unique[x∷xs]⇒x∉xs : Unique S (x ∷ xs) → x ∉ xs

Etc. (my weakness for named variables vs. point-free/combinator-style naming?)

Shoutout to @MatthewDaggitt who is usually pretty good on coming up with good/better names for things...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've renamed to your suggestion for now, although I have to point out that I find this naming approach redundant: why would you want to repeat the type as-is? This is mere duplication, while names should provide "shortcuts" that are more intuitive/abstract/natural than the type that accompanies them (e.g. ⁺/⁻ for intro/elim).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take your point, just as I'm grateful you took my suggestion. I confess I don't think my position is necessarily 'correct' or (cognitively) 'optimal', but sometimes I think there is a place for redundancy...

... hence the call-out to a second reviewer.

CHANGELOG.md Outdated Show resolved Hide resolved
@omelkonian omelkonian force-pushed the orestis/list-additions branch from b34bad7 to 0512041 Compare September 12, 2024 10:15
@omelkonian omelkonian force-pushed the orestis/list-additions branch from 0512041 to d462216 Compare September 12, 2024 10:20
@jamesmckinna jamesmckinna added this to the v2.2 milestone Sep 12, 2024
CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
@omelkonian omelkonian force-pushed the orestis/list-additions branch from d462216 to 75b4941 Compare September 13, 2024 14:14
@omelkonian omelkonian force-pushed the orestis/list-additions branch from 75b4941 to 53825a7 Compare September 13, 2024 15:32
@omelkonian omelkonian force-pushed the orestis/list-additions branch from 53825a7 to a50564e Compare September 13, 2024 15:44
@omelkonian omelkonian force-pushed the orestis/list-additions branch from a50564e to 49496df Compare September 16, 2024 13:13
CHANGELOG.md Outdated
Comment on lines 130 to 133
∈-map∘filter⁻ : y ∈₂ map f (filter P? xs) → ∃[ x ] x ∈₁ xs × y ≈₂ f x × P x
∈-map∘filter⁺ : f Preserves _≈₁_ ⟶ _≈₂_ →
∃[ x ] x ∈₁ xs × y ≈₂ f x × P x →
y ∈₂ map f (filter P? xs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These feel super ad-hoc. Why map and filter in particular?
Why not e.g. mapMaybe? Or unalignWith?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I agree, and I was actually reluctant to include this in this PR for the same reason.

The only argument I find here is that these are operations that are so incredibly common, that if one user goes through the trouble of defining the composition to re-use it across their codebase/application, then probably it is worth including in the stdlib for other users. (It is, however, difficult to provide evidence for what is common and what is not.)

Of course, it is infeasible to be complete in any way, i.e. cover all possible combinations of list operations, but maybe a "the-more-the-merrier" strategy is better in this case, and we should encourage users to submit such combinations as they arise?

CHANGELOG.md Outdated Show resolved Hide resolved
@@ -123,10 +126,13 @@ module _ {v : A} where
∈-++⁻ : ∀ xs {ys} → v ∈ xs ++ ys → (v ∈ xs) ⊎ (v ∈ ys)
∈-++⁻ = Membership.∈-++⁻ (≡.setoid A)

∈-++ : v ∈ xs ++ ys ⇔ (v ∈ xs ⊎ v ∈ ys)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably mention in the name (cf. concat-∈↔ below)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to have a consistent naming convention for bundling introductions & eliminations?
Since intros are named <property_name>⁺ (modulo left/right qualifiers) and eliminators <property_name>⁻, my suggestion was to just drop the polarities (i.e. <property_name>) to communicate that this includes both intro & elim.

Another naming convention that I find intuitive is to explicitly include both polarities: <property_name>⁺⁻, e.g. ∈-++⁺⁻.

The things I don't like about the current naming of such bundles are the following:

  • The exact arrow being used will change in each case (whether it's an equivalence , an inverse , a bi-inverse, etc...); isn't it preferable to have a universal convention that hides such differences (much like we do for intros/elims which might take different forms in each individual case but are always named with and .
  • names like concat-∈↔ are not immediately linked to the intros/elims that it contains (i.e. the <property_name> part is not retained)

So, if I follow the current practices, I would have to name this ++-∈⇔ :(
I can definitely do that, but I find it ugly and unintuitive.

@jamesmckinna jamesmckinna added this pull request to the merge queue Sep 24, 2024
@jamesmckinna
Copy link
Contributor

Thanks @JacquesCarette ! merging now...

@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 24, 2024
@jamesmckinna
Copy link
Contributor

jamesmckinna commented Sep 24, 2024

So, the CI errors on the attempted merge are beyond my knowledge/understanding... can anyone (eg @gallais ) diagnose/fix?

@jamesmckinna jamesmckinna added this pull request to the merge queue Sep 25, 2024
github-merge-queue bot pushed a commit that referenced this pull request Sep 25, 2024
* Lists: irrelevance of membership (in unique lists)

* Lists: the empty list is always a sublist of another

* Lists: lookupMaybe (unsafe version of lookup)

* List: `concatMap⁺` for the subset relation

* Lists: memberhsip lemma for AllPairs

* Lists: membership lemmas for map∘filter

* Lists: lemmas about concatMap

* Lists: lemmas about deduplicate

* Lists: membership lemmas about nested lists

* Lists: extra subset lemmas about _∷_

* Lists: extra lemmas about _∷_/Unique

* Lists: incorporate feedback from James

* Lists: incorporate feedback from Guillaume
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 25, 2024
@jamesmckinna jamesmckinna added this pull request to the merge queue Sep 25, 2024
@jamesmckinna
Copy link
Contributor

3rd time lucky?

Merged via the queue into master with commit 7273c6e Sep 25, 2024
2 checks passed
@omelkonian omelkonian deleted the orestis/list-additions branch September 26, 2024 09:46
github-merge-queue bot pushed a commit that referenced this pull request Sep 28, 2024
* [DRY] refactoring

* remove deprecations; leave lemma renamed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants