Using a monad function inside the monad transfomer variant

Hi, I am running into difficulties regarding the use of monad transformers. The situation is as follows: I implemented some functions returning "RVar" results. Now, in a different part of the program, I am using STUArrays. Therefore my idea was to create a monad transformer stack which would have this type: someFunction:: a -> b -> RVarT (ST s) (STUArray s Int Double) I still want to use the functions that return RVars, so eg. rvarDouble :: RVar Double and then the definition of the transformer function would be: someFunction a b = do ... the_double <- rvarDouble .... This does not compile, complaining that; • Couldn't match type ‘Data.Functor.Identity.Identity’ with ‘ST s’ Expected type: RVarT (ST s) Double Actual type: RVar Double How can I re-user the RVar function in the RVarT monad transformer? Sincerely yours, Moritz

On Fri, Feb 01, 2019 at 03:14:32PM +0100, Moritz Tacke wrote:
Hi,
I still want to use the functions that return RVars, so eg.
rvarDouble :: RVar Double
and then the definition of the transformer function would be:
someFunction a b = do ... the_double <- rvarDouble ....
This does not compile, complaining that;
• Couldn't match type ‘Data.Functor.Identity.Identity’ with ‘ST s’ Expected type: RVarT (ST s) Double Actual type: RVar Double
How can I re-user the RVar function in the RVarT monad transformer?
Your declaration of rvarDouble needs to be polymorphic in the monad: rvarDouble :: Monad m => RVarT m Double The crucial observation is that RVar is actually a type synonym for RVarT Identity, so the function can still be made to return a plain RVar Double, but it can also return an RVarT (ST s) Double, satisfying the type-checker in the example that you gave. -- Seph Shewell Brockway, BSc MSc (Glas.)

Ok, thank you, I'll try!
Just to understand this: Is this due to a specific reason? Couldn't
the compiler infer from a definition of a RVar that the same function
can also be used in the RVarT situation? It would (somehow) look
cleaner and I do not see any differences in the semantics
On Fri, Feb 1, 2019 at 6:57 PM Seph Shewell Brockway
On Fri, Feb 01, 2019 at 03:14:32PM +0100, Moritz Tacke wrote:
Hi,
I still want to use the functions that return RVars, so eg.
rvarDouble :: RVar Double
and then the definition of the transformer function would be:
someFunction a b = do ... the_double <- rvarDouble ....
This does not compile, complaining that;
• Couldn't match type ‘Data.Functor.Identity.Identity’ with ‘ST s’ Expected type: RVarT (ST s) Double Actual type: RVar Double
How can I re-user the RVar function in the RVarT monad transformer?
Your declaration of rvarDouble needs to be polymorphic in the monad:
rvarDouble :: Monad m => RVarT m Double
The crucial observation is that RVar is actually a type synonym for RVarT Identity, so the function can still be made to return a plain RVar Double, but it can also return an RVarT (ST s) Double, satisfying the type-checker in the example that you gave.
-- Seph Shewell Brockway, BSc MSc (Glas.) _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (2)
-
Moritz Tacke
-
Seph Shewell Brockway