当我撰写“演出”时会发生什么?和“读取”在Haskell中? [英] What is going on when I compose "show" and "read" in Haskell?

查看:113
本文介绍了当我撰写“演出”时会发生什么?和“读取”在Haskell中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是GHCi的简短记录:

  Prelude> :t读取
read ::读取a =>字符串 - > a
Prelude> :t show
show :: Show a => a - >字符串
前奏> :t show.read
show.read :: String - >字符串
前奏> (show.read)whales
***例外:Prelude.read:不分析

当我编写 show read 时,我只能假定GHC选择了一些任意类型阅读能够和显示可以是中间类型。


解决方案

GHCi是一种可以选择这种类型的产品,它有什么样的选择?违约规则说所选的类型是()。如果 Show 实例是缺省类型, GHCi将为一般约束选择(),为数字或整数约束选择整数,以及 Double 用于小数/其他真实约束。这不是由于Haskell内在的原因;它只是如何实现GHCi以便它可以轻松用作计算器。



如果您实际上已将代码输入到文件中并进行编译,那么更严格的GHC规则会被应用,并且你会得到一个错误,说中间类型无法解析。



当然,您可以指示GHC使用不同的类型通过赋予其中一个函数一个类型,例如:

  show。 (read :: String  - > Int)


Here's a short transcript from GHCi:

Prelude> :t read
read :: Read a => String -> a
Prelude> :t show
show :: Show a => a -> String
Prelude> :t show.read
show.read :: String -> String
Prelude> (show.read) "whales"
"*** Exception: Prelude.read: no parse

When I compose show and read I can only assume that GHC chose some arbitrary type which is both Readable and Showable to be the "intermediate" type.

How did it choose this type, and is there any way for me to find out what it is?

解决方案

The GHCi defaulting rules say that the chosen type is (). which is the default type that is chosen if a Show instance is demanded. GHCi will choose () for general constraints, Integer for numeric or integral constraints, and Double for fractional/other real constraints. This isn't due to some Haskell intrinsic; it's just how GHCi was implemented so that it can be used easily as a calculator.

If you had actually entered the code in a file and compiled it, the more strict GHC rules would have applied, and you would have gotten an error saying that the intermediate type cannot be resolved.

You can of course instruct GHC to use a different type by giving one of the functions a type, e.g.:

show . (read :: String -> Int)

这篇关于当我撰写“演出”时会发生什么?和“读取”在Haskell中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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