Stream: helpdesk (published)

Topic: Free Monads in Julia


view this post on Zulip Davi Sales Barreira (Aug 08 2023 at 11:52):

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.

view this post on Zulip Alec (Aug 08 2023 at 19:33):

Maybe this might help, or at least the rest of the page? https://github.com/Moelf/functional-programming-jargon.jl#monad

view this post on Zulip Alec (Aug 08 2023 at 19:33):

I’m a beginner at best at this sort of thing so apologies if not helpful

view this post on Zulip Davi Sales Barreira (Aug 08 2023 at 23:04):

Thanks @Alec . Ive seen this, but not really helpful in this situation :(

view this post on Zulip Sukera (Aug 09 2023 at 12:54):

implementing a functor in Julia is implementing map(f, x::T) -> T for your T, if I'm not mistaken

view this post on Zulip Sukera (Aug 09 2023 at 12:54):

since "functor" in haskell just means "mappable"

view this post on Zulip Sukera (Aug 09 2023 at 12:55):

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

view this post on Zulip Sukera (Aug 09 2023 at 12:56):

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

view this post on Zulip Sukera (Aug 09 2023 at 12:56):

I think the closest analog we have is collect after such a map over a functor

view this post on Zulip Sukera (Aug 09 2023 at 12:57):

(it's a bit confusing that we call callables functors in julia...)

view this post on Zulip Davi Sales Barreira (Aug 09 2023 at 13:40):

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.

view this post on Zulip Davi Sales Barreira (Aug 09 2023 at 13:41):

I call parametric struct functors when they have an fmap function. I think this is how they do in Functors.jl

view this post on Zulip Sukera (Aug 09 2023 at 18:54):

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)

view this post on Zulip Sukera (Aug 09 2023 at 18:56):

ah, Functors.jl supports that too

view this post on Zulip Sukera (Aug 09 2023 at 18:56):

yeah, it's unrelated to being parametrized or not :)

view this post on Zulip Sundar R (Aug 10 2023 at 20:55):

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

view this post on Zulip Davi Sales Barreira (Aug 11 2023 at 10:44):

Thanks, a lot @Sundar R !

view this post on Zulip aplavin (Aug 12 2023 at 09:34):

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