Skip to content

Commit 8b4b38a

Browse files
authored
Merge pull request #44 from purescript/inj-reflexive
Reorder Inject instances to allow inj :: a -> a
2 parents 1a2d94e + 23518f6 commit 8b4b38a

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/Data/Either/Inject.purs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ class Inject a b where
99
inj :: a -> b
1010
prj :: b -> Maybe a
1111

12-
instance injectLeft :: Inject a (Either a b) where
12+
instance injectReflexive :: Inject a a where
13+
inj = identity
14+
prj = Just
15+
16+
else instance injectLeft :: Inject a (Either a b) where
1317
inj = Left
1418
prj = either Just (const Nothing)
1519

1620
else instance injectRight :: Inject a b => Inject a (Either c b) where
1721
inj = Right <<< inj
1822
prj = either (const Nothing) prj
1923

20-
else instance injectReflexive :: Inject a a where
21-
inj = identity
22-
prj = Just

test/Main.purs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ main = do
2626
{ actual: inj 100 :: MySum
2727
, expected: in3 100
2828
}
29+
log "Test injection with the injectReflexive instance"
30+
assertEqual
31+
let
32+
x = inj 100 :: MySum
33+
in
34+
{ actual: inj x :: MySum
35+
, expected: x
36+
}
37+
log "Test that injection picks the left-most option"
38+
assertEqual
39+
{ actual: inj 100 :: Either3 Int Int Int
40+
, expected: in1 100
41+
}
2942
log "Test projection"
3043
assertEqual
3144
{ actual: prj (in1 true :: MySum)

0 commit comments

Comments
 (0)