home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / lisp / 2336 < prev    next >
Encoding:
Text File  |  1992-08-30  |  2.0 KB  |  53 lines

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