Simple Term Classes

The simplest sort of term class declaration has the following form:

:- class ClassName = term(Term).

This declares that any term that unifies with Term is an instance of class ClassName. For example, you might declare:

:- class rgb_color = term(color(_Red,_Green,_Blue)).

color(R,_G,_B) >> red(R).
color(_R,G,_B) >> green(G).
color(_R,_G,B) >> blue(B).

:- end_class rgb_color.

This would declare any term whose principal functor is color and arity is three to be an object of class rgb_color. Given this declaration, entering the goal

color(0.5, 0.1, 0.6) >> blue(B)

would bind B to 0.6.

Note that you cannot use create/2 to create a term class instance. Since they are just ordinary terms, you can create them the same way you’d create any ordinary Prolog term. Similarly, you cannot modify an existing term class instance.

You may specify a term class as the type of a slot of an ordinary class. This is effectively the same as specifing the type to be term. In particular, fetching and storing term class slots is not very efficient. Also, the default value for slots of term class type is ''; this is because not enough is known about a simple term class to determine a better default. For an explanation of how to avoid these pitfalls, see obj-tcl-tce.

Send feedback on this subject.