[plt-scheme] flavoured modules

From: Jos Koot (jos.koot at telefonica.net)
Date: Sat Jul 21 14:48:36 EDT 2007

Hi,
With help of the PLT-list I succeeded in making flavoured or composable modules, as I call them. Thanks Matthias and Scott! I am afraid though, that I dont fully understand what I am doing in the two lines marked with <======.
Jos Koot

; Definitions window

(module compose-module-body mzscheme
 (provide (rename compose-module-body #%module-begin))
 
 (define-for-syntax (reader filename-string)
  (with-input-from-file (syntax-object->datum filename-string) read-syntax))
 
 (define-for-syntax (map-local-introduce stx)
  (map syntax-local-introduce (syntax->list stx)))
 
 (define-syntax (compose-module-body stx)
  (syntax-case stx ()
   ((_ (requirement ...) (syntax-requirement ...) filename-string ...)
  #`(#%module-begin
     (require #,@(map-local-introduce #'(requirement ...))) ; <======
     (require-for-syntax #,@(map-local-introduce #'(syntax-requirement ...))) ; <======
  #,@(map reader (syntax->list #'(filename-string ...))))))))

#| Usage:

(module <module-name> compose-module-body
 (require-spec ...)
 (syntax-require-spec ...)
 <filename-string> ...)

>From each file one single sexpr (possibly a begin form) is read and inserted in the module.
The sexprs must be considered to form one single module body.
 
Examples:
|#

(with-output-to-file "part1.scm"
 (lambda () (write '(provide syn))) #;'replace)
(with-output-to-file "part2.scm"
 (lambda () (write '(define var "It works!"))) #;'replace)
(with-output-to-file "part3.scm"
 (lambda () (write '(define-syntax syn (syntax-rules () ((_) var))))) #;'replace)

(module example-1 compose-module-body 
 (mzscheme) (mzscheme)
 "part1.scm" "part2.scm" "part3.scm")

(require example-1)
(syn) ; --> "It works"

(with-output-to-file "wrong-part.scm"
 (lambda () (write 'unbound-identifier)) #;'replace)

(module example-2 compose-module-body
 (mzscheme) (mzscheme)
 "wrong-part.scm")

(require example-2) ; icon wrong-part.scm::1: compile: unbound variable in module in: unbound-identifier

#| Interaction window:

Welcome to DrScheme, version 370.6-svn21jul2007 [3m].
Language: Textual (MzScheme, includes R5RS).
"It works!"
icon wrong-part.scm::1: expand: unbound variable in module in: unbound-identifier
> 

Clicking on the icon opens the erroneous file. Very nice. |#
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20070721/cf10eb9f/attachment.html>

Posted on the users mailing list.