【问题标题】:Haskell type inference priority?Haskell 类型推理优先级?
【发布时间】:2016-03-28 21:15:46
【问题描述】:

据我所知,haskell 推断出最普遍的可能类型,即

integers      = [1,2,3]
integersOrNot = [1,2,3]

routine :: IO ()
routine = do
   let x = map (\i -> i / 2) integersOrNot
   return ()

当我测试integersOrNot 的类型时,我得到了

*Main> :t integersOrNot
integers_ :: [Double]

给定

*Main> :t 1
1 :: Num a => a

Haskell 没有太多选择。但是怎么会

*Main> :t integers
integers :: [Integer]

不是integers :: (Num a) => [a]?这是否意味着Integer 是Num 种类的“默认”真实类型?

【问题讨论】:

  • wiki.haskell.org/MonomorphismRestriction。顺便说一句,Haskell 并没有真正“以惰性方式推断类型”,而是总是推断 最一般的可能类型。
  • @leftaroundabout,仅当存在最通用的类​​型时。我似乎记得有一些奇怪的情况,即使存在最通用的类​​型,它也会推断出不那么通用的东西,但我可能会感到困惑,或者这可能只适用于某些 GHC 扩展。
  • @dfeuer:是的,它不会推断出更一般的 rank-N 类型,但它会给出最一般的 rank-1 类型(前提是您关闭了单态限制)。
  • @dfeuer 这里显示了一些推理失败的情况:stackoverflow.com/questions/27067905/…

标签: haskell type-inference


【解决方案1】:

http://wiki.haskell.org/MonomorphismRestriction

正如@leftaroundabout 建议的那样

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 2012-09-15
    • 1970-01-01
    • 2016-12-17
    • 2010-11-10
    • 2011-12-29
    • 1970-01-01
    相关资源
    最近更新 更多