[racket] problem with module language

From: Joop Ringelberg (joop.ringelberg at gmail.com)
Date: Thu Mar 22 15:54:38 EDT 2012

Hello all,

I try to write a small module language (named: biglooModuleSyntax.scm) 
in order to be able to run some files with a bigloo module statement in 
Racket. However, I run into a problem I do not understand at all.

The (Bigloo) code loads a library ssax-sxml. In Racket, this must be 
replaced by: (require (planet "sxml.ss" ("lizorkin" "sxml.plt" 2 1))). 
The Bigloo code also needs some extra functions not provided by sxml.ss, 
so I require an additional small module (in missing-ssax-functions.scm). 
To this end, biglooModuleSyntax.scm contains the following macro:

(define-syntax library
   (syntax-rules ()
     [(library ssax-sxml)
      (begin
        (require (planet "sxml.ss" ("lizorkin" "sxml.plt" 2 1)))
        (require "missing-ssax-functions.scm")
        )
      ]
     ))

This, however, does not produce the desired results, as shown below. Now 
there is another macro in biglooModuleSyntax.scm that handles (some) 
import forms::

(define-syntax import
   (syntax-rules ()
       [(import (moduleName path))
        (require path)]
     ))

and that works just fine. Now here is the problem. The following program 
works just fine:

;works.scm
#lang s-exp "biglooModuleSyntax.scm"
(module test
   (import (missing-ssax-functions "missing-ssax-functions.scm"))
   )
(display (SRV:send-reply 1))


where SRV:send-reply is a stub defined in missing-ssax-functions.scm 
that just returns its argument (its definition is of no importance 
here). However, the next program does NOT work:

;doesNotWork.scm
#lang s-exp "biglooModuleSyntax.scm"
(module test
   (library ssax-sxml)
   )
(display (SRV:send-reply 1))


even if the line requiring the sxml.ss module from Planet is commented out.
Racket complains: expand: unbound identifier in module in: SRV:send-reply.
It is all the more baffling because the macro expansions of both module 
statements are almost exactly the same, respectively:

'(begin (#%require "missing-ssax-functions.scm")) ;WORKS

and

'(begin (begin (#%require "missing-ssax-functions.scm"))) ;DOES NOT WORK

Replacing the module statements with either of the expansions makes the 
program(s) run as expected.
What is going on?

I would really appreciate help!

Please find the complete sources below.

Thanks in advance,

Joop Ringelberg

=============================================== BIGLOOMODULESYNTAX.SCM
#lang racket
;This module provides a bigloo module language.
;It does not aspire to be complete!

(provide import
          export
          library
          option
          module
          eval
          (except-out (all-from-out racket) #%module-begin)
          (rename-out [module-begin #%module-begin]))

(define-syntax module-begin
   (syntax-rules (module)
     [(_ (module ident clause ...)
         stmt ...)
      (#%module-begin
       (module ident clause ...)
       stmt ...)]
     ))

(define-syntax module
   (syntax-rules ()
     [(module ident clause ...)
      (begin
        clause ...)]
     ))

(define-syntax import
   (syntax-rules ()
       [(import (moduleName path))
        (require path)]
     ))

(define-syntax export
   (syntax-rules (macro)
     [(export (macro ident ...))
      (begin (provide ident) ...)]
     [(export (ident arg ...) ...)
      (begin
        (provide ident) ...)]
     [(export ident ...)
      (begin (provide ident) ...)]
     ))

(define-syntax library
   (syntax-rules ()
     [(library ssax-sxml)
      (begin
        ;(require (planet "sxml.ss" ("lizorkin" "sxml.plt" 2 1)))
        (require "missing-ssax-functions.scm")
        )
      ]
     ))

(define-syntax-rule (option ignore ...)
   (begin))

(define-syntax-rule (eval ignore ...)
   (begin))

=======================================================MISSING-SSAX-FUNCTIONS.SCM
#lang s-exp "biglooModuleSyntax.scm"
;Defines stubs for some missing ssax functions

(module missing-ssax-functions
   (export SRV:send-reply))

(define (SRV:send-reply arg)
   arg)

=======================================================WORKS.SCM
#lang s-exp "biglooModuleSyntax.scm"
(module test
   (import (missing-ssax-functions "missing-ssax-functions.scm"))
   )
(display (SRV:send-reply 1))

=======================================================DOESNOTWORK.SCM
#lang s-exp "biglooModuleSyntax.scm"
(module test
   (library ssax-sxml)
   )
(display (SRV:send-reply 1))



Posted on the users mailing list.