Add tuples to the language#1100
Conversation
Continue work from Ryan PR Remainder of changes from Ryan's pr Small fixes Match existing test output
Projection is a bit more technical, but it mainly creates further distance from 'Indexed' for easier reading
|
@SteveBronder I'm handing this off to you. If you run into any questions feel free to ping/email me as I've gotten quite familiar with the state of things. @rok-cesnovar once we're in the freeze you may want to take a look at what is needed to support tuples in The first thing that needs to happen is work in the code gen for transformations/IO - currently we hit |
|
Any chance this implementation will support tuples of user-defined functions? Not a request if it's a lot of work or anything, just curious |
|
Do you mean as a return or argument type? Yes |
|
As an argument type, so in stan pseudo-code: I'm prototyping an approach for user-defined gradients, and it assumes that a tuple of arguments and a tuple of gradient functions will be passed: real value_fun(real x, real y) {
return x * y;
}
real dx_fun(real x, real y) {
return y;
}
real dy_fun(real x, real y) {
return x;
}
user_gradients(make_tuple(a, b), value_fun, make_tuple(dx_fun, dy_fun));But if a tuple of functions/functors isn't feasible, then I can make it variadic and split the functions out in the c++ |
|
I don’t think that would work completely out of the box, but it would be a minor change to generate a tuple of the functor structs in C++. I see no reason why such a thing wouldn’t be possible for a library function. For user defined functions, that signature would of course be impossible to define until we have a way of writing down a function’s type inside Stan I have some more questions about your thinking on this but I’ll ask them on slack/not in this PR |
|
Is that something we'd desire being possible? I think we would need to have the requirement that if any container is used in the constraint, rightmost index must be included (e.g., you could make lowers2 |
|
The remaining uncertainties (marked |
Probably not but since it wasn't in the design doc it's hard know for sure. If the current requirement were that if any container is used in the constraint, all indexes must be included, that would be consistent with either as a future extension and also currently allow something semantically equivalent. |
|
I'd be happy to open a PR revising the design doc if you think that is necessary for this PR to proceed. I think that the current implementation does not prevent us from doing (... something semantically equivalent to ..) what you've described in the future. The eventual situation would end up looking something like a two-part rule:
Regardless from whether the design would allow for it, I still have some reservations about actually allowing different tuples in the same array to have different internal constraints, but those can be saved for later discussions of that actual feature |
|
Yes, I think updating the design doc is the best way to move forward. This argument has been a bit weird because I think we both agree what the eventual future is going to be. Specifically, it is one where all of the following declarations are accepted: data {
int N, M, K;
real L;
array[K] real Lk;
array[M,K] real Lmk;
array[N,M,K] real Lnmk;
}
parameters {
array[N,M] tuple(array[K] real<lower=L>, real) T;
array[N,M] tuple(array[K] real<lower=Lk>, real) Tk;
array[N,M] tuple(array[K] real<lower=Lmk>, real) Tmk;
array[N,M] tuple(array[K] real<lower=Lnmk>, real) Tnmk;
}And we also agree that this is not something to implement right now but a plan for the far future, likely requiring a new design doc, or might never even happen. Where we disagree is what the first concrete step toward that hypothetical future should be.
|
|
Design doc changes stan-dev/design-docs#50 I think your summary is fair. I will admit that, besides thinking that it is the right thing to do, the fact that homogeneous elements is easiest to implement (even easier than if we only allowed scalars at first) does weigh in to breaking the tie between the different ideas |
|
Over this past weekend I did some fuzzing of this PR for the equivalent of 8 days (4 with —O1, 4 without). It found four crashes, 3 of which were the parsing issue fixed in d59e0e3 and the last was unrelated (#1336). This isn’t the same as testing the generated C++ compiling obvious, but it does seem like we’ve resolved most/all of the internal exceptions which could have occurred (@nhuurre had already spotted a bunch manually before this) |
SteveBronder
left a comment
There was a problem hiding this comment.
C++ Code changes look good except for a few little spots.
SteveBronder
left a comment
There was a problem hiding this comment.
Ty! I looked over the rest of the code and read a lot of the C++ output and that looks good to me. @nhuurre unless you have any objections I think we are ready to merge!
This is a port of the changes @rybern made in #675 to
master. I tried to first port this while preserving his comments, etc, and then made some changes myself to reflect the new status of things.This will close stan-dev/stan#2431
Needs
TUPLEcomments - AllSubmission Checklist
Release notes
Add Tuples to the language
Copyright and Licensing
By submitting this pull request, the copyright holder is agreeing to
license the submitted work under the BSD 3-clause license (https://opensource.org/licenses/BSD-3-Clause)