Skip to content
This repository was archived by the owner on May 24, 2018. It is now read-only.

Commit f35d0cc

Browse files
authored
Merge pull request #2 from purescript/updates
Improve codegen, add "bihoist" operation
2 parents 9698e0a + 9a2b891 commit f35d0cc

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/Data/Functor/Coproduct.purs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Data.Functor.Coproduct where
22

33
import Prelude
44

5+
import Data.Bifunctor (bimap)
56
import Data.Either (Either(..), either)
67
import Data.Foldable (class Foldable, foldMap, foldl, foldr)
78
import Data.Traversable (class Traversable, traverse, sequence)
@@ -15,19 +16,28 @@ unCoproduct (Coproduct x) = x
1516

1617
-- | Left injection
1718
left :: forall f g a. f a -> Coproduct f g a
18-
left = Coproduct <<< Left
19+
left fa = Coproduct (Left fa)
1920

2021
-- | Right injection
2122
right :: forall f g a. g a -> Coproduct f g a
22-
right = Coproduct <<< Right
23+
right ga = Coproduct (Right ga)
2324

2425
-- | Eliminate a coproduct by providing eliminators for the left and
2526
-- | right components
2627
coproduct :: forall f g a b. (f a -> b) -> (g a -> b) -> Coproduct f g a -> b
27-
coproduct f g = either f g <<< unCoproduct
28+
coproduct f g (Coproduct e) = either f g e
29+
30+
-- | Change the underlying functors in a coproduct
31+
bihoistCoproduct
32+
:: forall f g h i
33+
. (f ~> h)
34+
-> (g ~> i)
35+
-> Coproduct f g
36+
~> Coproduct h i
37+
bihoistCoproduct natF natG (Coproduct e) = Coproduct (bimap natF natG e)
2838

2939
instance functorCoproduct :: (Functor f, Functor g) => Functor (Coproduct f g) where
30-
map f = Coproduct <<< coproduct (Left <<< (<$>) f) (Right <<< (<$>) f)
40+
map f (Coproduct e) = Coproduct (bimap (map f) (map f) e)
3141

3242
instance foldableCoproduct :: (Foldable f, Foldable g) => Foldable (Coproduct f g) where
3343
foldr f z = coproduct (foldr f z) (foldr f z)
@@ -36,8 +46,8 @@ instance foldableCoproduct :: (Foldable f, Foldable g) => Foldable (Coproduct f
3646

3747
instance traversableCoproduct :: (Traversable f, Traversable g) => Traversable (Coproduct f g) where
3848
traverse f = coproduct
39-
((<$>) (Coproduct <<< Left) <<< traverse f)
40-
((<$>) (Coproduct <<< Right) <<< traverse f)
49+
(map (Coproduct <<< Left) <<< traverse f)
50+
(map (Coproduct <<< Right) <<< traverse f)
4151
sequence = coproduct
42-
((<$>) (Coproduct <<< Left) <<< sequence)
43-
((<$>) (Coproduct <<< Right) <<< sequence)
52+
(map (Coproduct <<< Left) <<< sequence)
53+
(map (Coproduct <<< Right) <<< sequence)

0 commit comments

Comments
 (0)