Skip to content

Avoid ffi #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions MODULE.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
### Types

newtype Yoneda f a where
Yoneda :: forall b. (a -> b) -> f b -> Yoneda f a
Yoneda :: (forall b. (a -> b) -> f b) -> Yoneda f a


### Type Class Instances
Expand Down Expand Up @@ -126,7 +126,7 @@
data Free f a where
Pure :: a -> Free f a
Free :: f (Free f a) -> Free f a
Gosub :: forall s. (forall r. (Unit -> Free f r) -> (r -> Free f a) -> s) -> s -> Free f a
Gosub :: (forall s. (forall r. (Unit -> Free f r) -> (r -> Free f a) -> s) -> s) -> Free f a

type FreeC f a = Free (Coyoneda f) a

Expand Down Expand Up @@ -181,7 +181,7 @@

### Types

type Trampoline = Free Lazy
type Trampoline = Free Lazy


### Values
Expand Down
52 changes: 9 additions & 43 deletions src/Control/Monad/Free.purs
Original file line number Diff line number Diff line change
Expand Up @@ -81,58 +81,24 @@ resumeGosub (Gosub f) = f (\a g ->
Gosub h -> Right (h (\b i -> b unit >>= (\x -> i x >>= g)))
)

isGosub :: forall f a. Free f a -> Boolean
isGosub (Gosub _) = true
isGosub _ = false

unsafeFreeToEither :: forall f a. Free f a -> Either (f (Free f a)) a
unsafeFreeToEither (Pure x) = Right x
unsafeFreeToEither (Free x) = Left x

unsafeLeft :: forall a b. Either a b -> a
unsafeLeft (Left x) = x

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

foreign import resumeImpl
"function resumeImpl(isGosub, isLeft, toEither, fromRight, resumeGosub, value) {\
\ while (true) {\
\ if (!isGosub(value)) return toEither(value);\
\ var x = resumeGosub(value);\
\ if (isLeft(x)) return x;\
\ else value = fromRight(x);\
\ }\
\}" :: forall f a. Fn6
(Free f a -> Boolean)
(Either (f (Free f a)) a -> Boolean)
(Free f a -> Either (f (Free f a)) a)
(Either (f (Free f a)) a -> a)
(Free f a -> Either (f (Free f a)) (Free f a))
(Free f a)
(Either (f (Free f a)) a)

resume :: forall f a. (Functor f) => Free f a -> Either (f (Free f a)) a
resume f = runFn6 resumeImpl isGosub isLeft unsafeFreeToEither unsafeRight resumeGosub f

foreign import goImpl
"function goImpl(resume, isRight, fromLeft, fromRight, fn, value) {\
\ while (true) {\
\ var r = resume(value);\
\ if (isRight(r)) return fromRight(r);\
\ value = fn(fromLeft(r));\
\ }\
\}" :: forall f a. Fn6
(Free f a -> Either (f (Free f a)) a)
(Either (f (Free f a)) a -> Boolean)
(Either (f (Free f a)) a -> (f (Free f a)))
(Either (f (Free f a)) a -> a)
(f (Free f a) -> Free f a)
(Free f a)
a
resume f = case f of
Pure x -> Right x
Free x -> Left x
g -> case resumeGosub g of
Left l -> Left l
Right r -> resume r

go :: forall f a. (Functor f) => (f (Free f a) -> Free f a) -> Free f a -> a
go fn f = runFn6 goImpl resume isRight unsafeLeft unsafeRight fn f
go fn f = case resume f of
Left l -> go fn (fn l)
Right r -> r

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