  
  [1m[4m[31m2. Implementing circle objects[0m
  
  In  this  chapter  we  explain  how  the [1mGAP[0m system may be extended with new
  objects  using  the  circle  multiplication  as  an  example.  We follow the
  guidelines  given  in  the last two chapters of the [1mGAP[0m Programming Tutorial
  and refer to them for more details.
  
  
  [1m[4m[31m2.1 First attempts[0m
  
  Of course, having two ring elements, you can straightforwardly compute their
  circle  product  defined as r * s = r + s + rs. You can do this in a command
  line, and it is a trivial task to write a simplest function of two arguments
  that will do this:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> CircleMultiplication := function(a,b)[0m
    [22m[35m>      return a+b+a*b;[0m
    [22m[35m>    end;[0m
    [22m[35mfunction( a, b ) ... end[0m
    [22m[35mgap> CircleMultiplication(2,3); [0m
    [22m[35m11[0m
    [22m[35mgap> CircleMultiplication( ZmodnZObj(2,8), ZmodnZObj(5,8) );      [0m
    [22m[35mZmodnZObj( 1, 8 )[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  However,  there  is  no check whether both arguments belong to the same ring
  and  whether  they  are  ring  elements at all, so it is easy to obtain some
  meaningless results:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> CircleMultiplication( 3, ZmodnZObj(3,8) );[0m
    [22m[35mZmodnZObj( 7, 8 )[0m
    [22m[35mgap> CircleMultiplication( [1], [2,3] );[0m
    [22m[35m[ 5, 5 ][0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  You  can  include  some tests for arguments, and maybe the best way of doing
  this  would  be  declaring  a  new  operation  for  two  ring  elements, and
  installing  the  previous function as a method for this operation. This will
  check automatically if the arguments are ring elements from the common ring:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> DeclareOperation( "BetterCircleMultiplication",                             [0m
    [22m[35m>      [IsRingElement,IsRingElement] );[0m
    [22m[35mgap> InstallMethod( BetterCircleMultiplication,[0m
    [22m[35m>      IsIdenticalObj,[0m
    [22m[35m>      [IsRingElement,IsRingElement],  [0m
    [22m[35m>      CircleMultiplication );[0m
    [22m[35mgap> BetterCircleMultiplication(2,3);[0m
    [22m[35m11[0m
    [22m[35mgap> BetterCircleMultiplication( ZmodnZObj(2,8), ZmodnZObj(5,8) );[0m
    [22m[35mZmodnZObj( 1, 8 )[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  Nevertheless,  the  functionality gained from such operation would be rather
  limited.  You  will  not  be  able  to  compute circle product via the infix
  operator  [22m[32m*[0m,  and,  moreover,  you  will  not be able to create higher level
  objects   such   as  semigroups  and  groups  with  respect  to  the  circle
  multiplication.
  
  In  order  to  "integrate"  the  circle  multiplication into the [1mGAP[0m library
  properly, instead of defining [22m[36mnew[0m operations for existing objects, we should
  define  [22m[36mnew[0m  objects  for which the infix operator [22m[32m*[0m will perform the circle
  multiplication. This approach is explained in the next two sections.
  
  
  [1m[4m[31m2.2 Defining circle objects[0m
  
  Thus,  we  are  going to implement [22m[36mcircle objects[0m, for which we can envisage
  the following functionality:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> CircleObject( 2 ) * CircleObject( 3 );                       [0m
    [22m[35mCircleObject( 11 )[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  First  we need to distinguish these new objects from other [1mGAP[0m objects. This
  is  done  via  the  [22m[36mtype[0m  of the objects, that is mainly determined by their
  [22m[36mcategory[0m, [22m[36mrepresentation[0m and [22m[36mfamily[0m.
  
  We  start  with  declaring  the  category [22m[32mIsCircleObject[0m as a subcategory of
  [22m[32mIsMultiplicativeElementWithInverse[0m.  Thus,  each  circle  object will "know"
  that  it  is  [22m[32mIsMultiplicativeElementWithInverse[0m,  and  this  will  make  it
  possible  to apply to circle objects such operations as [22m[32mOne[0m and [22m[32mInverse[0m (the
  latter is allowed to return [22m[32mfail[0m for a given circle object).
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> DeclareCategory( "IsCircleObject", IsMultiplicativeElementWithInverse );[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  Further  we  would  like to create semigroups and groups generated by circle
  objects. Such structures will be [22m[36mcollections[0m of circle objects, so they will
  be  in  the  category  [22m[32mCategoryCollections(  IsCircleObject  )[0m.  This is why
  immediately  after  we declare the underlying category of circle objects, we
  need also to declare the category of their collections:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> DeclareCategoryCollections( "IsCircleObject" );[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  On the next step we should think about the internal representation of circle
  objects.  A  natural  way would be to store the underlying ring element in a
  list-like  structure at its first position. We do not foresee any other data
  that  we need to store internally in the circle object. This is quite common
  situation,  so  we may define first [22m[32mIsPositionalObjectOneSlotRep[0m that is the
  list-like  representation  with  only  one  position  in  the list, and then
  declare  a synonym [22m[32mIsDefaultCircleObject[0m that means that we are dealing with
  a circle object in one-slot representation:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> DeclareRepresentation( "IsPositionalObjectOneSlotRep",[0m
    [22m[35m>     IsPositionalObjectRep, [ 1 ] );[0m
    [22m[35mgap> DeclareSynonym( "IsDefaultCircleObject",[0m
    [22m[35m>     IsCircleObject and IsPositionalObjectOneSlotRep );[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  Until  now  we are still unable to create circle objects, because we did not
  specify  to which family they will belong. Naturally, having a ring, we want
  to  have  all circle objects for elements of this ring in the same family to
  be  able  to  multiply  them,  and  we expect circle objects for elements of
  different  rings  to be placed in different families. Thus, it would be nice
  to  establish  one-to-one correspondence between the family of ring elements
  and   a  family  of  circle  elements  for  this  ring.  We  can  store  the
  corresponding  circle family as an attribute of the ring elements family. To
  do this first we declare an attribute [22m[32mCircleFamily[0m for families:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> DeclareAttribute( "CircleFamily", IsFamily );[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  Now  we  install  the  method that stores the corresponding circle family in
  this attribute:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> InstallMethod( CircleFamily,[0m
    [22m[35m>     "for a family",[0m
    [22m[35m>     [ IsFamily ],[0m
    [22m[35m>     function( Fam )[0m
    [22m[35m>     local F;[0m
    [22m[35m>   # create the family of circle elements[0m
    [22m[35m>   F:= NewFamily( "CircleFamily(...)", IsCircleObject );[0m
    [22m[35m>   if HasCharacteristic( Fam ) then[0m
    [22m[35m>     SetCharacteristic( F, Characteristic( Fam ) );[0m
    [22m[35m>   fi;[0m
    [22m[35m>   # store the type of objects in the output[0m
    [22m[35m>   F!.CircleType:= NewType( F, IsDefaultCircleObject );[0m
    [22m[35m>   # Return the circle family[0m
    [22m[35m>   return F;[0m
    [22m[35m> end );[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  Similarly,  we  want  one-to-one  correspondence between circle elements and
  underlying  ring  elements.  We declare an attribute [22m[32mCircleObject[0m for a ring
  element,  and  then  install the method to create new circle object from the
  ring  element.  This  method  takes  the  family  of the ring element, finds
  corresponding circle family, extracts from it the type of circle objects and
  finally creates the new circle object of that type:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> DeclareAttribute( "CircleObject", IsRingElement );[0m
    [22m[35mgap> InstallMethod( CircleObject,[0m
    [22m[35m>     "for a ring element",[0m
    [22m[35m>     [ IsRingElement ],[0m
    [22m[35m>     obj -> Objectify( CircleFamily( FamilyObj( obj ) )!.CircleType,[0m
    [22m[35m>                       [ Immutable( obj ) ] ) );[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  Only after entering all code above we are able to create some circle object.
  However,  it is displayed just as [22m[32m<object>[0m, though we can get the underlying
  ring element using the "!" operator:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> a:=CircleObject(2);[0m
    [22m[35m<object>[0m
    [22m[35mgap> a![1];[0m
    [22m[35m2[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  We can check that the intended relation between families holds:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> FamilyObj( CircleObject ( 2 ) ) = CircleFamily( FamilyObj( 2 ) );[0m
    [22m[35mtrue[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  We  can not multiply circle objects yet. But before implementing this, first
  let us improve the output by installing the method for [22m[32mPrintObj[0m:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> InstallMethod( PrintObj,[0m
    [22m[35m>     "for object in `IsCircleObject'",[0m
    [22m[35m>     [ IsDefaultCircleObject ],[0m
    [22m[35m>     function( obj )[0m
    [22m[35m>     Print( "CircleObject( ", obj![1], " )" );[0m
    [22m[35m>     end );[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  This  method  will be used by [22m[32mPrint[0m function, and also by [22m[32mView[0m, since we did
  not  install  special  method for [22m[32mViewObj[0m for circle objects. As a result of
  this installation, the output became more meaningful:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> a;[0m
    [22m[35mCircleObject( 2 )[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  We  need  to  avoid  the  usage  of  "!" operator, which, in general, is not
  recommended  to  the  user  (for  example, if [1mGAP[0m developers will change the
  internal  representation of some object, all [1mGAP[0m functions that deal with it
  must  be  adjusted appropriately, while if the user's code had direct access
  to  that  representation  via  "!", an error may occur). To do this, we wrap
  getting the first component of a circle object in the following operation:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> DeclareOperation("UnderlyingRingElement", [ IsCircleObject] );[0m
    [22m[35mgap> InstallMethod( UnderlyingRingElement,[0m
    [22m[35m>     "for a circle object", [0m
    [22m[35m>     [ IsCircleObject],[0m
    [22m[35m>     obj -> obj![1] );[0m
    [22m[35mgap> UnderlyingRingElement(a);[0m
    [22m[35m2[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  
  [1m[4m[31m2.3 Installing operations for circle objects[0m
  
  Now we are finally able to install circle multiplication as a default method
  for  the  multiplication of circle objects, and perform the computation that
  we envisaged in the beginning:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> InstallMethod( \*,[0m
    [22m[35m>     "for two objects in `IsCircleObject'",[0m
    [22m[35m>     IsIdenticalObj,[0m
    [22m[35m>     [ IsDefaultCircleObject, IsDefaultCircleObject ],[0m
    [22m[35m>     function( a, b )[0m
    [22m[35m>     return CircleObject( a![1] + b![1] + a![1]*b![1] );[0m
    [22m[35m>     end );[0m
    [22m[35mgap> CircleObject(2)*CircleObject(3);[0m
    [22m[35mCircleObject( 11 )[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  However,  this  functionality  is  not  enough  to form semigroups or groups
  generated by circle elements. We need to be able to check whether two circle
  objects  are equal, and we need to define ordering for them (for example, to
  be  able  to  form  sets  of  circle  elements).  Since we already have both
  operations  for  underlying  ring  elements,  this  can  be implemented in a
  straightforward way:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> InstallMethod( \=,[0m
    [22m[35m>     "for two objects in `IsCircleObject'",[0m
    [22m[35m>     IsIdenticalObj,[0m
    [22m[35m>     [ IsDefaultCircleObject, IsDefaultCircleObject ],[0m
    [22m[35m>     function( a, b )[0m
    [22m[35m>     return a![1] = b![1];[0m
    [22m[35m>     end );[0m
    [22m[35mgap> InstallMethod( \<,[0m
    [22m[35m>     "for two objects in `IsCircleObject'",[0m
    [22m[35m>     IsIdenticalObj,[0m
    [22m[35m>     [ IsDefaultCircleObject, IsDefaultCircleObject ],[0m
    [22m[35m>     function( a, b )[0m
    [22m[35m>     return a![1] < b![1];[0m
    [22m[35m>     end );[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  Further,  zero  element  of the ring plays a role of the neutral element for
  the  circle  multiplication, and we add this knowledge to our code in a form
  of  a method for [22m[32mOneOp[0m that returns circle object for the corresponding zero
  object:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> InstallMethod( OneOp,[0m
    [22m[35m>     "for an object in `IsCircleObject'",[0m
    [22m[35m>     [ IsDefaultCircleObject ],[0m
    [22m[35m>     a -> CircleObject( Zero( a![1] ) ) );[0m
    [22m[35mgap> One(a);[0m
    [22m[35mCircleObject( 0 )[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  Now we are already able to create monoids generated by circle objects:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> S:=Monoid(a);[0m
    [22m[35m<monoid with 1 generator>[0m
    [22m[35mgap> One(S);[0m
    [22m[35mCircleObject( 0 )[0m
    [22m[35mgap> S:=Monoid( CircleObject( ZmodnZObj( 2,8) ) );[0m
    [22m[35m<monoid with 1 generator>[0m
    [22m[35mgap> Size(S);[0m
    [22m[35m2[0m
    [22m[35mgap> AsList(S);[0m
    [22m[35m[ CircleObject( ZmodnZObj( 0, 8 ) ), CircleObject( ZmodnZObj( 2, 8 ) ) ][0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  Finally,  to  generate  groups using circle objects, we need to add a method
  for  the [22m[32mInverseOp[0m. In our implementation we will assume that the underlying
  ring  is  a subring of the ring with one, thus, if the circle inverse for an
  element x exists, than it can be computed as -x(1+x)^-1:
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> InstallMethod( InverseOp,[0m
    [22m[35m>     "for an object in `IsCircleObject'",[0m
    [22m[35m>     [ IsDefaultCircleObject ],[0m
    [22m[35m>     function( a )[0m
    [22m[35m>     local x;[0m
    [22m[35m>     x := Inverse( One( a![1] ) + a![1] );[0m
    [22m[35m>     if x = fail then[0m
    [22m[35m>       return fail;[0m
    [22m[35m>     else[0m
    [22m[35m>       return CircleObject( -a![1] * x );[0m
    [22m[35m>     fi;[0m
    [22m[35m>     end );[0m
    [22m[35mgap> CircleObject(-2)^-1;                [0m
    [22m[35mCircleObject( -2 )[0m
    [22m[35mgap> CircleObject(2)^-1; [0m
    [22m[35mCircleObject( -2/3 )[0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  The  last  method  already  makes  it possible to create groups generated by
  circle objects (the warning may be ignored):
  
  [22m[35m---------------------------  Example  ----------------------------[0m
    [22m[35m[0m
    [22m[35mgap> Group( CircleObject(2) );                       [0m
    [22m[35m#I  default `IsGeneratorsOfMagmaWithInverses' method returns `true' for [0m
    [22m[35m[ CircleObject( 2 ) ][0m
    [22m[35m<group with 1 generators>[0m
    [22m[35mgap> G:=Group( [CircleObject( ZmodnZObj( 2,8 ) )  ]);[0m
    [22m[35m#I  default `IsGeneratorsOfMagmaWithInverses' method returns `true' for [0m
    [22m[35m[ CircleObject( ZmodnZObj( 2, 8 ) ) ][0m
    [22m[35m<group with 1 generators>[0m
    [22m[35mgap> Size(G);[0m
    [22m[35m2[0m
    [22m[35mgap> AsList(G);[0m
    [22m[35m[ CircleObject( ZmodnZObj( 0, 8 ) ), CircleObject( ZmodnZObj( 2, 8 ) ) ][0m
    [22m[35m[0m
  [22m[35m------------------------------------------------------------------[0m
  
  The   [1mGAP[0m   code   used   in   this  Chapter,  is  contained  in  the  files
  [1mcircle/lib/circle.gd[0m  and  [1mcircle/lib/circle.gi[0m.  We  also refer to last two
  chapters   of   the   [1mGAP[0m  Programming  Tutorial  for  another  examples  of
  implementing new [1mGAP[0m objects and further details.
  
