Hello, friends. I have recently learned of something called a Free Monad, which is similar to a list, but over functors. I was wondering how this could be implemented in Julia. In Haskell, a free monad is defined as
data Free f a = Pure a | Free (f (Free f a))
This Free f
takes a functor f
and creates a new functor (e.g. Free_f
) which has a monadic structure.
I'm having a hard time figuring out how to implement this in Julia, specifically due to this recrusive nature of the definition.
I was wondering if anyone with expirience in functional programming could help.
Maybe this might help, or at least the rest of the page? https://github.com/Moelf/functional-programming-jargon.jl#monad
I’m a beginner at best at this sort of thing so apologies if not helpful
Thanks @Alec . Ive seen this, but not really helpful in this situation :(
implementing a functor in Julia is implementing map(f, x::T) -> T
for your T
, if I'm not mistaken
since "functor" in haskell just means "mappable"
the important thing is that structure is preserved; if your T
is a tree, you'll get a tree with the same structure back out
what Free
then does is destroy that structure - instead of a tree, you'd get a flat list back out; hence why it's sometimes called flatmap
I think the closest analog we have is collect
after such a map
over a functor
(it's a bit confusing that we call callables functors in julia...)
I think I was able to implement a working example of an instance of FreeF
. The actual Free
functor that turns functors F
into FreeF
can perhaps be done via macros.
I call parametric struct functors when they have an fmap
function. I think this is how they do in Functors.jl
Yes, that's pretty much the same thing, except applied to a whole struct (which is not necessary in general; you can have "mappable structure" without mapping over every field of an object)
ah, Functors.jl supports that too
yeah, it's unrelated to being parametrized or not :)
I'm a complete novice here, but I came across Effects.jl on my github feed recently by coincidence. Its readme says:
An implementation of effect handlers in Julia (aka algebraic effects, free monads etc).
I know some of those words :smile: but no idea if this is helpful either
Thanks, a lot @Sundar R !
See also optics, as implemented in Accessors.jl: they allow both traversing and modifying a structure (flat or nested), or extracting all/specified elements as a flat list, or putting such a list of elements back into the original structure.
Last updated: Nov 22 2024 at 04:41 UTC