include functor support for OxCaml - #1452
Conversation
18499bf to
ca9079a
Compare
ca9079a to
a6c9652
Compare
art-w
left a comment
There was a problem hiding this comment.
Thanks a lot, this looks great! I only have minor feedback but nothing blocking :)
| (** This is a Module where the type is named and then included. *) | ||
| module type Make = (_ : sig type t end) -> sig type included end | ||
| type t | ||
| include functor Make |
There was a problem hiding this comment.
Out of curiosity, can we test the behavior with (** @inline *)?
|
I think there's a chunk of logic missing from this PR. At the moment, all it does AFAICT is get the expansion of the functor and splice it into the expansion of the include. While this works for simple examples, it's not sufficient for more involved cases. As a simple example, consider this: module F ( I : sig type t end ) = struct
type myt = I.t
end
module M = struct
type t = float
include functor F
endRunning this through module F : functor (I : sig type t end) -> sig type myt = I.t end
module M : sig type t = float type myt = float endIf we just simply get the signature of the functor body and splice it in, we end up something more like: ...
module M : sig
type t = float
type myt = I.t
endand obviously that I suspect the most straightforward implementation of this would be to do exactly what the docs suggest it does internally. Treat it more like: module M = struct
module __DUMMY__ = struct
type t = float
end
include __DUMMY__
include F(__DUMMY__)
endWe can ensure that the dummy module is hidden, meaning that the include will just inline the contents, |
78e4383 to
e8b5ba1
Compare
5997c73 to
ac92802
Compare
|
I've now implemented @jonludlam's proposal of compiling down the include functor to basically a The way it works is by parsing the However there's one outstanding issue: while the items coming from the functor expansions contain the right path (e.g. What I could imagine would be best if I could hide the generated modules but also tell the cross-linker that I want the item to be cross-referenced to the ("include") functor, not the generated module. Is there a way to tell odoc to "redirect" paths? |
|
Odoc has a mechanism to specify a substitution via |
panglesd
left a comment
There was a problem hiding this comment.
I'll continue the review, but here is already some comments. I think the Make functor should be made a little bit more complex, and in particular use its argument.
module Make (T : sig type t end) : sig type included = T.t endNow suppose it is used in the following way:
type t
include functor MakeFrom my understanding of the oxcaml feature, the rendered signature should be:
type t
> include functor Make
type included = t(where > is for the opened include accordion 🪗 )
In my testing, that is not the case!
I wonder if it is possible to fix that using the current "sugar". I believe the "desugared" form of the example above (extended with a value and a module) could use aliases, for types, modules and module types, instead of relying on the DUMMY__ include :
type t
val f : t
module X : S
module DUMMY__ : sig
type nonrec t = t
val f : t
module X = X
end
module STRUCT__Make : Make
include module type of STRUCT__Make(DUMMY__)
endWhat do you think?
Also, definitely we want those generated modules to be hidden. As @jonludlam mentioned, that would turn the "inlining" invisible if we keep it, and odoc would use links to a visible alias.
Also note that while I'd like to investigate the alternate sugar, the current version is better than no support, and as such (I still need more review of course) I would not block the merge on the problem noted above!
|
I had a go at the "alias desugaring" idea: https://github.com/panglesd/odoc/tree/include-functor-alias-desugaring It looks a reasonable first solution, I think. Maybe implementing the "include functor expansion" mechanism in odoc would be better, but slightly more invasive... |
The synthetic module standing in for the functor argument held copies of the
preceding items. Since it is hidden, everything the functor's expansion
inherited from its argument came out as `BODY__n.t` -- a hidden path, which
`Link.type_decl` resolves by looking through it, landing on the copy's own
abstract declaration. So a functor that used its argument, as
`Comparable.Make` does, lost exactly the part worth documenting.
Alias the items instead: a type's manifest points at the real type, a module
becomes an alias of the real module, a module type a path to it. `BODY__n.t`
then reduces to the enclosing signature's own `t`, and is rendered and linked
as such.
Class types are not aliased. A functor can refer to one from its argument, but
odoc does not chase class type aliases the way it chases type manifests, so
such a reference is still left printing the wrapper's hidden name. That is a
pre-existing limitation, reachable without `include functor`:
module Hidden__thing : sig class type ct = object method m : int end end
type from_class = Hidden__thing.ct
Suggested by @panglesd in ocaml#1452.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Rather than put the functor arguments in a module and `include` it, leave them where they were and construct a synthetic module that's just full of aliases to the items. Suggested by @panglesd in ocaml#1452. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Marek Kubica <marek@tarides.com>
Signed-off-by: Marek Kubica <marek@tarides.com>
Signed-off-by: Marek Kubica <marek@tarides.com>
bf33dad to
8b581e3
Compare
Four new cases, plus two existing ones strengthened: - `Include_functor_argument_shapes` exercises everything the expansion of the functor can inherit from its argument - plain, parameterised and anonymously parameterised types, a submodule and a module type. - `Multiple_include_functors` has two `include functor`s with an item between them, and `Include_functor_not_last` a single one with an item after it. - `Include_functor_named_type`'s functor and `Oxcaml_impl.Include_functor`'s now use their argument. The structure side had no case that did, so the argument being ignored would have gone unnoticed there. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Simplifies the desugaring of:
include functor Make
which in a signature became
module INCLUDE__1 : <module type of Make> (hidden)
module APPLIED__2 : module type of struct include INCLUDE__1(BODY__3) end
include APPLIED__2 (hidden)
where all that `APPLIED__2` achieves is giving the application a name to be
referred to by. But `INCLUDE__1` is already a path, so the application can be
written as one directly, as the structure reader has always done:
module INCLUDE__1 : <module type of Make> (hidden)
include INCLUDE__1(BODY__3)
That drops one generated module per `include functor`.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
An `include functor` in a structure need not name its functor:
type t
include functor (functor (T : sig type t end) -> struct type included = T.t end)
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The wrapper module was allocated once per signature and shared, the enclosing signature was rebuilt as an `include` of it, and the include functors were partitioned out of the fold and re-emitted at the end. This had 2 issues: - the fold rebound the accumulator instead of appending to it, so only the include functors yielded by the *final* item of a signature survived. In practice that means an `include functor` was silently dropped unless nothing followed it: writing a single one and then a type below it lost the whole include, and of two in a row only the second was kept. - the shared wrapper was built from the non-include-functor items only, so a second `include functor` could not see the output of the first, and the reference to it leaked the hidden wrapper's name into the rendered output. Instead notice the `include functor` during the fold over a signature items and pass the accumulator directly. This ends up reverting the threading through of the `include_functor_wrapper` parameter. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Rather than put the functor arguments in a module and `include` it, leave them where they were and construct a synthetic module that's just full of aliases to the items. Suggested by @panglesd in ocaml#1452. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
3c13e61 to
e3c4b33
Compare
|
I've ended up cherry-picking the commits from @jonludlam's branch, which among an alternative desugaring also solves another real problem that I didn't think of: the Also added an additional example of when the body of the module contains an |
These were obsoleted by Jon's code and the PR pulled out of this was rejected so no need to keep them around.
panglesd
left a comment
There was a problem hiding this comment.
I have some small comments on the included items in the wrapper module, which needs some testing and (I would bet but not a lot) some implementation.
Otherwise, very good work! Thanks!
| | Value _ | ModuleSubstitution _ | ModuleTypeSubstitution _ | Open _ | ||
| | TypeSubstitution _ | TypExt _ | Exception _ | Comment _ -> | ||
| (* Nothing in the expansion of [F(BODY__n)] can refer to these. *) | ||
| acc | ||
| | Class _ | ClassType _ -> |
There was a problem hiding this comment.
Nothing can refer to them, but without including them, we apply F(BODY_n) without BODY_n having module type what F is expecting.
Maybe that is fine, I don't think odoc will check that as it assumes the compiler has already enforced this, however I think it is worth adding tests (as for instance if later odoc requires BODY_n to have the right signature.
But maybe that is not fine! What happens to the values, if the input module is stored in the expansion?
I think it would be good to add a test with values, ..., classes in the input to the include functor. Moreover, the input functor should alias its input, to test the expansion of the situation described above.
There was a problem hiding this comment.
Oops, yes, you're right. Nothing stopping the functor from just making an alias of its argument, and then we'd lose all of those other items. My mistake, sorry!
There was a problem hiding this comment.
I've added a test for class, class types and aliases which exposes this issue:
- The classes and class types refer to the generated
BODY__nmodule. It's somewhat surprising givenBODY__nshould be hidden, I think there's someis_hiddencheck missing in some place. - The
Aliasedmodule is constructed properly but indeed missing the class types.
6517fe4 to
6843592
Compare
This PR is a follow up to #1368 and adds support for
include functorwhere it now displays that the items were included via the functor.