为什么没有( - >)默认使用Control.Monad.Instances实现 [英] Why isn't (->) implemented with Control.Monad.Instances by default

查看:136
本文介绍了为什么没有( - >)默认使用Control.Monad.Instances实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 LYAH 。它说我需要显式加载 Control.Monad.Instances 以获得以下语法:

 ((fmap(+5))(+5))4 

这是为什么?为什么如果仿函数是这种基础和统一的技术,我必须显式加载 Control.Monad.Instances 来获得该功能。 ( - >)没有它(或只是隐藏,只有 - > 导出)时如何实现?为什么不使用 fmap 来覆盖默认实现的函数类型? 解决方案

这里涉及3个不同的概念。函数类型,Functor类型类和Functor实例。函数类型( - >)嵌入语言中,其存在和实现与Functor和fmap完全无关。类型类是其关联方法的签名规范。 实例是该签名的一个实现。



所以为了让你的问题更清楚,我将它重新定义为为什么不是Functor实例的 - >)在Prelude中提供? (Prelude是默认情况下处于范围内的模块)。正如它目前的措词,它没有什么意义。



对修改后的问题的答案很简单:Haskell Report(官方语言规范,是指定Prelude的接口的地方)不包括它。可以说,它应该,但Haskell语言和库自那时起已经发展了很多,而Haskell标准往往变化缓慢。无论如何,对或错,这就是为什么。


I was reading LYAH. It says I need to explicitly load Control.Monad.Instances to get the following syntax to work:

( ( fmap (+5) ) (+5) ) 4

Why is that? Why if functors are this underlying and unifying technology do I have to explicitly load Control.Monad.Instances to get that functionality. How is (->) implemented without it (or is just hidden and only -> exported)? Why isn't the use of fmap over function types implemented by default?

解决方案

There are 3 different concepts involved here. The function type, the Functor type class, and the Functor "instance". The function type, (->), is built into the language and its existence and implementation are completely unrelated to Functor and fmap. The type class is the specification of the signature of its associated methods. The "instance" is an implementation of that signature.

So to make your question clearer, I would re-phrase it as "why isn't the Functor instance for (->) provided in the Prelude?" (the Prelude being the module that's in scope by default). As it is currently phrased, it doesn't make a lot of sense.

The answer to the modified question is simple: the Haskell Report (the official language specification, which is where the Prelude's interface is specified) doesn't include it. Arguably it should, but the Haskell language and libraries have evolved a lot since then, and Haskell standards tend to change slowly. In any case, right or wrong, that's why.

这篇关于为什么没有( - >)默认使用Control.Monad.Instances实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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