MENTAL
 Main Menu
 Applications
 Computer Science
 Generic Programming


Generic Programming
 GENERIC
PROGRAMMING

"To think is to forget differences, it is to generalize, to abstract" (Borges).

"We think in generalities, but we live in details" (A.N. Whitehead).

"One should always generalize" (Carl Jacobi).

"The generic can be more intense than the concrete" (Borges).



Generic Programming

Generic programming is a technique of developing software components as general as possible, with several objectives:
  1. So that components can be reused in other applications.

  2. So that they depend as little as possible on the execution environment, implementer details are ignored and interoperability is achieved.

  3. To make developments more conceptual and intelligible.
For example, an algorithm could rely on general concepts, such as: traversing a data structure, accessing one of its elements, selecting the elements that meet certain conditions, etc.

While these goals are desirable and beneficial, there are some drawbacks:
  1. Developments may be less efficient. Generality and efficiency are, in general, opposing forces.

  2. If the programming language is not high-level, generic developments can be more extensive than specific ones. This is due to the additional code needed to parse the type of each argument, the different treatment associated with each type, the possible inclusion of additional parameters, etc.
The implementation of generic programming depends primarily on the language used and the level of abstraction it supports.


The generic aspects

The concept "generic" in generic programming has different aspects:
Implementation in MENTAL

Generic programming is at its best in this language, since it covers all the aspects mentioned above:
Examples
  1. Copy the n first elements of the sequence x into the sequence y. Supone n>0.

    ⟨( Copy(x and n) = [y\i = x\i)/(i=[1...n])] )⟩

    This function is valid for any sequence, regardless of the type of its components.

    Application example:

      x=(a (b c) 17.4 {d e} u v)

      y=(1 2 3 4 5 6)

      Copy(x y 4) // copy the first 4 elements of x into y

      y // ev. (a (b c) 17.4 {d e} 5 6)

  2. Replace the n first elements of the sequence x by the element y. Supone n>0

    ⟨( Substitute(x n y) = [x\i = y)/(i=[1...n])] )⟩

    Application examples:

      x=(a (b c) 17.4 {d e} u v)

      Substitute(x 4 y)

      x // ev.(y y y y y y u v)

      x=(a (b c) 17.4 {d e} u v)

      Substitute(x 2 (s1 s2))

      x // ev. ((s1 s2) (s1 s2) (s1 s2) 17.4 {d e} u v)

  3. Sum all components of a sequence.
    In this case, there is a single parameter (the sequence), whose number of elements is variable. It is equivalent, in short, to a variable number of parameters.

      ⟨( Sum(x) = +⊣x )⟩

    Application examples:

      Add(1 2 3 4) // ev. 10

      Add(1234) // ev. 10

      Add(a b c d) // ev. a+b+c+d

      Sum(abcd) // ev. a+b+c+d

      Add(a 1 b 2 c 3) // ev. a+b+c+6


Addenda

History of generic programming

The Lisp language can be implicitly considered as the first generic programming language, thanks to its high level of abstraction, its unified system of code and data storage (the lists), its dynamic types, its parameterization capabilities, and the possibility of working with higher-order functions.

The C++ language is the first language to explicitly support two generic mechanisms: C++ is a language that has no versioning. The ANSI/ISO (1988) standard defines the core of the language. C++ is multi-paradigm (object-oriented, functional and procedural).

The efficiency of an algorithm can be relative if it is as efficient as the non-generic version (written in the same language) and absolute if it is as efficient as a non-generic version written in assembly language. With C++, relative efficiency has been achieved and approaching absolute efficiency, so that efficiency and generality are not mutually exclusive.

It is considered that true generic programming was born with the work of Alexander Stepanov with the Ada language and later transferred to C++ to create the STL (Standard Template Library, Standard Template Library), a library of generic software components implemented as templates, which was adopted as the ANSI/ISO standard of the language. This library was followed by others, also of generic type, such as the Boost Graph Library and the Matrix Template Library. C++ then became a multi-paradigm language enriched with generic programming.

Generic programming is a paradigm that has been gaining increasing importance, such that, in recent years, many languages (such as Standard ML, Haskell, Eiffel and Java) have incorporated generic mechanisms (at least in a basic form), inspired mainly by the STL model. Recent specific generic programming languages are Generic Haskell (2000) and Generic C# (2003).


STL

The C++ template library was designed to achieve maximum reusability without sacrificing efficiency. Its importance lies in its concepts (or abstract categories), rather than in the implementing details. STL sources have the C syntax. It is even possible to program in STL without knowing C++. Among these concepts the most prominent are the following:
Bibliography