need help with monad transformers

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

need help with monad transformers

Alexander Vershilov
Hello.

I'm trying to add monad stack into network-conduit, and everything
works except some details [1].

I've run runReaderT $ runTCPServer (wrapper around runResourceT) and
inside conduit I want to run writer to gather results of inner computation.
In inner computation I want to use IO, data from outter stack (ReaderT)
so I'm running {-1-}:

  (k,t) <- lift $ runWriterT $ ask >>= \x -> tell [x]     {- 1 -}

and {-2-}

  (k,t) <- lift $ runWriterT $ do {- 2 -}
                    x <- ask
                    liftIO $ print $x+1
                    tell [x]

and that will work (except I've thought I should not lift runWriterT, but
calling functions inside.

And finally in computation that will run once I want to register cleaning
function (for example register $ putStrLn "cleaned") ({-3-})

  (k,t) <- lift $ runWriterT $ do {- 3 -}
                    x <- ask
                    liftIO $ print $x+1
                    register $ print "freed2"
                    tell [x]

but I've got type error. If I'm running register outside runWriterT everything
will work.

I would apperated if there will be any suggestions how to make this code
better or use register in internal computation (runWriterT)

[1] https://gist.github.com/1941151
--
Best regards,
  Alexander V Vershilov

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

signature.asc (501 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: need help with monad transformers

Dmitry Olshansky
>> If I'm running register outside runWriterT everything will work.

Maybe just

> lift $ register $ print "freed2"

or I didn't catch something?



2012/3/1 Alexander V Vershilov <[hidden email]>
Hello.

I'm trying to add monad stack into network-conduit, and everything
works except some details [1].

I've run runReaderT $ runTCPServer (wrapper around runResourceT) and
inside conduit I want to run writer to gather results of inner computation.
In inner computation I want to use IO, data from outter stack (ReaderT)
so I'm running {-1-}:

 (k,t) <- lift $ runWriterT $ ask >>= \x -> tell [x]     {- 1 -}

and {-2-}

 (k,t) <- lift $ runWriterT $ do {- 2 -}
                   x <- ask
                   liftIO $ print $x+1
                   tell [x]

and that will work (except I've thought I should not lift runWriterT, but
calling functions inside.

And finally in computation that will run once I want to register cleaning
function (for example register $ putStrLn "cleaned") ({-3-})

 (k,t) <- lift $ runWriterT $ do {- 3 -}
                   x <- ask
                   liftIO $ print $x+1
                   register $ print "freed2"
                   tell [x]

but I've got type error. If I'm running register outside runWriterT everything
will work.

I would apperated if there will be any suggestions how to make this code
better or use register in internal computation (runWriterT)

[1] https://gist.github.com/1941151
--
Best regards,
 Alexander V Vershilov

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: need help with monad transformers

Alexander Vershilov
It will not work due:

gistfile1.hs:29:36:
    Could not deduce (m ~ ResourceT m0)
    from the context (Base m ~ IO,
                      MonadReader SInit m,
                      MonadIO m,
                      IsSource src,
                      Resource m)
      bound by the type signature for
                 app :: (Base m ~ IO,
                         MonadReader SInit m,
                         MonadIO m,
                         IsSource src,
                         Resource m) =>
                        src m a1 -> Sink a1 m b -> ResourceT m b
      at gistfile1.hs:(29,1)-(59,27)
      `m' is a rigid type variable bound by
          the type signature for
            app :: (Base m ~ IO,
                    MonadReader SInit m,
                    MonadIO m,
                    IsSource src,
                    Resource m) =>
                   src m a1 -> Sink a1 m b -> ResourceT m b
          at gistfile1.hs:29:1
    Expected type: Conduit a1 m a1
      Actual type: Conduit a1 (ResourceT m0) a1
    In the second argument of `(=$=)', namely `process2'
    In the second argument of `($=)', namely `(process1 =$= process2)'


Thu, Mar 01, 2012 at 02:19:29PM +0400, Dmitry Olshansky wrote

> >> If I'm running register outside runWriterT everything will work.
>
> Maybe just
>
> > lift $ register $ print "freed2"
>
> or I didn't catch something?
>
>
>
> 2012/3/1 Alexander V Vershilov <[hidden email]>
>
>     Hello.
>
>     I'm trying to add monad stack into network-conduit, and everything
>     works except some details [1].
>
>     I've run runReaderT $ runTCPServer (wrapper around runResourceT) and
>     inside conduit I want to run writer to gather results of inner computation.
>     In inner computation I want to use IO, data from outter stack (ReaderT)
>     so I'm running {-1-}:
>
>      (k,t) <- lift $ runWriterT $ ask >>= \x -> tell [x]     {- 1 -}
>
>     and {-2-}
>
>      (k,t) <- lift $ runWriterT $ do {- 2 -}
>                        x <- ask
>                        liftIO $ print $x+1
>                        tell [x]
>
>     and that will work (except I've thought I should not lift runWriterT, but
>     calling functions inside.
>
>     And finally in computation that will run once I want to register cleaning
>     function (for example register $ putStrLn "cleaned") ({-3-})
>
>      (k,t) <- lift $ runWriterT $ do {- 3 -}
>                        x <- ask
>                        liftIO $ print $x+1
>                        register $ print "freed2"
>                        tell [x]
>
>     but I've got type error. If I'm running register outside runWriterT
>     everything
>     will work.
>
>     I would apperated if there will be any suggestions how to make this code
>     better or use register in internal computation (runWriterT)
>
>     [1] https://gist.github.com/1941151
>     --
>     Best regards,
>      Alexander V Vershilov
>
>     _______________________________________________
>     Haskell-Cafe mailing list
>     [hidden email]
>     http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

signature.asc (501 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: need help with monad transformers

Alexander Vershilov
In reply to this post by Dmitry Olshansky
I've found a solution, I should not use lift for runWriterT, and should
explicilty lift all computation of level I need, i.e. (lift.lift) for ask
and lift for register.

Thu, Mar 01, 2012 at 02:19:29PM +0400, Dmitry Olshansky wrote

> >> If I'm running register outside runWriterT everything will work.
>
> Maybe just
>
> > lift $ register $ print "freed2"
>
> or I didn't catch something?
>
>
>
> 2012/3/1 Alexander V Vershilov <[hidden email]>
>
>     Hello.
>
>     I'm trying to add monad stack into network-conduit, and everything
>     works except some details [1].
>
>     I've run runReaderT $ runTCPServer (wrapper around runResourceT) and
>     inside conduit I want to run writer to gather results of inner computation.
>     In inner computation I want to use IO, data from outter stack (ReaderT)
>     so I'm running {-1-}:
>
>      (k,t) <- lift $ runWriterT $ ask >>= \x -> tell [x]     {- 1 -}
>
>     and {-2-}
>
>      (k,t) <- lift $ runWriterT $ do {- 2 -}
>                        x <- ask
>                        liftIO $ print $x+1
>                        tell [x]
>
>     and that will work (except I've thought I should not lift runWriterT, but
>     calling functions inside.
>
>     And finally in computation that will run once I want to register cleaning
>     function (for example register $ putStrLn "cleaned") ({-3-})
>
>      (k,t) <- lift $ runWriterT $ do {- 3 -}
>                        x <- ask
>                        liftIO $ print $x+1
>                        register $ print "freed2"
>                        tell [x]
>
>     but I've got type error. If I'm running register outside runWriterT
>     everything
>     will work.
>
>     I would apperated if there will be any suggestions how to make this code
>     better or use register in internal computation (runWriterT)
>
>     [1] https://gist.github.com/1941151
>     --
>     Best regards,
>      Alexander V Vershilov
>
>     _______________________________________________
>     Haskell-Cafe mailing list
>     [hidden email]
>     http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

signature.asc (501 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: need help with monad transformers

Dmitry Olshansky
Did you try to use transformers instead of mtl? I am just in doubt about it in my work.

I've found that Conduit use it. So you remove extra dependency.
I hope that transformers have more readable messages (without FD) and you can control what is "lift" more clear.

I have no experience although.



2012/3/2 Alexander V Vershilov <[hidden email]>
I've found a solution, I should not use lift for runWriterT, and should
explicilty lift all computation of level I need, i.e. (lift.lift) for ask
and lift for register.

Thu, Mar 01, 2012 at 02:19:29PM +0400, Dmitry Olshansky wrote
> >> If I'm running register outside runWriterT everything will work.
>
> Maybe just
>
> > lift $ register $ print "freed2"
>
> or I didn't catch something?
>
>
>
> 2012/3/1 Alexander V Vershilov <[hidden email]>
>
>     Hello.
>
>     I'm trying to add monad stack into network-conduit, and everything
>     works except some details [1].
>
>     I've run runReaderT $ runTCPServer (wrapper around runResourceT) and
>     inside conduit I want to run writer to gather results of inner computation.
>     In inner computation I want to use IO, data from outter stack (ReaderT)
>     so I'm running {-1-}:
>
>      (k,t) <- lift $ runWriterT $ ask >>= \x -> tell [x]     {- 1 -}
>
>     and {-2-}
>
>      (k,t) <- lift $ runWriterT $ do {- 2 -}
>                        x <- ask
>                        liftIO $ print $x+1
>                        tell [x]
>
>     and that will work (except I've thought I should not lift runWriterT, but
>     calling functions inside.
>
>     And finally in computation that will run once I want to register cleaning
>     function (for example register $ putStrLn "cleaned") ({-3-})
>
>      (k,t) <- lift $ runWriterT $ do {- 3 -}
>                        x <- ask
>                        liftIO $ print $x+1
>                        register $ print "freed2"
>                        tell [x]
>
>     but I've got type error. If I'm running register outside runWriterT
>     everything
>     will work.
>
>     I would apperated if there will be any suggestions how to make this code
>     better or use register in internal computation (runWriterT)
>
>     [1] https://gist.github.com/1941151
>     --
>     Best regards,
>      Alexander V Vershilov
>
>     _______________________________________________
>     Haskell-Cafe mailing list
>     [hidden email]
>     http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe