        (printer (and (pair? name-form) (cadr name-form)))
         (slot-names (map (lambda (slot) (if (pair? slot) (car slot) slot))
                          slots))
         (stem (trim-brackets name)))
    `(begin
       (define ,name (make-record-type ,(symbol->string name) ',slot-names
                                       ,@(if printer (list printer) '())))
       (define ,(symbol-append 'make- stem)
         (let ((slots (list ,@(map (lambda (slot)
                                     (if (pair? slot)
                                         `(cons ',(car slot) ,(cadr slot))
                                         `',slot))
                                   slots)))
               (constructor (record-constructor ,name)))
           (lambda args
             (apply constructor (%compute-initargs args slots)))))
       (define ,(symbol-append stem '?) (record-predicate ,name))
       ,@(map (lambda (sname)
                `(define ,(symbol-append stem '- sname)
                   (make-procedure-with-setter
                    (record-accessor ,name ',sname)
                    (record-modifier ,name ',sname))))
              slot-names))))

(define (%compute-initargs args slots)
  (define (finish out)
    (map (lambda (slot)
           (let ((name (if (pair? slot) (car slot) slot)))
             (cond ((assq name out) => cdr)
                   ((pair? slot) (cdr slot))
                   (else (error "unbound slot" args slots name)))))
         slots))
  (let lp ((in args) (positional slots) (out '()))
    (cond
     ((null? in)
      (finish out))
     ((keyword? (car in))
      (let ((sym (keyword->symbol (car in))))
        (cond
         ((and (not (memq sym slots))
               (not (assq sym (filter pair? slots))))
          (error "unknown slot" sym))
         ((assq sym out) (error "slot already set" sym out))
         (else (lp (cddr in) '() (acons sym (cadr in) out))))))
     ((null? positional)
      (error "too many initargs" args slots))
     (else
      (lp (cdr in) (cdr positional)
          (let ((slot (car positional)))
            (acons (if (pair? slot) (car slot) slot)
                   (car in)
                   out)))))))

;;; FIXME: Re-write uses of `record-case' to use `match' instead.
(define-syntax record-case
  (lambda (x)
    (syntax-case x ()
      ((_ record clause ...)
       (let ((r (syntax r))
             (rtd (syntax rtd)))
         (define (process-clause tag fields exprs)
           (let ((infix (trim-brackets (syntax->datum tag))))
             (with-syntax ((tag tag)
                           (((f . accessor) ...)
                            (let lp ((fields fields))
                              (syntax-case fields ()
                                (() (syntax ()))
                                (((v0 f0) f1 ...)
                                 (acons (syntax v0)
                                        (datum->syntax x 
                                                       (symbol-append infix '- (syntax->datum
                                                                                (syntax f0))))
                                        (lp (syntax (f1 ...)))))
                                ((f0 f1 ...)
                                 (acons (syntax f0)
                                        (datum->syntax x 
                                                       (symbol-append infix '- (syntax->datum
                                                                                (syntax f0))))
                                        (lp (syntax (f1 ...))))))))
                           ((e0 e1 ...)
                            (syntax-case exprs ()
                              (() (syntax (#t)))
                              ((e0 e1 ...) (syntax (e0 e1 ...))))))
               (syntax
                ((eq? rtd tag)
                 (let ((f (accessor r))
                       ...)
                   e0 e1 ...))))))
         (with-syntax
             ((r r)
              (rtd rtd)
              ((processed ...)
               (let lp ((clauses (syntax (clause ...)))
                        (out '()))
                 (syntax-case clauses (else)
                   (()
                    (reverse! (cons (syntax
                                     (else (error "unhandled record" r)))
                                    out)))
                   (((else e0 e1 ...))
                    (reverse! (cons (syntax (else e0 e1 ...)) out)))
                   (((else e0 e1 ...) . rest)
                    (syntax-violation 'record-case
                                      "bad else clause placement"
                                      (syntax x)
                                      (syntax (else e0 e1 ...))))
                   ((((<foo> f0 ...) e0 ...) . rest)
                    (lp (syntax rest)
                        (cons (process-clause (syntax <foo>)
                                              (syntax (f0 ...))
                                              (syntax (e0 ...)))
                              out)))))))
           (syntax
            (let* ((r record)
                   (rtd (struct-vtable r)))
              (cond processed ...)))))))))


;; Here we take the terrorism to another level. Nasty, but the client
;; code looks good.

(define-macro (transform-record type-and-common record . clauses)
  (let ((r (module-gensym "rec"))
        (rtd (module-gensym "rtd"))
        (type-stem (trim-brackets (car type-and-common))))
    (define (make-stem s)
      (symbol-append type-stem '- s))
    (define (further-predicates x record-stem slots)
      (define (access slot)
        `(,(symbol-append (make-stem record-stem) '- slot) ,x))
      (let lp ((in slots) (out '()))
        (cond ((null? in) out)
              ((pair? (car in))
               (let ((slot (caar in))
                     (arg (cadar in)))
                 (cond ((symbol? arg)
                        (lp (cdr in) out))
                       ((pair? arg)
                        (lp (cdr in)
                            (append (further-predicates (access slot)
                                                        (car arg)
                                                        (cdr arg))
                                    out)))
                       (else (lp (cdr in) (cons `(eq? ,(access slot) ',arg)
                                                out))))))
              (else (lp (cdr in) out)))))
    (define (let-clauses x record-stem slots)
      (define (access slot)
        `(,(symbol-append (make-stem record-stem) '- slot) ,x))
      (let lp ((in slots) (out '()))
        (cond ((null? in) out)
              ((pair? (car in))
               (let ((slot (caar in))
                     (arg (cadar in)))
                 (cond ((symbol? arg)
                        (lp (cdr in)
                            (cons `(,arg ,(access slot)) out)))
                       ((pair? arg)
                        (lp (cdr in)
                            (append (let-clauses (access slot)
                                                 (car arg)
                                                 (cdr arg))
                                    out)))
                       (else
                        (lp (cdr in) out)))))
              (else
               (lp (cdr in)
                   (cons `(,(car in) ,(access (car in))) out))))))
    (define (transform-expr x)
      (cond ((not (pair? x)) x)
            ((eq? (car x) '->)
             (if (= (length x) 2)
                 (let ((form (cadr x)))
                   `(,(symbol-append 'make- (make-stem (car form)))
                     ,@(cdr type-and-common)
                     ,@(map (lambda (y)
                              (if (and (pair? y) (eq? (car y) 'unquote))
                                  (transform-expr (cadr y))
                                  y))
                            (cdr form))))
                 (error "bad -> form" x)))
            (else (cons (car x) (map transform-expr (cdr x))))))
    (define (process-clause clause)
      (if (eq? (car clause) 'else)
          clause
          (let ((stem (caar clause))
                (slots (cdar clause))
                (body (cdr clause)))
            (let ((record-type (symbol-append '< (make-stem stem) '>)))
              `((and (eq? ,rtd ,record-type)
                     ,@(reverse (further-predicates r stem slots)))
                (let ,(reverse (let-clauses r stem slots))
                  ,@(if (pair? body)
                        (map transform-expr body)
                        '((if #f #f)))))))))
    `(let* ((,r ,record)
            (,rtd (struct-vtable ,r))
            ,@(map (lambda (slot)
                     `(,slot (,(make-stem slot) ,r)))
                   (cdr type-and-common)))
       (cond ,@(let ((clauses (map process-clause clauses)))
                 (if (assq 'else clauses)
                     clauses
                     (append clauses `((else (error "unhandled record" ,r))))))))))
