gpt4 book ai didi

Haskell 元组构造函数 (GHC) 以及语言与其实现之间的分离

转载 作者:行者123 更新时间:2023-12-02 06:57:02 25 4
gpt4 key购买 nike

当我意识到这一点时,Haskell 再次让我大吃一惊

(x,y)

只是语法糖

(,) x y

当然,我想将其扩展到更大的元组。但是

(,) x ((,) y z)

给了我

(x,(y,z))

这不是我想要的。一时兴起,我尝试了一下

(,,) x y z

它成功了,给出了我想要的:

(x,y,z)

这就提出了一个问题:你能走多远?令我惊讶的是,似乎没有限制。以下所有操作符都是有效的操作符:

(,)
(,,)
(,,,)
(,,,,)
--etc
(,,,,,,,,,,,,,,)
(,,,,,,,,,,,,,,,)
--etc
(,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)
--etc

这种行为令人惊奇,并引发了我的实际问题:它可以在我自己的函数中模拟吗?或者它只是元组运算符的 GHC 特定功能?我认为是后者,因为我已经阅读了 haskell98 规范,并且 iirc 说实现只需为最多 15 个项目定义元组运算符。而 GHC 则全力以赴,让您可以达到任意限制。

那么,是否有可能从 haskell 实现本身中定义这一族运算符/函数,只使用类型系统和现有语言功能(声明、类型签名、函数定义等)?如果是这样,怎么办?或者这是不可能的,你必须查看编译器来找到这个函数集合的支持框架?

这引出了一个更普遍的问题:Haskell 本身通过类型和函数定义、声明等支持了多少 Haskell 功能;编译器/实现支持多少? (我知道 GHC 是用 Haskell 编写的,但这并不能回答问题)

也就是说,如果你要放弃标准库(包括前奏)并在原始 Haskell 中从头开始做所有事情;是否有可能仅使用最少的功能集来构建具有 GHC 所有功能的完整实现?为了使用 Haskell 构建 haskell 实现,您需要的最小语言功能集是什么?我是否能够放弃前奏,然后从 GHC 中完全重建它手动?如果你放弃了前奏并且不再导入任何东西,那么你还剩下什么可以处理呢?

看起来我好像在问一百万个问题,但他们实际上都在试图用不同的措辞来问同样的事情。尽力而为吧!

最佳答案

唉,元组并没有什么魔力。 Here's the implementation GHC uses ,为了让您了解发生了什么,这里是最后一个定义的来源:

data (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_ f_ g_ h_ i_ j_ k_ l_ m_ n_ o_ p_ q_ r_ s_ t_ u_ v_ w_ x_ y_ z_ a__ b__ c__ d__ e__ f__ g__ h__ i__ j__
= (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_ f_ g_ h_ i_ j_ k_ l_ m_ n_ o_ p_ q_ r_ s_ t_ u_ v_ w_ x_ y_ z_ a__ b__ c__ d__ e__ f__ g__ h__ i__ j__

...是的。

So, would it be possible to define this family of operators/functions from within the haskell implementation itself, using nothing but the type system and existing language features (declarations, type signatures, function definitions etc.)? And if so, how? Or is it impossible and you have to instead look into the compiler to find the supporting framework for this collection of functions?

不,没有办法以通用的方式定义这样的元组。常见模式纯粹是语法上的,不能在类型系统或其他系统中递归地完成任何操作。当然,您可以使用 Template Haskell 生成此类定义,但您仍然需要使用字符串操作单独生成每个定义来创建名称,而不是使用任何类型的共享结构。

还有一个问题是元组语法是内置的,无法模仿,但这是一个单独的问题。您可能会想象这样的类型:

data Tuple2 a b = Tuple2 a b
data Tuple3 a b c = Tuple3 a b c

...等等,它们不使用特殊语法,但由于上述原因仍然无法通用定义。

This leads to an even more general question: How much of Haskell is supported by Haskell itself, through type and function definitions, declarations etc; and how much is supported by the compiler/implementation? (I am aware that GHC was written in Haskell, that doesn't answer the question)

几乎所有内容都是在 Haskell 中定义的。某些事物具有您无法模仿的特殊语法,但在大多数情况下,它们只会扩展到编译器特别注意某些定义。否则,this 之间没有区别。 :

data [] a = [] | a : [a]

...以及您自己定义的任何等效类型。

That is, if you were to abandon the standard libraries (including the prelude) and do everything from the ground up in raw Haskell; would it be possible to build a complete implementation that has all the features of GHC, using only that minimal set of features? What are the mimimum set of language features that you need in order to build a haskell implementation using Haskell? Would I be able to abandon the prelude and then completely rebuild it manually from within GHC? If you abandon the prelude and never import anything, what is left over for you to work with?

阅读 GHC 的 NoImplicitPrelude and RebindableSyntax 可能会给您带来启发。扩展,除其他外,它可以让您更改用于解释 do 表示法的定义、数字文字的处理方式、if then else 语法的作用等。

可以说,非常非常少的东西是无法重新实现的。大多数不能的东西只是由于语法而特殊,并且可以用等效的东西替换(例如上面的列表和元组)。

最后,有一组有限的事物具有非常的特殊行为——IO类型就是一个明显的例子——你根本无法替换它们,因为它们直接连接到运行时系统中的某些东西,而您无法替换。

关于Haskell 元组构造函数 (GHC) 以及语言与其实现之间的分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7086668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com