[GAP Forum] [GAP Support] protecting variables ( MakeImmutable )

Stephen Linton sal at cs.st-andrews.ac.uk
Thu Mar 8 18:18:06 GMT 2012


Dear Jakob, Dear GAP Forum,

Apologies for the slow response. 
I think you have confused MakeImmutable with MakeReadOnlyGlobal. 

MakeImmutable is used to make an OBJECT (and its subobjects) immutable, so that one can be sure that its mathematical value will not change.

A simple example is:

gap> l := [1,2,3];;
gap> MakeImmutable(l);
[ 1, 2, 3 ]
gap> l[2] := 4;
Error, Lists Assignment: <list> must be a mutable list
not in any function at line 3 of *stdin*
you can 'return;' and ignore the assignment
brk> 
gap> l := "foo";
"foo"

In this example the list created on the first line is later made immutable, but the variable l remains writable.
Functions, operations, properties, etc. are all immutable already, so MakeImmutable does nothing to them.

MakeReadOnlyGlobal makes a global VARIABLE read-only.

gap> x := 3;
3
gap> MakeReadOnlyGlobal("x");
gap> x := 2;
Error, Variable: 'x' is read only
not in any function at line 6 of *stdin*
you can 'return;' after making it writable
brk> 

This has no effect on the object referred to by the variable, as seen in:

gap> y := rec(a := 1);
rec( a := 1 )
gap> MakeReadOnlyGlobal("y");
gap> y.a := 2;
gap> y;
rec( a := 2 )


While I am not sure exactly what the reasoning behind your example is, there is no way to "lock" an operation so that further methods for it cannot be installed. 
I can see no need for such a feature, and lots of ways in which it would cause problems, such as interference between packages.

Finally, you ask about local constants. There is no way in GAP to make local variable read-only, but since its scope is limited to a single function, it is usually pretty easy to avoid simply avoid assigning to it. Adding such a feature would either greatly complicate the parser, or slow down local variable access at run-time, and neither of these seems desirable.


	Steve Linton

On 2 Mar 2012, at 17:43, kroeker wrote:

> Dear GAP-Forum,
> 
> 
> I am a little bit confused about protecting variables.
> Is it possible in all cases, and if not, will this be possible in future 
> versions of GAP?
> 
> For example, I failed to protect a 'Property':
> 
> 
> ######################################
> exampleRec := rec();
> exampleRec.IsShape := NewProperty("IsShape",IsObject);
> InstallMethod(exampleRec.IsShape , "" ,[IsObject],
> function(obj)
>    return false;
> end
> );
> 
> exampleRec.IsShape := MakeImmutable(exampleRec.IsShape);
> exampleRec := MakeImmutable(exampleRec);
> 
> IsMutable(exampleRec);
> IsMutable(exampleRec.IsShape);
> 
> InstallMethod(exampleRec.IsShape , "" ,[IsObject],
> function(obj)
>    return true;
> end
> );
> # I would expect an error , but there is none!
> 
> exampleRec.IsShape(4);
> ######################################
> 
> 
> My second question is, how to define  local constants?
> For example, I would like to protect  'constantInt':
> 
> ######################################
>    local constantInt;
>    constantInt := 5;
>    # how to protect  constantInt?
> ######################################
> 
> 
> Thanks,
> 
> 
> Jakob
> 
> _______________________________________________
> Forum mailing list
> Forum at mail.gap-system.org
> http://mail.gap-system.org/mailman/listinfo/forum


_______________________________________________
Support mailing list
Support at gap-system.org
http://mail.gap-system.org/mailman/listinfo/support



More information about the Forum mailing list