函数式编程语言自省
我正在草拟一个东西的设计(函数的机器学习),它最好需要函数式编程语言,以及内省,特别是以某种易于处理的格式检查程序自身代码的能力,并且最好还能够获得机器Lisp 当然有很强的自省能力,但静态类型语言也有优势;我正在考虑的是:
F# - .Net 平台在这里有一个很好的故事,您可以在运行时读取字节代码,也可以发出字节代码并对其进行编译;我认为从 F# 访问这些设施没有问题。
Haskell、Ocaml - 这些是否有类似的功能,无论是通过字节码还是解析树?
我还应该考虑其他语言吗?
I'm sketching a design of something (machine learning of functions) that will preferably want a functional programming language, and also introspection, specifically the ability to examine the program's own code in some nicely tractable format, and preferably also the ability to get machine generated code compiled at runtime, and I'm wondering what's the best language to write it in. Lisp of course has strong introspection capabilities, but the statically typed languages also have advantages; the ones I'm considering are:
F# - the .Net platform has a good story here, you can read byte code at run time and also emit byte code and get it compiled; I assume there's no problem accessing these facilities from F#.
Haskell, Ocaml - do these have similar facilities, either via byte code or parse tree?
Are there other languages I should also be looking at?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Ocaml 有:
Camlp4 用于在 Ocaml 中操作 Ocaml 具体语法树。 Camlp4 的维护实现是 Camlp5。
MetaOCaml 用于全面的多阶段编程。
Ocamljit 在运行时生成本机代码,但我认为最近没有维护。
Ocaml-Java 为 Java 虚拟机编译 Ocaml 代码。不知道有没有好的反射能力。
Ocaml has:
Camlp4 to manipulate Ocaml concrete syntax trees in Ocaml. The maintained implementation of Camlp4 is Camlp5.
MetaOCaml for full-scale multi-stage programming.
Ocamljit to generate native code at run time, but I don't think it's been maintained recently.
Ocaml-Java to compile Ocaml code for the Java virtual machine. I don't know if there are nice reflection capabilities.
不是真正的答案,但还要注意 F# 引用 功能和 < a href="http://msdn.microsoft.com/en-us/library/ee370558.aspx" rel="nofollow noreferrer">库,了解更多信息 同像性之类的东西。
Not really an answer, but note also the F# Quotations feature and library, for more homoiconicity stuff.
您可以查看 Racket(以前称为 PLT 方案)。它保留了Scheme 的大部分语法简单性,但提供了静态类型系统。由于 Racket 是一种方案,因此元编程是课程的标准,并且运行时可以通过
You might check out the typed variant of Racket (previously known as PLT Scheme). It retains most of the syntactic simplicity of Scheme, but provides a static type system. Since Racket is a Scheme, metaprogramming is par for the course, and the runtime can emit native code by way of a JIT.
Haskell 方法更倾向于解析源代码。 Haskell 平台包含一个完整的源解析器,或者您可以使用 GHC API 来以这种方式进行访问。
The Haskell approach would be more along the lines of parsing the source. The Haskell Platform includes a complete source parser, or you can use the GHC API to get access that way.
我还会查看 Scala 或 Clojure,它们附带了所有为 Java 开发的库。如果库不存在,您永远不必担心。但更重要的是,这些语言为您提供了与 Java 中相同的反射(或更强大的类型)。
I'd also look at Scala or Clojure which come with them all the libraries that have been developed for Java. You'll never need to worry if a library does not exist. But more to the point of your question, these languages give you the same reflection (or more powerful types) that you will find within Java.
你能不能像普通解释器或编译器一样只解析源代码?为什么需要内省?
F# 具有基本的引用机制,但您只能引用某些表达式,而不能引用其他类型的代码,尤其是类型定义。此外,它的评估机制比真正的编译慢几个数量级,因此它基本上完全没有用。您可以使用反射来分析类型定义,但同样,它非常初级。
您可以读取字节码,但它已被编译,因此许多信息和结构已丢失。
F# 还拥有词法分析和解析技术(最著名的是 fslex、fsyacc 和 FParsec),但它不如 OCaml 成熟。
Haskell 有 Template Haskell,但我从未听说过有人使用它(废弃软件?)。
OCaml 有它的 Camlp4 宏系统,也有少数人使用它,但它文档很少。
至于词法分析和解析,Haskell 有一些库(最著名的是 Parsec),OCaml 有 许多库。
像 Mathematica 这样的术语重写语言将是一个明显的选择,因为它们使操作代码变得微不足道。 Pure 语言可能会令人感兴趣。
您还可以考虑使用 MetaOCaml,因为它具有运行时编译功能。
Can you not just parse the source code like an ordinary interpreter or compiler? Why do you need introspection?
F# has a rudimentary quotation mechanism but you can only quote some expressions and not other kinds of code, most notably type definitions. Also, its evaluation mechanism is orders of magnitude slower than genuine compilation so it is basically completely useless. You can use reflection to analyze type definitions but, again, it is quite rudimentary.
You can read byte code but that has been compiled so a lot of information and structure has been lost.
F# also has lexing and parsing technology (most notably fslex, fsyacc and FParsec) but it is not as mature as OCaml's.
Haskell has Template Haskell but I've never heard of anyone using it (abandonware?).
OCaml has its Camlp4 macro system and a few people do use it but it is poorly documented.
As for lexing and parsing, Haskell has a few libraries (most notably Parsec) and OCaml has many libraries.
Term rewrite languages like Mathematica would be an obvious choice because they make it trivial to manipulate code. The Pure language might be of interest.
You might also consider MetaOCaml for its run-time compilation capabilities.
Haskell 的自省机制是 Template Haskell,它支持编译时元编程,并且与例如 llvm,提供运行时元编程工具。
Haskell's introspection mechanism is Template Haskell, which supports compile time metaprogramming, and when combined with e.g. llvm, provides runtime metaprogramming facilities.