GHCi忽略类型签名 [英] GHCi ignores type signature
问题描述
前奏>让myprint = putStrLn。显示
前奏> :t myprint
myprint ::() - > IO()
好的,这里没什么特别的。只是GHCi类型的违约规则,我猜... ...
Prelude> let myprint =(putStrLn。show):: Show x => x - > IO()
前奏> :t myprint
myprint ::() - > IO()
这是什么巫术?你是空白的忽略我的类型声明?! O_O
有什么方法可以说服GHCi去做我真正想做的事情? / div>
我们可以做到以下几点,对单态限制: > let myprint :: Show x = > x - > IO(); myprint = putStrLn。显示
>:t myprint
myprint ::显示x => x - > IO()
这与 let myprint = putStrLn不一样。 show :: Show x => x - > IO()
。在前一种情况下,我们使用类型签名进行绑定,在后一种情况下,我们使用右侧内的类型注释来绑定 let
。 Monomorphism检查顶级类型签名,但不检查本地注释。
Prelude> let myprint = putStrLn . show
Prelude> :t myprint
myprint :: () -> IO ()
OK, nothing too unusual here. Just GHCi type defaulting rules, I guess...
Prelude> let myprint = (putStrLn . show) :: Show x => x -> IO ()
Prelude> :t myprint
myprint :: () -> IO ()
What sorcery is this?? You're point-blank ignoring my type declaration?! O_O
Is there some way I can convince GHCi to do what I actually intended?
We can do the following, with monomorphism restriction on:
>let myprint :: Show x => x -> IO (); myprint = putStrLn . show
>:t myprint
myprint :: Show x => x -> IO ()
This is not the same as let myprint = putStrLn . show :: Show x => x -> IO ()
. In the former case we have a binding with a type signature, in the latter case we a have a let
binding with a type annotation inside the right hand side. Monomorphism checks top-level type signatures, but not local annotations.
这篇关于GHCi忽略类型签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!