[plt-scheme] Easy module switching?

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Thu Jun 28 13:34:04 EDT 2007

> Basically I want to be able to swap between different versions of a 
> module from a central location, preferably without having to actually 
> rename the module or change the source file(s). The command line would 
> be a convenient place to be able to set it from.
>
> For example, one solution would be to have all of my files require a new 
> "router" module which would require one of the alternatives depending on 
> which I wanted to use. However, to switch between them I would actually 
> have to change the source of this router module. I want to know if there 
> is a more convenient alternative.


Hi Henk,

This is a problem that units can address.  The idea is to provide several 
implementations for a signature.  We then then conditionally link things 
depending on some runtime expression.


As a concrete (but very toy) example using the new unit system, as well as 
the cmdline library, here you go:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module example-with-units mzscheme
   (require (lib "unit.ss")
            (lib "etc.ss")
            (lib "plt-match.ss")
            (lib "cmdline.ss"))

   (define-signature sq^ (sq))

   (define-unit impl-traced@
     (import)
     (export sq^)
     (define (sq x)
       (printf "(traced sq says: ~a)~n" x)
       (* x x)))


   (define-unit impl-default@
     (import)
     (export sq^)
     (define (sq x) (* x x)))


   (define (run-main some-impl@)
     (local ((define-unit main@
               (import sq^)
               (export)
               (printf "Main program says: ~a~n" (sq 3)))

             (define-unit-binding sq@ some-impl@ (import) (export sq^)))
       (invoke-unit
        (compound-unit/infer
         (import)
         (export)
         (link sq@ main@)))))


   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   (local
       ((define impl@ (make-parameter impl-default@)))
     (command-line "example-with-units" (current-command-line-arguments)
                   (once-each
                    [("-t" "--trace") "Run with tracing"
                                      (impl@ impl-traced@)]))
     (run-main (impl@))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



If we run this from the command line, we see that the unit that's used can 
be switched:

##############################################################
dyoo at kfisler-ra1:~$ mzscheme -u example-with-units.ss --trace
(traced sq says: 3)
Main program says: 9
dyoo at kfisler-ra1:~$ mzscheme -u example-with-units.ss
Main program says: 9
##############################################################


Posted on the users mailing list.