Re: Is DEFCONSTANT broken?



On 2009-06-22, Kaz Kylheku <kkylheku@xxxxxxxxx> wrote:
On 2009-06-22, Ron Garret <rNOSPAMon@xxxxxxxxxxx> wrote:
In article
<2e409792-6696-45b5-9474-636a45da86d2@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Scott Burson <FSet.SLB@xxxxxxxxx> wrote:

On Jun 21, 6:12 pm, Ron Garret <rNOSPA...@xxxxxxxxxxx> wrote:

Actually, DEFVAR comes closest to doing what I want.  But I also want to
insure that the value doesn't change or get bound.

Well, there's always DEFINE-SYMBOL-MACRO...

Doesn't work, because it doesn't insure the value won't change:

[ron@mickey:~/Desktop]$ cat test.lisp

(eval-when (:compile-toplevel :load-toplevel :execute)

(define-symbol-macro c7 '#.(gensym))
(defun c7 () c7)

Right; you get a different instance of the c7 macro when compiling and
loading.

It's evaluated again, just like the initializing value of defconstant.

How about this:

;; macro only at compile time
(eval-when (:compile-toplevel)
(define-symbol-macro c7 '#.(gensym)))

;; function only available in compiled form
(eval-when (:load-toplevel)
(defun c7 () c7))

But seriously, how does this fare on Clozure?

;; constant definition for compile time (and source loading)

(eval-when (:compile-toplevel :execute)
(define-symbol-macro c7 '#.(gensym)))

;; function to return value of constant:
;; - if this is compiled, c7 is folded by macroexpansion
;; to return the gensym set up by the above definition,
;; and that gensym is externalized into the compiled file.
;; See CLHS 3.2.4.2.2: uninterned symbols are externalizeable,
;; with a similarity based on their names!!!
;; - if a compiled form of this is being loaded, then the loader will
;; manufacture a gensym similar to the one that existed
;; at compile time; i.e. it will create a symbol
;; with the same name as if by MAKE-SYMBOL.
;; - if this is evaluated as source, then everything
;; is cool; the function refers to c7, which is set
;; up as a macro constant thanks to :execute above.

(defun c7 () c7)

;; load-time constant: now that the compiled file
;; is loaded, we make c7 a synonym for the function
;; call (c7). Thus (eq c7 (c7)) is assured.

(eval-when (:load-toplevel)
(define-symbol-macro c7 (c7)))

See: two different definitions for diferent situations, creating the
illusion of one consistent constant.

I suppose we could go as far as:

(eval-when (:load-toplevel)
(eval `(define-symbol-macro c7 ',(c7))))

Now c7 macroexpands to the actual symbol, quoted, similarly to the
way it did at compile time.
.



Relevant Pages

  • Re: Sacla loop: a new loop implementation
    ... way without demanding that the necessary type information is available ... necessary information for the macro to expand. ... > available at compile time" ... A statically typed language is a language where types are known at ...
    (comp.lang.lisp)
  • Re: macrology
    ... I'm trying to build a macro, pat-case, which is a case for patterns. ... takes a pattern, and input, and a list of forms to be executed (with ... Should return "LION TIGER" ... which will be all fixed in place at compile time, ...
    (comp.lang.lisp)
  • Re: Open stack and colon definitions as a features (was: Of course it IS!!!)
    ... forth and macro. ... execute at compile time like immediate words did thirty years ... design point I am interested in what it should/could do. ... found I only used each of the instructions RA SA RE SE once!!!! ...
    (comp.lang.forth)
  • Re: Sacla loop: a new loop implementation
    ... >> portably in common lisp, because the facilities just aren't there. ... No, macro expansion is a macroexpand-time operation, which occurs at ... > information must be produced at compile time. ... type information. ...
    (comp.lang.lisp)
  • Re: encrypting with preprocessor
    ... Here is a macro which compute the FNV hash function of the first 32 characters of a litteral string at compile time. ... The original litteral string 'str' does not appear in the binary. ... I let you adapt this macro to do encryption instead of computing hash code. ...
    (comp.lang.c)