Skip to content
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

Merged
merged 7 commits into from
May 17, 2017

Conversation

kailuowang
Copy link
Contributor

@kailuowang kailuowang commented Apr 25, 2017

I think this will be helpful because it's often referred as Natural Transformer Transformation in other context.

@wedens
Copy link
Contributor

wedens commented Apr 25, 2017

I think it's known as "natural transformation", not "natural transformer" :)

@kailuowang
Copy link
Contributor Author

@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.
Copy link
Contributor

@SystemFw SystemFw Apr 25, 2017

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.
Copy link
Contributor

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.

Copy link
Contributor

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

@codecov-io
Copy link

codecov-io commented Apr 25, 2017

Codecov Report

Merging #1636 into master will decrease coverage by 5.13%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1636      +/-   ##
==========================================
- Coverage   93.43%   88.29%   -5.14%     
==========================================
  Files         241       48     -193     
  Lines        4068      940    -3128     
  Branches      140       86      -54     
==========================================
- Hits         3801      830    -2971     
+ Misses        267      110     -157
Impacted Files Coverage Δ
.../main/scala/cats/laws/discipline/StrongTests.scala
core/src/main/scala/cats/data/EitherK.scala
core/src/main/scala/cats/instances/vector.scala
laws/src/main/scala/cats/laws/CartesianLaws.scala
...scala/cats/laws/discipline/SerializableTests.scala
core/src/main/scala/cats/instances/function.scala
...re/src/main/scala/cats/functor/Contravariant.scala
.../main/scala/cats/laws/discipline/ChoiceTests.scala
...c/main/scala/cats/laws/discipline/ArrowTests.scala
core/src/main/scala/cats/Cartesian.scala
... and 183 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5ffce8e...5a3e63d. Read the comment docs.


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))

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.

@kailuowang
Copy link
Contributor Author

kailuowang commented May 12, 2017

@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))

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.
Copy link
Contributor

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?

Copy link
Contributor

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

Copy link
Contributor

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)

Copy link
Contributor Author

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.

@ceedubs
Copy link
Contributor

ceedubs commented May 13, 2017

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 NaturalTransformation name in scalaz). I also like that it's added as a separate section at the end so people can get into the theory if they want to but can stop short of it if they prefer. 👍

@ceedubs
Copy link
Contributor

ceedubs commented May 13, 2017

@adelbertc as someone who has written a lot of docs and read up on your category theory, does this look good to you?

Copy link
Contributor

@adelbertc adelbertc left a 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.
Copy link
Contributor

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:
Copy link
Contributor

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 Functors 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.
Copy link
Contributor

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.
Copy link
Contributor

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.
Copy link
Contributor

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)

@adelbertc
Copy link
Contributor

@ceedubs you flatter me :-)

@ceedubs ceedubs merged commit 2cb7fcb into typelevel:master May 17, 2017
@kailuowang kailuowang modified the milestone: 1.0.0-MF May 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants