ghci randomio类型推断 [英] ghci randomio type inference

查看:92
本文介绍了ghci randomio类型推断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循 https://en.wikibooks.org/wiki/Haskell/Understanding_monads / State ,并且 randomIO 直接在 ghci 中打印一个整数。鉴于其类型是多态的, ghci 在这里如何知道它是 Int ?在 ghci 中有类型推断的特殊规则吗?

I am following https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State, and randomIO prints an integer in ghci directly. Given its type is polymorphic, how does ghci know it's Int here? Are there some special rules for type inference in ghci?

GHCi> :m System.Random
GHCi> :t randomIO
randomIO :: Random a => IO a
GHCi> randomIO
-1557093684
GHCi> randomIO
1342278538


推荐答案

简单的单态限制。多态类型,如 Num a =>如果没有指定实际类型,则被处理为像整数。也许这个规则也适用于 ghci ,你可以看到整型而不是一些未知类型的变量。

I suppose it is just simple Monomorphism restriction. Polymorphic types such as Num a => a are handled like Integer if actual type is not specified. Probably this rule also works in ghci and you see integer type instead of some unknown type variable.

UPD 1:实际上是包含在用户指南的这部分关于违约规则

UPD 1: actually the real answer contained under this part of user guide about defaulting rules.

UPD 2: < $ c> Random 类型类变得比我预期的更困难。因此,在这种情况下,由于默认(整数,双精度)声明,在报告中声明了违约规则。考虑下一个ghci会话

UPD 2: Case with Random type class turned to be more difficult than I expected. So in this case defaulting rules are resolved due to default (Integer, Double) declaration which is said in report. Consider next ghci session

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.41581766590151104

这篇关于ghci randomio类型推断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