9

In answering a question on stackoverflow, I noticed that GHCi (interactive) is assigning a too-restrictive type in a let statement. Namely, given the code,

import Control.Arrow
f = maximum &&& id >>> fst &&& (\(m,l) -> length $ filter (==m) l)

(as on my answer to https://stackoverflow.com/questions/6281813/maximum-of-list-and-count-of-repeat-maximum-number/6283594#6283594), if one inserts a "let" before f and enters this in ghci, it gives the following type information

Prelude Control.Arrow> :t f
f :: [()] -> ((), Int)

whereas just asking for the type of the expression gives the correct result, namely Ord a => [a] -> (a, Int). I'm using ghc 7.0.3.

Community
  • 1
  • 1
gatoatigrado
  • 16,580
  • 18
  • 81
  • 143

1 Answers1

10

See the extended defaulting rules used in GHCi for an explanation of where the () is coming from.

As for why the defaulting occurs in this case, compare the following:

> let f x = maximum &&& id >>> fst &&& (\(m,l) -> length $ filter (==m) l) $ x
> :t f
f :: (Ord a) => [a] -> (a, Int)

I assume this has something to do with bindings being monomorphic, but I'm not certain of the details.

C. A. McCann
  • 76,893
  • 19
  • 209
  • 302
  • 3
    Indeed it does. Running GHCi with `-XNoMonomorphismRestriction` gives the same result. – hammar Jun 08 '11 at 19:24
  • @hammar: Thanks, good catch! For some reason it didn't occur to me to try that and I wasn't sure whether GHCi might have other special behavior for bindings in the REPL. – C. A. McCann Jun 08 '11 at 19:27