@@ -81,58 +81,24 @@ resumeGosub (Gosub f) = f (\a g ->
81
81
Gosub h -> Right (h (\b i -> b unit >>= (\x -> i x >>= g)))
82
82
)
83
83
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
-
92
84
unsafeLeft :: forall a b . Either a b -> a
93
85
unsafeLeft (Left x) = x
94
86
95
87
unsafeRight :: forall a b . Either a b -> b
96
88
unsafeRight (Right x) = x
97
89
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
-
115
90
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
133
97
134
98
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
136
102
137
103
foreign import goEffImpl
138
104
" function goEffImpl(resume, isRight, fromLeft, fromRight, fn, value) {\
0 commit comments