4 ms·
May be worth to mention a 'Functor' in OCaml is a different kind of beast than the 'Functor' explained in this article (which is ~ the Haskell Functor type-clas
by nicolast 9y ago
May be worth to mention a 'Functor' in OCaml is a different kind of beast than the 'Functor' explained in this article (which is ~ the Haskell Functor type-class).
- eru 9y agoAnd perhaps add something about (OCaml) modules in that case? The whole list seems to be mostly Haskell-inspired terminology translated to JavaScript(?), so OCaml-style modules and thus functors might make less sense to describe thoroughly. (Even though the OCaml people have a better claim to be using Functor in something resembling its mathematical meaning.)
- GreaterFool 9y ago> Even though the OCaml people have a better claim to be using Functor in something resembling its mathematical meaning. I think that is not accurate. Haskell's notion of a functor is basically taken from category theory. Same with applicative functor. It is easy to draw commutative diagrams for things like `Maybe` or `Either a` to prove it! In contrast in OCaml functors are module functions. Do modules form categories? Maybe? I haven't seen anyone talking about category of OCaml modules. Perhaps in some papers. But even if they do I wouldn't call OCaml's claim better. When one says Functor in Haskell it is generally understood as "the category theory thing". When one says functor in OCaml I think most people, myself included, would think "module function", whether it satisfies category-theory requirements or not. Also, worth pointing out that OCaml too has applicative functors. Those are such functors that when applied to same modules produce same types. Whether that fits with the category-theoretic notion of applicative functor... haven't thought about that! Maybe? Basically module M1 = Make(F)(G) module M2 = Make(F)(G) then if `M1.t` is the same as `M2.t` for some `t` the we have an applicative functor. The other side of the coin are generative functors: each time you apply them they mint new types: module M1 = Make(F)(G)() module M2 = Make(F)(G)() Then `M1.t` /= `M2.t`. This has been introduced in OCaml `4.02`
- eru 9y agoThanks! The main point against Haskell functors being 'proper' is that they only work in the category of 'Hask'.
- LeanderK 9y agowell, there is always: https://github.com/mikeizbicki/subhask https://github.com/mikeizbicki/subhask which provides more flexibility on the categories you are programming in.
- alphaalpha101 9y ago>the category of Hask Which category would that be?
- shakna 9y ago> In contrast in OCaml functors are module functions. Do modules form categories? Wrong kind of functor, I believe. Unfortunately, functor actually refers to a whole heap of different terms in various mathematical fields. Ocaml's functor seems to come from Quine's predicate functor logic, or a related field like combinatory logic.
- bitwize 9y agoBasically anything not straightforwardly isomorphic to category Hask is considered to be not quite functional programming, so Haskell terminology will dominate.
- alphaalpha101 9y agoBy Haskellers. Those people are incorrect in this.
- eru 9y agoYou get a free pass if you do dependent types.
- k__ 9y agoIs there a reason for this? The mathematical concept seems to be like kind of a wrapper
- gmfawcett 9y agoAs shakna wrote in another comment, the term 'functor' has different meanings across fields of mathematics. The meanings in Haskell and Ocaml originate from different definitions.
- rbobrowicz 9y agoI don't think they originate from different definitions. Concepts in category theory are very abstract on purpose, because it focuses on the main essence of the idea, divorced from any particular application of it. Which will greatly vary, but have a common set of properties. In Haskell functors are endofunctors in the category Hask. Endofunctors are a special case of functors where the source and result category are the same, and Hask is a category of Haskell types. So essentially Haskell functors are all Hask -> Hask transformations. For example applying "fmap show" to a Maybe Int produces a Maybe String, which are both in Hask. In OCaml you can treat modules of the same signature as categories. If you look at [1] and look at the example for intervals, you have one category of Comparable modules and another category of Interval modules. The Make_interval functor maps objects from Comparable to Interval. So it really is the same concept, just different applications, but they follow the same theoretical framework and properties. 1: https://realworldocaml.org/v1/en/html/functors.html https://realworldocaml.org/v1/en/html/functors.html
- bellp1234 9y agohttps://reasonml.github.io/ https://reasonml.github.io/