Got "parse error on input ‘=’” in Emacs Intero REPL when define any function

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

Got "parse error on input ‘=’” in Emacs Intero REPL when define any function

乃潇 张
Hi, I am trying to define some functions in a REPL window of emacs intero. However, I always got "parse error on input ‘=’” as blow:

Starting:
  stack ghci --with-ghc intero "--docker-run-args=--interactive=true --tty=false" --no-build --no-load --ghci-options -odir=/Users/spike/Code/haskell/learn-haskell/baby/.stack-work/intero/intero445zea --ghci-options -hidir=/Users/spike/Code/haskell/learn-haskell/baby/.stack-work/intero/intero445zea baby
Intero 0.1.20 (GHC 8.0.2)
Type :intro and press enter for an introduction of the standard commands.

 addTwo x y = x + y

<interactive>:2:12: error:
    parse error on input ‘=’
    Perhaps you need a 'let' in a 'do' block?
    e.g. 'let x = 5' instead of 'x = 5’

I did some research, seems like “let” is not a must here for ghci version above 8.0. And my ghc version is 8.0.2. So is it a issue of ghci or intero?


Thanks,
Sid

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Got "parse error on input ‘=’” in Emacs Intero REPL when define any function

乃潇 张
I find if I run “stack ghci intero” (remove the “—with-ghc”) from terminal, then the issue will not occur. Does anyone have idea why?

On May 23, 2017, at 9:58 PM, 乃潇 张 <[hidden email]> wrote:

Hi, I am trying to define some functions in a REPL window of emacs intero. However, I always got "parse error on input ‘=’” as blow:

Starting:
  stack ghci --with-ghc intero "--docker-run-args=--interactive=true --tty=false" --no-build --no-load --ghci-options -odir=/Users/spike/Code/haskell/learn-haskell/baby/.stack-work/intero/intero445zea --ghci-options -hidir=/Users/spike/Code/haskell/learn-haskell/baby/.stack-work/intero/intero445zea baby
Intero 0.1.20 (GHC 8.0.2)
Type :intro and press enter for an introduction of the standard commands.

 addTwo x y = x + y

<interactive>:2:12: error:
    parse error on input ‘=’
    Perhaps you need a 'let' in a 'do' block?
    e.g. 'let x = 5' instead of 'x = 5’

I did some research, seems like “let” is not a must here for ghci version above 8.0. And my ghc version is 8.0.2. So is it a issue of ghci or intero?


Thanks,
Sid
_______________________________________________
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
Baa
Reply | Threaded
Open this post in threaded view
|

Re: Got "parse error on input ‘=’” in Emacs Intero REPL when define any function

Baa
try with "let":
let f x y = x + y

2017-05-23 17:04 GMT+03:00 乃潇 张 <[hidden email]>:
I find if I run “stack ghci intero” (remove the “—with-ghc”) from terminal, then the issue will not occur. Does anyone have idea why?

On May 23, 2017, at 9:58 PM, 乃潇 张 <[hidden email]> wrote:

Hi, I am trying to define some functions in a REPL window of emacs intero. However, I always got "parse error on input ‘=’” as blow:

Starting:
  stack ghci --with-ghc intero "--docker-run-args=--interactive=true --tty=false" --no-build --no-load --ghci-options -odir=/Users/spike/Code/haskell/learn-haskell/baby/.stack-work/intero/intero445zea --ghci-options -hidir=/Users/spike/Code/haskell/learn-haskell/baby/.stack-work/intero/intero445zea baby
Intero 0.1.20 (GHC 8.0.2)
Type :intro and press enter for an introduction of the standard commands.

addTwo x y = x + y

<interactive>:2:12: error:
    parse error on input ‘=’
    Perhaps you need a 'let' in a 'do' block?
    e.g. 'let x = 5' instead of 'x = 5’

I did some research, seems like “let” is not a must here for ghci version above 8.0. And my ghc version is 8.0.2. So is it a issue of ghci or intero?


