add -print-variance and Asttypes.Bivariant#13820
Conversation
|
The support for type +-'a tcould also be split in its own PR. |
|
This is a debugging flag, and |
|
The difference with usual debugging flags is that its output is intentionally made compatible with the parser. This is not the case for the -d... Flags. |
|
The At the same time, I think that supporting |
|
Summing up the opinions, may I leave this PR as is for reviewing, if it does not take seriously longer than two PRs? |
|
For tests, what about enabling |
|
Just splitting the commit in two is enough for me, there is no strong need to split the PR — if we agree on the name. What about For the tests, it might make sense to enable the option globally in |
|
I did not know about |
|
After splitting, I will try turning on the option in |
|
Splitting done, but I am afraid that most changes went into the first commit. |
| (fun (c,n,i) -> | ||
| let i = if check_injectivity then i else false in | ||
| if c || n then (c,n,i) else (true,true,i)) | ||
| if c <> n then (c,n,i) else (not c,not n,i)) |
There was a problem hiding this comment.
This line seems to be the most tricky change.
Jacques and I could not find an immediate way to refactor this part.
There was a problem hiding this comment.
To be more precise, c and n in required reflect + and - in the syntax, not the corresponding variance bits, so a conversion is needed here.
It would be nicer to do the conversion earlier, but other parts of the code depend on this meaning, so this should be a separate refactoring.
There was a problem hiding this comment.
What about writing the conversion
(* c and n reflects respectively + and - in the syntax, and maps respectively to
`not May_neg` and `not May_pos` in the {!Types.Variance.f} fields *)
not n, not c, i?
There was a problem hiding this comment.
👍
Thank you so much for a careful reading and a nice cleaning.
|
I'm not sure we want to turn this on on all expect tests at once, since this will require reviewing individually all the expect tests for changes. If we turn this on, I would suggest doing it in yet another PR. |
| and use_runtime = ref "" (* -use-runtime ... *) | ||
| and plugin = ref false (* -plugin ... *) | ||
| and principal = ref false (* -principal *) | ||
| and print_variance = ref false (* -print-variance *) |
There was a problem hiding this comment.
I would propose to move the definition just below print_types (and to update the comment).
There was a problem hiding this comment.
Thank you for spotting. I will fix it.
| { if $1 = "+!" then Covariant, Injective else | ||
| if $1 = "-!" then Contravariant, Injective else | ||
| if $1 = "+-" then Bivariant, NoInjectivity else | ||
| if $1 = "+-!" then Bivariant, Injective else |
There was a problem hiding this comment.
Do we have a good reason and mnemonic to allow only +- and not -+?
There was a problem hiding this comment.
I thought it would be better to minimize the change to the syntax, and did not think that the symmetry here could help a lot of users. Do you have other opinions?
There was a problem hiding this comment.
I would tend to prefer allowing any permutation of ± and !, and to consider that ± can be written either +- or -+.
In other words, adding +-, -+, +-!, -+!, !+-, and !-+ (but not +!- nor -!+).
There was a problem hiding this comment.
Okay, my preference was anyway not strong, and I am happy to follow your suggestions. I will add -+ and -+!.
(Btw, at the first attempt Jacques and I considered + -! +- !, etc. too, and soon realized that the list will become huge :)
The change in the tests becomes indeed large. What do you think about this, @Octachron ? |
|
I have mostly finished to review the changes in the testsuite, so I am fine either way. |
| (fun (c, n, i) -> make (not n) (not c) (not abstract || i)) | ||
| required | ||
| else begin | ||
| | _ -> (* TODO: list every type_kind *) |
There was a problem hiding this comment.
Do you plan to implement this TODO in this PR or a later one?
There was a problem hiding this comment.
Let me do it in this PR.
Thank you so much, and I am happy to know that variances have been correctly inferred in other tests. What about adding |
Sorry, I have been reading wrongly Jacques' comment. There is no concern about the testsuite after Florian's review. Let us not do "-no-i-variance". |
182fcf8 to
f7be37f
Compare
|
rebased |
| val _absname : unit -> unit | ||
| val _no_absname : unit -> unit | ||
| val _alert : string -> unit | ||
| val _i_variance : unit -> unit |
There was a problem hiding this comment.
I am not sure why you moved the option, having -i-variance available in the toplevel seemed useful to me.
There was a problem hiding this comment.
Oops, I misunderstood about the Common_options (that it were controlling only the usage output).
I will revert this change.
| ( Type_abstract _ | ||
| | Type_record _ | ||
| | Type_variant _ | ||
| | Type_open ); type_manifest = _} -> |
There was a problem hiding this comment.
I would propose to split the case in two patterns to make it commutative with the previous case:
| { type_kind = _; type_manifest = Some _ }
| { type_kind = Type_record _ | Type_variant _; type_manifest = _ } ->|
After rereading the testsuite, I fear that my opinion has soured a bit on the size of the testsuite change. Thus I would propose to not set trick. Looking back at the syntax, I was wondering if there were other possible options. Some quite bad options:
An ok-ish option suggested by @samsa1 :
Thus, even after looking at other options, I still think that |
| type !'a t : may_pos + may_neg + inj | ||
| type +!'a t : may_pos + inj | ||
| type -!'a t : may_neg + inj | ||
| type +-!'a t : inj |
There was a problem hiding this comment.
The alignment on : is broken here.
|
I have removed three commits that was enabling I did not know and just have learned the trick to import |
|
I think that the |
|
Added |
Octachron
left a comment
There was a problem hiding this comment.
I agree that the addition of a surface language for bivariance is natural, and the -i-variance option can be useful both for debugging and learning about variance.
@t6s, do you wish to clean-up the history (I would suggest to keep at least an -i-variance commit, a +- commit and a test commit)?
|
Thank you for reviewing @Octachron . I will soon push squashed 3 commits following your suggestion. |
|
It could be useful to have |
|
Could be. But a problem is that |
|
I have quickly checked that |
This PR adds a new command-line option
-print-variancethat lets the compiler print the variance of every type.Types parameters like
'aintype 'a t = Aare bivariant (assured to have no occurrence of the type parameter);the bivariance is now printed as
+-(liketype +-'a t = A).For the consistency between the printed result and the syntax, the parser is modified too.
Coauthored by @garrigue