# 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:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![robkuz](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/robkuz/32/842_2.png) [@robkuz](https://discuss.ocaml.org/u/robkuz)\
**Post date:** [March 26, 2018, 8:08am UTC](https://discuss.ocaml.org/t/resources-for-advanced-module-usage/1774/1 "2018-03-26T08:08:32Z")

</div>

Hi there,

does anybody have links/tutorials etc. for usage of modules?  
I have read the lightweight higher kinded types paper and I am searching for other resources in that direction.

Thx.

ciao robertj

---

<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)"

```

---

<div class="post-metadata">

**Author:** ![enetsee](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/enetsee/32/1041_2.png) [@enetsee](https://discuss.ocaml.org/u/enetsee)\
**Post date:** [November 21, 2018, 10:35am UTC](https://discuss.ocaml.org/t/resources-for-advanced-module-usage/1774/3 "2018-11-21T10:35:41Z")

</div>

> [@kantian](#):
>
> You can have a look on the paper [ML module mania](https://www.cs.tufts.edu/~nr/pubs/maniaws-abstract.html)

This looks really interesting - do you know if the complete source code is available anywhere?

Many thanks,

Michael

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [November 21, 2018, 11:01am UTC](https://discuss.ocaml.org/t/resources-for-advanced-module-usage/1774/4 "2018-11-21T11:01:39Z")

</div>

It’s [here](https://github.com/lindig/lua-ml).

---

<div class="post-metadata">

**Author:** ![ivg](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ivg/32/103_2.png) [@ivg](https://discuss.ocaml.org/u/ivg)\
**Post date:** [November 21, 2018, 2:29pm UTC](https://discuss.ocaml.org/t/resources-for-advanced-module-usage/1774/5 "2018-11-21T14:29:40Z")

</div>

The [Modules](https://ocaml.org/docs/papers.html#Modules) section is a prerequisite must read. All advance papers more or less are based on these papers. The [F-ing modules](https://people.mpi-sws.org/~rossberg/f-ing/f-ing-jfp.pdf) paper is a very interesting read, albeit being a little bit fictional. I also found [this thesis](https://people.mpi-sws.org/~dreyer/thesis/old/thesis050405.pdf) to be an interesting compilation, although it is focusing more on SML.
