[plt-scheme] conditional module inclusion and compilation

From: Lee Spector (lspector at hampshire.edu)
Date: Wed Sep 2 18:55:24 EDT 2009

On Sep 2, 2009, at 5:59 PM, Eli Barzilay wrote:
>> appropriate. I do this in Common Lisp by using the #+: and #-:
>
> (You mean #+ and #-, which are usually followed by a keyword...)

Yes indeed -- I had forgotten where the ":" came from.

> Not in that way.  More than that, the fact that there is no direct
> equivalent of this is an intnetional way of avoiding the mess you can
> easily get into with CL: `*features*' is a description of the
> "currently running system" but it is used for three separate roles:
> reading, compiling, and running.  For example, if you were using

I'm going to get myself into needless trouble by saying this, and it's  
not even directly related to my question here, but one of the things  
I've always liked about CL  is that I can decide when it's worth it to  
me to risk getting into such messes...


> A much better idea is to delay the decision to where it belongs more
> naturally -- in the runtime.  But this runs into the problem you've
> seen: `require' is not an expression, so you can't put it in an `if'.
> The reason you can't do that is that `require' is used at compile-time
> to tell the compiler how to compile and link the code (think about
> requiring a module that provides some macro that you're using: you
> *need* it at compile time then).

Actually, I think that everything I want to do here belongs most  
naturally at compile time. I have a single source file that I want to  
be able to compile and run in two different environments. In one  
environment I'd like the compiler to skip (require plot) and I'd also  
like it to compile one definition instead of another. So in CL I might  
do:

#+:MACOSX(require plot)

#+:MACOSX(defun report-plot () <do stuff here that produces graphics  
using stuff in the plot module>)

#-:MACOSX(defun report () (values)) ;; just do nothing if I'm not in  
macosx, which also means I don't have the plot module

All of this would most naturally be done at compile time -- under  
macosx the require would be done at compile time and the first  
definition of report-plot would be compiled, while in other  
environments the require would be skipped and the second definition of  
report-plot would be compiled.

SO -- looking back at something else in your message I saw that you  
had actually shown me how to do this! This may not be the most elegant  
implementation but it works:

(define-syntax (in-macosx stx)
   (if (eq? 'macosx (system-type))
      (syntax-case stx ()
        ((_ form)
         #`form))
      #`(void)))

(define-syntax (in-unix stx)
   (if (eq? 'unix (system-type))
      (syntax-case stx ()
        ((_ form)
         #`form))
      #`(void)))

(in-macosx (require plot))

(in-macosx
  (define plot-data
    (lambda (data x-label y-label)
      (plot (points (for/list ((i (number-list (length data))))
                      (vector i (list-ref data i)))
                    #:sym 'oplus)
            #:x-min 0
            #:x-max (length data)
            #:y-min 0
            #:y-max (+ 1 (apply max data))
            #:x-label x-label
            #:y-label y-label))))

(in-unix
  (define plot-data
    (lambda x (void))))


So my problem is solved! Thanks so much.

  -Lee

PS Thanks also to Matthias for the suggestion of using mred and thanks  
to Eli for explaining the problems I was having in trying it!

--
Lee Spector, Professor of Computer Science
School of Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspector at hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438

Check out Genetic Programming and Evolvable Machines:
http://www.springer.com/10710 - http://gpemjournal.blogspot.com/



Posted on the users mailing list.