4 ms·
> The big reason to not use F# vs OCaml tends to come down to the lack of functors... What do you mean by that? F# has support for functors, or are you referri
by Nuzzerino 6y ago
> The big reason to not use F# vs OCaml tends to come down to the lack of functors...
What do you mean by that? F# has support for functors, or are you referring to something else?
https://fsprojects.github.io/FSharpPlus/applicative-functors.html https://fsprojects.github.io/FSharpPlus/applicative-functors...
- darksaints 6y agoThose are an implementation of functors as they exist in Haskell. The functors I'm talking about are actually a language level construct that is a primary abstraction mechanism in OCaml/SML. They are essentially higher-level modules: modules that are parameterized by other modules. https://stackoverflow.com/a/16353711 https://stackoverflow.com/a/16353711
- pmoriarty 6y agoCan someone explain to me like I'm 5 what the big deal about functors is?
- darksaints 6y agoProbably not, as it is a very abstract generalization for what is essentially a simple concept that for most purposes doesn't need a math-y abstraction. And it would depend on whether you're talking about Haskell, or OCaml, or Category theory. The best I could describe the general concept is that a functor allows you to take something of some type and maps it to another type via some unspecified function.
- yawaramin 6y agoIf you mean ML (i.e. OCaml) functors, then to put it simply: they are the primary code abstraction mechanism in ML. ML doesn't have inheritance and runtime dispatch, instead it uses functors which do static dispatch. Simple example: in an OOP language you might have a Map<K, V> class for any type K, V where K must be an instance of an Ordered interface so that the map implementation can do efficient lookup. In OCaml, you have a Map.Make(Ord : Ordered) functor which explicitly takes a module Ord as its parameter. The Ord module implements the key data type, which must support the Ordered interface. When you (statically) apply the functor, you get back a new module which specializes a map data type to work for the specific key type. E.g., module StringMap = Map.Make(String) The String module implements the Ordered interface, so it can be used as the argument here. Now you can create and manipulate maps of strings to any values. You might be wondering, what's the payoff? It's very similar to the payoff for interfaces--code abstraction. It's just that it's all statically resolved and highly efficient.
- siwatanejo 6y agoSounds like this is covered by F# already, simply by the use of genercs (similar to what you described for OOP, which in that particular case doesn't really have any OOP downsides, i.e.: no inheritance, no state).
- ernst_klim 6y ago>simply by the use of genercs It depends on what you mean by "generics". Functors are generics, but very powerful ones, akin to packages in Ada. They are way more generic than what parametric polymorphism allows you to do in F#, they support Higher Kinded Types, module can have many type variables within it etc etc.
- Paradigma11 6y agoIn F# you have List<'a> but you cant have a single map operation abstracting over Lists, Arrays, Options..... so they have to be implemented separately Option.map, Array.map.....
- yawaramin 6y agoNot just with generics, but also with dynamic dispatch on the runtime class of the K type which implements Ordered interface. ML traditionally doesn't have dynamic dispatch, so it uses the static mechanism of building a map type specialized to a given key type. The value type is completely generic, though. I said 'traditionally' above because OCaml actually has dynamic dispatch now; it has a powerful and feature-complete OOP implementation, and a way to pass around modules at runtime as first-class objects. And in fact people take advantage of these capabilities to build more 'modern' APIs. But I would argue that the functor approach is one of, if not the best, overall.
- nukifw 6y agoHe's talking about functor from OCaml and SML which are basically a function in the module level. So a module which take other modules as arguments and produce a new module.
- tikhonj 6y agoIn OCaml, "functors" are parametrized modules—mostly unrelated to functors and applicatives in the Haskell sense. Think about it as a module that can take another module as an input.
- mc10 6y agoOCaml functors are not the same as Haskell/F# functors/applicatives (which are essentially one and the same in OCaml if you use named arguments). Instead, they're roughly "functions from modules to modules": https://dev.realworldocaml.org/functors.html https://dev.realworldocaml.org/functors.html. Functors allow you to replicate much of what classes/objects in an OO language offer, but with static instead of dynamic dispatch.
- ojnabieoot 6y agoI am an F# person rather than OCaml and am not familiar with the compiler, but this seems to be overstating the difference: I thought the OCaml functors are “real” mathematical functors that implemented a structured type polymorphism over the “input type” - they look pretty similar to C++ templates superficially but they work similarly to Haskell type classes. They are “functions from modules to modules” but those functions take types as values. Real experts should let me know if I am mistaken: I don’t think OCaml and Haskell have strictly the same type system, but they are both similarly more expressive than F# because of this specific type -> type polymorphism that in particular lets you do type-checked monads, etc.
- didibus 6y agoNeither Haskell's nor OCaml's functors are the same as the mathematical one I believe, but both are similar in some way that the word makes sense to be used for their respective feature. In OCaml, functors map between an abstract module and some concrete one. And in Haskell functors are structures that can have a function that maps over their elements. In math a functor is a map between categories. So you can see how all three kinda make sense as functors, but also are different in exactly what they are.
- tome 6y agoBoth Haskell's and OCaml's functors are special cases of the general mathematical one.
- 6y ago