csp icon indicating copy to clipboard operation
csp copied to clipboard

Casting of ts[int] to ts[float] as part of type checking will mutate input baskets in-place, which can lead to downstream errors.

Open ptomecek opened this issue 1 year ago • 0 comments

Describe the bug csp mutates dict/list baskets of ints when casting to float (instead of making a copy when casting is needed). This can cause errors in downstream code.

To Reproduce

@csp.graph
def foo(x: Dict[str, ts[float]], y: List[ts[float]]) -> ts[bool]:
    return csp.const(True)
x = {"a": csp.const(1)}
y = [csp.const(1)]
z = foo(x, y)
# At this stage, both x and y will have been replaced by ts[float], which is not what downstream code might expect, i.e. 
try:
    csp.cast_int_to_float(x["a"])
except TypeError:
    print("This should not happen")
try:
    csp.cast_int_to_float(y[0])
except TypeError:
    print("This should not happen either")    

Expected behavior The input baskets should be left as-is, and the type checking/casting code should make it's own copy of the basket to mutate

Error Message

Runtime Environment

0.0.2 3.11.7 | packaged by conda-forge | (main, Dec 23 2023, 14:43:09) [GCC 12.3.0] linux

Additional context A functional language should not mutate its input arguments

ptomecek avatar Apr 02 '24 00:04 ptomecek