Hi, Today, if I write: [a:[b] | a<-"ab" , b<-"12"] I get: ["a1","a2","b1","b2"] Are there any guarantees that I'll never get ["a1","b1","a2","b2"] instead, i.e., that the first list will always be the last one to be fully transversed? Even if I use a different compiler or a future version of Haskell? Reading how list comprehensions are translated in the Haskell report it seems the answer is yes. Is that written in stone? Can compilers do it in their own different way? Thanks, Maurício
On Thu, 2007-10-25 at 19:59 -0200, Maurício wrote:
Hi,
Today, if I write:
[a:[b] | a<-"ab" , b<-"12"]
I get:
["a1","a2","b1","b2"]
Are there any guarantees that I'll never get ["a1","b1","a2","b2"] instead, i.e., that the first list will always be the last one to be fully transversed? Even if I use a different compiler or a future version of Haskell?
Reading how list comprehensions are translated in the Haskell report it seems the answer is yes.
Correct.
Is that written in stone?
Yes. It's a consequence of the MonadPlus law (for [] and other non-determinism monads) (xn `mplus` ys) >>= f = (xn >>= f) `mplus` (ys >>= f) which implies [ f x y | x <- xn ++ xn', y <- ys] = [ f x y | x <- xn, y <- ys] ++ [ f x y | x <- xn', y <- ys] (This rule plus the monad laws plus the natural transformation law for return map f (return x) = return (f x) provides a complete calculation system for list comprehensions, btw. And those laws are all very much set in stone.)
Can compilers do it in their own different way?
No. jcc
If I understand list comprehensions correctly, what you wrote is the same as do a <- "ab"; b <- "12"; [a:[b]] which is the same as "ab" >== \a -> do b <- "12"; [a:[b]] which is the same as "ab" >>= \a -> "12" >>= \b -> [a:[b]] which is the same as concat $ map ( \a -> "12" >>= \b -> [a:[b]] ) "ab" .... enough desugaring for now Point is, yes it's written in stone. List comprehensions is just syntactic sugar for monad operations. Good exercise is to take the above expressions and add parenthesis to make it easier to understand order of operations. (Still trips me up often enough). Thomas. Maurício <briqueabraque@yahoo.com> Sent by: haskell-cafe-bounces@haskell.org 10/25/2007 05:59 PM To haskell-cafe@haskell.org cc Subject [Haskell-cafe] List comprehension order of evaluation Hi, Today, if I write: [a:[b] | a<-"ab" , b<-"12"] I get: ["a1","a2","b1","b2"] Are there any guarantees that I'll never get ["a1","b1","a2","b2"] instead, i.e., that the first list will always be the last one to be fully transversed? Even if I use a different compiler or a future version of Haskell? Reading how list comprehensions are translated in the Haskell report it seems the answer is yes. Is that written in stone? Can compilers do it in their own different way? Thanks, Maurício _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe --- This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
participants (3)
-
Jonathan Cast -
Maurício -
Thomas Hartman