# Resources for Advanced Module Usage

**URL:** <https://discuss.ocaml.org/t/resources-for-advanced-module-usage/1774>\
**Category:** Learning\
**Created:** [March 26, 2018, 8:08am UTC](https://discuss.ocaml.org/t/resources-for-advanced-module-usage/1774 "2018-03-26T08:08:32Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![kantian](https://avatars.discourse-cdn.com/v4/letter/k/4bbf92/32.png) [@kantian](https://discuss.ocaml.org/u/kantian)\
**Post date:** [March 26, 2018, 9:35am UTC](https://discuss.ocaml.org/t/resources-for-advanced-module-usage/1774/2 "2018-03-26T09:35:29Z")

</div>

You can have a look on the paper [ML module mania](https://www.cs.tufts.edu/~nr/pubs/maniaws-abstract.html) which present a type safe, separetly compiled and extensible interpreter for the Lua language. In the same spirit, you have the work of Oleg Kiselyov on [tagless-final embedded DSL](http://okmij.org/ftp/tagless-final/course/optimizations.html). Or from the same author than the paper on ligthweight higher-kinded types, you have their proposal for [modular implicit](https://www.cl.cam.ac.uk/~jdy22/papers/modular-implicits.pdf) which go further.

About the last paper, imagine that you want to write a function `show` that works on mutliple data types but not uniformly. In a lot of languages, this is done with dynamic dispatching. Since OCaml doesn’t have runtime type representation, we have to rely on something which is closed to a static dispatching, and we can do this with _first-class_ modules.

```ocaml
module type Showable = sig
  type t
  val show : t -> string
end

let show (type a) (module M : Showable with type t = a) x = M.show x

```

Now you can define a module for each type that you want to convert to a `string`, and you have to _explicitly_ pass it to your fonction `show`.

```ocaml
module Int_show = struct
  type t = int
  let show = string_of_int
end

module Float_show = struct
  type t = float
  let show = string_of_float
end

# show (module Int_show) 3;;
- : string = "3"

# show (module Float_show) 3.4;;
- : string = "3.4"

```

The idea behind modular implicit is that in this situation, the module to use to show your value is _automatically infered_ by the compiler in such a way that you don’t have to _explicitly_ pass it to your function. This will be very useful, especially when the module in question is the result of a functor application. Indeed, imagine that you know how to show values of two types `a` and `b`, then you know how to show a pair of type `a * b`. To encode this you have to write a _functor_:

```ocaml
module Pair_show (A : Showable) (B : Showable):
Showable with type t = A.t * B.t = struct
  type t = A.t * B.t
  let show (x,y) = Printf.sprintf "(%s, %s)" (A.show x) (B.show y)
end

# show (module Pair_show (Int_show) (Float_show)) (1, 2.5);;                                                              
- : string = "(1, 2.5)"

```

---

_[View the full topic](https://discuss.ocaml.org/t/resources-for-advanced-module-usage/1774)._
