简体   繁体   English

GHC在编译模式时会做什么样的迭代?

[英]What sort of iterations does GHC do while compiling patterns?

I just wrote a function for making a move in Tic-Tac-Toe. 我刚刚写了一个在Tic-Tac-Toe中移动的功能。 I wanted to push pattern matching. 我想推动模式匹配。 So I wrote 9 makeAMove clauses. 所以我写了9个makeAMove子句。 Each has a Tic-Tac-Toe board with a different space designated by an Empty symbol. 每个都有一个Tic-Tac-Toe板,其空间由空符号指定。 It looks something like this. 它看起来像这样。

makeAMove [[E,   m12, m13],
           [m21, m22, m23],
           [m31, m32, m33]] X 1 1 = ...

This clause would put an X in the upper left corner of the board. 该子句将X放在板的左上角。 X, O, and E are defined as Marks. X,O和E定义为标记。

data Mark = X |  O | E deriving (Eq, Show)

When I load the file I get this warning message. 当我加载文件时,我收到此警告消息。

warning:
    Pattern match checker exceeded (2000000) iterations in
    an equation for ‘mov1’. (Use -fmax-pmcheck-iterations=n
    to set the maximun number of iterations to n)

My question is one of curiosity. 我的问题是好奇心。 What sort of iterations is the pattern matcher doing? 模式匹配器在做什么样的迭代? And why are so many required? 为什么需要这么多?

When I limit the number of clauses to, say, 5 and put the rest in another function that is linked to by a default case, there is no problem. 当我将条款数量限制为5,并将其余条款放在另一个由默认情况链接的函数中时,没有问题。

Here's a MCVE: 这是一个MCVE:

{-# OPTIONS -Wall #-}
data T = O | A | B | C | D | E

f :: T -> T -> T -> T -> T -> T -> T -> T -> T -> ()
f O _ _ _ _ _ _ _ _ = ()
f _ O _ _ _ _ _ _ _ = ()
f _ _ O _ _ _ _ _ _ = ()
f _ _ _ O _ _ _ _ _ = ()
f _ _ _ _ O _ _ _ _ = ()
f _ _ _ _ _ O _ _ _ = ()
f _ _ _ _ _ _ O _ _ = ()
f _ _ _ _ _ _ _ O _ = ()
f _ _ _ _ _ _ _ _ O = ()

Result: 结果:

ExhaustivePattern3.hs:5:5: warning:
    Pattern match checker exceeded (2000000) iterations in
    an equation for ‘f’. (Use -fmax-pmcheck-iterations=n
    to set the maximun number of iterations to n)

I guess the checker is trying to generate the counterexamples too eagerly: there are many unmatched patterns, exponentially growing with the number of arguments. 我猜测试者正试图过于急切地产生反例:有许多不匹配的模式,随着参数的数量呈指数增长。

Indeed, after the first match the unmatched cases are 事实上,在第一场比赛之后,无与伦比的情况就是如此

A _ _ _ _ _ _ _ _
B _ _ _ _ _ _ _ _
...
E _ _ _ _ _ _ _ _

Then after the second one, this expands to: 然后在第二个之后,这扩展到:

A A _ _ _ _ _ _ _
A ...
A E _ _ _ _ _ _ _
B A _ _ _ _ _ _ _
B ...
B E _ _ _ _ _ _ _
...
E A _ _ _ _ _ _ _
E ...
E E _ _ _ _ _ _ _

And so on. 等等。 This grows exponentially. 这呈指数增长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM