我跟随https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State,randomIO直接在ghci中打印一个整数。考虑到它的类型是多态的,ghci怎么知道它是Int呢?在ghci中是否有一些特殊的类型推断规则
GHCi> :m System.Random
GHCi> :t randomIO
randomIO :: Random a => IO a
GHCi> randomIO
-1557093684
GHCi> randomIO
1342278538发布于 2016-09-01 00:21:01
我想它只是一个简单的Monomorphism restriction。如果未指定实际类型,则像Integer一样处理Num a => a等多态类型。也许这个规则也适用于ghci,你看到的是整型而不是一些未知类型的变量。
更新1:实际上是关于defaulting rules的这部分用户指南中包含的真正答案。
更新2:带有Random类型类的用例变得比我预期的更难。因此,在这种情况下,默认规则由于报告中所说的default (Integer, Double)声明而被解决。考虑下一次ghci会议
Prelude System.Random> default ()
Prelude System.Random> randomIO
<interactive>:6:1:
No instance for (Show (IO a0)) arising from a use of ‘print’
In a stmt of an interactive GHCi command: print it
Prelude System.Random> default (Integer)
Prelude System.Random> randomIO
-7948113563809442883
Prelude System.Random> default (Double)
Prelude System.Random> randomIO
0.41581766590151104https://stackoverflow.com/questions/39251728
复制相似问题