1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import Control.Applicative
data Kleene c d a
= Done a
| More (Kleene c d (d -> a)) c
instance Functor (Kleene c d) where
fmap f (Done a) = Done (f a)
fmap f (More v b) = More (fmap (f .) v) b
instance Applicative (Kleene c d) where
pure = Done
Done f <*> m = fmap f m
More z a <*> m = More (flip <$> z <*> m) a
unkleene :: Kleene c c a -> a
unkleene (Done a) = a
unkleene (More z a) = unkleene z a
dup :: Kleene c e a -> Kleene c d (Kleene d e a)
dup (Done b) = Done (Done b)
dup (More z a) = More (More <$> dup z) a |