[plt-scheme] flavoured modules

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Sat Jul 21 15:26:02 EDT 2007

On Sat, 2007-07-21 at 20:48 +0200, Jos Koot wrote:
> 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

Have you looked at the 'include' macro? You could write your 'example-1'
macro from below as follows:

(module example-1 mzscheme
  (require (lib "include.ss")
  (include "part1.scm")
  (include "part2.scm")
  (include "part3.scm"))

That might serve your purposes better than a new language module.

I'll try to send an explanation of the two lines later this evening. The
short answer: Try it out in the macro stepper (you may have to disable
macro hiding; there's a bug lurking in the module-handling code). Notice
that the require forms and all of the inserted code have the same
mark/color.

Ryan

>  
> ; 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. |#
>  
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.