Skip to content

Commit c8b4780

Browse files
committed
Add Control.Monad.Trampoline
1 parent e79902e commit c8b4780

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/Control/Monad/Trampoline.purs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Control.Monad.Trampoline where
2+
3+
import Control.Monad.Free
4+
5+
data Delay a = Delay ({} -> a)
6+
7+
instance delayFunctor :: Functor Delay where
8+
(<$>) f (Delay g) = Delay (const (f (g {})))
9+
10+
instance delayApply :: Apply Delay where
11+
(<*>) (Delay f) (Delay a) = Delay (\{} -> (f {}) (a {}))
12+
13+
instance delayApplicative :: Applicative Delay where
14+
pure a = Delay (\{} -> a)
15+
16+
type Trampoline a = Free Delay a
17+
18+
done :: forall a. a -> Trampoline a
19+
done = Pure
20+
21+
suspend :: forall a. Trampoline a -> Trampoline a
22+
suspend a = Free (Delay (\{} -> a))
23+
24+
delay :: forall a. ({} -> a) -> Trampoline a
25+
delay a = Free (done <$> Delay a)
26+
27+
runTrampoline :: forall a. Trampoline a -> a
28+
runTrampoline = go (\(Delay f) -> f {})

0 commit comments

Comments
 (0)