I am struggling with Elm's lack of monads. A library implementing the state monad for Elm (http://package.elm-lang.org/packages/folkertdev/elm-state/latest/State) has helped me quite a bit.
The problem is that I have now run into a situation where I have alternately nested Result and State types, when I only want to have one of each.
I tried to write a function with the following signature, but it seems impossible, because the inner Result is only known once the outer State is evaluated.
join : Result a (State s (Result a (State s x))) -> Result a (State s x)
Maybe it would work if I put the Result inside the State in the return value, but that would generate a dummy State in case the outer Result is Err.
I think the right idea would be to make something that is both Result and State. Can someone who is familiar with Haskell monad transformers explain how they solve this kind of problem or suggest an alternative solution?
Here is a rough version of one place where the problem arises:
generateConstraints environment value
|> Result.map (State.map (\(value, valueC) ->
Result.map
(State.map2 (\this (body, bodyC) ->
( this
, valueC ++ bodyC ++ [(this, body)]
))
freshTypevar)
(generateConstraints (extend environment name value) body))
)
generateConstraints, which returnsResult String (State Int (Type, List Constraint)). Recursion works fine, but there is something different with the constraint generation for let bindings that messes up the monads. - Joonazan