Necto
Necto

Reputation: 2654

How to define a compile time available function in scheme?

How the scheme compiler determines, which functions will be available during macroexpansion?

I mean such low level mechanisms, like syntax-case, where you can not only generate patter substitution, but call some functions, at least in a fender part

Edit:

I mean, I need to use an ordinary function in macroexpansion process. E. g.:

(define (twice a)
  (declare 'compile-time)
  (* 2 a))
(let-syntax ((mac (lambda (x)
                    (syntax-case x ()
                      ((_ n) (syntax (display (unsyntax (twice n)))))))))
   (mac 4))

Where n is known to be a number, and evaluation of (twice n) occurs during expansion.

Upvotes: 1

Views: 384

Answers (2)

GoZoner
GoZoner

Reputation: 70155

Every Scheme compiler determines the functions referenced by a macro expansion. In your case, the compilation of 'let-syntax' will result in the compiler determining that 'twice' is free (syntactically out-of-scope within 'let-syntax'). When the macro is applied, the free reference to the 'twice' function will have been resolved.

Different Scheme compilers perform the free value resolution at possibly different times. You can witness this by defining 'twice' as:

(define twice
  (begin (display 'bound')
         (lambda (x) (* 2 x))))

[In your case, with let-syntax it will be hard to notice. I'd suggest define-syntax and then a later use of '(mac 4'). With that, some compilers (guile) will print 'bound' when the define-syntax is compiled; others (ikarus) will print 'bound' when '(mac 4)' is expanded.]

Upvotes: 1

Justin Ethier
Justin Ethier

Reputation: 134167

It depends what macro system you are using. Some of these systems allow you to call regular scheme functions during expansion. For example, Explicit Renaming Macros let you do this:

(define-syntax swap!
  (er-macro-transformer
    (lambda (form rename compare?)
      ...
      `(let ((tmp ,x))
         (set! ,x ,y)
         (set! ,y tmp)))))

That said, the macro systems available to you will depend upon what Scheme you are using.

Upvotes: 0

Related Questions