How to detect a special variable

Discussion of Common Lisp
Post Reply
sinnatagg
Posts: 29
Joined: Tue Apr 21, 2009 3:04 am

How to detect a special variable

Post by sinnatagg » Mon Jan 24, 2011 4:59 am

You can find out if a symbol is bound to a variable with boundp(), but is it possible to detect if the variable is bound in special scope ?

-andré

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: How to detect a special variable

Post by ramarren » Mon Jan 24, 2011 5:25 am

Not with that standard functionality. SBCL has SB-CLTL2:VARIABLE-INFORMATION function, which can check that. For interactive use DESCRIBE will show top level special variables. What is your use case for such information anyway? A naming convention is usually sufficient.

sinnatagg
Posts: 29
Joined: Tue Apr 21, 2009 3:04 am

Re: How to detect a special variable

Post by sinnatagg » Mon Jan 24, 2011 1:07 pm

I just want a simple debugging scheme which binds certain data to a top-level special variable if it's set. I know it's not a particularly useful idea, mostly just a quick hack :D But now I started to wonder if it's possible.

I guess it's superfluous to check for specialness; if the variable is bound and it's not in lexical scope which the function controls, there are few other possibilities than that it is special.

Does anybody know the corresponding functionality for LispWorks ?


-andré

Warren Wilkinson
Posts: 117
Joined: Tue Aug 10, 2010 11:24 pm
Location: Calgary, Alberta
Contact:

Re: How to detect a special variable

Post by Warren Wilkinson » Mon Jan 24, 2011 1:55 pm

Would something like this work?

Code: Select all

(defvar *debug-store* nil)
(defmacro debug-store (value) `(setf *debug-store* ,value))
Need an online wiki database? My Lisp startup http://www.formlis.com combines a wiki with forms and reports.

sinnatagg
Posts: 29
Joined: Tue Apr 21, 2009 3:04 am

Re: How to detect a special variable

Post by sinnatagg » Mon Jan 24, 2011 2:04 pm

Since symbol-value() doesn't see lexical variables I think this is the easiest way.

Code: Select all

(if (boundp '*debug*)
  (setf (symbol-value '*debug*) local-value))
Happy hacking

-andré

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: How to detect a special variable

Post by ramarren » Mon Jan 24, 2011 11:32 pm

sinnatagg wrote:Since symbol-value() doesn't see lexical variables I think this is the easiest way.
BOUNDP doesn't see lexical variables either, see the note at the end of the Hyperspec entry. This has to be so, because it is a function, and lexical bindings names can be removed at run-time and replaced with direct memory accesses as an optimization during compilation, which is why checking lexical variables information by name requires additional compiler/interpreter support.

vsedach
Posts: 8
Joined: Wed Dec 17, 2008 1:59 pm
Location: Montreal, QC, CA
Contact:

Re: How to detect a special variable

Post by vsedach » Tue Jan 25, 2011 9:45 pm

The PORT package of CLOCC has a function to tell if a symbol is a special variable in sys.lisp:

Code: Select all

(defun variable-special-p (symbol)
  "Return T if the symbol names a global special variable."
  #+(and allegro (not (version>= 6))) (clos::variable-special-p symbol nil)
  #+(and allegro (version>= 6)) (excl::variable-special-p symbol nil)
  #+clisp (sys::special-variable-p symbol)
  #+cmu (walker:variable-globally-special-p symbol)
  #+gcl (si:specialp symbol)
  #+lispworks (eq :special (hcl:variable-information symbol))
  #+lucid (system:proclaimed-special-p symbol)
  #+sbcl (sb-walker:var-globally-special-p symbol)
  #-(or allegro clisp cmu gcl lispworks lucid sbcl)
  (error 'not-implemented :proc (list 'variable-special-p symbol)))
I'm not sure if this is up-to-date will all the latest implementation internals.

Post Reply