Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

  1. Outdated egg!
  2. Implicit renaming macros
  3. Important note
  4. Programming interface
    1. ir-macros
      1. ir-macro-rules
    2. ir-macro-define
    3. ir-macro-let
    4. ir-macro-letrec
  5. Usage
  6. Example
  7. Requirements
  8. Author
  9. License
  10. Last update
  11. Version History

Implicit renaming macros

THIS MODULE IS NOW OBSOLETE, USE low-level-macros INSTEAD!

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

(lambda (form inject compare?) ...)

which has to be enclosed by an ir-macro-transformer call. The programmer's job is to destructure the macro-code, form. All symbols of the macro-expression not protected by inject are transparently renamed in the background to achieve hygiene. If no symbol is injected, the macro is automatically hygienic.

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 ir-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 those renamed behind the scene, something which is needed to use symbols like else and => in the cond macro, remains to be done. Hence ir-macro-rules is unhygienic by design. It exports compare? and inject to its local scope, the latter in case the macro has to export unrenamed symbols.

Important note

All macros in this module are unhygienic by design, since the evaluate to ir-macro-rules. They pollute the local (!) namespace with the symbols compare? and inject. But the macros implemented with those macros can be - and are in most cases - hygienic.

Programming interface

ir-macros

[procedure] (ir-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 the symbol.

ir-macro-rules

[syntax] (ir-macro-rules (sym ...) (code0 xpr0) (code1 xpr1) ...)

pairs the differnt macro-codes code0 code1 ... with expressions xpr0 xpr1 ..., which usually evalute to backquoted templates in the scope of injected symbols sym ....

This macro is unhygienic by design, it introduces the two symbols inject and compare? into its scope. The macro is implemented in contracts and passed through to ir-macros

ir-macro-define

[syntax] (ir-macro-define (name . args) . body)

defines a macro with macro-code (name . args) by means of body, which is either a sequence of expressions xpr . xprs which generate a template, or a list of the form (with-injected (sym ...) xpr . xprs). In the latter case the macro is unhygienic and pollutes its local namespace with sym ..., in the former case it is hygienic.

Note, that this macro in its hygienic form has exactly the same syntax as define-macro of Chicken-3 and other Schemes!

ir-macro-let

[syntax] (ir-macro-let ((code0 . body0) ...) . body)

where code0 ... is the macro-code (name0 . args0) ... of a local macro and body0 ... the corresponding body which generates a template. As in ir-macro-define this must either be a sequence of expressions, xpr0 . xprs0, or a list of the form (with-injected (sym0 ...) xpr0 . xprs0). In the latter case the local macros are unhygienic. These macros are locally defined in parallel and in there scope body is executed.

ir-macro-letrec

[syntax] (ir-macro-letrec ((code0 body0) ...) . body)

recursive version of ir-macro-let, i.e. the local macros have access to each other.

Usage

(use ir-macros contracts)
(import-for-syntax (only ir-macros ir-macro-rules))

Example


(use ir-macros contracts)
(import-for-syntax (only ir-macros ir-macro-rules))

;; anaphoric versions of well-known macros

;; initialize documentation
(doclist '())

(define-syntax-with-contract aif
  "anaphoric if, can refer to it"
  (ir-macro-rules (it)
    ((_ test then)
     `(let ((,it ,test))
        (if ,it ,then)))
    ((_ test then else)
     `(let ((,it ,test))
        (if ,it ,then ,else)))))

(define-syntax-with-contract acond
  "anaphoric cond, which can reference the test in each clause with it"
  (ir-macro-rules (it)
    ((_ (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))))))

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

(ir-macro-define (freeze . xprs)
  `(lambda () ,@xprs))

;; unhygienic: an endless loop which can be exited with (exit val)
(ir-macro-define (do-forever . body)
  (with-injected (exit)
    `(call/cc (lambda (,exit)
                (let loop ()
                  ,@body
                  (loop))))))

;; anaphoric when, can refer to it
(ir-macro-define (awhen test xpr . xprs)
  (with-injected (it)
    `(let ((,it ,test))
       (if ,it (begin ,xpr ,@xprs)))))

;; anaphoric and, each arg can refer to its precursor via it
(ir-macro-define (aand . args)
  (with-injected (it)
   (let loop ((args args))
     (cond 
       ((null? args) #t)
       ((null? (cdr args)) (car args))
       (else 
         `(let ((,it ,(car args)))
            (if ,it
              ,(loop (cdr args)))))))))

;; anaphoric while, can refer to successive test via it
(ir-macro-define (awhile ok? xpr . xprs)
  (with-injected (it)
   `(let loop ((,it ,ok?))
      (when ,it
        ,xpr ,@xprs
        (loop ,ok?)))))

;; anaphoric lambda, can refer to itself via self
(ir-macro-define (alambda args xpr . xprs)
  (with-injected (self)
   `(letrec ((,self (lambda ,args ,xpr ,@xprs)))
      ,self)))

(define (f n) (+ n 10))

(define (bar)
  (ir-macro-let (
    ((f n) n)
    ((g n) `(f ,n))
    )
    (list (f 1) (g 1))))

(define (baz)
  (ir-macro-letrec (
    ((f n) n)
    ((g n) `(f ,n))
    )
    (list (f 1) (g 1))))

;; map with an anonymous factorial
(map (alambda (n) (if (zero? n) 1 (* n (self (- n 1))))) '(1 2 3 4 5))

Requirements

contracts

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 without 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

Sep 13, 2011

Version History

1.7
module moved to category obsolete
1.6
ir-macro-define-with-contract moved to contracts
1.5
ir-macro-rules from contracts reimplemented with define-syntax-with-contract
1.4
acond corrected
1.2
Bug in ir-macro-define-with-contract corrected
1.1
ir-macro-define-with-contract added, aliases without ir- prefix removed
1.0
All macros in this egg made unhygienic by design
0.3
changed syntax of [ir-]macro-rules, thanks to Moritz Heidkamp
0.2
added additional inject argument to all transformers
0.1
initial import