The intention of the rgb_color
class presented above is to represent a
color as a triple of floating point numbers between 0.0 and 1.0.
But the above definition does not restrict the arguments of the
color
term in any way: any color/3
term is considered
to be an instance of the rgb_color
class.
The second form of term class declaration allows you to specify constraints on instances of a term class. The form of such a declaration is as follows:
:- class ClassName = term(Term, Constraint).
This declares that any term that unifies with Term and satisfies Constraint is an instance of class ClassName. The Constraint term is an ordinary Prolog goal, which will usually share variables with Term.
To extend our rgb_color
class example so that only color/3
terms whose arguments are all floats between 0.0 and 1.0 are instances
of rgb_color
, we would instead begin the definition as follows:
:- class rgb_color = term(color(Red,Green,Blue), (float(Red), Red >= 0.0, Red =< 1.0, float(Green), Green >= 0.0, Green =< 1.0, float(Blue), Blue >= 0.0, Blue =< 1.0)).
Note the parentheses around the constraint in this example. Whenever the constraint contains multiple goals separated by commas, you will need to surround the goal with parentheses.
With this definition of the rgb_color
class, only color/3
terms whose arguments are all floating point numbers between 0 and 1
inclusive will be considered to be instances of rgb_color
.