stan icon indicating copy to clipboard operation
stan copied to clipboard

compiler allows ragged array expressions

Open mitzimorris opened this issue 6 years ago • 2 comments

Summary:

it's possible to create ragged arrays using array expressions and pass them in to user-defined functions. this shouldn't be allowed until we have proper support for ragged arrays. (reported on discourse: https://discourse.mc-stan.org/t/ragged-array-expressions/5213)

Description:

Here is an example of a minimal program which creates a ragged array:

functions {
  void foo(vector [] VS) {
    print("foo");
  }
}
transformed data {
  vector[2] A = [1.0, 2.0]';
  vector[3] B = [3.0, 4.0, 5.0]';
  vector[4] C = [6.0, 7.0, 8.0, 9.0]';
  foo( { A, B, C });
}

Reproducible Steps:

The above program compiles and runs. It shouldn't.

Additional Information:

generated c++ code:

   try {
            // initialize data block variables from context__

            // initialize transformed data variables
            current_statement_begin__ = 7;
            validate_non_negative_index("A", "2", 2);
            A = Eigen::Matrix<double, Eigen::Dynamic, 1>(2);
            stan::math::assign(A,transpose(stan::math::to_row_vector(stan::math::array_builder<double >().add(1.0).add(2.0).array())));

            current_statement_begin__ = 8;
            validate_non_negative_index("B", "3", 3);
            B = Eigen::Matrix<double, Eigen::Dynamic, 1>(3);
            stan::math::assign(B,transpose(stan::math::to_row_vector(stan::math::array_builder<double >().add(3.0).add(4.0).add(5.0).array())));

            current_statement_begin__ = 9;
            validate_non_negative_index("C", "4", 4);
            C = Eigen::Matrix<double, Eigen::Dynamic, 1>(4);
            stan::math::assign(C,transpose(stan::math::to_row_vector(stan::math::array_builder<double >().add(6.0).add(7.0).add(8.0).add(9.0).array())));

            // execute transformed data statements
            current_statement_begin__ = 10;
            foo(static_cast<std::vector<Eigen::Matrix<double, Eigen::Dynamic, 1> > >(stan::math::array_builder<Eigen::Matrix<double, Eigen::Dynamic, 1> >().add(A).add(B).add(C).array()), pstream__);

Current Version:

v2.18.0

mitzimorris avatar Oct 08 '18 21:10 mitzimorris

I just drove myself crazy trying to debug a problem in my code that was obscured by this. Trying to quad_form two square matrices that were instantiated as the same size, and getting size mismatch warnings -- because somewhere along the way I assigned a matrix of the wrong size and this was silently accepted. Just mentioning because I think I remember seeing the argument that this bug wasn't a real problem.

cdriveraus avatar Sep 11 '20 14:09 cdriveraus

thanks for reporting. I think you're right that there was discussion about whether or not this is a bug or feature. (it's a bug). at this point, stanc2 compiler isn't going to be fixed. not sure what the status of this in stanc3 is. would you mind opening an issue on the stanc3 repo reporting what you did and what would have been useful.

mitzimorris avatar Sep 11 '20 20:09 mitzimorris

Closing in favor of https://github.com/stan-dev/stan/issues/3150

WardBrian avatar Mar 20 '23 18:03 WardBrian