protect - Maple Programming Help

Home : Support : Online Help : System : Utilities : protect

protect

protect a name from modification

unprotect

undo name protection

 Calling Sequence protect(name1, name2, ...) unprotect(name1, name2, ...)

Parameters

 name1, name2, ... - names

Description

 • protect is used to prevent names from being modified either in an interactive session, user or library code. Most of the names initially known to Maple are protected by default.
 • Attempting to assign to a protected name will raise an error.  There are two ways to circumvent this error, either use the unprotect command, or declare the name as a "local" variable.
 Using unprotect can be dangerous as a subsequent assignment will override the default value of that name which will effect internal algorithms that may expect and rely on the default value for correctness. By the same token, being able to unprotect and reassign new values to methods in Maple gives you a powerful tool for extending the system.
 Variables assigned to within procedures are implicitly understood as being local, in which case you would not run into a conflict with a protected name.  At the top-level, you can declare a variable as local by using the statement, local X;.  This will create a new version or binding of the name, X. The original protected name in this case is still accessible using :-X.  In general the original name is always available using the :- prefix, except for the case of the imaginary unit, I. Declaring I to be local will always give you a warning telling you what the new global name is, favoring, _I, if available.  To remove the local X use the command unbind(X).
 • A warning will be displayed when declaring and assigning to local X at the top-level only if X had been previously used in the session.  To avoid this warning declare local X prior to using it.
 • protect has no effect on environment variables.

Examples

Protected Names

An error is raised when trying to assign to a protected name.

 > $\mathrm{Pi}≔3.14$
 > $\mathrm{solve}≔{x}^{2}$

Unprotect

Redefining protected names can affect results.  Consider the correct value of arctan(1).

 > $\mathrm{eval}\left(\mathrm{arctan}\left(1\right)\right)$
 $\frac{{\mathrm{\pi }}}{{4}}$ (1)

Observe what happens when the constant Pi is changed to 1.

 > $\mathrm{unprotect}\left(\mathrm{Pi}\right)$
 > $\mathrm{Pi}≔1$
 ${\mathrm{\pi }}{≔}{1}$ (2)
 > $\mathrm{eval}\left(\mathrm{arctan}\left(1\right)\right)$
 $\frac{{1}}{{4}}$ (3)

Important variables and constants can be protected.

 > $\mathrm{myname}≔123$
 ${\mathrm{myname}}{≔}{123}$ (4)
 > $\mathrm{protect}\left('\mathrm{myname}'\right)$
 > $\mathrm{myname}≔456$
 > $\mathrm{unprotect}\left('\mathrm{myname}'\right)$
 > $\mathrm{myname}≔789$
 ${\mathrm{myname}}{≔}{789}$ (5)

Multiple names can be protected at once.

 > $\mathrm{first}≔1$
 ${\mathrm{first}}{≔}{1}$ (6)
 > $\mathrm{second}≔2$
 ${\mathrm{second}}{≔}{2}$ (7)
 > $\mathrm{protect}\left('\mathrm{first}','\mathrm{second}','\mathrm{Pi}'\right):$
 > $\mathrm{second}≔3$

Creating Local Names

Local versions of names can be created that overshadow the protected version.

 >
 Warning, A new binding for the name length has been created. The global instance of this name is still accessible using the :- prefix, :-length.  See ?protect for details.
 ${0.001}$ (8)
 > ${\mathrm{length}}^{2}$
 ${1.}{}{{10}}^{{-6}}$ (9)

The original procedure is still available as :-length.

 > $:-\mathrm{length}\left("abc"\right)$
 ${3}$ (10)

The imaginary unit is special.  When overridden with a local, the global version can be accessed via _I.

 >
 Warning, The imaginary unit, I, has been renamed _I
 ${I}$
 $\left[\begin{array}{cc}{1}& {0}\\ {0}& {1}\end{array}\right]$ (11)
 > ${I}^{2}$
 $\left[\begin{array}{cc}{1}& {0}\\ {0}& {1}\end{array}\right]$ (12)
 > ${\mathrm{_I}}^{2}$
 ${-1}$ (13)
 > $\mathrm{interface}\left(\mathrm{imaginaryunit}\right)$
 ${\mathrm{_I}}$ (14)

To reset the imaginary unit after declaring it local, use the unbind command.

 > $\mathrm{unbind}\left('I'\right)$
 ${\mathrm{length}}$ (15)
 > $\mathrm{interface}\left(\mathrm{imaginaryunit}=I\right)$
 ${\mathrm{_I}}$ (16)
 > ${I}^{2}$
 ${-1}$ (17)