Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion generating and inserting definitions in macro expansions
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Marco Maggi  
View profile  
 More options Sep 12 2009, 7:36 am
Newsgroups: comp.lang.scheme
From: Marco Maggi <mrc....@gmail.com>
Date: Sat, 12 Sep 2009 13:36:29 +0200
Local: Sat, Sep 12 2009 7:36 am
Subject: generating and inserting definitions in macro expansions
I see that:

(import (rnrs))
(define-syntax doit
  (lambda (stx)
    (define def '(define a 123))
    (syntax-case stx ()
      ((k)
       (datum->syntax (syntax k) def)))))
(doit)
(write a)
(newline)

works; so, given:

(library (lib)
  (export
    alpha make-alpha alpha-a
    beta  make-beta  beta-b
    gamma make-gamma)
  (import (rnrs))
  (define-record-type alpha
    (fields (mutable a)))
  (define-record-type beta
    (parent alpha)
    (fields (mutable b)))
  (define-record-type gamma
    (parent beta)))

I want to automatically generate an equivalent of:

(import (rnrs)
  (lib))

(define gamma-a alpha-a)
(define gamma-b beta-b)

I do:

(import (rnrs)
  (for (lib) expand run))

(define-syntax make-record-accessor-aliases
  (syntax-rules ()
    ((_ ?record-name)
     (let-syntax
         ((A (lambda (stx)

               (define (make-record-accessor-aliases k rtd)
                 (let ((rtd-name (symbol->string (record-type-name rtd))))
                   (let process-parent ((parent-rtd (record-type-parent rtd))
                                        (result     '()))
                     (if parent-rtd
                         (let* ((slots  (record-type-field-names parent-rtd))
                                (len    (vector-length slots)))
                           (let make-def ((i    0)
                                          (defs '()))
                             (if (= i len)
                                 (process-parent (record-type-parent parent-rtd)
                                                 (append defs result))
                               (make-def (+ 1 i)
                                         (cons
                 (make-accessor k rtd-name
                 parent-rtd slots i)
                                               defs)))))
                       (begin
                         ;; (write result)
                         ;; (newline)
                         result)))))

               (define (make-accessor k rtd-name parent-rtd slots i)
                 (let ((accessor-name
                         (slot-name->accessor-name rtd-name
                                                   (vector-ref slots i)))
                       (accessor-proc
                         (record-accessor parent-rtd i)))
                   (datum->syntax k
                      `(define ,accessor-name ',accessor-proc))))

               (define (slot-name->accessor-name rtd-name slot-name)
                 (string->symbol (string-append rtd-name "-"
                                     (symbol->string slot-name))))

               (syntax-case stx ()
                 ((k)
                  (with-syntax (((B (... ...))
                                 (make-record-accessor-aliases
                                    (syntax k)
                                    (record-type-descriptor ?record-name))))
                    (syntax (begin B (... ...)))))))))
       (A)))))

(make-record-accessor-aliases gamma)
(define o (make-gamma 1 2))

which evaluates with no errors, but then:

(gamma-a o)

raises  a  "gamma-a unbound  identifier"  error.   I do  not
understand the difference between  the two cases; I tried to
structure the  second case as the  INCLUDE syntax example[1]
in the R6RS document, but I cannot spot the difference.

Suggestions?

[1] <http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-13.html#node_sec...>
--
Marco Maggi


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.