-
Notifications
You must be signed in to change notification settings - Fork 6
Using Traits.jl to define Monads #3
Comments
I have tried a few time but never understood monads... Anyway, there might be progress on accessing type parameters coming: Note that at the moment you can get at the type parameters using associated types: tpar1(t::Type) = t.parameters[1]
@traitdef Indexable{X} begin
Y = tpar1(X)
getindex(X, Any) -> Y
setindex!(X, Y, Any) -> X
end However, that would require that all indexable types have their element type as first parameter, so maybe better: @traitdef Indexable{X} begin
Y = eltype(X)
getindex(X, Any) -> Y
setindex!(X, Y, Any) -> X
end It would be cool if you could piece together a monad example using Traits, please submit a pull request! (I also edited @tonyhffong's original post to make the citing clearer) |
Interesting. I definitely want to play with it some more. Conceptually, though, a monad always need a type parameter, so ideally we should do something like this: @traitdef Monad{X{Y}} begin
mreturn(Y) -> X{Y}
bind( X{Y}, FuncFullSig{ Y, X{Z} } ) -> X{Z}
end Assuming we have for immutable FuncFullSig{TI,TO} # a function that is f(x::TI)::TO
f::Function
end As you can see, Let's take mreturn{T;Monad{Nullable{T}}}(x::T) = Nullable{T}(x)
function bind{T;Monad{Nullable{T}}}(x::Nullable{T}, f::FuncFullSig{T,Nullable{Z}} )
if isnull(x)
return Nullable{Z}()
else
return f.f( x.value ) # ideally, we could have a try block here too.
end
end We can chain all nullable functions even if their arguments do not understand Nullable. Disclaimer: I'm struggling with how the dispatch should really work in this case. So all these are hypothetical. For completeness, I've also come across an experimental package https://github.com/pao/Monads.jl from @pao. |
Regarding monads: I find the
where |
I'm intrigued by this statement under
to ponder
which is one of the requirements to define a monad.
What do you think are the challenges in trying to do this?
One can think of Nullable{T}, Array{T}, and many others in terms of monads so the possibilities are quite exciting.
The text was updated successfully, but these errors were encountered: