This is a quick handout to help you with debugging macros.
In DrRacket
If you’re working with DrRacket, then there is a macro stepper button in the toolbar. When you click it, you’ll get a new window showing macro expansion steps. Note that Racket itself is full of macros, so you’ll generally want to click the option to hide library macros. I’m not sure that you can get it to work sanely though.
In Racket
Another option is to use the Racket console REPL. Here’s a rough sketch of how you’d do it:
- Open a terminal, and
cdinto the directory holding your file. - Start
racket, the command-line REPL. - “enter” your file at the prompt:> ,en myfile.rkt(It will go inside the context of the file and execute the code, kind of like running the code in DrRacket, so expect some time for that to finish.)
"myfile.rkt"> _ - Set a syntax to observe, for example:"myfile.rkt"> ,stx (pushdown ...blah blah blah...)(Note that it’s better to have small examples that focus only on the macro you want to inspect. In this case, I entered a
; Syntax set
"myfile.rkt"> _pushdownexpression, and not(define foo (pushdown ...))so we focus on the expansion of the macro and not whateverdefineexpands to.) - This is now your “current” syntax. You can perform one expansion step by
entering
,stx +. If you enter it again, you get another expansion step etc. You probably won’t need to go beyond the first step or few steps. - Read more about what you can do with the
,stxcommand in the docs for it. - Note that for various resons, expanding this way might have some issues,
especially combined with typed racket, so you might run into inexplicable
type errors etc when you expand more steps. (And you’ll also see
expansion of internal stuff coming from the
#lang pletc.)