An interesting corner case is 
```
open struct let map f x = f x end
let () = map (fun _ -> ()) 0
```
since OCaml let's us open anonymous modules. In this case, -dtypedtree generates an unqualified names. So it would be rather easy to remove all opens to of the named modules, since OCaml will generate fully qualified names, e.g., `Texp_ident "Stdlib!.List.map"`, but for anonymous modules, it would be harder to do. 

On Wed, Apr 23, 2025 at 10:32 AM Francois Pottier <francois.pottier@inria.fr> wrote:

Hello,

Le 23/04/2025 à 16:10, Kenichi Asai a écrit :
> Would it be possible to transform an OCaml file to the one that does
> not use open?

I don't know whether it is possible/easy to do this today,
but it would certainly interesting and useful to have such
a tool.

I note that the output that you expect cannot always be
produced, due to name shadowing issues. For example if
the program is

open List
module List = struct end
let test = map (fun x -> x + 1) [1; 2; 3]

then the best output that one can expect is

let map = List.map
module List = struct end
let test = map (fun x -> x + 1) [1; 2; 3]

That said (contradicting myself), one can actually obtain
better output if one is careful to always use absolute paths.
In this example one could write:

module List = struct end
let test = Stdlib.List.map (fun x -> x + 1) [1; 2; 3]

which relies on the fact that the name "Stdlib" is not shadowed.

As far as I know there is currently no syntax for absolute
paths in OCaml (every path is relative, and every name can
be shadowed). Maybe we should consider adding such a syntax?

--
François Pottier
francois.pottier@inria.fr
https://cambium.inria.fr/~fpottier/