How are functors in Haskell and OCaml similar? -


i've been toying around in haskell past year or , i'm starting 'get' it, until monads, lenses, type families, ... lot.

i'm leave comfort zone little , moving ocaml project day job. going through syntax little looking similar higher level concepts, example functor.

i read code in ocaml , structure of functor cannot seem whether similar concepts in haskell , ocaml or not. in nutshell, functor in haskell me way lift functions in haskell , use (and it) that. in ocaml gives me feeling it's closer programming interface (for example when making set or list, compare function) , wouldn't know how example lift functions on functor or so.

can explain me whether 2 concepts similar , if missing or not seeing? googled around bit , there doesn't seem clear answer found.

kasper

from practical standpoint, can think of "functors" in ocaml , haskell unrelated. said, in haskell functor type allows map function on it. in ocaml, functor module parametrized module.

in functional programming, functor? has description of functors in 2 languages , how differ.

however, name implies, there connection between 2 seemingly disparate concepts! both language's functors realizations of concept category theory.

category theory study of categories, arbitrary collections of objects "morphisms" between them. idea of category abstract, "objects" , "morphisms" can few restrictions—there has identity morphism every object , morphisms have compose.

the obvious example of category category of sets , functions: sets objects , functions between sets morphisms. clearly, every set has has identity function , functions can composed. similar category can formed looking @ functional programming language haskell or ocaml: concrete types (e.g. types kind *) objects , haskell/ocaml functions morphisms between them.

in category theory, functor transformation between categories. function between categories. when we're looking @ category of haskell types, functor type-level function: maps types else. particular kind of functor care maps types other types. perfect example of maybe: maybe maps int maybe int, string maybe string , on. provides mapping every possible haskell type.

functors have 1 additional requirement—they have map category's morphisms objects. in particular, if have morphism a → b , our functor maps a a' , b b', has map morphism a → b morphism a' → b'. concrete example, let's have types int , string. there whole bunch of haskell functions int → string. maybe proper functor, has have function maybe int → maybe string each of these.

happily, fmap function does—it maps functions. maybe, has type (a → b) → maybe → maybe b; can add parentheses get: (a → b) → (maybe → maybe b). type signature tells normal function have, have corresponding function on maybes.

so functor mapping between types preserves functions between them. fmap function proof of second restriction on functors. makes easy see how haskell functor class particular version of mathematical concept.

so ocaml? in ocaml, functor not type—it's module. in particular, it's parametrized module: module takes module argument. already, can see parallels: in haskell, functor type-level function; in ocaml, functor module-level function. really, it's same mathematical idea; however, instead of being used on types—like in haskell—it's used on modules.

there's far more detail how ocaml functors related category theory functors on cs site: what relation between functors in sml , category theory?. question talks sml rather ocaml per se, understanding module system of ocaml closely related of sml.

in summary: functors in haskell , in ocaml 2 fundamentally different structures both happen reifications of same abstract mathematical idea. think it's pretty neat :).


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -