简体   繁体   English

为什么Racket博士不展开嵌套宏?

[英]Why doesn't Dr. Racket expand nested macros?

I am using Dr. Racket, version 5.3.1. 我正在使用5.3.1版的Racket博士。 I am trying to use the Macro Stepper feature, and am having problems with "nested" macros. 我正在尝试使用宏步进器功能,“嵌套”宏有问题。 (By "nested" macros, I mean macros that expand to a form which contains more (used-defined) macros. I don't know if this is the correct terminology). (通过“嵌套”宏,我的意思是宏扩展为包含更多(使用定义的)宏的形式。我不知道这是否是正确的术语)。 The macro stepper only expands these macros once, and then doesn't bother to continue expanding. 宏步进器只将这些宏扩展一次,然后再继续扩展就不再麻烦了。

For example, I type the following into the Dr. Racket definitions area: 例如,我在Dr. Racket定义区域中键入以下内容:

#lang racket

(define-syntax foo
  (syntax-rules ()
    ((foo a) 1)
    ((foo a stuff ...) (+ 1 (foo stuff ...)))))

(foo a b c d e)

Running this returns 5, as expected. 运行此程序将按预期返回5。 Furthermore, typing (expand '(foo abcde)) in the Interactions window yields the syntax '(#%app + '1 (#%app + '1 (#%app + '1 (#%app + '1 '1)))) , also as expected. 此外,在“交互”窗口中输入(expand '(foo abcde))产生语法'(#%app + '1 (#%app + '1 (#%app + '1 (#%app + '1 '1)))) ,也符合预期。 However, going into the Macro Stepper (with standard macro hiding) and pressing the End button, I only see (+ 1 (foo bcde)) . 但是,进入宏步进器(隐藏标准宏)并按“ End按钮,我只会看到(+ 1 (foo bcde)) If I disable macro hiding, I get the expected result, but also a whole lot of line noise that I'd rather not see. 如果禁用宏隐藏,则可以得到预期的结果,但也会产生很多我不希望看到的线路噪声。

Is this a bug, or expected behaviour? 这是错误还是预期的行为? I swear that Dr. Racket didn't used to behave like this... 我发誓Racket博士以前就没有这种行为...

I actual submitted a bug report about this a month ago ( http://bugs.racket-lang.org/query/?cmd=view&pr=13203 ), but then I started having second thoughts about whether it was a bug or not, so I decided to ask here. 我实际上是在一个月前提交了与此有关的错误报告( http://bugs.racket-lang.org/query/?cmd=view&pr=13203 ),但是随后我开始重新考虑是否是错误,所以我决定在这里问。

PS - other random notes about this: PS-关于此的其他随机注释:

It seems to depend on whether or not the nested macro is the outer-most expression in the expanded form. 它似乎取决于嵌套宏是否是扩展形式中的最外面的表达式。 For example, if I define (in addition to foo): 例如,如果我定义(除了foo):

(define-syntax bar
  (syntax-rules ()
    ((bar xs ...) (foo xs ...))))

(define-syntax baz
  (syntax-rules ()
    ((baz xs ...) (bar xs ...))))

(baz a b c d e)

Then the macro stepper shows me that (baz abcde) expands to (bar abcde) to (foo abcde) to (+ 1 (foo bcde)) , but then it stops. 然后宏步进器显示(baz abcde)扩展到(bar abcde)(foo abcde)(+ 1 (foo bcde)) ,但随后停止。

The previous example might make you think it has something to do with macros that expand to themselves, but this doesn't appear to be the case. 前面的示例可能使您认为它与扩展到自身的宏有关,但事实并非如此。 For example, if I redefine foo as follows: 例如,如果我按以下方式重新定义foo:

(define-syntax foo
  (syntax-rules ()
    ((foo a) 1)
    ((foo a stuff ...) (+ 1 (blah stuff ...)))))

With

(define-syntax blah
  (syntax-rules ()
    ((blah xs ...) 10)))

Then (foo abcde) expands to (+ 1 (blah bcde)) and then stops. 然后(foo abcde)扩展为(+ 1 (blah bcde)) ,然后停止。

I'm not sure if this is a bug or not, although the foo in the expanded code is a different color then the original foo, so I'm inclined to think that is what is confusing it. 我不确定这是否是错误,尽管扩展代码中的foo与原始foo的颜色不同,所以我倾向于认为这是造成混淆的原因。

By default, the macro stepper tries to simplify the expanded macro to show only macros defined in the module, and files required with strings (ie files from your 'project'). 默认情况下,宏步进器会尝试简化扩展宏以仅显示模块中定义的宏以及字符串所需的文件(即来自“项目”的文件)。 This is the feature that seems to be throwing it off. 这个功能似乎正在抛弃它。

One way you can work around this (at least in smallish files) is to disable this feature. 解决此问题的一种方法(至少在较小的文件中)是禁用此功能。 To do this, go to the bottom left of the window where it says: Macro hiding: , click the drop down menu and select Disable . 为此,请转到窗口左下方显示的内容: Macro hiding: ,单击下拉菜单并选择Disable

Macro hiding: -> Disable Macro hiding: -> Disable

From there, you should see the entire expansion of your module, including the expansion of foo . 从那里,您应该看到模块的整个扩展,包括foo的扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM