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

Low-level macros made easy

This module contains some macros to make the use of low-level macros easier. It replaces the now obsolete modules er-macros and ir-macros.

Recall that low-level macros are implemented as transformer routines, which are three-parameter procedures enclosed in er-macro-transformer or ir-macro-transformer respectively

(er-macro-transformer
  (lambda (form rename compare?) ...))
(ir-macro-transformer
  (lambda (form inject compare?) ...))

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 in the explicit-renaming case or to inject those symbols, which should not be renamed in the implicit-renaming case. In any case, symbols which are not renamed are unhygienic. The third parameter allows to handle additional keywords.

Each of these transformer arguments does a special job, each of which is tedious and error-prone. In this module, we'll automate each of these jobs.

Let's start with destructuring the macro-code. It can be done by a local macro, dbind, which in turn uses procedures destruc, dbind-ex, dbind-lit and dbind-len, which must be imported for-syntax and hence should appear in a helper module, macro-helpers. The implementation of dbind follows Graham's classic "On Lisp, p. 232". Using dbind two exported macros, bind and bind-case, are defined and used in macro-rules, an analogon to syntax-rules.

Renaming or injecting can be done by supplying a special prefix, % in most cases. Symbols with this prefix can be extracted from the macro-body and transformed into a let which defines the necessary symbols. Last but not least, additional keywords can be extracted from the macro body as well and transformed into a where clause of the bind macro. This way, the transformer disappears completely on the surface, but ,of course, happens to do the job behind the scene.

Programming interface

The module macro-helpers

macro-helpers

[procedure] (macro-helpers [sym])

shows which symbols are exported, if called with no argument, or sym's documentation.

symbol-dispatcher

[procedure] (symbol-dispatcher alist)

creates a documentation procedure as used by macro-helpers and low-level-macros.

bind-exception

[procedure] (bind-exception loc msg . args)

generates a composite condition of type (exn bind) with location loc, message msg and arguments args. Imported and reexported by low-level-macros.

prefixed-with?

[procedure] (prefixed-with? pre)

returns a predicate, which checks, if pre is a prefix of its argument.

strip-prefix

[procedure] (strip-prefix pre id)

strips the prefix pre from the identifier id.

strip-suffix

[procedure] (strip-suffix suf id)

strips the suffix suf from the identifier id.

extract

[procedure] (extract ok? tree)

returns a flat list of all the symbols in a tree which pass the ok? test.

remove-duplicates

[procedure] (remove-duplicates lst)

returns a sublist of lst with dups removed.

adjoin

[procedure] (adjoin obj lst)

conses obj to lst provided it isn't already there.

memp

[procedure] (memp ok? lst)

returns the tail of lst, whose car passes ok?, or #f otherwise.

assp

[procedure] (assp ok? tbl)

returns the table item whose car passes ok?

substitute*

[procedure] (substitute* old new tree)

substitutes old by new in a tree.

filter

[procedure] (filter ok? lst)

returns two sublists of lst where each item passes ok? or not ok? respectively.

flatten

[procedure] (flatten tree)

returns a flat list with all the items of tree.

mappend

[procedure] (mappend fn lists)

combination of map and append, i.e. mapcan in CL.

plength

[procedure] (plength pl)

returns the length of a pseudolist. For example (plength 1) is 0. Imported for-syntax and reexported by low-level-macros.

pnull?

[procedure] (pnull? xpr)

is xpr a null? pseudolist? For example (pnull? 1) is true.

plist-ref

[procedure] (plist-ref pl k)

returns the kth item of a pseudolist. Imported for-syntax and reexported by low-level-macros.

plist-tail

[procedure] (plist-tail pl k)

returns the tail, starting from k, of a pseudolist. Imported for-syntax and reexported by low-level-macros.

seq-length

[procedure] (seq-length seq)

returns the length of the generic sequence seq, presently a string, vector or (pseudo-)list.

seq-ref

[procedure] (seq-ref seq n)

returns the nth item of the generic sequence seq, presently a string, vector or (pseudo-)list.

seq-tail

[procedure] (seq-tail seq n)

returns the tail of the generic sequence seq, presently a string, vector or (pseudo-)list, starting at n.

seq-length-ref-tail!

[procedure] (seq-length-ref-tail! type? type-length type-ref type-tail)

updates the local tables of seq-length, seq-ref and seq-tail in one go by adding appropriate pairs to its front.

destruc

[procedure] (destruc pat seq)
[procedure] (destruc pat seq glen gref gtail)

