Haskell 并行和广义(类似 SQL)列表理解问题

发布于 2024-11-05 17:42:16 字数 845 浏览 0 评论 0原文

也许我误读了文档(http://www.haskell.org/ghc/docs/7.0.1/html/users_guide/syntax-extns.html#parallel-list-compregnions),但在下面的代码中我期望列表推导式 zs 和 zs' 具有相同的值。然而,它们是不同的,如主打印两行不同的行所示:

{-# LANGUAGE ParallelListComp, TransformListComp #-}
import GHC.Exts

xs = [10,20..90]
ys = map (`mod`7) xs

zs = [(x,y) | x<-xs | y<-ys, then sortWith by y]
zs' = [(x,y) | (x,y) <- zip xs ys, then sortWith by y]

main = print zs >> print zs'

我只是误读了文档,还是有一些更糟糕的问题?我很惊讶类型系统没有捕获从中抽象出来的实际代码中的错误。

产生此输出:

*Main> main
[(10,0),(20,1),(30,2),(40,3),(50,3),(60,4),(70,5),(80,6),(90,6)]
[(70,0),(50,1),(30,2),(10,3),(80,3),(60,4),(40,5),(20,6),(90,6)]

谢谢。

Maybe I'm misreading the docs (http://www.haskell.org/ghc/docs/7.0.1/html/users_guide/syntax-extns.html#parallel-list-comprehensions) but in the following code I'd expect the list comprehensions zs and zs' to have the same value. However, they are different, as shown by main printing two different lines:

{-# LANGUAGE ParallelListComp, TransformListComp #-}
import GHC.Exts

xs = [10,20..90]
ys = map (`mod`7) xs

zs = [(x,y) | x<-xs | y<-ys, then sortWith by y]
zs' = [(x,y) | (x,y) <- zip xs ys, then sortWith by y]

main = print zs >> print zs'

Am I simply misreading the docs, or is there some worse problem? I'm surprised the type system didn't catch the error in the actual code this was abstracted from.

This output is produced:

*Main> main
[(10,0),(20,1),(30,2),(40,3),(50,3),(60,4),(70,5),(80,6),(90,6)]
[(70,0),(50,1),(30,2),(10,3),(80,3),(60,4),(40,5),(20,6),(90,6)]

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作业与我同在 2024-11-12 17:42:16

唐斯,谢谢,现在我不确定我做了什么才能产生正确的输出。是的,我的评论中的代码是一个语法错误(我无法修复它或在那里发表评论,因为我不再有浏览器 cookie,叹气)。除了使用“zip”之外,我没有看到任何方法可以解决这个问题,这有点令人失望,但是哦,好吧。 (哦,看起来我可以将编辑放入队列中,这与 mathoverflow 不同)。

Dons, thanks, now I'm not sure what I did that made the right output. Yes that code in my comment is a syntax error (and I can't fix it or comment there because I don't have the browser cookie any more, sigh). I don't see any way to fix this except with "zip", which is a bit disappointing, but oh well. (Oh, looks like I can put edits into a queue, unlike on mathoverflow).

凉城已无爱 2024-11-12 17:42:16

看来 Solrize 的评论是正确的。第一个结果是因为横杠比逗号关联更松散。这记录在“并行列表理解”部分下的 dons 链接中。不知道对此能做什么...

下面的内容非常丑陋((x,y)的重复绑定),但顺便修复了 solrize 的语法错误...

zs <- [(x,y) | (x,y) <- [(x,y) | x<-xs | y<-ys], then sortWith by y]

理想的方法是用括号括起来 并行列表推导式的 | 部分,如下所示:

zs = [(x,y) | (x<-xs | y<-ys), then sortWith by y]

Solrize's comment is correct, it seems. The first result is because the bar associates more loosely than the comma. This is documented in dons' link under the "Parallel List Comprehensions" section. Not sure what can be done about this...

The following is terribly ugly (duplicate bindings of (x,y)) but fixes solrize's syntax error by the way...

zs <- [(x,y) | (x,y) <- [(x,y) | x<-xs | y<-ys], then sortWith by y]

What would be ideal would be some way to parenthesize | sections of parallel list comprehensions, such as the following:

zs = [(x,y) | (x<-xs | y<-ys), then sortWith by y]
著墨染雨君画夕 2024-11-12 17:42:16

对我来说看起来像一个错误。至少是未能清楚地传达句法翻译是什么。 solrize 提供的链接指出:

给出形式的并行理解:

[ e | p1 <- e11, p2 <- e12, ... 
    | q1 <- e21, q2 <- e22, ... 
     ... 
 ] 

这将被翻译为:

[ e | ((p1,p2), (q1,q2), ...) <- zipN [(p1,p2) | p1 <- e11, p2 <- e12, ...] 
                                      [(q1,q2) | q1 <- e21, q2 <- e22, ...] 
                                      ... 

直觉会将 ,然后 sortWith by y 放在最后的省略号中:

[ (x,y) | x <- xs
        | y <- ys
        , then sortWith by y ]

这将翻译为

-- this produces the same result as your zs'
[ (x,y) | (x,y) <- zip [x' | x' <- xs]
                       [y' | y' <- ys]
                       , then sortWith by y ]

然而,它实际上似乎放在倒数第二个省略号位置:

[ (x,y) | x <- xs
        | y <- ys, then sortWith by y
        ]

翻译为

-- this produces the same result as your zs
[ (x,y) | (x,y) <- zip [x' | x' <- xs]
                       [y' | y' <- ys, then sortWith by y']
                       ]

Looks like a bug to me. It is at minimum a failure to communicate clearly what the syntactic translation is. solrize's provdied link states:

Given a parallel comprehension of the form:

[ e | p1 <- e11, p2 <- e12, ... 
    | q1 <- e21, q2 <- e22, ... 
     ... 
 ] 

This will be translated to:

[ e | ((p1,p2), (q1,q2), ...) <- zipN [(p1,p2) | p1 <- e11, p2 <- e12, ...] 
                                      [(q1,q2) | q1 <- e21, q2 <- e22, ...] 
                                      ... 

Intuition would put the , then sortWith by y in the final ellipsis:

[ (x,y) | x <- xs
        | y <- ys
        , then sortWith by y ]

Which would translate to

-- this produces the same result as your zs'
[ (x,y) | (x,y) <- zip [x' | x' <- xs]
                       [y' | y' <- ys]
                       , then sortWith by y ]

However, it actually seems to go in the second-to-last ellipsis spot:

[ (x,y) | x <- xs
        | y <- ys, then sortWith by y
        ]

Translating to

-- this produces the same result as your zs
[ (x,y) | (x,y) <- zip [x' | x' <- xs]
                       [y' | y' <- ys, then sortWith by y']
                       ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文