You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wonder how the P4 frontend should check the validity of default parameter value, of generic type. The spec allows supplying default values for parameters, but does not explain much about their typing:
If a parameter has a default value and no corresponding argument is supplied, the default value is used as an argument. (section 6.8)
Take this code for example (a slight variant of the code example in section 6.8.2),
It depends on the decision whether the type constraint H = M = empty_t implied by the declaration package P<H, M>(C<H, M> c = nothing()); persists throughout the program, or is discarded.
If the constraint persists, then we should reject the instantiation P(something()) main;, since it implies H = M = some_t != empty_t.
If the constraint is discarded, then we can accept the instantiation P(something()) main;.
Question 3. What if there are multiple generic parameters with default values?
The problem gets even more complicated with multiple generic parameters with defaults.
Now, parameters c and d imply conflicting type variable resolutions. c implies H = M = empty_t while d implies H = M = some_t, where empty_t and some_t are distinct types.
Then, do we consider the package declaration as valid?
If we treat default parameter values independently, then we may accept the declaration as valid. But this may introduce some confusion in the instantiation sites of the package. For example,
P() main; // is invalid since nothing() and something() impose conflicting type constraintsP(c = something()) main; // is validP(d = nothing()) main; // is valid
If we take them collectively, i.e., unifying C<H, M> c = nothing(), C<H, M> d = something() altogether, the declaration should result in a type error.
Personal Thoughts
So there are multiple design questions regarding this feature: lifetime of an implicit type constraint (persists throughout the program or discarded) and span of type constraint (independently or collectively).
IMHO, I would propose specifying (i) type constraint implied by default parameter value does not persist across the program and (ii) default parameter values are unified collectively.
The text was updated successfully, but these errors were encountered:
I wonder how the P4 frontend should check the validity of default parameter value, of generic type. The spec allows supplying default values for parameters, but does not explain much about their typing:
Take this code for example (a slight variant of the code example in section 6.8.2),
Here,
nothing()
is a default parameter value for the generic parameterc
of typeC<H, M>
.Question 1. How do we check the validity of the package declaration
package P<H, M>(C<H, M> c = nothing());
?Checking the validity of default values without generics is, straightforward.
Here, we check that the default value
true
forb
is indeedbool
type, soh
is a valid extern function declaration.But for the given example, should the frontend check if
nothing()
is unifiable to typeC<H, M>
?Question 2. Does default parameter value impose a type constraint?
Let's introduce type
some_t
and a control declarationsomething
that has typeC<some_t, some_t>
.Should we check the program as valid or not?
It depends on the decision whether the type constraint
H
=M
=empty_t
implied by the declarationpackage P<H, M>(C<H, M> c = nothing());
persists throughout the program, or is discarded.If the constraint persists, then we should reject the instantiation
P(something()) main;
, since it impliesH
=M
=some_t
!=empty_t
.If the constraint is discarded, then we can accept the instantiation
P(something()) main;
.Question 3. What if there are multiple generic parameters with default values?
The problem gets even more complicated with multiple generic parameters with defaults.
Now, parameters
c
andd
imply conflicting type variable resolutions.c
impliesH
=M
=empty_t
whiled
impliesH
=M
=some_t
, whereempty_t
andsome_t
are distinct types.Then, do we consider the package declaration as valid?
If we treat default parameter values independently, then we may accept the declaration as valid. But this may introduce some confusion in the instantiation sites of the package. For example,
If we take them collectively, i.e., unifying
C<H, M> c = nothing(), C<H, M> d = something()
altogether, the declaration should result in a type error.Personal Thoughts
So there are multiple design questions regarding this feature: lifetime of an implicit type constraint (persists throughout the program or discarded) and span of type constraint (independently or collectively).
IMHO, I would propose specifying (i) type constraint implied by default parameter value does not persist across the program and (ii) default parameter values are unified collectively.
The text was updated successfully, but these errors were encountered: