haskellari / these

An either-or-both data type, with corresponding hybrid error/writer monad transformer.
117 stars 49 forks source link

"unalign can be defined for any Functor" - incorrect? #185

Closed LightAndLight closed 1 year ago

LightAndLight commented 1 year ago

The README says unalign can be defined for any Functor. Am I missing something? That doesn't seem right:

unalignDefault :: Functor f => f (These a b) -> (f a, f b)
unalignDefault xs =
  ( fmap (\case; This a -> a; That b -> _ :: a; These a _ -> a) xs
  , fmap (\case; This a -> _ :: b; That b -> b; These _ b -> b) xs
  )

There are holes we can't fill.

Interestingly, it would seem thatunalign can be implemented for any Filterable. I don't know if it's lawful:

unalignDefault :: Filterable f => f (These a b) -> (f a, f b)
unalignDefault xs =
  ( mapMaybe (\case; This a -> Just a; That _ -> Nothing; These a _ -> Just a) xs
  , mapMaybe (\case; This _ -> Nothing; That b -> Just b; These _ b -> Just b) xs
  )

Given the compatibility note in the unalign docs, I'm guessing that the README was written about this version, before semialign was split out of these.

I'll submit a PR soon.

phadej commented 1 year ago

Given that README haven't been touched in 8 years, and I'm not the one wrote that, it could be a mistake.