Thanks,
Sid
_______________________________________________
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



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Got "parse error on input ‘=’” in Emacs Intero REPL when define any function

乃潇 张
Thanks, I know if using “let”, there’ll be no error. My question is:
1. what does option "—with-ghc” do? 
2. Is it better to remove “—with-ghc” when running in intero REPL? If yes, how to remove it?

On May 24, 2017, at 12:22 AM, baa dg <[hidden email]> wrote:

try with "let":
let f x y = x + y

2017-05-23 17:04 GMT+03:00 乃潇 张 <[hidden email]>:
I find if I run “stack ghci intero” (remove the “—with-ghc”) from terminal, then the issue will not occur. Does anyone have idea why?

On May 23, 2017, at 9:58 PM, 乃潇 张 <[hidden email]> wrote:

Hi, I am trying to define some functions in a REPL window of emacs intero. However, I always got "parse error on input ‘=’” as blow:

Starting:
  stack ghci --with-ghc intero "--docker-run-args=--interactive=true --tty=false" --no-build --no-load --ghci-options -odir=/Users/spike/Code/haskell/learn-haskell/baby/.stack-work/intero/intero445zea --ghci-options -hidir=/Users/spike/Code/haskell/learn-haskell/baby/.stack-work/intero/intero445zea baby
Intero 0.1.20 (GHC 8.0.2)
Type :intro and press enter for an introduction of the standard commands.

addTwo x y = x + y

<interactive>:2:12: error:
    parse error on input ‘=’
    Perhaps you need a 'let' in a 'do' block?
    e.g. 'let x = 5' instead of 'x = 5’

I did some research, seems like “let” is not a must here for ghci version above 8.0. And my ghc version is 8.0.2. So is it a issue of ghci or intero?


Thanks,
Sid
_______________________________________________
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


_______________________________________________
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
Baa
Reply | Threaded
Open this post in threaded view
|

ListT + Writer

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

Re: ListT + Writer

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

Re: ListT + Writer

