Skip to content

Commit d5d398a

Browse files
committed
Merge pull request #1 from paf31/exists
Use purescript-exists
2 parents 98122cc + 96a4cf1 commit d5d398a

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
### Types
66

77
newtype Coyoneda f a where
8-
Coyoneda :: forall i. { fi :: f i, k :: i -> a } -> Coyoneda f a
8+
Coyoneda :: Exists (CoyonedaF f a) -> Coyoneda f a
99

1010
type Natural f g = forall a. f a -> g a
1111

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"dist"
1616
],
1717
"dependencies": {
18+
"purescript-exists": "*",
1819
"purescript-control": "0.2.1",
1920
"purescript-transformers": "0.1.1"
2021
}

src/Data/Coyoneda.purs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ module Data.Coyoneda
88
, liftCoyonedaTF
99
) where
1010

11+
import Data.Exists
12+
1113
import Control.Comonad
1214
import Control.Extend
1315
import Control.Monad.Trans
1416

15-
newtype Coyoneda f a = Coyoneda (forall i. { k :: i -> a , fi :: f i })
17+
newtype CoyonedaF f a i = CoyonedaF { k :: i -> a, fi :: f i }
18+
19+
newtype Coyoneda f a = Coyoneda (Exists (CoyonedaF f a))
1620

1721
type Natural f g = forall a. f a -> g a
1822

1923
instance functorCoyoneda :: Functor (Coyoneda f) where
20-
(<$>) f (Coyoneda v) = Coyoneda v { k = f <<< v.k }
24+
(<$>) f (Coyoneda e) = runExists (\(CoyonedaF v) -> coyoneda (f <<< v.k) v.fi) e
2125

2226
instance applyCoyoneda :: (Apply f) => Apply (Coyoneda f) where
2327
(<*>) f g = liftCoyoneda $ lowerCoyoneda f <*> lowerCoyoneda g
@@ -26,32 +30,30 @@ instance applicativeCoyoneda :: (Applicative f) => Applicative (Coyoneda f) wher
2630
pure = liftCoyoneda <<< pure
2731

2832
instance bindCoyoneda :: (Bind f) => Bind (Coyoneda f) where
29-
(>>=) (Coyoneda v) k = liftCoyoneda $ v.fi >>= lowerCoyoneda <<< k <<< v.k
33+
(>>=) (Coyoneda e) k = liftCoyoneda $ runExists (\(CoyonedaF v) -> v.fi >>= lowerCoyoneda <<< k <<< v.k) e
3034

3135
instance monadCoyoneda :: (Monad f) => Monad (Coyoneda f)
3236

3337
instance monadTransCoyoneda :: MonadTrans Coyoneda where
3438
lift = liftCoyoneda
3539

3640
instance extendCoyoneda :: (Extend w) => Extend (Coyoneda w) where
37-
(<<=) f (Coyoneda w) = liftCoyoneda $ f <<< coyoneda w.k <<= w.fi
41+
(<<=) f (Coyoneda e) = runExists (\(CoyonedaF w) -> liftCoyoneda $ f <<< coyoneda w.k <<= w.fi) e
3842

3943
instance comonadCoyoneda :: (Comonad w) => Comonad (Coyoneda w) where
40-
extract (Coyoneda w) = w.k $ extract w.fi
44+
extract (Coyoneda e) = runExists (\(CoyonedaF w) -> w.k $ extract w.fi) e
4145

4246
coyoneda :: forall f a b. (a -> b) -> f a -> Coyoneda f b
43-
coyoneda k fi = k <$> liftCoyoneda fi
47+
coyoneda k fi = Coyoneda $ mkExists $ CoyonedaF { k: k, fi: fi }
4448

45-
--liftCoyoneda :: forall f a. f a -> Coyoneda f a
46-
--liftCoyoneda fa = Coyoneda { k: id , fi: fa }
47-
foreign import liftCoyoneda "function liftCoyoneda(fa){return {k: function(a){return a;}, fi: fa};}"
48-
:: forall f a. f a -> Coyoneda f a
49+
liftCoyoneda :: forall f a. f a -> Coyoneda f a
50+
liftCoyoneda fa = Coyoneda $ mkExists $ CoyonedaF { k: id, fi: fa }
4951

5052
lowerCoyoneda :: forall f a. (Functor f) => Coyoneda f a -> f a
51-
lowerCoyoneda (Coyoneda v) = v.k <$> v.fi
53+
lowerCoyoneda (Coyoneda e) = runExists (\(CoyonedaF v) -> v.k <$> v.fi) e
5254

5355
liftCoyonedaT :: forall f g. Natural f g -> Natural (Coyoneda f) (Coyoneda g)
54-
liftCoyonedaT nat = \(Coyoneda v) -> Coyoneda v { fi = nat v.fi }
56+
liftCoyonedaT nat (Coyoneda e) = runExists (\(CoyonedaF v) -> coyoneda v.k (nat v.fi)) e
5557

5658
liftCoyonedaTF :: forall f g. (Functor g) => Natural f g -> Natural (Coyoneda f) g
5759
liftCoyonedaTF nat = lowerCoyoneda <<< liftCoyonedaT nat

0 commit comments

Comments
 (0)