Skip to content

Unbiased Semigroup instance for Data.Map.Map #36

@kl0tl

Description

@kl0tl

The Semigroup instance for Data.Map.Map is right-biased, meaning that duplicates are droped from the LHS, but the instance for Foreign.Object.Object requires a Semigroup constraint on its elements type to append duplicates!

We should at least document the difference or either bias the instance for Foreign.Object.Object or unbias the instance for Data.Map.Map.

I believe the behaviour of Foreign.Object.Object to be the most sensible here and would like the Data.Map.Map instance to be unbiased, especially now that we have Coercible.

The following would benefit from type applications, deriving via, constraint kinds and quantified constraints (:upside_down_face:) but is otherwise usable today and allows us to recover the behaviour of the current Semigroup instance for Data.Map.Map with _LeftBiased and the combinators from Data.Newtype (ala _LeftBiased foldMap and under2 _LeftBiased (<>) for example) given an unbiased instance:

data Bias

foreign import data Left :: Bias
foreign import data Right :: Bias

newtype Biased :: forall k. Bias -> (k -> Type) -> k -> Type
newtype Biased bias f a = Biased (f a)

_LeftBiased :: forall f a. f a -> Biased Left f a
_LeftBiased = Biased

_RightBiased :: forall f a. f a -> Biased Right f a
_RightBiased = Biased

derive instance newtypeBiased :: Newtype (Biased bias f a) _

instance semigroupLeftBiased :: (Semigroup (f (First a)), Coercible (f a) (f (First a))) => Semigroup (Biased Left f a) where
  append = coerce ((<>) :: f (First a) -> f (First a) -> f (First a))
instance monoidLeftBiased :: (Monoid (f (First a)), Coercible (f a) (f (First a))) => Monoid (Biased Left f a) where
  mempty = coerce (mempty :: f (First a))

instance semigroupRightBiased :: (Semigroup (f (Last a)), Coercible (f a) (f (Last a))) => Semigroup (Biased Right f a) where
  append = coerce ((<>) :: (f (Last a) -> f (Last a) -> f (Last a)))
instance monoidRightBiased :: (Monoid (f (Last a)), Coercible (f a) (f (Last a))) => Monoid (Biased Right f a) where
 mempty = coerce (mempty :: f (Last a))

Metadata

Metadata

Assignees

No one assigned

    Labels

    purs-0.15A reminder to address this issue or merge this PR before we release PureScript v0.15.0type: breaking changeA change that requires a major version bump.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions