What kind of iterations do the GHC do when compiling templates?

I just wrote a function to move to Tic-Tac-Toe. I wanted to push the pattern. So I wrote 9 makeAMove . Each of them has a Tic-Tac-Toe board with a different space, indicated by a blank symbol. It looks something like this.

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

At this point, X will be placed in the upper left corner of the board. X, O, and E are defined as labels.

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

When I upload a file, I get this warning.

 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 kind of iteration does the template perform? And why is so much required?

When I limit the number of sentences to, say, 5, and the rest is in another function related to the default case, there is no problem.

+7
pattern-matching haskell ghc
source share
1 answer

Here's 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 think checker is trying to generate counterexamples too eagerly: there are many unsurpassed patterns that grow exponentially with the number of arguments.

Indeed, after the first match, inconsistent cases

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

Then after the second, it expands to:

 AA _ _ _ _ _ _ _ A ... AE _ _ _ _ _ _ _ BA _ _ _ _ _ _ _ B ... BE _ _ _ _ _ _ _ ... EA _ _ _ _ _ _ _ E ... EE _ _ _ _ _ _ _ 

And so on. It is growing exponentially.

+8
source share

All Articles