[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?
;     (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))

  ;; 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,
> (require "check-bound.ss")
> (bound? 'bound?)
> (bound-in-mzscheme-namespace? 'bound?)
> (bound? 'x)
> (define x 42)
> (bound? 'x)

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.