Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  ListT + Writer (Baa)
   2. Re:  ListT + Writer (Francesco Ariis)
   3. Re:  ListT + Writer (David McBride)
   4. Re:  ListT + Writer (Baa)
   5. Re:  ListT + Writer (Baa)
   6. Re:  ListT + Writer (David McBride)


----------------------------------------------------------------------

Message: 1
Date: Thu, 25 May 2017 18:10:27 +0300
From: Baa <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] ListT + Writer
Message-ID: <20170525181027.1a74deff@Pavel>
Content-Type: text/plain; charset=US-ASCII

Hello, everybody!

I can process list in monad style with "do" syntax and to use "guard"
function in the body. Something like:

  fn :: [a] -> [a]
  fn lst = do
    el <- lst
    guard $ condition el
    ...
    return $ change el

How can I do the same but with possibility to call "tell" of "Write"
monad in the fn's body? As I understand it should be:

   ListT (Writer w) Int

for this example?

- but how to write it?
- how to call (run) it?
- and how is it safe ("transformers" package has bug in ListT, so "mtl"
  must be used?)?
- is there other canonical way to do it without to use fold*, recursive
  calls/fix, State/RWS ?


/Cheers


------------------------------

Message: 2
Date: Thu, 25 May 2017 17:43:49 +0200
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] ListT + Writer
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Thu, May 25, 2017 at 06:10:27PM +0300, Baa wrote:
>   fn :: [a] -> [a]
>   fn lst = do
>     el <- lst
>     guard $ condition el
>     ...
>     return $ change el
> 
> How can I do the same but with possibility to call "tell" of "Write"
> monad in the fn's body? As I understand it should be:

Should be as easy as:

    import Control.Monad.List
    import Control.Monad.Writer

    type Prova = ListT (Writer String) Int

    fn :: Prova -> Prova
    fn lst = do el <- lst
                guard (rem el 2 == 0)
                lift $ tell "hey bby"
                return (el + 1)


------------------------------

Message: 3
Date: Thu, 25 May 2017 11:52:01 -0400
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] ListT + Writer
Message-ID:
        <CAN+Tr43m00fYjsnObm4kP8x4Daq=j4ljswvkggr81yx3ewd...@mail.gmail.com>
Content-Type: text/plain; charset="UTF-8"

ListT is a bit weird in that it affects whatever monad is underneath
it, so the order of your types in your Transformer stack matters.
Both ways have different meanings and each have legitimate uses.  In
any case you must use the lift function to get to the monad below the
one you are at.

import Control.Monad.List
import Control.Monad.Writer

test :: IO ()
test = do
  (runListT $ runWriterT  proc1) >>= print
  (runWriterT $ runListT proc2) >>= print
  return ()


proc1 :: Monad m => WriterT String (ListT m) Int
proc1 = do
  tell ("started: " :: String)
  x <- lift $ ListT (return [1,2])
  y <- lift $ ListT (return [3,4,5])
  lift $ guard (y /= 5)
  tell ("x:" ++ show x)
  tell ("y:" ++ show y)
  return (x * y)


proc2 :: Monad m => ListT (WriterT String m) Int
proc2 = do
  lift $ tell ("started: " :: String)
  x <- ListT (return [1,2])
  y <- ListT (return [3,4,5])
  guard (y /= 5)
  lift $ tell (" x:" ++ show x)
  lift $ tell (" y:" ++ show y)

  return (x * y)

On Thu, May 25, 2017 at 11:10 AM, Baa <[email protected]> wrote:
> Hello, everybody!
>
> I can process list in monad style with "do" syntax and to use "guard"
> function in the body. Something like:
>
>   fn :: [a] -> [a]
>   fn lst = do
>     el <- lst
>     guard $ condition el
>     ...
>     return $ change el
>
> How can I do the same but with possibility to call "tell" of "Write"
> monad in the fn's body? As I understand it should be:
>
>    ListT (Writer w) Int
>
> for this example?
>
> - but how to write it?
> - how to call (run) it?
> - and how is it safe ("transformers" package has bug in ListT, so "mtl"
>   must be used?)?
> - is there other canonical way to do it without to use fold*, recursive
>   calls/fix, State/RWS ?
>
>
> /Cheers
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

Message: 4
Date: Thu, 25 May 2017 19:02:58 +0300
From: Baa <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] ListT + Writer
Message-ID: <20170525190258.7b6b0c13@Pavel>
Content-Type: text/plain; charset=UTF-8

В Thu, 25 May 2017 17:43:49 +0200
Francesco Ariis <[email protected]> пишет:

