Skip to content

Commit c168ced

Browse files
committed
Added Apply and Bind.
1 parent bfacf3f commit c168ced

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/Control/Apply.purs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module Control.Apply where
2+
3+
-- This should currently replace everything in Control.Applicative.
4+
-- Ideally it would be its superclass.
5+
6+
infixl 4 <.
7+
infixl 4 .>
8+
9+
-- This should be a subclass of Functor.
10+
class Apply f where
11+
-- Until we get default implementations, use `<.>` as `<*>`
12+
(<.>) :: forall a b. (Functor f) => f (a -> b) -> f a -> f b
13+
14+
(<.) :: forall a b f. (Apply f, Functor f) => f a -> f b -> f a
15+
(<.) a b = const <$> a <.> b
16+
17+
(.>) :: forall a b f. (Apply f, Functor f) => f a -> f b -> f b
18+
(.>) a b = const id <$> a <.> b
19+
20+
lift2 :: forall a b c f. (Apply f, Functor f) => (a -> b -> c) -> f a -> f b -> f c
21+
lift2 f a b = f <$> a <.> b
22+
23+
lift3 :: forall a b c d f. (Apply f, Functor f) => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
24+
lift3 f a b c = f <$> a <.> b <.> c

src/Control/Bind.purs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module Control.Bind where
2+
3+
-- This should replace most things in Control.Monad.
4+
-- Ideally it would be its superclass.
5+
6+
infixl 1 >>-
7+
8+
import Control.Apply
9+
10+
-- This should be a subclass of Apply
11+
class Bind m where
12+
-- We should be able to provide either one of these and derive the other.
13+
(>>-) :: forall a b. (Apply m) => m a -> (a -> m b) -> m b
14+
-- (>>-) m f = join (f <$> m)
15+
join :: forall a. (Apply m) => m (m a) -> m a
16+
-- join m = m >>- id
17+
18+
instance applyFromBind :: (Bind m) => Apply m where
19+
(<.>) f a = f >>- \f' -> f' <$> a
20+
21+
(-<<) :: forall a b m. (Apply m, Bind m) => (a -> m b) -> m a -> m b
22+
(-<<) f m = m >>- f
23+
24+
(>->) :: forall a b c m. (Apply m, Bind m) => (a -> m b) -> (b -> m c) -> a -> m c
25+
(>->) f g a = f a >>- g
26+
27+
(<-<) :: forall a b c m. (Apply m, Bind m) => (b -> m c) -> (a -> m b) -> a -> m c
28+
(<-<) f g a = f -<< g a
29+
30+
ifM :: forall a m. (Apply m, Bind m) => m Boolean -> m a -> m a -> m a
31+
ifM cond t f = cond >>- \cond' -> if cond' then t else f
32+
33+
forever :: forall a b m. (Apply m, Functor m, Bind m) => m a -> m b
34+
forever a = a .> forever a

0 commit comments

Comments
 (0)