ChapelCon2025-Slides/type-level/abstract.txt

33 lines
2.0 KiB
Plaintext
Raw Normal View History

Chapels type system can be surprisingly powerful. In addition to “usual”
features such as generics and polymorphism, Chapel provides the ability to
manipulate types using functions; this involves both taking types as arguments
to functions and returning them from these functions. This can enable powerful
programming techniques that are typically confined to the domain of
metaprogramming.
For example, although Chapels notion of compile-time values — params — is
limited to primitive types such as integers, booleans, and strings, one can
encode compile-time lists of these values as types. Such encodings can be used
to create compile-time specializations of functions that would be otherwise
tedious to write by hand. One use case for such specializations is the
implementation of a family of functions for approximating differential
equations, the Adams-Bashforth methods. Some variants of these methods can be
encoded as lists of coefficients. Thus, it becomes possible to define a single
function that accepts a type-level list of coefficients and produces a
“stamped out” implementation of the corresponding method. This reduces the
need to implement each method explicitly by hand. Another use case of function
specialization is a type-safe printf function that validates that users
format specifiers match the type of arguments to the function.
More generally, Chapels types can be used to encode algebraic sums (disjoint
unions) and products (Cartesian) of types. This, in turn, makes it possible to
build arbitrary data structures at the type level. The lists-of-values case
above is an instance of this general principle. Functions can be defined on
type-level data structures by relying on overloading and type arguments.
Programming in this manner starts to resemble programming in a purely
functional language such as Haskell.
Though this style of programming has not seen much use thus far, it can be a
powerful technique for controlling types of arguments or constructing highly
customized functions with no runtime overhead.