> On Thu, May 25, 2017 at 06:10:27PM +0300, Baa wrote:
> >   fn :: [a] -> [a]
> >   fn lst = do
> >     el <- lst
> >     guard $ condition el
> >     ...
> >     return $ change el
> > 
> > How can I do the same but with possibility to call "tell" of "Write"
> > monad in the fn's body? As I understand it should be:  
> 
> Should be as easy as:
> 
>     import Control.Monad.List
>     import Control.Monad.Writer
> 
>     type Prova = ListT (Writer String) Int
> 
>     fn :: Prova -> Prova
>     fn lst = do el <- lst
>                 guard (rem el 2 == 0)
>                 lift $ tell "hey bby"
>                 return (el + 1)

Yes! And this is the source of my questions.. 1) How to call it? 2)
What does mean to provide argument of type ".. Writer .."? As result,
it's good: you can run it with "runWriter", but what is "writer" in
input argument? Fake writer (which is ignoring)?


> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



------------------------------

Message: 5
Date: Thu, 25 May 2017 19:11:50 +0300
From: Baa <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] ListT + Writer
Message-ID: <20170525191150.51d8e3ac@Pavel>
Content-Type: text/plain; charset=UTF-8

В Thu, 25 May 2017 11:52:01 -0400
David McBride <[email protected]> пишет:

Hello, David! Am I right that "WriterT ... ListT" is "list of writers"?
As I understand, internal representation is "m (a, w)" where m is a-la
List? So, this is list of "writers"? I am confused only of this "m" in
your "proc1" function, because I suppose this must be Identity and type
becomes "WriterT String [Int]" ? Or?

Can this function "proc1" be modified in the way to get input list and
to "iterate" over its elements with "do el <- ..." but to can call
Writer's tell in the same time? This is the problem for my mind - I can
not understand how to pass input list and to have writer inside :) You
call ListT's bind but over internal hardcoded list values...


> ListT is a bit weird in that it affects whatever monad is underneath
> it, so the order of your types in your Transformer stack matters.
> Both ways have different meanings and each have legitimate uses.  In
> any case you must use the lift function to get to the monad below the
> one you are at.
> 
> import Control.Monad.List
> import Control.Monad.Writer
> 
> test :: IO ()
> test = do
>   (runListT $ runWriterT  proc1) >>= print
>   (runWriterT $ runListT proc2) >>= print
>   return ()
> 
> 
> proc1 :: Monad m => WriterT String (ListT m) Int
> proc1 = do
>   tell ("started: " :: String)
>   x <- lift $ ListT (return [1,2])
>   y <- lift $ ListT (return [3,4,5])
>   lift $ guard (y /= 5)
>   tell ("x:" ++ show x)
>   tell ("y:" ++ show y)
>   return (x * y)
> 
> 
> proc2 :: Monad m => ListT (WriterT String m) Int
> proc2 = do
>   lift $ tell ("started: " :: String)
>   x <- ListT (return [1,2])
>   y <- ListT (return [3,4,5])
>   guard (y /= 5)
>   lift $ tell (" x:" ++ show x)
>   lift $ tell (" y:" ++ show y)
> 
>   return (x * y)
> 
> On Thu, May 25, 2017 at 11:10 AM, Baa <[email protected]> wrote:
> > Hello, everybody!
> >
> > I can process list in monad style with "do" syntax and to use
> > "guard" function in the body. Something like:
> >
> >   fn :: [a] -> [a]
> >   fn lst = do
> >     el <- lst
> >     guard $ condition el
> >     ...
> >     return $ change el
> >
> > How can I do the same but with possibility to call "tell" of "Write"
> > monad in the fn's body? As I understand it should be:
> >
> >    ListT (Writer w) Int
> >
> > for this example?
> >
> > - but how to write it?
> > - how to call (run) it?
> > - and how is it safe ("transformers" package has bug in ListT, so
> > "mtl" must be used?)?
> > - is there other canonical way to do it without to use fold*,
> > recursive calls/fix, State/RWS ?
> >
> >
> > /Cheers
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners  
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



------------------------------

Message: 6
Date: Thu, 25 May 2017 12:55:00 -0400
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] ListT + Writer
Message-ID:
        <CAN+Tr40V5jAqs1jqshQyX3E8u=ycpvsz5z2avsmki8gmodk...@mail.gmail.com>
Content-Type: text/plain; charset="UTF-8"

To start all these types with T at the end are transformers.  They are
a type that is wrapped around some inner m.  StateT s m, ErrorT e m a,
and so on.

In order to use do notation,  you must be in a type which is an
instance of Monad.

newtype ListT (m :: * -> *) a = ListT {runListT :: m [a]}
instance [safe] Monad m => Monad (ListT m)

