Repository navigation
occur_rec on a graph rather than a tree - #13866
Conversation
| | Tobject _ | Tvariant _ -> | ||
| () | ||
| | _ -> | ||
| if allow_recursive || TypeSet.mem ty visited then () else begin |
There was a problem hiding this comment.
I don't know where to leave this comment so I'll put it here :
I'm surprised by this line. Here we say that if we see a recursive type when checking that we don't create one we can just ignore it. I know that rejecting it at that moment without having a good location for an error message might be problematic however isn't it better to reject it at that moment than to accept the code ?
The only tests in the testsuite affected by adding an assert allow_recursive here are tests identified as bugs
There was a problem hiding this comment.
The test is typing-polyvariants-bug-2/pr3918.ml is not exactly a bug but it is probably fine to reject a recursive type which is not allowed due to a missing cmi file.
There was a problem hiding this comment.
@garrigue , can you think of any other circumstances when we may have a cycle not guarded by an object or variant types?
Otherwise, I would be tempted to consider that it is better to fail on those kind of unguarded cycles created by missing cmis.
There was a problem hiding this comment.
I wrote some documentation on what I knew of recursive type situations in OCaml in typedecl.ml (to document check_well_founded):
Line 680 in 8f7c58f
The documentation agrees with your intuition that in absence of -rectypes, recursive types are only allowed to go through polymorphic variants and object types. (To check this, look for the occurrences of " guarded" in the documentation, which list the only sources of allowed cycles; when -rectypes is used, the "rectypes-guarded" constructs are also allowed.)
There was a problem hiding this comment.
Exploring the history
- pre-existing cycles were allowed in bea2f16 to handle GADT-induced cycles
- Since Fix #7902: Type-checker infers a recursive type, even though -rectype… #9556, the typechecker rejects GADT-induced cycles without
-rectypes(outside of the counter-example checking mode).
Thus it seems that we could remove the support for pre-existing cycles in occur_rec, but in a later PR.
There was a problem hiding this comment.
For whether it is ok to fail when discovering a pre-existing cycle, this requires checking a number of things: namely, interactions with the counter-example mode (which is allowed to create such cycles), and mixing of files compiled with an without -rectypes. I don't have all the details in mind, but this would depend on this.
Also, logically, it should probably be an assertion rather than an occur check error, since it would not be supposed to happen.
| Tconstr(p, _tl, _abbrev) -> | ||
| if allow_recursive && is_contractive env p then () else | ||
| begin try | ||
| if TypeSet.mem ty visited then raise Occur; |
There was a problem hiding this comment.
You could use two distinct marks (a visited mark and a seen mark) to remove the TypeSet entirely.
There was a problem hiding this comment.
I visited is handled as a mark we would need to remove the mark whenever an exception is raised (which seems quite costly).
However I fully agree that mark is not a good name and seen is much better
3fa9466 to
4637f69
Compare
|
The logic seems fine to me. |
|
I am currently planning to run a compiler benchmark on the change this weekend as a sanity check before merging on Monday. |
|
Looking at performance number while compiling a hundred of opam packages, there is an expected increase in the time spent in
Thus the changes look ready to merge (once the Changes conflict is fixed). |
4637f69 to
db20341
Compare
When unifying two types we check that this does not create a recursive type (if
-rectypesis not enabled).However this test can be quite costly as reported by @diremy.
For example if we take the following example :
If we repeat line 6 we get a catastrophic complexity :
On the other hand, with
-rectypes, repeating this line 14 times leads to a whole compilation time below 0.1s.A first fix for this problem is to do the occurrence check on a graph rather than on a tree. With this optimization we get the following performances on the test presented above :
As a side note : I checked and this does not seam to impact the time needed to compile the testsuite