[gambit-list] module system with letrec

Cyrille Duret cduret at gmail.com
Wed Sep 19 03:20:18 EDT 2012


hello,
I want to make a portable module system using lexical scoping (1).

By using a letrec form, one can write

(define my-module
  (lambda ()
    (letrec ((private-fun (lambda () ..))
                 (public-fun (lambda () ..)))
      (list 'public-fun public-fun))))

I use the let () form wich I believe is equivalent :

(define my-module
  (lambda ()
    (let ()
      (define private-fun
        (lambda () ..))
      (define public-fun (lambda () ..))
      (list 'public-fun public-fun))))

But I have an annoying limitation with letrec.
For letrec, each init expression must evaluate without referencing or
assigning the value of any of the vars.

That is a problem when I want this code to be encapsulated in a module :

(define private-fun (lambda (x) ...)
(define public-var (private-fun 'a-value))

by putting these expression into a let () form violate the scheme rules (2)
The letrec* macro does exactly what I want, unfortunately it is not
available in a r5rs system as gambit-c

The solution I see is tracking the backward references to variables or
functions declared earlier and add a new closure each time a reference is
made.
The preceding code would be translated by :

(define my-module
  (lambda ()
    (let ()
      (define private-fun (lambda (x) ..))
      (let ()
        (define public-var (private-fun 'a-value))
        (list 'public-var public-var)))))

Is it the best strategy to deal with this problem ?
Does the letrec* macro can be written for gambit-c ?

thanks
cyrille

references :
1. Using letrec and lambda to Implement Modules :
http://icem-www.folkwang-hochschule.de/~finnendahl/cm_kurse/doc/schintro/schintro_126.html#SEC163
2. letrec in the interpreter :
https://mercure.iro.umontreal.ca/pipermail/gambit-list/2005-September/000412.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20120919/c301ecf1/attachment.htm>


More information about the Gambit-list mailing list