You are looking at historical revision 24634 of this page. It may differ significantly from its current revision.

Explicit-renaming macros

This module contains some macros to make the use of low-level explicit renaming macros easier. Recall that those macros are implemented as a transformer routine, which is a three-parameter procedure

(lambda (form rename compare?) ...)

which should (but needn't) be enclosed by an er-macro-transformer call. The programmer's job is to destructure the macro-code, form, and to do the renaming of all symbols which should appear in the macro-expansion by hand. An exception are the unhygienic symbols, they mustn't be renamed.

The job of destructuring the macro-code is tedious and error prone, it can be done by a tool, bind from the contracts module, for example. Based on that, a macro, er-macro-rules, has been implemented there. It mimics syntax-rules by design. The other job, compare? literal symbols supplied by the macro's client with renamed ones, something which is needed to use symbols like else and => in the cond macro, remains to be done. Hence er-macro-rules is unhygienic by design. It exports compare? to its local scope.

Important note

All macros in this module except with-aliases are unhygienic by design. They pollute the local (!) namespace with the symbol compare?. But the macros implemented with those macros can be - and are in most cases - hygienic.

Programming interface

er-macros

[procedure] (er-macros sym)

where sym is optional.

This is the documentation dispatcher provided for all modules written in the Design-by-Contract style. Without a symbol, it lists all exported symbols of the module, with one of these symbols it prints the contract of that symbol.

Unhygienic macros can not be implemented with syntax-rules, so we must use er-macro-transformer or er-macro-rules.

Based on er-macro-rules, it's easy to implement three macros, er-macro-define, er-macro-let and er-macro-letrec, which facilitate the implementation of low-level macros even more: We simply match one pattern, the macro code, against a list of the form

 (with-renamed (%sym ...) xpr . xprs)

where %sym ... are aliases of sym ... and the sequence xpr . xprs produces the macro-expansion.

er-macro-define

[syntax] (er-macro-define code (with-renamed (%sym ...) xpr . xprs))

where code is the complete macro-code (name . args), i.e. the pattern of a macro call, and (with-renamed ...) is explained above.

er-macro-let and er-macro-letrec are local versions of er-macro-define, where the local macros are evaluated in parallel or recursively.

er-macro-let

[syntax] (er-macro-let ((code0 (with-renamed (%sym0 ...) . body0)) ...) . body)

where code0, %sym0 and body0 are as in macro-define. This is a local version of er-macro-define, allowing a list of (code with-xpr) lists to be processed in body in parallel.

er-macro-letrec

[syntax] (er-macro-letrec ((code0 (with-renamed (%sym0 ...) . body0)) ...) . body)

where code0, %sym0 and body0 are as in macro-define. This is a local version of er-macro-define, allowing a list of (code with-xpr) lists to be processed recursively.

er-macro-define-with-contract

[syntax] (er-macro-define-with-contract code [docstring] (with-renamed (%sym ...) . body))

where code is the complete macro-code (name . args), i.e. the pattern of a macro call, %sym ... are aliases of sym ... and the sequence of expressions body produces the macro-expansion.

with-aliases

[syntax] (with-aliases (op %sym ...) . body)

binds %sym ... to (op sym) ... and executes body in this scope. Mostly used within raw low-level macros, if destructuring is not a problem. In that case, op is the rename operator.

Requires

contracts

Usage

(import er-macros contracts)
(import-for-syntax
  (only contracts er-macro-rules syntax-contract)
  (only er-macros with-aliases))

Examples

(import er-macros contracts)
(import-for-syntax
  (only contracts er-macro-rules syntax-contract))

;; initialize documentation
(doclist '())

(er-macro-define (my-or . args)
	(with-renamed (%if %my-or)
		(if (null? args)
			#f
			(let ((tmp (car args)))
				`(,%if ,tmp ,tmp (,%my-or ,@(cdr args)))))))

(er-macro-define-with-contract (my-or . args)
  "a variant of or"
  (with-renamed (%if %my-or)
    (if (null? args)
      #f
      (let ((tmp (car args)))
        `(,%if ,tmp ,tmp (,%my-or ,@(cdr args)))))))

(define-syntax-with-contract aif
  "anaphoric if, which can reference the test result with name it"
  (er-macro-rules (%let %if)
    ((_ test then)
     `(,%let ((it ,test))
        (,%if it ,then)))
    ((_ test then else)
     `(,%let ((it ,test))
        (,%if it ,then ,else)))))

(er-macro-define-with-contract (awhen test xpr . xprs)
  "anaphoric when, which can reference the test result with it"
  (with-renamed (%let %if %begin)
    `(,%let ((it ,test))
       (,%if it (,%begin ,xpr ,@xprs)))))

(er-macro-define-with-contract (aand . args)
  "anaphoric and, which can reference the previous arg with it"
  (with-renamed (%let %if)
   (let loop ((args args))
     (cond 
       ((null? args) #t)
       ((null? (cdr args)) (car args))
       (else 
         `(,%let ((it ,(car args)))
            (,%if it
              ,(loop (cdr args)))))))))

