GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing en.wikipedia.org — Type class - Wikipedia This article is about polymorphic type systems in computer science. For the mathematical class of order-types of a given cardinality, see Glossary of set theory § T.
Not to be confused with class (computer programming).
In computer science, a type class is a type system construct that supports ad hoc polymorphism in a programming language. This is achieved by adding constraints to type variables in parametrically polymorphic types. Such a constraint typically involves a type class Type classes were first implemented in the language Haskell after first being proposed by Philip Wadler and Stephen Blott as an extension to OverviewType classes are defined by specifying a set of function or constant names, together with their respective types, that must exist for every type that belongs to the class. In Haskell, types can be parameterized; a type class class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool where The type variable Eq :: Type -> Constraint The declaration may be read as stating a "type elem :: Eq a => a -> [a] -> Bool elem y [] = False elem y (x:xs) = (x == y) || elem y xs The function Any type Type classes are different from classes in object-oriented programming languages. Specifically, Type classes are closely related to parametric polymorphism. For example, the type of Higher-kinded polymorphismA type class need not take a type variable of kind class Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b That Monad :: (Type -> Type) -> Constraint Multi-parameter type classesType classes permit multiple type parameters, and so type classes can be seen as relations on types.[6] For example, in the GHC standard library, the class Like multimethods, multi-parameter type classes support calling different implementations of a method depending on the types of multiple arguments, and indeed return types. Multi-parameter type classes do not require searching for the method to call on every call at runtime;[7]: minute 25:12 rather the method to call is first compiled and stored in the dictionary of the type class instance, just as with single-parameter type classes. Haskell code that uses multi-parameter type classes is not portable as of the Haskell 98 standard, which is not the newest standard. The popular Haskell implementations, GHC and Hugs, support multi-parameter type classes. Functional dependenciesIn Haskell, type classes have been refined to allow the programmer to declare functional dependencies between type parameters—a concept inspired from relational database theory.[8][9] That is, the programmer can assert that a given assignment of some subset of the type parameters uniquely determines the remaining type parameters. For example, a general monad Simon Peyton Jones has objected to the introduction of functional dependencies in Haskell on grounds of complexity.[10] Type classes and implicit parametersType classes and implicit parameters are very similar in nature, although not quite the same. A polymorphic function with a type class constraint such as: sum :: Num a => [a] -> a can be intuitively treated as a function that implicitly accepts an instance of sum_ :: Num_ a -> [a] -> a The instance However, there is a crucial difference: implicit parameters are more flexible; different instances of As an example, an ordered set (of type Instances (or "dictionaries") in Scala type classes are just ordinary values in the language, rather than a completely separate kind of entity.[12][13] While these instances are by default supplied by finding appropriate instances in scope to be used as the implicit parameters for explicitly-declared implicit formal parameters, that they are ordinary values means that they can be supplied explicitly, to resolve ambiguity. As a result, Scala type classes do not satisfy the coherence property and are effectively a syntactic sugar for implicit parameters. This is an example taken from the Cats documentation:[14] // A type class to provide textual representation trait Show[A] { def show(f: A): String } // A polymorphic function that works only when there is an implicit // instance of Show[A] available def log[A](a: A)(implicit s: Show[A]) = println(s.show(a)) // An instance for String implicit val stringShow = new Show[String] { def show(s: String) = s } // The parameter stringShow was inserted by the compiler. scala> log("a string") a string Rocq (formerly named Coq), version 8.2 onward, also supports type classes by inferring the appropriate instances.[15] Recent versions of Agda 2 also provide a similar feature, called "instance arguments".[16] Other approaches to operator overloadingIn Standard ML, the mechanism of "equality types" corresponds roughly to Haskell's built-in type class SML's and OCaml's modules and functors can play a role similar to that of Haskell's type classes, the principal difference being the role of type inference, which makes type classes suitable for ad hoc polymorphism.[17] The object oriented subset of OCaml is yet another approach which is somewhat comparable to the one of type classes. Related notionsAn analogous notion for overloaded data (implemented in GHC) is that of type family.[18] In C++, notably C++20, has support for type classes using "concepts". As an illustration, the above mentioned Haskell example of typeclass using std::convertible_to; // equivalent to std::equality_comparable template <typename T> concept EqualityComparable = requires (T a, T b) { { a == b } -> convertible_to<bool>; { a != b } -> convertible_to<bool>; }; // using concept EqualityComparable: template <EqualityComparable T> [[nodiscard]] constexpr bool isEqual(const T& x, const T& y) noexcept { return x == y; } In Go, an interface can be seen as a type class, and is roughly equivalent to a C++ concept.[19] In Java and C#, interfaces are similar to type classes, defining an "interface" of methods to implement (see Java interfaces). In Clean typeclasses are similar to Haskell, but have a slightly different syntax. Rust supports traits, which are a limited form of type classes with coherence, and can also be seen as similar to interfaces.[20] Mercury has typeclasses, although they are not exactly the same as in Haskell. In Scala, type classes are a programming idiom that can be implemented with existing language features such as implicit parameters, not a separate language feature per se. Because of the way they are implemented in Scala, it is possible to explicitly specify which type class instance to use for a type at a particular place in the code, in case of ambiguity. However, this is not necessarily a benefit as ambiguous type class instances can be error-prone. The proof assistant Rocq has also supported type classes in recent versions. Unlike in ordinary programming languages, in Rocq, any laws of a type class (such as the monad laws) that are stated within the type class definition, must be mathematically proved of each type class instance before using them. References
External links
Hidden categories:
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |