File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
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 {})
You can’t perform that action at this time.
0 commit comments