File tree Expand file tree Collapse file tree 2 files changed +14
-1
lines changed Expand file tree Collapse file tree 2 files changed +14
-1
lines changed Original file line number Diff line number Diff line change @@ -2,10 +2,13 @@ module Data.Profunctor.Joker where
22
33import Prelude
44
5- import Data.Either (Either (..))
5+ import Control.Alternative (empty )
6+ import Control.MonadPlus (class MonadZero )
7+ import Data.Either (Either (..), either )
68import Data.Newtype (class Newtype , un )
79import Data.Profunctor (class Profunctor )
810import Data.Profunctor.Choice (class Choice )
11+ import Data.Profunctor.Cochoice (class Cochoice )
912
1013-- | Makes a trivial `Profunctor` for a covariant `Functor`.
1114newtype Joker f a b = Joker (f b )
@@ -38,5 +41,10 @@ instance bindJoker :: Bind f => Bind (Joker f a) where
3841
3942instance monadJoker :: Monad m => Monad (Joker m a )
4043
44+ instance cochoiceJoker :: MonadZero f => Cochoice (Joker f )
45+ where
46+ unleft (Joker fa) = Joker $ fa >>= either pure (const empty)
47+ unright (Joker fb) = Joker $ fb >>= either (const empty) pure
48+
4149hoistJoker :: forall f g a b . (f ~> g ) -> Joker f a b -> Joker g a b
4250hoistJoker f (Joker a) = Joker (f a)
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import Data.Newtype (class Newtype)
1515import Data.Profunctor (class Profunctor )
1616import Data.Profunctor.Choice (class Choice )
1717import Data.Profunctor.Closed (class Closed )
18+ import Data.Profunctor.Cochoice (class Cochoice )
1819import Data.Profunctor.Strong (class Strong )
1920import Data.Tuple (Tuple (..))
2021
@@ -75,6 +76,10 @@ instance choiceStar :: Applicative f => Choice (Star f) where
7576 left (Star f) = Star $ either (map Left <<< f) (pure <<< Right )
7677 right (Star f) = Star $ either (pure <<< Left ) (map Right <<< f)
7778
79+ instance cochoiceStar :: MonadZero f => Cochoice (Star f ) where
80+ unleft (Star f) = Star $ \a -> (=<<) (either pure (const empty)) $ f (Left a)
81+ unright (Star f) = Star $ \a -> (=<<) (either (const empty) pure) $ f (Right a)
82+
7883instance closedStar :: Distributive f => Closed (Star f ) where
7984 closed (Star f) = Star \g -> distribute (f <<< g)
8085
You can’t perform that action at this time.
0 commit comments