当向GHCI请求一种类型时,我往往会得到与理想类型相去甚远的类型(例如,函数签名中的内容不完全相同,或者语法上类似于文档化的库组合器类型)。
*Main Control.Monad.Shell> :t script
script :: Script f -> text-1.2.1.3:Data.Text.Internal.Lazy.Text
*Main Control.Monad.Shell> :t cmd "echo" "foo"
cmd "echo" "foo" :: CmdParams t => t为什么GHCI会显示隐藏的内部类型(甚至包版本),但是在第二种情况下,当包(可能)被设计为使用别名时,会显示一般类型?
基本上避免使用非平凡类型和查找文档的类型推断者,这是一个很好的实践吗?
一个更受欢迎的库示例:
*Main Control.Lens> :t _2
_2 :: (Functor f, Field2 s t a b) => (a -> f b) -> s -> f t
*Main Control.Lens> :t over
over :: Profunctor p => Setting p s t a b -> p a b -> s -> t
*Main Control.Lens> :t over _2
over _2 :: Field2 s t a b => (a -> b) -> s -> t发布于 2015-10-13 21:58:46
ghci如何描述该类型取决于:
下面是一种情况,您将看到一个内部类型而不是预期的导出类型:
=== file B.hs ===
module B (hello) where
import Data.Text
hello = pack "Hello, world"
=== file A.hs ===
module A where
import B然后运行ghci A.hs和命令:t hello将报告:
ghci> :t hello
hello :: Data.Text.Internal.Text但是,如果模块A导入Data.Text:
module A where
import B
import Data.Text然后,ghci以以下方式响应类型查询:
ghci> :t hello
hello :: Text至于显示在类型名称中的特定包名,如果在您的阴谋文件中没有显式地将text作为依赖项来提及,而是从另一个包导入Data.Text值,则可能会发生这种情况。
例如,假设上面的模块B是hello包的一部分,而模块A是单独包的一部分,比如uses-hello。
假设uses-hello.cabal文件如下所示:
=== file uses-hello.cabal ===
...
library:
exposed-modules: A
build-depends: base, hello
...请注意,uses-hello没有将text列为包依赖项,但它可以通过从hello包导入B来使用文本值。如果在cabal repl包中运行uses-hello,将得到如下输出:
$ cabal repl
*A> :t hello
hello :: text-1.2.1.1:Data.Text.Internal.Texthttps://stackoverflow.com/questions/33112477
复制相似问题