cucapra / dahlia

Time-sensitive affine types for predictable hardware generation
https://capra.cs.cornell.edu/dahlia
MIT License
130 stars 8 forks source link

Support reverse range iteration #358

Closed rachitnigam closed 3 years ago

rachitnigam commented 3 years ago

Dahlia currently does not allow for decrementing loops:

for (let i = 10..0) { ... }

There is no deep reason for this. I think all the guarantees w.r.t of parallelization will still hold if the iteration domain is flipped. @sampsyo @sgpthomas sound reasonable?

One funky to figure out which bound is inclusive and which is exclusive. For the above program, if we're iterating on an array, we'd like the range to be [0, 10) which is weird because in the incrementing case, the first point is inclusive while the second one is exclusive. Are these semantics confusing? Should we use a different syntax?

sampsyo commented 3 years ago

Indeed; I can't think of a parallelism issue here!

As for syntax, it might be worth checking out what other languages do… for example, Rust doesn't have a built-in way to do this (you apparently need to construct your own Range from scratch or use (0..10).rev()), and Swift seems to be very similar. I agree that the inclusiveness of 10..0 could be super confusing… I kinda think the only reasonable choice is that it would be include/exclude the same elements (i.e., it would be like (0..10).rev() in Rust).