Skip to content

Commit 08112da

Browse files
committed
Implement goEff directly
1 parent ef06b4d commit 08112da

File tree

1 file changed

+3
-25
lines changed

1 file changed

+3
-25
lines changed

src/Control/Monad/Free.purs

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ resumeGosub (Gosub f) = f (\a g ->
8181
Gosub h -> Right (h (\b i -> b unit >>= (\x -> i x >>= g)))
8282
)
8383

84-
unsafeLeft :: forall a b. Either a b -> a
85-
unsafeLeft (Left x) = x
86-
87-
unsafeRight :: forall a b. Either a b -> b
88-
unsafeRight (Right x) = x
89-
9084
resume :: forall f a. (Functor f) => Free f a -> Either (f (Free f a)) a
9185
resume f = case f of
9286
Pure x -> Right x
@@ -100,26 +94,10 @@ go fn f = case resume f of
10094
Left l -> go fn (fn l)
10195
Right r -> r
10296

103-
foreign import goEffImpl
104-
"function goEffImpl(resume, isRight, fromLeft, fromRight, fn, value) {\
105-
\ return function(){\
106-
\ while (true) {\
107-
\ var r = resume(value);\
108-
\ if (isRight(r)) return fromRight(r);\
109-
\ value = fn(fromLeft(r))();\
110-
\ }\
111-
\ };\
112-
\}" :: forall e f a. Fn6
113-
(Free f a -> Either (f (Free f a)) a)
114-
(Either (f (Free f a)) a -> Boolean)
115-
(Either (f (Free f a)) a -> (f (Free f a)))
116-
(Either (f (Free f a)) a -> a)
117-
(f (Free f a) -> Eff e (Free f a))
118-
(Free f a)
119-
(Eff e a)
120-
12197
goEff :: forall e f a. (Functor f) => (f (Free f a) -> Eff e (Free f a)) -> Free f a -> Eff e a
122-
goEff fn f = runFn6 goEffImpl resume isRight unsafeLeft unsafeRight fn f
98+
goEff fn f = case resume f of
99+
Left l -> fn l >>= goEff fn
100+
Right r -> return r
123101

124102
-- Note: can blow the stack!
125103
goMC :: forall f m a. (Monad m) => Natural f m -> FreeC f a -> m a

0 commit comments

Comments
 (0)