Skip to content

Commit 7a588ea

Browse files
committed
Add inject class for coproducts
1 parent 8178535 commit 7a588ea

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Data.Functor.Coproduct.Inject where
2+
3+
import Prelude
4+
5+
import Data.Either (Either(..))
6+
import Data.Functor.Coproduct (Coproduct(..), coproduct)
7+
import Data.Maybe (Maybe(..))
8+
9+
class Inject f g where
10+
inj :: forall a. f a -> g a
11+
prj :: forall a. g a -> Maybe (f a)
12+
13+
instance injectLeft :: Inject f g => Inject f (Coproduct g h) where
14+
inj = Coproduct <<< Left <<< inj
15+
prj = coproduct prj (const Nothing)
16+
17+
else instance injectRight :: Inject f g => Inject f (Coproduct h g) where
18+
inj = Coproduct <<< Right <<< inj
19+
prj = coproduct (const Nothing) prj
20+
21+
else instance injectReflexive :: Inject f f where
22+
inj = identity
23+
prj = Just

0 commit comments

Comments
 (0)