futhark
futhark copied to clipboard
Shorthand syntax for in-place record updates
Futhark has a convenient shorthand syntax for in-place updates, but this syntax doesn't work with records:
let record_example a b =
let var = {a,b} in
let var.a = var.a + 1 -- parse error
in var.a+var.b
There's another way to do this, but it's more verbose:
let record_example a b =
let var = {a,b} in
let var = var with a = var.a + 1
in var.a+var.b
Would it be possible to make this "shorthand" syntax work with records as well as arrays?
That may not be a bad idea. I'm surprised nobody has proposed it before. I don't think there are any ambiguities or great technical hurdles to overcome.
I'd like to second this proposal, and extend it with a request for the ability to update arrays in records. This is currently impossible - the solution given here [#1560] is to create a new record. That quickly becomes unwieldy, and difficult to read, for large records.
That's a bit more intricate. A good starting point would be to implement expressions such as r with a.b.c[i] = x
. For simplicity, we could start by requiring the index to be at the end.
That would be very appreciated (by me).
I have some records, the definitions of which are ~400 lines long, and that have arrays which need in-place updating, so de- and reconstructing these records is not feasable.
I think a record of that size will give you other problems. I don't think anyone will be working on this feature in the short term, but I suggest writing lens-like accessor functions manually to make the composition of updates more sane.
I'm completely unfamiliar with such functions. Has it been done before in futhark? Maybe you can elaborate a bit.
This tutorial for Haskell seems to get the point across although it goes much further than you'd need to in Futhark.