Summary

expand1: Expand macro-form only once

Metadata

Attachments

Description

These patches add the `expand1` procedure to module (chicken syntax), and add the `,x1` toplevel command to csi. This has been a personal wish of mine for several years and I finally decided to implement it. :)

`expand1` is like `expand` but it expands the form only once, instead of repeating until a non-macro form is produced. This is helpful when debugging macros because you can see the intermediate results of the expansion.

`,x1` is like the `,x` command (expand and pretty-print the form) but it uses `expand1` instead of `expand`. This is not difficult for users to implement in their `.csirc` once `expand1` is available, so you can ignore the second patch if you don't think it is useful enough to build into csi.

I tried adding the tests below to `tests/syntax-tests.scm`, but they only pass when using csi, not csc. Specifically, `(expand '(rec-macro 1))` and `(expand1 '(rec-macro 1))` both expand to `(rec-macro 1)`. I suspect this is because the `rec-macro` macro is not visible at run time in compiled code. The only solution I have found so far is to use `(eval '(begin (define-syntax rec-macro ...) (expand '(rec-macro 1))))`, which is messy because every test must repeat the macro definition. Maybe someone more knowledgeable than me can find a cleaner way to make the tests work.

 #!scheme
 ;;; expand / expand1
 
 (define-syntax rec-macro
   (syntax-rules ()
     ((rec-macro 1) (rec-macro 2))
     ((rec-macro 2) "done") ) )
 
 ;; `expand` expands repeatedly until it finds a non-macro form.
 (t "done" (expand '(rec-macro 1)))
 
 ;; `expand1` expands only once.
 (t '(rec-macro 2) (strip-syntax (expand1 '(rec-macro 1))))
 (t "done" (expand1 (expand1 '(rec-macro 1))))

Changes and comments

[2021-07-12 08:38:51 UTC] jacius attached 0001-Add-expand1-procedure-in-module-chicken-syntax.patch (description=#f)

[2021-07-12 08:39:02 UTC] jacius attached 0002-Add-x1-toplevel-command-to-csi.patch (description=#f)

[2025-08-05 14:03:01 UTC] sjamaan changed milestone from someday to 6.0.0

[2025-08-05 14:03:01 UTC] sjamaan wrote:

Implemented with fea4d53e

[2025-08-05 14:03:05 UTC] sjamaan changed status from new to closed

[2025-08-05 14:03:05 UTC] sjamaan set resolution to fixed