Prolog Object-oriented Embedded Manager, POEM --------------------------------------------- This note explains how to use the simple object manipulation package for Prolog systems, POEM. 1. Class Definition ------------------- POEM makes available some of the features found in languages like Simula-67. Classes may be defined, objects (instantiations of classes) created and operated on as high- level entities. An example is often the best way to introduce an idea. Suppose that points are to be represented in 2-dimensional Cartesian co-ordinates, and only the quadrant 0 <= x,y <= 10 is to be considered: class point(X, Y) checks ( X >= 0, X =< 10, Y >= 0, Y =< 10 ) body identical( point(X1, Y1) ) => ( X1 = X, Y1 = Y ) -&- distance2( point(X1, Y1), Dist ) => Dist is (X1-X)*(X1-X)+(Y1-Y)*(Y1-Y). This declaration then sets up a class 'point'. The clauses following 'checks' are executed whenever a new point object is created, and the 'checks' goal must succeed for the object to be successfully instantiated. Two predicates are defined to manipulate the class: identical/1 succeeds if the argument point structure is the same as the point that owns this incarnation of identical/1. distance2/2 instantiates its second argument to the square of the distance between the two points. 2. Subclass Definition ---------------------- As in Simula, a hierarchy of classes may be established. This allows subclasses to be defined with all the checks and predicates of their superclasses, and further subclass specific ones. For example, suppose a class 'rhombus' has been defined. A rhombus is specified by any three of its vertices. Further to this, some operations can be defined only for squares owing to their special symmetry. This setup could be realised by: class rhombus(P1, P2, P3) checks /* ensure that P1, P2, P3 are valid points */ body area(A) => /* find the rhombus's area */. square(P1, P2, P3) class rhombus checks /* extra checks for squares */ body circle(C) => /* find the circumcircle */. For objects of class 'square', both the predicates 'area' and 'circle' are available. Note that all the initialisation checks defined for rhombuses are automatically applied to squares too. Extra ones specific to squares may be defined. If class 'square' were to define another predicate 'area', then this new version would be executed only on failure of the superclass 'rhombus' version of 'area'. Subclasses must have the same description list as their superclass, otherwise the superclass predicates cannot meaningfully be applied. 3. Class Syntax Summary ----------------------- Classes of objects may be defined with the following syntax: ::= | ::= class