(define-syntax-with-contract acond
  "anaphoric cond, which can reference the test in each clause with it"
  (er-macro-rules (%else %begin %let %if %error %acond)
    ((_ (test . xprs))
     (if (compare? test %else)
       `(,%begin ,@xprs)
       `(,%let ((it ,test))
          (,%if it
            (,%begin ,@xprs)
            (,%error 'acond "no test succeeds")))))
    ((_ (test . xprs) (test1 . xprs1) . clauses)
     `(,%let ((it ,test))
        (,%if it
          (,%begin ,@xprs)
          (,%acond (,test1 ,@xprs1) ,@clauses))))))

(er-macro-define-with-contract (awhile ok? xpr . xprs)
  "anaphoric while, which can reference the result of each ok? with it"
  (with-renamed (%let %loop)
   `(,%let ,%loop ((it ,ok?))
      (when it
        ,xpr ,@xprs
        (,%loop ,ok?)))))

(er-macro-define-with-contract (alambda args xpr . xprs)
  "anaphoric lambda which can reference itself with self"
  (with-renamed (%letrec %lambda)
   `(,%letrec ((self (,%lambda ,args ,xpr ,@xprs)))
      self)))

;; save documentation in dispatcher
(define docs (doclist->dispatcher (doclist)))

(let ((f (lambda (n) (+ n 10))))
	(er-macro-let (
		((f n) (with-renamed ()  n))
		((g n) (with-renamed (%f) `(,%f ,n)))
		)
		(list (f 1) (g 1)))) ; -> (1 11)

(let ((f (lambda (n) (+ n 10))))
	(er-macro-letrec (
		((f n) (with-renamed ()  n))
		((g n) (with-renamed (%f) `(,%f ,n)))
		)
		(list (f 1) (g 1)))) ; -> (1 1)

(er-macro-letrec (
	((aif test then)
	 (with-renamed (%let %if) `(,%let ((it ,test)) (,%if it ,then))))
	)
	(aif (memv 2 '(1 2 3)) it)) ; -> '(2 3)

(er-macro-let (
	((aif test then)
	 (with-renamed (%let %if) `(,%let ((it ,test)) (,%if it ,then))))
	)
	(aif (memv 2 '(1 2 3)) it)) ; -> '(2 3)

(map (alambda (n) (if (zero? n) 1 (* n (self (- n 1)))))
		 '(1 2 3 4 5)) ; -> '(1 2 6 24 120)

(let ((lst '(0 1 2 3))) (aand lst (cdr it) (cdr it))) ; -> '(2 3)

(let ((lst '(0 1 2 3)))
	(acond ((memv 5 lst) it) ((memv 2 lst) it) (else it))) ; -> '(2 3)

(let ((lst '(0 1 2 3))) (aif (memv 2 lst) it #f)) ; -> '(2 3)

(let ((lst '(0 1 2 3))) (awhen (memv 2 lst) (reverse it))) ; -> '(3 2)

(let ((lst '(0 1 2 3)) (acc '()))
	(awhile lst
		(if (null? lst)
			(set! lst #f)
			(begin 
				(set!  acc (cons (car lst) acc))
				(set! lst (cdr lst)))))
	acc) ; -> '(3 2 1 0)

(with-aliases (identity %x %y)
  (list %x %y)) ; -> '(x y))

Author

Juergen Lorenz

License

Copyright (c) 2011, Juergen Lorenz
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.  Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software with-renamedout specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

Last update

Jul 31, 2011

Version History

1.2
additional tests introduced
1.1
moved er-macro-rules to contracts, dependency changed from matchable to contracts, added er-macro-define-with-contract
1.0
except with-aliases all macros are unhygienic
0.3
changed syntax of er-macro-rules and resulting corrections
0.2
added with-renamed-aliases, renamed explicit-renaming er-macro-rules, added er-prefix to other symbols
0.1
initial import