1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
#############################################################################
##
#W record.g GAP library Thomas Breuer
#W & Frank Celler
##
#H @(#)$Id: record.g,v 4.17.2.1 2005/04/27 16:31:22 gap Exp $
##
#Y Copyright (C) 1997, Lehrstuhl D fuer Mathematik, RWTH Aachen, Germany
#Y (C) 1998 School Math and Comp. Sci., University of St. Andrews, Scotland
#Y Copyright (C) 2002 The GAP Group
##
## This file contains methods for records.
## Compared to {\GAP}~3, where records were used to represent domains and
## all kinds of external arithmetic objects, in {\GAP}~4 there is no
## important role for records.
## So the standard library provides only methods for `PrintObj', `String',
## `\=', and `\<', and the latter two are not installed to compare records
## with objects in other families.
##
## In order to achieve a special behaviour of records as in {\GAP}~3 such
## that a record can be regarded as equal to objects in other families
## or such that a record can be compared via `\<' with objects in other
## families, one can load the file `compat3c.g'.
##
Revision.record_g :=
"@(#)$Id: record.g,v 4.17.2.1 2005/04/27 16:31:22 gap Exp $";
#############################################################################
##
#C IsRecord( <obj> )
#C IsRecordCollection( <obj> )
#C IsRecordCollColl( <obj> )
##
DeclareCategoryKernel( "IsRecord", IsObject, IS_REC );
DeclareCategoryCollections( "IsRecord" );
DeclareCategoryCollections( "IsRecordCollection" );
#############################################################################
##
#V RecordsFamily . . . . . . . . . . . . . . . . . . . . . family of records
##
BIND_GLOBAL( "RecordsFamily", NewFamily( "RecordsFamily", IS_REC ) );
#############################################################################
##
#V TYPE_PREC_MUTABLE . . . . . . . . . . . type of a mutable internal record
##
BIND_GLOBAL( "TYPE_PREC_MUTABLE",
NewType( RecordsFamily, IS_MUTABLE_OBJ and IS_REC and IsInternalRep ) );
#############################################################################
##
#V TYPE_PREC_IMMUTABLE . . . . . . . . type of an immutable internal record
##
BIND_GLOBAL( "TYPE_PREC_IMMUTABLE",
NewType( RecordsFamily, IS_REC and IsInternalRep ) );
#############################################################################
##
#o \.( <rec>, <name> ) . . . . . . . . . . . . . . . . get a component value
##
DeclareOperationKernel( ".", [ IsObject, IsObject ], ELM_REC );
#############################################################################
##
#o IsBound\.( <rec>, <name> ) . . . . . . . . . . . . . . test a component
##
DeclareOperationKernel( "IsBound.", [ IsObject, IsObject ], ISB_REC );
#############################################################################
##
#o \.\:\=( <rec>, <name>, <val> ) . . . . . . . . . . . . . assign a value
##
DeclareOperationKernel( ".:=", [ IsObject, IsObject, IsObject ], ASS_REC );
#############################################################################
##
#o Unbind\.( <rec>, <name> ) . . . . . . . . . . . . . . . unbind component
##
DeclareOperationKernel( "Unbind.", [ IsObject, IsObject ], UNB_REC );
#############################################################################
##
#A RecNames( <rec> )
##
## returns a list of strings corresponding to the names of the record
## components of the record <rec>.
##
DeclareAttribute( "RecNames", IsRecord );
#############################################################################
##
#F RecFields( <record> )
##
BIND_GLOBAL( "RecFields", RecNames );
#############################################################################
##
#M RecNames( <record> ) . . . . . . . . . . . . . . . . names of components
##
InstallMethod( RecNames,
"for a record in internal representation",
[ IsRecord and IsInternalRep ],
REC_NAMES );
#############################################################################
##
#F NamesOfComponents( <obj> )
##
## For a component object <obj>, `NamesOfComponents' returns a list of
## strings, which are the names of components currently bound in <comobj>.
##
## For a record <obj>, `NamesOfComponents' returns the result of `RecNames'.
##
BIND_GLOBAL( "NamesOfComponents", function( obj )
if IsComponentObjectRep( obj ) then
return REC_NAMES_COMOBJ( obj );
elif IsRecord( obj ) then
return RecNames( obj );
else
Error( "<obj> must be a component object or a record" );
fi;
end );
#############################################################################
##
#m PrintObj( <record> )
##
## The record <record> is printed by printing all its components.
##
InstallMethod( PrintObj,
"record",
[ IsRecord ],
function( record ) PRINT_PREC_DEFAULT( record ); end );
#############################################################################
##
#m String( <record> ) . . . . . . . . . . . . . . . . . . . . for a record
##
InstallMethod( String,
"record",
[ IsRecord ],
function( record )
local str, nam, com;
str := "rec( ";
com := false;
for nam in RecNames( record ) do
if com then
Append( str, ", " );
else
com := true;
fi;
Append( str, nam );
Append( str, " := " );
if IsStringRep( record.( nam ) )
or ( IsString( record.( nam ) )
and not IsEmpty( record.( nam ) ) ) then
Append( str, "\"" );
Append( str, String( record.(nam) ) );
Append( str, "\"" );
else
Append( str, String( record.(nam) ) );
fi;
od;
Append( str, " )" );
ConvertToStringRep( str );
return str;
end );
#############################################################################
##
#m ViewObj( <record> ) . . . . . . . . . . . . . . . for a record (default)
##
##
InstallMethod( ViewObj,
"record",
[ IsRecord ],
function( record )
local nam, com, i;
Print("\>\>rec( \>\>");
com := false;
i := 1;
for nam in RecNames( record ) do
if com then
Print("\<,\< \>\>");
else
com := true;
fi;
SET_PRINT_OBJ_INDEX(i);
i := i+1;
Print(nam, " := ");
ViewObj(record.(nam));
od;
Print(" \<\<\<\<)");
end);
#############################################################################
##
#m <record> = <record>
##
InstallMethod( \=,
"record = record",
IsIdenticalObj,
[ IsRecord, IsRecord ],
EQ_PREC );
#############################################################################
##
#m <record> < <record>
##
InstallMethod( \<,
"record < record",
IsIdenticalObj,
[ IsRecord, IsRecord ],
LT_PREC );
# methods to catch error cases
#############################################################################
##
#m \.
##
InstallMethod(\.,"catch error",true,[IsObject,IsObject],0,
function(obj,nr)
local msg;
msg:=Concatenation("illegal access to record component `obj.",
NameRNam(nr),"'\n",
"of the object <obj>. (Objects by default do not have record components.\n",
"The error might be a relic from translated GAP3 code.) ");
Error(msg);
end);
#############################################################################
##
#m IsBound\.
##
InstallMethod(IsBound\.,"catch error",true,[IsObject,IsObject],0,
function(obj,nr)
local msg;
msg:=Concatenation("illegal access to record component `IsBound(obj.",
NameRNam(nr),")'\n",
"of the object <obj>. (Objects by default do not have record components.\n",
"The error might be a relic from translated GAP3 code.) ");
Error(msg);
end);
#############################################################################
##
#m Unbind\.
##
InstallMethod(Unbind\.,"catch error",true,[IsObject,IsObject],0,
function(obj,nr)
local msg;
msg:=Concatenation("illegal access to record component `Unbind(obj.",
NameRNam(nr),")'\n",
"of the object <obj>. (Objects by default do not have record components.\n",
"The error might be a relic from translated GAP3 code.) ");
Error(msg);
end);
#############################################################################
##
#m \.\:\=
##
InstallMethod(\.\:\=,"catch error",true,[IsObject,IsObject,IsObject],0,
function(obj,nr,elm)
local msg;
msg:=Concatenation("illegal assignement to record component `obj.",
NameRNam(nr),"'\n",
"of the object <obj>. (Objects by default cannot have record components.\n",
"The error might be a relic from translated GAP3 code.) ");
Error(msg);
end);
#############################################################################
##
#E
|