Protecting variables







I like these versions of protect and unprotect better:

(a) A user can now protect and unprotect several symbols at once.

(b) This version doesn't error on a non-symbol, instead it prints a
message.  I think this is better.

(c) Both functions now return true -- previously, they returned
stuff that a user might think was weird.

(d) Users will need to quote the arguments of protect and unprotect;
I think this is okay.

(e) Protect uses a mechanism that is already in Maxima, but mostly
unused. Because the mechanism is already in the code, I don't think
using protect should slow Maxima.  A more elaborate protect scheme
might slow Maxima.

(f) Some code in /share, for example units.mac, might benefit from
using protect / unprotect; however, a well-mannered package should
have code for cleaning house.

(g) The protect scheme is global:

(C2) gee(x) := block([x1 : concat(x,1)],
        protect(x1),
        x+1)$
(C3) gee(z);
(D3)                         z + 1
(C4) z1 : 1;

Improper value assignment to z1
 -- an error.  Quitting.  To debug this try DEBUGMODE(TRUE);)

(C5) hee(x) := block([z1 : 5], x +1);

(D5)               hee(x) := BLOCK([z1 : 5], x + 1)
(C6) hee(6);

Improper value assignment to z1
#0: hee(x=6)
 -- an error.  Quitting.  To debug this try DEBUGMODE(TRUE);)

This is bad, but I don't think Maxima provides any way around it.


----------- start of file--------------------
(defun $protect (&rest x)
  (mapcar #'(lambda (s) (if (symbolp s) (putprop s 'neverset 'assign)
     (mtell "Can't protect a nonsymbol ~:M" s))) x)
  t)

(defun $unprotect (&rest x)
  (mapcar #'(lambda (s) (if (symbolp s) (remprop s 'assign)
     (mtell "Can't unprotect a nonsymbol ~:M" s))) x)
  t)
----------- end of file-------------------------

(C1) load("protect.lisp");
(D1)                     protect.lisp
(C2) a : 7$

---> Notice the quote on 'a' -- it's important.
(C3) protect('a);
(D3)                         TRUE
(C4) a;
(D4)                           7
(C5) a : 2;
Improper value assignment to a
 -- an error.  Quitting.  To debug this try DEBUGMODE(TRUE);)
(C6) unprotect('a)$
(C7) a : 2;
(D7)                           2
(C8) a;
(D8)                           2

(C9) protect('a,'b,'c)$
(C10) b : 1;

Improper value assignment to b
 -- an error.  Quitting.  To debug this try DEBUGMODE(TRUE);)
(C11) unprotect('a,'b,'c);

(D11)                              TRUE


Barton