Skip to content

Commit 3100ed7

Browse files
committed
Merge pull request #8 from purescript/alternative
Alternative & MonadPlus
2 parents 1c28012 + 849032c commit 3100ed7

File tree

6 files changed

+121
-1
lines changed

6 files changed

+121
-1
lines changed

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
# Module Documentation
22

3+
## Module Control.Alt
4+
5+
### Type Classes
6+
7+
class (Functor f) <= Alt f where
8+
(<|>) :: forall a. f a -> f a -> f a
9+
10+
11+
## Module Control.Alternative
12+
13+
### Type Classes
14+
15+
class (Applicative f, Plus f) <= Alternative f where
16+
17+
18+
### Values
19+
20+
many :: forall f a. (Alternative f, Lazy1 f) => f a -> f [a]
21+
22+
some :: forall f a. (Alternative f, Lazy1 f) => f a -> f [a]
23+
24+
325
## Module Control.Apply
426

527
### Values
@@ -34,6 +56,29 @@
3456
join :: forall a m. (Bind m) => m (m a) -> m a
3557

3658

59+
## Module Control.Lazy
60+
61+
### Type Classes
62+
63+
class Lazy l where
64+
defer :: (Unit -> l) -> l
65+
66+
class Lazy1 l where
67+
defer1 :: forall a. (Unit -> l a) -> l a
68+
69+
class Lazy2 l where
70+
defer2 :: forall a b. (Unit -> l a b) -> l a b
71+
72+
73+
### Values
74+
75+
fix :: forall l a. (Lazy l) => (l -> l) -> l
76+
77+
fix1 :: forall l a. (Lazy1 l) => (l a -> l a) -> l a
78+
79+
fix2 :: forall l a b. (Lazy2 l) => (l a b -> l a b) -> l a b
80+
81+
3782
## Module Control.Monad
3883

3984
### Values
@@ -44,4 +89,24 @@
4489

4590
unless :: forall m. (Monad m) => Boolean -> m Unit -> m Unit
4691

47-
when :: forall m. (Monad m) => Boolean -> m Unit -> m Unit
92+
when :: forall m. (Monad m) => Boolean -> m Unit -> m Unit
93+
94+
95+
## Module Control.MonadPlus
96+
97+
### Type Classes
98+
99+
class (Monad m, Alternative m) <= MonadPlus m where
100+
101+
102+
### Values
103+
104+
guard :: forall m. (MonadPlus m) => Boolean -> m Unit
105+
106+
107+
## Module Control.Plus
108+
109+
### Type Classes
110+
111+
class (Alt f) <= Plus f where
112+
empty :: forall a. f a

src/Control/Alt.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Control.Alt where
2+
3+
infixl 3 <|>
4+
5+
class (Functor f) <= Alt f where
6+
(<|>) :: forall a. f a -> f a -> f a

src/Control/Alternative.purs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Control.Alternative where
2+
3+
import Control.Alt
4+
import Control.Lazy
5+
import Control.Plus
6+
7+
class (Applicative f, Plus f) <= Alternative f
8+
9+
some :: forall f a. (Alternative f, Lazy1 f) => f a -> f [a]
10+
some v = (:) <$> v <*> defer1 (\_ -> many v)
11+
12+
many :: forall f a. (Alternative f, Lazy1 f) => f a -> f [a]
13+
many v = some v <|> pure []
14+

src/Control/Lazy.purs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Control.Lazy where
2+
3+
class Lazy l where
4+
defer :: (Unit -> l) -> l
5+
6+
class Lazy1 l where
7+
defer1 :: forall a. (Unit -> l a) -> l a
8+
9+
class Lazy2 l where
10+
defer2 :: forall a b. (Unit -> l a b) -> l a b
11+
12+
fix :: forall l a. (Lazy l) => (l -> l) -> l
13+
fix f = defer (\_ -> f (fix f))
14+
15+
fix1 :: forall l a. (Lazy1 l) => (l a -> l a) -> l a
16+
fix1 f = defer1 (\_ -> f (fix1 f))
17+
18+
fix2 :: forall l a b. (Lazy2 l) => (l a b -> l a b) -> l a b
19+
fix2 f = defer2 (\_ -> f (fix2 f))

src/Control/MonadPlus.purs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Control.MonadPlus where
2+
3+
import Control.Alternative
4+
import Control.Plus
5+
6+
class (Monad m, Alternative m) <= MonadPlus m
7+
8+
guard :: forall m. (MonadPlus m) => Boolean -> m Unit
9+
guard true = return unit
10+
guard false = empty

src/Control/Plus.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Control.Plus where
2+
3+
import Control.Alt
4+
5+
class (Alt f) <= Plus f where
6+
empty :: forall a. f a

0 commit comments

Comments
 (0)