David McBride
In reply to this post by Baa
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 <[hidden email]> 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
> [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
Baa
Reply | Threaded
Open this post in threaded view
|

Re: ListT + Writer

Baa
In reply to this post by Francesco Ariis
В Thu, 25 May 2017 17:43:49 +0200
Francesco Ariis <[hidden email]> пишет:

> 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
> [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
Baa
Reply | Threaded
Open this post in threaded view
|

Re: ListT + Writer

Baa
In reply to this post by David McBride
В Thu, 25 May 2017 11:52:01 -0400
David McBride <[hidden email]> пишет:

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 <[hidden email]> 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
> > [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

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: ListT + Writer

David McBride
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 <[hidden email]> wrote:

> В Thu, 25 May 2017 11:52:01 -0400
> David McBride <[hidden email]> пишет:
>
> 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 <[hidden email]> 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
>> > [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
>
> _______________________________________________
> 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
Baa
Reply | Threaded
Open this post in threaded view
|

Re: ListT + Writer

Baa
David, many thanks for such a detailed answer and explanation!!

So, if I know that I'll iterate over list (vs abstract "m"'s bind), can
I replace "m" with Identity, to get list instead of abstract "ListT m"?

And last question: what is more canonical (Haskelish) way to iterate
over list with state? Fold*/State/Writer? Actually my ultimate goal was
to process list items with some state and possible IO (REST calls,
something else)... Is the usage of ListT + Writer (or ListT + State) a
good solution or better is to make all logic in one function and to
pass it to fold* (so state will be accumulating value)? I will iterate
over items of items of this list too: some of them are also lists,
so ListT looks more naturally IMHO, but I'm not sure...

David, in all cases - many thanks!!



В Thu, 25 May 2017 12:55:00 -0400
David McBride <[hidden email]> пишет:

> 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 <[hidden email]> wrote:
> > В Thu, 25 May 2017 11:52:01 -0400
> > David McBride <[hidden email]> пишет:
> >
> > 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 <[hidden email]> 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
> >> > [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 
> >
> > _______________________________________________
> > 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



--
Best regards,
  Paul a.k.a. 6apcyk
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: ListT + Writer

Francesco Ariis
In reply to this post by Baa
On Thu, May 25, 2017 at 07:02:58PM +0300, Baa wrote:

>
> В Thu, 25 May 2017 17:43:49 +0200
> > Should be as easy as:
> >
> >     import Control.Monad.List
> >     import Control.Monad.Writer
> >
> >     type Prova = ListT (Writer String) Int
>
> 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)?

I am not sure for question #1. If we look at the constructor of ListT
(`runListT :: m [a]`) and MonadWriter (`(a, w)`) and we compose the two
we get:

    ([a], w)
    -- or ([Integer], String) in our example
    -- e.g. ListT (writer ([1,2], "ciao")) <-- to construct

No idea what is the correct name of this critter.

As we can see by running it, the argument in `fn` (both the list part and
the string part) is not ignored:

    λ> fn $ ListT (writer ([1,2], "ciao"))
    ListT (WriterT (Identity ([3],"ciaohey baby")))

Does this answer your questions?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: ListT + Writer

David McBride
In reply to this post by Baa
If you fill in Identity for m, ListT m Int ends up becoming Identity
[Int], which is just a list.

Doing effects over a list is a classic FoldM.  You can use the foldl
library on hackage to do most things, I think.  To do it over a list
of lists, that actually sounds like something you would use ListT for,
but I don't use it much in my code, so I can't say for sure how well
it will work for you.  For what it is worth I've also heard that the
version of ListT in base is not quite correct, although I don't know
why.

If you are feeling adventurous, check out the pipes tutorial
http://hackage.haskell.org/package/pipes-4.3.4/docs/Pipes-Tutorial.html,
there is a ListT done right section toward the end where it shows how
to construct and use IO code from his version of a ListT.  That would
require you to understand and buy into the pipes ecosystem.

On Thu, May 25, 2017 at 2:37 PM, aquagnu <[hidden email]> wrote:

> David, many thanks for such a detailed answer and explanation!!
>
> So, if I know that I'll iterate over list (vs abstract "m"'s bind), can
> I replace "m" with Identity, to get list instead of abstract "ListT m"?
>
> And last question: what is more canonical (Haskelish) way to iterate
> over list with state? Fold*/State/Writer? Actually my ultimate goal was
> to process list items with some state and possible IO (REST calls,
> something else)... Is the usage of ListT + Writer (or ListT + State) a
> good solution or better is to make all logic in one function and to
> pass it to fold* (so state will be accumulating value)? I will iterate
> over items of items of this list too: some of them are also lists,
> so ListT looks more naturally IMHO, but I'm not sure...
>
> David, in all cases - many thanks!!
>
>
>
> В Thu, 25 May 2017 12:55:00 -0400
> David McBride <[hidden email]> пишет:
>
>> 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 <[hidden email]> wrote:
>> > В Thu, 25 May 2017 11:52:01 -0400
>> > David McBride <[hidden email]> пишет:
>> >
>> > 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 <[hidden email]> 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
>> >> > [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
>> >
>> > _______________________________________________
>> > 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
>
>
>
> --
> Best regards,
>   Paul a.k.a. 6apcyk
> _______________________________________________
> 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
Baa
Reply | Threaded
Open this post in threaded view
|

Re: ListT + Writer

Baa
In reply to this post by Francesco Ariis
@Francesco, yes, it completely answers my questions. Thank you very much
for clarification!! I got it.

В Thu, 25 May 2017 20:44:57 +0200
Francesco Ariis <[hidden email]> пишет:

> On Thu, May 25, 2017 at 07:02:58PM +0300, Baa wrote:
> >
> > В Thu, 25 May 2017 17:43:49 +0200  
> > > Should be as easy as:
> > >
> > >     import Control.Monad.List
> > >     import Control.Monad.Writer
> > >
> > >     type Prova = ListT (Writer String) Int  
> >
> > 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)?  
>
> I am not sure for question #1. If we look at the constructor of ListT
> (`runListT :: m [a]`) and MonadWriter (`(a, w)`) and we compose the
> two we get:
>
>     ([a], w)
>     -- or ([Integer], String) in our example
>     -- e.g. ListT (writer ([1,2], "ciao")) <-- to construct
>
> No idea what is the correct name of this critter.
>
> As we can see by running it, the argument in `fn` (both the list part
> and the string part) is not ignored:
>
>     λ> fn $ ListT (writer ([1,2], "ciao"))  
>     ListT (WriterT (Identity ([3],"ciaohey baby")))
>
> Does this answer your questions?
> _______________________________________________
> 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
Baa
Reply | Threaded
Open this post in threaded view
|

Re: ListT + Writer

Baa
In reply to this post by David McBride
David,

On https://hackage.haskell.org/package/transformers-0.5.4.0/docs/Control-Monad-Trans-List.html
is written that

  "Deprecated: This transformer is invalid on most monads".

Also I found this: https://wiki.haskell.org/ListT_done_right.
But no such warning in the "mtl" package, and I don't know is it done
rightly in the "mtl" package...

David, what does mean "space leak" here:

  "Use this foldl library when you want to compute multiple folds over a
  collection in one pass over the data without space leaks"
  --http://hackage.haskell.org/package/foldl
?

As I understand no real space leak when we are talking about Haskell
code, only not optimal usage of memory (for example, thunks number
grows up drastically)?


Guys, it's so good that there are people like you who have enough time
and desire to support us - Haskell beginners.



В Thu, 25 May 2017 15:10:10 -0400
David McBride <[hidden email]> пишет:

> If you fill in Identity for m, ListT m Int ends up becoming Identity
> [Int], which is just a list.
>
> Doing effects over a list is a classic FoldM.  You can use the foldl
> library on hackage to do most things, I think.  To do it over a list
> of lists, that actually sounds like something you would use ListT for,
> but I don't use it much in my code, so I can't say for sure how well
> it will work for you.  For what it is worth I've also heard that the
> version of ListT in base is not quite correct, although I don't know
> why.
>
> If you are feeling adventurous, check out the pipes tutorial
> http://hackage.haskell.org/package/pipes-4.3.4/docs/Pipes-Tutorial.html,
> there is a ListT done right section toward the end where it shows how
> to construct and use IO code from his version of a ListT.  That would
> require you to understand and buy into the pipes ecosystem.
>
> On Thu, May 25, 2017 at 2:37 PM, aquagnu <[hidden email]> wrote:
> > David, many thanks for such a detailed answer and explanation!!
> >
> > So, if I know that I'll iterate over list (vs abstract "m"'s bind),
> > can I replace "m" with Identity, to get list instead of abstract
> > "ListT m"?
> >
> > And last question: what is more canonical (Haskelish) way to iterate
> > over list with state? Fold*/State/Writer? Actually my ultimate goal
> > was to process list items with some state and possible IO (REST
> > calls, something else)... Is the usage of ListT + Writer (or ListT
> > + State) a good solution or better is to make all logic in one
> > function and to pass it to fold* (so state will be accumulating
> > value)? I will iterate over items of items of this list too: some
> > of them are also lists, so ListT looks more naturally IMHO, but I'm
> > not sure...
> >
> > David, in all cases - many thanks!!
> >
> >
> >
> > В Thu, 25 May 2017 12:55:00 -0400
> > David McBride <[hidden email]> пишет:
> >  
> >> 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 <[hidden email]> wrote:  
> >> > В Thu, 25 May 2017 11:52:01 -0400
> >> > David McBride <[hidden email]> пишет:
> >> >
> >> > 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 <[hidden email]>
> >> >> 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
> >> >> > [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 
> >> >
> >> > _______________________________________________
> >> > 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 
> >
> >
> >
> > --
> > Best regards,
> >   Paul a.k.a. 6apcyk
> > _______________________________________________
> > 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

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Baa
Reply | Threaded
Open this post in threaded view
|

First steps with Streaming library

Baa
In reply to this post by 乃潇 张
I'm trying to start working with Streaming package. First attempt
is to simulate source of stream items (I call the function "gen")
and some processor ("proc"). Both should be stateful: to be able
to sabe some info about processing steps, etc.

  ...
  import Streaming
  import qualified Streaming.Prelude as S
  ...

  gen :: S.Stream (S.Of Int) IO [String]
  gen = do
    S.yield 1000
    S.yield 2000
    x <- lift getLine
    return ["a", "b", "c", x] -- results

  proc :: S.Stream (S.Of Int) IO [String] -> S.Stream (S.Of Int) IO [String]
  proc str = do
    e <- str
    lift $ print "Enter x:"
    x <- lift getLine
    return $ e ++ [" -- " ++ x] -- put stream items in result
 
  main :: IO
  main = do
    s <- S.mapM_ print $ S.map show gen
    p <- S.mapM_ print $ proc gen
    putStr "s: " >> print s
    putStr "p: " >> print p

And I try to simulate "piping" between "gen" and "proc", seems that
function application is enought to compose producers and consumers. But
this snippet is not correct: "e <- str" extracts element not from
stream, but from results (of "gen"). Even more, I don't know how to
"yield" new items, based on stream items. Something like "await" of
Conduit, or like in Python "for e in str: ... yield modified(e)...". Is
it possible to do it with "do" notation?

---
Best,
  Paul
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Baa
Reply | Threaded
Open this post in threaded view
|

Re: First steps with Streaming library

Baa
I'll try to clarify the question. Sure I can traverse the stream items
with function like `a -> m b` applying it with `mapM`. But in this case
I will not have access to return of previous stream node, which I'm
planning to use as global state for whole pipe workflow:

     .--state-------+--state'--------+--state''-->
     |              |                |
  [e0..eN] ==> [e0'...eN'] ==> [e0''..eN''] =====>

This "state" will be used for statistics, errors, whatever - through
the whold workflow. This "pipe" will iterate over `eN` items (which
will be streams too), concatenates results... How this can be achieved
with Streaming library? I mean each "node" should have access to stream
items but to "global" state (result of prev. node return?) too.

Is it possible?


В Sat, 27 May 2017 18:30:28 +0300
aquagnu <[hidden email]> пишет:

> I'm trying to start working with Streaming package. First attempt
> is to simulate source of stream items (I call the function "gen")
> and some processor ("proc"). Both should be stateful: to be able
> to sabe some info about processing steps, etc.
>
>   ...
>   import Streaming
>   import qualified Streaming.Prelude as S
>   ...
>
>   gen :: S.Stream (S.Of Int) IO [String]
>   gen = do
>     S.yield 1000
>     S.yield 2000
>     x <- lift getLine
>     return ["a", "b", "c", x] -- results
>
>   proc :: S.Stream (S.Of Int) IO [String] -> S.Stream (S.Of Int) IO
> [String] proc str = do
>     e <- str
>     lift $ print "Enter x:"
>     x <- lift getLine
>     return $ e ++ [" -- " ++ x] -- put stream items in result
>  
>   main :: IO
>   main = do
>     s <- S.mapM_ print $ S.map show gen
>     p <- S.mapM_ print $ proc gen
>     putStr "s: " >> print s
>     putStr "p: " >> print p
>
> And I try to simulate "piping" between "gen" and "proc", seems that
> function application is enought to compose producers and consumers.
> But this snippet is not correct: "e <- str" extracts element not from
> stream, but from results (of "gen"). Even more, I don't know how to
> "yield" new items, based on stream items. Something like "await" of
> Conduit, or like in Python "for e in str: ... yield modified(e)...".
> Is it possible to do it with "do" notation?
>
> ---
> Best,
>   Paul


--
Best regards,
  Paul a.k.a. 6apcyk
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Baa
Reply | Threaded
Open this post in threaded view
|

Alternative instance w/ additional restriction

Baa
In reply to this post by David McBride
Hello all!

If I try to write, for example:

  instance Alternative MyData where
    empty = NoMyData
    a <|> b = if a == b then ...

I get error (see on bottom of the mail) sure, bcz I suppose something
like:

   Eq a => (MyData a)

All my attempts to add something to instance's `pure` signature have
failed. How can I instantiate something with additional restrictions,
like in this case? Or are there another solutions for such problem?

Interesting is that MyData derives Eq itself! Which, I suppose, must
means that "Eq (MyData a)", and if it's true than "Eq a" is true,
because how "MyData a" can be Eq without to be "Eq a" (with
*automatically deriving* of Eq instance) ?!

By the way, for Monoid (which is "* -> Constraint") I can add "Eq a"
constraint without problems:

  instance Eq a => Monoid (Origin a) where
    mempty = NoMyData
    mappend NoMyData a = a
    mappend a NoMyData = a
    mappend (MyData a) (MyData b)|a == b = MyData a
                                 |otherwise = NoMyData

but not for Alternative, which is "(* -> *) -> Constraint".

*ORIGINAL ERROR DUMP*:
======================
    42  16 error           error:                                                                                                      
                               • No instance for (Eq a) arising from a use of ‘==’                                                      
                                 Possible fix:                                                                                          
                                   add (Eq a) to the context of                                                                        
                                     the type signature for:                                                                            
                                       (<|>) :: Origin a -> Origin a -> Origin a                                                        
                               • In the expression: a == b                                                                              
                                 In the expression: if a == b then NoOrigin else NoOrigin                                              
                                 In an equation for ‘<|>’:                                                                              
                                     a <|> b = if a == b then NoOrigin
                                     else NoOrigin (intero)


/Best regards
  Paul
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Alternative instance w/ additional restriction

David McBride
The Alternative class says nothing about the a in MyData a.  It only
represents code relevant to MyData.

When you see class Applicative f => Alternative (f :: * -> *), that
means all of its functions had to work on any f, where f takes any
type and becomes some other type which could be anything.

The reason it works for Monoid is that class Monoid a where implies
that a is completely known by the time the instance is fulfilled,
therefore the instance can look at what a ended up being and ensure
whatever a is, it must have this constraint on it.

You can tell the difference because mempty returns a type that
mentions the a mentioned in the class, whereas empty returns an a that
is not mentioned in the class, therefore it has to work for any a.

On Wed, Jun 7, 2017 at 8:26 AM, Baa <[hidden email]> wrote:

> Hello all!
>
> If I try to write, for example:
>
>   instance Alternative MyData where
>     empty = NoMyData
>     a <|> b = if a == b then ...
>
> I get error (see on bottom of the mail) sure, bcz I suppose something
> like:
>
>    Eq a => (MyData a)
>
> All my attempts to add something to instance's `pure` signature have
> failed. How can I instantiate something with additional restrictions,
> like in this case? Or are there another solutions for such problem?
>
> Interesting is that MyData derives Eq itself! Which, I suppose, must
> means that "Eq (MyData a)", and if it's true than "Eq a" is true,
> because how "MyData a" can be Eq without to be "Eq a" (with
> *automatically deriving* of Eq instance) ?!
>
> By the way, for Monoid (which is "* -> Constraint") I can add "Eq a"
> constraint without problems:
>
>   instance Eq a => Monoid (Origin a) where
>     mempty = NoMyData
>     mappend NoMyData a = a
>     mappend a NoMyData = a
>     mappend (MyData a) (MyData b)|a == b = MyData a
>                                  |otherwise = NoMyData
>
> but not for Alternative, which is "(* -> *) -> Constraint".
>
> *ORIGINAL ERROR DUMP*:
> ======================
>     42  16 error           error:
>                                • No instance for (Eq a) arising from a use of ‘==’
>                                  Possible fix:
>                                    add (Eq a) to the context of
>                                      the type signature for:
>                                        (<|>) :: Origin a -> Origin a -> Origin a
>                                • In the expression: a == b
>                                  In the expression: if a == b then NoOrigin else NoOrigin
>                                  In an equation for ‘<|>’:
>                                      a <|> b = if a == b then NoOrigin
>                                      else NoOrigin (intero)
>
>
> /Best regards
>   Paul
> _______________________________________________
> 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
Baa
Reply | Threaded
Open this post in threaded view
|

Re: Alternative instance w/ additional restriction

Baa
So, if I understood right, I'm trying to restrict instance more than
class allows. And only way to accomplish it - is to create own class
instead of Alternative which will not be based on
Functor :: (* -> *) -> *, like Alternative. No other workarounds?



В Wed, 7 Jun 2017 11:25:26 -0400
David McBride <[hidden email]> пишет:

> The Alternative class says nothing about the a in MyData a.  It only
> represents code relevant to MyData.
>
> When you see class Applicative f => Alternative (f :: * -> *), that
> means all of its functions had to work on any f, where f takes any
> type and becomes some other type which could be anything.
>
> The reason it works for Monoid is that class Monoid a where implies
> that a is completely known by the time the instance is fulfilled,
> therefore the instance can look at what a ended up being and ensure
> whatever a is, it must have this constraint on it.
>
> You can tell the difference because mempty returns a type that
> mentions the a mentioned in the class, whereas empty returns an a that
> is not mentioned in the class, therefore it has to work for any a.
>
> On Wed, Jun 7, 2017 at 8:26 AM, Baa <[hidden email]> wrote:
> > Hello all!
> >
> > If I try to write, for example:
> >
> >   instance Alternative MyData where
> >     empty = NoMyData
> >     a <|> b = if a == b then ...
> >
> > I get error (see on bottom of the mail) sure, bcz I suppose
> > something like:
> >
> >    Eq a => (MyData a)
> >
> > All my attempts to add something to instance's `pure` signature have
> > failed. How can I instantiate something with additional
> > restrictions, like in this case? Or are there another solutions for
> > such problem?
> >
> > Interesting is that MyData derives Eq itself! Which, I suppose, must
> > means that "Eq (MyData a)", and if it's true than "Eq a" is true,
> > because how "MyData a" can be Eq without to be "Eq a" (with
> > *automatically deriving* of Eq instance) ?!
> >
> > By the way, for Monoid (which is "* -> Constraint") I can add "Eq a"
> > constraint without problems:
> >
> >   instance Eq a => Monoid (Origin a) where
> >     mempty = NoMyData
> >     mappend NoMyData a = a
> >     mappend a NoMyData = a
> >     mappend (MyData a) (MyData b)|a == b = MyData a
> >                                  |otherwise = NoMyData
> >
> > but not for Alternative, which is "(* -> *) -> Constraint".
> >
> > *ORIGINAL ERROR DUMP*:
> > ======================
> >     42  16 error           error:
> >                                • No instance for (Eq a) arising
> > from a use of ‘==’ Possible fix:
> >                                    add (Eq a) to the context of
> >                                      the type signature for:
> >                                        (<|>) :: Origin a -> Origin
> > a -> Origin a • In the expression: a == b
> >                                  In the expression: if a == b then
> > NoOrigin else NoOrigin In an equation for ‘<|>’:
> >                                      a <|> b = if a == b then
> > NoOrigin else NoOrigin (intero)
> >
> >
> > /Best regards
> >   Paul
> > _______________________________________________
> > 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

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