Constant Default Parameter Support #98
|
@ -1135,8 +1135,16 @@ impl TopLevelComposer {
|
|||
ty,
|
||||
default_value: match default {
|
||||
None => None,
|
||||
Some(default) =>
|
||||
Some(Self::parse_parameter_default_value(default, resolver)?)
|
||||
Some(default) => Some({
|
||||
let v = Self::parse_parameter_default_value(default, resolver)?;
|
||||
Self::check_default_param_type(
|
||||
&v,
|
||||
&type_annotation,
|
||||
primitives_store,
|
||||
unifier
|
||||
).map_err(|err| format!("{} at {}", err, x.location))?;
|
||||
v
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
@ -1353,7 +1361,12 @@ impl TopLevelComposer {
|
|||
if name == "self".into() {
|
||||
return Err(format!("`self` parameter cannot take default value at {}", x.location));
|
||||
}
|
||||
Some(Self::parse_parameter_default_value(default, class_resolver)?)
|
||||
Some({
|
||||
let v = Self::parse_parameter_default_value(default, class_resolver)?;
|
||||
Self::check_default_param_type(&v, &type_ann, primitives, unifier)
|
||||
.map_err(|err| format!("{} at {}", err, x.location))?;
|
||||
v
|
||||
})
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -350,6 +350,62 @@ impl TopLevelComposer {
|
|||
pub fn parse_parameter_default_value(default: &ast::Expr, resolver: &(dyn SymbolResolver + Send + Sync)) -> Result<SymbolValue, String> {
|
||||
parse_parameter_default_value(default, resolver)
|
||||
}
|
||||
|
||||
pub fn check_default_param_type(val: &SymbolValue, ty: &TypeAnnotation, primitive: &PrimitiveStore, unifier: &mut Unifier) -> Result<(), String> {
|
||||
let res = match val {
|
||||
SymbolValue::Bool(..) => {
|
||||
if matches!(ty, TypeAnnotation::Primitive(t) if *t == primitive.bool) {
|
||||
ychenfo marked this conversation as resolved
Outdated
|
||||
None
|
||||
ychenfo marked this conversation as resolved
Outdated
sb10q
commented
Isn't that an internal compiler error if this code is executed, not a problem with the user code? Isn't that an internal compiler error if this code is executed, not a problem with the user code?
Using ``unreachable!()`` sounds more appropriate here.
sb10q
commented
Isn't that an internal compiler error if this code is executed, not a problem with the user code? Isn't that an internal compiler error if this code is executed, not a problem with the user code?
Using ``unreachable!()`` sounds more appropriate here.
pca006132
commented
No I don't think so? It is possible for users to specify some default value outside the range of int64. I think a more appropriate error message would be value out of range and only say No I don't think so? It is possible for users to specify some default value outside the range of int64. I think a more appropriate error message would be value out of range and only say `int64` if the value is really an int64?
sb10q
commented
OK, yes. OK, yes.
ychenfo
commented
Thanks, this error message is updated in the new commit Thanks, this error message is updated in the new commit
|
||||
} else {
|
||||
Some("bool".to_string())
|
||||
}
|
||||
}
|
||||
SymbolValue::Double(..) => {
|
||||
if matches!(ty, TypeAnnotation::Primitive(t) if *t == primitive.float) {
|
||||
None
|
||||
} else {
|
||||
Some("float".to_string())
|
||||
}
|
||||
}
|
||||
SymbolValue::I32(..) => {
|
||||
if matches!(ty, TypeAnnotation::Primitive(t) if *t == primitive.int32) {
|
||||
None
|
||||
} else {
|
||||
Some("int32".to_string())
|
||||
}
|
||||
sb10q
commented
Aren't we matching on Aren't we matching on ``id(numpy.int64)`` elsewhere?
https://git.m-labs.hk/M-Labs/nac3/src/branch/master/nac3artiq/src/lib.rs#L270-L306
I'm fine with string matches since they're simpler and potentially faster, but we should do things consistently.
ychenfo
commented
I think here we are handling default parameters in nac3core so we do not really know the In type inference module we also do things using strings matches here and here. So I think it is ok to use strings? Or did I miss anything? I think here we are handling default parameters in nac3core so we do not really know the `id(numpy.int64)`?
In type inference module we also do things using strings matches [here](https://git.m-labs.hk/M-Labs/nac3/src/branch/master/nac3core/src/typecheck/type_inferencer/mod.rs#L659) and [here](https://git.m-labs.hk/M-Labs/nac3/src/branch/master/nac3core/src/typecheck/type_inferencer/mod.rs#L624). So I think it is ok to use strings? Or did I miss anything?
pca006132
commented
That is matching on the value type, not about functions. > Aren't we matching on `id(numpy.int64)` elsewhere?
https://git.m-labs.hk/M-Labs/nac3/src/branch/master/nac3artiq/src/lib.rs#L270-L306
I'm fine with string matches since they're simpler and potentially faster, but we should do things consistently.
That is matching on the value type, not about functions.
sb10q
commented
What's the difference? There isn't one in CPython. What's the difference? There isn't one in CPython.
Also this can cause issues e.g. if the user types ``from numpy import int64 as i64``. CPython and nac3artiq will expect ``i64``, whereas nac3core will expect ``int64``.
If that's the simplest thing to do, I'm fine having restrictions such as "numpy types must be imported as their reserved NAC3 keywords int64/int32/..." but they should be documented and enforced by the compiler.
sb10q
commented
Anyway this isn't the most pressing issue at the moment and we can do this inconsistent string/id match for now. Let's continue this discussion in #105 Anyway this isn't the most pressing issue at the moment and we can do this inconsistent string/id match for now. Let's continue this discussion in https://git.m-labs.hk/M-Labs/nac3/issues/105
|
||||
}
|
||||
SymbolValue::I64(..) => {
|
||||
if matches!(ty, TypeAnnotation::Primitive(t) if *t == primitive.int64) {
|
||||
None
|
||||
} else {
|
||||
Some("int64".to_string())
|
||||
}
|
||||
}
|
||||
SymbolValue::Tuple(elts) => {
|
||||
if let TypeAnnotation::Tuple(elts_ty) = ty {
|
||||
for (e, t) in elts.iter().zip(elts_ty.iter()) {
|
||||
Self::check_default_param_type(e, t, primitive, unifier)?
|
||||
}
|
||||
if elts.len() != elts_ty.len() {
|
||||
sb10q
commented
Lists should also be supported (also if they are a module global, see comment below). Lists should also be supported (also if they are a module global, see comment below).
pca006132
commented
Lists are not immutable, I think we discussed this previously?
Lists are not immutable, I think we discussed this previously?
https://git.m-labs.hk/M-Labs/nac3-spec/issues/6
> Function parameter defaults for kernel, portable, and rpc functions are very important for us. The limitation of only allowing immutable or primitive default types is fine.
sb10q
commented
Indeed, and that's a well-known pitfall in CPython as well. Indeed, and that's a well-known pitfall in CPython as well.
ARTIQ drivers do use lists with default parameter. AFAICT this can be changed to tuple, so far...
sb10q
commented
Well we can start with tuples only for now and see if that causes any serious problems. Well we can start with tuples only for now and see if that causes any serious problems.
|
||||
Some(format!("tuple of length {}", elts.len()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
sb10q
commented
We also want to support globals defined in the module, e.g.:
See the ARTIQ drivers, this is used in many places. We also want to support globals defined in the module, e.g.:
```python
FOO = 1234
class XXX:
def yyy(self, z: int32 = FOO):
...
```
See the ARTIQ drivers, this is used in many places.
sb10q
commented
In fact, why not just compile the expression like any other? In fact, why not just compile the expression like any other?
Things like ``def foo(x=[i*2 for i in range(3)])`` are also valid Python.
pca006132
commented
The expression should be immutable and primitive types/tuples. I think we can The expression should be immutable and primitive types/tuples. I think we can `eval()` the expression and set the default value if its type can be accepted.
sb10q
commented
``eval`` is slow and not available in nac3standalone.
pca006132
commented
Regarding performance issue, I think we can optimize it later if it is really that slow. For nac3standalone, I think we can support simple cases first and leave this later, I don't think this is of high priority? Regarding performance issue, I think we can optimize it later if it is really that slow. For nac3standalone, I think we can support simple cases first and leave this later, I don't think this is of high priority?
sb10q
commented
Well the only thing we really need is module globals. If that can be done then something like the current code seems fine. Well the only thing we really need is module globals. If that can be done then something like the current code seems fine.
ychenfo
commented
Ok I will add module globals support on top of the current constant support Ok I will add module globals support on top of the current constant support
ychenfo
commented
Sorry, still one thing not very clear about this... To handle complex expressions(calling Sorry, still one thing not very clear about this...
To handle complex expressions(calling `eval` for now? Also, expressions like `Module.T` which refers to things defined in another module imported) as default value, should we add a function in symbol resolver, which accepts a `ast::Expr` and return a `SymbolValue`?
sb10q
commented
Let's not call
Let's not call ``eval`` and let's keep the more restricted approach. Keep the current mechanism and simply add support for being able to use globals e.g.:
```python
FOO = 1234
class XXX:
def yyy(self, z: int32 = FOO):
...
```
ychenfo
commented
I have added the basic module primitive/tuple global default parameter in the new commit. No I have added the basic module primitive/tuple global default parameter in the new commit.
No `eval` is used, only simple variable name is supported, but still involve some changes in symbol resolver(adding a function in symbol resolver which accepts a `ast::Expr` and return a `SymbolValue`). If I do not miss anything, I do not think there is another way to workaround this without changing symbol resolver... since in nac3artiq the core compiler does not know anything about the module globals if symbol resolver does not provide any information to it. Please have a review, thanks!
sb10q
commented
LGTM LGTM
|
||||
} else {
|
||||
Some("tuple".to_string())
|
||||
}
|
||||
}
|
||||
};
|
||||
if let Some(found) = res {
|
||||
Err(format!(
|
||||
"incompatible default parameter type, expect {}, found {}",
|
||||
ty.stringify(unifier),
|
||||
found
|
||||
))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_parameter_default_value(default: &ast::Expr, resolver: &(dyn SymbolResolver + Send + Sync)) -> Result<SymbolValue, String> {
|
||||
|
|
Should we just accept int32? int64 is handled in the call part, e.g.
int64(1234)
.this is handled in the new commit below