Skip to content

Pattern match translation suboptimal

Compile the following code and look at the (sad) intermediate code:

baz :: Integer -> Int -> Int
baz 10 1 = 1
baz 20 1 = 2
baz 10 2 = 2
baz 20 2 = 3
baz 10 3 = 1
baz 20 3 = 2
baz 10 4 = 2
baz 20 4 = 3
baz _ _  = 0

The pattern match compiler has not rearranged the clauses, and so it produces an 8 level deep nested test.

Now change the type signature to

baz :: Int -> Int -> Int

Now the pattern match compiler does its job and rearranges the clauses to make the tests 2 levels deep.

The same phenomenon happens when matching string literals. For the predefined String type the right thing happens, but for some other string type (using OverloadedStrings) it doesn't.

Trac metadata
Trac field Value
Version 7.8.3
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information