Safe Haskell | None |
---|---|
Language | Haskell2010 |
- class Compactable f where
- fforMaybe :: (Compactable f, Functor f) => f a -> (a -> Maybe b) -> f b
- filter :: (Compactable f, Functor f) => (a -> Bool) -> f a -> f a
- fmapMaybeM :: (Compactable f, Monad f) => (a -> MaybeT f b) -> f a -> f b
- fforMaybeM :: (Compactable f, Monad f) => f a -> (a -> MaybeT f b) -> f b
- applyMaybeM :: (Compactable f, Monad f) => f (a -> MaybeT f b) -> f a -> f b
- bindMaybeM :: (Compactable f, Monad f) => f a -> (a -> f (MaybeT f b)) -> f b
- traverseMaybeM :: (Monad m, Compactable t, Traversable t) => (a -> MaybeT m b) -> t a -> m (t b)
Documentation
class Compactable f where Source #
This is a generalization of catMaybes as a new function compact. Compact has relations with Functor, Applicative, Monad, Alternative, and Traversable. In that we can use these class to provide the ability to operate on a data type by throwing away intermediate Nothings. This is useful for representing striping out values or failure.
In order to be Compactable, the following law should hold:
- Kleisli composition
fmapMaybe (l <=< r) = fmapMaybe l . fmapMaybe r
If the data type is also a Functor the following should hold:
- Functor identity 1
compact . fmap Just = id
- Functor identity 2
fmapMaybe Just = id
- Functor relation
compact = fmapMaybe id
According to Kmett, (Compactable f, Functor f) is a functor from the
kleisli category of Maybe to the category of haskell data types.
Kleisli Maybe -> Hask
.
If the data type is also Applicative the following should hold:
- Applicative identity 1
@compact . (pure Just *) = id
- Applicative identity 2
applyMaybe (pure Just) = id
- Applicative relation
compact = applyMaybe (pure id)
If the data type is also a Monad the following should hold:
- Monad nameme
bindMaybe (return (Just x)) return = return x
- Monad identity 1
flip bindMaybe (return . Just) = id
- Monad identity 2
compact . (return . Just =<<) = id
- Monad relation
compact = flip bindMaybe return
If the data type is also Alternative the following should hold:
- Alternative identity
compact empty = empty
- Alternative annihilation
compact (const Nothing <$> xs) = empty
If the data type is also Traversable the following should hold:
- Traversable name me
traverseMaybe (pure . Just) = pure
- Traversable composition
Compose . fmap (traverseMaybe f) . traverseMaybe g = traverseMaybe (Compose . fmap (traverseMaybe f) . g)
- Traversable name me
traverse f = traverseMaybe (fmap Just . f)
- Traversable naturality
t . traverseMaybe f = traverseMaybe (t . f)
If you know of more useful laws, or have better names for the ones above (especially those marked "name me"). Please let me know.
compact :: f (Maybe a) -> f a Source #
compact :: (Monad f, Alternative f) => f (Maybe a) -> f a Source #
fmapMaybe :: Functor f => (a -> Maybe b) -> f a -> f b Source #
applyMaybe :: Applicative f => f (a -> Maybe b) -> f a -> f b Source #
bindMaybe :: Monad f => f a -> (a -> f (Maybe b)) -> f b Source #
traverseMaybe :: (Applicative g, Traversable f) => (a -> g (Maybe b)) -> f a -> g (f b) Source #
Compactable [] Source # | |
Compactable Maybe Source # | |
Compactable IO Source # | |
Compactable Option Source # | |
Compactable STM Source # | |
Compactable ReadPrec Source # | |
Compactable ReadP Source # | |
Compactable IntMap Source # | |
Compactable Seq Source # | |
Compactable Vector Source # | |
Compactable (Proxy *) Source # | |
Compactable (Map k) Source # | |
(Compactable f, Compactable g) => Compactable (Product * f g) Source # | |
(Functor f, Functor g, Compactable g) => Compactable (Compose * * f g) Source # | |
fmapMaybeM :: (Compactable f, Monad f) => (a -> MaybeT f b) -> f a -> f b Source #
fforMaybeM :: (Compactable f, Monad f) => f a -> (a -> MaybeT f b) -> f b Source #
applyMaybeM :: (Compactable f, Monad f) => f (a -> MaybeT f b) -> f a -> f b Source #
bindMaybeM :: (Compactable f, Monad f) => f a -> (a -> f (MaybeT f b)) -> f b Source #
traverseMaybeM :: (Monad m, Compactable t, Traversable t) => (a -> MaybeT m b) -> t a -> m (t b) Source #