home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ogicse!mintaka.lcs.mit.edu!gateway
- From: KMP@stony-brook.scrc.symbolics.com (Kent M Pitman)
- Newsgroups: comp.lang.lisp
- Subject: Re: Freezing a variable: how?
- Message-ID: <19920829213038.4.KMP@PLANTAIN.SCRC.Symbolics.COM>
- Date: 29 Aug 92 21:40:16 GMT
- Article-I.D.: PLANTAIN.19920829213038.4.KMP
- References: <1992Aug28.194300.20461@jpl-devvax.jpl.nasa.gov>
- Sender: news@mintaka.lcs.mit.edu
- Organization: LCS news/mail gateway
- Lines: 39
- X-Unparseable-Date: Sat
-
- It's not remarkably graceful, but for some purposes it might work to just
- promote the variable to a constant by doing DEFCONSTANT of the thing which
- is already a variable. (In some implementations you might get some warnings
- but they are likely not to be fatal.) e.g., in Symbolics Lisp, I just tried
- the following. I didn't try it in Lucid Lisp only because I don't have one
- on my desk, but I'd be surprised if you didn't get pretty similar results.
-
- ;;; -*- Mode: LISP; Syntax: Common-Lisp; Package: USER; Base: 10 -*-
-
- (eval-when (eval compile load)
- ;; Make sure the value is known to the compiler.
- ;; If more computation is done on the variable to get it in its
- ;; ready-to-be-frozen state, that stuff must also happen aggressively
- ;; at compile-time.
- (defvar *foo-17* 7)
- )
- (defmacro promote-variable-to-constant (x) `(defconstant ,x ,x))
- (promote-variable-to-constant *foo-17*)
- (defun foo (x) (+ x *foo-17*))
-
- And then did:
-
- Command: Compile File (file) S:>KMP>foo.lisp.newest
- For Variable *FOO-17*
- *FOO-17* may not have a constant compile-time value -- '7 will be used.
- S:>KMP>foo.lisp.newest compiled.
- Command: Load File (file) S:>KMP>foo
- Loading S:>KMP>foo.ibin.newest into package USER
- For Variable *FOO-17*
- Variable *FOO-17* is defined twice in the file S:>KMP>foo.
- ... Done.
- Command: (foo 3)
- 10
- Command: (disassemble'foo)
- 0 ENTRY: 1 REQUIRED, 0 OPTIONAL ;Creating X
- 2 FIXUP-TOS ;X
- 3 ADD 7
- 4 RETURN-SINGLE-STACK
- #<Compiled function FOO 21001534070>
-