- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我意识到这一点时,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/
runghc 和 ghc 有什么区别? 我运行了一个简短的程序,似乎可以用两者编译,除了我用 runghc 得到了以下内容,但不是普通的 ghc: error: Variable not in sco
如果 friend 想要运行我的 Haskell 二进制文件,他是否必须先安装 Haskell,还是可以立即自行运行二进制文件? Mac、Windows 和 Linux 上的答案相同吗? 最佳答案 G
ffunction glMultiDrawElements 需要一个指向指针的指针作为其参数之一。如何从 StorableArray Int a 获取 Ptr(Ptr a) ? 最佳答案 您需要先将索
module Has (r,p,s) where import Prelude ((==),Bool(..),otherwise,(||),Eq) import qualified Data.List
我已经构建了 ghc-HEAD,我想尝试构建所有的 stackage lts 或 nightly 看看它能做多少。 无论我说什么都无法说服 stack 使用我的新 ghc 构建任何东西。我尝试设置如下
我正在使用 Emacs (24.3.1) 在 haskell-mode 中与 ghc-mod 一起使用 Haskell。 现在除了一件烦人的事情外,一切都很好: GHC 信息缓冲区中的每个输出仅包含第
为什么升级到 OSX Mavericks 后我的 GHC 7.6.3 不能工作? 最佳答案 花了很长时间才弄清楚如何同时使用 OSX 10.9 和 GHC 7.6.3,这里有一些技巧可以帮助您重新构建
我有一个带有 cabal 文件的主要 Haskell 可执行程序。在那里,我指定 ghc-options . 这个可执行文件链接到野外的其他库。请问ghc-options忽略这些库的 cabal 文件
我有一个使用 -Wall 编译的库,并且我有一个使用 -Wall -Wno-incomplete-patterns 的测试套件 当我使用 stack ghci --test 启动 ghci 时,ghc
我正在尝试使用以下脚本在 Windows 上安装 GHC(并将 ghc 放在路径中),但是当我尝试运行 ghci --version 时它失败了。我认为脚本有问题。 install: - ps:
我在 $HOME 中安装了 ghc-7.2.2 unknown linux tar.bz2在archlinux上。 在使用 cabal-dev 成功安装多个软件包后,尝试安装例如。我得到的解析数字、文
想法 我正在写 DSL ,编译为 Haskell。 该语言的用户可以定义自己的不可变数据结构和相关函数。关联函数是指属于数据结构的函数。 例如,用户可以编写(在“pythonic”伪代码中): dat
(我的问题是在没有 haskell-platform、ghc、cabal 等的情况下分发二进制文件) 我需要部署一个结构良好的 haskell 应用程序(Yesod 脚手架),但我有磁盘空间限制。 G
我试图将 Cygwin 安装程序指向 http://haskell.org/ghc/cygwin ,但安装程序无法找到 setup.ini.sig。如果可能的话,我该如何编辑我的 .bashrc 以引
我有一个现有的 Haskell 函数,它使用 GHC API 从模块中动态加载已编译的代码。它基于博客文章中的代码 Dynamic Compilation and Loading of Modules
我使用:Ubuntu 上的 GHC 7.6.3(通过 apt-get install haskell-platform) 从当前存储库安装它。 正在尝试安装ghc-mod ,因为我的 ide 插件需要
AFAIK,有两种方法可以在 Haskell 中获取用于调试的调用堆栈: 添加 HasCallStack代码中的约束 使用 ghc -prof -fprof-auto-top 编译代码 我的测试代码:
我想用 64 位 GHC 构建 32 位 DLL。这是最小的例子。 测试.hs {-# LANGUAGE ForeignFunctionInterface #-} module Test where
ghc-gc-tune 0.2.1 可以与 ghc 7.4.1 一起使用吗?看来 ghc-gc-tune 已经有一段时间没有更新了,可能只适用于 ghc 6.x?我找不到任何可靠的信息。 我收到以下错
语言扩展 ExplicitForall 使得使用 forall 绑定(bind)类型变量成为可能但不是必需的。 例如,下面的程序可以编译 {-# LANGUAGE ExplicitForAll #-}
我是一名优秀的程序员,十分优秀!