12.5 Identical Objects

Two objects that are equal as objects (that is they actually refer to the same area of computer memory) and not only w.r.t. the equality relation ``='' are called identical. Identical objects do of course describe the same element.

  • IsIdenticalObj( obj1, obj2 ) F

    IsIdenticalObj( obj1, obj2 ) tests whether the objects obj1 and obj2 are identical (that is they are either equal immediate objects or are both stored at the same location in memory.

    If two copies of a simple constant object (see section Mutability and Copyability) are created, it is not defined whether GAP will actually store two equal but non-identical objects, or just a single object. For mutable objects, however, it is important to know whether two value refer to identical or non-identical objects, and the documentation of operations that return mutable values should make clear whether the values returned are new, or may be identical to values stored elsewhere.

    gap> IsIdenticalObj( 10^6, 10^6);
    true
    gap> IsIdenticalObj( 10^12, 10^12);
    false
    gap> IsIdenticalObj( true, true);
    true
    

    Generally, one may compute with objects but think of the results in terms of the underlying elements because one is not interested in locations in memory, data formats or information beyond underlying equivalence relations. But there are cases where it is important to distinguish the relations identity and equality. This is best illustrated with an example. (The reader who is not familiar with lists in GAP, in particular element access and assignment, is referred to Chapter Lists.)

    gap> l1:= [ 1, 2, 3 ];; l2:= [ 1, 2, 3 ];;
    gap> l1 = l2;
    true
    gap> IsIdenticalObj( l1, l2 );
    false
    gap> l1[3]:= 4;; l1; l2;
    [ 1, 2, 4 ]
    [ 1, 2, 3 ]
    gap> l1 = l2;
    false
    
    The two lists l1 and l2 are equal but not identical. Thus a change in l1 does not affect l2.
    gap> l1:= [ 1, 2, 3 ];; l2:= l1;;
    gap> l1 = l2;
    true
    gap> IsIdenticalObj( l1, l2 );
    true
    gap> l1[3]:= 4;; l1; l2;
    [ 1, 2, 4 ]
    [ 1, 2, 4 ]
    gap> l1 = l2;
    true
    
    Here, l1 and l2 are identical objects, so changing l1 means a change to l2 as well.

    The library also provides:

  • IsNotIdenticalObj( obj1, obj2 ) F

    tests whether the objects obj1 and objs2 are not identical.

    [Top] [Previous] [Up] [Next] [Index]

    GAP 4 manual
    February 2000