为什么Haskell难以解决“超载"问题?操作员? [英] Why has Haskell troubles resolving "overloaded" operators?

查看:74
本文介绍了为什么Haskell难以解决“超载"问题?操作员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章提出了以下问题: !!.接受的答案告诉我们,您实际上正在做的是创建一个新函数!!,然后您应该避免导入标准函数.

This post poses the question for the case of !! . The accepted answer tell us that what you are actually doing is creating a new function !! and then you should avoid importing the standard one.

但是,如果要将新功能应用于与标准功能不同的类型,为什么要这样做呢?编译器难道不能根据其参数选择合适的代码吗? 是否有任何编译器标志允许这样做?

But, why to do so if the new function is to be applied to different types than the standard one? Is not the compiler able to choose the right one according to its parameters? Is there any compiler flag to allow this?

例如,如果未为[Float] * Float定义*

为什么编译器会哭


>  Ambiguous occurrence *
>  It could refer to either `Main.*', defined at Vec.hs:4:1
>                          or `Prelude.*',

此代码:

(*) :: [Float] -> Float -> [Float]
(*) as k = map (\a -> a*k) as  -- here: clearly Float*Float


r = [1.0, 2.0, 3.0] :: [Float]

s = r * 2.0 -- here: clearly [Float] * Float

main = do
     print r
     print s

推荐答案

这是一项设计决策,而不是理论问题,不包括在Haskell中.如您所说,许多其他语言都使用类型以特殊方式消除术语间的歧义.但是类型类具有类似的功能,并且还允许对重载的事物进行抽象.类型导向的名称解析没有.

It is a design decision, not a theoretical problem, not to include this in Haskell. As you say, many other languages use types to disambiguate between terms on an ad-hoc way. But type classes have similar functionality and additionally allow abstraction over things that are overloaded. Type-directed name resolution does not.

尽管如此,已针对Haskell讨论了类型定向名称解析的形式(例如,在解决记录字段选择器的上下文中),并且类似于Haskell的某些语言也支持这种语言,例如Agda(用于数据构造函数)或Idris(更多)一般来说).

Nevertheless, forms of type-directed name resolution have been discussed for Haskell (for example in the context of resolving record field selectors) and are supported by some languages similar to Haskell such as Agda (for data constructors) or Idris (more generally).

这篇关于为什么Haskell难以解决“超载"问题?操作员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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