Skip to content

Commit

Permalink
Merge pull request #19 from purescript/inj-reflexive
Browse files Browse the repository at this point in the history
Reorder Inject instances to allow inj :: f ~> f
  • Loading branch information
garyb authored Nov 30, 2018
2 parents f5665c4 + 772bc67 commit 96bcf8a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Data/Functor/Coproduct/Inject.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class Inject f g where
inj :: forall a. f a -> g a
prj :: forall a. g a -> Maybe (f a)

instance injectLeft :: Inject f (Coproduct f g) where
instance injectReflexive :: Inject f f where
inj = identity
prj = Just

else instance injectLeft :: Inject f (Coproduct f g) where
inj = Coproduct <<< Left
prj = coproduct Just (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 = identity
prj = Just
23 changes: 23 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ module Test.Main where

import Prelude

import Data.Const (Const)
import Data.Functor.Compose (Compose(..))
import Data.Functor.Coproduct (Coproduct, left, right)
import Data.Functor.Coproduct.Inject (inj)
import Data.Identity (Identity(..))
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Test.Assert (assertEqual)

main :: Effect Unit
main = do
testComposeOrdering
testInjections

testComposeOrdering :: Effect Unit
testComposeOrdering = do
assertEqual
{ expected: Compose (Identity (Just true))
, actual: Compose (Identity (Just true))
Expand All @@ -18,3 +26,18 @@ main = do
{ expected: Compose (Identity (Just true)) > Compose (Identity (Just false))
, actual: true
}

testInjections :: Effect Unit
testInjections = do
assertEqual
{ expected: Identity unit
, actual: inj (Identity unit)
}
assertEqual
{ expected: left (Identity unit) :: Coproduct Identity (Const Void) Unit
, actual: inj (Identity unit)
}
assertEqual
{ expected: right (Identity unit) :: Coproduct (Const Void) Identity Unit
, actual: inj (Identity unit)
}

0 comments on commit 96bcf8a

Please sign in to comment.