newtype WriterT w (m :: * -> *) a = WriterT {runWriterT :: m (a, w)}
instance [safe] (Monoid w, Monad m) => MonadWriter w (WriterT w m)

These types and their instances say the following:
  ListT m a is a Monad if m is a Monad.
  WriterT w m a is a Monad if m is a Monad and w is a Monoid.

So in order to use do notation in a WriterT String (ListT m) a, I must
add the Monad m contstraint to proc, and also ensure that the writer's
w is a monoid (it is because it is a string).

Now to pass in a ListT as an argument, I must construct one.  Remember
that in order to use the return function, m must be in a monad, so I
must add the Monad constraint.

foo :: Monad m => ListT m Int
foo = ListT (return [1,2,3])

test = (runListT $ runWriterT (proc3 foo)) >>= print

proc3 :: Monad m => ListT m Int -> WriterT String (ListT m) Int
proc3 foo = do
  tell ("started: " :: String)
  x <- lift foo
  y <- lift $ ListT (return [3,4,5])
  lift $ guard (y /= 5)
  tell ("x:" ++ show x)
  tell ("y:" ++ show y)
  return (x * y)

As you saw in the other comment in this thread, most people use a type
alias to make it more palatable.

type MyApp m a = WriterT String (ListT m) Int
-- or type MyApp a = WriterT String (ListT IO) Int

proc3 :: Monad m =>ListT m a -> MyApp m Int
-- or proc3 :: ListT m a -> MyApp Int

On Thu, May 25, 2017 at 12:11 PM, Baa <[email protected]> wrote:
> В Thu, 25 May 2017 11:52:01 -0400
> David McBride <[email protected]> пишет:
>
> Hello, David! Am I right that "WriterT ... ListT" is "list of writers"?
> As I understand, internal representation is "m (a, w)" where m is a-la
> List? So, this is list of "writers"? I am confused only of this "m" in
> your "proc1" function, because I suppose this must be Identity and type
> becomes "WriterT String [Int]" ? Or?
>
> Can this function "proc1" be modified in the way to get input list and
> to "iterate" over its elements with "do el <- ..." but to can call
> Writer's tell in the same time? This is the problem for my mind - I can
> not understand how to pass input list and to have writer inside :) You
> call ListT's bind but over internal hardcoded list values...
>
>
>> ListT is a bit weird in that it affects whatever monad is underneath
>> it, so the order of your types in your Transformer stack matters.
>> Both ways have different meanings and each have legitimate uses.  In
>> any case you must use the lift function to get to the monad below the
>> one you are at.
>>
>> import Control.Monad.List
>> import Control.Monad.Writer
>>
>> test :: IO ()
>> test = do
>>   (runListT $ runWriterT  proc1) >>= print
>>   (runWriterT $ runListT proc2) >>= print
>>   return ()
>>
>>
>> proc1 :: Monad m => WriterT String (ListT m) Int
>> proc1 = do
>>   tell ("started: " :: String)
>>   x <- lift $ ListT (return [1,2])
>>   y <- lift $ ListT (return [3,4,5])
>>   lift $ guard (y /= 5)
>>   tell ("x:" ++ show x)
>>   tell ("y:" ++ show y)
>>   return (x * y)
>>
>>
>> proc2 :: Monad m => ListT (WriterT String m) Int
>> proc2 = do
>>   lift $ tell ("started: " :: String)
>>   x <- ListT (return [1,2])
>>   y <- ListT (return [3,4,5])
>>   guard (y /= 5)
>>   lift $ tell (" x:" ++ show x)
>>   lift $ tell (" y:" ++ show y)
>>
>>   return (x * y)
>>
>> On Thu, May 25, 2017 at 11:10 AM, Baa <[email protected]> wrote:
>> > Hello, everybody!
>> >
>> > I can process list in monad style with "do" syntax and to use
>> > "guard" function in the body. Something like:
>> >
>> >   fn :: [a] -> [a]
>> >   fn lst = do
>> >     el <- lst
>> >     guard $ condition el
>> >     ...
>> >     return $ change el
>> >
>> > How can I do the same but with possibility to call "tell" of "Write"
>> > monad in the fn's body? As I understand it should be:
>> >
>> >    ListT (Writer w) Int
>> >
>> > for this example?
>> >
>> > - but how to write it?
>> > - how to call (run) it?
>> > - and how is it safe ("transformers" package has bug in ListT, so
>> > "mtl" must be used?)?
>> > - is there other canonical way to do it without to use fold*,
>> > recursive calls/fix, State/RWS ?
>> >
>> >
>> > /Cheers
>> > _______________________________________________
>> > Beginners mailing list
>> > [email protected]
>> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 107, Issue 12
******************************************

Reply via email to