Why is GHCi typing this statement oddly?

别说谁变了你拦得住时间么 提交于 2019-12-01 00:17:17

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6283681/why-is-ghci-typing-this-statement-oddly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!