-
-
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
Conversation
I think it's known as "natural transformation", not "natural transformer" :) |
@wedens thanks for pointing out. Corrected. |
|
||
## 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. |
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.
while preserving the internal structures
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)
|
||
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. | ||
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Pedantry warning (sorry): it should be sometimes
, not sometime
. :)
|
||
This is why a parametric polymorphic function `FunctionK[F, G]` is sometime referred as a Natural Transformation. | ||
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 comment
The 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
|
||
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: | ||
```Scala | ||
(fa:F[A]) => fK(F.map(fa)(f)) |
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.
Minor inconsistency: fK
and fk
.
@ceedubs I am interested in hearing your thoughts on this, if you have time. |
|
||
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: | ||
```Scala | ||
(fa:F[A]) => fK(F.map(fa)(f)) |
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.
This has fK
where is is defined above as fk
|
||
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. | ||
|
||
This is why a parametric polymorphic function `FunctionK[F, G]` is sometimes referred as a Natural Transformation. However, they are two different concepts. |
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.
Minor: is there a particular reason that you capitalized "Natural Transformation" here?
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.
Probably don't need to capitalize
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.
This line could use some elaboration.. maybe say something about how FunctionK
is more powerful than natural transformations, or natural transformations can be implemented in terms of FunctionK
(I prefer the latter)
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.
We had a discussion about this on gitter, and @edmundnoble pointed out that natural transformation could be deemed as more powerful in some ways. My intuition is that FunctionK
is more powerful in one category but natural transformation is a concept that could involve two categories. Maybe when we restrict natural transformation to endofunctors we can say something about it. In conclusion we feel it's too complex a thing to include here.
Thanks @kailuowang. I really like linking this back to the category theory (and probably also providing some insight to people who are used to the |
@adelbertc as someone who has written a lot of docs and read up on your category theory, does this look good to you? |
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.
Minor comments, but otherwise LGTM
|
||
## Natural Transformation | ||
|
||
In category theory, a [Natural Transformation](https://en.wikipedia.org/wiki/Natural_transformation) provides a morphism between Functors while preserving the internal structure. It's one of the most fundamental notions of category theory. |
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.
ncatlab might be a better reference than Wikipedia for such a subject :-) https://ncatlab.org/nlab/show/natural+transformation
|
||
In category theory, a [Natural Transformation](https://en.wikipedia.org/wiki/Natural_transformation) provides a morphism between Functors while preserving the internal structure. 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 comment
The 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 Functor
s F
and G
, FunctionK[F, G]
is a natural transformation via parametricity. That is, given fk: FunctionK[F, G]
, for all functions A => B
the following are equivalent:
(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 comment
The reason will be displayed to describe this comment to others. Learn more.
by parametricity
|
||
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. | ||
|
||
This is why a parametric polymorphic function `FunctionK[F, G]` is sometimes referred as a Natural Transformation. However, they are two different concepts. |
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.
Probably don't need to capitalize
|
||
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. | ||
|
||
This is why a parametric polymorphic function `FunctionK[F, G]` is sometimes referred as a Natural Transformation. However, they are two different concepts. |
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.
This line could use some elaboration.. maybe say something about how FunctionK
is more powerful than natural transformations, or natural transformations can be implemented in terms of FunctionK
(I prefer the latter)
@ceedubs you flatter me :-) |
I think this will be helpful because it's often referred as Natural
TransformerTransformation in other context.