| Start |
|---|
| Datatype Families Data families can be used to build datatypes that have different implementations based on their type arguments. Standalone data familiesIn the above declaration, Associated data familiesData families can also be associated with typeclasses. This is often useful for types with “helper objects”, which are required for generic typeclass methods but need to contain different information depending on the concrete instance. For instance, indexing locations in a list just requires a single number, whereas in a tree you need a number to indicate the path at each node: |
| Type Synonym Families Type synonym families are just type-level functions: they associate parameter types with result types. These come in three different varieties. Closed type-synonym familiesThese work much like ordinary value-level Haskell functions: you specify some clauses, mapping certain types to others: Open type-synonym familiesThese work more like typeclass instances: anybody can add more clauses in other modules. Class-associated type synonymsAn open type family can also be combined with an actual class. This is usually done when, like with associated data families (opens new window), some class method needs additional helper objects, and these helper objects can be different for different instances but may possibly also shared. A good example is Note how in the first two instances, the implementation of While non-injectivity opens up some possibilities like the above, it also makes type inference more difficult. For instance, the following will not typecheck: In this case, the compiler can't know what instance to use, because the argument to †With only these two instances, it is actually injective, but the compiler can't know somebody won't add more instances later on and thereby break the behaviour. |
| Injectivity Type Families are not necessarily injective. Therefore, we cannot infer the parameter from an application. For example, in |
Read Article
https://develop.horje.com/learn/1434/reference