helper, which does most of the work to destructure seq according to the pattern pat, a nested pseudolist of symbols and non-symbol literals. Returns three lists, to be used by dbind-ex, dbind-lit and dbind-len respectively. The second version accepts generic functions to replace plength, plist-ref and plist-tail respectively. This isn't used in low-level-macros but can be used in other modules, e.g. bindings.

dbind-ex

[procedure] (dbind-ex symbols body)

where body is a list starting with a fender expression and symbols is the first returned value of destruc. Generates a nested let expression.

dbind-lit

[procedure] (dbind-lit literals)

where literals is the second returned value of destruc. Generates code which checks if literals match.

dbind-len

[procedure] (dbind-len length-checks)

where length-checks is the third returned value of destruc. Generates code which checks if pat and seq are matchable at all.

The module low-level-macros

low-level-macros

[procedure] (low-level-macros [sym])

returns a list of all the exported symbols of the module, if called with no argument, or sym's documentation.

bind

[syntax] (bind pat seq xpr . xprs)
[syntax] (bind pat seq (where . fenders) xpr . xprs)

binds pattern variables of pat to subexpressions of seq and executes xpr . xprs in this context, provided all fenders return #t, if supplied.

Note, that non-symbol literals are accepted in pat and seq and considered a match if they are equal.

bind-case

[syntax] (bind-case seq clause ....)

where seq is a nested pseudolist expression and each clause is of one of two forms

(pat (where . fenders) xpr . xprs)
(pat xpr . xprs)

Matches seq against a series of patterns and executes the body of the first matching pattern satisfying fenders (if given).

Note, that non-symbol literals are accepted in seq and each pat and considered a match if they are equal.

define-macro

[syntax] (define-macro (name . args) xpr . xprs))
[syntax] (define-macro (name . args) (with-rename-prefix % xpr . xprs))
[syntax] (define-macro (name . args) (with-inject-prefix % xpr . xprs))
[syntax] (define-macro (name . args) (with-keywords (x . xs) xpr . xprs))
[syntax] (define-macro (name . args) (with-rename-prefix % (with-keywords (x . xs) xpr . xprs)))
[syntax] (define-macro (name . args) (with-inject-prefix % (with-keywords (x . xs) xpr . xprs)))

generates a macro name, which is of type explicit-renaming if with-rename-prefix is supplied or implicit-renaming otherwise. Keywords and prefixed symbols are extracted from the macro body transformed into appropriate subexpressions of the macro-transformer.

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

let-macro

[syntax] (let-macro ((code0 clause0) (code1 clause1)...) xpr . xprs)

where (code0 clause0), (code1 clause0), ... are as in define-macro.

This is a local version of define-macro, allowing a list of (code clause) lists to be processed in xpr . xprs in parallel.

letrec-macro

[syntax] (letre-macro ((code0 clause0) (code1 clause1)...) xpr . xprs)

where (code0 clause0), (code1 clause0), ... are as in define-macro.

This is a local version of define-macro, allowing a list of (code clause) lists to be processed in xpr . xprs recursively.

macro-rules

[syntax] (macro-rules sym ... (keyword ...) (pat0 tpl0) (pat1 tpl1) ...)

like syntax-rules, but the templates are usually quasiquote-expressions. Moreover, the symbols sym ... are injected, if there are any.

Note, that non-symbol literals are accepted in each pat and considered a match if they are equal to a corresponding literal in the macro-code. macro-rules must be imported for-syntax.

once-only

[syntax] (once-only (x ...) . body)

to be used in a macro-body to avoid side-effects. The arguments x ... are only evaluated once. once-only must be imported for-syntax.

with-gensyms

[syntax] (with-gensyms (x ...) . body)

to be used in a macro body. Generates a list of gensyms x ... with-gensyms must be imported for-syntax.

define-syntax-rule

[syntax] (define-syntax-rule (name . args) tpl)

the only high-level macro. To be used instead of syntax-rules in case there is only one rule and no additional keywords.

Requires

nothing

Usage

(require-library low-level-macros)
(import low-level-macros macro-helpers)
(import-for-syntax
  (only low-level-macros macro-rules once-only with-gensyms))

Examples

(require-library low-level-macros)
(import low-level-macros macro-helpers)
(import-for-syntax
  (only low-level-macros macro-rules once-only with-gensyms))

