Frequently Asked Questions
on the Concept-Oriented Programming (COP)
Alexandr Savinov
http://conceptoriented.org/
First started: 14.03.2006
Last updated: 30.03.2007
1 General Questions
1.1 What is Concept-Oriented Programming?
1.2 What are main COP principles?
1.3 What is the main programming construct in COP?
1.4 What is basically a COP program?
1.5 What is the main concern in a COP program?
1.6 How a COP program is functioning?
1.7 How a COP program is being developed?
2 Concepts and Inclusion
2.1 What is a concept?
2.2 What is a concept inclusion?
2.3 What are concept instances?
2.4 How concepts are declared?
2.5 What is a continuation method?
2.6 What is the role and properties of references?
2.7 What is the role and properties of objects?
2.8 Why concepts are useful?
3 Dual Methods
3.1 What are dual methods?
3.2 If a dual method is invoked then which of two version is
really executed?
3.3 How COP methods visually differ from OOP methods?
4 Object Creation and Deletion
4.1 How objects are created and deleted?
4.2 What is the purpose of the reference creation method?
4.3 What is the purpose of the object creation method?
4.4 What is the purpose of the reference deletion method?
4.5 What is the purpose of the object deletion method?
4.6 What is the sequence of object creation?
4.7 What is the sequence of object deletion?
5 Examples of "Hello World" Program
5.1 What is "Hello World" for reference substitution?
5.2 What is "Hello World" for method interception?
Concept-oriented programming (COP) is a new programming paradigm,
which generalizes object-oriented programming.
It is based on using a new programming construct, called concept.
Links:
Here are some general principles of COP:
The main programming construct in COP is concept. Each concept has a parent concept assigned explicitly or implicitly and called superconcept. COP uses concepts where OOP uses classes.
Links:
At the highest level a COP program is simply a hierarchy of concepts. Each concept in this hierarchy has one parent concept, called superconcept. This hierarchy has one root which is provided by the compiler, interpreter or the underlying runtime environment. At run-time a COP program is a hierarchy of objects where each object has a parent object. Thus any object of a concept-oriented program exists in the context of its parent concept object.
The main concern of any concept-oriented program is indirect object representation and access (ORA) within a system. This concern is implemented by means of concepts rather than via classes in OOP. ORA functions cross-cut the whole program which means that one and the same ORA code is used in very different places of the program.
A COP program runs by accessing its indirectly represented objects. Since a great deal of its functionality is concentrated on space borders, representation and access are its main functions which are executed behind the scenes when objects are being accessed. An object access in a COP program may well result in a rather complex sequence of intermediate actions. COP allows the programmer to describe the whole sequence of intermediate actions performed during object access rather than only the last step.
A COP program is a hierarchy of concepts at compile time and a hierarchy of reference-object pairs at run time. In order to develop such a program it is necessary to define a hierarchy of concepts and then use them just as we are using classes by creating concept instances and calling their methods. However, the design and patterns used in COP program are different. One reason is that most of functionality is concentrated on space borders and is executed implicitly behind the scenes (in OOP it is concentrated in target objects and is executed explicitly). Another reason is that environment plays more important role than objects which live in it. In this sense the most important distinction of the concept-oriented program from the object-oriented program is that the former is viewed as objects in spaces or objects in context while the latter is viewed as simply a flat set of objects. In other words, in COP we must take into account an environment where objects are going to live because frequently it play more important role and possesses more important functions.
Concept is a programming construct consisting of two classes:
Concept can be viewed as a generalization of the conventional class
as defined in OOP, i.e., a concept is a class with an additional
reference class attached to it. Informally the relationship between
concepts and classes is analogous to that between complex numbers and
real numbers in mathematics. Having two constituents in both complex
numbers (real and imaginable parts) and concepts (object class and
reference class) makes the description of processes and phenomena much
more easy and natural.
Links:
Each concept is included into another concept, called a parent concept or superconcept. If no parent has been specified then it is assumed to be a default concept, called a root concept. The main role of the parent concept consists in providing an environment, container or space for its child concepts. It describes an intermediate border and all access requests to internal child concepts pass through their parent concepts in a nested manner. The deeper this inclusion hierarchy the more indirect is the mechanism of representation and access for its internal concepts.
Since a concept consists of two classes it produces two types of instances. Instances of
the object class are referred to as objects and are passed indirectly by
reference. Instances of the reference class are referred to as references and are
passed by value. For each object there is one reference that represents it.
Links:
Each concrete concept-oriented language provides its own syntactic means for describing concepts. The simplest way to declare a concept consists in using keyword 'concept' followed by definitions of its object class and reference class:
concept MyConcept in ParentConcept
reference { /* reference class definition */ }
object { /* object class definition */ }
Here both the object class and reference class are defined just as conventional classes with a set of fields, methods and other facilities. COP also supports classes as defined in OOP which are concepts with the empty reference class.
A concept declares a pair of special continuation methods, called continue, within its object class and reference class. Just as all other methods of concepts they are dual which means that they may have two versions within the object class and the reference class. These methods are used automatically in order to execute the correct sequence of access. These methods can be viewed as doors, passes or portals in the borders between spaces - it is the only general way to intersect a border.
The continuation methods do not take any parameters and do not return any value, for example:
concept MyConcept
reference {
void continue() { /* reference continuation method */ }
}
object {
void continue() { /* object continuation method */ }
}
The main role of the reference continuation method consists in resolving the current reference
into a primitive reference which can be then used for direct access. The object
continuation method is called after this object has just been resolved so its main role consists in preparing it for access.
Links:
References are passed by value and can be stored in object fields or passed in parameters.. Their main role consists in representing objects. (However, it is not necessary and they can be thought of as data passed by value.) It is important that references do not have their own references and hence they do not have any constant position in space (constant coordinates) like objects. References are traveling elements which can reside in many points of space while objects have only one unique postion. A reference is real element in the sense that it is always available by value without any representatives or intermediate elements.
Objects and references are dual elements of the concept-oriented program. Informally objects can be thought of as material elements while reference are non-material. References can be thought of as program waves or light which are intended to transfer interactions between material objects by traveling in space.
An object is an element which posses a constant position in space. This position cannot be changed during this object life time (we can only simulate this process, for example, by creating a more or less identical clone). Once an object is created (and even before its real creation) it gets some coordinate in space which is its reference representing this object in other parts of the space.
An object is a kind virtual element because we do not know where it is stored, how it is stored and what is its substance. The only thing we know about an object is some piece of real data - its reference. Everything else is a result of interaction which produces an impression of the object having some structure and functionality. For example, if an object has a double field then it says almost nothing about this object position in space, the type of storage and how this field is stored. The object can be stored on Mars and the field value represented as a number of stones of certain form. The only real thing is its reference and the guarantee to get some double value when we call the corresponding read method. This is a consequence of the concept-oriented principle that any object access is indirect or, equivalently, instant access does not exist in nature. Hence we never know what the substance is. Instead, we have a piece of non-material object called a reference and the ability to travel in space and indirectly interact with different positions in space having a certain response. This response is is then thought of as part of the object in this location.
Concepts are useful because they allow us to effectively and naturally implement principles of the concept-oriented programming. So the question is whether these principles are useful or not. In other words, concept is simply one possible programming construct that can be used to implement concept-oriented principles just as a class is one possible construct intended to implement object-oriented principles.
Duality is one of the main concept-oriented principles. In particular, a
concept consists of two parts: an object class and a reference class.
Accordingly these two classes have their own methods which are referred to as
dual methods. For example, in the following example concept MyConcept defines
dual method myMethod in the object class and the reference class:
concept MyConcept
reference {
...
int myMethod() { return 555; }
...
}
object {
...
int myMethod() { return 111; }
...
}
Links:
One of two definitions is chosen for execution depending on the
current position in space. Informally, if the method call is made from
one side of the border then one version of the dual method is called.
If the same method is called from the other side of the space border
then the second method is executed. The method of the object class is
called from inside(from child objects) while the method of the reference class
is made from outside. Parent reference methods have precedence over (override) child reference methods and vice versa, child object methods have precedence over (override)
parent object methods. The sequence of access on reference methods is
analogous to inner mehtods in the Beta programming language.
One distinguishing feature of COP methods is the use of the continuation method which can be found somewhere in the code of the method:
int myMethod() {
...
continue();
...
}
The continuation method indicates the point in the execution process where
this method passes control further. However, we never know what concretely will be
executed at this moment. Thus frequently a COP method gets control from
somewhere (it is not called explicitly from anywhere in the program) and then
passes it to somewhere. Such an approach emphasizes its role as an intermediate
border where passing requests are being processed. The methods gets control,
perform some operations, pass control to the next processing element in the
sequence and finally performs some operations before it returns.
For object creation a concept has a pair of dual creation methods, called create, defined in the object class and in the reference class. For object deletion a concept has a pair of dual deletion methods, called delete, defined in the object class and the reference class.
concept MyConcept
reference {
...
void create() { ... }
void delete() { ... }
...
}
object {
...
void create() { ... }
void delete() { ... }
...
}
concept Main
object {
public static void main(String[] args) {
MyConcept myObject.create(); // Create object
myObject.someMethod(); // Use object
myObject.delete(); // Delete object
}
}
reference { }
This method is applied to an empty reference with null value which is interpreted as the absence of the object. Its task consists in assigning a value to this reference that can be then used to access the represented object via the resolution method. The reference creation method does not return any value because it is applied directly to the reference that has to be initialized. In other words, the fact of creation is recorded in the reference fields initialized by the creation method.
The object creation method is equivalent to the conventional object constructor as used in OOP. This method is applied to just created object (after the corresponding reference creation method) and its task consists in initializing its fields. The object creation method (constructor) does not return any value because its task consists only in initializing the fields of the object while the reference is initialized in the dual reference creation method.
This method is applied to a valid reference and its task consists in invalidating it so that it cannot be used anymore to access the underlying object. After this method returns the reference should store null, which is interpreted as the absence of the object. The reference deletion method does not return any value and the fact of deletion is recorded in invalidating the reference values.
The object deletion method is equivalent to the conventional object destructor as used in OOP. This method is applied to an object just before it is deleted and its task consists in cleaning up its fields. This method does not return any value just like other creation/deletion/continuation methods.
The sequence of creation is the same as the general sequence of access in the concept-oriented programming. The reference class and object class creation methods interleave. When a new reference segment is initialized by the reference creation method, it continues and then this object is initialized via its object class creation method. This constructor then again continues and calls the next reference segment creation method and so on till the target object.
The sequence of deletion is the same as the general sequence of access in the concept-oriented programming. The reference class and object class deletion methods interleave. Normally, just before a reference is deleted, its object is de-initialized.
Here is an example of the simplest concept-oriented program consisting of two concepts:
concept Main
reference { }
object {
public static void main(String[] args) {
HelloWorld helloWorld.create();
helloWorld.sayHello();
helloWorld.delete();
}
}
static Map map = new Map();
concept HelloWorld
reference {
int objectId;
void continue() {
System.out.println("> Before resolution of reference "+id);
Object object = map.get(objectId);
object.continue();
System.out.println("< After resolution of reference "+id);
}
}
object {
void sayHello() {
System.out.println("Hello World!");
}
}
The first concept Main has only an object class where it defines the main method. This method creates one object of concept HelloWorld and then calls its method sayHello. In OOP this would result in immediate execution of the specified method. However, in the concept-oriented programming it is not so and the programming has a possibility to indirect object representation and access.
In this example the target concept HelloWorld is has an integer field in its reference class. Hence all instances of HelloWorld will be represented by using unique integer values (creation and deletion methods are not shown). In particular, variable helloWorld in method sayHello will be an integer which replaces some system default reference. In order to resolve its references concept HelloWorld defines the continuation method. This method resolves an integer field of this reference using the map and then applies the continuation method to the primitive reference of type Object. Thus any access to the objects of concept HelloWorld will be wrapped into the resolution procedure provided by its referefence class.
The output of this program is shown below:
$ > Before resolution of reference 123
$ Hello World!
$ < After resolution of reference 123
Notice that a single call of the target object method automatically triggers an intermediate reference resolution procedure which prints two messages when it starts and before it returns.
Here is an example of the simplest concept-oriented program consisting of three concepts:
concept Main
reference { }
object {
static void main(String[] args) {
HelloWorld helloWorld.create();
helloWorld.sayHello();
helloWorld.delete();
}
}
concept HelloWorld in Indirect
reference { }
object {
void sayHello() {
System.out.println("Hello World!");
}
}
concept Indirect
reference {
void sayHello() {
System.out.println("> Enter space via sayHello");
continue();
System.out.println("< Exit space via sayHello");
}
}
object { }
The first concept Main has only an object class where it defines the main method. This method creates one object of concept HelloWorld and then calls its method sayHello. In OOP this would result in immediate execution of the specified method. However, in the concept-oriented programming it is not so and the programming has a possibility to indirect object representation and access.
In this example the target concept HelloWorld is included in concept Indirect and hence all access to its instances will be intercepted by the reference class of Indirect. Both classes HelloWorld and Indirect define method sayHello however the parent reference will intercept any access to child objects. The method of the child object will called only if the parent calls the continuation method.
The output of this program is shown below:
$ > Enter context via sayHello
$ Hello World!
$ < Exit context via sayHello
Notice that all the concepts have the empty reference class and hence there are only object classes as in OOP (there is no reference substitution and resolution). The only difference is that the parent reference is able to intercept calls to its child elements. In COP any reference has a possibility to control access into its space. It can be used for security checks, access preparation and other purposes.