<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>I came up with this utility function so I can access some info (`n`) from the parent's level:</div><div><br></div><div>hoistWithUpper<br>    :: forall f g s t n<br>     . (Functor g)<br>    => (forall a. f a -> n)<br>    -> n<br>    -> (forall a. n -> f a -> g a)<br>    -> (n -> s -> t)<br>    -> Free f s<br>    -> Free g t<br>hoistWithUpper fu n0 hoistFr hoistPure = go n0<br>  where<br>    go :: n -> Free f s -> Free g t<br>    go n fr = case fr of<br>        Pure s -> Pure (hoistPure n s)<br>        Free f -> let n2 = fu f<br>                  in Free (go n2 <$> (hoistFr n f :: g (Free f s)))</div><div><br></div><div>I wonder if there's already a generalized form of this in recursion-schemes? Admittedly I'm fine with my helper so don't loose nights on this, but a little type golfing never hurts.<br></div><div><br></div><div>There's a similar function `inherit` [1] in fixplate, but that operates on Fix (Mu there), not Free. With Free I guess the complication is managing the different way of maintaining annotation at the Free and Pure ctors.</div><div><br></div><div>Practically I pass in</div><div><br></div><div>   (\n f -> ConstProd (Pair (Const n) f))  -- for hoistFr</div><div>   (\n u -> (n,u))  -- for hoistPure.<br></div><div><br></div><div>where</div><div><br></div><div>    newtype ConstProd c f a = ConstProd (Product (Const c) f a)<br></div><div><br></div><div>Thanks!</div><div>Robin<br></div><div><br></div><div>[1]: <a href="http://hackage.haskell.org/package/fixplate-0.1.7/docs/src/Data-Generics-Fixplate-Attributes.html#inherit">http://hackage.haskell.org/package/fixplate-0.1.7/docs/src/Data-Generics-Fixplate-Attributes.html#inherit</a><br></div></div></div></div></div></div></div>