csp icon indicating copy to clipboard operation
csp copied to clipboard

Node parser fails to recognize renamed `ts` inputs

Open AdamGlustein opened this issue 1 year ago • 3 comments

Describe the bug

Baskets in csp are not iterable, but should be.

To Reproduce

This code will raise an error:

import csp
from csp import ts

@csp.node
def n(values: [ts[int]]):
    for val in values:
        if csp.ticked(val):
            pass
 
>> CspParseError: unrecognized input 'val'

This code will not:

import csp
from csp import ts

@csp.node
def n(values: [ts[int]]):
    for i in range(len(values)):
        if csp.ticked(values[i]):
            pass

Similarly for dictbaskets:

import csp
from csp import ts

@csp.node
def n(values: {str: ts[int]}):
    for key, val in values.items():
        if csp.ticked(val):
            pass

>> CspParseError: unrecognized input 'val'
import csp
from csp import ts

@csp.node
def n(values: {str: ts[int]}):
    for key, val in values.items():
        if csp.ticked(values[key]):
            pass

Expected behavior

Both of the snippets given above (for list/dictbaskets) should be equivalent.

Error Message

Runtime Environment

0.0.4 3.8.12 (default, Apr 7 2022, 17:33:24) [GCC 11.2.0] linux

Additional context

AdamGlustein avatar May 29 '24 18:05 AdamGlustein

The real issue is that the node parser is failing to recognize a time-series if its renamed. This code snippet will also fail for the exact same reason:

@csp.node
def n(x: ts[int]):
    y = x
    if csp.ticked(y):
        pass

AdamGlustein avatar Jul 12 '24 19:07 AdamGlustein

Thats right, because ts inputs have this sort of duality. They can act as ts inputs ( which can be passed to csp.xxx() methods ), or they can be accessed as the current value ( ie x + y ) when iterating there, its accessing by value and then cant be passed to csp methods

robambalu avatar Jul 12 '24 20:07 robambalu

We can maintain the duality of an input type and value during iteration so that for val in values works the exact same as for i in range(len(values)). We just need to ast parse the loop properly and update the input map.

I think it's feasible to implement, but maybe I'm missing some of the complexity. The error message here is pretty cryptic too.

AdamGlustein avatar Jul 12 '24 20:07 AdamGlustein