From: T. V. Raman
Subject: Freezing a variable: how?
Date: 
Message-ID: <1992Aug28.155335.12562@cs.cornell.edu>
 In common lisp Lucid 4.1 I would like to have the following:

After an initial setup phase, I would like to declare a variable as
being unmodifiable.  The constant declaration clearly will not work,
since the setup functions do need to modify the variable.  At present
I am making sure that none of the other functions accidentally change
this variable later on, but I would like lisp to take over this
responsibility.  Is there a standard way of doing this?

Thanks,

--Raman
-- 
   T. V. Raman <·····@cs.cornell.edu>Tel: (607)255-9202  R 272-3649
                       Office: 4116 Upson Hall,
Department of Computer Science, Cornell University Ithaca NY 14853-6201
                Res: 226 Bryant Avenue Ithaca NY 14850

From: Len Charest
Subject: Re: Freezing a variable: how?
Date: 
Message-ID: <1992Aug28.194300.20461@jpl-devvax.jpl.nasa.gov>
In article <······················@cs.cornell.edu>, ·····@cs.cornell.edu (T. V. Raman) writes:
|> 
|>  In common lisp Lucid 4.1 I would like to have the following:
|> 
|> After an initial setup phase, I would like to declare a variable as
|> being unmodifiable. ...  Is there a standard way of doing this?

I don't think so. I think you're stuck doing your own variable access
management. However, it would be relatively easy to 'localize' this
access management if you were willing to access the variables through a
standard form (e.g,. SLOT-VALUE, READERS in CLOS) rather than as
regular symbols. In fact, if you did use CLOS, you could store the
varibles as class slots that initially have both a READER and a WRITER.
After the setup phase was complete you could use the MOP to remove all
of the WRITER methods. (For completeness, you'd probably also want to
override the default method on SLOT-VALUE-USING-CLASS such that it
signalled an error.)
..................................................
                                  Len Charest, Jr.
                 JPL Artificial Intelligence Group
                          ·······@aig.jpl.nasa.gov
From: Kent M Pitman
Subject: Re: Freezing a variable: how?
Date: 
Message-ID: <19920829213038.4.KMP@PLANTAIN.SCRC.Symbolics.COM>
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>
From: Richard A. O'Keefe
Subject: Re: Freezing a variable: how?
Date: 
Message-ID: <14290@goanna.cs.rmit.oz.au>
In article <····················@PLANTAIN.SCRC.Symbolics.COM>, ···@stony-brook.scrc.symbolics.com (Kent M Pitman) writes:
> 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.

As I understand it, a program is loaded in two phases, in the first of which
a variable is updated, and in the second of which it should be read-only.

The thought that occurs to me at once is "why does it have to be the same
variable?"  Why can't there be a variable which is updated by the first phase,
and the last thing it installs is a defconstant holding the final value of the
thing?

As an alternative, you can do this in Scheme:

	(define thingy "thingy")
	(define set-thingy! "set-thingy!")
	(let ((hidden-variable initial-value))
	    (set! thingy (lambda () hidden-variable))
	    (set! set-thingy! (lambda (x) (set! hidden-variable x) "!"))
	    "access functions for thingy defined")

	;; first phase now uses (thingy) and (set-thingy! x)

	(set! set-thingy! (lambda (x) (error <dialect-dependent arguments>)))

	;; second phase now uses (thingy) but cannot change the value at all
	;; In fact, _no-one_ can change hidden-variable any more.

Presumably something similar is doable in Common Lisp.

-- 
You can lie with statistics ... but not to a statistician.
From: Jeff Dalton
Subject: Re: Freezing a variable: how?
Date: 
Message-ID: <7417@skye.ed.ac.uk>
For global variables, you could write your own macro to use instead of
SETQ and have the expansion check.  Eg:

  (defmacro safe-setq (var form)
    `(if (get ',var 'constant)
         (error ...)
       (setq ,var ,form)))

and then

  (defun make-constant (var)
    (setf (get var 'constant) t))

-- jd


  
From: Barry Margolin
Subject: Re: Freezing a variable: how?
Date: 
Message-ID: <17m446INNfm5@early-bird.think.com>
In article <······················@cs.cornell.edu> ·····@cs.cornell.edu (T. V. Raman) writes:
>After an initial setup phase, I would like to declare a variable as
>being unmodifiable. ... Is there a standard way of doing this?

No.  Constantness of a variable is a lexical property, not a dynamic one.
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar