diff --git a/docs/typeclasses.md b/docs/typeclasses.md index 6e7cf551a1..24146eda3d 100644 --- a/docs/typeclasses.md +++ b/docs/typeclasses.md @@ -1,4 +1,8 @@ # Type Classes + + + + Type classes are a powerful tool used in functional programming to enable ad-hoc polymorphism, more commonly known as overloading. Where many object-oriented languages leverage subtyping for polymorphic code, functional programming tends towards a combination of parametric polymorphism (think type parameters, like Java generics) @@ -227,8 +231,262 @@ You can find out more about law testing [here](typeclasses/lawtesting.md). ## Type classes in Cats -infographic -From [cats-infographic by @tpolecat](https://github.com/tpolecat/cats-infographic). +Dotted line means that typeclass does not inherit other typeclass, but uses it. +The highlighted type classes are the most important to learn in particular library. + +```mermaid +graph BT; + classDef kernel fill:#FF99FF,stroke-width:2px,stroke:#000; + classDef kernelImportant fill:#FF00FF,stroke-width:2px,stroke:#000; + + subgraph kernel + direction BT + id1(Eq):::kernelImportant; + id2(PartialOrder):::kernel; + id3(Hash):::kernel; + id4(Order):::kernelImportant; + + id2-->id1; + id3-->id1; + id4-->id2; + end + + subgraph kernel + direction BT + id8(Semigroup):::kernelImportant; + id9(Band):::kernel; + id10(CommutativeSemigroup):::kernel; + id11(Monoid):::kernelImportant; + id12(Semilattice):::kernel; + id13(CommutativeMonoid):::kernel; + id14(Group):::kernel; + id15(BoundedSemilattice):::kernel; + id16(CommutativeGroup):::kernel; + + id9-->id8; + id10-->id8; + id11-->id8; + id12-->id9; + id12-->id10; + id13-->id10; + id13-->id11; + id14-->id11; + id15-->id12; + id15-->id13; + id16-->id13; + id16-->id14; + end +``` + +```mermaid +graph BT; + classDef core fill:#BBBBFF,stroke-width:2px,stroke:#000; + + subgraph core + direction BT + id5(Bifoldable):::core; + id6(Bifunctor):::core; + id7(Bitraverse):::core; + id79(NotNull):::core; + id80(Show):::core; + id81(Inject):::core; + id82(InjectK):::core; + id83(Parallel):::core; + id84(NonEmptyParallel):::core; + + id7-->id5; + id7-->id6; + id83-->id84; + end +``` + +```mermaid +graph BT; + + classDef core fill:#BBBBFF,stroke-width:2px,stroke:#000; + classDef coreImportant fill:#6666FF,stroke-width:2px,stroke:#000; + + subgraph core + direction BT + id25(UnorderedFoldable):::core; + id26(UnorderedTraverse):::core; + id27(Invariant):::core; + id28(Foldable):::coreImportant; + id29(Functor):::coreImportant; + id30(Semigroupal):::core; + id31(Contravariant):::core; + id32(Reducible):::core; + id33(Traverse):::coreImportant; + id34(InvariantSemigroupal):::core; + id35(ContravariantSemigroupal):::core; + id36(NonEmptyTraverse):::core; + id37(Distributive):::core; + id38(Apply):::core; + id39(InvariantMonoidal):::core; + id40(CoflatMap):::core; + id41(FlatMap):::core; + id42(CommutativeApply):::core; + id43(Applicative):::coreImportant; + id44(ContravariantMonoidal):::core; + id45(Comonad):::core; + id46(CommutativeFlatMap):::core; + id47(Monad):::coreImportant; + id48(CommutativeApplicative):::core; + id49(ApplicativeError):::core; + id50(Bimonad):::core; + id51(CommutativeMonad):::core; + id52(MonadError):::core; + id53(SemigroupK):::core; + id56(NonEmptyAlternative):::core; + id54(MonoidK):::core; + id55(Alternative):::coreImportant; + + id26-->id25; + id28-->id25; + id29-->id27; + id31-->id27; + id32-->id28; + id33-->id26; + id33-->id28; + id33-->id29; + id34-->id27; + id34-->id30; + id35-->id31; + id35-->id34; + id36-->id32; + id36-->id33; + id38-->id29; + id38-->id34; + id39-->id34; + id40-->id29; + id37-->id29; + id41-->id38; + id42-->id38; + id43-->id38; + id43-->id39; + id44-->id35; + id44-->id39; + id45-->id40; + id46-->id41; + id46-->id42; + id47-->id41; + id47-->id43; + id48-->id42; + id48-->id43; + id49-->id43; + id50-->id45; + id50-->id47; + id51-->id46; + id51-->id47; + id51-->id48; + id52-->id47; + id52-->id49; + id56-->id43; + id56-->id53; + id55-->id43; + id55-->id54; + id55-->id56; + id54-->id53; + end +``` + +```mermaid +graph BT; + classDef core fill:#BBBBFF,stroke-width:2px,stroke:#000; + classDef effect fill:#FFFF00,stroke-width:2px,stroke:#000; + + subgraph effect + direction BT + id52(MonadError):::core; + id57(Unique):::effect; + id58(MonadCancel):::effect; + id59(GenSpawn):::effect; + id60(Clock):::effect; + id61(GenConcurrent):::effect; + id62(Defer):::effect; + id63(GenTemporal):::effect; + id64(Sync):::effect; + id65(Async):::effect; + + id58-->id52; + id59-->id57; + id59-->id58; + id61-->id59; + id63-->id60; + id63-->id61; + id64-->id57; + id64-->id58; + id64-->id60; + id64-->id62; + id65-->id63; + id65-->id64; + end +``` + +```mermaid +graph BT; + classDef arrow fill:#FFFF99,stroke-width:2px,stroke:#000; + + subgraph arrow + direction BT + id17(Compose):::arrow; + id18(Profunctor):::arrow; + id19(Category):::arrow; + id20(Strong):::arrow; + id21(Choice):::arrow; + id22(Arrow):::arrow; + id23(ArrowChoice):::arrow; + id24(CommutaticeArrow):::arrow; + + id19-->id17; + id20-->id18; + id21-->id19; + id22-->id19; + id22-->id20; + id23-->id21; + id23-->id22; + id24-->id22; + end +``` + +```mermaid +graph BT; + + classDef coreInMtl fill:#AAAA88,stroke-width:2px,stroke:#000; + classDef mtl fill:#FFBBBB,stroke-width:2px,stroke:#000; + + subgraph mtl + direction BT + id66(Applicative):::coreInMtl; + id67(Functor):::coreInMtl; + id68(Ask):::mtl; + id69(Raise):::mtl; + id70(Tell):::mtl; + id71(Local):::mtl; + id72(Handle):::mtl; + id73(Listen):::mtl; + id74(Monoid):::coreInMtl; + id75(Monad):::coreInMtl; + id76(Censor):::mtl; + id77(Stateful):::mtl; + id78(Chronicle):::mtl; + + id68-.->id66; + id69-.->id67; + id70-.->id67; + id71--->id68; + id72-.->id66; + id72--->id69; + id73--->id70; + id76--->id73; + id76-.->id74; + id77-.->id75; + id78-.->id75; + id76-.->id66; + end +``` + +(The typeclass graph based on @tpolecat's repository `cats-infographic`). ## Incomplete type class instances in cats