Skip to content

[breaking] Switch union to be right-biased - #4

Closed
fehrenbach wants to merge 2 commits into
purescript:masterfrom
fehrenbach:union
Closed

fehrenbach wants to merge 2 commits into
purescript:masterfrom
fehrenbach:union

Conversation

@fehrenbach

Copy link
Copy Markdown

On top of #3.

This is a breaking change!

Closes #2.

@fehrenbach fehrenbach closed this Oct 22, 2018
@garyb

garyb commented Oct 22, 2018

Copy link
Copy Markdown
Member

I'm still in favour of doing this, it's just a breaking change that will spread really far so will probably wait until we have a couple of other breaking core library changes.

@fehrenbach

Copy link
Copy Markdown
Author

Ah, sorry. I was going through stuff and was feeling less enthusiastic. It's a subtle breaking change that is not visible in the type and the benefit is marginal. I'll reopen.

@fehrenbach fehrenbach reopened this Oct 23, 2018
@garyb

garyb commented Oct 23, 2018

Copy link
Copy Markdown
Member

No problem! Sorry it was just left sitting here with no comment even.

@paluh

paluh commented Dec 18, 2018 •

Copy link
Copy Markdown

@garyb Do you plan to merge this PR or do you consider take the more flexible approach suggested by @LiamGoodacre: (Ord k, Semigroup v) => Semigroup (Map k v)?

@garyb

garyb commented Dec 18, 2018

Copy link
Copy Markdown
Member

I'm happy to talk about it, that version definitely has some nice properties, but in practice I've very rarely wanted that style of union - StrMap had that instance, and it tripped me up more than once, as I assumed it was a biased union instead.

It is pretty easy to get either behaviour specifically via unionWith, so I guess we don't need to provide a bunch of union varieties directly, it's just the problem of deciding which is the "more natural" default for Semigroup. I guess the other option would be to remove the instance entirely, as similarly we don't provide them for numbers since there's no obvious default choice for additive or multiplicative behaviour.

@paluh

paluh commented Dec 19, 2018 •

Copy link
Copy Markdown

I'm happy to talk about it, that version definitely has some nice properties, but in practice I've very rarely wanted that style of union

From this PR it seems that it is really hard to say what is desired style of union. Instance based on value Semigroup make no assumptions about exact append strategy and gives the user the opportunity to define desired union strategy like it is done in case of Maybe.

I guess the other option would be to remove the instance entirely, as similarly we don't provide them for numbers since there's no obvious default choice for additive or multiplicative behaviour.

I don't think it is better than defining instance based on value Semigroup - from my perspective it makes minimal (hard do say if "natural" ;-)) choice.

StrMap had that instance (...)

Foreign.Object has still this instance (instance semigroupObject :: (Semigroup a) => Semigroup (Object a)) and this is also somewhat surprising difference between these two types.

@garyb

garyb commented Dec 19, 2018

Copy link
Copy Markdown
Member

I don't think it is better than defining instance based on value Semigroup - from my perspective it makes minimal (hard do say if "natural" ;-)) choice.

The benefit I see is that it would avoid weird unexpected behaviour (when under the assumption it's union based), which is what I encountered with the StrMap / Object instance, since many types are Semigroup so it can be used "accidentally" - that also means in a lot of cases it'd be a drastic breaking change that doesn't result in compilation errors.

For what it's worth, Haskell uses union for the Monoid / Semigroup instance too 😉

Maybe does raise an interesting point though - its Alt instance is left-biased choice between values, so maybe we should do something similar with Map - use union for alt and unionWith append for append. 🤷‍♂️

@arsdragonfly

Copy link
Copy Markdown

I would suggest introducing (Ord k, Semigroup v) => Semigroup (HashMap k v) into ordered-collections and using the First and Last newtype to achieve the left-biased and right-biased behavior. A more flexible semigroup allows applications like

counter = foldMap (\w -> singleton w (Additive 1)) [ "apple", "banana", "apple" ]
inverted_index = foldMap (\t -> singleton (snd t) [ fst t ]) [ Tuple "Alice" "left-biased", Tuple "Bob" "right-biased", Tuple "Eve" "left-biased" ]

@hdgarrood

Copy link
Copy Markdown
Contributor

I’m sorry that I didn’t raise this sooner, but I’m actually not at all keen on this. This is almost certainly going to break lots of downstream code in a way that’s only visible at runtime. Also, I feel like right bias for these kinds of functions is generally pretty uncommon anyway. Are we using right bias anywhere else in the core libraries?

@paluh

paluh commented May 25, 2020 •

Copy link
Copy Markdown

@hdgarrood @garyb should I open another issue related to possible Alt addition and possible switch to instance semigroupMap :: Semigroup a => Semigroup (Map a) to improve clarity of the discussion?

@hdgarrood

Copy link
Copy Markdown
Contributor

I'm not sure a new issue is necessary, since we already have this thread and #2. My vote would be to leave everything as it is, though. I don't see a way of changing this instance to any of the suggested alternatives which won't break a lot of code. At the very least I think we would need to get community approval for this proposal on Discourse.

@paluh

paluh commented May 25, 2020

Copy link
Copy Markdown

@hdgarrood Cool. Thanks for quick response!

@hdgarrood

Copy link
Copy Markdown
Contributor

Thanks for the PR, but I think at this stage it’s fairly clear that this isn’t going to happen (cf https://discourse.purescript.org/t/unbiasing-the-semigroup-instance-for-map/1935) - there appears to be a fairly strong consensus that unionWith (<>) would be a preferable Semigroup instance, and additionally it’s doubtful whether we can justify changing this in a breaking way anyway; so I’m going to close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Change union to be right-biased

5 participants