# How to map the inputs and outputs of a list of functions in a module?

**URL:** <https://discuss.ocaml.org/t/how-to-map-the-inputs-and-outputs-of-a-list-of-functions-in-a-module/3065>\
**Category:** Learning\
**Created:** [December 16, 2018, 11:11pm UTC](https://discuss.ocaml.org/t/how-to-map-the-inputs-and-outputs-of-a-list-of-functions-in-a-module/3065 "2018-12-16T23:11:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnWu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/johnwu/32/1351_2.png) [@JohnWu](https://discuss.ocaml.org/u/JohnWu)\
**Post date:** [December 16, 2018, 11:11pm UTC](https://discuss.ocaml.org/t/how-to-map-the-inputs-and-outputs-of-a-list-of-functions-in-a-module/3065/1 "2018-12-16T23:11:13Z")

</div>

So let’s say I have a module A with the following signature

```
module A : sig
type t
val foo : t -> input -> output
val bar : t -> input -> output
end

```

Also, I have an arbitrary monad module M, that has the following type `type 'a t`.

Now I would like to create a module B such that it maps the signatures of the functions in the following way:

```
module B: sig
type t
val foo : t M.t-> input -> output M.t
val bar : t M.t -> input -> output M.t
end

```

Is there a nice way to do this in OCaml? I don’t think you can use regular functors to do this because they require you to know the name of each function.

---

<div class="post-metadata">

**Author:** ![Juloo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/juloo/32/881_2.png) [@Juloo](https://discuss.ocaml.org/u/Juloo)\
**Post date:** [December 17, 2018, 9:25am UTC](https://discuss.ocaml.org/t/how-to-map-the-inputs-and-outputs-of-a-list-of-functions-in-a-module/3065/2 "2018-12-17T09:25:05Z")

</div>

This is often done with a `lift` function

Also, OCaml let you use higher-order functions in modules:

```auto
module B = struct

(* type t *)

let lift f t input = (* do the lift *)

let foo = lift A.foo
let bar = lift A.bar

end
```

---

<div class="post-metadata">

**Author:** ![steinuil](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/steinuil/32/993_2.png) [@steinuil](https://discuss.ocaml.org/u/steinuil)\
**Post date:** [December 17, 2018, 9:32am UTC](https://discuss.ocaml.org/t/how-to-map-the-inputs-and-outputs-of-a-list-of-functions-in-a-module/3065/3 "2018-12-17T09:32:17Z")

</div>

Not within the type system. First of all, OCaml doesn’t support the kind of type-level compuation you’d need to turn `'a -> 'b -> 'c` into `'a 't -> 'b -> 'c 't`. Then you’d need some functor that could iterate over its argument’s `val`s to apply this function, which I don’t think is possible in any language with ML-like modules.

You could probably write a ppx to generate all the code automatically, but IMO that’s more trouble than it’s worth and it’s better to just do it manually as @Juloo suggested above.

---

<div class="post-metadata">

**Author:** ![lindig](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lindig/32/532_2.png) [@lindig](https://discuss.ocaml.org/u/lindig)\
**Post date:** [December 17, 2018, 1:36pm UTC](https://discuss.ocaml.org/t/how-to-map-the-inputs-and-outputs-of-a-list-of-functions-in-a-module/3065/4 "2018-12-17T13:36:46Z")

</div>

It’s probably not central to your problem: there is no way to create a value of `A.t` because `t` is abstract and none of the functions in `A` are creating such a value.
