5

我想知道 F# 中无类型/有类型的代码引用与宏系统的卫生之间是否存在关系。他们是否用各自的语言解决了相同的问题,或者他们是不同的关注点?

4

2 回答 2

7

The meta-programming aspect is the only similarity, and even in that regard, there is a big difference. You can think of the macro's transformer as a function from syntax to syntax like you can manipulate quotations, but the transformers are globally coordinated so that names used as binders follow a specific protocol:

1) Binders may not be the same as any free name in input to the macro (unless you use an unhygienic escape hatch)

2) Names bound in a macro definition's context that are free in the macro's expansion must point to the same thing at macro use time. (this needs global coordination)

Choices for names are made so that expansion does not fail if you used the wrong name (unless it turns out that name is unbound).

Transformers of typed quotations do not have this definition time context idea. You manipulate quotations to form a program that does not refer to any names in your program. They are not meant to provide a syntactic abstraction mechanism. Arbitrary shapes of syntax? Nope. It all has to be core AST shapes.

Open code in typed quotation systems can be closed with anything that fits the type structure of the expected context - there is no coordinated composition of several open components into a coherent structure.

于 2012-05-18T21:38:14.660 回答
6

引用是元编程的一种形式。它们允许您以编程方式操作抽象语法树,然后可以将其拼接成代码并进行评估。

类型引用将 AST 的具体类型嵌入到宿主语言的类型系统中,因此它们确保您不会生成类型错误的代码片段。未键入的报价不提供这种保证(它可能会因运行时错误而失败)。

顺便说一句,键入的引文与 Template Haskell quasiquotations 非常相似。

类 Lisp 语言中的 Hygenic 宏是相关的,因为它们的存在是为了支持元编程。然而,卫生是为了简单的名称捕获混淆,键入准引号已经避免(以及更多)。

所以是的,它们是相似的,因为它们分别是有类型和无类型语言的元编程机制。类型化的准引号和卫生宏都为完全非类型化、不健全的元编程增加了额外的安全性。他们为程序员提供的保证水平是不同的。键入的引号更严格。

于 2012-05-17T18:12:44.677 回答