purescript-profunctor icon indicating copy to clipboard operation
purescript-profunctor copied to clipboard

`second`/`right` derived from `first`/`left`

Open erykciepiela opened this issue 7 months ago • 1 comments

Is there any reason why Strong/Choice require second/right and not instead derive them from first/left?

secondBasedOnFirst :: forall p a b c. Strong p => p a b -> p (Tuple c a) (Tuple c b)
-- equivalent to `second`?
secondBasedOnFirst = dimap swap swap <<< first
  where
    swap :: forall x y. Tuple x y -> Tuple y x
    swap (Tuple a b) = Tuple b a

rightBasedOnLeft :: forall p a b c. Choice p => p a b -> p (Either c a) (Either c b)
-- equivalent to `right`?
rightBasedOnLeft = dimap flip flip <<< left
  where
    flip :: forall x y. Either x y -> Either y x
    flip (Left a) = Right a
    flip (Right b) = Left b

Is this only because of lack of default type class member implementations feature in PureScript or is there any other reason?

erykciepiela avatar May 11 '25 10:05 erykciepiela

Yeah, it's due to the lack of default member implementations. We could add these to the library though, we do that for the similar case for Foldable, Traversable, etc: https://pursuit.purescript.org/packages/purescript-foldable-traversable/docs/Data.Foldable#v:foldrDefault

garyb avatar May 11 '25 13:05 garyb