3. How To Use Objects

3.1. Objects and Sorts

In CCalc code we often refer to finite sets, for instance, sets of integers. A set can serve as the set of values of a fluent, or as the domain of a variable, or for other purposes. The language of CCalc allows us to introduce names for finite sets. These names are called sorts, and the elements of a set are called objects of the corresponding sort. Sorts can be used in variable and constant declarations.

For example, the fragment

:- constants
 c :: inertialFluent(0..n);
 a :: exogenousAction.

:- variables
 I :: 0..n-1.
of file coins can be rewritten as follows:
:- sorts
 number; smallnumber.

:- objects
 0..n :: number;
 0..n-1 :: smallnumber.

:- constants 
 c :: inertialFluent(number);
 a :: exogenousAction.

:- variables
 I :: smallnumber.
The sort declaration in the first two lines says that the symbols number and smallnumber are names of sets. The object declaration that follows tells us what the elements of those sets are. The names are used then in the declarations of the constant c and the variable I.

After declaring a sort in a CCalc file, we "populate" it by listing the objects of that sort. If this is not done, CCalc will treat this sort as empty.

The sort and object declarations

:- sorts
 number; smallnumber.

:- objects
 0..n :: number;
 0..n-1 :: smallnumber.
can be also written as
:- sorts
 number >> smallnumber.

:- objects
 0..n-1 :: smallnumber;
 n :: number.
The symbol >> in the sort declaration expresses that the sort number is a "supersort" of sort smallnumber, so that every element of the latter is automatically counted as an element of the former. Accordingly, when we declare objects of sort number later, there is no need to include all integers in the interval 0..n; it is enough to mention n.

Sort and object declarations allow us to talk about sets that are more complex than intervals. For instance, we can declare:

:- sorts
 number.

:- objects
 1..10, 20, 50, 100 :: number.
A sort can be populated not only with numbers, but also with identifiers. Consider, for instance, the blocks world with the blocks called a, b, c, in which a block can be located on top of another block or on the table. The description of this system can include the declarations
:- sorts
 location >> block.

:- objects
 a, b, c :: block;
 table :: location.
Exercise 3.1. Consider the following modification of the domain from Exercise 1.4 (Section 1.3). Instead of 11 people around the table, there are just two, John and Mary. The value of the fluent owner tells us which of them has the coin. When the action give is executed, the current owner of the coin gives it to the other person. This action was executed twice, and now the coin belongs to John. Use CCalc to find out who was the owner of the coin originally.


Forward to Section 3.2: Families of Fluents, Actions and Objects
Back to Section 2.3: Counting Bullets
Up to the Table of Contents