我有以下
readStatement :: String -> IO [Transaction]
readStatement path = do
csvData <- readFile path
return $ catMaybes (map _ (splitOn "\r" csvData))
GHC找到以下类型的孔[Char] -> Maybe Transaction
。现在,如果我给这个孔取一个名字并移到where子句中
readStatement :: String -> IO [Transaction]
readStatement path = do
csvData <- readFile path
return $ catMaybes (map process (splitOn "\r" csvData))
where process = _
GHC找不到新行的类型,但给出了:
Found hole ‘_’ with type: t
Where: ‘t’ is a rigid type variable bound by
the inferred type of process :: t at Statement.hs:63:11
Relevant bindings include
process :: t (bound at Statement.hs:63:11)
path :: String (bound at Statement.hs:60:15)
readStatement :: String -> IO [Transaction]
(bound at Statement.hs:60:1)
In the expression: _
In an equation for ‘process’: process = _
In an equation for ‘readStatement’:
readStatement path
= do { csvData <- readFile path;
return $ catMaybes (map process (splitOn "\r" csvData)) }
where
process = _
该代码严格等效,因此类型推断为什么不会给出相同的结果?(我正在使用GHC 7.8.3)
在下面的代码中
let x = something
in f x
类型推断x = something
不考虑f
。也就是说,something
推断的类型,x
将其概括为多态类型,然后获取该类型。这可能会分配x
比所需的类型更通用的类型f
。
更具体地说,在
let x = \y->y
in x "hello"
我们推断出多态结合x :: forall a. a->a
。请注意,这String
还没有出现。
我猜是在对let
定义执行类型推断之后,GHC会在那时打印类型孔信息。在发布的示例中,
do one
two
where process = _
我们推断时_ :: t
无需查看上下文并打印该类型。如果您想要更专业的(单态)类型,请尝试
(\process -> do
one
two
) _
在上面,我们不使用let
或where
,因此仅推断单态类型。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句