<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.6000.16414" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV>If you are interested in module top level bindings only, you'll have
to:</DIV>
<DIV> </DIV>
<DIV>0: keep record of module top level variables (e.g. in a hash table)</DIV>
<DIV>1: Redefine syntax define such as to recognize whether or not it is a
module top level define by means of procedure syntax-local-context. If it does
not return a top level, expand to normal define. If the define form is at top
level, consult the record of variables and act accordingly.</DIV>
<DIV>2: You also may want to redefine define-values in this way.</DIV>
<DIV>3: The above should be done in a separate module defining a complete
language (i.e. all of mzscheme except the define forms, which can be exported by
(provide (rename ...))</DIV>
<DIV>4: Use the language as in (module module-name my-mzscheme-module
language)</DIV>
<DIV> </DIV>
<DIV>Example:</DIV>
<DIV> </DIV>
<DIV>(module my-mzscheme-module-language mzscheme<BR> (require (only
(lib "etc.ss") namespace-defined?))<BR> (define-for-syntax top-var-hash
(make-hash-table))</DIV>
<DIV> </DIV>
<DIV> (define-syntax (my-define stx)<BR> (syntax-case stx
()<BR> ((my-define (name . args) . body) #'(my-define name (lambda
args . body)))<BR> ((my-define var value)<BR>
(if</DIV>
<DIV> (let ((binding-type
(identifier-binding #'var)))</DIV>
<DIV> (and binding-type (not (eq?
binding-type 'lexical))))<BR> #'(define var
value))<BR> (let ((var-name (syntax-object->datum
#'var)))<BR> (if (hash-table-get top-var-hash
var-name (lambda () #f))<BR> #'(set! var
value)<BR> (begin (hash-table-put!
top-var-hash var-name #t) #'(define var
value)))))))<BR> <BR> (provide (all-from-except mzscheme define)
(rename my-define define)))</DIV>
<DIV> </DIV>
<DIV>(module use-my-mzscheme-module-language
my-mzscheme-module-language<BR> (define a 1)<BR> (define a
2)<BR> (define (b x) (define y (add1 x)) y)<BR> (provide a b))</DIV>
<DIV> </DIV>
<DIV>(module use mzscheme<BR> (require
use-my-mzscheme-module-language)<BR> (printf "~a~n" a)<BR> (printf
"~a~n" (b 2)))</DIV>
<DIV> </DIV>
<DIV>(require use) ; prints: 2 newline 3 newline<BR></DIV>
<DIV> </DIV>
<DIV>(((((lambda(x)((((((((x x)x)x)x)x)x)x)x))<BR>
(lambda(x)(lambda(y)(x(x y)))))<BR> (lambda(x)(x)x))<BR>
(lambda()(printf "Greetings, Jos~n"))))</DIV>
<BLOCKQUOTE
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style="FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV
style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B>
<A title=yinso.chen@gmail.com href="mailto:yinso.chen@gmail.com">Yin-So
Chen</A> </DIV>
<DIV style="FONT: 10pt arial"><B>To:</B> <A title=jos.koot@telefonica.net
href="mailto:jos.koot@telefonica.net">jos koot</A> </DIV>
<DIV style="FONT: 10pt arial"><B>Cc:</B> <A title=plt-scheme@list.cs.brown.edu
href="mailto:plt-scheme@list.cs.brown.edu">mzscheme Mailing List</A> </DIV>
<DIV style="FONT: 10pt arial"><B>Sent:</B> Sunday, April 22, 2007 12:32
PM</DIV>
<DIV style="FONT: 10pt arial"><B>Subject:</B> Re: [plt-scheme] verifying
whether a symbol is defined at compile time?</DIV>
<DIV><BR></DIV>Hi Jos - <BR><BR>Thanks for the quick response and the helpful
answer (the references/example are great). My motivation for having a
conditional define is to make it play nice with the module system, which
doesn't work well when having multiple definitions with the same name. I
was hoping there is an "automated" way of detecting whether a symbol is
already defined in the current module, if so, either override or not import
the definition. <BR><BR>Of course this is probably circumventing
module's design (seems that module is *a lot* stricter than REPL by design),
but I am certainly curious on how this could be done. Seems like that
compiler would have to keep symbol table(s) internally for all of the defined
symbols in order to throw the "already defined" error... ;) <BR><BR>Thanks,
<BR>yinso <BR><BR><BR>
<DIV><SPAN class=gmail_quote>On 4/22/07, <B class=gmail_sendername>jos
koot</B> <<A
href="mailto:jos.koot@telefonica.net">jos.koot@telefonica.net</A>>
wrote:</SPAN>
<BLOCKQUOTE class=gmail_quote
style="PADDING-LEFT: 1ex; MARGIN: 0pt 0pt 0pt 0.8ex; BORDER-LEFT: rgb(204,204,204) 1px solid">
<DIV bgcolor="#ffffff">
<DIV>Oops, the contra-example should read:</DIV>
<DIV dir=ltr> </DIV>
<DIV dir=ltr>(require (lib "etc.ss"))</DIV>
<DIV> </DIV>
<DIV dir=ltr><SPAN class=q>(define-syntax my-define<BR> (syntax-rules
()<BR> ((my-define (name . args) . body) (my-define name (lambda args
. body)))<BR> ((my-define var value)<BR></SPAN> (define
var ; <== correction<SPAN class=q><BR>
(begin<BR> (printf "my-define ~a status is ~s~n"
'var<BR> (if (namespace-defined? 'var)
'defined 'undefined))<BR>
value)))))</SPAN></DIV><SPAN class=q>
<DIV> </DIV>
<DIV dir=ltr>(if (read) (namespace-set-variable-value! 'b 2))<BR>(my-define
b 2)</DIV>
<DIV dir=ltr></DIV>
<DIV dir=ltr> </DIV>
<DIV dir=ltr>Jos Koot</DIV></SPAN></DIV></BLOCKQUOTE></DIV><BR><BR
clear=all><BR>-- <BR><A
href="http://www.yinsochen.com">http://www.yinsochen.com</A><BR>...continuous
learning... </BLOCKQUOTE></BODY></HTML>