[plt-scheme] Checking to see if some variable is an mzscheme primitive or not?

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Fri Feb 18 04:11:46 EST 2005

Hi everyone,

I'm a PLT Scheme newbie, so if my code below is hideous, please attribute
it to ignorance, and not malice.  *grin*


I'm trying to write a function that will help me trace down the names of
mzscheme primitives in a program, and I've written the following module:

;;;;;;;
(module check-bound mzscheme
  ;; Checks to see if a name is already bound.
  ;; Danny Yoo (dyoo at hkn.eecs.berkeley.edu)

  (provide bound? bound-in-namespace? bound-in-mzscheme-namespace?)

  ;; I have no clue if there's a better way to do this.
  (define failure?
    exn?)
;     (if (> (string->number (version)) 209)
; 	exn:fail?
; 	exn:variable?))


  ;; Returns true if the name is bound is some namespace
  (define (bound-in-namespace? name ns)
      (with-handlers ((failure? (lambda (e) #f)))
		     (and (symbol? name)
			  (parameterize ((current-namespace ns))
					(eval name))
			  #t)))


  ;; Returns true if the name is bound in the current namespace.
  (define (bound? name)
    (bound-in-namespace? name (current-namespace)))


  ;; Returns true if the name is bound in the primitive mzscheme
  ;; namespace.
  (define (bound-in-mzscheme-namespace? name)
    (bound-in-namespace? name (make-namespace)))
)
;;;;;;


For example:

######
volado:~/work/scratch/2005/plt dyoo$ ~/work/plt/bin/mzscheme
Welcome to MzScheme version 299.31, Copyright (c) 2004-2005 PLT Scheme,
Inc.
> (require "check-bound.ss")
> (bound? 'bound?)
#t
> (bound-in-mzscheme-namespace? 'bound?)
#f
> (bound? 'x)
#f
> (define x 42)
> (bound? 'x)
#t
######


I know I shouldn't really be abusing exceptions like this, but this is
something I need for a silly preprocessor program.  I couldn't find
equivalent functionality in mzlib yet, but I'm just double checking to
make sure I haven't reinvented the wheel.


Thanks in advance!



Posted on the users mailing list.