File tree Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ module Control.Monad.Free
6
6
, hoistFree
7
7
, injF
8
8
, foldFree
9
+ , substFree
9
10
, runFree
10
11
, runFreeM
11
12
, resume
@@ -90,7 +91,7 @@ suspendF f = fromView (Bind (unsafeCoerceF (pure f)) unsafeCoerceVal)
90
91
-- | Use a natural transformation to change the generating type constructor of a
91
92
-- | free monad.
92
93
hoistFree :: forall f g . (f ~> g ) -> Free f ~> Free g
93
- hoistFree k = foldFree (liftF <<< k)
94
+ hoistFree k = substFree (liftF <<< k)
94
95
95
96
-- | Embed computations in one `Free` monad as computations in the `Free` monad
96
97
-- | for a coproduct type constructor.
@@ -112,6 +113,16 @@ foldFree k = tailRecM go
112
113
Return a -> Right <$> pure a
113
114
Bind g i -> (Left <<< i) <$> k g
114
115
116
+ -- | Like `foldFree`, but for folding into some other Free monad without the
117
+ -- | overhead that `MonadRec` incurs.
118
+ substFree :: forall f g . (f ~> Free g ) -> Free f ~> Free g
119
+ substFree k = go
120
+ where
121
+ go :: Free f ~> Free g
122
+ go f = case toView f of
123
+ Return a -> pure a
124
+ Bind g i -> k g >>= go <$> i
125
+
115
126
-- | Run a free monad with a function that unwraps a single layer of the functor
116
127
-- | `f` at a time.
117
128
runFree :: forall f a . Functor f => (f (Free f a ) -> Free f a ) -> Free f a -> a
You can’t perform that action at this time.
0 commit comments