Skip to content

Commit f3e4ddb

Browse files
justinwoohdgarrood
authored andcommitted
fill in Functional Dependencies (documentationjs#212)
1 parent 57e9084 commit f3e4ddb

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

language/Type-Classes.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,23 @@ For multi-parameter type classes, the orphan instance check requires that the in
110110

111111
## Functional Dependencies
112112

113-
TODO. For now, see the section in [PureScript by Example](https://leanpub.com/purescript/read#leanpub-auto-functional-dependencies).
113+
Instances for type classes with multiple parameters generally only need a subset of the parameters to be concrete to match instances. Declarations on which parameters can determine others in instance heads are called Functional Dependencies. For example:
114+
115+
```purescript
116+
class TypeEquals a b | a -> b, b -> a where
117+
to :: a -> b
118+
from :: b -> a
119+
120+
instance refl :: TypeEquals a a where
121+
to a = a
122+
from a = a
123+
```
124+
125+
The `|` symbol marks the beginning of functional dependencies, which are separated by a comma if there are more than one. In this case, the first parameter determines the type of the second, and the second determines the type of the first.
126+
127+
Functional dependencies are especially useful with the various `Prim` typeclasses, such as `Prim.Row.Cons`: https://pursuit.purescript.org/builtins/docs/Prim.Row#t:Cons
128+
129+
See also the section in [PureScript by Example](https://leanpub.com/purescript/read#leanpub-auto-functional-dependencies).
114130

115131
## Type Class Deriving
116132

0 commit comments

Comments
 (0)