4
votes

How does one create aliases for macros in Scheme?

in the same vein as this for Clojure

define a synonym for a Clojure macro

and this for Common Lisp

common lisp function/macro aliases

I realize for functions it is

(define alias function)

but I can't seem to find, or come up with, how to define aliases for macros (in GNU Guile to be precise)

3

3 Answers

2
votes

Guile supports syntax-rules, so a plausible way to create an alias for, say, `and' would look like this:

;; define my-and as an alias for and:
(define-syntax my-and   
  (syntax-rules ()
    [(_ arg ...)
     (and arg ...)]))

;; try it out:
(display (my-and #t #t)) (newline)
(display (my-and #t #f)) (newline)
(display (my-and #f (/ 1 0))) (newline)

If you really wanted a syntax-alias form, you could... surprise! ... define that as a macro:

;; define a 'syntax-alias' form
(define-syntax syntax-alias
  (syntax-rules ()
    [(_ newname oldname)
     (define-syntax newname
       (syntax-rules ()
         [(_ arg (... ...))
          (oldname arg (... ...))]))]))

;; use 'syntax-alias' to define 'my-and' as an alias for 'and':
(syntax-alias my-and and)

;; try it out:
(display (my-and #t #t)) (newline)
(display (my-and #t #f)) (newline)
(display (my-and #f (/ 1 0))) (newline)
3
votes

If you are using a scheme implementation which supports R6RS, such as Chez Scheme or guile, the simplest approach is to use identifier-syntax to create an alias:

(define-syntax new-name (identifier-syntax old-name))

identifier-syntax can do more than alias a name: it can alias an expression. So you could define an **unspecified** macro as follows:

(define-syntax **unspecified** (identifier-syntax (if #f #f)))
2
votes

In portable Scheme from R5RS you need to make a macro that implements it:

(define-syntax test 
  (syntax-rules ()
    ((_ . args) (if . args))))

It's a little verbose so you can make a macro that does this for you:

(define-syntax define-syntax-alias
  (syntax-rules ()
    ((_ new-name old-name)
     (define-syntax new-name
       (syntax-rules ()
         ((_ . args) (old-name . args)))))))

(define-syntax-alias test if)

Of course the implementation for define-syntax-alias can be put in its own library and reused everywhere you need it for R6RS and beyond.

In a related dialect Racket which started off as Scheme but now has diverged into it's own lisp language you have something like this in the standard language:

(define-syntax test (make-rename-transformer #'if))