世界上是否存在针对简单函数式语言的编译器,例如定义 on this article 的编译器? ,或者只是 Scheme 甚至 ML 本身?也就是说,一个编译器会分析该简单函数式语言的代码,执行复杂的优化(例如流融合)并返回优化后的代码?
我需要这个的原因是我正在尝试将我的简单玩具语言(相当多的 lambda 演算 + 数字和数组)编译为 JavaScript,但事实上,由于天真的编译器,它非常慢完全没有优化。函数不是 uncurried,诸如 (map f (map g h))
之类的东西不会融合到 (map (f . g) h)
中。实现优化编译器会造成 much work .我考虑过利用现有的编译器,例如 GHC,但我还没有决定如何实现。例如,我可以将我的语言编译为 Haskell,然后将其编译回 JavaScript,但大多数 Haskell->JS 解决方案都不是最优的,并且会产生大量开销。这种现有的针对简单函数式语言的强大优化编译器本身会减少很多工作。世界上有吗?
GHC可以产生GHC-Core,这是一种中间原始的类Haskell语言。它是在编译器的所有优化之后上演的,包括像流融合这样的高级东西。所以我想它可能是您任务的完美人选。
来自 the docs :
The Glasgow Haskell Compiler (GHC) uses an intermediate language, called “Core,” as its internal program representation within the compiler’s simplification phase. Core resembles a subset of Haskell, but with explicit type annotations in the style of the polymorphic lambda calculus (Fω).
GHC’s front-end translates full Haskell 98 (plus some extensions) into Core. The GHC optimizer then repeatedly transforms Core programs while preserving their meaning. A “Core Lint” pass in GHC typechecks Core in between transformation passes (at least when the user enables linting by setting a compiler flag), verifying that transformations preserve type-correctness. Finally, GHC’s back-end translates Core into STG-machine code [stg-machine] and then into C or native code.