Using a monad function inside the monad transfomer variant

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Using a monad function inside the monad transfomer variant

Moritz Tacke-2
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
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Using a monad function inside the monad transfomer variant

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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Using a monad function inside the monad transfomer variant

Moritz Tacke-2
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 <[hidden email]> wrote:

>
> 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
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners