Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scheme aliases for macros

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)

like image 284
Kyuvi Avatar asked Mar 07 '23 23:03

Kyuvi


2 Answers

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)))
like image 75
Chris Vine Avatar answered Mar 15 '23 02:03

Chris Vine


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)
like image 37
John Clements Avatar answered Mar 15 '23 02:03

John Clements