Watch
1
0
Fork
You've already forked ASimpleTutorialForIdentifierProperties
0
a simple tutorial for identifier properties
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-08-30 23:09:18 -04:00
chez.scm init 2026-08-30 23:09:18 -04:00
README.md init 2026-08-30 23:09:18 -04:00

A Simple Tutorial for Identifier Properties

(A REPL script modified for Chez is available.)

Identifier properties are (probably) a feature of the future R7RS-Large. They allow one to associate an expand-time value to an identifier. One could do things like implement pattern matchers (SRFI 262) or add different namespaces to bindings, like the identifiers in syntax-case.

Those examples are pretty heavyweight, so I will instead show off something simpler.

Let's say we wanted to extend set! to allow for function-like sets:

(set! (operator operands ...) value)

One way to do this is to associate certain procedures with a setter field, and then replace the set! with an invocation of the procedure in the setter field: this is what SRFI 17 does. However, this is a runtime lookup. How much could we do at the syntax level? We can do this with identifier properties.

First we need to name our identifier property. Generally speaking, any identifier can serve as an identifier property, but for our purposes we will make an auxiliary keyword.

(define-syntax set!-info
  (lambda (x)
    (syntax-violation 'set!-info "identifier property" x #f)))

If we want to associate a value with the property, we use define-property.

(define-syntax add-set!-handler
  (syntax-rules ()
    ((_ name transformer)
     (define-property name set!-info
       (let ((tmp transformer))
         (unless (procedure? tmp)
           (assertion-violation 'add-set!-handler
                                "not a procedure"
                                tmp))
         tmp)))))

Here, add-set!-handler evaluates transformer to a procedure at expansion time. The procedure is a macro transformer.

Now we can define our set!.

 (define-syntax my-set!
   (lambda (x)
     (syntax-case x ()
       ((_ (operator operands ...) value)
        (cond
          ((and (identifier? #'operator)
                (identifier-property #'operator #'set!-info))
           => (lambda (transformer)
                (transformer x)))))
       ((_ id value)
        (identifier? #'id)
        #'(set! id value)))))

Note the use of identifier-property. It looks up the property of set!-info on the passed identifier operator. The transformer then tail-calls the returned value, passing control from the current macro transformer to a new transformer, dynamically.

We can give an example.

(add-set!-handler car
  (lambda (x)
    (syntax-case x ()
      ((_ (_ operands ...) value)
       #'(set-car! operands ... value)))))
(define x (cons 1 2))
(my-set! (car x) 10)
x ; => (10 . 2)

© 2026 Peter McGoron

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.