From: Felix Schlesinger
Subject: changing a constant
Date: 
Message-ID: <slrnaagtqb.bo.fam_Schlesinger@schlesinger.dyndns.org>
Hello

Is there a reason why Lisp implementations (at least CLisp and CMUCL)
dont generate a warning if one tries to change an object that was a
bound to a constant? Today I again spent a long time chasing some voodoo
happening in my code and in the end found and i had done an complex
equivalent of
(let x '(1)
  (setq '(2)))
which of course had strange effects all over the place. Shouldnt this be
easy for an compiler to catch?

Ciao
  Felix
From: Barry Margolin
Subject: Re: changing a constant
Date: 
Message-ID: <dU3q8.13$531.3469@paloalto-snr2.gtei.net>
In article <·····························@schlesinger.dyndns.org>,
Felix Schlesinger <···············@t-online.de> wrote:
>Is there a reason why Lisp implementations (at least CLisp and CMUCL)
>dont generate a warning if one tries to change an object that was a
>bound to a constant? 

It's quite common to modify constants when working interactively, e.g.

(setq thing '(1 2 3 4))
(setf (car thing) 'a)

Although the standard says that the consequences of this are undefined, it
has traditionally worked as expected and no implementation I know of goes
out of its way to catch you doing it.

Furthermore, in order to implement such a warning, all cells would have to
have a tag indicating how they were created: via a literal or a normal
allocation.  This overhead is not usually considered interesting.

If you compile your program, it may relocate literals into the pure text
section of virtual memory, which is typically marked read-only.  Then,
attempts to modify them will generate a hardware exception that can be
trapped and reported to the user.

>		      Today I again spent a long time chasing some voodoo
>happening in my code and in the end found and i had done an complex
>equivalent of
>(let x '(1)
>  (setq '(2)))

This isn't even close to valid Lisp syntax.  SETQ needs a variable.  If
it was supposed to be:

(let ((x '(1)))
  (setq x '(2)))

I don't see the problem.  You're not modifying a constant with this.  On
the other hand, if it were

(let ((x '(1)))
  (setf (car x) 2))

you'll likely have problems.

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.