futhark icon indicating copy to clipboard operation
futhark copied to clipboard

Shorthand syntax for in-place record updates

Open jarble opened this issue 3 years ago • 8 comments

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?

jarble avatar Oct 13 '20 17:10 jarble

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.

athas avatar Oct 13 '20 17:10 athas

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.

Ulrik-dk avatar May 25 '22 19:05 Ulrik-dk

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.

athas avatar May 25 '22 21:05 athas

That would be very appreciated (by me).

Ulrik-dk avatar May 25 '22 21:05 Ulrik-dk

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.

Ulrik-dk avatar May 25 '22 21:05 Ulrik-dk

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.

athas avatar May 26 '22 08:05 athas

I'm completely unfamiliar with such functions. Has it been done before in futhark? Maybe you can elaborate a bit.

Ulrik-dk avatar May 30 '22 13:05 Ulrik-dk

This tutorial for Haskell seems to get the point across although it goes much further than you'd need to in Futhark.

athas avatar May 30 '22 14:05 athas