# Confused about aliases of first class module arguments

**URL:** <https://discuss.ocaml.org/t/confused-about-aliases-of-first-class-module-arguments/8695>\
**Category:** Learning\
**Tags:** first-class\_modules\
**Created:** [October 26, 2021, 7:31pm UTC](https://discuss.ocaml.org/t/confused-about-aliases-of-first-class-module-arguments/8695 "2021-10-26T19:31:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfeser](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jfeser/32/2093_2.png) [@jfeser](https://discuss.ocaml.org/u/jfeser)\
**Post date:** [October 26, 2021, 7:31pm UTC](https://discuss.ocaml.org/t/confused-about-aliases-of-first-class-module-arguments/8695/1 "2021-10-26T19:31:01Z")

</div>

I ran into the following example, and I was surprised to find that `f'` is accepted while `f` is not. Is this expected behavior? I’m guessing it’s related to module type inference on `(module B)`—but I thought that module types are structural, so it should be fine to pass a `B` to a function that expects an `A`.

```auto
module type A = sig
  val a : int
end

module type B = sig
  include A

  val b : int
end

let g (module A : A) = A.a

let f ((module B : B) as m) = g m

let f' (module B : B) = g (module B)

```

---

<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:** [October 26, 2021, 10:26pm UTC](https://discuss.ocaml.org/t/confused-about-aliases-of-first-class-module-arguments/8695/2 "2021-10-26T22:26:41Z")

</div>

> [@jfeser](#):
>
> I’m guessing it’s related to module type inference on `(module B)`

Not really, it’s a problem of subtyping. In `f` the value `m` belongs to the core language with type `(module B)`, but there is no built-in subtyping relation in the core language: `g` expects a value of type `(module A)` that is not a supertype of `(module B)`. Contrarily, in `f'` the value `B` belongs to the module language and the module type `B` is a subtype of `A`, so the built-in subtyping relation of the module language applies.

You could also have defined `f` this way:

```ocaml
let f (m : (module B)) = g m (* don't type check since g : (module A) -> int *)

```

and if you want it to type check, you should write:

```ocaml
let f (m : (module B)) = g (module (val m))

```

---

<div class="post-metadata">

**Author:** ![CraigFe](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/craigfe/32/1989_2.png) [@CraigFe](https://discuss.ocaml.org/u/CraigFe)\
**Post date:** [October 27, 2021, 2:15pm UTC](https://discuss.ocaml.org/t/confused-about-aliases-of-first-class-module-arguments/8695/3 "2021-10-27T14:15:06Z")

</div>

As a bit of an aside, there’s a good reason for not providing structural subtyping of first-class modules: their run-time representation (effectively a record of values) depends on the _order_ in which the values appear in the module type. For example:

```ocaml
(* test_order.ml *)
module type A_then_B = sig val a : int val b : int end
module type B_then_A = sig val b : int val a : int end
module X = struct let a = 1000 let b = 2000 end

let x = (module X : A_then_B)
let x' = (module X : B_then_A)

```

```nohighlight
$ ocamlc -dlambda test_order.ml
(setglobal Test_order!
  (let
    (T/88 =
       (module-defn(T/88) Test_order test_order.ml(3):110-157
         (let (a/86 =[int] 1000 b/87 =[int] 2000) (makeblock 0 a/86 b/87)))
     x'/94 = (makeblock 0 (field 1 T/88) (field 0 T/88)))
    (makeblock 0 T/88 T/88 x'/94)))

```

Our `Test_order` module gets compiled down to a block with three fields: `T`, `x` and `x'`. We see that `x` is represented as exactly `T` (with no computation at runtime), but evaluating `x'` requires making a _new_ heap block in which the fields have been swapped. (Something similar happens when running under `ocamlopt`, but with a bit more noise.)

In your example, the runtime implementation of `f'` depends on the order of fields in `B`:

```diff
-f' = (function m : int (apply g (makeblock 0 (field 0 m))))
+f'_reversed = (function m : int (apply g (makeblock 0 (field 1 m)))))

```

Thus, `f'` can’t in general be implemented as exactly `g` (and the inference to check whether it can would require some notion of row polymorphism, and not just plain structural subtyping). The type-checker thus requires going via the module language (as @kantian describes), with the corresponding run-time operations that this implies.

---

<div class="post-metadata">

**Author:** ![emillon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/emillon/32/1206_2.png) [@emillon](https://discuss.ocaml.org/u/emillon)\
**Post date:** [October 27, 2021, 3:14pm UTC](https://discuss.ocaml.org/t/confused-about-aliases-of-first-class-module-arguments/8695/4 "2021-10-27T15:14:47Z")

</div>

I had no idea that first class module unpacking did something at runtime. Thanks a lot @CraigFe !

---

<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:** [October 27, 2021, 3:50pm UTC](https://discuss.ocaml.org/t/confused-about-aliases-of-first-class-module-arguments/8695/5 "2021-10-27T15:50:52Z")

</div>

> [@CraigFe](#):
>
> the inference to check whether it can would require some notion of row polymorphism, and not just plain structural subtyping

I’m not sure that row polymorphism is necessary for this, they could have transfer the structural subtyping of modules to first-class modules, following this rule: _(module A) \<: (module B) iff A \<: B_. But It’s more code to write and maintain in the compiler, and I don’t think it worth it: it’s easy for a user to retrieve this subtyping relation by unpacking the first class module.

@emillon : this is not the unpacking that did something at runtime, but the coercion on module. It’s the same thing with normal functors when there is a module type coercion.

```ocaml
module type A = sig val a : int end
module type B = sig include A val b : int end

module A : A = struct let a = 1 end
module B : B = struct let a = 1 let b = 2 end
(* the definition of C implies a block creation at runtime *)
module C = (B : A)

```

Basically, modules and records have exactly the same runtime representation (that’s why there is no computation at runtime when defining `x` in @CraigFe code). And the OP example is just similar to this one:

```ocaml
type a = { a : int }
type b = { a : int; b : int }

let g {a} = a

(* the subtyping coercion function *)
let b_to_a {a ; b } = { a }

(* doesn't type check *)
let f ({a; b} as m) = g m

(* type check *)
let f' ({a ; b} as m) = g (b_to_a m)

```

The difference is that with first-class module the coercion function is implicitly applied.

---

<div class="post-metadata">

**Author:** ![CraigFe](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/craigfe/32/1989_2.png) [@CraigFe](https://discuss.ocaml.org/u/CraigFe)\
**Post date:** [October 27, 2021, 8:30pm UTC](https://discuss.ocaml.org/t/confused-about-aliases-of-first-class-module-arguments/8695/6 "2021-10-27T20:30:58Z")

</div>

> [@kantian](#):
>
> I’m not sure that row polymorphism is necessary for this, they could have transfer the structural subtyping of modules to first-class modules, following this rule: _(module A) \<: (module B) iff A \<: B_ . But It’s more code to write and maintain in the compiler, and I don’t think it worth it: it’s easy for a user to retrieve this subtyping relation by unpacking the first class module.

My point here was about using the type system to track whether a particular first-class module coercion could have no run-time cost given the runtime representation (i.e. whether `f' = g`), and this is order-dependent. Naturally if one allows the subtyping coercion `<:` to perform some transformation at run-time then it’s possible to provide structural subtyping here (or any number of fancy subtyping relations 🙂).

To me at least, such a feature would be jarring: I’d rather the type-system not paper over potentially-expensive runtime conversions in the core language. (I think coercions in the module language get a pass here because they’re typically static or happen once at initialisation time.) I’d be interested to know if any of the existing subtyping relations in OCaml have a runtime implementation.

---

<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:** [October 27, 2021, 9:52pm UTC](https://discuss.ocaml.org/t/confused-about-aliases-of-first-class-module-arguments/8695/7 "2021-10-27T21:52:44Z")

</div>

> [@CraigFe](#):
>
> My point here was about using the type system to track whether a particular first-class module coercion could have no run-time cost given the runtime representation

I’m not sure I get your point. When you say:

> [@CraigFe](#):
>
> To me at least, such a feature would be jarring: I’d rather the type-system not paper over potentially-expensive runtime conversions in the core language.

I think that’s what we have with first-class module: coercion in the core language that implies some runtime conversions (because that’s what we have with the module language).

For sure we can’t write this:

```ocaml
let a_modB_is_a_modA m = (m : (module B) :> (module A));;
Error: Type (module B) is not a subtype of (module A)

```

because there is no such built-in subtyping relation, but morally this is exactly what is stating this function:

```ocaml
let a_modB_is_a_modA (module B : B) = (module B : A);;
val a_modB_is_a_modA : (module B) -> (module A) = <fun>

```

And in most (if not all) use cases of first-class functor, such coercion with runtime conversion is what’s happen, because that’s what will do these functors:

```ocaml
(* functor versions of the OP examples *)
module G (A : A) = struct let res = A.a end

(* the runtime creates a block with the shapes of module type A
   in order to apply the functor G to the input B *)
module F (B : B) = G (B)

```

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [October 28, 2021, 9:49am UTC](https://discuss.ocaml.org/t/confused-about-aliases-of-first-class-module-arguments/8695/8 "2021-10-28T09:49:06Z")

</div>

I personnally think that the fact that `:>` is a type-level only operator without any effect on the runtime values is a meaningful property. In particular, without this property, you break separate compilation since you cannot implement coercion for an abstract type constructor with a covariant or contravariant type parameter (because `x: 'a t :> 'b t` now requires to know the definition of `t`) .

---

<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:** [October 28, 2021, 12:59pm UTC](https://discuss.ocaml.org/t/confused-about-aliases-of-first-class-module-arguments/8695/9 "2021-10-28T12:59:10Z")

</div>

Good point, I hadn’t thought to this issue and it’s a very good reason to keep `:>` a type-level operator and a no-op at runtime. I think I get @CraigFe point now.  
And, if we really want such coercion, it’s not really more verbose to write:

```ocaml
let conv m = (m : (module B) :> (module A))

(* compare to *)
let conv m = (module (val m : B) : A)

```

---

<div class="post-metadata">

**Author:** ![gasche](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gasche/32/4_2.png) [@gasche](https://discuss.ocaml.org/u/gasche)\
**Post date:** [October 28, 2021, 2:34pm UTC](https://discuss.ocaml.org/t/confused-about-aliases-of-first-class-module-arguments/8695/10 "2021-10-28T14:34:20Z")

</div>

> [@octachron](#):
>
> In particular, without this property, you break separate compilation since you cannot implement coercion for an abstract type constructor with a covariant or contravariant type parameter (because `x: 'a t :> 'b t` now requires to know the definition of `t` ) .

Well in that alternative world I would expect the module defining `t` to implicitly export a coercion function.

---

<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:** [November 10, 2021, 4:46pm UTC](https://discuss.ocaml.org/t/confused-about-aliases-of-first-class-module-arguments/8695/11 "2021-11-10T16:46:44Z")

</div>

In our actual world, this coercion function is usually explicitly exported and named `map` for covariant functor. But, I guess it’s better to let the user choose how to apply it for efficiency reason. For instance, with the OP example and the `list` functor, we could want to do:

```ocaml
let foo (l : (module B) list) = List.map g (l :> (module A) list)

```

which can be correctly (_i.e_ it type checks) translated to:

```ocaml
let coerce (m : (module B)) = (module (val m) : A)

let foo (l : (module B) list) = List.map (fun m -> g (coerce m)) l

(* or this way if we coerce the list first, but it's less efficient *)
let foo (l : (module B) list) = l |> List.map coerce |> List.map g

```
