Subject: Re: bug#2951: Suggestion: self-evaluating-p function

From: Ralph Schleicher <[email-masked]>
Date: Sat, 11 Apr 2009 23:35:04 +0200

Stefan Monnier <[email-masked]> writes:

>> I would like to have a 'self-evaluating-p' function in Emacs to check
>> whether or not a value has to be quoted.
>
> I'm not necessarily opposed, but I'd first hear some arguments
> explaining why/when one would need that.

Okay, this is probably best explained by an example.  I'm working on
an Emacs package for parsing numbers in a flexible way.  That means
the regular expression string matching a number is a function of the
selected numeral system and set of numbers to match.

While some numeral systems have really different notations for numbers,
other systems are quite close to each other.  This is especially true
for numeral systems of programming languages.  Therefore it is natural
to define related numeral systems by inheritance:

     (define-numeral-system Fortran
       '((number-numerals "0123456789")
         (number-zero "0")
         (number-plus "+")
         (number-minus "-")
         (number-radix ".")
         (number-exponent "DEde")
         ;; ...
         )
       "Numeral system for the Fortran programming language.")
     
     (define-numeral-system C
       (with-numeral-system 'Fortran
         (setq number-exponent "Ee")
         ;; ...
         (numeral-system-bindings t))
       "Numeral system for the C programming language.")

The 'with-numeral-system' macro and 'numeral-system-bindings' function
are defined as follows.

     (defmacro with-numeral-system (name &rest body)
       "Evaluate BODY within numeral system NAME."
       `(let ,(numeral-system-bindings (eval name))
          ,@body))

     (defun numeral-system-bindings (name)
       "Return the variable bindings for a numeral system.
     Argument NAME is the symbolic name of a numeral system (a symbol).
      A value of nil means to bind all symbols to nil, t means to bind
      all symbols to their current value, and `default' means to bind all
      symbols to their default values.

     Return value is a `let'-like list of variable bindings."
       (cond
        ;; ...
        ((eq name t)
         (let (value)
           (mapcar (lambda (symbol)
                     (setq value (symbol-value symbol))
;;;; ==>
                     (list symbol (if (self-evaluating-p value)
                                      value
                                    (list 'quote value))))
                   numeral-system-variables)))
        ;; ...
        ))

I noticed that if I don't check for a self-evaluating form here, the
variable bindings for a numeral system are improperly quoted in the
alist of numeral systems.

I hope these explanations are clear enough.

-- 
Ralph