diff --git a/src/Data/Inject.purs b/src/Data/Inject.purs deleted file mode 100644 index fb044c1..0000000 --- a/src/Data/Inject.purs +++ /dev/null @@ -1,41 +0,0 @@ --- | This module defines a type class `Inject` which is useful --- | when working with coproducts of functors. - -module Data.Inject - ( class Inject - , inj, prj - ) where - -import Prelude (const, (<<<), id) - -import Data.Either (Either(..)) -import Data.Functor.Coproduct (Coproduct(..), coproduct) -import Data.Maybe (Maybe(..)) - --- | The `Inject` class asserts a coproduct relationship between two functors. --- | --- | Specifically, an instance `Inject f g` indicates that `g` is isomorphic to --- | a coproduct of `f` and some third functor. --- | --- | Laws: --- | --- | - `prj g = Just f` if and only if `inj f = g` -class Inject f g where - inj :: forall a. f a -> g a - prj :: forall a. g a -> Maybe (f a) - --- | Any functor is isomorphic to the coproduct of itself with the --- | constantly-`Void` functor. -instance injectReflexive :: Inject f f where - inj = id - prj = Just - --- | Left injection -instance injectLeft :: Inject f (Coproduct f g) where - inj = Coproduct <<< Left - prj = coproduct Just (const Nothing) - --- | Right injection -instance injectRight :: (Inject f g) => Inject f (Coproduct h g) where - inj = Coproduct <<< Right <<< inj - prj = coproduct (const Nothing) prj diff --git a/src/Data/Inject/Coproduct.purs b/src/Data/Inject/Coproduct.purs new file mode 100644 index 0000000..0527bb6 --- /dev/null +++ b/src/Data/Inject/Coproduct.purs @@ -0,0 +1,23 @@ +module Data.Inject.Either where + +import Prelude + +import Data.Either (Either(..)) +import Data.Functor.Coproduct (Coproduct(..), coproduct) +import Data.Maybe (Maybe(..)) + +class Inject f g where + inj :: forall a. f a -> g a + prj :: forall a. g a -> Maybe (f a) + +instance injectLeft :: Inject f g => Inject f (Coproduct g h) where + inj = Coproduct <<< Left <<< inj + prj = coproduct prj (const Nothing) + +else instance injectRight :: Inject f g => Inject f (Coproduct h g) where + inj = Coproduct <<< Right <<< inj + prj = coproduct (const Nothing) prj + +else instance injectReflexive :: Inject f f where + inj = id + prj = Just diff --git a/src/Data/Inject/Either.purs b/src/Data/Inject/Either.purs new file mode 100644 index 0000000..364e6a8 --- /dev/null +++ b/src/Data/Inject/Either.purs @@ -0,0 +1,22 @@ +module Data.Inject.Either where + +import Prelude + +import Data.Either (Either(..), either) +import Data.Maybe (Maybe(..)) + +class Inject a b where + inj :: a -> b + prj :: b -> Maybe a + +instance injectLeft :: Inject a b => Inject a (Either b c) where + inj = Left <<< inj + prj = either prj (const Nothing) + +else instance injectRight :: Inject a b => Inject a (Either c b) where + inj = Right <<< inj + prj = either (const Nothing) prj + +else instance injectReflexive :: Inject a a where + inj = id + prj = Just