Skip to content

Commit ab72047

Browse files
committed
Merge pull request #16 from purescript-contrib/avoid-ffi
Avoid ffi
2 parents 2af0f48 + f2cbbe5 commit ab72047

File tree

2 files changed

+12
-46
lines changed

2 files changed

+12
-46
lines changed

MODULE.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
### Types
5151

5252
newtype Yoneda f a where
53-
Yoneda :: forall b. (a -> b) -> f b -> Yoneda f a
53+
Yoneda :: (forall b. (a -> b) -> f b) -> Yoneda f a
5454

5555

5656
### Type Class Instances
@@ -126,7 +126,7 @@
126126
data Free f a where
127127
Pure :: a -> Free f a
128128
Free :: f (Free f a) -> Free f a
129-
Gosub :: forall s. (forall r. (Unit -> Free f r) -> (r -> Free f a) -> s) -> s -> Free f a
129+
Gosub :: (forall s. (forall r. (Unit -> Free f r) -> (r -> Free f a) -> s) -> s) -> Free f a
130130

131131
type FreeC f a = Free (Coyoneda f) a
132132

@@ -181,7 +181,7 @@
181181

182182
### Types
183183

184-
type Trampoline = Free Lazy
184+
type Trampoline = Free Lazy
185185

186186

187187
### Values

src/Control/Monad/Free.purs

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

84-
isGosub :: forall f a. Free f a -> Boolean
85-
isGosub (Gosub _) = true
86-
isGosub _ = false
87-
88-
unsafeFreeToEither :: forall f a. Free f a -> Either (f (Free f a)) a
89-
unsafeFreeToEither (Pure x) = Right x
90-
unsafeFreeToEither (Free x) = Left x
91-
9284
unsafeLeft :: forall a b. Either a b -> a
9385
unsafeLeft (Left x) = x
9486

9587
unsafeRight :: forall a b. Either a b -> b
9688
unsafeRight (Right x) = x
9789

98-
foreign import resumeImpl
99-
"function resumeImpl(isGosub, isLeft, toEither, fromRight, resumeGosub, value) {\
100-
\ while (true) {\
101-
\ if (!isGosub(value)) return toEither(value);\
102-
\ var x = resumeGosub(value);\
103-
\ if (isLeft(x)) return x;\
104-
\ else value = fromRight(x);\
105-
\ }\
106-
\}" :: forall f a. Fn6
107-
(Free f a -> Boolean)
108-
(Either (f (Free f a)) a -> Boolean)
109-
(Free f a -> Either (f (Free f a)) a)
110-
(Either (f (Free f a)) a -> a)
111-
(Free f a -> Either (f (Free f a)) (Free f a))
112-
(Free f a)
113-
(Either (f (Free f a)) a)
114-
11590
resume :: forall f a. (Functor f) => Free f a -> Either (f (Free f a)) a
116-
resume f = runFn6 resumeImpl isGosub isLeft unsafeFreeToEither unsafeRight resumeGosub f
117-
118-
foreign import goImpl
119-
"function goImpl(resume, isRight, fromLeft, fromRight, fn, value) {\
120-
\ while (true) {\
121-
\ var r = resume(value);\
122-
\ if (isRight(r)) return fromRight(r);\
123-
\ value = fn(fromLeft(r));\
124-
\ }\
125-
\}" :: forall f a. Fn6
126-
(Free f a -> Either (f (Free f a)) a)
127-
(Either (f (Free f a)) a -> Boolean)
128-
(Either (f (Free f a)) a -> (f (Free f a)))
129-
(Either (f (Free f a)) a -> a)
130-
(f (Free f a) -> Free f a)
131-
(Free f a)
132-
a
91+
resume f = case f of
92+
Pure x -> Right x
93+
Free x -> Left x
94+
g -> case resumeGosub g of
95+
Left l -> Left l
96+
Right r -> resume r
13397

13498
go :: forall f a. (Functor f) => (f (Free f a) -> Free f a) -> Free f a -> a
135-
go fn f = runFn6 goImpl resume isRight unsafeLeft unsafeRight fn f
99+
go fn f = case resume f of
100+
Left l -> go fn (fn l)
101+
Right r -> r
136102

137103
foreign import goEffImpl
138104
"function goEffImpl(resume, isRight, fromLeft, fromRight, fn, value) {\

0 commit comments

Comments
 (0)