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

kroeker kroeker at uni-math.gwdg.de
Fri Mar 9 14:52:51 GMT 2012


Dear Steve,



thanks for you answer!

 >Apologies for the slow response.
no problem!


 >I think you have confused 'MakeImmutable' with 'MakeReadOnlyGlobal'.

Oh, that was the case... thanks for clarifying!


 > there is no way to "lock" an operation, [..] it would also cause problems

Let for now assume that it is not necessary to lock operations. Then in 
my opinion a user should get at least a warning that locking is not 
possible when trying it.
By the way, It shouldn't be possible to lock existing read-only 
operations, so it is not obvious to me that locking an operation would 
cause problems a priori.
It also seems that other functionality in GAP has  similar behaviour 
which is unusual from my point of view:
actions fail without a warning, e.g. calling an attribute setter twice 
with different values (the second call has no effect)


I asked for local constants, because a language should support the user 
to write correct code (and at the same time not to sacrifice performance 
too much). Since this would result in a performance penalty, it's 
probably ok to go without read-only locals, but code reviewing and 
testing is even more important then.



Best,


Jakob






Am 08.03.2012 19:18, schrieb Stephen Linton:
> 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