Mon Jan 24 10:12:05 EST 2011

Syntax parameters

To implement the `state-space' form above it's probably best to use
syntax parameters.

These are strange though, and remind me of my previous encounters with
`let-syntax'.  The idea is that the transformer phase code is
_generated_, i.e. it doesn't now about the transformer code outside of
#`(syntax-parameterize ...)

It's working, but with a little bit of "foefelare".  Look at the way
the `state' and `out' pattern variables are propageted into the
expansion-time part of the `syntax-parameterize' expression.  This is
a staged macro, and not a lexical substitution as I first expected!

(define-syntax-parameter model-update #f)

(define-syntax (model-lambda stx)
  (define (unpack vec ids)
    (for/list ((id (syntax->list ids))
               (n (in-naturals)))
      #`(#,id (vector-ref #,vec #,n))))
  (syntax-case stx ()
    ((_ (state in out)
        . body)
          (lambda (stx)
            (define (context sym)
              (datum->syntax stx sym))
            (syntax-case stx ()
              ((_ bindings)
               #`(let bindings
                      (vector #,@(map context 'state))
                      (vector #,@(map context 'out)))))))))
        (lambda (vstate vin)
          (let (#,@(unpack #'vstate #'state)
                #,@(unpack #'vin    #'in))
            . body)

(model-lambda ((s1 s2)  ;; state
               (i1 i2)  ;; input
               (o1 o2)) ;; output
   ((s1 (+ s1 s2))
    (s2 (- s1 s2))
    (o1 (* s1 i1))
    (o2 (* s2 i2)))))

[1] http://macrologist.blogspot.com/2006/04/macros-parameters-binding-and.html