;; destructuring
(bind a 1 a) ; -> 1
(bind (x y z w) '(1 2 3 4) (list x y z w) ; -> '(1 2 3 4)
(bind (x (y (z))) '(1 (2 (3))) (where (odd? y)) (list x y z))
  ; -> error
(bind (x (y (z))) '(1 (2 (3))) (list x y z))
  ; -> '(1 2 3)

(letrec (
  (my-map
    (lambda (fn lst)
      (bind-case lst
        (() '())
        ((x . xs) (cons (fn x) (my-map fn xs))))))
  )
  (my-map add1 '(1 2 3)))
; -> '(2 3 4)
(bind-case '(1 2 3 4 5)
   ((a (b . C) . d) (list a b C d))
   ((e . f) (where (zero? e)) e)
   ((e . f) (list e f)))
; -> '(1 (2 3 4 5)))

(bind-case '(1 (#f 3))
   ((x y) (where (number? y)) (list x y))
   ((x ("y" . z)) (list x z))
   ((x (#f z)) (list x z)))
; -> '(1 3)

;; two anaphoric macros
(define-syntax aif
  (macro-rules it ()
    ((_ test consequent)
     `(let ((,it ,test))
        (if ,it ,consequent)))
    ((_ test consequent alternative)
     `(let ((,it ,test))
        (if ,it ,consequent ,alternative)))))

(define-macro (alambda args xpr . xprs)
  (with-inject-prefix %
    `(letrec ((,%self (lambda ,args ,xpr ,@xprs)))
       ,%self)))

;; effective membership testing
(define-macro (in? what equ? . choices)
  (let ((insym 'in))
    `(let ((,insym ,what))
       (or ,@(map (lambda (choice) `(,equ? ,insym ,choice))
                  choices)))))

;; two versions of a verbose if
(define-macro (verbose-if test (then . xprs) (else . yprs))
  (with-rename-prefix %
    (with-keywords (then else)
      `(,%if ,test
         (,%begin ,@xprs)
         (,%begin ,@yprs)))))

(define-syntax vif
  (macro-rules (then else)
    ((_ test (then xpr . xprs))
     `(if ,test
        (begin ,xpr ,@xprs)))
    ((_ test (else xpr . xprs))
     `(if ,(not test)
        (begin ,xpr ,@xprs)))
    ((_ test (then xpr . xprs) (else ypr . yprs))
     `(if ,test
        (begin ,xpr ,@xprs)
        (begin ,ypr ,@yprs)))))

;; low-level version of cond
(define-syntax my-cond
  (macro-rules (else =>)
    ((_ (else xpr . xprs))
     `(begin ,xpr ,@xprs))
    ((_ (test => xpr))
     (let ((temp test))
       `(if ,temp (,xpr ,temp))))
    ((_ (test => xpr) . clauses)
     (let ((temp test))
       `(if ,temp
          (,xpr ,temp)
          (my-cond ,@clauses))))
    ((_ (test)) test)
    ((_ (test) . clauses)
     (let ((temp test))
       `(if ,temp
          ,temp
          (my-cond ,@clauses))))
    ((_ (test xpr . xprs))
     `(if ,test (begin ,xpr ,@xprs)))
    ((_ (test xpr . xprs) . clauses)
     `(if ,test
        (begin ,xpr ,@xprs)
        (my-cond ,@clauses)))))

;; non-symbolic literals
(define-syntax foo
  (macro-rules ()
    ((_ "foo" x) x)
    ((_ #f x) `(list 'false))
    ((_ #f x) 'false)
    ((_ a b) (where (string? a)) `(list ,a ,b))
    ((_ a b) (where (odd? a)) `(list ,a ,b))
    ((_ a b) a)))
(foo "foo" 1) ; -> 1
(foo "bar" 2) ; -> '("bar" 2)
(foo #f 'blabla) ; -> '(false)
(foo 1 2) ; -> '(1 2)
(foo 2 3) ; -> 2

;; generics
(use tuples)
(seq-tail '#(0 1 2) 3) ; -> '#()
(seq-tail "foo" 1) ; -> "oo"
(seq-ref "foo" 1) ; -> #\o
(seq-tail '(0 1 2) 1) ; -> '(1 2)
;; add tuples to generic sequences
(seq-length-ref-tail! tuple?
                      tuple-length
                      tuple-ref
                      tuple-from-upto)
(seq-ref (tuple 0 1 2) 1) ; -> 1
(seq-tail (tuple 0 1 2) 1) ; -> (tuple 1 2)

Author

Juergen Lorenz

License

Copyright (c) 2011-2014, 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

Jan 23, 2014

Version History

3.2.2
destruc fixed
3.2.1
seq-tail fixed
3.2
internal documenatation added, generic updater renamed
3.1
generic sequences added in macro-helpers, destruc enhanced accordingly
3.0
complete rewrite, accepting now non-symbolic literals
2.1.1
exception-handler introduced
2.1
dependency on bindings removed, simplified versions of bind and bind-case added
2.0
complete rewrite
1.2
renamed macro-define to define-macro
1.1
fixed low-level-macros.meta and low-level-macros.setup
1.0
initial import, merging er-macros and ir-macros