[plt-scheme] Null syntax / Conditional definition

From: Eli Barzilay (eli at barzilay.org)
Date: Sat Jun 18 12:39:44 EDT 2005

On Jun 19, Chihiro Kuraya wrote:
> 
> What I want to do is defining ABC macro
> which is not specific to string-append.
> 
> For example, I want also (list 123 ABC 456)
> to be expanded to (list 123 456).

What Doug said -- redefining `#%app' is your best bet.  It wouldn't
work for macros, but there is really no sane way to make it work for
macros, for example (define ABC 3), or a macro that happens to use ABC
as a keyword.


> > (define ABC
> >    (case (system-type)
> >      ((windows) 123)
> >      ((macosx) 567)
> >      (else #f)))
> > 
> > (define-syntax XYZ
> >    (case (system-type)
> >      ((windows)
> >       (lambda (x)
> >         (syntax-case x ()
> >           ((_ arg0 ...)
> >            #'(display "Win")))))
> >      ((macosx)
> >       (lambda (x)
> >         (syntax-case x ()
> >           ((_ arg0 ...)
> >            #'(display "Mac")))))
> >      (else
> >       (lambda (x)
> >         (syntax-case x ()
> >           ((_ arg0 ...)
> >            #'(display "???")))))))
> 
> I expect all definitions not intended for Windows
> are stripped after syntax expansion.
> The above code doesn't suffice my expectation.
> Is it possible ?

You have to decide when you inspect the OS, either compile time or run
time.  With compile-time you have the advantage of eliminating the
other code, but the disadvantage of requiring compilation to happen on
the platform where you want it to run.  The code that Jordan wrote
mixes the two -- it uses run-time for normal definitions and
compile-time for syntax.  This might be what you want, but here's a
way to make normal definitions rely on the OS at compile time:

  (define-syntax (OS-dep stx)
    (syntax-case ()
      [(_ u w)
       (case (system-type)
         [(unix)    #'u]
         [(windows) #'w])]))
  (OS-dep (define foo ...)
          (define foo ...))

You need to do it with syntax case.  (Should really be made into
something more generic, with a case-like syntax.)

OTOH, if you want to do everything at run-time, including syntax, then
the solution is not to do these decisions in the syntax expansion, but
to generate code that has the `case'.  (If you restrict yourself to
(define-syntax foo (syntax-rules ...)) then you have to do it this
way.)

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!



Posted on the users mailing list.