On Thu, 28 Aug 1997, Johannes Waldmann wrote:

> If comprehensions are allowed for arbitrary monads,
> then [x] as an expression means "return, in some monad"
> while [x] as a type expression means "the list type".

I think this is a nuicanse too, I really haven't grasped the advantages of the
monad comprehension.
I mean for lists it is quite obvious that expressions like:

allPairs m n = [ (a,b) | a <- [1..m], b <- [1..n] ]

are good and readable. The [] suggest it has to do with lists.

Sure, lists can be defined as monads. This doesn't mean that every function
that works on lists works on monads. Maybe one should have an Container class
like:

class (Monad m) => Container m where
      size :: m a -> Integer -- or length?

instance Container [] where
      size = length

class (Container m) => OrderedContainer m where
      (!!) :: m a -> Int -> a
      head :: m a -> a
      tail :: m a -> m a

      and a few more...

and have

foldr :: (OrderedContainer m) => (a -> b -> b) -> b -> m a -> b

but I think that is stretching it too far.

What I mean to say is that even if some functions could be very polymorphic,
one need not make them that.

Take for instance an example where monad comprehension looks good.

ident :: Parser String

-- With monad comprehension
ident = [ x:xs | x <- lower, xs <- many alphanum ]

-- With do notation
ident = do x  <- lower
           xs <- many alphanum
           return (x:xs)

-- With ordinary monad operators
ident = lower         >>= \x ->
        many alphanum >>= \xs ->
        return (x:xs)

The uppermost example is clearly easy to read. However the [] can make it
confusing, especially for beginners. There are also a lot of things hidden,
a return and some >>=. This makes error messages hard to read, and I don't see
an easy way of fixing that.

The third example is almost as easy to read as the two first, and is core
Haskell.

>
> This is a discrepancy. I think it looks confusing
> to disambiguate  ['c']  by writing   ['c'] :: [Char].
> A naive spectator would say: Clearly 'c' :: Char,
> so why should putting brackets around both sides have any effect?
>

Is ['c'] really disambiguated?
this is not an instance of monad comprehension as a comprehension always
include a '|'.

Regarding comprehensions: hugs gives me an error for:
[a | a <- [10], b <- getLine ]
and says that getLine must be of type [a], but why? b is not used!

I agree that monad comprehension troubles a bit, and that is confusing.
It certainly should not be in Standard Haskell!

        n.

-----[ norpan ]---------[ [EMAIL PROTECTED] ]--------[ martin norb{ck ]-----
-----[ please be noted that the { is really a swedish letter but it is ]-----
-----[ unrepresentable in ascii. in iso8859-1 it looks like this: "a". ]-----




Reply via email to