Free Monads in Julia Programming

83 views Asked by At

I'm learning about Free Monads and I was wondering how one could implement it in the Julia programming language. I've seen the implementation in Haskell, but I'm having a hard time porting it to Julia.

Here is the idea. A free monoid is a list. It's definition in Haskell is:

data List a = Nil | Cons a (List a)

We can code this in Julia using the following:

struct Nil end
struct Cons{T}
  val::T
  next::Union{Cons{T},Nil} # n.b. abstract type!
end
List{T} = Union{Cons{T}, Nil}

Another variation of this in Julia would be:

abstract type List{T} end
struct Nil{T} <: List{T} end
struct Cons{T} <: List{T}
  val::T
  next::List{T} # n.b. abstract type!
end

Now, a Free monad is similar to a list, but it instead defines a "list" in the functor category. Here is the Haskell definition:

data Free f a = Pure a | Free (f (Free f a))

I'm having trouble with such recursive definition. In Julia we cannot simply define such thing.

0

There are 0 answers