Title: A Comparative Study of Language Support for Generic Programming
1A Comparative Study of Language Support for
Generic Programming
- Programming Languages Seminar
- University of Illinois Urbana-Champaign
- March 10, 2004
2Overview
- Generic Programming
- Features for proper language support
- Discussion of Findings
- Questions
3Generic Programming
- What is generic programming?
- Generic programming is a sub-discipline of
computer science that deals with finding abstract
representations of efficient algorithms, data
structures, and other software concepts, and with
their systematic organization. The goal of
generic programming is to express algorithms and
data structures in a broadly adaptable,
interoperable form that allows their direct use
in software construction.
4Key Generic Programming Ideas
- Expressing algorithms with minimal assumptions
about data abstractions, and vice versa, thus
making them as interoperable as possible - Lifting of a concrete algorithm to as general a
level as possible with losing efficiency i.e.,
the most abstract form such that when specialized
back to the concrete case the result is just as
efficient as the original algorithm
5Key Generic Programming Ideas, cont.
- When the result of lifting is not general enough
to cover all uses of an algorithm, additionally
providing a more general form, but ensuring that
the most efficient specialized form is
automatically chosen when applicable - Providing more than one generic algorithm for the
same purpose and at the same level of
abstraction, when none dominates the others in
efficiency for all inputs. This introduces the
necessity to provide sufficiently precise
characterizations of the domain for which each
algorithm is the most efficient.
6Key terminology
- Concept formalization of an abstraction as a set
of requirements on a type or set of types - Refine incorporating one concept into another
- Model types that meet the requirements of a
concept model that concept
7Features for Proper Language Support
- Multi-type concepts can multiple types be
simultaneously constrained?Example in a graph
type, you may want to constrain the type of the
nodes and edges at the same time - Multiple constraints can multiple constraints be
placed upon a type parameter?Example
8Features for Proper Language Support
- Associated type access can a type be mapped to
other types easily within a generic
function?Example If a generic function for a
graph uses an associated type for a node, how
easily can this type be specified/used? - Retroactive Modeling can a new modeling
relationship be added easily after a type has
been defined?Example Once a generic type of
list has been specified, how easily can it be
used with other types?
9Features for Proper Language Support
- Type aliases are mechanisms available for
supporting shorter names for types?Example Is
it possible to give a highly parameterized type
name a simple type, such as with typedef, or is
it required to continue using all types in the
name? - Separate compilation are generic functions
type-checked and compiled independently of their
use?Example Is the generic function generated
for each type (C), or can it be compiled
separately (Java) and then specified where
needed?
10Features for Proper Language Support
- Implicit instantiation can type parameters be
deduced without requiring explicit syntax for
their instantiation?Example Once created, is
it possible to use a function without specifying
the type parameter information? - Consise syntax is the syntax required to compose
multiple generic layers independent of the scale
of composition?Example can multiple layers be
composed without requiring lots of additional
typing syntax?
11Findings
12Support in Various Languages
13Generics Features
14Establishing the Modeling Relation
- Two basic types name conformance and structural
conformance - Name conformance (Java, C, Eiffel, Haskell) an
explicit declaration links a concrete type to the
concept it models - Structural conformance (C, ML) a type models a
concept if it meets the structural requirements
of the concept
15Establishing the Modeling Relation
- Named conformance can lead to difficulties with
retroactive modeling, since the concepts are
identified in the concrete type - This is a problem in Java, C, and Eiffel, but
not Haskell, since Haskell separately specifies
modeling and refinement relationships - Structural conformance can lead to the problem of
accidental conformance (rarely), where
syntactically identical but semantically
different methods could be used
16Subtyping is not Modeling or Refinement
- Constraints on multiple types can be implemented
using Java - However, it is difficult to do this consistently
using just a subtyping mechanism - Often, very verbose constraints are needed, with
much repetition
17Access to Associated Types
- Some languages, like C (through a trait
mechanism) or ML, provide support for accessing
associated types easily in the generic function - Others make this more difficult, requiring the
associated types to be specified in each generic
function
18Implicit Instantiation Type Aliases
- Implicit instantiation is required to prevent
extremely verbose generic function calls - Type aliases are required to prevent extremely
verbose type specifications - When these features are not present, type
declarations and function calls can become very
painful
19Lack of type aliases
20Questions?