semantic_actions_def.cpp
Go to the documentation of this file.
1 #ifndef STAN_LANG_GRAMMARS_SEMANTIC_ACTIONS_DEF_CPP
2 #define STAN_LANG_GRAMMARS_SEMANTIC_ACTIONS_DEF_CPP
3 
5 #include <stan/lang/ast.hpp>
8 #include <boost/algorithm/string.hpp>
9 #include <boost/format.hpp>
10 #include <boost/spirit/include/qi.hpp>
11 #include <boost/variant/apply_visitor.hpp>
12 #include <boost/variant/recursive_variant.hpp>
13 #include <cstddef>
14 #include <limits>
15 #include <climits>
16 #include <iomanip>
17 #include <iostream>
18 #include <map>
19 #include <set>
20 #include <stdexcept>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 namespace stan {
26 
27  namespace lang {
28 
29  /**
30  * Add namespace qualifier `stan::math::` or `std::` to function names
31  * in order to avoid ambiguities for functions in the Stan language which
32  * are also defined in c and/or other libraries that some compilers (gcc)
33  * bring into the top-level namespace.
34  *
35  * @param[in, out] f Function to qualify.
36  */
38  if ((f.name_ == "max" || f.name_ == "min")
39  && f.args_.size() == 2
40  && f.args_[0].expression_type().is_primitive_int()
41  && f.args_[1].expression_type().is_primitive_int()) {
42  f.name_ = "std::" + f.name_;
43  return;
44  }
45 
46  if (f.name_ == "ceil"
47  && f.args_[0].expression_type().is_primitive_int()) {
48  f.name_ = "std::" + f.name_;
49  return;
50  }
51 
52  if ((f.args_.size() == 0
53  && (f.name_ == "e" || f.name_ == "pi"
54  || f.name_ == "log2" || f.name_ == "log10"
55  || f.name_ == "sqrt2" || f.name_ == "not_a_number"
56  || f.name_ == "positive_infinity"
57  || f.name_ == "negative_infinity"
58  || f.name_ == "machine_precision"))
59  || (f.args_.size() == 1
60  && (f.name_ == "abs" || f.name_ == "acos"|| f.name_ == "acosh"
61  || f.name_ == "asin"|| f.name_ == "asinh"
62  || f.name_ == "atan" || f.name_ == "atan2"
63  || f.name_ == "atanh" || f.name_ == "cbrt"
64  || f.name_ == "ceil" || f.name_ == "cos"|| f.name_ == "cosh"
65  || f.name_ == "erf" || f.name_ == "erfc" || f.name_ == "exp"
66  || f.name_ == "exp2" || f.name_ == "expm1"
67  || f.name_ == "fabs" || f.name_ == "floor"
68  || f.name_ == "lgamma" || f.name_ == "log"
69  || f.name_ == "log1p" || f.name_ == "log2"
70  || f.name_ == "log10" || f.name_ == "round"
71  || f.name_ == "sin" || f.name_ == "sinh"
72  || f.name_ == "sqrt" || f.name_ == "tan" || f.name_ == "tanh"
73  || f.name_ == "tgamma" || f.name_ == "trunc"))
74  || (f.args_.size() == 2
75  && (f.name_ == "fdim" || f.name_ == "fmax" || f.name_ == "fmin"
76  || f.name_ == "hypot"))
77  || (f.args_.size() == 3 && f.name_ == "fma"))
78  f.name_ = "stan::math::" + f.name_;
79  }
80 
81  bool has_prob_suffix(const std::string& s) {
82  return ends_with("_lpdf", s) || ends_with("_lpmf", s)
83  || ends_with("_lcdf", s) || ends_with("_lccdf", s);
84  }
85 
87  return ends_with("_lp", s) || ends_with("_rng", s);
88  }
89 
90  void replace_suffix(const std::string& old_suffix,
91  const std::string& new_suffix, fun& f) {
92  if (!ends_with(old_suffix, f.name_)) return;
93  f.name_ = f.name_.substr(0, f.name_.size() - old_suffix.size())
94  + new_suffix;
95  }
96 
97  bool deprecate_fun(const std::string& old_name, const std::string& new_name,
98  fun& f, std::ostream& msgs) {
99  if (f.name_ != old_name) return false;
100  f.original_name_ = f.name_;
101  f.name_ = new_name;
102  msgs << "Warning: Function name '" << old_name << "' is deprecated"
103  << " and will be removed in a later release; please replace"
104  << " with '" << new_name << "'" << std::endl;
105  return true;
106  }
107 
108  bool deprecate_suffix(const std::string& deprecated_suffix,
109  const std::string& replacement, fun& f,
110  std::ostream& msgs) {
111  if (!ends_with(deprecated_suffix, f.name_)) return false;
112  msgs << "Warning: Deprecated function '" << f.name_ << "';"
113  << " please replace suffix '" << deprecated_suffix
114  << "' with " << replacement << std::endl;
115  return true;
116  }
117 
118  void set_fun_type(fun& fun, std::ostream& error_msgs) {
119  std::vector<expr_type> arg_types;
120  for (size_t i = 0; i < fun.args_.size(); ++i)
121  arg_types.push_back(fun.args_[i].expression_type());
123  .get_result_type(fun.name_, arg_types, error_msgs);
124  }
125 
126  int num_dimss(std::vector<std::vector<stan::lang::expression> >& dimss) {
127  int sum = 0;
128  for (size_t i = 0; i < dimss.size(); ++i)
129  sum += dimss[i].size();
130  return sum;
131  }
132 
133  bool is_double_return(const std::string& function_name,
134  const std::vector<expr_type>& arg_types,
135  std::ostream& error_msgs) {
137  .get_result_type(function_name, arg_types, error_msgs, true)
139  }
140 
141  bool is_univariate(const expr_type& et) {
142  return et.num_dims_ == 0
143  && (et.base_type_.is_int_type()
144  || et.base_type_.is_double_type());
145  }
146 
147 
148  bool can_assign_to_lhs_var(const std::string& lhs_var_name,
149  const scope& var_scope,
150  const variable_map& vm,
151  std::ostream& error_msgs) {
152  if (lhs_var_name == std::string("lp__")) {
153  error_msgs << std::endl
154  << "Error (fatal): Use of lp__ is no longer supported."
155  << std::endl
156  << " Use target += ... statement to increment log density."
157  << std::endl;
158  return false;
159  }
160  if (!vm.exists(lhs_var_name)) {
161  error_msgs << "Unknown variable in assignment"
162  << "; lhs variable=" << lhs_var_name
163  << std::endl;
164  return false;
165  }
166  scope lhs_origin = vm.get_scope(lhs_var_name);
167  // enforce constancy of loop variables
168  if (lhs_origin.program_block() == loop_identifier_origin) {
169  error_msgs << "Loop variable " << lhs_var_name
170  << " cannot be used on left side of assignment statement."
171  << std::endl;
172  return false;
173  }
174  // enforce constancy of function args
175  if (!lhs_origin.is_local() && lhs_origin.fun()) {
176  error_msgs << "Cannot assign to function argument variables."
177  << std::endl
178  << "Use local variables instead."
179  << std::endl;
180  return false;
181  }
182  if (lhs_origin.program_block() != var_scope.program_block()) {
183  error_msgs << "Cannot assign to variable outside of declaration block"
184  << "; left-hand-side variable origin=";
185  print_scope(error_msgs, lhs_origin);
186  error_msgs << std::endl;
187  return false;
188  }
189  return true;
190  }
191 
193  const variable_dims& var_dims) {
194  size_t var_num_dims = var_type.dims_.size();
195  size_t num_index_dims = var_dims.dims_.size();
196  return infer_type_indexing(var_type.base_type_,
197  var_num_dims,
198  num_index_dims);
199  }
200 
201  bool has_same_shape(const expr_type& lhs_type,
202  const expression& rhs_expr,
203  const std::string& name,
204  const std::string& stmt_type,
205  std::ostream& error_msgs) {
206  if (lhs_type.num_dims_ != rhs_expr.expression_type().num_dims_) {
207  error_msgs << "Dimension mismatch in "
208  << stmt_type
209  << "; variable name = "
210  << name
211  << ", num dimensions given = "
212  << lhs_type.num_dims_
213  << "; right-hand side dimensions = "
214  << rhs_expr.expression_type().num_dims_
215  << std::endl;
216  return false;
217  }
218  base_expr_type lhs_base_type = lhs_type.base_type_;
219  base_expr_type rhs_base_type = rhs_expr.expression_type().base_type_;
220  // allow int -> double promotion, even in arrays
221  bool types_compatible =
222  (lhs_base_type == rhs_base_type
223  || (lhs_base_type.is_double_type() && rhs_base_type.is_int_type()));
224  if (!types_compatible) {
225  error_msgs << "Base type mismatch in "
226  << stmt_type
227  << "; variable name = "
228  << name
229  << ", type = ";
230  write_base_expr_type(error_msgs, lhs_base_type);
231  error_msgs << "; right-hand side type=";
232  write_base_expr_type(error_msgs, rhs_base_type);
233  error_msgs << std::endl;
234  return false;
235  }
236  return true;
237  }
238 
239 
240  // //////////////////////////////////
241  // *** functors for grammar rules ***
242  // //////////////////////////////////
243 
245  bool& pass,
246  std::stringstream& error_msgs)
247  const {
249  && !expr.expression_type().is_primitive_int()) {
250  error_msgs << "Expression denoting real required; found type="
251  << expr.expression_type() << std::endl;
252  pass = false;
253  return;
254  }
255  pass = true;
256  }
257  boost::phoenix::function<validate_double_expr> validate_double_expr_f;
258 
259  template <typename L, typename R>
260  void assign_lhs::operator()(L& lhs, const R& rhs) const {
261  lhs = rhs;
262  }
263  boost::phoenix::function<assign_lhs> assign_lhs_f;
264 
265  template void assign_lhs::operator()(expression&, const expression&) const;
266  template void assign_lhs::operator()(expression&, const double_literal&)
267  const;
268  template void assign_lhs::operator()(expression&, const int_literal&) const;
269  template void assign_lhs::operator()(expression&, const integrate_ode&)
270  const;
271  template void assign_lhs::operator()(expression&,
272  const integrate_ode_control&)
273  const;
274  template void assign_lhs::operator()(expression&, const algebra_solver&)
275  const;
276  template void assign_lhs::operator()(expression&,
277  const algebra_solver_control&)
278  const;
279  template void assign_lhs::operator()(expression&,
280  const map_rect&)
281  const;
282  template void assign_lhs::operator()(array_expr&,
283  const array_expr&) const;
284  template void assign_lhs::operator()(matrix_expr&,
285  const matrix_expr&) const;
287  const row_vector_expr&) const;
288  template void assign_lhs::operator()(int&, const int&) const;
289  template void assign_lhs::operator()(size_t&, const size_t&) const;
290  template void assign_lhs::operator()(statement&, const statement&) const;
291  template void assign_lhs::operator()(std::vector<var_decl>&,
292  const std::vector<var_decl>&) const;
293  template void assign_lhs::operator()(std::vector<idx>&,
294  const std::vector<idx>&) const;
295  template void assign_lhs::operator()(
296  std::vector<std::vector<expression> >&,
297  const std::vector<std::vector<expression> >&) const;
298  template void assign_lhs::operator()(fun&, const fun&) const;
299  template void assign_lhs::operator()(variable&, const variable&) const;
300  template void assign_lhs::operator()(variable_dims&,
301  const variable_dims&) const;
302  template void assign_lhs::operator()(base_expr_type&,
303  const base_expr_type&) const;
304 
305  void validate_expr_type3::operator()(const expression& expr, bool& pass,
306  std::ostream& error_msgs) const {
307  pass = !expr.expression_type().is_ill_formed();
308  if (!pass)
309  error_msgs << "Expression is ill formed" << std::endl;
310  }
311  boost::phoenix::function<validate_expr_type3> validate_expr_type3_f;
312 
313 
315  bool& pass) const {
316  pass = has_prob_suffix(s);
317  }
318  boost::phoenix::function<is_prob_fun> is_prob_fun_f;
319 
321  std::ostream& error_msgs) const {
322  if (expr1.expression_type().is_primitive()
323  && expr2.expression_type().is_primitive()) {
324  expr1 += expr2;
325  return;
326  }
327  std::vector<expression> args;
328  args.push_back(expr1);
329  args.push_back(expr2);
330  fun f("add", args);
331  set_fun_type(f, error_msgs);
332  expr1 = expression(f);
333  }
334  boost::phoenix::function<addition_expr3> addition3_f;
335 
337  const expression& expr2,
338  std::ostream& error_msgs) const {
339  if (expr1.expression_type().is_primitive()
340  && expr2.expression_type().is_primitive()) {
341  expr1 -= expr2;
342  return;
343  }
344  std::vector<expression> args;
345  args.push_back(expr1);
346  args.push_back(expr2);
347  fun f("subtract", args);
348  set_fun_type(f, error_msgs);
349  expr1 = expression(f);
350  }
351  boost::phoenix::function<subtraction_expr3> subtraction3_f;
352 
353  void increment_size_t::operator()(size_t& lhs) const {
354  ++lhs;
355  }
356  boost::phoenix::function<increment_size_t> increment_size_t_f;
357 
358 
360  const scope& var_scope,
361  bool& pass,
362  const variable_map& var_map,
363  std::ostream& error_msgs) const {
364  expr_type cond_type = conditional_op.cond_.expression_type();
365  if (!cond_type.is_primitive_int()) {
366  error_msgs << "Condition in ternary expression must be"
367  << " primitive int or real;"
368  << " found type=" << cond_type
369  << std::endl;
370  pass = false;
371  return;
372  }
373 
374  expr_type true_val_type = conditional_op.true_val_.expression_type();
375  base_expr_type true_val_base_type = true_val_type.base_type_;
376  expr_type false_val_type = conditional_op.false_val_.expression_type();
377  base_expr_type false_val_base_type = false_val_type.base_type_;
378 
379  bool types_compatible
380  = (true_val_type == false_val_type)
381  || (true_val_type.is_primitive() && false_val_type.is_primitive()
382  && (true_val_base_type == false_val_base_type
383  || (true_val_base_type.is_double_type()
384  && false_val_base_type.is_int_type())
385  || (true_val_base_type.is_int_type()
386  && false_val_base_type.is_double_type())));
387 
388  if (!types_compatible) {
389  error_msgs << "Base type mismatch in ternary expression,"
390  << " expression when true is: ";
391  write_base_expr_type(error_msgs, true_val_base_type);
392  error_msgs << "; expression when false is: ";
393  write_base_expr_type(error_msgs, false_val_base_type);
394  error_msgs << std::endl;
395  pass = false;
396  return;
397  }
398 
399  if (true_val_type.is_primitive())
400  conditional_op.type_
401  = (true_val_base_type == false_val_base_type)
402  ? true_val_base_type
403  : double_type();
404  else
405  conditional_op.type_ = true_val_type;
406 
407  conditional_op.has_var_ = has_var(conditional_op, var_map);
408  conditional_op.scope_ = var_scope;
409  pass = true;
410  }
411  boost::phoenix::function<validate_conditional_op>
413 
415  const std::string& op,
416  const std::string& fun_name,
417  std::ostream& error_msgs) const {
418  if (!expr1.expression_type().is_primitive()
419  || !expr2.expression_type().is_primitive()) {
420  error_msgs << "Binary infix operator " << op
421  << " with functional interpretation " << fun_name
422  << " requires arguments or primitive type (int or real)"
423  << ", found left type=" << expr1.expression_type()
424  << ", right arg type=" << expr2.expression_type()
425  << "; "
426  << std::endl;
427  }
428  std::vector<expression> args;
429  args.push_back(expr1);
430  args.push_back(expr2);
431  fun f(fun_name, args);
432  set_fun_type(f, error_msgs);
433  expr1 = expression(f);
434  }
435  boost::phoenix::function<binary_op_expr> binary_op_f;
436 
438  bool& pass,
439  std::ostream& error_msgs) const {
440  pass = !arg_type.is_void();
441  if (!pass)
442  error_msgs << "Functions cannot contain void argument types; "
443  << "found void argument."
444  << std::endl;
445  }
446  boost::phoenix::function<validate_non_void_arg_function>
448 
450  scope& var_scope, bool& pass,
451  std::ostream& error_msgs) const {
452  if (return_type.is_void() && return_type.num_dims() > 0) {
453  error_msgs << "Void return type may not have dimensions declared."
454  << std::endl;
455  pass = false;
456  return;
457  }
458  var_scope = return_type.is_void()
461  pass = true;
462  }
463  boost::phoenix::function<set_void_function> set_void_function_f;
464 
466  scope& var_scope) const {
467  if (ends_with("_lp", identifier)) {
468  var_scope = var_scope.void_fun()
471  } else if (ends_with("_rng", identifier)) {
472  var_scope = var_scope.void_fun()
475  } else {
476  var_scope = var_scope.void_fun()
479  }
480  }
481  boost::phoenix::function<set_allows_sampling_origin>
483 
485  std::set<std::pair<std::string,
486  function_signature_t> >& declared,
487  std::set<std::pair<std::string,
488  function_signature_t> >& defined,
489  std::ostream& error_msgs,
490  bool allow_undefined) const {
491  using std::set;
492  using std::string;
493  using std::pair;
494  typedef set<pair<string, function_signature_t> >::iterator iterator_t;
495  if (!allow_undefined) {
496  for (iterator_t it = declared.begin(); it != declared.end(); ++it) {
497  if (defined.find(*it) == defined.end()) {
498  error_msgs <<"Function declared, but not defined."
499  << " Function name=" << (*it).first
500  << std::endl;
501  pass = false;
502  return;
503  }
504  }
505  }
506  pass = true;
507  }
508  boost::phoenix::function<validate_declarations> validate_declarations_f;
509 
510 
511  bool fun_exists(const std::set<std::pair<std::string,
512  function_signature_t> >& existing,
513  const std::pair<std::string,
514  function_signature_t>& name_sig,
515  bool name_only = true) {
516  for (std::set<std::pair<std::string,
517  function_signature_t> >::const_iterator it
518  = existing.begin();
519  it != existing.end();
520  ++it) {
521  if (name_sig.first == (*it).first
522  && (name_only
523  || name_sig.second.second == (*it).second.second))
524  return true; // name and arg sequences match
525  }
526  return false;
527  }
528 
530  std::ostream& error_msgs) const {
531  if (has_prob_fun_suffix(fname)) {
532  std::string dist_name = strip_prob_fun_suffix(fname);
533  if (!fun_name_exists(fname) // catch redefines later avoid fwd
534  && (fun_name_exists(dist_name + "_lpdf")
535  || fun_name_exists(dist_name + "_lpmf")
536  || fun_name_exists(dist_name + "_log"))) {
537  error_msgs << "Parse Error. Probability function already defined"
538  << " for " << dist_name << std::endl;
539  pass = false;
540  return;
541  }
542  }
543  if (has_cdf_suffix(fname)) {
544  std::string dist_name = strip_cdf_suffix(fname);
545  if (fun_name_exists(dist_name + "_cdf_log")
546  || fun_name_exists(dist_name + "_lcdf")) {
547  error_msgs << " Parse Error. CDF already defined for "
548  << dist_name << std::endl;
549  pass = false;
550  return;
551  }
552  }
553  if (has_ccdf_suffix(fname)) {
554  std::string dist_name = strip_ccdf_suffix(fname);
555  if (fun_name_exists(dist_name + "_ccdf_log")
556  || fun_name_exists(dist_name + "_lccdf")) {
557  error_msgs << " Parse Error. CCDF already defined for "
558  << dist_name << std::endl;
559  pass = false;
560  return;
561  }
562  }
563  }
564  boost::phoenix::function<validate_prob_fun> validate_prob_fun_f;
565 
567  bool& pass,
568  std::set<std::pair<std::string, function_signature_t> >&
569  functions_declared,
570  std::set<std::pair<std::string, function_signature_t> >&
571  functions_defined,
572  std::ostream& error_msgs) const {
573  // build up representations
574  expr_type result_type(decl.return_type_.base_type_,
575  decl.return_type_.num_dims_);
576  std::vector<function_arg_type> arg_types;
577  for (size_t i = 0; i < decl.arg_decls_.size(); ++i)
578  arg_types.push_back(
579  function_arg_type(expr_type(decl.arg_decls_[i].arg_type_.base_type_,
580  decl.arg_decls_[i].arg_type_.num_dims_),
581  decl.arg_decls_[i].is_data_));
582 
583  function_signature_t sig(result_type, arg_types);
584  std::pair<std::string, function_signature_t> name_sig(decl.name_, sig);
585 
586  // check that not already declared if just declaration
587  if (decl.body_.is_no_op_statement()
588  && fun_exists(functions_declared, name_sig)) {
589  error_msgs << "Parse Error. Function already declared, name="
590  << decl.name_;
591  pass = false;
592  return;
593  }
594 
595  // check not already user defined
596  if (fun_exists(functions_defined, name_sig)) {
597  error_msgs << "Parse Error. Function already defined, name="
598  << decl.name_;
599  pass = false;
600  return;
601  }
602 
603  // check not already system defined
604  if (!fun_exists(functions_declared, name_sig)
606  error_msgs << "Parse Error. Function system defined, name="
607  << decl.name_;
608  pass = false;
609  return;
610  }
611 
612  // check argument qualifiers
613  if (!decl.body_.is_no_op_statement()) {
614  function_signature_t decl_sig =
616  if (!decl_sig.first.is_ill_formed()) {
617  for (size_t i = 0; i < arg_types.size(); ++i) {
618  if (decl_sig.second[i].expr_type_ != arg_types[i].expr_type_
619  || decl_sig.second[i].data_only_ != arg_types[i].data_only_) {
620  error_msgs << "Declaration doesn't match definition "
621  << "for function: "
622  << decl.name_ << " argument " << (i + 1)
623  << ": argument declared as "
624  << arg_types[i]
625  << ", defined as "
626  << decl_sig.second[i] << "." << std::endl;
627  pass = false;
628  return;
629  }
630  }
631  }
632  }
633 
634  if (ends_with("_lpdf", decl.name_)
635  && arg_types[0].expr_type_.base_type_.is_int_type()) {
636  error_msgs << "Parse Error. Probability density functions require"
637  << " real variates (first argument)."
638  << " Found type = " << arg_types[0] << std::endl;
639  pass = false;
640  return;
641  }
642  if (ends_with("_lpmf", decl.name_)
643  && !arg_types[0].expr_type_.base_type_.is_int_type()) {
644  error_msgs << "Parse Error. Probability mass functions require"
645  << " integer variates (first argument)."
646  << " Found type = " << arg_types[0] << std::endl;
647  pass = false;
648  return;
649  }
650 
651  // add declaration in local sets and in parser function sigs
652  if (functions_declared.find(name_sig) == functions_declared.end()) {
653  functions_declared.insert(name_sig);
655  .add(decl.name_, result_type, arg_types);
657  }
658 
659  // add as definition if there's a body
660  if (!decl.body_.is_no_op_statement())
661  functions_defined.insert(name_sig);
662  pass = true;
663  }
664  boost::phoenix::function<add_function_signature> add_function_signature_f;
665 
667  bool& pass,
668  std::ostream& error_msgs)
669  const {
670  if (!has_prob_fun_suffix(decl.name_))
671  return;
672  if (decl.arg_decls_.size() == 0) {
673  error_msgs << "Parse Error. Probability functions require"
674  << " at least one argument." << std::endl;
675  pass = false;
676  return;
677  }
678  expr_type variate_type = decl.arg_decls_[0].arg_type_;
679  if (ends_with("_lpdf", decl.name_)
680  && variate_type.base_type_.is_int_type()) {
681  error_msgs << "Parse Error. Probability density functions require"
682  << " real variates (first argument)."
683  << " Found type = " << variate_type << std::endl;
684  pass = false;
685  return;
686  }
687  if (ends_with("_lpmf", decl.name_)
688  && !variate_type.base_type_.is_int_type()) {
689  error_msgs << "Parse Error. Probability mass functions require"
690  << " integer variates (first argument)."
691  << " Found type = " << variate_type << std::endl;
692  pass = false;
693  return;
694  }
695  }
696  boost::phoenix::function<validate_pmf_pdf_variate>
698 
700  bool& pass,
701  std::ostream& error_msgs) const {
702  pass = decl.body_.is_no_op_statement()
704  error_msgs);
705  if (!pass) {
706  error_msgs << "Improper return in body of function." << std::endl;
707  return;
708  }
709 
710  if ((ends_with("_log", decl.name_)
711  || ends_with("_lpdf", decl.name_)
712  || ends_with("_lpmf", decl.name_)
713  || ends_with("_lcdf", decl.name_)
714  || ends_with("_lccdf", decl.name_))
715  && !decl.return_type_.is_primitive_double()) {
716  pass = false;
717  error_msgs << "Require real return type for probability functions"
718  << " ending in _log, _lpdf, _lpmf, _lcdf, or _lccdf."
719  << std::endl;
720  }
721  }
722  boost::phoenix::function<validate_return_type> validate_return_type_f;
723 
725  const {
726  var_scope = scope(var_scope.program_block(), true);
727  // TODO(morris): remove if params_r__ no longer used
728  vm.add("params_r__", base_var_decl(vector_type()), parameter_origin);
729  }
730  boost::phoenix::function<set_fun_params_scope> set_fun_params_scope_f;
731 
733  variable_map& vm) const {
734  vm.remove("params_r__");
735  for (size_t i = 0; i < decl.arg_decls_.size(); ++i)
736  vm.remove(decl.arg_decls_[i].name_);
737  }
738  boost::phoenix::function<unscope_variables> unscope_variables_f;
739 
740  void add_fun_var::operator()(arg_decl& decl, scope& scope, bool& pass,
741  variable_map& vm,
742  std::ostream& error_msgs) const {
743  if (vm.exists(decl.name_)) {
744  pass = false;
745  error_msgs << "Duplicate declaration of variable, name="
746  << decl.name_
747  << "; attempt to redeclare as function argument"
748  << "; original declaration as ";
749  print_scope(error_msgs, vm.get_scope(decl.name_));
750  error_msgs << std::endl;
751  return;
752  }
753  pass = true;
754  origin_block var_origin = scope.program_block();
755  if (var_origin == data_origin) {
757  pass = false;
758  error_msgs << "Data qualifier cannot be applied to int variable, "
759  << "name " << decl.name_ << ".";
760  error_msgs << std::endl;
761  return;
762  }
763  decl.is_data_ = true;
764  } else {
765  var_origin = function_argument_origin;
766  }
767  vm.add(decl.name_, decl.base_variable_declaration(),
768  var_origin);
769  }
770  boost::phoenix::function<add_fun_var> add_fun_var_f;
771 
772  // TODO(carpenter): seems redundant; see if it can be removed
774  val = omni_idx();
775  }
776  boost::phoenix::function<set_omni_idx> set_omni_idx_f;
777 
779  const {
780  pass = e.expression_type().is_primitive_int();
781  }
782  boost::phoenix::function<validate_int_expr_silent>
784 
786  bool& pass,
787  std::ostream& error_msgs)
788  const {
789  if (!e.expression_type().type().is_int_type()) {
790  error_msgs << "Indexes must be expressions of integer type."
791  << " found type = ";
792  write_base_expr_type(error_msgs, e.expression_type().type());
793  error_msgs << '.' << std::endl;
794  }
795  pass = e.expression_type().is_primitive_int();
796  }
797  boost::phoenix::function<validate_int_expression_warn>
799 
800 
802  std::ostream& error_msgs) const {
803  if (!e.expression_type().type().is_int_type()) {
804  error_msgs << "Container index must be integer; found type=";
805  write_base_expr_type(error_msgs, e.expression_type().type());
806  error_msgs << std::endl;
807  pass = false;
808  return;
809  }
810  if (e.expression_type().num_dims_ > 1) {
811  // tests > 1 so that message is coherent because the single
812  // integer array tests don't print
813  error_msgs << "Index must be integer or 1D integer array;"
814  << " found number of dimensions="
816  << std::endl;
817  pass = false;
818  return;
819  }
820  if (e.expression_type().num_dims_ == 0) {
821  // need integer array expression here, but nothing else to report
822  pass = false;
823  return;
824  }
825  pass = true;
826  }
827  boost::phoenix::function<validate_ints_expression>
829 
830 
832  vm.add("params_r__",
833  base_var_decl("params_r__", std::vector<expression>(),
834  vector_type()),
835  parameter_origin); // acts like a parameter
836  }
837  boost::phoenix::function<add_params_var> add_params_var_f;
838 
840  vm.remove("params_r__");
841  }
842  boost::phoenix::function<remove_params_var> remove_params_var_f;
843 
844  void dump_program_line(size_t idx_errline, int offset,
845  const std::string& origin_file,
846  size_t origin_line,
847  const io::program_reader& reader,
848  const std::vector<std::string>& program_lines,
849  std::stringstream& error_msgs) {
850  boost::format fmt_lineno("%6d: ");
851  if (idx_errline + offset > 0
852  && idx_errline + offset < program_lines.size()) {
853  io::program_reader::trace_t trace = reader.trace(idx_errline + offset);
854  if (trace[trace.size() - 1].first == origin_file) {
855  std::string lineno = str(fmt_lineno % (origin_line + offset));
856  error_msgs << lineno << program_lines[idx_errline + offset - 1]
857  << std::endl;
858  }
859  }
860  }
861 
863  pos_iterator_t where, variable_map& vm,
864  std::stringstream& error_msgs,
865  const io::program_reader& reader) const {
866  // extract line and column of error
867  size_t idx_errline = boost::spirit::get_line(where);
868  if (idx_errline == 0) {
869  error_msgs << "Error before start of program." << std::endl;
870  return;
871  }
872  size_t idx_errcol = 0;
873  idx_errcol = get_column(begin, where) - 1;
874 
875  // extract lines of included program
876  std::basic_stringstream<char> program_ss;
877  program_ss << boost::make_iterator_range(begin, end);
878  std::vector<std::string> program_lines;
879  while (!program_ss.eof()) {
881  std::getline(program_ss, line);
882  program_lines.push_back(line);
883  }
884 
885  // dump include trace for error line
886  io::program_reader::trace_t trace = reader.trace(idx_errline);
887  std::string origin_file = trace[trace.size() - 1].first;
888  size_t origin_line = trace[trace.size() - 1].second;
889  error_msgs << " error in '" << trace[trace.size() - 1].first
890  << "' at line " << trace[trace.size() - 1].second
891  << ", column " << idx_errcol << std::endl;
892  for (int i = trace.size() - 1; i-- > 0; )
893  error_msgs << " included from '" << trace[i].first
894  << "' at line " << trace[i].second << std::endl;
895 
896  // dump context of error
897  error_msgs << " -------------------------------------------------"
898  << std::endl;
899 
900  dump_program_line(idx_errline, -2, origin_file, origin_line,
901  reader, program_lines, error_msgs);
902  dump_program_line(idx_errline, -1, origin_file, origin_line,
903  reader, program_lines, error_msgs);
904  dump_program_line(idx_errline, 0, origin_file, origin_line,
905  reader, program_lines, error_msgs);
906  error_msgs << std::setw(idx_errcol + 8) << "^" << std::endl;
907  dump_program_line(idx_errline, +1, origin_file, origin_line,
908  reader, program_lines, error_msgs);
909 
910  error_msgs << " -------------------------------------------------"
911  << std::endl << std::endl;
912  }
913  boost::phoenix::function<program_error> program_error_f;
914 
916  const expression& e,
917  bool& pass,
918  std::stringstream& error_msgs)
919  const {
920  if (!e.expression_type().is_primitive()) {
921  error_msgs << "conditions in if-else statement must be"
922  << " primitive int or real;"
923  << " found type=" << e.expression_type()
924  << std::endl;
925  pass = false;
926  return;
927  }
928  cs.conditions_.push_back(e);
929  pass = true;
930  return;
931  }
932  boost::phoenix::function<add_conditional_condition>
934 
936  const statement& s) const {
937  cs.bodies_.push_back(s);
938  }
939  boost::phoenix::function<add_conditional_body> add_conditional_body_f;
940 
942  std::ostream& error_msgs)
943  const {
944  error_msgs << "Warning (non-fatal): assignment operator <- deprecated"
945  << " in the Stan language;"
946  << " use = instead."
947  << std::endl;
948  std::string eq("=");
949  op = eq;
950  }
951  boost::phoenix::function<deprecate_old_assignment_op>
953 
954  void non_void_return_msg::operator()(scope var_scope, bool& pass,
955  std::ostream& error_msgs) const {
956  pass = false;
957  if (var_scope.non_void_fun()) {
958  error_msgs << "Non-void function must return expression"
959  << " of specified return type."
960  << std::endl;
961  return;
962  }
963  error_msgs << "Return statement only allowed from function bodies."
964  << std::endl;
965  }
966  boost::phoenix::function<non_void_return_msg> non_void_return_msg_f;
967 
968  void validate_return_allowed::operator()(scope var_scope, bool& pass,
969  std::ostream& error_msgs) const {
970  if (var_scope.void_fun()) {
971  error_msgs << "Void function cannot return a value."
972  << std::endl;
973  pass = false;
974  return;
975  }
976  if (!var_scope.non_void_fun()) {
977  error_msgs << "Returns only allowed from function bodies."
978  << std::endl;
979  pass = false;
980  return;
981  }
982  pass = true;
983  }
984  boost::phoenix::function<validate_return_allowed> validate_return_allowed_f;
985 
987  bool& pass,
988  std::ostream& error_msgs)
989  const {
990  if (!var_scope.void_fun()) {
991  error_msgs << "Void returns only allowed from function"
992  << " bodies of void return type."
993  << std::endl;
994  pass = false;
995  return;
996  }
997  pass = true;
998  }
999  boost::phoenix::function<validate_void_return_allowed>
1001 
1003  const scope& var_scope,
1004  bool& pass, const variable_map& vm,
1005  std::ostream& error_msgs) const {
1007  if (!can_assign_to_lhs_var(name, var_scope, vm, error_msgs)) {
1008  pass = false;
1009  return;
1010  }
1011  a.lhs_var_.set_type(vm.get_base_type(name), vm.get_num_dims(name));
1012  }
1013  boost::phoenix::function<validate_lhs_var_assgn> validate_lhs_var_assgn_f;
1014 
1015 
1016 
1018  bool& pass, const variable_map& vm,
1019  std::ostream& error_msgs) const {
1020  if (!vm.exists(name)) {
1021  error_msgs << "Unknown variable: " << name << std::endl;
1022  pass = false;
1023  return;
1024  }
1025  variable v(name);
1026  a.lhs_var_ = v;
1027  }
1028  boost::phoenix::function<set_lhs_var_assgn> set_lhs_var_assgn_f;
1029 
1031  const variable_map& vm,
1032  std::ostream& error_msgs) const {
1033  // validate var exists
1035  expression lhs_expr = expression(a.lhs_var_);
1036  expr_type lhs_type = indexed_type(lhs_expr, a.idxs_);
1037  if (lhs_type.is_ill_formed()) {
1038  error_msgs << "Left-hand side indexing incompatible with variable."
1039  << std::endl;
1040  pass = false;
1041  return;
1042  }
1043  if (a.is_simple_assignment()) {
1044  if (!has_same_shape(lhs_type, a.rhs_, name, "assignment", error_msgs)) {
1045  pass = false;
1046  return;
1047  }
1048  if (a.lhs_var_occurs_on_rhs()) {
1049  // this only requires a warning --- a deep copy will be made
1050  error_msgs << "Warning: left-hand side variable"
1051  << " (name=" << name << ")"
1052  << " occurs on right-hand side of assignment, causing"
1053  << " inefficient deep copy to avoid aliasing."
1054  << std::endl;
1055  }
1056  pass = true;
1057  return;
1058  } else {
1059  // compound operator-assignment
1060  std::string op_equals = a.op_;
1061  a.op_ = op_equals.substr(0, op_equals.size()-1);
1062 
1063  if (lhs_type.num_dims() > 0) {
1064  error_msgs << "Cannot apply operator '" << op_equals
1065  << "' to array variable; variable name = "
1066  << name
1067  << ".";
1068  error_msgs << std::endl;
1069  pass = false;
1070  return;
1071  }
1072 
1073  expr_type lhs_base_type = lhs_type.type();
1074  expr_type rhs_type = a.rhs_.expression_type();
1075  if (lhs_base_type.is_primitive()
1076  && boost::algorithm::starts_with(a.op_, ".")) {
1077  error_msgs << "Cannot apply element-wise operation to scalar"
1078  << "; compound operator is: " << op_equals
1079  << std::endl;
1080  pass = false;
1081  return;
1082  }
1083  if (lhs_base_type.is_primitive()
1084  && rhs_type.is_primitive()
1085  && (lhs_base_type.type().is_double_type()
1086  || lhs_base_type == rhs_type)) {
1087  pass = true;
1088  return;
1089  }
1090 
1091  std::string op_name;
1092  if (a.op_ == "+") {
1093  op_name = "add";
1094  } else if (a.op_ == "-") {
1095  op_name = "subtract";
1096  } else if (a.op_ == "*") {
1097  op_name = "multiply";
1098  } else if (a.op_ == "/") {
1099  op_name = "divide";
1100  } else if (a.op_ == "./") {
1101  op_name = "elt_divide";
1102  } else if (a.op_ == ".*") {
1103  op_name = "elt_multiply";
1104  }
1105  // check that "lhs <op> rhs" is valid stan::math function sig
1106  std::vector<function_arg_type> arg_types;
1107  arg_types.push_back(function_arg_type(lhs_base_type));
1108  arg_types.push_back(function_arg_type(rhs_type));
1109  function_signature_t op_equals_sig(lhs_base_type, arg_types);
1110  if (!function_signatures::instance().is_defined(op_name,
1111  op_equals_sig)) {
1112  error_msgs << "Cannot apply operator '" << op_equals
1113  << "' to operands;"
1114  << " left-hand side type = " << lhs_base_type
1115  << "; right-hand side type=" << rhs_type
1116  << std::endl;
1117  pass = false;
1118  return;
1119  }
1120  a.op_name_ = op_name;
1121  pass = true;
1122  }
1123  }
1124  boost::phoenix::function<validate_assgn> validate_assgn_f;
1125 
1127  const variable_map& var_map, bool& pass,
1128  std::ostream& error_msgs) const {
1129  static const bool user_facing = true;
1130  std::vector<expr_type> arg_types;
1131  arg_types.push_back(s.expr_.expression_type());
1132  for (size_t i = 0; i < s.dist_.args_.size(); ++i)
1133  arg_types.push_back(s.dist_.args_[i].expression_type());
1134  std::string function_name(s.dist_.family_);
1135  std::string internal_function_name = get_prob_fun(function_name);
1137  .discrete_first_arg(internal_function_name);
1138 
1139  if (internal_function_name.size() == 0) {
1140  pass = false;
1141  error_msgs << "Unknown distribution name: "
1142  << function_name << std::endl;
1143  return;
1144  }
1145 
1146  if (!(ends_with("_lpdf", internal_function_name)
1147  || ends_with("_lpmf", internal_function_name)
1148  || ends_with("_log", internal_function_name))) {
1149  pass = false;
1150  error_msgs << "Probability function must end in _lpdf or _lpmf."
1151  << " Found distribution family = " << function_name
1152  << " with no corresponding probability function"
1153  << " " << function_name << "_lpdf"
1154  << ", " << function_name << "_lpmf"
1155  << ", or " << function_name << "_log" << std::endl;
1156  return;
1157  }
1158 
1159  if ((internal_function_name.find("multiply_log") != std::string::npos)
1160  || (internal_function_name.find("binomial_coefficient_log")
1161  != std::string::npos)) {
1162  error_msgs << "Only distribution names can be used with"
1163  << " sampling (~) notation; found non-distribution"
1164  << " function: " << function_name
1165  << std::endl;
1166  pass = false;
1167  return;
1168  }
1169 
1170  if (internal_function_name.find("cdf_log") != std::string::npos) {
1171  error_msgs << "CDF and CCDF functions may not be used with"
1172  << " sampling notation."
1173  << " Use increment_log_prob("
1174  << internal_function_name << "(...)) instead."
1175  << std::endl;
1176  pass = false;
1177  return;
1178  }
1179 
1180  if (internal_function_name == "lkj_cov_log") {
1181  error_msgs << "Warning: the lkj_cov_log() sampling distribution"
1182  << " is deprecated. It will be removed in Stan 3."
1183  << std::endl
1184  << "Code LKJ covariance in terms of an lkj_corr()"
1185  << " distribution on a correlation matrix"
1186  << " and independent lognormals on the scales."
1187  << std::endl << std::endl;
1188  }
1189 
1190  if (!is_double_return(internal_function_name, arg_types, error_msgs)) {
1191  error_msgs << "require real scalar return type for"
1192  << " probability function." << std::endl;
1193  pass = false;
1194  return;
1195  }
1196  // test for LHS not being purely a variable
1197  if (has_non_param_var(s.expr_, var_map)) {
1198  error_msgs << "Warning (non-fatal):"
1199  << std::endl
1200  << "Left-hand side of sampling statement (~) may contain a"
1201  << " non-linear transform of a parameter or local variable."
1202  << std::endl
1203  << "If it does, you need to include a target += statement"
1204  << " with the log absolute determinant of the Jacobian of"
1205  << " the transform."
1206  << std::endl
1207  << "Left-hand-side of sampling statement:"
1208  << std::endl
1209  << " ";
1210  generate_expression(s.expr_, user_facing, error_msgs);
1211  error_msgs << " ~ " << function_name << "(...)"
1212  << std::endl;
1213  }
1214  // validate that variable and params are univariate if truncated
1215  if (s.truncation_.has_low() || s.truncation_.has_high()) {
1216  if (!is_univariate(s.expr_.expression_type())) {
1217  error_msgs << "Outcomes in truncated distributions"
1218  << " must be univariate."
1219  << std::endl
1220  << " Found outcome expression: ";
1221  generate_expression(s.expr_, user_facing, error_msgs);
1222  error_msgs << std::endl
1223  << " with non-univariate type: "
1224  << s.expr_.expression_type()
1225  << std::endl;
1226  pass = false;
1227  return;
1228  }
1229  for (size_t i = 0; i < s.dist_.args_.size(); ++i)
1230  if (!is_univariate(s.dist_.args_[i].expression_type())) {
1231  error_msgs << "Parameters in truncated distributions"
1232  << " must be univariate."
1233  << std::endl
1234  << " Found parameter expression: ";
1235  generate_expression(s.dist_.args_[i], user_facing, error_msgs);
1236  error_msgs << std::endl
1237  << " with non-univariate type: "
1238  << s.dist_.args_[i].expression_type()
1239  << std::endl;
1240  pass = false;
1241  return;
1242  }
1243  }
1244  if (s.truncation_.has_low()
1246  error_msgs << "Lower bounds in truncated distributions"
1247  << " must be univariate."
1248  << std::endl
1249  << " Found lower bound expression: ";
1250  generate_expression(s.truncation_.low_, user_facing, error_msgs);
1251  error_msgs << std::endl
1252  << " with non-univariate type: "
1254  << std::endl;
1255  pass = false;
1256  return;
1257  }
1258  if (s.truncation_.has_high()
1260  error_msgs << "Upper bounds in truncated distributions"
1261  << " must be univariate."
1262  << std::endl
1263  << " Found upper bound expression: ";
1264  generate_expression(s.truncation_.high_, user_facing, error_msgs);
1265  error_msgs << std::endl
1266  << " with non-univariate type: "
1268  << std::endl;
1269  pass = false;
1270  return;
1271  }
1272 
1273  // make sure CDFs or CCDFs exist with conforming signature
1274  // T[L, ]
1275  if (s.truncation_.has_low() && !s.truncation_.has_high()) {
1276  std::vector<expr_type> arg_types_trunc(arg_types);
1277  arg_types_trunc[0] = s.truncation_.low_.expression_type();
1278  std::string function_name_ccdf = get_ccdf(s.dist_.family_);
1279  if (function_name_ccdf == s.dist_.family_
1280  || !is_double_return(function_name_ccdf, arg_types_trunc,
1281  error_msgs)) {
1282  error_msgs << "lower truncation not defined for specified"
1283  << " arguments to "
1284  << s.dist_.family_ << std::endl;
1285  pass = false;
1286  return;
1287  }
1288  if (!is_double_return(function_name_ccdf, arg_types, error_msgs)) {
1289  error_msgs << "lower bound in truncation type does not match"
1290  << " sampled variate in distribution's type"
1291  << std::endl;
1292  pass = false;
1293  return;
1294  }
1295  }
1296  // T[, H]
1297  if (!s.truncation_.has_low() && s.truncation_.has_high()) {
1298  std::vector<expr_type> arg_types_trunc(arg_types);
1299  arg_types_trunc[0] = s.truncation_.high_.expression_type();
1300  std::string function_name_cdf = get_cdf(s.dist_.family_);
1301  if (function_name_cdf == s.dist_.family_
1302  || !is_double_return(function_name_cdf, arg_types_trunc,
1303  error_msgs)) {
1304  error_msgs << "upper truncation not defined for"
1305  << " specified arguments to "
1306  << s.dist_.family_ << std::endl;
1307 
1308  pass = false;
1309  return;
1310  }
1311  if (!is_double_return(function_name_cdf, arg_types, error_msgs)) {
1312  error_msgs << "upper bound in truncation type does not match"
1313  << " sampled variate in distribution's type"
1314  << std::endl;
1315  pass = false;
1316  return;
1317  }
1318  }
1319  // T[L, H]
1320  if (s.truncation_.has_low() && s.truncation_.has_high()) {
1321  std::vector<expr_type> arg_types_trunc(arg_types);
1322  arg_types_trunc[0] = s.truncation_.low_.expression_type();
1323  std::string function_name_cdf = get_cdf(s.dist_.family_);
1324  if (function_name_cdf == s.dist_.family_
1325  || !is_double_return(function_name_cdf, arg_types_trunc,
1326  error_msgs)) {
1327  error_msgs << "lower truncation not defined for specified"
1328  << " arguments to "
1329  << s.dist_.family_ << std::endl;
1330  pass = false;
1331  return;
1332  }
1333  if (!is_double_return(function_name_cdf, arg_types, error_msgs)) {
1334  error_msgs << "lower bound in truncation type does not match"
1335  << " sampled variate in distribution's type"
1336  << std::endl;
1337  pass = false;
1338  return;
1339  }
1340  }
1341  pass = true;
1342  }
1343  boost::phoenix::function<validate_sample> validate_sample_f;
1344 
1346  const stan::lang::expression& expr,
1347  std::stringstream& error_msgs) const {
1348  static const bool user_facing = true;
1349  if (!(expr.expression_type().type().is_void_type())) {
1350  error_msgs << "Illegal statement beginning with non-void"
1351  << " expression parsed as"
1352  << std::endl << " ";
1353  generate_expression(expr.expr_, user_facing, error_msgs);
1354  error_msgs << std::endl
1355  << "Not a legal assignment, sampling, or function"
1356  << " statement. Note that"
1357  << std::endl
1358  << " * Assignment statements only allow variables"
1359  << " (with optional indexes) on the left;"
1360  << std::endl
1361  << " * Sampling statements allow arbitrary"
1362  << " value-denoting expressions on the left."
1363  << std::endl
1364  << " * Functions used as statements must be"
1365  << " declared to have void returns"
1366  << std::endl << std::endl;
1367  pass = false;
1368  return;
1369  }
1370  pass = true;
1371  }
1372  boost::phoenix::function<expression_as_statement> expression_as_statement_f;
1373 
1374  void unscope_locals::operator()(const std::vector<var_decl>& var_decls,
1375  variable_map& vm) const {
1376  for (size_t i = 0; i < var_decls.size(); ++i)
1377  vm.remove(var_decls[i].name());
1378  }
1379  boost::phoenix::function<unscope_locals> unscope_locals_f;
1380 
1382  const expression& e, bool& pass,
1383  std::stringstream& error_msgs) const {
1384  pass = e.expression_type().is_primitive();
1385  if (!pass) {
1386  error_msgs << "conditions in while statement must be primitive"
1387  << " int or real;"
1388  << " found type=" << e.expression_type() << std::endl;
1389  return;
1390  }
1391  ws.condition_ = e;
1392  }
1393  boost::phoenix::function<add_while_condition> add_while_condition_f;
1394 
1396  const {
1397  ws.body_ = s;
1398  }
1399  boost::phoenix::function<add_while_body> add_while_body_f;
1400 
1402  const scope& var_scope,
1403  variable_map& vm) const {
1404  vm.add(name, base_var_decl(name, std::vector<expression>(), int_type()),
1405  scope(var_scope.program_block(), true));
1406  }
1407  boost::phoenix::function<add_loop_identifier> add_loop_identifier_f;
1408 
1409  void add_array_loop_identifier
1410  ::operator()(const stan::lang::expression& expr,
1411  std::string& name,
1412  const scope& var_scope,
1413  bool& pass, variable_map& vm) const {
1414  int numdims = expr.expression_type().num_dims();
1415  if (!(numdims > 0)) {
1416  pass = false;
1417  } else {
1418  std::vector<expression> dimvector(numdims - 1);
1419  vm.add(name, base_var_decl(name, dimvector,
1420  expr.expression_type().type()),
1421  scope(loop_identifier_origin, true));
1422  pass = true;
1423  }
1424  }
1425  boost::phoenix::function<add_array_loop_identifier>
1427 
1428  void add_matrix_loop_identifier
1429  ::operator()(const stan::lang::expression& expr,
1430  std::string& name,
1431  const scope& var_scope,
1432  bool& pass, variable_map& vm,
1433  std::stringstream& error_msgs) const {
1434  if (!(expr.expression_type().num_dims() == 0)
1435  || !(expr.expression_type().type().is_matrix_type()
1436  || expr.expression_type().type().is_vector_type()
1437  || expr.expression_type().type().is_row_vector_type())) {
1438  pass = false;
1439  error_msgs << "Loop must be over container or range."
1440  << std::endl;
1441  } else {
1442  vm.add(name, base_var_decl(name, std::vector<expression>(),
1443  double_type()),
1444  scope(loop_identifier_origin, true));
1445  pass = true;
1446  }
1447  }
1448  boost::phoenix::function<add_matrix_loop_identifier>
1450 
1452  std::string& name_local,
1453  bool& pass, variable_map& vm,
1454  std::stringstream& error_msgs)
1455  const {
1456  pass = !(vm.exists(name));
1457  if (!pass) {
1458  // avoid repeated error message due to backtracking
1459  if (error_msgs.str().find("Loop variable already declared.")
1460  == std::string::npos)
1461  error_msgs << "Loop variable already declared."
1462  << " variable name=\"" << name << "\"" << std::endl;
1463  } else {
1464  name_local = name;
1465  }
1466  }
1467  boost::phoenix::function<store_loop_identifier> store_loop_identifier_f;
1468 
1470  variable_map& vm) const {
1471  vm.remove(name);
1472  }
1473  boost::phoenix::function<remove_loop_identifier> remove_loop_identifier_f;
1474 
1476  bool& pass,
1477  std::stringstream& error_msgs)
1478  const {
1479  if (!expr.expression_type().is_primitive_int()) {
1480  error_msgs << "Expression denoting integer required; found type="
1481  << expr.expression_type() << std::endl;
1482  pass = false;
1483  return;
1484  }
1485  pass = true;
1486  }
1487  boost::phoenix::function<validate_int_expr> validate_int_expr_f;
1488 
1490  bool& pass,
1491  std::stringstream& error_msgs)
1492  const {
1493  if (!expr.expression_type().is_primitive_int()) {
1494  pass = false;
1495  return;
1496  }
1497  pass = true;
1498  }
1499  boost::phoenix::function<validate_int_expr_no_error_msgs>
1501 
1503  std::stringstream& error_msgs) const {
1504  error_msgs << "Warning (non-fatal): increment_log_prob(...);"
1505  << " is deprecated and will be removed in the future."
1506  << std::endl
1507  << " Use target += ...; instead."
1508  << std::endl;
1509  }
1510  boost::phoenix::function<deprecate_increment_log_prob>
1512 
1514  bool& pass,
1515  std::stringstream& error_msgs)
1516  const {
1517  pass = var_scope.allows_sampling();
1518  if (!pass)
1519  error_msgs << "Sampling statements (~) and increment_log_prob() are"
1520  << std::endl
1521  << "only allowed in the model block or lp functions."
1522  << std::endl;
1523  }
1524  boost::phoenix::function<validate_allow_sample> validate_allow_sample_f;
1525 
1527  bool& pass,
1528  std::ostream& error_msgs)
1529  const {
1530  pass = !e.expression_type().is_void();
1531  if (!pass)
1532  error_msgs << "attempt to increment log prob with void expression"
1533  << std::endl;
1534  }
1535  boost::phoenix::function<validate_non_void_expression>
1537 
1538  template <typename T, typename I>
1540  const I& begin,
1541  const I& end) const {
1542  line.begin_line_ = get_line(begin);
1543  line.end_line_ = get_line(end);
1544  }
1545  boost::phoenix::function<add_line_number>
1547 
1548  template void add_line_number::operator()(var_decl&,
1549  const pos_iterator_t& begin,
1550  const pos_iterator_t& end) const;
1551 
1552 
1553  template void add_line_number::operator()(statement&,
1554  const pos_iterator_t& begin,
1555  const pos_iterator_t& end) const;
1556 
1557 
1559  s = return_statement();
1560  }
1561  boost::phoenix::function<set_void_return> set_void_return_f;
1562 
1564  s = no_op_statement();
1565  }
1566  boost::phoenix::function<set_no_op> set_no_op_f;
1567 
1568 
1569  void deprecated_integrate_ode::operator()(std::ostream& error_msgs)
1570  const {
1571  error_msgs << "Warning: the integrate_ode() function is deprecated"
1572  << " in the Stan language; use the following functions"
1573  << " instead.\n"
1574  << " integrate_ode_rk45()"
1575  << " [explicit, order 5, for non-stiff problems]\n"
1576  << " integrate_ode_adams()"
1577  << " [implicit, up to order 12, for non-stiff problems]\n"
1578  << " integrate_ode_bdf()"
1579  << " [implicit, up to order 5, for stiff problems]."
1580  << std::endl;
1581  }
1582  boost::phoenix::function<deprecated_integrate_ode>
1584 
1585  template <class T>
1587  const variable_map& var_map,
1588  bool& pass,
1589  std::ostream& error_msgs) {
1590  pass = true;
1591  // test function argument type
1592  expr_type sys_result_type(double_type(), 1);
1593  std::vector<function_arg_type> sys_arg_types;
1594  sys_arg_types.push_back(function_arg_type(expr_type(double_type(), 0)));
1595  sys_arg_types.push_back(function_arg_type(expr_type(double_type(), 1)));
1596  sys_arg_types.push_back(function_arg_type(expr_type(double_type(), 1)));
1597  sys_arg_types.push_back(function_arg_type(expr_type(double_type(), 1)));
1598  sys_arg_types.push_back(function_arg_type(expr_type(int_type(), 1)));
1599  function_signature_t system_signature(sys_result_type, sys_arg_types);
1601  .is_defined(ode_fun.system_function_name_, system_signature)) {
1602  error_msgs << "first argument to "
1603  << ode_fun.integration_function_name_
1604  << " must be the name of a function with signature"
1605  << " (real, real[], real[], real[], int[]) : real[] ";
1606  pass = false;
1607  }
1608  // test regular argument types
1609  if (ode_fun.y0_.expression_type() != expr_type(double_type(), 1)) {
1610  error_msgs << "second argument to "
1611  << ode_fun.integration_function_name_
1612  << " must have type real[] for intial system state;"
1613  << " found type="
1614  << ode_fun.y0_.expression_type()
1615  << ". ";
1616  pass = false;
1617  }
1618  if (!ode_fun.t0_.expression_type().is_primitive()) {
1619  error_msgs << "third argument to "
1620  << ode_fun.integration_function_name_
1621  << " must have type real or int for initial time;"
1622  << " found type="
1623  << ode_fun.t0_.expression_type()
1624  << ". ";
1625  pass = false;
1626  }
1627  if (ode_fun.ts_.expression_type() != expr_type(double_type(), 1)) {
1628  error_msgs << "fourth argument to "
1629  << ode_fun.integration_function_name_
1630  << " must have type real[]"
1631  << " for requested solution times; found type="
1632  << ode_fun.ts_.expression_type()
1633  << ". ";
1634  pass = false;
1635  }
1636  if (ode_fun.theta_.expression_type() != expr_type(double_type(), 1)) {
1637  error_msgs << "fifth argument to "
1638  << ode_fun.integration_function_name_
1639  << " must have type real[] for parameters; found type="
1640  << ode_fun.theta_.expression_type()
1641  << ". ";
1642  pass = false;
1643  }
1644  if (ode_fun.x_.expression_type() != expr_type(double_type(), 1)) {
1645  error_msgs << "sixth argument to "
1646  << ode_fun.integration_function_name_
1647  << " must have type real[] for real data; found type="
1648  << ode_fun.x_.expression_type()
1649  << ". ";
1650  pass = false;
1651  }
1652  if (ode_fun.x_int_.expression_type() != expr_type(int_type(), 1)) {
1653  error_msgs << "seventh argument to "
1654  << ode_fun.integration_function_name_
1655  << " must have type int[] for integer data; found type="
1656  << ode_fun.x_int_.expression_type()
1657  << ". ";
1658  pass = false;
1659  }
1660 
1661  // test data-only variables do not have parameters (int locals OK)
1662  if (has_var(ode_fun.t0_, var_map)) {
1663  error_msgs << "third argument to "
1664  << ode_fun.integration_function_name_
1665  << " (initial times)"
1666  << " must be data only and not reference parameters";
1667  pass = false;
1668  }
1669  if (has_var(ode_fun.ts_, var_map)) {
1670  error_msgs << "fourth argument to "
1671  << ode_fun.integration_function_name_
1672  << " (solution times)"
1673  << " must be data only and not reference parameters";
1674  pass = false;
1675  }
1676  if (has_var(ode_fun.x_, var_map)) {
1677  error_msgs << "sixth argument to "
1678  << ode_fun.integration_function_name_
1679  << " (real data)"
1680  << " must be data only and not reference parameters";
1681  pass = false;
1682  }
1683  }
1684 
1686  const variable_map& var_map,
1687  bool& pass,
1688  std::ostream& error_msgs) const {
1689  validate_integrate_ode_non_control_args(ode_fun, var_map, pass,
1690  error_msgs);
1691  }
1692  boost::phoenix::function<validate_integrate_ode> validate_integrate_ode_f;
1693 
1695  const integrate_ode_control& ode_fun,
1696  const variable_map& var_map, bool& pass,
1697  std::ostream& error_msgs) const {
1698  validate_integrate_ode_non_control_args(ode_fun, var_map, pass,
1699  error_msgs);
1700  if (!ode_fun.rel_tol_.expression_type().is_primitive()) {
1701  error_msgs << "eighth argument to "
1702  << ode_fun.integration_function_name_
1703  << " (relative tolerance) must have type real or int;"
1704  << " found type="
1705  << ode_fun.rel_tol_.expression_type()
1706  << ". ";
1707  pass = false;
1708  }
1709  if (!ode_fun.abs_tol_.expression_type().is_primitive()) {
1710  error_msgs << "ninth argument to "
1711  << ode_fun.integration_function_name_
1712  << " (absolute tolerance) must have type real or int;"
1713  << " found type="
1714  << ode_fun.abs_tol_.expression_type()
1715  << ". ";
1716  pass = false;
1717  }
1718  if (!ode_fun.max_num_steps_.expression_type().is_primitive()) {
1719  error_msgs << "tenth argument to "
1720  << ode_fun.integration_function_name_
1721  << " (max steps) must have type real or int;"
1722  << " found type="
1723  << ode_fun.max_num_steps_.expression_type()
1724  << ". ";
1725  pass = false;
1726  }
1727 
1728  // test data-only variables do not have parameters (int locals OK)
1729  if (has_var(ode_fun.rel_tol_, var_map)) {
1730  error_msgs << "eighth argument to "
1731  << ode_fun.integration_function_name_
1732  << " (relative tolerance) must be data only"
1733  << " and not depend on parameters";
1734  pass = false;
1735  }
1736  if (has_var(ode_fun.abs_tol_, var_map)) {
1737  error_msgs << "ninth argument to "
1738  << ode_fun.integration_function_name_
1739  << " (absolute tolerance ) must be data only"
1740  << " and not depend parameters";
1741  pass = false;
1742  }
1743  if (has_var(ode_fun.max_num_steps_, var_map)) {
1744  error_msgs << "tenth argument to "
1745  << ode_fun.integration_function_name_
1746  << " (max steps) must be data only"
1747  << " and not depend on parameters";
1748  pass = false;
1749  }
1750  }
1751  boost::phoenix::function<validate_integrate_ode_control>
1753 
1754  template <class T>
1756  const variable_map& var_map,
1757  bool& pass,
1758  std::ostream& error_msgs) {
1759  pass = true;
1760  // test function argument type
1761  expr_type sys_result_type(vector_type(), 0);
1762  std::vector<function_arg_type> sys_arg_types;
1763  sys_arg_types.push_back(function_arg_type(expr_type(vector_type(),
1764  0), true)); // y
1765  sys_arg_types.push_back(function_arg_type(expr_type(vector_type(),
1766  0))); // theta
1767  sys_arg_types.push_back(function_arg_type(expr_type(double_type(),
1768  1), true)); // x_r
1769  sys_arg_types.push_back(function_arg_type(expr_type(int_type(),
1770  1))); // x_i
1771  function_signature_t system_signature(sys_result_type, sys_arg_types);
1773  .is_defined(alg_fun.system_function_name_, system_signature)) {
1774  error_msgs << "first argument to "
1775  << "algebra_solver"
1776  << " must be the name of a function with signature"
1777  << " (vector, vector, real[], int[]) : vector "
1778  << std::endl;
1779  pass = false;
1780  }
1781 
1782  // test regular argument types
1783  if (alg_fun.y_.expression_type() != expr_type(vector_type(), 0)) {
1784  error_msgs << "second argument to algebra_solver"
1785  << " must have type vector for initial guess;"
1786  << " found type = "
1787  << alg_fun.y_.expression_type()
1788  << ". " << std::endl;
1789  pass = false;
1790  }
1791  if (alg_fun.theta_.expression_type() != expr_type(vector_type(), 0)) {
1792  error_msgs << "third argument to algebra_solver"
1793  << " must have type vector for parameters;"
1794  << " found type = "
1795  << alg_fun.theta_.expression_type()
1796  << ". " << std::endl;
1797  pass = false;
1798  }
1799  if (alg_fun.x_r_.expression_type() != expr_type(double_type(), 1)) {
1800  error_msgs << "fourth argument to algebra_solver"
1801  << " must have type real[] for real data;"
1802  << " found type = "
1803  << alg_fun.x_r_.expression_type()
1804  << ". " << std::endl;
1805  pass = false;
1806  }
1807  if (alg_fun.x_i_.expression_type() != expr_type(int_type(), 1)) {
1808  error_msgs << "fifth argument to algebra_solver"
1809  << " must have type int[] for integer data;"
1810  << " found type = "
1811  << alg_fun.x_i_.expression_type()
1812  << ". " << std::endl;
1813  pass = false;
1814  }
1815 
1816  // test data-only variables do not have parameters (int locals OK)
1817  if (has_var(alg_fun.x_r_, var_map)) {
1818  error_msgs << "fourth argument to algebra_solver"
1819  << " (real data)"
1820  << " must be data only and not reference parameters"
1821  << std::endl;
1822  pass = false;
1823  }
1824  }
1825 
1827  const variable_map& var_map,
1828  bool& pass,
1829  std::ostream& error_msgs) const {
1830  validate_algebra_solver_non_control_args(alg_fun, var_map, pass,
1831  error_msgs);
1832  }
1833  boost::phoenix::function<validate_algebra_solver>
1835 
1837  const algebra_solver_control& alg_fun,
1838  const variable_map& var_map, bool& pass,
1839  std::ostream& error_msgs) const {
1840  validate_algebra_solver_non_control_args(alg_fun, var_map, pass,
1841  error_msgs);
1842  if (!alg_fun.rel_tol_.expression_type().is_primitive()) {
1843  error_msgs << "sixth argument to algebra_solver "
1844  << " (relative tolerance) must have type real or int;"
1845  << " found type="
1846  << alg_fun.rel_tol_.expression_type()
1847  << ". " << std::endl;
1848  pass = false;
1849  }
1850  if (!alg_fun.fun_tol_.expression_type().is_primitive()) {
1851  error_msgs << "seventh argument to algebra_solver "
1852  << " (function tolerance) must have type real or int;"
1853  << " found type="
1854  << alg_fun.fun_tol_.expression_type()
1855  << ". " << std::endl;
1856  pass = false;
1857  }
1858  if (!alg_fun.max_num_steps_.expression_type().is_primitive()) {
1859  error_msgs << "eighth argument to algebra_solver"
1860  << " (max number of steps) must have type real or int;"
1861  << " found type="
1862  << alg_fun.max_num_steps_.expression_type()
1863  << ". " << std::endl;
1864  pass = false;
1865  }
1866 
1867  // test data-only variables do not have parameters (int locals OK)
1868  if (has_var(alg_fun.rel_tol_, var_map)) {
1869  error_msgs << "sixth argument to algebra_solver"
1870  << " (relative tolerance) must be data only"
1871  << " and not depend on parameters"
1872  << std::endl;
1873  pass = false;
1874  }
1875  if (has_var(alg_fun.fun_tol_, var_map)) {
1876  error_msgs << "seventh argument to algebra_solver"
1877  << " (function tolerance ) must be data only"
1878  << " and not depend parameters"
1879  << std::endl;
1880  pass = false;
1881  }
1882  if (has_var(alg_fun.max_num_steps_, var_map)) {
1883  error_msgs << "eighth argument to algebra_solver"
1884  << " (max number of steps) must be data only"
1885  << " and not depend on parameters" << std::endl;
1886  pass = false;
1887  }
1888  }
1889  boost::phoenix::function<validate_algebra_solver_control>
1891 
1893  map_rect& mr, const variable_map& var_map,
1894  bool& pass, std::ostream& error_msgs) const {
1895  pass = true;
1896 
1897  if (has_rng_lp_suffix(mr.fun_name_)) {
1898  error_msgs << "mapped function cannot be an _rng or _lp function,"
1899  << " found function name: "
1900  << mr.fun_name_ << std::endl;
1901  pass = false;
1902  }
1903 
1904  // mapped function signature
1905  // vector f(vector param_shared, vector param_local,
1906  // real[] data_r, int[] data_i)
1907  expr_type shared_params_type(vector_type(), 0);
1908  expr_type job_params_type(vector_type(), 0);
1909  expr_type job_data_r_type(double_type(), 1);
1910  expr_type job_data_i_type(int_type(), 1);
1911  expr_type result_type(vector_type(), 0);
1912  std::vector<function_arg_type> arg_types
1913  = { function_arg_type(shared_params_type),
1914  function_arg_type(job_params_type),
1915  function_arg_type(job_data_r_type),
1916  function_arg_type(job_data_i_type) };
1917  function_signature_t mapped_fun_signature(result_type, arg_types);
1918 
1919  // validate mapped function signature
1921  .is_defined(mr.fun_name_, mapped_fun_signature)) {
1922  error_msgs << "first argument to map_rect"
1923  << " must be the name of a function with signature"
1924  << " (vector, vector, real[], int[]) : vector" << std::endl;
1925  pass = false;
1926  }
1927 
1928  // validate parameter and data argument shapes
1929  if (mr.shared_params_.expression_type() != shared_params_type) {
1930  if (!pass) error_msgs << "; ";
1931  error_msgs << "second argument to map_rect must be of type vector"
1932  << std::endl;
1933  pass = false;
1934  }
1935  // one more array dim for args other than shared params
1936  expr_type job_paramss_type(vector_type(), 1);
1937  if (mr.job_params_.expression_type() != job_paramss_type) {
1938  if (!pass) error_msgs << "; ";
1939  error_msgs << "third argument to map_rect must be of type vector[]"
1940  << " (array of vectors)" << std::endl;
1941  pass = false;
1942  }
1943  expr_type job_data_rs_type(double_type(), 2);
1944  if (mr.job_data_r_.expression_type() != job_data_rs_type) {
1945  if (!pass) error_msgs << "; ";
1946  error_msgs << "fourth argument to map_rect must be of type real[ , ]"
1947  << " (two dimensional array of reals)" << std::endl;
1948  pass = false;
1949  }
1950  expr_type job_data_is_type(int_type(), 2);
1951  if (mr.job_data_i_.expression_type() != job_data_is_type) {
1952  if (!pass) error_msgs << "; ";
1953  error_msgs << "fifth argument to map_rect must be of type int[ , ]"
1954  << " (two dimensional array of integers)" << std::endl;
1955  pass = false;
1956  }
1957 
1958  // test data is data only
1959  if (has_var(mr.job_data_r_, var_map)) {
1960  if (!pass) error_msgs << "; ";
1961  error_msgs << "fourth argment to map_rect must be data only"
1962  << std::endl;
1963  pass = false;
1964  }
1965  if (has_var(mr.job_data_i_, var_map)) {
1966  if (!pass) error_msgs << "; ";
1967  error_msgs << "fifth argument to map_rect must be data only"
1968  << std::endl;
1969  pass = false;
1970  }
1971 
1972  if (pass)
1973  mr.register_id();
1974  }
1975  boost::phoenix::function<validate_map_rect> validate_map_rect_f;
1976 
1977 
1979  const scope& var_scope,
1980  bool& pass,
1981  const variable_map& var_map,
1982  std::ostream& error_msgs) const {
1983  if (fun.name_ == "get_lp")
1984  error_msgs << "Warning (non-fatal): get_lp() function deprecated."
1985  << std::endl
1986  << " It will be removed in a future release."
1987  << std::endl
1988  << " Use target() instead."
1989  << std::endl;
1990  if (fun.name_ == "target")
1991  fun.name_ = "get_lp"; // for code gen and context validation
1992 
1993  std::vector<expr_type> arg_types;
1994  for (size_t i = 0; i < fun.args_.size(); ++i)
1995  arg_types.push_back(fun.args_[i].expression_type());
1996 
1998  .get_result_type(fun.name_, arg_types, error_msgs);
1999  if (fun.type_.type().is_ill_formed_type()) {
2000  pass = false;
2001  return;
2002  }
2003 
2004  // get function definition for this functiion
2005  std::vector<function_arg_type> fun_arg_types;
2006  for (size_t i = 0; i < fun.args_.size(); ++i)
2007  fun_arg_types.push_back(function_arg_type(arg_types[i]));
2008  function_signature_t sig(fun.type_, fun_arg_types);
2009  function_signature_t decl_sig =
2011  if (!decl_sig.first.is_ill_formed()) {
2012  for (size_t i = 0; i < fun_arg_types.size(); ++i) {
2013  if (decl_sig.second[i].data_only_
2014  && has_var(fun.args_[i], var_map)) {
2015  error_msgs << "Function argument error, function: "
2016  << fun.name_ << ", argument: " << (i + 1)
2017  << " must be data only, "
2018  << "found expression containing a parameter varaible."
2019  << std::endl;
2020  pass = false;
2021  return;
2022  }
2023  }
2024  }
2025 
2026  // disjunction so only first match triggered
2027  deprecate_fun("binomial_coefficient_log", "lchoose", fun, error_msgs)
2028  || deprecate_fun("multiply_log", "lmultiply", fun, error_msgs)
2029  || deprecate_suffix("_cdf_log", "'_lcdf'", fun, error_msgs)
2030  || deprecate_suffix("_ccdf_log", "'_lccdf'", fun, error_msgs)
2031  || deprecate_suffix("_log",
2032  "'_lpdf' for density functions or '_lpmf' for mass functions",
2033  fun, error_msgs);
2034 
2035 
2036  // use old function names for built-in prob funs
2037  if (!function_signatures::instance().has_user_defined_key(fun.name_)) {
2038  replace_suffix("_lpdf", "_log", fun);
2039  replace_suffix("_lpmf", "_log", fun);
2040  replace_suffix("_lcdf", "_cdf_log", fun);
2041  replace_suffix("_lccdf", "_ccdf_log", fun);
2042  }
2043  // know these are not user-defined`x
2044  replace_suffix("lmultiply", "multiply_log", fun);
2045  replace_suffix("lchoose", "binomial_coefficient_log", fun);
2046 
2047  if (has_rng_suffix(fun.name_)) {
2048  if (!(var_scope.allows_rng())) {
2049  error_msgs << "Random number generators only allowed in"
2050  << " transformed data block, generated quantities block"
2051  << " or user-defined functions with names ending in _rng"
2052  << "; found function=" << fun.name_ << " in block=";
2053  print_scope(error_msgs, var_scope);
2054  error_msgs << std::endl;
2055  pass = false;
2056  return;
2057  }
2058  }
2059 
2060  if (has_lp_suffix(fun.name_) || fun.name_ == "target") {
2061  if (!(var_scope.allows_lp_fun())) {
2062  error_msgs << "Function target() or functions suffixed with _lp only"
2063  << " allowed in transformed parameter block, model block"
2064  << std::endl
2065  << "or the body of a function with suffix _lp."
2066  << std::endl
2067  << "Found function = "
2068  << (fun.name_ == "get_lp" ? "target or get_lp" : fun.name_)
2069  << " in block = ";
2070  print_scope(error_msgs, var_scope);
2071  error_msgs << std::endl;
2072  pass = false;
2073  return;
2074  }
2075  }
2076 
2077  if (fun.name_ == "abs"
2078  && fun.args_.size() > 0
2079  && fun.args_[0].expression_type().is_primitive_double()) {
2080  error_msgs << "Warning: Function abs(real) is deprecated"
2081  << " in the Stan language."
2082  << std::endl
2083  << " It will be removed in a future release."
2084  << std::endl
2085  << " Use fabs(real) instead."
2086  << std::endl << std::endl;
2087  }
2088 
2089  if (fun.name_ == "lkj_cov_log") {
2090  error_msgs << "Warning: the lkj_cov_log() function"
2091  << " is deprecated. It will be removed in Stan 3."
2092  << std::endl
2093  << "Code LKJ covariance in terms of an lkj_corr()"
2094  << " distribution on a correlation matrix"
2095  << " and independent lognormals on the scales."
2096  << std::endl << std::endl;
2097  }
2098 
2099  if (fun.name_ == "if_else") {
2100  error_msgs << "Warning (non-fatal): the if_else() function"
2101  << " is deprecated. "
2102  << "Use the conditional operator '?:' instead."
2103  << std::endl;
2104  }
2105 
2106  // add namespace qualifier to avoid ambiguities w/ c math fns
2107  qualify_builtins(fun);
2108 
2109  fun_result = fun;
2110  pass = true;
2111  }
2112  boost::phoenix::function<set_fun_type_named> set_fun_type_named_f;
2113 
2116  const scope& var_scope,
2117  bool& pass,
2118  const variable_map& var_map,
2119  std::ostream& error_msgs) const {
2120  if (array_expr.args_.size() == 0) {
2121  // shouldn't occur, because of % operator used to construct it
2122  error_msgs << "Array expression found size 0, must be > 0";
2123  array_expr.type_ = expr_type(ill_formed_type());
2124  pass = false;
2125  return;
2126  }
2127  expr_type et;
2128  et = array_expr.args_[0].expression_type();
2129  for (size_t i = 1; i < array_expr.args_.size(); ++i) {
2130  expr_type et_next;
2131  et_next = array_expr.args_[i].expression_type();
2132  if (et.num_dims_ != et_next.num_dims_) {
2133  error_msgs << "Expressions for elements of array must have"
2134  << " same array sizes; found"
2135  << " previous type=" << et
2136  << "; type at position " << i << "=" << et_next;
2137  array_expr.type_ = expr_type(ill_formed_type());
2138  pass = false;
2139  return;
2140  }
2141  if ((et.base_type_.is_int_type() && et_next.base_type_.is_double_type())
2142  || (et.base_type_.is_double_type()
2143  && et_next.base_type_.is_int_type())) {
2144  et.base_type_ = double_type();
2145  } else if (et.base_type_ != et_next.base_type_) {
2146  error_msgs << "Expressions for elements of array must have"
2147  << " the same or promotable types; found"
2148  << " previous type=" << et
2149  << "; type at position " << i << "=" << et_next;
2150  array_expr.type_ = expr_type(ill_formed_type());
2151  pass = false;
2152  return;
2153  }
2154  }
2155  ++et.num_dims_;
2156  array_expr.type_ = et;
2157  array_expr.array_expr_scope_ = var_scope;
2158  array_expr.has_var_ = has_var(array_expr, var_map);
2159  e = array_expr;
2160  pass = true;
2161  }
2162  boost::phoenix::function<infer_array_expr_type> infer_array_expr_type_f;
2163 
2165  row_vector_expr& vec_expr,
2166  const scope& var_scope,
2167  bool& pass,
2168  const variable_map& var_map,
2169  std::ostream& error_msgs) const {
2170  if (vec_expr.args_.size() == 0) {
2171  // shouldn't occur, because of % operator used to construct it
2172  error_msgs << "Vector or matrix expression found size 0, must be > 0";
2173  pass = false;
2174  return;
2175  }
2176  expr_type et = vec_expr.args_[0].expression_type();
2177  if (!(et.is_primitive() || et.type().is_row_vector_type())) {
2178  error_msgs << "Matrix expression elements must be type row_vector "
2179  << "and row vector expression elements must be int "
2180  << "or real, but found element of type "
2181  << et << std::endl;
2182  pass = false;
2183  return;
2184  }
2185  bool is_matrix_el = et.type().is_row_vector_type();
2186  for (size_t i = 1; i < vec_expr.args_.size(); ++i) {
2187  if (is_matrix_el &&
2188  !vec_expr.args_[i].expression_type().type().is_row_vector_type()) {
2189  error_msgs << "Matrix expression elements must be type row_vector, "
2190  << "but found element of type "
2191  << vec_expr.args_[i].expression_type() << std::endl;
2192  pass = false;
2193  return;
2194  } else if (!is_matrix_el &&
2195  !(vec_expr.args_[i].expression_type().is_primitive())) {
2196  error_msgs << "Row vector expression elements must be int or real, "
2197  << "but found element of type "
2198  << vec_expr.args_[i].expression_type() << std::endl;
2199  pass = false;
2200  return;
2201  }
2202  }
2203  if (is_matrix_el) {
2204  // create matrix expr object
2205  matrix_expr me = matrix_expr(vec_expr.args_);
2206  me.matrix_expr_scope_ = var_scope;
2207  me.has_var_ = has_var(me, var_map);
2208  e = me;
2209  } else {
2210  vec_expr.row_vector_expr_scope_ = var_scope;
2211  vec_expr.has_var_ = has_var(vec_expr, var_map);
2212  e = vec_expr;
2213  }
2214  pass = true;
2215  }
2216  boost::phoenix::function<infer_vec_or_matrix_expr_type>
2218 
2220  const expression& expr2,
2221  const scope& var_scope,
2222  bool& pass,
2223  std::ostream& error_msgs) const {
2224  if (!expr1.expression_type().is_primitive()
2225  || !expr2.expression_type().is_primitive()) {
2226  error_msgs << "arguments to ^ must be primitive (real or int)"
2227  << "; cannot exponentiate "
2228  << expr1.expression_type()
2229  << " by "
2230  << expr2.expression_type()
2231  << " in block=";
2232  print_scope(error_msgs, var_scope);
2233  error_msgs << std::endl;
2234  pass = false;
2235  return;
2236  }
2237  std::vector<expression> args;
2238  args.push_back(expr1);
2239  args.push_back(expr2);
2240  fun f("pow", args);
2241  set_fun_type(f, error_msgs);
2242  expr1 = expression(f);
2243  }
2244  boost::phoenix::function<exponentiation_expr> exponentiation_f;
2245 
2247  const expression& expr2,
2248  std::ostream& error_msgs) const {
2249  if (expr1.expression_type().is_primitive()
2250  && expr2.expression_type().is_primitive()) {
2251  expr1 *= expr2;;
2252  return;
2253  }
2254  std::vector<expression> args;
2255  args.push_back(expr1);
2256  args.push_back(expr2);
2257  fun f("multiply", args);
2258  set_fun_type(f, error_msgs);
2259  expr1 = expression(f);
2260  }
2261  boost::phoenix::function<multiplication_expr> multiplication_f;
2262 
2264  const expression& expr2,
2265  std::ostream& error_msgs) const {
2266  static const bool user_facing = true;
2267  if (expr1.expression_type().is_primitive()
2268  && expr2.expression_type().is_primitive()
2269  && (expr1.expression_type().is_primitive_double()
2270  || expr2.expression_type().is_primitive_double())) {
2271  expr1 /= expr2;
2272  return;
2273  }
2274  std::vector<expression> args;
2275  args.push_back(expr1);
2276  args.push_back(expr2);
2277  if (expr1.expression_type().is_primitive_int()
2278  && expr2.expression_type().is_primitive_int()) {
2279  // result might be assigned to real - generate warning
2280  error_msgs << "Warning: integer division"
2281  << " implicitly rounds to integer."
2282  << " Found int division: ";
2283  generate_expression(expr1.expr_, user_facing, error_msgs);
2284  error_msgs << " / ";
2285  generate_expression(expr2.expr_, user_facing, error_msgs);
2286  error_msgs << std::endl
2287  << " Positive values rounded down,"
2288  << " negative values rounded up or down"
2289  << " in platform-dependent way."
2290  << std::endl;
2291 
2292  fun f("divide", args);
2293  set_fun_type(f, error_msgs);
2294  expr1 = expression(f);
2295  return;
2296  }
2297  if ((expr1.expression_type().type().is_matrix_type()
2298  || expr1.expression_type().type().is_row_vector_type())
2299  && expr2.expression_type().type().is_matrix_type()) {
2300  fun f("mdivide_right", args);
2301  set_fun_type(f, error_msgs);
2302  expr1 = expression(f);
2303  return;
2304  }
2305  fun f("divide", args);
2306  set_fun_type(f, error_msgs);
2307  expr1 = expression(f);
2308  return;
2309  }
2310  boost::phoenix::function<division_expr> division_f;
2311 
2313  bool& pass, std::ostream& error_msgs) const {
2314  if (!expr1.expression_type().is_primitive_int()
2315  && !expr2.expression_type().is_primitive_int()) {
2316  error_msgs << "both operands of % must be int"
2317  << "; cannot modulo "
2318  << expr1.expression_type()
2319  << " by "
2320  << expr2.expression_type();
2321  error_msgs << std::endl;
2322  pass = false;
2323  return;
2324  }
2325  std::vector<expression> args;
2326  args.push_back(expr1);
2327  args.push_back(expr2);
2328  fun f("modulus", args);
2329  set_fun_type(f, error_msgs);
2330  expr1 = expression(f);
2331  }
2332  boost::phoenix::function<modulus_expr> modulus_f;
2333 
2335  const expression& expr2,
2336  std::ostream& error_msgs) const {
2337  std::vector<expression> args;
2338  args.push_back(expr1);
2339  args.push_back(expr2);
2340  if (expr1.expression_type().type().is_matrix_type()
2341  && (expr2.expression_type().type().is_vector_type()
2342  || expr2.expression_type().type().is_matrix_type())) {
2343  fun f("mdivide_left", args);
2344  set_fun_type(f, error_msgs);
2345  expr1 = expression(f);
2346  pass = true;
2347  return;
2348  }
2349  fun f("mdivide_left", args); // set for alt args err msg
2350  set_fun_type(f, error_msgs);
2351  expr1 = expression(f);
2352  pass = false;
2353  }
2354  boost::phoenix::function<left_division_expr> left_division_f;
2355 
2357  const expression& expr2,
2358  std::ostream& error_msgs) const {
2359  if (expr1.expression_type().is_primitive()
2360  && expr2.expression_type().is_primitive()) {
2361  expr1 *= expr2;
2362  return;
2363  }
2364  std::vector<expression> args;
2365  args.push_back(expr1);
2366  args.push_back(expr2);
2367  fun f("elt_multiply", args);
2368  set_fun_type(f, error_msgs);
2369  expr1 = expression(f);
2370  }
2371  boost::phoenix::function<elt_multiplication_expr> elt_multiplication_f;
2372 
2374  const expression& expr2,
2375  std::ostream& error_msgs) const {
2376  if (expr1.expression_type().is_primitive()
2377  && expr2.expression_type().is_primitive()) {
2378  expr1 /= expr2;
2379  return;
2380  }
2381  std::vector<expression> args;
2382  args.push_back(expr1);
2383  args.push_back(expr2);
2384  fun f("elt_divide", args);
2385  set_fun_type(f, error_msgs);
2386  expr1 = expression(f);
2387  }
2388  boost::phoenix::function<elt_division_expr> elt_division_f;
2389 
2391  const expression& expr, bool& pass,
2392  std::ostream& error_msgs) const {
2393  if (expr.expression_type().is_primitive()) {
2394  expr_result = expression(unary_op('-', expr));
2395  return;
2396  }
2397  std::vector<expression> args;
2398  args.push_back(expr);
2399  fun f("minus", args);
2400  set_fun_type(f, error_msgs);
2401  expr_result = expression(f);
2402  }
2403  boost::phoenix::function<negate_expr> negate_expr_f;
2404 
2406  const expression& expr,
2407  std::ostream& error_msgs) const {
2408  if (!expr.expression_type().is_primitive()) {
2409  error_msgs << "logical negation operator !"
2410  << " only applies to int or real types; ";
2411  expr_result = expression();
2412  }
2413  std::vector<expression> args;
2414  args.push_back(expr);
2415  fun f("logical_negation", args);
2416  set_fun_type(f, error_msgs);
2417  expr_result = expression(f);
2418  }
2419  boost::phoenix::function<logical_negate_expr> logical_negate_expr_f;
2420 
2421  void transpose_expr::operator()(expression& expr, bool& pass,
2422  std::ostream& error_msgs) const {
2423  if (expr.expression_type().is_primitive())
2424  return;
2425  std::vector<expression> args;
2426  args.push_back(expr);
2427  fun f("transpose", args);
2428  set_fun_type(f, error_msgs);
2429  expr = expression(f);
2430  pass = !expr.expression_type().is_ill_formed();
2431  }
2432  boost::phoenix::function<transpose_expr> transpose_f;
2433 
2434  void add_idxs::operator()(expression& e, std::vector<idx>& idxs,
2435  bool& pass, std::ostream& error_msgs) const {
2436  e = index_op_sliced(e, idxs);
2437  pass = !e.expression_type().is_ill_formed();
2438  if (!pass)
2439  error_msgs << "Indexed expression must have at least as many"
2440  << " dimensions as number of indexes supplied:"
2441  << std::endl
2442  << " indexed expression dims="
2443  << e.total_dims()
2444  << "; num indexes=" << idxs.size()
2445  << std::endl;
2446  }
2447  boost::phoenix::function<add_idxs> add_idxs_f;
2448 
2450  std::vector<std::vector<stan::lang::expression> >& dimss,
2451  bool& pass, std::ostream& error_msgs) const {
2452  int expr_dims = expression.total_dims();
2453  int index_dims = num_dimss(dimss);
2454  if (expr_dims < index_dims) {
2455  error_msgs << "Too many indexes, expression dimensions="
2456  << expr_dims
2457  << ", indexes found="
2458  << index_dims
2459  << std::endl;
2460  pass = false;
2461  return;
2462  }
2463  index_op iop(expression, dimss);
2464  iop.infer_type();
2465  if (iop.type_.is_ill_formed()) {
2466  error_msgs << "Indexed expression must have at least as many"
2467  << " dimensions as number of indexes supplied."
2468  << std::endl;
2469  pass = false;
2470  return;
2471  }
2472  pass = true;
2473  expression = iop;
2474  }
2475  boost::phoenix::function<add_expression_dimss> add_expression_dimss_f;
2476 
2478  expression& val, variable_map& vm,
2479  std::ostream& error_msgs, bool& pass) const {
2480  std::string name = var_expr.name_;
2481  if (name == std::string("lp__")) {
2482  error_msgs << std::endl
2483  << "Error (fatal): Use of lp__ is no longer supported."
2484  << std::endl
2485  << " Use target += ... statement to increment log density."
2486  << std::endl
2487  << " Use target() function to get log density."
2488  << std::endl;
2489  pass = false;
2490  return;
2491  } else if (name == std::string("params_r__")) {
2492  error_msgs << std::endl << "Warning:" << std::endl
2493  << " Direct access to params_r__ yields an inconsistent"
2494  << " statistical model in isolation and no guarantee is"
2495  << " made that this model will yield valid inferences."
2496  << std::endl
2497  << " Moreover, access to params_r__ is unsupported"
2498  << " and the variable may be removed without notice."
2499  << std::endl;
2500  } else if (name == std::string("data")
2501  || name == std::string("generated")
2502  || name == std::string("model")
2503  || name == std::string("parameters")
2504  || name == std::string("transformed")) {
2505  error_msgs << std::endl
2506  << "Unexpected open block, missing close block \"}\""
2507  << " before keyword \""
2508  << name
2509  << "\"."
2510  << std::endl;
2511  pass = false;
2512  return;
2513  }
2514  pass = vm.exists(name);
2515  if (pass) {
2516  var_expr.set_type(vm.get_base_type(name), vm.get_num_dims(name));
2517  } else {
2518  error_msgs << "variable \"" << name << '"' << " does not exist."
2519  << std::endl;
2520  return;
2521  }
2522  val = expression(var_expr);
2523  }
2524  boost::phoenix::function<set_var_type> set_var_type_f;
2525 
2526  void require_vbar::operator()(bool& pass, std::ostream& error_msgs) const {
2527  pass = false;
2528  error_msgs << "Probabilty functions with suffixes _lpdf, _lpmf,"
2529  << " _lcdf, and _lccdf," << std::endl
2530  << "require a vertical bar (|) between the first two"
2531  << " arguments." << std::endl;
2532  }
2533  boost::phoenix::function<require_vbar> require_vbar_f;
2534 
2535 
2536 
2538  std::stringstream& error_msgs)
2539  : error_msgs_(error_msgs) { }
2540 
2542  error_msgs_ << "nil declarations not allowed";
2543  return false; // fail if arises
2544  }
2546  if (x.range_.has_low() || x.range_.has_high()) {
2547  error_msgs_ << "require unconstrained."
2548  << " found range constraint." << std::endl;
2549  return false;
2550  }
2551  return true;
2552  }
2554  const {
2555  if (x.range_.has_low() || x.range_.has_high()) {
2556  error_msgs_ << "require unconstrained."
2557  << " found range constraint." << std::endl;
2558  return false;
2559  }
2560  return true;
2561  }
2563  const {
2564  if (x.range_.has_low() || x.range_.has_high()) {
2565  error_msgs_ << "require unconstrained."
2566  << " found range constraint." << std::endl;
2567  return false;
2568  }
2569  return true;
2570  }
2572  const {
2573  if (x.range_.has_low() || x.range_.has_high()) {
2574  error_msgs_ << "require unconstrained."
2575  << " found range constraint." << std::endl;
2576  return false;
2577  }
2578  return true;
2579  }
2581  const {
2582  if (x.range_.has_low() || x.range_.has_high()) {
2583  error_msgs_ << "require unconstrained."
2584  << " found range constraint." << std::endl;
2585  return false;
2586  }
2587  return true;
2588  }
2590  const unit_vector_var_decl& /*x*/) const {
2591  error_msgs_ << "require unconstrained variable declaration."
2592  << " found unit_vector." << std::endl;
2593  return false;
2594  }
2596  const {
2597  error_msgs_ << "require unconstrained variable declaration."
2598  << " found simplex." << std::endl;
2599  return false;
2600  }
2602  const {
2603  error_msgs_ << "require unconstrained variable declaration."
2604  << " found ordered." << std::endl;
2605  return false;
2606  }
2608  const positive_ordered_var_decl& /*x*/) const {
2609  error_msgs_ << "require unconstrained variable declaration."
2610  << " found positive_ordered." << std::endl;
2611  return false;
2612  }
2614  const cholesky_factor_var_decl& /*x*/) const {
2615  error_msgs_ << "require unconstrained variable declaration."
2616  << " found cholesky_factor." << std::endl;
2617  return false;
2618  }
2620  const cholesky_corr_var_decl& /*x*/) const {
2621  error_msgs_ << "require unconstrained variable declaration."
2622  << " found cholesky_factor_corr." << std::endl;
2623  return false;
2624  }
2626  const cov_matrix_var_decl& /*x*/) const {
2627  error_msgs_ << "require unconstrained variable declaration."
2628  << " found cov_matrix." << std::endl;
2629  return false;
2630  }
2632  const corr_matrix_var_decl& /*x*/) const {
2633  error_msgs_ << "require unconstrained variable declaration."
2634  << " found corr_matrix." << std::endl;
2635  return false;
2636  }
2637 
2638 
2639  data_only_expression::data_only_expression(std::stringstream& error_msgs,
2640  variable_map& var_map)
2641  : error_msgs_(error_msgs),
2642  var_map_(var_map) {
2643  }
2644  bool data_only_expression::operator()(const nil& /*e*/) const {
2645  return true;
2646  }
2648  return true;
2649  }
2651  return true;
2652  }
2654  for (size_t i = 0; i < x.args_.size(); ++i)
2655  if (!boost::apply_visitor(*this, x.args_[i].expr_))
2656  return false;
2657  return true;
2658  }
2660  for (size_t i = 0; i < x.args_.size(); ++i)
2661  if (!boost::apply_visitor(*this, x.args_[i].expr_))
2662  return false;
2663  return true;
2664  }
2666  for (size_t i = 0; i < x.args_.size(); ++i)
2667  if (!boost::apply_visitor(*this, x.args_[i].expr_))
2668  return false;
2669  return true;
2670  }
2672  scope var_scope = var_map_.get_scope(x.name_);
2673  bool is_data = var_scope.allows_size();
2674  if (!is_data) {
2675  error_msgs_ << "non-data variables not allowed"
2676  << " in dimension declarations."
2677  << std::endl
2678  << " found variable=" << x.name_
2679  << "; declared in block=";
2680  print_scope(error_msgs_, var_scope);
2682  }
2683  return is_data;
2684  }
2686  return boost::apply_visitor(*this, x.y0_.expr_)
2687  && boost::apply_visitor(*this, x.theta_.expr_);
2688  }
2690  const {
2691  return boost::apply_visitor(*this, x.y0_.expr_)
2692  && boost::apply_visitor(*this, x.theta_.expr_);
2693  }
2695  return boost::apply_visitor(*this, x.theta_.expr_);
2696  }
2698  const {
2699  return boost::apply_visitor(*this, x.theta_.expr_);
2700  }
2702  const {
2703  return boost::apply_visitor(*this, x.shared_params_.expr_)
2704  && boost::apply_visitor(*this, x.job_params_.expr_);
2705  }
2707  for (size_t i = 0; i < x.args_.size(); ++i)
2708  if (!boost::apply_visitor(*this, x.args_[i].expr_))
2709  return false;
2710  return true;
2711  }
2713  if (!boost::apply_visitor(*this, x.expr_.expr_))
2714  return false;
2715  for (size_t i = 0; i < x.dimss_.size(); ++i)
2716  for (size_t j = 0; j < x.dimss_[i].size(); ++j)
2717  if (!boost::apply_visitor(*this, x.dimss_[i][j].expr_))
2718  return false;
2719  return true;
2720  }
2722  return boost::apply_visitor(*this, x.expr_.expr_);
2723  }
2725  return boost::apply_visitor(*this, x.cond_.expr_)
2726  && boost::apply_visitor(*this, x.true_val_.expr_)
2727  && boost::apply_visitor(*this, x.false_val_.expr_);
2728  }
2730  return boost::apply_visitor(*this, x.left.expr_)
2731  && boost::apply_visitor(*this, x.right.expr_);
2732  }
2734  return boost::apply_visitor(*this, x.subject.expr_);
2735  }
2736 
2737  void validate_decl_constraints::operator()(const bool& allow_constraints,
2738  const bool& declaration_ok,
2739  const var_decl& var_decl,
2740  bool& pass,
2741  std::stringstream& error_msgs)
2742  const {
2743  if (!declaration_ok) {
2744  error_msgs << "Problem with declaration." << std::endl;
2745  pass = false;
2746  return; // short-circuits test of constraints
2747  }
2748  if (allow_constraints) {
2749  pass = true;
2750  return;
2751  }
2752  validate_no_constraints_vis vis(error_msgs);
2753  pass = boost::apply_visitor(vis, var_decl.decl_);
2754  }
2755  boost::phoenix::function<validate_decl_constraints>
2757 
2759  const var_decl& var_decl,
2760  bool& pass,
2761  std::stringstream& error_msgs)
2762  const {
2763  if (!var_decl.has_def()) return;
2764 
2765  // validate that assigment is allowed in this block
2766  if (!var_scope.allows_assignment()) {
2767  error_msgs << "variable definition not possible in this block"
2768  << std::endl;
2769  pass = false;
2770  }
2771 
2772  // validate type
2773  expr_type decl_type(var_decl.base_decl().base_type_,
2774  var_decl.dims().size());
2775  expr_type def_type = var_decl.def().expression_type();
2776 
2777  bool types_compatible
2778  = (decl_type.is_primitive()
2779  && def_type.is_primitive()
2780  && (decl_type.type() == def_type.type()
2781  || (decl_type.type().is_double_type()
2782  && def_type.type().is_int_type())))
2783  || (decl_type.type() == def_type.type());
2784  if (!types_compatible) {
2785  error_msgs << "variable definition base type mismatch,"
2786  << " variable declared as base type: ";
2787  write_base_expr_type(error_msgs, decl_type.type());
2788  error_msgs << " variable definition has base: ";
2789  write_base_expr_type(error_msgs, def_type.type());
2790  pass = false;
2791  }
2792  // validate dims
2793  if (decl_type.num_dims() != def_type.num_dims()) {
2794  error_msgs << "variable definition dimensions mismatch,"
2795  << " definition specifies "
2796  << decl_type.num_dims()
2797  << ", declaration specifies "
2798  << def_type.num_dims();
2799  pass = false;
2800  }
2801  return;
2802  }
2803  boost::phoenix::function<validate_definition>
2805 
2807  reserved_word_set_.insert(w);
2808  }
2809  bool validate_identifier::contains(const std::set<std::string>& s,
2810  const std::string& x) const {
2811  return s.find(x) != s.end();
2812  }
2814  const {
2815  return contains(reserved_word_set_, identifier)
2816  || (contains(function_signatures::instance().key_set(), identifier)
2817  && !contains(const_fun_name_set_, identifier));
2818  }
2819 
2821  // constant functions which may be used as identifiers
2822  const_fun_name_set_.insert("pi");
2823  const_fun_name_set_.insert("e");
2824  const_fun_name_set_.insert("sqrt2");
2825  const_fun_name_set_.insert("log2");
2826  const_fun_name_set_.insert("log10");
2827  const_fun_name_set_.insert("not_a_number");
2828  const_fun_name_set_.insert("positive_infinity");
2829  const_fun_name_set_.insert("negative_infinity");
2830  const_fun_name_set_.insert("epsilon");
2831  const_fun_name_set_.insert("negative_epsilon");
2832  const_fun_name_set_.insert("machine_precision");
2833 
2834  // illegal identifiers
2835  reserve("for");
2836  reserve("in");
2837  reserve("while");
2838  reserve("repeat");
2839  reserve("until");
2840  reserve("if");
2841  reserve("then");
2842  reserve("else");
2843  reserve("true");
2844  reserve("false");
2845 
2846  reserve("int");
2847  reserve("real");
2848  reserve("vector");
2849  reserve("unit_vector");
2850  reserve("simplex");
2851  reserve("ordered");
2852  reserve("positive_ordered");
2853  reserve("row_vector");
2854  reserve("matrix");
2855  reserve("cholesky_factor_cov");
2856  reserve("cholesky_factor_corr");
2857  reserve("cov_matrix");
2858  reserve("corr_matrix");
2859 
2860  reserve("target");
2861 
2862  reserve("model");
2863  reserve("data");
2864  reserve("parameters");
2865  reserve("quantities");
2866  reserve("transformed");
2867  reserve("generated");
2868 
2869  reserve("var");
2870  reserve("fvar");
2871  reserve("STAN_MAJOR");
2872  reserve("STAN_MINOR");
2873  reserve("STAN_PATCH");
2874  reserve("STAN_MATH_MAJOR");
2875  reserve("STAN_MATH_MINOR");
2876  reserve("STAN_MATH_PATCH");
2877 
2878  reserve("alignas");
2879  reserve("alignof");
2880  reserve("and");
2881  reserve("and_eq");
2882  reserve("asm");
2883  reserve("auto");
2884  reserve("bitand");
2885  reserve("bitor");
2886  reserve("bool");
2887  reserve("break");
2888  reserve("case");
2889  reserve("catch");
2890  reserve("char");
2891  reserve("char16_t");
2892  reserve("char32_t");
2893  reserve("class");
2894  reserve("compl");
2895  reserve("const");
2896  reserve("constexpr");
2897  reserve("const_cast");
2898  reserve("continue");
2899  reserve("decltype");
2900  reserve("default");
2901  reserve("delete");
2902  reserve("do");
2903  reserve("double");
2904  reserve("dynamic_cast");
2905  reserve("else");
2906  reserve("enum");
2907  reserve("explicit");
2908  reserve("export");
2909  reserve("extern");
2910  reserve("false");
2911  reserve("float");
2912  reserve("for");
2913  reserve("friend");
2914  reserve("goto");
2915  reserve("if");
2916  reserve("inline");
2917  reserve("int");
2918  reserve("long");
2919  reserve("mutable");
2920  reserve("namespace");
2921  reserve("new");
2922  reserve("noexcept");
2923  reserve("not");
2924  reserve("not_eq");
2925  reserve("nullptr");
2926  reserve("operator");
2927  reserve("or");
2928  reserve("or_eq");
2929  reserve("private");
2930  reserve("protected");
2931  reserve("public");
2932  reserve("register");
2933  reserve("reinterpret_cast");
2934  reserve("return");
2935  reserve("short");
2936  reserve("signed");
2937  reserve("sizeof");
2938  reserve("static");
2939  reserve("static_assert");
2940  reserve("static_cast");
2941  reserve("struct");
2942  reserve("switch");
2943  reserve("template");
2944  reserve("this");
2945  reserve("thread_local");
2946  reserve("throw");
2947  reserve("true");
2948  reserve("try");
2949  reserve("typedef");
2950  reserve("typeid");
2951  reserve("typename");
2952  reserve("union");
2953  reserve("unsigned");
2954  reserve("using");
2955  reserve("virtual");
2956  reserve("void");
2957  reserve("volatile");
2958  reserve("wchar_t");
2959  reserve("while");
2960  reserve("xor");
2961  reserve("xor_eq");
2962 
2963  // function names declared in signatures
2965  using std::set;
2966  using std::string;
2968 
2969  set<string> fun_names = sigs.key_set();
2970  for (set<string>::iterator it = fun_names.begin();
2971  it != fun_names.end();
2972  ++it)
2973  if (!contains(const_fun_name_set_, *it))
2974  reserve(*it);
2975  }
2976 
2978  bool& pass,
2979  std::stringstream& error_msgs) const {
2980  int len = identifier.size();
2981  if (len >= 2
2982  && identifier[len-1] == '_'
2983  && identifier[len-2] == '_') {
2984  error_msgs << "variable identifier (name) may"
2985  << " not end in double underscore (__)"
2986  << std::endl
2987  << " found identifer=" << identifier << std::endl;
2988  pass = false;
2989  return;
2990  }
2991  size_t period_position = identifier.find('.');
2992  if (period_position != std::string::npos) {
2993  error_msgs << "variable identifier may not contain a period (.)"
2994  << std::endl
2995  << " found period at position (indexed from 0)="
2996  << period_position
2997  << std::endl
2998  << " found identifier=" << identifier
2999  << std::endl;
3000  pass = false;
3001  return;
3002  }
3003  if (identifier_exists(identifier)) {
3004  error_msgs << "variable identifier (name) may not be reserved word"
3005  << std::endl
3006  << " found identifier=" << identifier
3007  << std::endl;
3008  pass = false;
3009  return;
3010  }
3011  pass = true;
3012  }
3013  boost::phoenix::function<validate_identifier> validate_identifier_f;
3014 
3015  // copies single dimension from M to N if only M declared
3018  if (is_nil(var_decl.N_))
3019  var_decl.N_ = var_decl.M_;
3020  }
3021  boost::phoenix::function<copy_square_cholesky_dimension_if_necessary>
3023 
3025  std::stringstream& /*error_msgs*/) const {
3026  r = range();
3027  }
3028  boost::phoenix::function<empty_range> empty_range_f;
3029 
3031  const expression& expr,
3032  bool& pass,
3033  std::stringstream& error_msgs) const {
3034  range.low_ = expr;
3035  validate_int_expr validator;
3036  validator(expr, pass, error_msgs);
3037  }
3038  boost::phoenix::function<set_int_range_lower> set_int_range_lower_f;
3039 
3041  const expression& expr,
3042  bool& pass,
3043  std::stringstream& error_msgs) const {
3044  range.high_ = expr;
3045  validate_int_expr validator;
3046  validator(expr, pass, error_msgs);
3047  }
3048  boost::phoenix::function<set_int_range_upper> set_int_range_upper_f;
3049 
3051  const scope& var_scope,
3052  bool& pass,
3053  variable_map& var_map,
3054  std::stringstream& error_msgs)
3055  const {
3056  if (!expr.expression_type().is_primitive_int()) {
3057  error_msgs << "dimension declaration requires expression"
3058  << " denoting integer; found type="
3059  << expr.expression_type()
3060  << std::endl;
3061  pass = false;
3062  return;
3063  }
3064 
3065  if (!var_scope.is_local()) {
3066  data_only_expression vis(error_msgs, var_map);
3067  bool only_data_dimensions = boost::apply_visitor(vis, expr.expr_);
3068  pass = only_data_dimensions;
3069  return;
3070  }
3071 
3072  // don't need to check data vs. parameter in dimensions for
3073  // local variable declarations
3074  pass = true;
3075  }
3076  boost::phoenix::function<validate_int_data_expr> validate_int_data_expr_f;
3077 
3079  const expression& expr,
3080  bool& pass,
3081  std::stringstream& error_msgs)
3082  const {
3083  range.low_ = expr;
3084  validate_double_expr validator;
3085  validator(expr, pass, error_msgs);
3086  }
3087  boost::phoenix::function<set_double_range_lower> set_double_range_lower_f;
3088 
3090  const expression& expr,
3091  bool& pass,
3092  std::stringstream& error_msgs)
3093  const {
3094  range.high_ = expr;
3095  validate_double_expr validator;
3096  validator(expr, pass, error_msgs);
3097  }
3098  boost::phoenix::function<set_double_range_upper> set_double_range_upper_f;
3099 
3100  template <typename T>
3101  void add_var::operator()(var_decl& var_decl_result, const T& var_decl,
3102  variable_map& vm, bool& pass,
3103  const scope& var_scope,
3104  std::ostream& error_msgs) const {
3105  if (vm.exists(var_decl.name_)) {
3106  pass = false;
3107  error_msgs << "duplicate declaration of variable, name="
3108  << var_decl.name_;
3109 
3110  error_msgs << "; attempt to redeclare as ";
3111  print_scope(error_msgs, var_scope);
3112 
3113  error_msgs << "; original declaration as ";
3114  print_scope(error_msgs, vm.get_scope(var_decl.name_));
3115 
3116  error_msgs << std::endl;
3117  var_decl_result = var_decl;
3118  return;
3119  }
3120  if (var_scope.par_or_tpar()
3121  && var_decl.base_type_.is_int_type()) {
3122  pass = false;
3123  error_msgs << "parameters or transformed parameters"
3124  << " cannot be integer or integer array; "
3125  << " found declared type int, parameter name="
3126  << var_decl.name_
3127  << std::endl;
3128  var_decl_result = var_decl;
3129  return;
3130  }
3131  pass = true;
3132  vm.add(var_decl.name_, var_decl, var_scope);
3133  var_decl_result = var_decl;
3134  }
3135  boost::phoenix::function<add_var> add_var_f;
3136 
3137  template void add_var::operator()(var_decl&, const int_var_decl&,
3138  variable_map&, bool&, const scope&,
3139  std::ostream&) const;
3140  template void add_var::operator()(var_decl&, const double_var_decl&,
3141  variable_map&, bool&, const scope&,
3142  std::ostream&) const;
3143  template void add_var::operator()(var_decl&, const vector_var_decl&,
3144  variable_map&, bool&, const scope&,
3145  std::ostream&) const;
3146  template void add_var::operator()(var_decl&, const row_vector_var_decl&,
3147  variable_map&, bool&, const scope&,
3148  std::ostream&) const;
3149  template void add_var::operator()(var_decl&, const matrix_var_decl&,
3150  variable_map&, bool&, const scope&,
3151  std::ostream&) const;
3152  template void add_var::operator()(var_decl&, const simplex_var_decl&,
3153  variable_map&, bool&, const scope&,
3154  std::ostream&) const;
3155  template void add_var::operator()(var_decl&, const unit_vector_var_decl&,
3156  variable_map&, bool&, const scope&,
3157  std::ostream&) const;
3158  template void add_var::operator()(var_decl&, const ordered_var_decl&,
3159  variable_map&, bool&, const scope&,
3160  std::ostream&) const;
3161  template void add_var::operator()(var_decl&,
3163  variable_map&, bool&, const scope&,
3164  std::ostream&) const;
3165  template void add_var::operator()(var_decl&,
3166  const cholesky_factor_var_decl&,
3167  variable_map&, bool&, const scope&,
3168  std::ostream&) const;
3169  template void add_var::operator()(var_decl&, const cholesky_corr_var_decl&,
3170  variable_map&, bool&, const scope&,
3171  std::ostream&) const;
3172  template void add_var::operator()(var_decl&, const cov_matrix_var_decl&,
3173  variable_map&, bool&, const scope&,
3174  std::ostream&) const;
3175  template void add_var::operator()(var_decl&, const corr_matrix_var_decl&,
3176  variable_map&, bool&, const scope&,
3177  std::ostream&) const;
3178 
3179  void validate_in_loop::operator()(bool in_loop, bool& pass,
3180  std::ostream& error_msgs) const {
3181  pass = in_loop;
3182  if (!pass)
3183  error_msgs << "Break and continue statements are only allowed"
3184  << " in the body of a for-loop or while-loop."
3185  << std::endl;
3186  }
3187  boost::phoenix::function<validate_in_loop> validate_in_loop_f;
3188 
3190  std::ostream& error_msgs) const {
3191  // ill-formed shouldn't be possible, but just in case
3192  pass = !(e.expression_type().type().is_void_type()
3194  if (!pass)
3195  error_msgs << "Error: expected printable (non-void) expression."
3196  << std::endl;
3197  }
3198  boost::phoenix::function<non_void_expression> non_void_expression_f;
3199 
3201  const origin_block& program_block)
3202  const {
3203  var_scope = scope(program_block);
3204  }
3205  boost::phoenix::function<set_var_scope> set_var_scope_f;
3206 
3207  void set_data_origin::operator()(scope& var_scope) const {
3208  var_scope = scope(data_origin);
3209  }
3210  boost::phoenix::function<set_data_origin> set_data_origin_f;
3211 
3213  const origin_block& program_block)
3214  const {
3215  var_scope = scope(program_block, true);
3216  }
3217  boost::phoenix::function<set_var_scope_local> set_var_scope_local_f;
3218 
3220  const scope& scope_enclosing)
3221  const {
3222  origin_block enclosing_block = scope_enclosing.program_block();
3223  var_scope = scope(enclosing_block, true);
3224  }
3225  boost::phoenix::function<reset_var_scope> reset_var_scope_f;
3226 
3227  void trace::operator()(const std::string& msg) const {
3228  }
3229  boost::phoenix::function<trace> trace_f;
3230 
3231  void deprecate_pound_comment::operator()(std::ostream& error_msgs) const {
3232  error_msgs << "Warning (non-fatal): Comments beginning with #"
3233  << " are deprecated. Please use // in place of #"
3234  << " for line comments." << std::endl;
3235  }
3236  boost::phoenix::function<deprecate_pound_comment>
3238 
3239  }
3240 }
3241 
3242 #endif
const XML_Char int len
Definition: expat.h:262
expr_type get_result_type(const std::string &name, const std::vector< expr_type > &args, std::ostream &error_msgs, bool sampling_error_style=false)
expression high_
Definition: range.hpp:25
boost::phoenix::function< validate_return_allowed > validate_return_allowed_f
void operator()(expression &expr_result, const expression &expr, bool &pass, std::ostream &error_msgs) const
boost::phoenix::function< require_vbar > require_vbar_f
bool fun_exists(const std::set< std::pair< std::string, function_signature_t > > &existing, const std::pair< std::string, function_signature_t > &name_sig, bool name_only=true)
const XML_Char * name
Definition: expat.h:151
void operator()(const algebra_solver_control &alg_fun, const variable_map &var_map, bool &pass, std::ostream &error_msgs) const
std::string strip_ccdf_suffix(const std::string &dist_fun)
base_var_decl base_decl() const
boost::phoenix::function< set_void_function > set_void_function_f
bool lhs_var_occurs_on_rhs() const
Definition: assgn_def.hpp:21
void operator()(expression &expr1, const expression &expr2, std::ostream &error_msgs) const
boost::phoenix::function< add_while_body > add_while_body_f
boost::phoenix::function< set_var_scope_local > set_var_scope_local_f
bool fun() const
Definition: scope_def.hpp:44
bool has_lp_suffix(const std::string &name)
void operator()(const std::string &identifier, bool &pass, std::stringstream &error_msgs) const
const int function_argument_origin
void operator()(const expr_type &arg_type, bool &pass, std::ostream &error_msgs) const
void operator()(scope var_scope, bool &pass, std::ostream &error_msgs) const
boost::phoenix::function< set_int_range_upper > set_int_range_upper_f
boost::phoenix::function< validate_algebra_solver_control > validate_algebra_solver_control_f
expr_type type_
Definition: fun.hpp:37
void operator()(const integrate_ode &ode_fun, const variable_map &var_map, bool &pass, std::ostream &error_msgs) const
void operator()(expression &e, array_expr &array_expr, const scope &var_scope, bool &pass, const variable_map &var_map, std::ostream &error_msgs) const
boost::phoenix::function< validate_assgn > validate_assgn_f
boost::phoenix::function< set_var_type > set_var_type_f
void set_fun_type(fun &fun, std::ostream &error_msgs)
void operator()(expression &e, row_vector_expr &vec_expr, const scope &var_scope, bool &pass, const variable_map &var_map, std::ostream &error_msgs) const
set< int >::iterator it
std::string name_
Definition: variable.hpp:18
void operator()(expression &e, std::vector< idx > &idxs, bool &pass, std::ostream &error_msgs) const
origin_block program_block() const
Definition: scope_def.hpp:20
distribution dist_
Definition: sample.hpp:54
std::vector< expression > dims_
boost::phoenix::function< set_int_range_lower > set_int_range_lower_f
std::string original_name_
Definition: fun.hpp:27
void operator()(const expression &expr, bool &pass, std::stringstream &error_msgs) const
boost::phoenix::function< binary_op_expr > binary_op_f
std::string strip_cdf_suffix(const std::string &dist_fun)
boost::phoenix::function< validate_expr_type3 > validate_expr_type3_f
void operator()(std::stringstream &error_msgs) const
bool has_rng_lp_suffix(const std::string &s)
std::vector< expression > args_
Definition: array_expr.hpp:21
std::size_t num_dims_
Definition: expr_type.hpp:31
bool allows_size() const
Definition: scope_def.hpp:90
boost::phoenix::function< deprecated_integrate_ode > deprecated_integrate_ode_f
int num_dimss(std::vector< std::vector< stan::lang::expression > > &dimss)
const int parameter_origin
void operator()(const expression &e, bool &pass, std::ostream &error_msgs) const
boost::phoenix::function< add_array_loop_identifier > add_array_loop_identifier_f
bool is_local() const
Definition: scope_def.hpp:24
boost::phoenix::function< set_fun_type_named > set_fun_type_named_f
void add(const std::string &name, const base_var_decl &base_decl, const scope &scope_decl)
bool has_cdf_suffix(const std::string &name)
boost::phoenix::function< set_no_op > set_no_op_f
boost::phoenix::function< validate_conditional_op > validate_conditional_op_f
const int function_argument_origin_rng
boost::phoenix::function< set_fun_params_scope > set_fun_params_scope_f
void operator()(function_decl_def &decl, bool &pass, std::ostream &error_msgs) const
void qualify_builtins(fun &f)
std::size_t num_dims() const
bool allows_sampling() const
Definition: scope_def.hpp:84
boost::phoenix::function< add_params_var > add_params_var_f
bool deprecate_fun(const std::string &old_name, const std::string &new_name, fun &f, std::ostream &msgs)
void operator()(scope &var_scope, const origin_block &program_block) const
void operator()(assgn &a, bool &pass, const variable_map &vm, std::ostream &error_msgs) const
std::string op_name_
Definition: assgn.hpp:71
std::string get_prob_fun(const std::string &dist_name)
const int loop_identifier_origin
static function_signatures & instance()
void operator()(range &range, const expression &expr, bool &pass, std::stringstream &error_msgs) const
void operator()(scope var_scope, bool &pass, std::ostream &error_msgs) const
void operator()(T &line, const I &begin, const I &end) const
boost::phoenix::function< deprecate_increment_log_prob > deprecate_increment_log_prob_f
void operator()(expression &expr1, const expression &expr2, bool &pass, std::ostream &error_msgs) const
std::string op_
Definition: assgn.hpp:65
boost::phoenix::function< program_error > program_error_f
void operator()(expression &expr1, const expression &expr2, std::ostream &error_msgs) const
std::vector< expression > args_
Definition: fun.hpp:32
validate_no_constraints_vis(std::stringstream &error_msgs)
void operator()(std::string &op, std::ostream &error_msgs) const
bool has_prob_fun_suffix(const std::string &name)
void operator()(const std::string &name, std::string &name_local, bool &pass, variable_map &vm, std::stringstream &error_msgs) const
std::string get_cdf(const std::string &dist_name)
Definition: get_cdf_def.hpp:10
bool is_double_return(const std::string &function_name, const std::vector< expr_type > &arg_types, std::ostream &error_msgs)
void operator()(const std::vector< var_decl > &var_decls, variable_map &vm) const
void operator()(const expression &e, bool &pass, std::ostream &error_msgs) const
bool exists(const std::string &name) const
void operator()(const std::string &msg) const
boost::phoenix::function< remove_params_var > remove_params_var_f
void operator()(scope &var_scope) const
void print_scope(std::ostream &o, const scope &var_scope)
void operator()(sample &s, const variable_map &var_map, bool &pass, std::ostream &error_msgs) const
bool void_fun() const
Definition: scope_def.hpp:59
bool is_primitive_int() const
expr_type infer_type_indexing(const base_expr_type &base_type, std::size_t dims, std::size_t num_indexes)
void operator()(arg_decl &decl, scope &scope, bool &pass, variable_map &vm, std::ostream &error_msgs) const
bool has_prob_suffix(const std::string &s)
expression expr_
Definition: index_op.hpp:18
dictionary contains
void operator()(range &range, const expression &expr, bool &pass, std::stringstream &error_msgs) const
void operator()(while_statement &ws, const expression &e, bool &pass, std::stringstream &error_msgs) const
boost::spirit::line_pos_iterator< input_iterator_t > pos_iterator_t
void operator()(expression &expr_result, const expression &expr, std::ostream &error_msgs) const
bool discrete_first_arg(const std::string &name) const
void operator()(cholesky_factor_var_decl &var_decl) const
void operator()(expression &expr1, bool &pass, const expression &expr2, std::ostream &error_msgs) const
boost::phoenix::function< validate_ints_expression > validate_ints_expression_f
bool has_same_shape(const expr_type &lhs_type, const expression &rhs_expr, const std::string &name, const std::string &stmt_type, std::ostream &error_msgs)
expression job_data_i_
Definition: map_rect.hpp:60
boost::phoenix::function< add_conditional_condition > add_conditional_condition_f
boost::phoenix::function< empty_range > empty_range_f
bool is_no_op_statement() const
boost::phoenix::function< subtraction_expr3 > subtraction3_f
boost::phoenix::function< addition_expr3 > addition3_f
void operator()(std::string &fname, bool &pass, std::ostream &error_msgs) const
void validate_integrate_ode_non_control_args(const T &ode_fun, const variable_map &var_map, bool &pass, std::ostream &error_msgs)
void replace_suffix(const std::string &old_suffix, const std::string &new_suffix, fun &f)
boost::phoenix::function< store_loop_identifier > store_loop_identifier_f
void operator()(const expression &e, bool &pass, std::ostream &error_msgs) const
boost::phoenix::function< add_var > add_var_f
expression def() const
void operator()(const integrate_ode_control &ode_fun, const variable_map &var_map, bool &pass, std::ostream &error_msgs) const
bool allows_lp_fun() const
Definition: scope_def.hpp:70
boost::phoenix::function< deprecate_pound_comment > deprecate_pound_comment_f
boost::phoenix::function< exponentiation_expr > exponentiation_f
void operator()(const expression &expr, bool &pass, std::stringstream &error_msgs) const
boost::phoenix::function< add_loop_identifier > add_loop_identifier_f
boost::phoenix::function< validate_lhs_var_assgn > validate_lhs_var_assgn_f
std::string name_
Definition: fun.hpp:21
std::string get_ccdf(const std::string &dist_name)
boost::phoenix::function< set_double_range_lower > set_double_range_lower_f
boost::phoenix::function< validate_void_return_allowed > validate_void_return_allowed_f
const int function_argument_origin_lp
bool starts_with(const std::string &p, const std::string &s)
Definition: starts_with.hpp:17
void operator()(const expression &e, bool &pass) const
void operator()(omni_idx &val) const
void operator()(expression &expr1, const expression &expr2, const std::string &op, const std::string &fun_name, std::ostream &error_msgs) const
void operator()(scope &var_scope, const scope &scope_enclosing) const
boost::phoenix::function< infer_array_expr_type > infer_array_expr_type_f
boost::phoenix::function< validate_non_void_arg_function > validate_non_void_arg_f
const XML_Char * s
Definition: expat.h:262
expr_type expression_type() const
bool contains(const std::set< std::string > &s, const std::string &x) const
boost::phoenix::function< non_void_expression > non_void_expression_f
void validate_algebra_solver_non_control_args(const T &alg_fun, const variable_map &var_map, bool &pass, std::ostream &error_msgs)
boost::phoenix::function< trace > trace_f
void reserve(const std::string &w)
bool has_def() const
boost::phoenix::function< remove_loop_identifier > remove_loop_identifier_f
void operator()(expression &expr1, const expression &expr2, std::ostream &error_msgs) const
void operator()(range &range, const expression &expr, bool &pass, std::stringstream &error_msgs) const
boost::phoenix::function< add_conditional_body > add_conditional_body_f
Definition: NueSkimmer.h:24
boost::phoenix::function< transpose_expr > transpose_f
void operator()(pos_iterator_t _begin, pos_iterator_t _end, pos_iterator_t _where, variable_map &vm, std::stringstream &error_msgs, const io::program_reader &reader) const
boost::phoenix::function< validate_definition > validate_definition_f
void operator()(std::ostream &error_msgs) const
expression job_params_
Definition: map_rect.hpp:50
boost::phoenix::function< non_void_return_msg > non_void_return_msg_f
boost::phoenix::function< multiplication_expr > multiplication_f
bool is_nil(const expression &e)
Definition: is_nil_def.hpp:10
void operator()(const std::string &s, bool &pass) const
void operator()(const bool &allow_constraints, const bool &declaration_ok, const var_decl &var_decl, bool &pass, std::stringstream &error_msgs) const
boost::phoenix::function< set_data_origin > set_data_origin_f
boost::phoenix::function< validate_identifier > validate_identifier_f
void operator()(expression &expression, std::vector< std::vector< stan::lang::expression > > &dimss, bool &pass, std::ostream &error_msgs) const
boost::phoenix::function< validate_int_expression_warn > validate_int_expression_warn_f
std::vector< expression > args_
double lang
Definition: runWimpSim.h:113
void remove(const std::string &name)
const int void_function_argument_origin_rng
boost::phoenix::function< validate_int_expr > validate_int_expr_f
function_signature_t get_definition(const std::string &name, const function_signature_t &sig)
static constexpr double L
bool fun_name_exists(const std::string &name)
void operator()(scope &var_scope, variable_map &vm) const
void operator()(const expr_type &return_type, scope &var_scope, bool &pass, std::ostream &error_msgs) const
void operator()(range &range, const expression &expr, bool &pass, std::stringstream &error_msgs) const
boost::phoenix::function< validate_decl_constraints > validate_decl_constraints_f
bool is_primitive() const
const double a
void operator()(scope var_scope, bool &pass, std::ostream &error_msgs) const
bool ends_with(const std::string &suffix, const std::string &s)
bool returns_type(const expr_type &return_type, const statement &statement, std::ostream &error_msgs)
boost::phoenix::function< add_matrix_loop_identifier > add_matrix_loop_identifier_f
boost::phoenix::function< add_expression_dimss > add_expression_dimss_f
void operator()(expression &expr1, const expression &expr2, std::ostream &error_msgs) const
void operator()(const scope &var_scope, const var_decl &var_decl, bool &pass, std::stringstream &error_msgs) const
boost::phoenix::function< validate_prob_fun > validate_prob_fun_f
boost::phoenix::function< add_line_number > add_line_number_f
bool is_defined(const std::string &name, const function_signature_t &sig)
void operator()(bool &pass, std::set< std::pair< std::string, function_signature_t > > &declared, std::set< std::pair< std::string, function_signature_t > > &defined, std::ostream &error_msgs, bool allow_undefined) const
boost::phoenix::function< reset_var_scope > reset_var_scope_f
boost::phoenix::function< validate_int_data_expr > validate_int_data_expr_f
std::vector< idx > idxs_
Definition: assgn.hpp:60
void add(const std::string &name, const expr_type &result_type, const std::vector< function_arg_type > &arg_types)
void operator()(const scope &var_scope, bool &pass, std::stringstream &error_msgs) const
bool is_ill_formed() const
expression shared_params_
Definition: map_rect.hpp:45
std::vector< expression > args_
std::vector< path_line_t > trace_t
boost::phoenix::function< validate_int_expr_silent > validate_int_expr_silent_f
base_expr_type type() const
expr_type indexed_type(const expression &e, const std::vector< idx > &idxs)
void operator()(variable &var_expr, expression &val, variable_map &vm, std::ostream &error_msgs, bool &pass) const
#define R(x)
void operator()(const expression &expr, bool &pass, std::ostream &error_msgs) const
const double j
Definition: BetheBloch.cxx:29
bool has_low() const
Definition: range_def.hpp:14
void operator()(const expression &expr, bool &pass, std::stringstream &error_msgs) const
boost::phoenix::function< is_prob_fun > is_prob_fun_f
void operator()(conditional_statement &cs, const expression &e, bool &pass, std::stringstream &error_msgs) const
void operator()(const algebra_solver &alg_fun, const variable_map &var_map, bool &pass, std::ostream &error_msgs) const
trace_t trace(int target) const
void operator()(var_decl &var_decl_result, const T &var_decl, variable_map &vm, bool &pass, const scope &var_scope, std::ostream &error_msgs) const
expression job_data_r_
Definition: map_rect.hpp:55
const int void_function_argument_origin
boost::phoenix::function< set_omni_idx > set_omni_idx_f
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
void operator()(variable_map &vm) const
std::size_t get_num_dims(const std::string &name) const
void operator()(bool &pass, std::ostream &error_msgs) const
bool has_non_param_var(const expression &e, const variable_map &var_map)
void operator()(expression &expr1, const expression &expr2, const scope &var_scope, bool &pass, std::ostream &error_msgs) const
expression rhs_
Definition: assgn.hpp:77
boost::phoenix::function< validate_allow_sample > validate_allow_sample_f
const int data_origin
boost::phoenix::function< assign_lhs > assign_lhs_f
void operator()(size_t &lhs) const
void operator()(expression &fun_result, fun &fun, const scope &var_scope, bool &pass, const variable_map &var_map, std::ostream &error_msgs) const
void operator()(std::ostream &error_msgs) const
boost::phoenix::function< validate_integrate_ode > validate_integrate_ode_f
const int void_function_argument_origin_lp
boost::phoenix::function< validate_map_rect > validate_map_rect_f
boost::phoenix::function< validate_double_expr > validate_double_expr_f
base_expr_type base_type_
Definition: expr_type.hpp:26
base_var_decl base_variable_declaration() const
boost::phoenix::function< expression_as_statement > expression_as_statement_f
boost::phoenix::function< validate_pmf_pdf_variate > validate_pmf_pdf_variate_f
std::string name_
Definition: arg_decl.hpp:45
std::vector< arg_decl > arg_decls_
bool allows_assignment() const
Definition: scope_def.hpp:65
variable lhs_var_
Definition: assgn.hpp:55
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
std::set< std::string > key_set() const
void operator()(const std::string &name, variable_map &vm) const
void operator()(assgn &a, const scope &var_scope, bool &pass, const variable_map &vm, std::ostream &error_msgs) const
void operator()(const expression &expr, const scope &var_scope, bool &pass, variable_map &var_map, std::stringstream &error_msgs) const
bool has_var(const expression &e, const variable_map &var_map)
Definition: has_var_def.hpp:10
boost::phoenix::function< elt_multiplication_expr > elt_multiplication_f
bool is_univariate(const expr_type &et)
expression expr_
Definition: sample.hpp:49
std::vector< expression > dims_
bool has_ccdf_suffix(const std::string &name)
void operator()(assgn &a, const std::string &name, bool &pass, const variable_map &vm, std::ostream &error_msgs) const
boost::phoenix::function< infer_vec_or_matrix_expr_type > infer_vec_or_matrix_expr_type_f
void operator()(variable_map &vm) const
expression low_
Definition: range.hpp:19
void operator()(function_decl_def &decl, variable_map &vm) const
boost::phoenix::function< set_allows_sampling_origin > set_allows_sampling_origin_f
void operator()(expression &expr, bool &pass, std::ostream &error_msgs) const
void operator()(expression &expr1, const expression &expr2, std::ostream &error_msgs) const
boost::phoenix::function< logical_negate_expr > logical_negate_expr_f
bool identifier_exists(const std::string &identifier) const
boost::phoenix::function< validate_sample > validate_sample_f
boost::phoenix::function< increment_size_t > increment_size_t_f
boost::phoenix::function< add_function_signature > add_function_signature_f
void operator()(const function_decl_def &decl, bool &pass, std::set< std::pair< std::string, function_signature_t > > &functions_declared, std::set< std::pair< std::string, function_signature_t > > &functions_defined, std::ostream &error_msgs) const
bool allows_rng() const
Definition: scope_def.hpp:77
boost::phoenix::function< set_double_range_upper > set_double_range_upper_f
boost::phoenix::function< add_idxs > add_idxs_f
void operator()(function_decl_def &decl, bool &pass, std::ostream &error_msgs) const
void operator()(while_statement &ws, const statement &s) const
std::string strip_prob_fun_suffix(const std::string &dist_fun)
expression subject
Definition: unary_op.hpp:23
TRandom3 r(0)
void operator()(scope &var_scope, const origin_block &program_block) const
void operator()(const std::string &name, const scope &var_scope, variable_map &vm) const
bool non_void_fun() const
Definition: scope_def.hpp:53
boost::phoenix::function< unscope_locals > unscope_locals_f
std::ostream & write_base_expr_type(std::ostream &o, base_expr_type type)
void operator()(map_rect &mr, const variable_map &var_map, bool &pass, std::ostream &error_msgs) const
void operator()(const expression &e, bool &pass, std::ostream &error_msgs) const
bool is_simple_assignment() const
Definition: assgn_def.hpp:17
void set_user_defined(const std::pair< std::string, function_signature_t > &name_sig)
void dump_program_line(size_t idx_errline, int offset, const std::string &origin_file, size_t origin_line, const io::program_reader &reader, const std::vector< std::string > &program_lines, std::stringstream &error_msgs)
boost::phoenix::function< division_expr > division_f
boost::phoenix::function< unscope_variables > unscope_variables_f
boost::phoenix::function< elt_division_expr > elt_division_f
bool can_assign_to_lhs_var(const std::string &lhs_var_name, const scope &var_scope, const variable_map &vm, std::ostream &error_msgs)
void operator()(range &r, std::stringstream &) const
double T
Definition: Xdiff_gwt.C:5
boost::phoenix::function< validate_in_loop > validate_in_loop_f
void set_type(const base_expr_type &base_type, std::size_t num_dims)
std::string fun_name_
Definition: map_rect.hpp:40
void operator()(bool in_loop, bool &pass, std::ostream &error_msgs) const
boost::phoenix::function< negate_expr > negate_expr_f
expr_type infer_var_dims_type(const base_var_decl &var_type, const variable_dims &var_dims)
void operator()(L &lhs, const R &rhs) const
boost::phoenix::function< copy_square_cholesky_dimension_if_necessary > copy_square_cholesky_dimension_if_necessary_f
Double_t sum
Definition: plot.C:31
Float_t e
Definition: plot.C:35
boost::phoenix::function< validate_return_type > validate_return_type_f
bool is_primitive_double() const
boost::phoenix::function< add_fun_var > add_fun_var_f
void operator()(expression &expr1, const expression &expr2, std::ostream &error_msgs) const
std::vector< expression > args_
Definition: matrix_expr.hpp:21
std::vector< std::vector< expression > > dimss_
Definition: index_op.hpp:23
boost::phoenix::function< add_while_condition > add_while_condition_f
boost::phoenix::function< validate_int_expr_no_error_msgs > validate_int_expr_no_error_msgs_f
base_expr_type get_base_type(const std::string &name) const
bool deprecate_suffix(const std::string &deprecated_suffix, const std::string &replacement, fun &f, std::ostream &msgs)
boost::phoenix::function< validate_integrate_ode_control > validate_integrate_ode_control_f
Float_t w
Definition: plot.C:20
boost::phoenix::function< validate_algebra_solver > validate_algebra_solver_f
boost::phoenix::function< validate_declarations > validate_declarations_f
void operator()(no_op_statement &s) const
boost::phoenix::function< left_division_expr > left_division_f
boost::phoenix::function< set_void_return > set_void_return_f
boost::phoenix::function< modulus_expr > modulus_f
bool has_rng_suffix(const std::string &name)
data_only_expression(std::stringstream &error_msgs, variable_map &var_map)
void operator()(conditional_statement &cs, const statement &s) const
void operator()(conditional_op &cond_expr, const scope &var_scope, bool &pass, const variable_map &var_map, std::ostream &error_msgs) const
void operator()(return_statement &s) const
void operator()(const std::string &identifier, scope &var_scope) const
boost::phoenix::function< deprecate_old_assignment_op > deprecate_old_assignment_op_f
bool par_or_tpar() const
Definition: scope_def.hpp:34
boost::phoenix::function< set_var_scope > set_var_scope_f
std::pair< expr_type, std::vector< function_arg_type > > function_signature_t
std::vector< expression > dims() const
void generate_expression(const expression &e, bool user_facing, std::ostream &o)
boost::phoenix::function< set_lhs_var_assgn > set_lhs_var_assgn_f
boost::phoenix::function< validate_non_void_expression > validate_non_void_expression_f
scope get_scope(const std::string &name) const
void operator()(bool &pass, const stan::lang::expression &expr, std::stringstream &error_msgs) const
bool has_high() const
Definition: range_def.hpp:18