[Haskell-beginners] Using a monad function inside the monad transfomer variant

Seph Shewell Brockway seph at codex.scot
Fri Feb 1 17:57:17 UTC 2019


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.)


More information about the Beginners mailing list