Added recent discussions #16

Merged
sb10q merged 3 commits from pca into master 2021-06-15 10:00:44 +08:00
1 changed files with 39 additions and 3 deletions

View File

@ -47,6 +47,24 @@ def fail_write() -> None:
self.a = a
self.b = b
```
* Instance variables not used would be warned by the compiler, except for those
preceded by the pseudocomment `# nac3:no_warn_unused`. (#9) The comment can
either be placed on top of the variable or next to the variable. Example:
```python
class Foo:
# nac3:no_warn_unused
a: int
b: int # nac3:no_warn_unused
def __init__(self):
pass
```
* Use-before-define:
* Host-only constructor: Error if a certain field `f` is
used in other kernel methods but missing from the object constructed in the
host.
* `@portable`/`@kernel` constructor: Error if a certain
field `f` is used in other kernel methods but not defined in the
constructor, or used in the constructor before definition.
* Three types of instance variables: (Issue #5)
* Host only variables: Do not add type annotation for it in the class.
* Kernel only variables: Denoted with type `Kernel[T]`.
@ -140,11 +158,18 @@ class Foo(EnvExperiment):
* Type variables are invariant, same as the default in Python. We disallow
covariant or contravariant. The compiler should mark as error if it encounters
a type variable used in kernel that is declared covariant or contravariant.
* Code region protected by a type check, such as `if type(x) == int:`, would
* A custom function `is_type(x, T)` would be provided to check whether `x` is an
instance of `T`, other methods like `type(x) == int` or `isinstance(x, int)`
would not compile. The function would be able to check generic types for
`list` and `tuple`. When running on the host, user can specify whether to use
a debug mode checking (recursively check all elements, which would be slower
for large lists) or performance mode which only check the first element of
each list. (#15)
* Code region protected by a type check, such as `if is_type(x, int):`, would
treat `x` as `int`, similar to how [typescript type guard](https://www.typescripttutorial.net/typescript-tutorial/typescript-type-guards/) works.
```python
def add1(x: Union[int, bool]) -> int:
if type(x) == int:
if is_type(x, int):
# x is int
return x + 1
else:
@ -152,11 +177,22 @@ class Foo(EnvExperiment):
return 2 if x else 1
```
* Generics are instantiated at compile time, all the type checks like
`type(x) == int` would be evaluated as constants. Type checks are not allowed
`is_type(x, int)` would be evaluated as constants. Type checks are not allowed
in area outside generics.
* Type variable cannot occur alone in the result type, i.e. must be bound to the
input parameters.
## For loop unrolling (#12)
A pseudocomment can be used for unrolling for loops that iterates a fixed amount
of time. This can be used for iterating over inhomogeneous tuples. Example:
```python
params = (1, 1.5, "foo")
# nac3:unroll
for p in params:
print(p)
```
## Lifetime
Probably need more discussions...