Next: , Previous: Data-functors, Up: Data-terms   [Contents]


2.15.2 Record syntax

Record syntax provides a convenient way to select or update fields of data constructors, independent of the definition of the constructor. Record syntax expressions are transformed into sequences of calls to field selection or update functions (see Field access functions).

A field specifier is a name or a compound data-term. A field list is a list of field specifiers separated by ^. field, field1 ^ field2 and field1(A) ^ field2(B, C) are all valid field lists.

If the top-level functor of a field specifier is ‘field/N’, there must be a visible selection function ‘field/(N + 1)’. If the field specifier occurs in a field update expression, there must also be a visible update function named ‘'field :='/(N + 2)’.

Record syntax expressions have one of the following forms. There are also record syntax DCG goals (see DCG-goals), which provide similar functionality to record syntax expressions, except that they act on the DCG arguments of a DCG clause.

Term ^ field_list

A field selection. For each field specifier in field_list, apply the corresponding selection function in turn.

Term must be a valid data-term. field_list must be a valid field list.

A field selection is transformed using the following rules:

transform(Term ^ Field(Arg1, …)) = Field(Arg1, …, Term).
transform(Term ^ Field(Arg1, …) ^ Rest) =
                transform(Field(Arg1, …, Term) ^ Rest).

Examples:

Term ^ field is equivalent to field(Term).

Term ^ field(Arg) is equivalent to field(Arg, Term).

Term ^ field1(Arg1) ^ field2(Arg2, Arg3) is equivalent to field2(Arg2, Arg3, field1(Arg1, Term)).

Term ^ field_list := FieldValue

A field update, returning a copy of Term with the value of the field specified by field_list replaced with FieldValue.

Term must be a valid data-term. field_list must be a valid field list.

A field update is transformed using the following rules:

transform(Term ^ Field(Arg1, …) := FieldValue) =
                'Field :='(Arg1, …, Term, FieldValue)).

transform(Term0 ^ Field(Arg1, …) ^ Rest := FieldValue) = Term :-
        OldFieldValue = Field(Arg1, …, Term0),
        NewFieldValue = transform(OldFieldValue ^ Rest := FieldValue),
        Term = 'Field :='(Arg1, …, Term0, NewFieldValue).

Examples:

Term ^ field := FieldValue is equivalent to 'field :='(Term, FieldValue).

Term ^ field(Arg) := FieldValue is equivalent to 'field :='(Arg, Term, FieldValue).

Term ^ field1(Arg1) ^ field2(Arg2) := FieldValue is equivalent to the code

OldField1 = field1(Arg1, Term),
NewField1 = 'field2 :='(Arg2, OldField1, FieldValue),
Result = 'field1 :='(Arg1, Term, NewField1)

Next: , Previous: Data-functors, Up: Data-terms   [Contents]