2
votes

Reasoning about an error given in Haskell type error on compilation I found a strange behaviour of GHCi

test.hs

members :: String -> Bool;
members str = and [ b | x <- str, b <- map (elem x) "abcde" ]

produces the following error message

Prelude> :l test.hs
[1 of 1] Compiling Main             ( test.hs, interpreted )
test.hs:2:53: error:
    • Couldn't match type ‘Char’ with ‘t0 Char’
      Expected type: [t0 Char]
        Actual type: [Char]
    • In the second argument of ‘map’, namely ‘"abcde"’
      In the expression: map (elem x) "abcde"
      In a stmt of a list comprehension: b <- map (elem x) "abcde"

whereas

GHCi

Prelude> members :: String -> Bool; members xs = and [ b | x <- xs, b <- map (elem x) "abcde"]

produces

<interactive>:18:78: error:
    • Couldn't match type ‘Char’ with ‘[Char]’
      Expected type: [[Char]]
        Actual type: [Char]
    • In the second argument of ‘map’, namely ‘"abcde"’
      In the expression: map (elem x) "abcde"
      In a stmt of a list comprehension: b <- map (elem x) "abcde"

Update

What I also do not understand, is why GHC omits the Foldable t0 class constraint - I would have expected the error message be something like:

test.hs:2:53: error:
    • Couldn't match type ‘Char’ with ‘t0 Char’
      Expected type: Foldable t0 => [t0 Char]
        Actual type: [Char]
    • In the second argument of ‘map’, namely ‘"abcde"’
      In the expression: map (elem x) "abcde"
      In a stmt of a list comprehension: b <- map (elem x) "abcde"
1

1 Answers

2
votes

Introduced by the FTP (Foldable-Traversable-Proposal) function elem has gotten a new type signature.

elem :: (Eq a, Foldable t) => a -> t a -> Bool

And it seems GHCi uses the ExtendedDefaultRules extension which can be deactivated by

:set -NoExtendedDefaultRules

This extension and trac:10971 makes Foldable and Traversable default to [].

update

The class constraint is not shown, because the type checking for it is done at a later stage.