为什么GHCi奇怪地输入这个陈述? [英] Why is GHCi typing this statement oddly?
问题描述
在回答关于stackoverflow的问题时,我注意到GHCi(交互式)在 let 语句中指定了限制过多的类型。也就是说,给定代码,
import Control.Arrow
f = maximum&&& id>>> fst&&& (\(m,l) - > length $ filter(== m)l)
(正如我对 https://的答案stackoverflow.com/questions/6281813/maximum-of-list-and-count-of-repeat-maximum-number/6283594#6283594 ),如果在 f之前插入let
并在ghci中输入,它给出以下类型信息:
Prelude Control.Arrow> :t f
f :: [()] - > ((),Int)
只要求表达式的类型给出正确的结果,即 Ord a => [a] - > (a,Int)
。我使用的是ghc 7.0.3。 扩展违约规则,用于解释() code>来自
至于为何在这种情况下发生违约,请比较以下内容:
>让f x =最大&&& id>>> fst&&& (\(m,l) - >长度$ filter(== m)l)$ x
> :t f
f ::(Ord a)=> [a] - > (a,Int)
我认为这与绑定是单形的有关,但我不是特定的细节。
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.
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.
这篇关于为什么GHCi奇怪地输入这个陈述?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!