Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polymorphic parameters #14

Merged
merged 6 commits into from
Dec 16, 2022
Merged

Polymorphic parameters #14

merged 6 commits into from
Dec 16, 2022

Conversation

lpw25
Copy link
Contributor

@lpw25 lpw25 commented Apr 5, 2022

Seems self-explanatory

@lpw25 lpw25 changed the title First draft of polymorphic parameters Polymorphic parameters Apr 7, 2022
@lpw25 lpw25 requested a review from antalsz May 2, 2022 16:36
Copy link
Contributor

@antalsz antalsz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great change, and the code broadly makes sense to me. That said, I think the larger-scale changed structure needs to be clarified. My understanding is that this PR introduces a new invariant that Tarrows always start with a Tpoly. This needs to be called out somehow in the code, and I can see two ways:

  1. This could be reflected structurally in the code, such as by adding a new type:

    and type_desc =
      (* ... *)
      | Tarrow of arrow_desc * type_arg * type_expr * commutable
      (* ... *)
    
    and type_arg =
      { arg_vars : type_expr list
      ; arg_type : type_expr }
    (** [{arg_vars = [a; b]; arg_type = t}] ==> ['a 'b. t]to arrow types
        Like [Tpoly], but mandatory for arguments  *)
    
    (* ... *)
    

    (Equivalently, type_arg could be inlined into the Tarrow constructor. And maybe it should be flipped around to match Tpoly.)

  2. This could be left as an invariant, but documented, and possibly tagged with a type synonym:

    and type_desc =
      (* ... *)
      | Tarrow of arrow_desc * type_arg * type_expr * commutable
      (* ... *)
    
    and type_arg = type_expr
    (** A [type_arg] is a [type_expr] that is headed by a [Tpoly] constructor, as is
        required for the arguments to [Tarrow]s. This isn't checked, but functions
        that consume [type_arg]s are entitled to assume that it's true. *)
    
    (* ... *)
    

The important thing is that this invariant needs to be legible from the source code; as it stands, it has to be deduced from usage. The tradeoffs between (1) and (2) are fairly typical: on the one hand, if it's reflected structurally, things can't go wrong; on the other hand, then you lose out on having actual type_descs/type_exprs that are Tpolys around, which is potentially inconvenient. I can see arguments for both approaches, so I don't have a strong opinion about which approach to take.

On a related explanatory note, it would be good to see some user-facing documentation for this change (and I'd appreciate some clarification on the principality check, although I'm not sure how important that actually is). And upstream will ask for a changelog entry when we bring this to them; is there a reason not to just write one now?

testsuite/tests/typing-poly/prXXXX.ml Outdated Show resolved Hide resolved
typing/btype.ml Outdated Show resolved Hide resolved
typing/btype.ml Show resolved Hide resolved
typing/btype.ml Outdated Show resolved Hide resolved
typing/btype.mli Outdated Show resolved Hide resolved
testsuite/tests/typing-poly/poly_params.ml Show resolved Hide resolved
testsuite/tests/typing-poly/poly_params.ml Show resolved Hide resolved
typing/ctype.ml Outdated Show resolved Hide resolved
typing/typecore.ml Show resolved Hide resolved
typing/typecore.mli Show resolved Hide resolved
@lpw25 lpw25 force-pushed the poly-param2 branch 2 times, most recently from 737de59 to 68ceb6a Compare September 15, 2022 13:44
typing/ctype.mli Outdated Show resolved Hide resolved
typing/typetexp.ml Outdated Show resolved Hide resolved
typing/typecore.ml Show resolved Hide resolved
typing/typecore.ml Show resolved Hide resolved
typing/typecore.ml Outdated Show resolved Hide resolved
typing/typecore.ml Outdated Show resolved Hide resolved
testsuite/tests/typing-poly/poly_params.ml Show resolved Hide resolved
lambda/translcore.ml Show resolved Hide resolved
typing/typecore.ml Outdated Show resolved Hide resolved
typing/typecore.ml Show resolved Hide resolved
@goldfirere
Copy link
Contributor

I know this is an annoying comment, but it would be really helpful (I think, to all of us, not just newbie me) to have a design document describing this change. That is, this patch is an implementation. But what is the specification of that implementation? Without a specification, it's hard to assess the correctness of an implementation. That is, I can try to read this and figure out whether we've introduced new soundness bugs (clearly bad), but it would be nice if I could do more than just that -- by assessing whether this implementation correctly, optimally implements a specification.

Important bonus of having a specification: we then have a document to describe to others (e.g. upstream) what we are up to -- very valuable!

Copy link
Contributor

@goldfirere goldfirere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't really gotten to the code proper yet -- still trying to understand the specification as hinted at in the poly_params.ml file. But there's enough to plumb there! More to come, but I thought getting these preliminary thoughts out there might be helpful, in case I time out later.

parsing/parser.mly Show resolved Hide resolved
testsuite/tests/typing-poly/poly_params.ml Show resolved Hide resolved
Copy link
Contributor

@antalsz antalsz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had one place I'd like an extra comment, but this looks good to me :-)

typing/typecore.ml Show resolved Hide resolved
@lpw25
Copy link
Contributor Author

lpw25 commented Oct 3, 2022

I think that this PR just needs a couple of changes now:

  1. The use of the new feature should be guarded by an extension flag.
  2. There should be user documentation in the jane/doc folder.

I think the user documentation plus the typing rules should hopefully be a sufficient spec for the feature.

@goldfirere
Copy link
Contributor

I think the user documentation plus the typing rules should hopefully be a sufficient spec for the feature.

Yes, I agree.

Copy link
Contributor

@goldfirere goldfirere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gaining steam understanding the code. More soon.

testsuite/tests/typing-poly/poly_params.ml Show resolved Hide resolved
typing/btype.mli Outdated Show resolved Hide resolved
typing/typetexp.ml Outdated Show resolved Hide resolved
typing/types.mli Show resolved Hide resolved
Copy link
Contributor

@goldfirere goldfirere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, I'm timing out here. But this has been really educational for me!

I do hope the "please comment this more" comments are not too annoying. It's very hard as a current outsider to know what the right level is for documentation, but my (very pleasant) experience in working in a heavily commented compiler suggests that more would be better.

typing/ctype.ml Show resolved Hide resolved
typing/ctype.ml Outdated Show resolved Hide resolved
typing/ctype.ml Outdated Show resolved Hide resolved
typing/ctype.ml Outdated Show resolved Hide resolved
typing/ctype.ml Show resolved Hide resolved
typing/typecore.ml Outdated Show resolved Hide resolved
typing/typecore.ml Outdated Show resolved Hide resolved
typing/typecore.ml Show resolved Hide resolved
typing/typecore.ml Show resolved Hide resolved
typing/typecore.ml Show resolved Hide resolved
@lpw25
Copy link
Contributor Author

lpw25 commented Dec 14, 2022

This is rebased on 4.14 and should be ready to merge once CI has run

@lpw25 lpw25 merged commit fae9aef into ocaml-flambda:main Dec 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants