3
votes

In Guix there's a layer made of macros beautifying the creation and manipulation of srfi-9 records

The code is in guix/records.scm

The code is large and articulated

I thought to expand those macros to see their input and their output in order to have a feel of what they do

The thing is that even the standard vanilla srfi-9 records are macros around structures, in their own turn

So the macro expansion gives me a completely expanded code creating and manipulating structures.

I'd prefer to see the result of a single pass of macro expansion, to see what srfi-9 code the guix macros have produced

In Clojure there are macroexpand and macroexpand-1

macroexpand-1 does a single pass of macro expansion and macroexpand calls macroexpand-1 repeatedly until there are no more macors to expand in the forms being processed

You can see this here

Is there a similar option in Guile scheme ?

Am I missing any workflow trick, any tool, any library function or macro for dealing with this ?

2

2 Answers

3
votes

Here is a simple version of macroexpand1:

#lang racket

(define-syntax (expand1 stx)
  (syntax-case stx ()
    [(_expand1 form)
     (syntax-case #'form ()
       [(id . more)
        (identifier? #'id)
        (let ([transformer (syntax-local-value #'id)])          
          (with-syntax ([expansion (transformer #'form)])
            #''expansion))]
       [_
        #''form])]))

(expand1 (or 1 2 3))

The output is:

'(let ((or-part 1)) (if or-part or-part (or 2 3)))

Note that Clojure's macro expansion algorithm is more simplistic than what's used in most Scheme implementation.

For an explanation psyntax aka portable syntax-case look in the book "Beautiful Code" for the chapter by Dybvig.

https://www.cs.indiana.edu/~dyb/pubs/bc-syntax-case.pdf

2
votes

Ok, in the NEWS file there's this excerpt

** Removed function: `macroexpand-1'

It is unclear how to implement `macroexpand-1' with syntax-case, though PLT Scheme does prove that it is possible.

I don' t know what the problem is between macroexpand-1 and syntax-case and I don't know what Racket is doing about it

But at least now I know that macroexpand-1 has been explicitly removed