如果用作Show类型类的实例 [英] If functions as instances of the Show typeclass

查看:98
本文介绍了如果用作Show类型类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数不是Show类型类的实例,所以不能以简洁的形式看到它。但是编译器在某个地方返回失败。

那么为什么不能显示它,以及它可能是什么样的?
如果你能拿一些简单的例子来说明这一点,我将非常感激。

解决方案

Haskell是一种编译语言。函数在内部只是代码。它对 Show 它们没有任何意义。即使这是可能的,也可能并不需要,因为它很可能意味着扩展平等的函数具有不同的 String 表示,并且/或者编译器优化会是可以在字符串中生成。



您可以执行的唯一操作是:定义虚拟表示所有函数,或为具有小域的特定函数类型定义特定实例(例如,类型为 Bool - > Bool Bool - > Int ),仍然可以从外部完全观察到。


Functions aren't instances of the Show typeclass, so one cannot see it in neat form. But compiler has it somewhere on which it returns fails.

So why it is not possible to show it and if it can be what it will look like? I shall be thankful if you could take some trivial example to state this.

解决方案

Haskell is a compiled language. Functions are, internally, just code. It doesn't really make sense to Show them. Even if it was possible, it'd probably not be desirable, because it would most likely mean that extensionally equal functions have different String representations, and/or that compiler optimisations would be visible in the String that is being generated.

The only things you can do are: define a dummy representation for all functions, or define a specific instance for specific function types with small domains (say, functions of type Bool -> Bool or Bool -> Int) that can still easily be observed completely from the outside.

这篇关于如果用作Show类型类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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