[SLP-Homepage]    [Source Modules]    [Manual]    [Run]    [Examples]
 

Ground Term (Constant)

This module defines a class cnst_c. The objects of this class correspond to ground terms, i.e. constants. Currently, SLP supports only symbolic constants (atoms) and integer values, but one could easily add constants of other data types. This should require only changes in this module and in the scanner/parser.

If the ground term is a symbolic constant (atom), the term object contains a pointer to an object of class atom_c (the symbol table entry). If the ground term is an integer, it contains the integer value.

It is possible that there are different cnst_c objects that are actually the same term, therefore this class has a method equal_to that compares the atom pointers or integer values. Of course, it is an alternative to represent all data values in the symbol table in order to ensure that different objects cannot be equal terms. The interface of this module hides such details, so the change should be easy. However, in Prolog systems data type values are usually not represented in the symbol table, so I followed this example. Probably this is more efficient if an input program uses many different integers and computes integers at runtime (the latter is currently impossible in SLP, but one could easily imagine such an extension for a future version).

Note that there are other classes for argument terms in input formulas (class term_c), and for argument terms in rules (class rarg_c). Both use objects of class cnst_c for ground terms.


Construction:

Because there are two kinds of ground terms (more may be added later), the constructor is overloaded:

Constructor from Symbolic Constant (Atom):
cnst_c(atom_t atom);
This method constructs a new ground term that is the symbolic constant atom.
Constructor from Integer:
cnst_c(int n);
This method constructs a new ground term that is the integer n.


Object Methods:

is_atom:
bool_t is_atom(void) const;
This method returns BOOL_TRUE if this term is a symbolic constant (atom). Otherwise it returns BOOL_FALSE.
is_int:
bool_t is_int(void) const;
This method returns BOOL_TRUE if this term is an integer Otherwise it returns BOOL_FALSE.
get_atom:
atom_t get_atom(void) const;
This method may only be called if this term is a symbolic constant (atom). It then returns a pointer to the corresponding object of class atom_c (symbol table entry).
get_int:
int get_int(void) const;
This method may only be called if this term is an integer. It then returns the integer value.
equal_to:
bool_t equal_to(cnst_c *cnst) const;
This method checks whether this ground term is equal to the ground term cnst. It is not sufficient to compare the pointers for checking two ground terms for equality, since the same atom or integer may appear in two different cnst_c objects. Therefore this method looks inside the objects and compares the two atoms or integers.
print:
void print(void) const;
This method prints the term, i.e. the name of the symbolic constant or the integer value.


Implementation:


Stefan Brass (sbrass@sis.pitt.edu), October 10, 2001.    [HTML 3.2 Checked]