@@ -8,6 +8,7 @@ module Control.Monad.Free
8
8
, foldFree
9
9
, runFree
10
10
, runFreeM
11
+ , resume
11
12
) where
12
13
13
14
import Prelude
@@ -17,7 +18,6 @@ import Control.Monad.Trans (class MonadTrans)
17
18
18
19
import Data.CatList (CatList , empty , snoc , uncons )
19
20
import Data.Either (Either (..), either )
20
- import Data.Identity (Identity (..), runIdentity )
21
21
import Data.Inject (class Inject , inj )
22
22
import Data.Maybe (Maybe (..))
23
23
import Data.Tuple (Tuple (..))
@@ -115,7 +115,12 @@ foldFree k = tailRecM go
115
115
-- | Run a free monad with a function that unwraps a single layer of the functor
116
116
-- | `f` at a time.
117
117
runFree :: forall f a . Functor f => (f (Free f a ) -> Free f a ) -> Free f a -> a
118
- runFree k = runIdentity <<< runFreeM (Identity <<< k)
118
+ runFree k = go
119
+ where
120
+ go :: Free f a -> a
121
+ go f = case toView f of
122
+ Return a -> a
123
+ Bind g i -> go (k (i <$> g))
119
124
120
125
-- | Run a free monad with a function mapping a functor `f` to a tail-recursive
121
126
-- | monad `m`. See the `MonadRec` type class for more details.
@@ -132,6 +137,16 @@ runFreeM k = tailRecM go
132
137
Return a -> Right <$> pure a
133
138
Bind g i -> Left <$> k (i <$> g)
134
139
140
+ -- | Unwraps a single layer of the functor `f`.
141
+ resume
142
+ :: forall f a
143
+ . Functor f
144
+ => Free f a
145
+ -> Either (f (Free f a )) a
146
+ resume f = case toView f of
147
+ Return a -> Right a
148
+ Bind g i -> Left (i <$> g)
149
+
135
150
fromView :: forall f a . FreeView f a Val -> Free f a
136
151
fromView f = Free (unsafeCoerceFreeView f) empty
137
152
where
0 commit comments