2013-03-16 98 views
7

ghci周围玩,我得到了下面的表达式后不签名变化。但现在,如果我尝试将它分配给了一些名字,我得到比原来的更具体的签名:为什么分配

> let f = unlines . map (\(a,b) -> show a ++ " " ++ show b) 
> :t f 
f :: [((),())] -> String 

为什么会出现这种情况?

+1

另见:[1](http://stackoverflow.com/questions/7055146),[2](http://stackoverflow.com/questions/11439163),[3](http:// stackoverflow。问题/ 9714697),[4](http://stackoverflow.com/questions/8434808),[5](http://stackoverflow.com/questions/7799345),[6](http:// stackoverflow .com/questions/8262020),[7](http://stackoverflow.com/questions/8655900),[8](http://stackoverflow.com/questions/11003535)。我真的不确定哪些(如果有的话)这些标记为重复的。 – 2013-03-16 22:40:14

回答

12

由于monomorphism restriction,形式x = ...(无参数)的定义被给定为单形(即非多形)类型,其通常涉及一些违约,如in the other answer所述。

为防止发生这种情况,请在定义中添加类型签名,或使用:set -XNoMonomorphismRestriction禁用单态限制。您可以将其添加到您的.ghci file,以使其在启动时自动运行,直到it gets disabled by default in GHCi in some future version

+1

或eta扩大;即'let f x = unlines。 map(\(a,b) - > show a ++“”++ show b)$ x' – luqui 2013-03-16 17:44:27

4

违约规则。

当您在GHCi中输入东西时,它会尝试应用默认类型。 IIRC,对于Num约束条件,它选择Integer,对于Fractional它选择Double,对于其他所有选项()

如果你在一个Haskell源代码文件中写入并将其加载到GHCi中,这不会发生(我相信)。

我想你也可以说一些类似default Int来改变每个模块的默认规则。