[Haskell-cafe] Merry monad mixup?

2011-01-28 Thread michael rice
The first and third work, but not the second. Why?

Michael

==

f :: String - IO ()
f s = do putStrLn s

{-
g :: [String] - IO ()
g l = do s - l
 putStrLn s
-}

{-
h :: [Int] - [Int]
h l = do i - l
 return (i+1)
-} 

==

serialize2.hs:29:9:
    Couldn't match expected type `[String]'
   against inferred type `IO String'
    In a stmt of a 'do' expression: s - l
    In the expression:
    do { s - l;
 putStrLn s }
    In the definition of `g':
    g l = do { s - l;
   putStrLn s }




  ___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Merry monad mixup?

2011-01-28 Thread Chris Smith
On Fri, 2011-01-28 at 11:20 -0800, michael rice wrote:
 The first and third work, but not the second. Why?

When you use a do block, it can be the syntactic sugar for whatever
monad you like; but you do have to make a choice.  Your first example
had a do block for the IO monad.  Your third example used the [] monad.
Both are fine.

The second, though, wasn't clear on what monad it was using.  When you
used the (-) syntax to nondeterministically choose from a list, the
compiler settled upon the [] monad.  But then the next line was a
statement in the IO monad.  That's inconsistent, hence the error.

Perhaps you wanted to build a monad out of both behaviors?  In this
case, you should likely look into monad transformers, and in particular,
the ListT monad transformer in the List package.  This would allow you
to write the code you did in a monad called ListT IO, except that the IO
actions would need to be lifted through the use of either `lift` or
`liftIO`.

-- 
Chris Smith


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Merry monad mixup?

2011-01-28 Thread Daniel Peebles
Beware of ListT. It only works if your internal monad is commutative, which
IO is not. (Maybe would work, for example)

On Fri, Jan 28, 2011 at 2:41 PM, Chris Smith cdsm...@gmail.com wrote:

 On Fri, 2011-01-28 at 11:20 -0800, michael rice wrote:
  The first and third work, but not the second. Why?

 When you use a do block, it can be the syntactic sugar for whatever
 monad you like; but you do have to make a choice.  Your first example
 had a do block for the IO monad.  Your third example used the [] monad.
 Both are fine.

 The second, though, wasn't clear on what monad it was using.  When you
 used the (-) syntax to nondeterministically choose from a list, the
 compiler settled upon the [] monad.  But then the next line was a
 statement in the IO monad.  That's inconsistent, hence the error.

 Perhaps you wanted to build a monad out of both behaviors?  In this
 case, you should likely look into monad transformers, and in particular,
 the ListT monad transformer in the List package.  This would allow you
 to write the code you did in a monad called ListT IO, except that the IO
 actions would need to be lifted through the use of either `lift` or
 `liftIO`.

 --
 Chris Smith


 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Merry monad mixup?

2011-01-28 Thread Daniel Peebles
And by works, I mean, ListT is is a monad only if the internal monad is
commutative.

On Fri, Jan 28, 2011 at 2:46 PM, Daniel Peebles pumpkin...@gmail.comwrote:

 Beware of ListT. It only works if your internal monad is commutative, which
 IO is not. (Maybe would work, for example)


 On Fri, Jan 28, 2011 at 2:41 PM, Chris Smith cdsm...@gmail.com wrote:

 On Fri, 2011-01-28 at 11:20 -0800, michael rice wrote:
  The first and third work, but not the second. Why?

 When you use a do block, it can be the syntactic sugar for whatever
 monad you like; but you do have to make a choice.  Your first example
 had a do block for the IO monad.  Your third example used the [] monad.
 Both are fine.

 The second, though, wasn't clear on what monad it was using.  When you
 used the (-) syntax to nondeterministically choose from a list, the
 compiler settled upon the [] monad.  But then the next line was a
 statement in the IO monad.  That's inconsistent, hence the error.

 Perhaps you wanted to build a monad out of both behaviors?  In this
 case, you should likely look into monad transformers, and in particular,
 the ListT monad transformer in the List package.  This would allow you
 to write the code you did in a monad called ListT IO, except that the IO
 actions would need to be lifted through the use of either `lift` or
 `liftIO`.

 --
 Chris Smith


 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Merry monad mixup?

2011-01-28 Thread michael rice
So, my suspicions are confirmed.

Thanks, all.

Michael

--- On Fri, 1/28/11, Daniel Peebles pumpkin...@gmail.com wrote:

From: Daniel Peebles pumpkin...@gmail.com
Subject: Re: [Haskell-cafe] Merry monad mixup?
To: Chris Smith cdsm...@gmail.com
Cc: michael rice nowg...@yahoo.com, haskell cafe 
haskell-cafe@haskell.org
Date: Friday, January 28, 2011, 2:47 PM

And by works, I mean, ListT is is a monad only if the internal monad is 
commutative.

On Fri, Jan 28, 2011 at 2:46 PM, Daniel Peebles pumpkin...@gmail.com wrote:

Beware of ListT. It only works if your internal monad is commutative, which IO 
is not. (Maybe would work, for example)


On Fri, Jan 28, 2011 at 2:41 PM, Chris Smith cdsm...@gmail.com wrote:

On Fri, 2011-01-28 at 11:20 -0800, michael rice wrote:

 The first and third work, but not the second. Why?



When you use a do block, it can be the syntactic sugar for whatever

monad you like; but you do have to make a choice.  Your first example

had a do block for the IO monad.  Your third example used the [] monad.

Both are fine.



The second, though, wasn't clear on what monad it was using.  When you

used the (-) syntax to nondeterministically choose from a list, the

compiler settled upon the [] monad.  But then the next line was a

statement in the IO monad.  That's inconsistent, hence the error.



Perhaps you wanted to build a monad out of both behaviors?  In this

case, you should likely look into monad transformers, and in particular,

the ListT monad transformer in the List package.  This would allow you

to write the code you did in a monad called ListT IO, except that the IO

actions would need to be lifted through the use of either `lift` or

`liftIO`.



--

Chris Smith





___

Haskell-Cafe mailing list

Haskell-Cafe@haskell.org

http://www.haskell.org/mailman/listinfo/haskell-cafe








  ___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe