Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is GHCi typing this statement oddly?

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.

like image 533
gatoatigrado Avatar asked Jun 08 '11 18:06

gatoatigrado


1 Answers

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.

like image 180
C. A. McCann Avatar answered Sep 20 '22 12:09

C. A. McCann