-
Notifications
You must be signed in to change notification settings - Fork 27
Description
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))