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

MzScheme library question



Hi,
Is it correct to post questions about MzScheme in this list? I hope so. I
would be much obliged if you can point me to a reasonably simple way to
construct a tool for preparing and loading/requiring libraries meeting the
following criteria:
1: the values returned by the library do not depend on the environment in
which the library is loaded/required, i.e. no need for hash-percent, except
may be in the expansion of macros.
2: an option to prevent the library from overriding the value or syntactic
binding of any variable that at load/require time happens already to be
bound in the global namespace of the calling program. Say
(require-library-dont-override name collects ...)
3: an option for the specification of symbols that may be redefined despite
of requirement 2, say (require-library-dont-override-except (symbol ...)
name collects ...)
4: load/require commands within a library preferably do not load a copy if
already loaded. May be loading can even be lazy.
5: if the library loads/requires other libraries, it must still meet the
above conditions
I have played with units and namespaces, but my trials look clumsy and much
too complicated. I am sure there is a more obvious way. Below you find what
I have tried. It does not work for macro definitions (and is useful for top
level loads only: how about a let-library or with-library form?) Help will
be much appreciated. Sorry about the length of this mail. Kind regards, Jos
Jacob J. A. Koot

; trial library, for global value bindings only.
; not affecting existing global vars, i hope.
; insensitive to redefinition of global bindings, i hope.

(#%define-values (a b c foldl foldr)
 (#%let-values
  (((new-a new-b new-c new-foldl new-foldr binder)
    (#%parameterize
     ((#%current-namespace
       (#%make-namespace (#%quote all-syntax) (#%quote keywords))))
     (invoke-unit ; here we dont need #%
      (unit (import) (export)
       (require-library "functio.ss") ; not sure how this works at this spot
       (define foldl (global-defined-value 'foldl)) ; this is not nice
       (define foldr (global-defined-value 'foldr)) ; this is not nice
       (define x 4)
       (define a 5)
       (define b 6)
       (define c (lambda (z) (+ a x z)))
       (define binder
        (lambda (x new-x)
         (if (defined? x) (global-defined-value x) new-x)))
       (values a b c foldl foldr binder))))))
  (#%apply #%values
   (#%map binder (#%quote (a b c foldl foldr))
    (#%list new-a new-b new-c new-foldl new-foldr)))))

;;===========================================================
;; program using the above library
;
;(define a 33)     ; the library must not affect this binding
;(define foldl 44) ; the library must not affect this binding
;
;; perversely disable some primitives.
;; nevertheless the library should work
;
;(define list (void))
;(define values (void))
;(define define-values (void))
;(define current-namespace (void))
;
;(require-library "synrule.ss")
;(define-syntax require-private-library
; (syntax-rules ()
;  ((require-private-library name)
;   (require-library name ; is there a simpler way for this?
;    ".." ".." ".." ".." "mijn documenten" "scheme" "libraries"))))
;(require-private-library "trial library.scm")
;
;a     ; --> 33  not affected, ok
;b     ; --> 6   defined by unit, ok
;(c 3) ; --> 12  ok, c uses correct bindings
;foldl ; --> 44  not affected, ok
;foldr ; --> #<proc:foldr> defined by library, ok