Monday, 26 August 2013

How to nest monad transformers correctly?

How to nest monad transformers correctly?

Imagine I have a function
calc_set :: Context
-> Term
-> StateT FreshName (EitherT String Identity) (Type, Set)
The intuition is that this function will take a context, a term, a
generator of fresh names, and will produce some complex result. However, I
can't seem to write it! One case for terms would look like this:
calc_set ctx (TmVar i) = return (getTypeFromContext ctx i, emptySet)
However, it won't typecheck because getTypeFromContext has type
getTypeFromContext :: Context -> Index -> Either String Type
Okay, I re-write it as
calc_set ctx (TmVar i) = do
a <- getTypeFromContext ctx i
return (a, emptySet)
and it doesn't work, because the compiler Couldn't match expected type
'StateT String (EitherT String Identity) t0' with actual type 'Either
String Type'... and I have no idea how to turn Either String Type into
EitherT String Identity. What do I do?
I am not even sure I have the order of transformers correct. May someone
recommend an elaborated tutorial on monad transformers?

No comments:

Post a Comment