-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added some category theory into FunctionK document #1636
Changes from 2 commits
f48450d
64ac09c
1a4a36c
c825740
78ded9d
d579487
5a3e63d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,3 +121,25 @@ type ErrorOr[A] = Either[String, A] | |
val errorOrFirst: FunctionK[List, ErrorOr] = | ||
λ[FunctionK[List, ErrorOr]](_.headOption.toRight("ERROR: the list was empty!")) | ||
``` | ||
|
||
## Natural Transformation | ||
|
||
In category theory, a [Natural Transformation](https://en.wikipedia.org/wiki/Natural_transformation) provides a morphism between Functors while preserving the internal structures. It's one of the most fundamental notions of category theory. | ||
|
||
If we have two `Functor`s: `F` and `G`, then, being parametric polymorphic, `FunctionK[F, G]` is automatically a Natural Transformation between them. That is, if we have a `fk: F ~> G`, then for any combination of `A`, `B` and function `f: A => B`, the following two functions are equivalent: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps drop the colon, and use 'parametricity', e.g. If we have two |
||
```Scala | ||
(fa:F[A]) => fK(F.map(fa)(f)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor inconsistency: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has |
||
``` | ||
and | ||
```Scala | ||
(fa: F[A]) => G.map(fK(fa))(f) | ||
```` | ||
|
||
We don't need to write a law to test the implementation of the `fk` for the above to be true. It's automatically given by its parametric polymorphism. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. by parametricity |
||
|
||
This is why a parametric polymorphic function `FunctionK[F, G]` is sometime referred as a Natural Transformation. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pedantry warning (sorry): it should be |
||
However, they are two different concepts. `FunctionK` is a stronger and stricter construction than Natural Transformation. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not entirely sure this is worth including unless we can give an explanation as to how do they differ |
||
For more details, Bartosz Milewski has written a great blog post titled | ||
["Parametricity: Money for Nothing and Theorems for Free"](https://bartoszmilewski.com/2014/09/22/parametricity-money-for-nothing-and-theorems-for-free/). | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments as requested on Gitter: I think the internal structure is slightly better. It refers to the composition of morphisms (similarly to what a functor does)