hemanth / functional-programming-jargon

Jargon from the functional programming world in simple terms!
http://git.io/fp-jargons
MIT License
18.58k stars 1.02k forks source link

Improve mathematical correctness #148

Closed dmitriz closed 3 years ago

dmitriz commented 7 years ago

Attempting to give more mathematically correct explanations.

jethrolarson commented 7 years ago

For those that want mathematically correct definitions there is no shortage of other sources. I think this guide aims to be approachable as it's first goal. We can link to other strict definitions though

dmitriz commented 7 years ago

The problem with the current definition that it is both incorrect and misleading in quite a number of points for such a short sentence. ;)

  1. An object is not functor, just like function value is not function. For instance, the array [1,2] is not functor. The Array functor is the map sending type a into type [a]. You can compose functors but how are you going to compose individual arrays?

  2. Calling functor an "object implementing a map method" is like calling "flight" a passenger who can move. With that "definition", the given composition law right below does not even make any sense - how is the second .map in object.map(g).map(f) is supposed to be understood?

  3. "... map function which, while running over each value in the object to produce a new object..." -- really? Do you really need each value? Then what about the prop map? Do only object's values matter? What about keys? Own property values or prototypal? What about functors like Maybe, Either? Where is the running over each value part?

The biggest problem with such definitions is not only the reader can get confused and give up on or the opposite -- would think she now knows it all, but the consequences of that. In the first case, the reader will go complaining how these FP geeks make everything hard to understand that "I don't need anyway". And in the second case, she will go writing posts and blogs incorrectly applying and explaining the concepts, making them more confusing for everyone else, to get the readers doing the same ad infinitum. At the end, the whole community is at loss, and this is how people get to hate and avoid maths, after their teachers made it painfully confusing to them, because they didn't properly understand it themselves, but of course would never admit that ...

Having said that, I am happy to update the correction according to the maintainer's desires ;)

For those that want mathematically correct definitions there is no shortage of other sources. I think this guide aims to be approachable as it's first goal. We can link to other strict definitions though

Cross out "mathematically" here. Does anyone who really wants to learn what functor is, want an incorrect definition here? Of course, she can google it, but then why is she here?

This strict definition expect reader to have some previous knowledge. Link to morphisms is needed. The function notation is confusing for me, maybe it because function notation is different from that are used in document.

I can replace morphisms with functions. A wikipedia link is easy to give though. Also I'd honestly like to understand how the function notation is confusing, given that function is even a reserved word in JS ;)

SeanSilke commented 7 years ago

@dmitriz First of all I truly appreciate what your are doing. Second I have no previous knowledge of functors. Your definition is compact and dense, and this is can be difficult comprehension for someone with no previous knowledge.

Here is some things that gave me trouble:

An object that implements a map function which, while running over each value in the object to produce a new object

An map between categories sending objects to objects and morphisms to morphisms.

  1. In current definition functor is some method that apply function to each(I see you mention that this each part is not correct) element of object and generate the new object with result of function application.Somehow the part (that we have some set and casting it to another) was lost for me until I read the category definition.

  2. Also the map is introduced here for the first time and it is not clear what is it. Is it the same as Array.prototype.map or some different beast.

The solution can be link to the category. And adding definition of map.

dmitriz commented 7 years ago

@SeanSilke Thanks you for the appreciation 😄

In current definition functor is some method that apply function to each(I see you mention that this each part is not correct) element of object and generate the new object with result of function application.Somehow the part (that we have some set and casting it to another) was lost for me until I read the category definition.

That is exactly the problem, the current definition makes it even more confusing than it is. The whole attempt to look inside the object for its elements, values, keys etc, is misleading, to say the least. It is precisely the opposite -- the object must be considered as single entity, with all details hidden away and unimportant, just like in OOP.

Even calling it "applying a function" can be confusing, because applying f to x means evaluating f(x), which is not what the map method does. A better word is "lifting". When you apply f to every element in array, you don't really apply it to the array, you lift it in some sense to transform the arrays.

