r/purescript • u/paulvictor • Jan 04 '18
Change the structure of a free functor in Free
Hi all,
I want to implement something of this sort, which changes the structure of the functor in Free f a. Here is a simplified view of what I am trying to do
data F a = F1 a | F2 String a | F3
type M = Free F
append :: forall a. M a -> String -> M a
append ma s = -- {If the constructor of the underlying structure of ma is F2, append s to the first argument, else return ma as it is.}
Can anyone help me with this? The data constructors Free and Pure are also not exposed as is done in the Haskell library.
Thanks Paul
3
Upvotes
1
u/sloosch Jan 04 '18
What you are probably looking for is hoistFree https://github.com/purescript/purescript-free/blob/master/src/Control/Monad/Free.purs#L136 with f and g equal to F you will be able to append s to F2
1
u/natefaubion Jan 04 '18
If you only want to peek at the head, then you can use
resumewhich is equivalent to exposingFreeandPureas constructors.resumepeels off a layer using the underlyingFunctor.If you want to interpret up until you hit your first
F2, then you could write a loop that peels of the head, checks it, and then rebuilds the tree (lifts the F back into Free) until you find theF2.