google-research / dex-lang

Research language for array processing in the Haskell/ML family
BSD 3-Clause "New" or "Revised" License
1.58k stars 107 forks source link

Should functions have an `Add` instance? #1230

Closed axch closed 1 year ago

axch commented 1 year ago

Pro yes: It's well-defined, and easy to implement. (And status quo is they do, but it's almost completely unused in the corpus.)

Pro no: Such an instance would violate the implicit contract that + performs work and produces a result that's the same size as its inputs.

Pro no: It also arguably wouldn't be a first-class instance, because functions aren't fully first-class. In particular, functions cannot be stored in references, so sum, cumsum, and so on cannot work on (arrays of) them anyway.

This decision is topical with the upcoming addition of the Data constraint (#1150) -- if we decide that functions do not need Add, then we can make Add require Data and save the latter annotation on sum, cumsum, etc.

Thoughts? Discuss!

axch commented 1 year ago

The same question applies to Eq and Ix, though possibly with a different answer, because functions do not have useful a Eq instance anyway, and while a functional type could be given an Ix instance, doing so feels artificial.

dougalm commented 1 year ago

Let's have Add require Data, and likewise for Eq and Ix. If we change our minds we can always make them more general, but going in the other direction is hard.