KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am experimenting with the module language of OCaml (3.12.1), defining functors and signatures for modules and so on, mostly following the examples from Chapter 2 of the OCaml manual and I've stumbled, by accident, on a situation where apparently my mental model of how functors and module signatures work is flawed. I tried to narrow the situation I encountered to the shortest amount of code possible so don't ask what I am trying to accomplish, this is a totally contrived example to demonstrate the OCaml feature in question. So, we have a functor that simply provides an identity function f and is parametrized by a module supplying the type of that function's input parameter. Totally contrived example like I said. module type SOMETYPE = sig type t end ;; module Identity = functor (Type: SOMETYPE) -> struct let f (x: Type.t) = x end ;; Given the above, we proceed to define a module to supply the int type: module IntType = struct type t = int end ;; .. and then we use the functor to generate a module for the int identity function: module IdentityInt = Identity(IntType) ;; Sure enough the generated module and its f function behave as expected: #IdentityInt.f(3) + 10 ;; - : int = 13 The mental model of functors being functions that take modules as inputs and return modules seems to be serving us right so far. The Identity functor expects as input parameter a module of signature (module type) SOMETYPE , and indeed the module we supplied ( IntType ) has the correct signature and so a valid output module is produced ( IdentityInt ) whose f function behaves as expected. Now comes the un-intuitive part. What if we would like to m
Tags (comma-separated)
Save Edits
Cancel