1

Reading the comments to this blog post made me realize I don't know much about some really interesting functional mechanisms between languages like Haskell, OCaml and Standard ML. I'd love a high-level overview not of syntax but of the general concept of what these 3 things are.

  • Did you read the research paper that blog post links to? – Robert Harvey Aug 1 '16 at 19:09
  • @RobertHarvey the paper makes no mention of generative or applicative modules – johnbakers Aug 1 '16 at 19:49
  • Are you sure you're using the right terms? "Applicative Module" doesn't even Google, though "Applicative Functor" does. The term "Generative Modules" seems to be associated with electronic art and music. The blog post you linked uses the terms "applicative" and "generative," but not in a module context. – Robert Harvey Aug 1 '16 at 20:22
6

Since both are in OCaml, We can explore the difference between generative and applicative functors easily:

module type S = sig type t end
module M = struct type t = int end

(* An Applicative functor. *)
module F (M : S) = struct
  type t = Foo of M.t list
end

module F1 = F(M)
module F2 = F(M)

(* M1 = M2 => F(M1).t ≡ F(M2).t
   This works.
*)
let x : F1.t = F2.Foo [3]


(* A Generative functor. Note the () argument *)
module F (M : S) () = struct
  type t = Foo of M.t list
end

module F1 = F(M)()
module F2 = F(M)()

(* This doesn't work anymore ! F1.t ≠ F2.t *)
let x : F1.t = F2.Foo [3]

Applicative functors are the default in OCaml, they are more appropriate when a functor application is pure (which is, arguably, the most common case). Generative functors are available since 4.02.

Generative functors are the default in SML, they are more appropriate when a functor is impure (or when doing type shenanigans). AFAIK, applicative functors are not available in any SML dialects.

In practice in OCaml, generative functors are used in general in combination with first class modules to have functions that generates a fresh type at each application. This has various use cases (heterogeneous maps, emulating singletons types, ...). In particular, this sentence from the manual is of importance: "As a side-effect of this generativity, one is allowed to unpack first-class modules in the body of generative functors."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.