8000 `('a : value mod non_float) or_null` is `non_float` by dkalinichenko-js · Pull Request #4158 · oxcaml/oxcaml · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

('a : value mod non_float) or_null is non_float #4158

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

dkalinichenko-js
Copy link
Collaborator

Change or_null to be non_float when its type argument is itself non_float. We can't express this behavior with
mod-bounds, so hardcode it in estimate_type_jkind for or_null itself and types marked as re-exporting or_null through the magic [@@or_null_reexport] attribute.

This change has no risk on its own, since 'a or_null can't be put into arrays regardless. However, a subsequent PR will allow 'a or_null array.

@@ -96,7 +96,7 @@ Line 1, characters 19-30:
^^^^^^^^^^^
Error: This type "int or_null" should be an instance of type
"('a : immediate_or_null)"
The kind of int or_null is immediate_or_null with int
The kind of int or_null is immediate_or_null mod non_float with int
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm. immediate should imply non_float. It's impossible to have a non-pointer float.

8000

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed. Before, immediate_or_null had to be maybe-separable because 'a or_null had kind immediate_or_null with 'a, but now the kind is value_or_null with magic.

typing/ctype.ml Outdated
called from [constrain_type_jkind]; the jkind returned without
substing is just weaker than the one we would get by substing. *)
jkind_subst env level type_decl.type_params args jkind
let substituted_jkind =
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this is quite the right implementation strategy.

Instead, I think we should view or_null as closer to an [@@unboxed] type. Interestingly, estimate_type_jkind does not treat [@@unboxed] types specially. The special treatment for [@@unboxed] types comes in constrain_type_jkind, which knows how to look inside them. This is all ok, because estimate_type_jkind can just return an upper bound. This means that the separability of ty or_null can be maybe_separable for any ty -- as returned from estimate_type_jkind. But we must work harder in constrain_type_jkind to make up for this.

The way I think would work better is to teach unbox_once about or_null. unbox_once could return a new constructor Stepped_or_null (or something) in the or_null case. Critically, in constrain_type_jkind, before recurring on the type in the or_null, we have to adjust the jkind passed to loop, much like the existing use of Jkind.apply_modality_r call on line 2572.

I think this new implementation strategy is more "with the grain" of current things, and it also should empower more inference: if we have val f : 'a or_null accepts_non_float -> ..., we'll be able to infer that 'a is mod non_float too. (I don't think this works with the current implementation.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, you're right. We want to eventually unify Variant_with_null with Variant_unboxed anyway.

Done.

@dkalinichenko-js
Copy link
Collaborator Author

Now, 'a or_null is back to having jkind value_or_null, with the same magic behavior for modal axes as we have on unboxed types. It also now accepts maybe-separable values.

@dkalinichenko-js dkalinichenko-js added typing unboxed types or_null Related to or_null layouts labels Jun 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
or_null Related to or_null layouts typing unboxed types
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0