The "set" in question is the type, which is basically the set of values. The functor consists of two parts -- lifting the types, and the functions between them.

Also the map is introduced here for the first time and it is not clear what is it. Is it the same as Array.prototype.map or some different beast.

Perhaps it is easier to avoid some formalism and simply say that functor f assigns to every type a the new "lifted" type f a, and to every function u between types a and b its "lift" called map u between f a and f b?

That would be easier to comprehend for people interested in practical FP aspects.

BTW, here is some longer explanation I've written on SO:

http://stackoverflow.com/questions/2030863/in-functional-programming-what-is-a-functor/41368880#41368880

SeanSilke commented 7 years ago

The "set" in question is the type, which is basically the set of values. The functor consists of two parts -- lifting the types, and the functions between them.

The set definition is certainly add clarity. It seems that I confused type with data types. I is correct that type is any set of values? to understand new definition.

I also find confusing notation of functor application. f a is new set and it can be easily confused with f(a). Is the any better notation?

This part is rather confusing. Maybe diagram or step by step algorithm can clarify it?

and to every function u between types a and b its "lift" called 'map u' between f a and f b

dmitriz commented 7 years ago

The set definition is certainly add clarity. It seems that I confused type with data types. I is correct that type is any set of values? to understand new definition.

Good point, "type" can be ambiguous, so "data type" is preferred. Yes, a type is simply a set of values, that is the easiest way to think of it.

I also find confusing notation of functor application. f a is new set and it can be easily confused with f(a). Is the any better notation?

It is on purpose, to distinguish it from function applications. The notation f a is influenced by Haskell, where it is the way to apply function to value, and is now being used everywhere, so it is important to know. It is quite established too for functors in programming circles, so better not to change.

Actually in maths, you write F(A) instead! Notice the capitals! Capitals make it very easy to separate functor F from function f, then you don't need to change the function application notation! Sadly the folks in Haskell does not seem to like capitals for functors, which leads to the "criminal" activity of using f for both functor and functions in the same formulas! Ironically, in Haskell, also applying ordinary function to a value is still written as f x (no pesky braces, lol), so the benefit of notational separation is gone in favour of confusion.

Yes, f a is the new set aka data type. It is the new type type returned by f when applying to a, so the meaning is the same as f(a) but we want to reserve the latter notation for functions acting on values (aka morphisms in category theory), and we want to make clear separation between functor and function actions in the same diagram.

This part is rather confusing. Maybe diagram or step by step algorithm can clarify it?

and to every function u between types a and b its "lift" called 'map u' between f a and f b

Which part is confusing?

Here are some nice diagrams: https://ncatlab.org/nlab/show/functor

SeanSilke commented 7 years ago

@dmitriz Please pardon me for such long delay. I found that your conversation is motivated by my lack of knowledge and raw interest, and not leading to improvement of this repo. Thank you for this answers. What entry level materials you would recommend for study FP concepts?

jethrolarson commented 7 years ago

Yeah. We can bring in a local definition of those rules if it can be described in brief. Otherwise we could just omit that sentence and leave the research as a reader exercise. I think the rules are not necessary to get a basic understanding of most of these concepts and examples give more bang for the buck.

On Sat, Aug 5, 2017 at 6:15 AM stereobooster notifications@github.com wrote:

@stereobooster commented on this pull request.

In readme.md https://github.com/hemanth/functional-programming-jargon/pull/148#discussion_r131521827 :

@@ -435,6 +435,7 @@ const g = x => x * 2

Pointed Functor

An object with an of function that puts any single value into it. +The of function must satisfy the so-called laws of natural tranformations

do not like wikipedia as a reference. Wikipedia is good, when there is nothing else or when you looking for some keywords. But definitions and explanations are poor

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/hemanth/functional-programming-jargon/pull/148#pullrequestreview-54515175, or mute the thread https://github.com/notifications/unsubscribe-auth/AAB-4G6i0IzfGK68zTERLTQ29yVVh11pks5sVGrbgaJpZM4NCfKc .

jethrolarson commented 3 years ago

Closing as stale. Reopen if you want to be reconsidered