The prospect of GP Technology -- one day after hongjun

Source: Internet
Author: User
The prospect of GP Technology -- one day after hongjunMo Huafeng

Since the emergence of advanced languages, types have always been the core of languages. Almost all language features require type as a prerequisite. The type is like heaven and earth, and exists before everything. However, is there anything more primitive and essential than the type, but exists before it? Read down. :)

Generic and type


The simplest and most intuitive description of generics should be: the class
Type. Although such a description is not complete, it is enough to explain the problem. As early as 1960s, the concept of generic has emerged. It originally exists in the name of "parameterized type. Developed in the end of 1970s
Dinosaur-level ADA (I don't mean Augusta Ada Byron Lovelace: The lady of the dinosaur, from the perspective of portraits, the programmer's ancestor's milk looks pretty :)), not having OOP (ada83 ), you have implemented the generic type (generic ). Although generics have a long history, they are still developing in the early 1990s S,
Alexander A. Stepanov, a genius, created STL and promoted the establishment of generic programming.
For simple purposes, I applied an old generic container to explain the concept of generics. (Even if I'm perfunctory: P, after all, the major drama is behind. For details, see the previous link ). Suppose I need an int stack during programming, so I made a class to implement this stack:
Class intstack {...};
It works well. After two days, I needed another stack, but the type changed to double. So I will write another one:
Class doublestack {...};

It seems that, through the horse honeycomb, various types of stacks are constantly emerging, including string, datetime, point, and even dialog. Each
You have to write a class, and the code is almost the same each time, but the types involved are different. As a result, I eagerly expect something to appear. It is just a code framework that implements the stack architecture.
Function, but the type is empty. When I need the new type, I will get a new Stack class.
This is generic.
However, this alone is not enough to achieve GP's prestige.
I have a strange demand (haha, continue to be perfunctory. :)):
Make a template with a vector member:
Template <typename T>
{
...
Vector <t> m_x;
};
However, if the type argument is of the int type, set <> is required. For ease of use, the Template Name must be. Therefore, we have to use the following techniques:
Template <> A <int>

{

...

Set <t> m_x;

};

This is called specialization, which is equivalent to telling the compiler to use the following parameter if the real parameter of the type is int. Otherwise, use the previous one. Specialization is actually the selection of the execution template by the compiler based on the Type arguments. In other words, specialization is a compilation scheduling technology.
Here is another more eccentric requirement: If the type argument is a pointer, use list <>. This requires another special feature:
Template <typename T> A <t *>
{
...
List <t> m_x;
}
This is partial specialization, and the previous one is called explicit specialization. Local features are the Template Selection performed based on the characteristics (or categories) of the type real parameters.
Finally, there is another odd requirement: deque is used if the type of the real parameter has a form such as the type of the void func (int A) member function. This... is a bit difficult. The existing C ++ compiler cannot meet this requirement. However, we still hope that we can solve this problem in the new version C ++ 09 in the future.

Concept and type

Concept is the inevitable result of GP development. As mentioned above, we sometimes need the compiler to recognize certain types of features, such as having specific members, and then perform some operations. The following is a common example:
Swap () is a very useful function template that can exchange the content of two objects, which is the basis of the SWAp method. The basic definition of swap () is similar to this:
Template <typename T> swap (T & LHS, T & RHs ){
T TMP (LHS );
LHS = RHS;
RHS = TMP;
}

However, if the object to be exchanged is a large object such as a container, the performance of this swap () will be poor. Because it performs three copies, this is often O (n. Standard containers provide
The swap member function obtains the O (1) performance by switching the pointer pointing to the data buffer in the container. Therefore, swap () members are preferred. However, this requires programmers to identify whether an object exists.
Swap member, and then call it. If the swap () function can automatically identify whether an object has swap members, it is much more convenient. If a swap member exists, call the Member. Otherwise,
It is the version that is exchanged through intermediate variables.
This requires the concept technology:
Template <swappable T> void swap (T & LHS, T & RHs ){
LHS. Swap (RHs );
}
Swappable is a concept:
Concept swappable <typename T> {
Void T: swap (T &);
}
Therefore, if an object with a swap member function exactly matches the swappable concept, the compiler can use the second version to complete the exchange within the complexity of O (1. Otherwise, the previous version is used:
Vector A, B;
... // Initialize A and B
Swap (a, B); // use the last version
Int c = 10, D = 23;
Swap (c, d); // use the previous version
The swap () here also applies the special feature. The difference is that it is carried out under the guidance of concept. This kind of feature is also called concept based overload.

From the above example, we can see that the original special type, whether it is full or partial special, either special type or special type (such as pointer. But it cannot be more precise. For example, I
It is expected that a template can be customized for All integers (INT, long, short, Char, etc.), which was previously impossible. But with the concept, we can
Define a concept that represents all integers, and then use the concept to execute the special operation. In other words, concept makes the feature more refined, and the entire generic System
Instead, it becomes "continuous.
However, the specific concept template above does not look really nice. The template on the head is really unsightly. Since it is a concept based overload, why not directly use the overload form, instead of bringing a cumbersome template <...>:
Void fun (anytype A) {...} // #1. anytype is a forged keyword, indicating all types. It is best to use less.
Void fun (integers A) {...} // #2. integers are concept, indicating all integer types.
Void fun (floats A) {...} // #3. Floats is concept, indicating all floating point types.
Void fun (long a) {...} // #4
Void fun (int A) {...} // #5
Void fun (double A) {...} // #6
...
Int x = 1;
Long y = 10;
Short z = 7;
String S = "AAA ";
Float T = 23.4;
Fun (x); // select #5
Fun (y); // select #4
Fun (z); // select #2
Fun (s); // select #1
Fun (t); // select #3
In terms of semantics, this form is almost the same as the original template form. Note that it is almost. The following situations cannot be implemented in the overload mode:
Template <integers t> t swap (t lhs, t RHs ){
T temp (LHS );
...
}
Here, the template does two things: first, the template extracts the type T, In the function body, you can use t to execute some operations, such as the construction of the temporary object temp in the above Code. This problem is easy to solve, because the extraction type T has other methods, a typeof () operator can be implemented:
Integers swap (integers LHS, integers RHs ){
Typeof (LHS) temp (LHS );
...
}
Second, the template ensures that the LHS, RHS, and return values are of the same type. This problem can be solved through the concept constraint applied on the function:
Integers swap (integers LHS, integers RHs)
Requires sametype <LHS, RHS>
& Sametype <LHS, retval> {// retval is a written keyword used to indicate the return value.
Typeof (LHS) temp (LHS );
...
}

In contrast, heavy load is cumbersome. In general, although the form of heavy load is lengthy, the meaning is clearer and more intuitive. In addition, the concept interface function requires consistent parameter types.
It is usually rare (generally in basic types, such as integer type. These operations require that the type have a specific length to avoid overflow. Other types, especially user-defined types, are usually caused
Does not have too many requirements on the internal features of the type. Otherwise, generic algorithms should not be used ). If the syntax can be changed, replace typeof, =, @.
The sametype method reduces the amount of code:
Integers swap (integers LHS, integers RHs)

Requires @ LHS ==@ RHS & @ LHS ==@ retval {

@ LHS temp (LHS );

...

}


Concept, type, and Object

Things can also be exaggerated. Previously, the generic model was made special. Can we also make some "special" on the type? Of course:
Void fun (int );
Void fun (int A: A = 0); // For int type, A = 0 is "special"
More complete, there can also be "local special ":
Void fun (int A); // #1
Void fun (int A: A = 0); // #2
Void fun (int A: A> 200); // #3
Void fun (int A: A <20 & A> 10); // #4
Void fun (int A :( A> 70 & A <90) | (A <-10); // #5
...
Int A = 0, B = 15, c = 250, D =-50;
Fun (80); // use #5
Fun (50); // use #1
Fun (a); // use #2
Fun (B); // use #4
Fun (c); // use #3
Fun (d); // use #5

In fact, this is nothing more than adding a set of constraints after the parameter declaration to indicate the selection conditions of the function of this version. Function versions without constraints are selected without any matching constraints. For
That is, for the call of a number or static object, the function is executed during the compilation period, and the compiler directly calls the matching version according to the conditions. For calling a variable as a real parameter, it needs to be expanded. The compiler will automatically generate
Code below:
// Rename the function and assign it a unique name.
Void fun_1 (int A); // #1

Void fun_2 (int A); // #2

Void fun_3 (int A); // #3

Void fun_4 (int A); // #4

Void fun_5 (int A); // #5
// Then construct the dispatch function
Void fun_d (int ){
If (A = 0)
Fun_2 ();
Else if (a-> 200)
Fun_3 ();
...
Else
Fun_1 ();
}
In some cases, you may need to restrict the members of an object. In this case, you can use this form:
Struct
{
Float X;
};
...
Void fun (A: a.x> 39.7 );
...

The so-called "special" applied to types is actually just a syntactic sugar, but the compiler automatically generates the dispatch function. This mechanism already exists in Haskell and other languages.
To provide great flexibility. If this mechanism is not available, you must manually modify the dispatch function once you need to add a function assignment condition. If these functions, including the dispatch function, are the Code provided by a third party
It will be very troublesome. Once this mechanism is available, you only need to add a corresponding function overload.
When the concept-type overload and the type-object overload are combined, the greater effect is displayed:
Void fun (anytype );
Void fun (integers );
Void fun (floats );
Void fun (long );
Void fun (int );
Void fun (double );
Void fun (double A: A = 0.8 );
Void fun (short a: A <10 );
Void fun (string a: a = "ABC ");
...

Concept-type-the object overload system follows the principle that the most specific matching function is preferred. This is actually an extension of the type overload rule. All types are larger
Concept is more special, and all object constraints are more special than their types. For concept, if concept a refine from Concept
B, so a is more special than B. Similarly, if the constraint of one type is stronger than that of another type, the first one is more special than the last one. For example, a = 20 is more special than a> 10. In combination, you can
There is an abstract rule: two constraints (concept, or the constraint applied on the object) a and B, which are used to generate a set on the type or object respectively, if the set generated by a is a set generated by B
So that A is more special than B.
Based on these rules, a "special tree" can be constructed for a function overload ":

The closer it is to the root of the tree, the more general it is. The closer it is to the leaves, the more special it is. The real parameters used for the call are searched on the "special tree" to find the most matched function version.
Concept-type-the object system unifies generics, types, and objects in a single system, making function overloading (special) easier forms and rules. In addition, this system can also be used in class templates to simplify the definition and use of templates.

Class Template

The special form of C ++ class templates is not very popular:
Template <typename T> A {...}; // Basic Template
Template <> A <int> {...}; // explicit special (full special)
Template <typename T> A <t *> {...}; // localized Specialization
In C ++ 09, you can directly use concept to define the type parameters of the template:
Template <integers t> {...};
In essence, this form itself is a local special form, so the original cumbersome local special form can be abolished, on behalf of the concept style:
Template <pointer T> A {...}; // pointer indicates that the pointer-specific template is used here.
Similarly, if the promotion is to be fully-specific, the form will be further simplified:
Template <int> A {...}; // This form is a little abrupt. Here we only want to express this meaning, and there should be a more "harmonious" Form
If the template parameter is an object, the existing definition form is used:
Template <int A> {...};
Furthermore, we can introduce object constraints:
Template <int A: A> 10> {...};
In addition, a basic template is required before the template is made special in C ++. In fact, this is redundant. The D language has removed this restriction, which is of great help to simplify the use of the template.


In essence...

Essentially, we can regard all types as a set T = {Ti}, while concept is a constraint applied on the type set. With the concept constraint, we can obtain the class
A subset C of type set T. Theoretically, CJ, the type subset corresponding to all concept, constitutes the power set {CJ} of the type set }. In {CJ}, two types of subsets are special. One group is
T-book
Body, that is, all types. If a concept does not impose any constraints on T, C0 = T is obtained. The second category is another extreme. There is a group of concept which is obtained after being applied to T.
The subset contains only one type: CI = {Ti }. Since this set of concept has a one-to-one correspondence with the type, we can use this set of concept to refer to the type. That is, to set the type
It is processed as a special concept. In this way, concept is the same type in a single system. This processing can greatly benefit us.

This special concept still uses the corresponding type name as the title, which is still called "type", but in essence it is still concept. A special concept is created for any type. If you use a specific type in the template, it is actually using the corresponding special concept:

Void func (typea A); // although typea is used, it actually refers to the special concept corresponding to typea.

Under the role of this concept system, the special and heavy loads of function templates are unified as a whole (concept based overload ).
The role of the "special" type is also the same. For a type T, all its objects constitute a set O. If a group of constraints apply to O
A constraint corresponds to a subset of O. Theoretically, we can construct a set of constraints that correspond to each subset of o one by one. Similarly, two types of subsets in these subsets are special. One is all objects
. The other type is only a subset of an object. Therefore, we can use the constraints corresponding to this set of special object subsets to refer to the corresponding objects. That is, the object is treated as a special constraint. In this case, the type and Object
It is also unified in a system.
Furthermore, types are processed as special concept logically, and objects are processed as special types. As a result, the three can be integrated into a single system and participate in the special process together.

Summary

Although forms cannot represent the essence, changes in forms often lead to many beneficial advances. More importantly, many essential changes will always follow the changes in form. By adding the concept, type, and Object
Logically integrated into a unified system, the template, special, function overloading and other mechanisms can be promoted to achieve a unified form. This simplifies the use of these features. This is also the current emphasis on language (Tool) usability
Is an inevitable demand. This form of unification is not a surface change such as syntactic sugar. It relies entirely on the Establishment and Development of concept, a new type description (generic) system.
The emergence of concept makes up for the shortcomings of generic systems in the past, recovers the missing links of generic systems, and makes up for the cracks between the same type of generic systems. Under this function, you can build a concept-type-
The abstract system of objects. A unified system includes these three original discrete concepts. In this new three-in-one system, the template features the same form of special and heavy load, so as to obtain more intuitive semantics, and
Better ease of use.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.