Data Types

A data type is a set of data values of certain structure that can be held and processed by agents and passed between agents.

Each data in CAOPLE language has a type, which can be determined at compilation time and checked for type correctness statically.

User can define data types by type declarations based on data types predefined in the CAOPLE language.

Predefined Data Types

There are four predefined data types: integer, real, bool and string. Their sets of values are the integers numbers, real numbers, boolean values and strings of characters, respectively. In the syntax in EBNF is:

PredefinedDataType ::= ⟨INT⟩ | ⟨REAL⟩ | ⟨BOOL⟩ | ⟨STRING⟩

Agent Data Types

The agent type data enables a program to refer to agents in the system.

The values of an agent data type are the universally unique identifiers of the agent of a caste in the system; see the Chapter for caste declarations.

The syntax of agent data type in EBNF is:

AgentDataType ::= ⟨AGENT⟩ [ "{" CasteName "}" ]

When the clause "{" CasteName "}" is omitted, the values are the universally unique identifiers of all agents of any caste.

User Defined Data Types

Users can define three kinds of data types: Record, List and Enumerate.

Record and List are structured data types. They are constructed from existing data types, including predefined data types integer, real, bool, string, and agent data types, and user defined data types as well.

Enumerate data types are primitive data types, whose values are atomic.

Type Declarations

The syntax of a data type declaration is:

TypeDef ::= ⟨TYPE⟩ DataTypeName=Identifier "=" TypeExpr ";"

TypeExpr ::= RecordTypeExpr | ListTypeExpr | EnumerateTypeExpr

User defined data types can be declared in a definition package (see Chapter 5) and used in caste declarations (see Chapter 8). They can also included in a caste declaration as local type definitions.

Record Types

The syntax of a record type expression in EBNF is:

RecordTypeExpr ::= ⟨STRUCT⟩ "{" { FieldName ":" TypeName ";" }+ "}"

TypeName ::= PredefinedDataType | DataTypeName | AgentDataType

A record groups a collection of data elements, called fields. These elements are accessed through identifiers called the field names.

A record type declaration defines the structure of such records by specifying the names and the types of the fields.

A record type expression must contain at least one field.

TypeName in the above syntax rule can be either a keyword for a predefined data type, i.e. ⟨INT⟩, ⟨REAL⟩, ⟨BOOL⟩ or ⟨STRING⟩, or a user defined data type name, i.e. an identifier introduced in a data type declaration, or an agent data type.

For example, the following is a record type declaration.

type Person1 = struct {

name: string;

day: int; month: int; year: int;

}

In the above example, the name of the record data type is Person1. It contains four fields named as name, day, month, year, respectively. The field name is of string data type. The fields day, month, and year are of integer data type.

An alternative to the above data type declaration is given below.

type Date = struct {

day: int;

month: int;

year: int;

}

type Person2 = struct {

name: string;

DoB: Date;

}

In this example, two record data types are defined. The first is named Date, which consists of three fields day, month and year of integer type. The second is a record type named Person2, which consists of two fields name of string type and DoB of the structured type Date.

The following is an example of record type that contains an agent type as a field type. Let Peer be a caste name declared in a caste declaration; see Chapter 8 for details.

type person3 = struct {

name: string;

DoB: Date;

PDA: Peer;

}

In this record data type, the field PDA holds the identity of an agent in caste Peer.

List Types

A list is an ordered sequence of elements all of the same type. A list type declaration defines a particular named type to be a list. The syntax in EBNF is:

ListTypeExpr ::= ⟨LIST⟩ "{" TypeName "}"

The type name gives the type to which all elements of the list must belong.

The following is an example of list data type declaration, which define data type IntList as a list of integers.

type IntList = list { integer };

The element data type of a list can also be a structured data type. For example, the following data type contains a list of information about persons.

type ClassList = list { Person2 };

The following are two examples of lists type declarations, whose element date types are agent data types. The values of ListOfWorkers data type are lists whose elements are the universally unique identifiers of agents in the caste called Workers. For the type ListOfAgents, the elements can be the universally unique identifiers of all agent in any caste.

type ListOfWorkers = list { Agent{Workers} };

type ListOfAgents = list { Agent };

Enumerate Types

An enumerate data type contains a finite set of user defined values represented in the form of identifiers. A enumerate date type declaration explicitly lists its possible values. The syntax in EBNF is:

EnumerateTypeExpr ::=

⟨ENUM⟩"{" EnumValue=Identifier { "," EnumValue=Identifier } "}"

The comma-separated list of identifiers denotes the values that the enumerate type can take. For example:

type Day = enum { Mon, Tues, Wed, Thur, Fri, Sat, Sun }

The following is an example of record type Person4, which uses an enumerate data type as a field.

type Gender = enum { Male, Female } ;

type Person4 = struct {

name : string ;

sex : Gender;

DoB : Date;

}