Skip to content

Reorder Inject instances to allow inj :: a -> a #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Data/Either/Inject.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ class Inject a b where
inj :: a -> b
prj :: b -> Maybe a

instance injectLeft :: Inject a (Either a b) where
instance injectReflexive :: Inject a a where
inj = identity
prj = Just

else instance injectLeft :: Inject a (Either a b) where
inj = Left
prj = either Just (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 = identity
prj = Just
13 changes: 13 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ main = do
{ actual: inj 100 :: MySum
, expected: in3 100
}
log "Test injection with the injectReflexive instance"
assertEqual
let
x = inj 100 :: MySum
in
{ actual: inj x :: MySum
, expected: x
}
log "Test that injection picks the left-most option"
assertEqual
{ actual: inj 100 :: Either3 Int Int Int
, expected: in1 100
}
log "Test projection"
assertEqual
{ actual: prj (in1 true :: MySum)
Expand Down