[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From modules, macros and geniuses.



I never thought defining those hygienic syntax-transformers could
provide so much fun. But, indeed, it did. Syntax-case rules! So here I
present - without even the implied hope that sombody might find this
useful - a set of macros that make it possible to comfortably:

- import only a subset of the identifiers of a certain module with:
(require-from <module> 
  <name0> 
  <name1> 
   ...)

- import these identifiers in prefixed form with:
(require-prefix-from <prefix> <module> 
  <name0> 
  <name1>
   ...)

- export some prefixed identifiers without their prefix with:
(provide-prefixless <prefix> 
  <name0> 
  <name1> 
   ...)

These macros use those most general mzscheme-syntaxes:
(require (rename <module> <local-name> <imported-name>))
(provide (rename          <local-name> <exported-name>))

To make use of the macros, simply import the module where they are
defined with:
(require module-macros)  ;; Creative name, heh?

Best regards,
Sebastian


PS: Here are the macros and a little example of usage:


(module module-macros  mzscheme
  (provide require-from
           require-prefix-from
           provide-prefixless
           )
  (define-syntax require-from
    ;; <module> <identifier>+ -> unspecified
    ;; Require from <module> specified <identifier>s, only.
    (lambda (stx)
      (syntax-case stx ()
        ((_ module id0 id1 ...) (syntax (require
                                         (rename module id0 id0)
                                         (rename module id1 id1)
                                         ...))))))
  (define-syntax require-prefix-from
    ;; <prefix> <module> <identifier>+ -> unspecified
    ;; From <module>, import specified <identifier>s, only. 
    ;; To each imported <identifier>, assign the 
    ;; name <prefix><identifier>.
    (lambda (stx)
      (syntax-case stx ()
        ((_ pre mod e0 e1 ...)
         (letrec ((prefix  
                   ;; Make Prefix-String.
                   (symbol->string
                    (syntax-object->datum
                     (syntax pre))))
                  (prefixer
                   ;; <symbol> -> <list>
                   ;;  name -> (prefixed-name name)
                   (lambda (name)
                     (list (string->symbol
                            (string-append prefix
                                           (symbol->string name)))
                           name))))
           (syntax-case (map prefixer
                             (cdddr (syntax-object->datum stx))) ()
             (((prefixed-name name) ...)
              (syntax (require (rename mod prefixed-name name) 
                               ...)))))))))
  (define-syntax provide-prefixless
    ;; <prefix> <identifier>+ -> unspecified
    ;; Export every <identifier> without its local <prefix>.
    (lambda (stx)
      (syntax-case stx ()
        ((_ pre e0 e1 ...)
         (letrec ((prefix  
                   ;; Make Prefix-String.
                   (symbol->string
                    (syntax-object->datum
                     (syntax pre))))
                  (prefixer
                   ;; <symbol> -> <list>
                   ;;  name -> (prefixed-name name)
                   (lambda (name)
                     (list (string->symbol
                            (string-append prefix
                                           (symbol->string name)))
                           name))))
           (syntax-case (map prefixer
                             (cddr (syntax-object->datum stx))) ()
             (((prefixed-name name) ...)
              (syntax (provide (rename prefixed-name name) 
                               ...)))))))))
  )

(module Germans mzscheme
  (require module-macros)
  (provide-prefixless Albert: Einstein Schweizer)
  (define Albert:Einstein 'physicist)
  (define Albert:Schweizer 'physician))

(require module-macros)
(require-prefix-from Ingenious: Germans 
  Einstein 
  Schweizer)

Ingenious:Einstein  ;; ==> physicist
Ingenious:Schweizer ;; ==> physician