Stan  2.10.0
probability, sampling & optimization
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 
4 #include <stan/lang/ast.hpp>
7 #include <boost/format.hpp>
8 #include <boost/spirit/include/qi.hpp>
9 #include <boost/variant/apply_visitor.hpp>
10 #include <boost/variant/recursive_variant.hpp>
11 #include <cstddef>
12 #include <limits>
13 #include <climits>
14 #include <iomanip>
15 #include <iostream>
16 #include <map>
17 #include <set>
18 #include <stdexcept>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 namespace stan {
24 
25  namespace lang {
26 
27  bool has_prob_suffix(const std::string& s) {
28  return ends_with("_lpdf", s) || ends_with("_lpmf", s)
29  || ends_with("_lcdf", s) || ends_with("_lccdf", s);
30  }
31 
32  void replace_suffix(const std::string& old_suffix,
33  const std::string& new_suffix, fun& f) {
34  if (!ends_with(old_suffix, f.name_)) return;
35  f.original_name_ = f.name_;
36  f.name_ = f.name_.substr(0, f.name_.size() - old_suffix.size())
37  + new_suffix;
38  }
39 
40  bool deprecate_fun(const std::string& old_name, const std::string& new_name,
41  fun& f, std::ostream& msgs) {
42  if (f.name_ != old_name) return false;
43  f.original_name_ = f.name_;
44  f.name_ = new_name;
45  msgs << "Warning: Function name '" << old_name << "' is deprecated"
46  << " and will be removed in a later release; please replace"
47  << " with '" << new_name << "'" << std::endl;
48  return true;
49  }
50 
51  bool deprecate_suffix(const std::string& deprecated_suffix,
52  const std::string& replacement, fun& f,
53  std::ostream& msgs) {
54  if (!ends_with(deprecated_suffix, f.name_)) return false;
55  msgs << "Warning: Deprecated function '" << f.name_ << "';"
56  << " please replace suffix '" << deprecated_suffix
57  << "' with " << replacement << std::endl;
58  return true;
59  }
60 
61  bool validate_double_expr(const expression& expr,
62  std::stringstream& error_msgs) {
64  && !expr.expression_type().is_primitive_int()) {
65  error_msgs << "expression denoting real required; found type="
66  << expr.expression_type() << std::endl;
67  return false;
68  }
69  return true;
70  }
71 
72  void set_fun_type(fun& fun, std::ostream& error_msgs) {
73  std::vector<expr_type> arg_types;
74  for (size_t i = 0; i < fun.args_.size(); ++i)
75  arg_types.push_back(fun.args_[i].expression_type());
77  .get_result_type(fun.name_, arg_types, error_msgs);
78  }
79 
80  int num_dimss(std::vector<std::vector<stan::lang::expression> >& dimss) {
81  int sum = 0;
82  for (size_t i = 0; i < dimss.size(); ++i)
83  sum += dimss[i].size();
84  return sum;
85  }
86 
87  template <typename L, typename R>
88  void assign_lhs::operator()(L& lhs, const R& rhs) const {
89  lhs = rhs;
90  }
91  boost::phoenix::function<assign_lhs> assign_lhs_f;
92 
93  template void assign_lhs::operator()(expression&, const expression&) const;
94  template void assign_lhs::operator()(expression&, const double_literal&)
95  const;
96  template void assign_lhs::operator()(expression&, const int_literal&) const;
97  template void assign_lhs::operator()(expression&, const integrate_ode&)
98  const;
99  template void assign_lhs::operator()(expression&,
100  const integrate_ode_control&)
101  const;
102  template void assign_lhs::operator()(int&, const int&) const;
103  template void assign_lhs::operator()(size_t&, const size_t&) const;
104  template void assign_lhs::operator()(statement&, const statement&) const;
105  template void assign_lhs::operator()(std::vector<var_decl>&,
106  const std::vector<var_decl>&) const;
107  template void assign_lhs::operator()(std::vector<idx>&,
108  const std::vector<idx>&) const;
109  template void assign_lhs::operator()(
110  std::vector<std::vector<expression> >&,
111  const std::vector<std::vector<expression> >&) const;
112  template void assign_lhs::operator()(fun&, const fun&) const;
113  template void assign_lhs::operator()(variable&, const variable&) const;
114 
115  void validate_expr_type3::operator()(const expression& expr, bool& pass,
116  std::ostream& error_msgs) const {
117  pass = !expr.expression_type().is_ill_formed();
118  if (!pass)
119  error_msgs << "expression is ill formed" << std::endl;
120  }
121  boost::phoenix::function<validate_expr_type3> validate_expr_type3_f;
122 
123  void is_prob_fun::operator()(const std::string& s,
124  bool& pass) const {
125  pass = has_prob_suffix(s);
126  }
127  boost::phoenix::function<is_prob_fun> is_prob_fun_f;
128 
130  std::ostream& error_msgs) const {
131  if (expr1.expression_type().is_primitive()
132  && expr2.expression_type().is_primitive()) {
133  expr1 += expr2;
134  return;
135  }
136  std::vector<expression> args;
137  args.push_back(expr1);
138  args.push_back(expr2);
139  fun f("add", args);
140  set_fun_type(f, error_msgs);
141  expr1 = expression(f);
142  }
143  boost::phoenix::function<addition_expr3> addition3_f;
144 
146  const expression& expr2,
147  std::ostream& error_msgs) const {
148  if (expr1.expression_type().is_primitive()
149  && expr2.expression_type().is_primitive()) {
150  expr1 -= expr2;
151  return;
152  }
153  std::vector<expression> args;
154  args.push_back(expr1);
155  args.push_back(expr2);
156  fun f("subtract", args);
157  set_fun_type(f, error_msgs);
158  expr1 = expression(f);
159  }
160  boost::phoenix::function<subtraction_expr3> subtraction3_f;
161 
162  void increment_size_t::operator()(size_t& lhs) const {
163  ++lhs;
164  }
165  boost::phoenix::function<increment_size_t> increment_size_t_f;
166 
167 
169  bool& pass,
170  std::ostream& error_msgs) const {
171  pass = true;
172  expr_type cond_type = conditional_op.cond_.expression_type();
173  if (!cond_type.is_primitive_int()) {
174  error_msgs << "condition in ternary expression must be"
175  << " primitive int or real;"
176  << " found type=" << cond_type
177  << std::endl;
178  pass = false;
179  }
180  expr_type
181  true_val_type(conditional_op.true_val_.expression_type().type(),
182  conditional_op.true_val_.expression_type().num_dims_);
184  true_val_base_type = true_val_type.base_type_;
185  expr_type
186  false_val_type(conditional_op.false_val_.expression_type().type(),
187  conditional_op.false_val_.expression_type().num_dims_);
189  false_val_base_type = false_val_type.base_type_;
190 
191  bool types_compatible
192  = (true_val_type.is_primitive()
193  && false_val_type.is_primitive()
194  && (true_val_base_type == false_val_base_type
195  || (true_val_base_type == DOUBLE_T
196  && false_val_base_type == INT_T)
197  || (true_val_base_type == INT_T
198  && false_val_base_type == DOUBLE_T)))
199  || (true_val_type == false_val_type);
200 
201  if (!types_compatible) {
202  error_msgs << "base type mismatch in ternary expression,"
203  << " expression when true is: ";
204  write_base_expr_type(error_msgs, true_val_base_type);
205  error_msgs << "; expression when false is: ";
206  write_base_expr_type(error_msgs, false_val_base_type);
207  error_msgs << std::endl;
208  pass = false;
209  }
210  if (!pass) return;
211 
212  if (!true_val_type.is_primitive()) {
213  conditional_op.type_ = true_val_type;
214  } else {
215  conditional_op.type_ =
216  (true_val_base_type == false_val_base_type) ?
217  true_val_base_type : DOUBLE_T;
218  }
219  }
220  boost::phoenix::function<validate_conditional_op>
222 
224  const std::string& op,
225  const std::string& fun_name,
226  std::ostream& error_msgs) const {
227  if (!expr1.expression_type().is_primitive()
228  || !expr2.expression_type().is_primitive()) {
229  error_msgs << "binary infix operator " << op
230  << " with functional interpretation " << fun_name
231  << " requires arguments or primitive type (int or real)"
232  << ", found left type=" << expr1.expression_type()
233  << ", right arg type=" << expr2.expression_type()
234  << "; "
235  << std::endl;
236  }
237  std::vector<expression> args;
238  args.push_back(expr1);
239  args.push_back(expr2);
240  fun f(fun_name, args);
241  set_fun_type(f, error_msgs);
242  expr1 = expression(f);
243  }
244  boost::phoenix::function<binary_op_expr> binary_op_f;
245 
247  bool& pass,
248  std::ostream& error_msgs) const {
249  pass = !arg_type.is_void();
250  if (!pass)
251  error_msgs << "Functions cannot contain void argument types; "
252  << "found void argument."
253  << std::endl;
254  }
255  boost::phoenix::function<validate_non_void_arg_function>
257 
258  void set_void_function:: operator()(const expr_type& return_type,
259  var_origin& origin, bool& pass,
260  std::ostream& error_msgs) const {
261  if (return_type.is_void() && return_type.num_dims() > 0) {
262  error_msgs << "Void return type may not have dimensions declared."
263  << std::endl;
264  pass = false;
265  return;
266  }
267  origin = return_type.is_void()
270  pass = true;
271  }
272  boost::phoenix::function<set_void_function> set_void_function_f;
273 
274  void set_allows_sampling_origin::operator()(const std::string& identifier,
275  bool& allow_sampling,
276  int& origin) const {
277  bool is_void_function_origin
278  = (origin == void_function_argument_origin);
279  if (ends_with("_lp", identifier)) {
280  allow_sampling = true;
281  origin = is_void_function_origin
284  } else if (ends_with("_rng", identifier)) {
285  allow_sampling = false;
286  origin = is_void_function_origin
289  } else {
290  allow_sampling = false;
291  origin = is_void_function_origin
294  }
295  }
296  boost::phoenix::function<set_allows_sampling_origin>
298 
300  std::set<std::pair<std::string,
301  function_signature_t> >& declared,
302  std::set<std::pair<std::string,
303  function_signature_t> >& defined,
304  std::ostream& error_msgs) const {
305  using std::set;
306  using std::string;
307  using std::pair;
308  typedef set<pair<string, function_signature_t> >::iterator iterator_t;
309  for (iterator_t it = declared.begin(); it != declared.end(); ++it) {
310  if (defined.find(*it) == defined.end()) {
311  error_msgs <<"Function declared, but not defined."
312  << " Function name=" << (*it).first
313  << std::endl;
314  pass = false;
315  return;
316  }
317  }
318  pass = true;
319  }
320  boost::phoenix::function<validate_declarations> validate_declarations_f;
321 
322  bool fun_exists(const std::set<std::pair<std::string,
323  function_signature_t> >& existing,
324  const std::pair<std::string,
325  function_signature_t>& name_sig,
326  bool name_only = true) {
327  for (std::set<std::pair<std::string,
328  function_signature_t> >::const_iterator it
329  = existing.begin();
330  it != existing.end();
331  ++it)
332  if (name_sig.first == (*it).first
333  && (name_only
334  || name_sig.second.second == (*it).second.second))
335  return true; // name and arg sequences match
336  return false;
337  }
338 
339  void validate_prob_fun::operator()(std::string& fname, bool& pass,
340  std::ostream& error_msgs) const {
341  if (has_prob_fun_suffix(fname)) {
342  std::string dist_name = strip_prob_fun_suffix(fname);
343  if (!fun_name_exists(fname) // catch redefines later avoid fwd
344  && (fun_name_exists(dist_name + "_lpdf")
345  || fun_name_exists(dist_name + "_lpmf")
346  || fun_name_exists(dist_name + "_log"))) {
347  error_msgs << "Parse Error. Probability function already defined"
348  << " for " << dist_name << std::endl;
349  pass = false;
350  return;
351  }
352  }
353  if (has_cdf_suffix(fname)) {
354  std::string dist_name = strip_cdf_suffix(fname);
355  if (fun_name_exists(dist_name + "_cdf_log")
356  || fun_name_exists(dist_name + "_lcdf")) {
357  error_msgs << " Parse Error. CDF already defined for "
358  << dist_name << std::endl;
359  pass = false;
360  return;
361  }
362  }
363  if (has_ccdf_suffix(fname)) {
364  std::string dist_name = strip_ccdf_suffix(fname);
365  if (fun_name_exists(dist_name + "_ccdf_log")
366  || fun_name_exists(dist_name + "_lccdf")) {
367  error_msgs << " Parse Error. CCDF already defined for "
368  << dist_name << std::endl;
369  pass = false;
370  return;
371  }
372  }
373  }
374  boost::phoenix::function<validate_prob_fun> validate_prob_fun_f;
375 
377  bool& pass,
378  std::set<std::pair<std::string, function_signature_t> >&
379  functions_declared,
380  std::set<std::pair<std::string, function_signature_t> >&
381  functions_defined,
382  std::ostream& error_msgs) const {
383  // build up representations
384  expr_type result_type(decl.return_type_.base_type_,
385  decl.return_type_.num_dims_);
386  std::vector<expr_type> arg_types;
387  for (size_t i = 0; i < decl.arg_decls_.size(); ++i)
388  arg_types.push_back(expr_type(decl.arg_decls_[i].arg_type_.base_type_,
389  decl.arg_decls_[i].arg_type_.num_dims_));
390 
391  function_signature_t sig(result_type, arg_types);
392  std::pair<std::string, function_signature_t> name_sig(decl.name_, sig);
393  // check that not already declared if just declaration
394  if (decl.body_.is_no_op_statement()
395  && fun_exists(functions_declared, name_sig)) {
396  error_msgs << "Parse Error. Function already declared, name="
397  << decl.name_;
398  pass = false;
399  return;
400  }
401 
402  // check not already user defined
403  if (fun_exists(functions_defined, name_sig)) {
404  error_msgs << "Parse Error. Function already defined, name="
405  << decl.name_;
406  pass = false;
407  return;
408  }
409 
410  // check not already system defined
411  if (!fun_exists(functions_declared, name_sig)
413  error_msgs << "Parse Error. Function system defined, name="
414  << decl.name_;
415  pass = false;
416  return;
417  }
418 
419 
420  if (ends_with("_lpdf", decl.name_) && arg_types[0].base_type_ == INT_T) {
421  error_msgs << "Parse Error. Probability density functions require"
422  << " real variates (first argument)."
423  << " Found type = " << arg_types[0] << std::endl;
424  pass = false;
425  return;
426  }
427  if (ends_with("_lpmf", decl.name_) && arg_types[0].base_type_ != INT_T) {
428  error_msgs << "Parse Error. Probability mass functions require"
429  << " integer variates (first argument)."
430  << " Found type = " << arg_types[0] << std::endl;
431  pass = false;
432  return;
433  }
434 
435  // add declaration in local sets and in parser function sigs
436  if (functions_declared.find(name_sig) == functions_declared.end()) {
437  functions_declared.insert(name_sig);
439  .add(decl.name_, result_type, arg_types);
441  }
442 
443  // add as definition if there's a body
444  if (!decl.body_.is_no_op_statement())
445  functions_defined.insert(name_sig);
446  pass = true;
447  }
448  boost::phoenix::function<add_function_signature> add_function_signature_f;
449 
450 
452  bool& pass,
453  std::ostream& error_msgs)
454  const {
455  if (!has_prob_fun_suffix(decl.name_))
456  return;
457  if (decl.arg_decls_.size() == 0) {
458  error_msgs << "Parse Error. Probability functions require"
459  << " at least one argument." << std::endl;
460  pass = false;
461  return;
462  }
463  expr_type variate_type = decl.arg_decls_[0].arg_type_;
464  if (ends_with("_lpdf", decl.name_) && variate_type.base_type_ == INT_T) {
465  error_msgs << "Parse Error. Probability density functions require"
466  << " real variates (first argument)."
467  << " Found type = " << variate_type << std::endl;
468  pass = false;
469  return;
470  }
471  if (ends_with("_lpmf", decl.name_) && variate_type.base_type_ != INT_T) {
472  error_msgs << "Parse Error. Probability mass functions require"
473  << " integer variates (first argument)."
474  << " Found type = " << variate_type << std::endl;
475  pass = false;
476  return;
477  }
478  }
479  boost::phoenix::function<validate_pmf_pdf_variate>
481 
483  bool& pass,
484  std::ostream& error_msgs) const {
485  pass = decl.body_.is_no_op_statement()
487  error_msgs);
488  if (!pass) {
489  error_msgs << "Improper return in body of function." << std::endl;
490  return;
491  }
492 
493  if ((ends_with("_log", decl.name_)
494  || ends_with("_lpdf", decl.name_)
495  || ends_with("_lpmf", decl.name_)
496  || ends_with("_lcdf", decl.name_)
497  || ends_with("_lccdf", decl.name_))
498  && !decl.return_type_.is_primitive_double()) {
499  pass = false;
500  error_msgs << "Require real return type for probability functions"
501  << " ending in _log, _lpdf, _lpmf, _lcdf, or _lccdf."
502  << std::endl;
503  }
504  }
505  boost::phoenix::function<validate_return_type> validate_return_type_f;
506 
508  vm.add("lp__", DOUBLE_T, local_origin);
509  vm.add("params_r__", VECTOR_T, local_origin);
510  }
511  boost::phoenix::function<scope_lp> scope_lp_f;
512 
514  variable_map& vm) const {
515  vm.remove("lp__");
516  vm.remove("params_r__");
517  for (size_t i = 0; i < decl.arg_decls_.size(); ++i)
518  vm.remove(decl.arg_decls_[i].name_);
519  }
520  boost::phoenix::function<unscope_variables> unscope_variables_f;
521 
522  void add_fun_var::operator()(arg_decl& decl, bool& pass, variable_map& vm,
523  std::ostream& error_msgs) const {
524  if (vm.exists(decl.name_)) {
525  // variable already exists
526  pass = false;
527  error_msgs << "duplicate declaration of variable, name="
528  << decl.name_
529  << "; attempt to redeclare as function argument"
530  << "; original declaration as ";
531  print_var_origin(error_msgs, vm.get_origin(decl.name_));
532  error_msgs << std::endl;
533  return;
534  }
535  pass = true;
536  vm.add(decl.name_, decl.base_variable_declaration(),
538  }
539  boost::phoenix::function<add_fun_var> add_fun_var_f;
540 
541  // TODO(carpenter): seems redundant; see if it can be removed
543  val = omni_idx();
544  }
545  boost::phoenix::function<set_omni_idx> set_omni_idx_f;
546 
548  const {
549  pass = e.expression_type().is_primitive_int();
550  }
551  boost::phoenix::function<validate_int_expression>
553 
555  std::ostream& error_msgs) const {
556  if (e.expression_type().type() != INT_T) {
557  error_msgs << "index must be integer; found type=";
558  write_base_expr_type(error_msgs, e.expression_type().type());
559  error_msgs << std::endl;
560  pass = false;
561  return;
562  }
563  if (e.expression_type().num_dims_ > 1) {
564  // tests > 1 so that message is coherent because the single
565  // integer array tests don't print
566  error_msgs << "index must be integer or 1D integer array;"
567  << " found number of dimensions="
569  << std::endl;
570  pass = false;
571  return;
572  }
573  if (e.expression_type().num_dims_ == 0) {
574  // need integer array expression here, but nothing else to report
575  pass = false;
576  return;
577  }
578  pass = true;
579  }
580  boost::phoenix::function<validate_ints_expression>
582 
583 
585  vm.add("lp__",
586  base_var_decl("lp__", std::vector<expression>(), DOUBLE_T),
587  local_origin); // lp acts as a local where defined
588  vm.add("params_r__",
589  base_var_decl("params_r__", std::vector<expression>(), VECTOR_T),
590  local_origin); // lp acts as a local where defined
591  }
592  boost::phoenix::function<add_lp_var> add_lp_var_f;
593 
595  vm.remove("lp__");
596  vm.remove("params_r__");
597  }
598  boost::phoenix::function<remove_lp_var> remove_lp_var_f;
599 
601  pos_iterator_t _where, variable_map& vm,
602  std::stringstream& error_msgs) const {
603  using boost::spirit::get_line;
604  using boost::format;
605  using std::setw;
606 
607  size_t idx_errline = get_line(_where);
608 
609  error_msgs << std::endl;
610 
611  if (idx_errline > 0) {
612  error_msgs << "ERROR at line " << idx_errline
613  << std::endl << std::endl;
614 
615  std::basic_stringstream<char> sprogram;
616  sprogram << boost::make_iterator_range(_begin, _end);
617 
618  // show error in context 2 lines before, 1 lines after
619  size_t idx_errcol = 0;
620  idx_errcol = get_column(_begin, _where) - 1;
621 
622  std::string lineno = "";
623  format fmt_lineno("% 3d: ");
624 
625  std::string line_2before = "";
626  std::string line_before = "";
627  std::string line_err = "";
628  std::string line_after = "";
629 
630  size_t idx_line = 0;
631  size_t idx_before = idx_errline - 1;
632  if (idx_before > 0) {
633  // read lines up to error line, save 2 most recently read
634  while (idx_before > idx_line) {
635  line_2before = line_before;
636  std::getline(sprogram, line_before);
637  idx_line++;
638  }
639  if (line_2before.length() > 0) {
640  lineno = str(fmt_lineno % (idx_before - 1) );
641  error_msgs << lineno << line_2before << std::endl;
642  }
643  lineno = str(fmt_lineno % idx_before);
644  error_msgs << lineno << line_before << std::endl;
645  }
646 
647  std::getline(sprogram, line_err);
648  lineno = str(fmt_lineno % idx_errline);
649  error_msgs << lineno << line_err << std::endl
650  << setw(idx_errcol + lineno.length()) << "^" << std::endl;
651 
652  if (!sprogram.eof()) {
653  std::getline(sprogram, line_after);
654  lineno = str(fmt_lineno % (idx_errline+1));
655  error_msgs << lineno << line_after << std::endl;
656  }
657  }
658  error_msgs << std::endl;
659  }
660  boost::phoenix::function<program_error> program_error_f;
661 
663  const expression& e,
664  bool& pass,
665  std::stringstream& error_msgs)
666  const {
667  if (!e.expression_type().is_primitive()) {
668  error_msgs << "conditions in if-else statement must be"
669  << " primitive int or real;"
670  << " found type=" << e.expression_type()
671  << std::endl;
672  pass = false;
673  return;
674  }
675  cs.conditions_.push_back(e);
676  pass = true;
677  return;
678  }
679  boost::phoenix::function<add_conditional_condition>
681 
683  const statement& s) const {
684  cs.bodies_.push_back(s);
685  }
686  boost::phoenix::function<add_conditional_body> add_conditional_body_f;
687 
688  void deprecate_old_assignment_op::operator()(std::ostream& error_msgs)
689  const {
690  error_msgs << "Warning (non-fatal): assignment operator <- deprecated"
691  << " in the Stan language;"
692  << " use = instead."
693  << std::endl;
694  }
695  boost::phoenix::function<deprecate_old_assignment_op>
697 
699  std::ostream& error_msgs) const {
700  if (origin != function_argument_origin
701  && origin != function_argument_origin_lp
702  && origin != function_argument_origin_rng) {
703  error_msgs << "Returns only allowed from function bodies."
704  << std::endl;
705  pass = false;
706  return;
707  }
708  pass = true;
709  }
710  boost::phoenix::function<validate_return_allowed> validate_return_allowed_f;
711 
713  bool& pass,
714  std::ostream& error_msgs)
715  const {
716  if (origin != void_function_argument_origin
718  && origin != void_function_argument_origin_rng) {
719  error_msgs << "Void returns only allowed from function"
720  << " bodies of void return type."
721  << std::endl;
722  pass = false;
723  return;
724  }
725  pass = true;
726  }
727  boost::phoenix::function<validate_void_return_allowed>
729 
730  void identifier_to_var::operator()(const std::string& name,
731  const var_origin& origin_allowed,
732  variable& v, bool& pass,
733  const variable_map& vm,
734  std::ostream& error_msgs) const {
735  // validate existence
736  if (!vm.exists(name)) {
737  pass = false;
738  return;
739  }
740  // validate origin
741  var_origin lhs_origin = vm.get_origin(name);
742  if (lhs_origin != local_origin
743  && lhs_origin != origin_allowed) {
744  pass = false;
745  return;
746  }
747  // enforce constancy of function args
748  if (lhs_origin == function_argument_origin
749  || lhs_origin == function_argument_origin_lp
750  || lhs_origin == function_argument_origin_rng
751  || lhs_origin == void_function_argument_origin
752  || lhs_origin == void_function_argument_origin_lp
753  || lhs_origin == void_function_argument_origin_rng) {
754  pass = false;
755  return;
756  }
757  v = variable(name);
758  v.set_type(vm.get_base_type(name), vm.get_num_dims(name));
759  pass = true;
760  }
761  boost::phoenix::function<identifier_to_var> identifier_to_var_f;
762 
763  void validate_assgn::operator()(const assgn& a, bool& pass,
764  std::ostream& error_msgs) const {
765  // resolve type of lhs[idxs] and make sure it matches rhs
766  std::string name = a.lhs_var_.name_;
767  expression lhs_expr = expression(a.lhs_var_);
768  expr_type lhs_type = indexed_type(lhs_expr, a.idxs_);
769  if (lhs_type.is_ill_formed()) {
770  error_msgs << "Left-hand side indexing incompatible with variable."
771  << std::endl;
772  pass = false;
773  return;
774  }
775 
776  expr_type rhs_type = a.rhs_.expression_type();
777  base_expr_type lhs_base_type = lhs_type.base_type_;
778  base_expr_type rhs_base_type = rhs_type.base_type_;
779  // allow int -> double promotion, even in arrays
780  bool types_compatible
781  = lhs_base_type == rhs_base_type
782  || (lhs_base_type == DOUBLE_T && rhs_base_type == INT_T);
783  if (!types_compatible) {
784  error_msgs << "base type mismatch in assignment"
785  << "; variable name="
786  << name
787  << ", type=";
788  write_base_expr_type(error_msgs, lhs_base_type);
789  error_msgs << "; right-hand side type=";
790  write_base_expr_type(error_msgs, rhs_base_type);
791  error_msgs << std::endl;
792  pass = false;
793  return;
794  }
795 
796  if (lhs_type.num_dims_ != rhs_type.num_dims_) {
797  error_msgs << "dimension mismatch in assignment"
798  << "; variable name="
799  << name
800  << ", num dimensions given="
801  << lhs_type.num_dims_
802  << "; right-hand side dimensions="
803  << rhs_type.num_dims_
804  << std::endl;
805  pass = false;
806  return;
807  }
808 
809  if (a.lhs_var_occurs_on_rhs()) {
810  // this only requires a warning --- a deep copy will be made
811  error_msgs << "WARNING: left-hand side variable"
812  << " (name=" << name << ")"
813  << " occurs on right-hand side of assignment, causing"
814  << " inefficient deep copy to avoid aliasing."
815  << std::endl;
816  }
817 
818  pass = true;
819  }
820  boost::phoenix::function<validate_assgn> validate_assgn_f;
821 
823  const var_origin& origin_allowed,
824  bool& pass, variable_map& vm,
825  std::ostream& error_msgs) const {
826  // validate existence
827  std::string name = a.var_dims_.name_;
828  if (!vm.exists(name)) {
829  error_msgs << "unknown variable in assignment"
830  << "; lhs variable=" << a.var_dims_.name_
831  << std::endl;
832 
833  pass = false;
834  return;
835  }
836 
837  // validate origin
838  var_origin lhs_origin = vm.get_origin(name);
839  if (lhs_origin != local_origin
840  && lhs_origin != origin_allowed) {
841  error_msgs << "attempt to assign variable in wrong block."
842  << " left-hand-side variable origin=";
843  print_var_origin(error_msgs, lhs_origin);
844  error_msgs << std::endl;
845  pass = false;
846  return;
847  }
848 
849  // enforce constancy of function args
850  if (lhs_origin == function_argument_origin
851  || lhs_origin == function_argument_origin_lp
852  || lhs_origin == function_argument_origin_rng
853  || lhs_origin == void_function_argument_origin
854  || lhs_origin == void_function_argument_origin_lp
855  || lhs_origin == void_function_argument_origin_rng) {
856  error_msgs << "Illegal to assign to function argument variables."
857  << std::endl
858  << "Use local variables instead."
859  << std::endl;
860  pass = false;
861  return;
862  }
863 
864  // validate types
865  a.var_type_ = vm.get(name);
866  size_t lhs_var_num_dims = a.var_type_.dims_.size();
867  size_t num_index_dims = a.var_dims_.dims_.size();
868 
870  lhs_var_num_dims,
871  num_index_dims);
872 
873  if (lhs_type.is_ill_formed()) {
874  error_msgs << "too many indexes for variable "
875  << "; variable name = " << name
876  << "; num dimensions given = " << num_index_dims
877  << "; variable array dimensions = " << lhs_var_num_dims
878  << std::endl;
879  pass = false;
880  return;
881  }
882 
883  base_expr_type lhs_base_type = lhs_type.base_type_;
884  base_expr_type rhs_base_type = a.expr_.expression_type().base_type_;
885  // allow int -> double promotion
886  bool types_compatible
887  = lhs_base_type == rhs_base_type
888  || (lhs_base_type == DOUBLE_T && rhs_base_type == INT_T);
889  if (!types_compatible) {
890  error_msgs << "base type mismatch in assignment"
891  << "; variable name = "
892  << a.var_dims_.name_
893  << ", type = ";
894  write_base_expr_type(error_msgs, lhs_base_type);
895  error_msgs << "; right-hand side type=";
896  write_base_expr_type(error_msgs, rhs_base_type);
897  error_msgs << std::endl;
898  pass = false;
899  return;
900  }
901 
902  if (lhs_type.num_dims_ != a.expr_.expression_type().num_dims_) {
903  error_msgs << "dimension mismatch in assignment"
904  << "; variable name = "
905  << a.var_dims_.name_
906  << ", num dimensions given = "
907  << lhs_type.num_dims_
908  << "; right-hand side dimensions = "
910  << std::endl;
911  pass = false;
912  return;
913  }
914 
915  pass = true;
916  }
917  boost::phoenix::function<validate_assignment> validate_assignment_f;
918 
919  bool is_defined(const std::string& function_name,
920  const std::vector<expr_type>& arg_types) {
921  expr_type ret_type(DOUBLE_T, 0);
922  function_signature_t sig(ret_type, arg_types);
923  return function_signatures::instance().is_defined(function_name, sig);
924  }
925 
926  bool is_double_return(const std::string& function_name,
927  const std::vector<expr_type>& arg_types,
928  std::ostream& error_msgs) {
930  .get_result_type(function_name, arg_types, error_msgs, true)
932  }
933 
934  bool is_univariate(const expr_type& et) {
935  return et.num_dims_ == 0
936  && (et.base_type_ == INT_T
937  || et.base_type_ == DOUBLE_T);
938  }
939 
941  const variable_map& var_map, bool& pass,
942  std::ostream& error_msgs) const {
943  static const bool user_facing = true;
944  std::vector<expr_type> arg_types;
945  arg_types.push_back(s.expr_.expression_type());
946  for (size_t i = 0; i < s.dist_.args_.size(); ++i)
947  arg_types.push_back(s.dist_.args_[i].expression_type());
948  std::string function_name(s.dist_.family_);
949 
950  std::string internal_function_name = get_prob_fun(function_name);
951  if (internal_function_name.size() == 0) {
952  pass = false;
953  error_msgs << "Error: couldn't find distribution named "
954  << function_name << std::endl;
955  return;
956  }
957 
958  if ((internal_function_name.find("multiply_log") != std::string::npos)
959  || (internal_function_name.find("binomial_coefficient_log")
960  != std::string::npos)) {
961  error_msgs << "Only distribution names can be used with"
962  << " sampling (~) notation; found non-distribution"
963  << " function: " << function_name
964  << std::endl;
965  pass = false;
966  return;
967  }
968 
969  if (internal_function_name.find("cdf_log") != std::string::npos) {
970  error_msgs << "CDF and CCDF functions may not be used with"
971  << " sampling notation."
972  << " Use increment_log_prob("
973  << internal_function_name << "(...)) instead."
974  << std::endl;
975  pass = false;
976  return;
977  }
978 
979  if (internal_function_name == "lkj_cov_log") {
980  error_msgs << "Warning: the lkj_cov_log() sampling distribution"
981  << " is deprecated. It will be removed in Stan 3."
982  << std::endl
983  << "Code LKJ covariance in terms of an lkj_corr()"
984  << " distribution on a correlation matrix"
985  << " and independent lognormals on the scales."
986  << std::endl << std::endl;
987  }
988 
989  if (!is_double_return(internal_function_name, arg_types, error_msgs)) {
990  error_msgs << "require real scalar return type for"
991  << " probability function." << std::endl;
992  pass = false;
993  return;
994  }
995 
996  // test for LHS not being purely a variable
997  if (has_non_param_var(s.expr_, var_map)) {
998  error_msgs << "Warning (non-fatal):"
999  << std::endl
1000  << "Left-hand side of sampling statement (~) may contain a"
1001  << " non-linear transform of a parameter or local variable."
1002  << std::endl
1003  << "If so, you need to call increment_log_prob() with"
1004  << " the log absolute determinant of the Jacobian of"
1005  << " the transform."
1006  << std::endl
1007  << "Left-hand-side of sampling statement:"
1008  << std::endl
1009  << " ";
1010  generate_expression(s.expr_, user_facing, error_msgs);
1011  error_msgs << " ~ " << function_name << "(...)"
1012  << std::endl;
1013  }
1014  // validate that variable and params are univariate if truncated
1015  if (s.truncation_.has_low() || s.truncation_.has_high()) {
1016  if (!is_univariate(s.expr_.expression_type())) {
1017  error_msgs << "Outcomes in truncated distributions"
1018  << " must be univariate."
1019  << std::endl
1020  << " Found outcome expression: ";
1021  generate_expression(s.expr_, user_facing, error_msgs);
1022  error_msgs << std::endl
1023  << " with non-univariate type: "
1024  << s.expr_.expression_type()
1025  << std::endl;
1026  pass = false;
1027  return;
1028  }
1029  for (size_t i = 0; i < s.dist_.args_.size(); ++i)
1030  if (!is_univariate(s.dist_.args_[i].expression_type())) {
1031  error_msgs << "Parameters in truncated distributions"
1032  << " must be univariate."
1033  << std::endl
1034  << " Found parameter expression: ";
1035  generate_expression(s.dist_.args_[i], user_facing, error_msgs);
1036  error_msgs << std::endl
1037  << " with non-univariate type: "
1038  << s.dist_.args_[i].expression_type()
1039  << std::endl;
1040  pass = false;
1041  return;
1042  }
1043  }
1044  if (s.truncation_.has_low()
1046  error_msgs << "Lower bounds in truncated distributions"
1047  << " must be univariate."
1048  << std::endl
1049  << " Found lower bound expression: ";
1050  generate_expression(s.truncation_.low_, user_facing, error_msgs);
1051  error_msgs << std::endl
1052  << " with non-univariate type: "
1054  << std::endl;
1055  pass = false;
1056  return;
1057  }
1058  if (s.truncation_.has_high()
1060  error_msgs << "Upper bounds in truncated distributions"
1061  << " must be univariate."
1062  << std::endl
1063  << " Found upper bound expression: ";
1064  generate_expression(s.truncation_.high_, user_facing, error_msgs);
1065  error_msgs << std::endl
1066  << " with non-univariate type: "
1068  << std::endl;
1069  pass = false;
1070  return;
1071  }
1072 
1073  // make sure CDFs or CCDFs exist with conforming signature
1074  // T[L, ]
1075  if (s.truncation_.has_low() && !s.truncation_.has_high()) {
1076  std::vector<expr_type> arg_types_trunc(arg_types);
1077  arg_types_trunc[0] = s.truncation_.low_.expression_type();
1078  std::string function_name_ccdf = get_ccdf(s.dist_.family_);
1079  if (!is_double_return(function_name_ccdf, arg_types_trunc,
1080  error_msgs)) {
1081  error_msgs << "lower truncation not defined for specified"
1082  << " arguments to "
1083  << s.dist_.family_ << std::endl;
1084  pass = false;
1085  return;
1086  }
1087  if (!is_double_return(function_name_ccdf, arg_types, error_msgs)) {
1088  error_msgs << "lower bound in truncation type does not match"
1089  << " sampled variate in distribution's type"
1090  << std::endl;
1091  pass = false;
1092  return;
1093  }
1094  }
1095  // T[, H]
1096  if (!s.truncation_.has_low() && s.truncation_.has_high()) {
1097  std::vector<expr_type> arg_types_trunc(arg_types);
1098  arg_types_trunc[0] = s.truncation_.high_.expression_type();
1099  std::string function_name_cdf = get_cdf(s.dist_.family_);
1100  if (!is_double_return(function_name_cdf, arg_types_trunc,
1101  error_msgs)) {
1102  error_msgs << "upper truncation not defined for"
1103  << " specified arguments to "
1104  << s.dist_.family_ << std::endl;
1105 
1106  pass = false;
1107  return;
1108  }
1109  if (!is_double_return(function_name_cdf, arg_types, error_msgs)) {
1110  error_msgs << "upper bound in truncation type does not match"
1111  << " sampled variate in distribution's type"
1112  << std::endl;
1113  pass = false;
1114  return;
1115  }
1116  }
1117  // T[L, H]
1118  if (s.truncation_.has_low() && s.truncation_.has_high()) {
1119  std::vector<expr_type> arg_types_trunc(arg_types);
1120  arg_types_trunc[0] = s.truncation_.low_.expression_type();
1121  std::string function_name_cdf = get_cdf(s.dist_.family_);
1122  if (!is_double_return(function_name_cdf, arg_types_trunc,
1123  error_msgs)) {
1124  error_msgs << "lower truncation not defined for specified"
1125  << " arguments to "
1126  << s.dist_.family_ << std::endl;
1127  pass = false;
1128  return;
1129  }
1130  if (!is_double_return(function_name_cdf, arg_types, error_msgs)) {
1131  error_msgs << "lower bound in truncation type does not match"
1132  << " sampled variate in distribution's type"
1133  << std::endl;
1134  pass = false;
1135  return;
1136  }
1137  }
1138 
1139  pass = true;
1140  }
1141  boost::phoenix::function<validate_sample> validate_sample_f;
1142 
1144  const stan::lang::expression& expr,
1145  std::stringstream& error_msgs) const {
1146  static const bool user_facing = true;
1147  if (expr.expression_type() != VOID_T) {
1148  error_msgs << "Illegal statement beginning with non-void"
1149  << " expression parsed as"
1150  << std::endl << " ";
1151  generate_expression(expr.expr_, user_facing, error_msgs);
1152  error_msgs << std::endl
1153  << "Not a legal assignment, sampling, or function"
1154  << " statement. Note that"
1155  << std::endl
1156  << " * Assignment statements only allow variables"
1157  << " (with optional indexes) on the left;"
1158  << std::endl
1159  << " if you see an outer function logical_lt (<)"
1160  << " with negated (-) second argument,"
1161  << std::endl
1162  << " it indicates an assignment statement A <- B"
1163  << " with illegal left"
1164  << std::endl
1165  << " side A parsed as expression (A < (-B))."
1166  << std::endl
1167  << " * Sampling statements allow arbitrary"
1168  << " value-denoting expressions on the left."
1169  << std::endl
1170  << " * Functions used as statements must be"
1171  << " declared to have void returns"
1172  << std::endl << std::endl;
1173  pass = false;
1174  return;
1175  }
1176  pass = true;
1177  }
1178  boost::phoenix::function<expression_as_statement> expression_as_statement_f;
1179 
1180  void unscope_locals::operator()(const std::vector<var_decl>& var_decls,
1181  variable_map& vm) const {
1182  for (size_t i = 0; i < var_decls.size(); ++i)
1183  vm.remove(var_decls[i].name());
1184  }
1185  boost::phoenix::function<unscope_locals> unscope_locals_f;
1186 
1188  const expression& e, bool& pass,
1189  std::stringstream& error_msgs) const {
1190  pass = e.expression_type().is_primitive();
1191  if (!pass) {
1192  error_msgs << "conditions in while statement must be primitive"
1193  << " int or real;"
1194  << " found type=" << e.expression_type() << std::endl;
1195  return;
1196  }
1197  ws.condition_ = e;
1198  }
1199  boost::phoenix::function<add_while_condition> add_while_condition_f;
1200 
1202  const {
1203  ws.body_ = s;
1204  }
1205  boost::phoenix::function<add_while_body> add_while_body_f;
1206 
1207  void add_loop_identifier::operator()(const std::string& name,
1208  std::string& name_local,
1209  bool& pass, variable_map& vm,
1210  std::stringstream& error_msgs) const {
1211  name_local = name;
1212  pass = !vm.exists(name);
1213  if (!pass)
1214  error_msgs << "ERROR: loop variable already declared."
1215  << " variable name=\"" << name << "\"" << std::endl;
1216  else
1217  vm.add(name, base_var_decl(name, std::vector<expression>(), INT_T),
1218  local_origin); // loop var acts like local
1219  }
1220  boost::phoenix::function<add_loop_identifier> add_loop_identifier_f;
1221 
1222  void remove_loop_identifier::operator()(const std::string& name,
1223  variable_map& vm) const {
1224  vm.remove(name);
1225  }
1226  boost::phoenix::function<remove_loop_identifier> remove_loop_identifier_f;
1227 
1229  bool& pass,
1230  std::stringstream& error_msgs)
1231  const {
1232  pass = expr.expression_type().is_primitive_int();
1233  if (!pass)
1234  error_msgs << "expression denoting integer required; found type="
1235  << expr.expression_type() << std::endl;
1236  }
1237  boost::phoenix::function<validate_int_expr_warn> validate_int_expr_warn_f;
1238 
1240  std::stringstream& error_msgs) const {
1241  error_msgs << "Warning (non-fatal): increment_log_prob(...);"
1242  << " is deprecated and will be removed in the future."
1243  << std::endl
1244  << " Use target += ...; instead."
1245  << std::endl;
1246  }
1247  boost::phoenix::function<deprecate_increment_log_prob>
1249 
1250  void validate_allow_sample::operator()(const bool& allow_sample,
1251  bool& pass,
1252  std::stringstream& error_msgs)
1253  const {
1254  pass = allow_sample;
1255  if (!pass)
1256  error_msgs << "Sampling statements (~) and increment_log_prob() are"
1257  << std::endl
1258  << "only allowed in the model block."
1259  << std::endl;
1260  }
1261  boost::phoenix::function<validate_allow_sample> validate_allow_sample_f;
1262 
1264  bool& pass,
1265  std::ostream& error_msgs)
1266  const {
1267  pass = !e.expression_type().is_void();
1268  if (!pass)
1269  error_msgs << "attempt to increment log prob with void expression"
1270  << std::endl;
1271  }
1272  boost::phoenix::function<validate_non_void_expression>
1274 
1275 
1277  const pos_iterator_t& begin,
1278  const pos_iterator_t& end) const {
1279  stmt.begin_line_ = get_line(begin);
1280  stmt.end_line_ = get_line(end);
1281  }
1282  boost::phoenix::function<add_line_number> add_line_number_f;
1283 
1285  s = return_statement();
1286  }
1287  boost::phoenix::function<set_void_return> set_void_return_f;
1288 
1290  s = no_op_statement();
1291  }
1292  boost::phoenix::function<set_no_op> set_no_op_f;
1293 
1294 
1295  void deprecated_integrate_ode::operator()(std::ostream& error_msgs)
1296  const {
1297  error_msgs << "Warning: the integrate_ode() function is deprecated"
1298  << " in the Stan language; use integrate_ode_rk45() [non-stiff]"
1299  << " or integrate_ode_bdf() [stiff] instead."
1300  << std::endl;
1301  }
1302  boost::phoenix::function<deprecated_integrate_ode>
1304 
1305  template <class T>
1307  const variable_map& var_map,
1308  bool& pass,
1309  std::ostream& error_msgs) {
1310  pass = true;
1311  // test function argument type
1312  expr_type sys_result_type(DOUBLE_T, 1);
1313  std::vector<expr_type> sys_arg_types;
1314  sys_arg_types.push_back(expr_type(DOUBLE_T, 0));
1315  sys_arg_types.push_back(expr_type(DOUBLE_T, 1));
1316  sys_arg_types.push_back(expr_type(DOUBLE_T, 1));
1317  sys_arg_types.push_back(expr_type(DOUBLE_T, 1));
1318  sys_arg_types.push_back(expr_type(INT_T, 1));
1319  function_signature_t system_signature(sys_result_type, sys_arg_types);
1321  .is_defined(ode_fun.system_function_name_, system_signature)) {
1322  error_msgs << "first argument to "
1323  << ode_fun.integration_function_name_
1324  << " must be a function with signature"
1325  << " (real, real[], real[], real[], int[]) : real[] ";
1326  pass = false;
1327  }
1328 
1329  // test regular argument types
1330  if (ode_fun.y0_.expression_type() != expr_type(DOUBLE_T, 1)) {
1331  error_msgs << "second argument to "
1332  << ode_fun.integration_function_name_
1333  << " must have type real[] for intial system state;"
1334  << " found type="
1335  << ode_fun.y0_.expression_type()
1336  << ". ";
1337  pass = false;
1338  }
1339  if (!ode_fun.t0_.expression_type().is_primitive()) {
1340  error_msgs << "third argument to "
1341  << ode_fun.integration_function_name_
1342  << " must have type real or int for initial time; found type="
1343  << ode_fun.t0_.expression_type()
1344  << ". ";
1345  pass = false;
1346  }
1347  if (ode_fun.ts_.expression_type() != expr_type(DOUBLE_T, 1)) {
1348  error_msgs << "fourth argument to "
1349  << ode_fun.integration_function_name_
1350  << " must have type real[]"
1351  << " for requested solution times; found type="
1352  << ode_fun.ts_.expression_type()
1353  << ". ";
1354  pass = false;
1355  }
1356  if (ode_fun.theta_.expression_type() != expr_type(DOUBLE_T, 1)) {
1357  error_msgs << "fifth argument to "
1358  << ode_fun.integration_function_name_
1359  << " must have type real[] for parameters; found type="
1360  << ode_fun.theta_.expression_type()
1361  << ". ";
1362  pass = false;
1363  }
1364  if (ode_fun.x_.expression_type() != expr_type(DOUBLE_T, 1)) {
1365  error_msgs << "sixth argument to "
1366  << ode_fun.integration_function_name_
1367  << " must have type real[] for real data; found type="
1368  << ode_fun.x_.expression_type()
1369  << ". ";
1370  pass = false;
1371  }
1372  if (ode_fun.x_int_.expression_type() != expr_type(INT_T, 1)) {
1373  error_msgs << "seventh argument to "
1374  << ode_fun.integration_function_name_
1375  << " must have type int[] for integer data; found type="
1376  << ode_fun.x_int_.expression_type()
1377  << ". ";
1378  pass = false;
1379  }
1380 
1381  // test data-only variables do not have parameters (int locals OK)
1382  if (has_var(ode_fun.t0_, var_map)) {
1383  error_msgs << "third argument to "
1384  << ode_fun.integration_function_name_
1385  << " (initial times)"
1386  << " must be data only and not reference parameters";
1387  pass = false;
1388  }
1389  if (has_var(ode_fun.ts_, var_map)) {
1390  error_msgs << "fourth argument to "
1391  << ode_fun.integration_function_name_
1392  << " (solution times)"
1393  << " must be data only and not reference parameters";
1394  pass = false;
1395  }
1396  if (has_var(ode_fun.x_, var_map)) {
1397  error_msgs << "fifth argument to "
1398  << ode_fun.integration_function_name_
1399  << " (real data)"
1400  << " must be data only and not reference parameters";
1401  pass = false;
1402  }
1403  }
1404 
1406  const variable_map& var_map,
1407  bool& pass,
1408  std::ostream& error_msgs) const {
1409  validate_integrate_ode_non_control_args(ode_fun, var_map, pass,
1410  error_msgs);
1411  }
1412  boost::phoenix::function<validate_integrate_ode> validate_integrate_ode_f;
1413 
1415  const integrate_ode_control& ode_fun,
1416  const variable_map& var_map, bool& pass,
1417  std::ostream& error_msgs) const {
1418  validate_integrate_ode_non_control_args(ode_fun, var_map, pass,
1419  error_msgs);
1420  if (!ode_fun.rel_tol_.expression_type().is_primitive()) {
1421  error_msgs << "eight argument to "
1422  << ode_fun.integration_function_name_
1423  << " (relative tolerance) must have type real or int;"
1424  << " found type="
1425  << ode_fun.rel_tol_.expression_type()
1426  << ". ";
1427  pass = false;
1428  }
1429  if (!ode_fun.abs_tol_.expression_type().is_primitive()) {
1430  error_msgs << "ninth argument to "
1431  << ode_fun.integration_function_name_
1432  << " (absolute tolerance) must have type real or int;"
1433  << " found type="
1434  << ode_fun.abs_tol_.expression_type()
1435  << ". ";
1436  pass = false;
1437  }
1438  if (!ode_fun.max_num_steps_.expression_type().is_primitive()) {
1439  error_msgs << "tenth argument to "
1440  << ode_fun.integration_function_name_
1441  << " (max steps) must have type real or int;"
1442  << " found type="
1443  << ode_fun.max_num_steps_.expression_type()
1444  << ". ";
1445  pass = false;
1446  }
1447 
1448  // test data-only variables do not have parameters (int locals OK)
1449  if (has_var(ode_fun.rel_tol_, var_map)) {
1450  error_msgs << "eight argument to "
1451  << ode_fun.integration_function_name_
1452  << " (real data) must be data only"
1453  << " and not depend on parameters";
1454  pass = false;
1455  }
1456  if (has_var(ode_fun.abs_tol_, var_map)) {
1457  error_msgs << "ninth argument to "
1458  << ode_fun.integration_function_name_
1459  << " (real data) must be data only"
1460  << " and not depend parameters";
1461  pass = false;
1462  }
1463  if (has_var(ode_fun.max_num_steps_, var_map)) {
1464  error_msgs << "tenth argument to "
1465  << ode_fun.integration_function_name_
1466  << " (real data) must be data only"
1467  << " and not depend on parameters";
1468  pass = false;
1469  }
1470  }
1471  boost::phoenix::function<validate_integrate_ode_control>
1473 
1475  const var_origin& var_origin,
1476  bool& pass,
1477  std::ostream& error_msgs) const {
1478  if (fun.name_ == "get_lp")
1479  error_msgs << "Warning (non-fatal): get_lp() function deprecated."
1480  << std::endl
1481  << " It will be removed in a future release."
1482  << std::endl
1483  << " Use target() instead."
1484  << std::endl;
1485  if (fun.name_ == "target")
1486  fun.name_ = "get_lp"; // for code gen and context validation
1487 
1488  std::vector<expr_type> arg_types;
1489  for (size_t i = 0; i < fun.args_.size(); ++i)
1490  arg_types.push_back(fun.args_[i].expression_type());
1491 
1493  .get_result_type(fun.name_, arg_types, error_msgs);
1494  if (fun.type_ == ILL_FORMED_T) {
1495  pass = false;
1496  return;
1497  }
1498 
1499  // disjunction so only first match triggered
1500  deprecate_fun("binomial_coefficient_log", "lchoose", fun, error_msgs)
1501  || deprecate_fun("multiply_log", "lmultiply", fun, error_msgs)
1502  || deprecate_suffix("_cdf_log", "'_lcdf'", fun, error_msgs)
1503  || deprecate_suffix("_ccdf_log", "'_lccdf'", fun, error_msgs)
1504  || deprecate_suffix("_log",
1505  "'_lpdf' for density functions or '_lpmf' for mass functions",
1506  fun, error_msgs);
1507 
1508  // need old function names (_log) until math gets updated to new ones
1509  replace_suffix("_lpdf", "_log", fun);
1510  replace_suffix("_lpmf", "_log", fun);
1511  replace_suffix("_lcdf", "_cdf_log", fun);
1512  replace_suffix("_lccdf", "_ccdf_log", fun);
1513  replace_suffix("lmultiply", "multiply_log", fun);
1514  replace_suffix("lchoose", "binomial_coefficient_log", fun);
1515 
1516  if (has_rng_suffix(fun.name_)) {
1517  if (!(var_origin == derived_origin
1518  || var_origin == function_argument_origin_rng)) {
1519  error_msgs << "random number generators only allowed in"
1520  << " generated quantities block or"
1521  << " user-defined functions with names ending in _rng"
1522  << "; found function=" << fun.name_
1523  << " in block=";
1524  print_var_origin(error_msgs, var_origin);
1525  error_msgs << std::endl;
1526  pass = false;
1527  return;
1528  }
1529  }
1530 
1531  if (has_lp_suffix(fun.name_) || fun.name_ == "target") {
1532  // modified function_argument_origin to add _lp because
1533  // that's only viable context
1534  if (!(var_origin == transformed_parameter_origin
1535  || var_origin == function_argument_origin_lp
1536  || var_origin == void_function_argument_origin_lp
1537  || var_origin == local_origin)) {
1538  error_msgs << "Function target() or functions suffixed with _lp only"
1539  << " allowed in transformed parameter block, model block"
1540  << std::endl
1541  << "or the body of a function with suffix _lp."
1542  << std::endl
1543  << "Found function = "
1544  << (fun.name_ == "get_lp" ? "target or get_lp" : fun.name_)
1545  << " in block = ";
1546  print_var_origin(error_msgs, var_origin);
1547  error_msgs << std::endl;
1548  pass = false;
1549  return;
1550  }
1551  }
1552 
1553  if (fun.name_ == "max" || fun.name_ == "min") {
1554  if (fun.args_.size() == 2) {
1555  if (fun.args_[0].expression_type().is_primitive_int()
1556  && fun.args_[1].expression_type().is_primitive_int()) {
1557  fun.name_ = "std::" + fun.name_;
1558  }
1559  }
1560  }
1561 
1562  if (fun.name_ == "abs"
1563  && fun.args_.size() > 0
1564  && fun.args_[0].expression_type().is_primitive_double()) {
1565  error_msgs << "Warning: Function abs(real) is deprecated"
1566  << " in the Stan language."
1567  << std::endl
1568  << " It will be removed in a future release."
1569  << std::endl
1570  << " Use fabs(real) instead."
1571  << std::endl << std::endl;
1572  }
1573 
1574  if (fun.name_ == "lkj_cov_log") {
1575  error_msgs << "Warning: the lkj_cov_log() function"
1576  << " is deprecated. It will be removed in Stan 3."
1577  << std::endl
1578  << "Code LKJ covariance in terms of an lkj_corr()"
1579  << " distribution on a correlation matrix"
1580  << " and independent lognormals on the scales."
1581  << std::endl << std::endl;
1582  }
1583 
1584  fun_result = fun;
1585  pass = true;
1586  }
1587  boost::phoenix::function<set_fun_type_named> set_fun_type_named_f;
1588 
1590  const expression& expr2,
1591  const var_origin& var_origin,
1592  bool& pass,
1593  std::ostream& error_msgs) const {
1594  if (!expr1.expression_type().is_primitive()
1595  || !expr2.expression_type().is_primitive()) {
1596  error_msgs << "arguments to ^ must be primitive (real or int)"
1597  << "; cannot exponentiate "
1598  << expr1.expression_type()
1599  << " by "
1600  << expr2.expression_type()
1601  << " in block=";
1602  print_var_origin(error_msgs, var_origin);
1603  error_msgs << std::endl;
1604  pass = false;
1605  return;
1606  }
1607  std::vector<expression> args;
1608  args.push_back(expr1);
1609  args.push_back(expr2);
1610  fun f("pow", args);
1611  set_fun_type(f, error_msgs);
1612  expr1 = expression(f);
1613  }
1614  boost::phoenix::function<exponentiation_expr> exponentiation_f;
1615 
1617  const expression& expr2,
1618  std::ostream& error_msgs) const {
1619  if (expr1.expression_type().is_primitive()
1620  && expr2.expression_type().is_primitive()) {
1621  expr1 *= expr2;;
1622  return;
1623  }
1624  std::vector<expression> args;
1625  args.push_back(expr1);
1626  args.push_back(expr2);
1627  fun f("multiply", args);
1628  set_fun_type(f, error_msgs);
1629  expr1 = expression(f);
1630  }
1631  boost::phoenix::function<multiplication_expr> multiplication_f;
1632 
1634  const expression& expr2,
1635  std::ostream& error_msgs) const {
1636  static const bool user_facing = true;
1637  if (expr1.expression_type().is_primitive()
1638  && expr2.expression_type().is_primitive()
1639  && (expr1.expression_type().is_primitive_double()
1640  || expr2.expression_type().is_primitive_double())) {
1641  expr1 /= expr2;
1642  return;
1643  }
1644  std::vector<expression> args;
1645  args.push_back(expr1);
1646  args.push_back(expr2);
1647  if (expr1.expression_type().is_primitive_int()
1648  && expr2.expression_type().is_primitive_int()) {
1649  // result might be assigned to real - generate warning
1650  error_msgs << "Warning: integer division"
1651  << " implicitly rounds to integer."
1652  << " Found int division: ";
1653  generate_expression(expr1.expr_, user_facing, error_msgs);
1654  error_msgs << " / ";
1655  generate_expression(expr2.expr_, user_facing, error_msgs);
1656  error_msgs << std::endl
1657  << " Positive values rounded down,"
1658  << " negative values rounded up or down"
1659  << " in platform-dependent way."
1660  << std::endl;
1661 
1662  fun f("divide", args);
1663  set_fun_type(f, error_msgs);
1664  expr1 = expression(f);
1665  return;
1666  }
1667  if ((expr1.expression_type().type() == MATRIX_T
1668  || expr1.expression_type().type() == ROW_VECTOR_T)
1669  && expr2.expression_type().type() == MATRIX_T) {
1670  fun f("mdivide_right", args);
1671  set_fun_type(f, error_msgs);
1672  expr1 = expression(f);
1673  return;
1674  }
1675  fun f("divide", args);
1676  set_fun_type(f, error_msgs);
1677  expr1 = expression(f);
1678  return;
1679  }
1680  boost::phoenix::function<division_expr> division_f;
1681 
1683  bool& pass, std::ostream& error_msgs) const {
1684  if (!expr1.expression_type().is_primitive_int()
1685  && !expr2.expression_type().is_primitive_int()) {
1686  error_msgs << "both operands of % must be int"
1687  << "; cannot modulo "
1688  << expr1.expression_type()
1689  << " by "
1690  << expr2.expression_type();
1691  error_msgs << std::endl;
1692  pass = false;
1693  return;
1694  }
1695  std::vector<expression> args;
1696  args.push_back(expr1);
1697  args.push_back(expr2);
1698  fun f("modulus", args);
1699  set_fun_type(f, error_msgs);
1700  expr1 = expression(f);
1701  }
1702  boost::phoenix::function<modulus_expr> modulus_f;
1703 
1705  const expression& expr2,
1706  std::ostream& error_msgs) const {
1707  std::vector<expression> args;
1708  args.push_back(expr1);
1709  args.push_back(expr2);
1710  if (expr1.expression_type().type() == MATRIX_T
1711  && (expr2.expression_type().type() == VECTOR_T
1712  || expr2.expression_type().type() == MATRIX_T)) {
1713  fun f("mdivide_left", args);
1714  set_fun_type(f, error_msgs);
1715  expr1 = expression(f);
1716  pass = true;
1717  return;
1718  }
1719  fun f("mdivide_left", args); // set for alt args err msg
1720  set_fun_type(f, error_msgs);
1721  expr1 = expression(f);
1722  pass = false;
1723  }
1724  boost::phoenix::function<left_division_expr> left_division_f;
1725 
1727  const expression& expr2,
1728  std::ostream& error_msgs) const {
1729  if (expr1.expression_type().is_primitive()
1730  && expr2.expression_type().is_primitive()) {
1731  expr1 *= expr2;
1732  return;
1733  }
1734  std::vector<expression> args;
1735  args.push_back(expr1);
1736  args.push_back(expr2);
1737  fun f("elt_multiply", args);
1738  set_fun_type(f, error_msgs);
1739  expr1 = expression(f);
1740  }
1741  boost::phoenix::function<elt_multiplication_expr> elt_multiplication_f;
1742 
1744  const expression& expr2,
1745  std::ostream& error_msgs) const {
1746  if (expr1.expression_type().is_primitive()
1747  && expr2.expression_type().is_primitive()) {
1748  expr1 /= expr2;
1749  return;
1750  }
1751  std::vector<expression> args;
1752  args.push_back(expr1);
1753  args.push_back(expr2);
1754  fun f("elt_divide", args);
1755  set_fun_type(f, error_msgs);
1756  expr1 = expression(f);
1757  }
1758  boost::phoenix::function<elt_division_expr> elt_division_f;
1759 
1761  const expression& expr, bool& pass,
1762  std::ostream& error_msgs) const {
1763  if (expr.expression_type().is_primitive()) {
1764  expr_result = expression(unary_op('-', expr));
1765  return;
1766  }
1767  std::vector<expression> args;
1768  args.push_back(expr);
1769  fun f("minus", args);
1770  set_fun_type(f, error_msgs);
1771  expr_result = expression(f);
1772  }
1773  boost::phoenix::function<negate_expr> negate_expr_f;
1774 
1776  const expression& expr,
1777  std::ostream& error_msgs) const {
1778  if (!expr.expression_type().is_primitive()) {
1779  error_msgs << "logical negation operator !"
1780  << " only applies to int or real types; ";
1781  expr_result = expression();
1782  }
1783  std::vector<expression> args;
1784  args.push_back(expr);
1785  fun f("logical_negation", args);
1786  set_fun_type(f, error_msgs);
1787  expr_result = expression(f);
1788  }
1789  boost::phoenix::function<logical_negate_expr> logical_negate_expr_f;
1790 
1791  void transpose_expr::operator()(expression& expr, bool& pass,
1792  std::ostream& error_msgs) const {
1793  if (expr.expression_type().is_primitive())
1794  return;
1795  std::vector<expression> args;
1796  args.push_back(expr);
1797  fun f("transpose", args);
1798  set_fun_type(f, error_msgs);
1799  expr = expression(f);
1800  pass = !expr.expression_type().is_ill_formed();
1801  }
1802  boost::phoenix::function<transpose_expr> transpose_f;
1803 
1804  void add_idxs::operator()(expression& e, std::vector<idx>& idxs,
1805  bool& pass, std::ostream& error_msgs) const {
1806  e = index_op_sliced(e, idxs);
1807  pass = !e.expression_type().is_ill_formed();
1808  if (!pass)
1809  error_msgs << "Indexed expression must have at least as many"
1810  << " dimensions as number of indexes supplied:"
1811  << std::endl
1812  << " indexed expression dims="
1813  << e.total_dims()
1814  << "; num indexes=" << idxs.size()
1815  << std::endl;
1816  }
1817  boost::phoenix::function<add_idxs> add_idxs_f;
1818 
1820  std::vector<std::vector<stan::lang::expression> >& dimss,
1821  bool& pass, std::ostream& error_msgs) const {
1822  index_op iop(expression, dimss);
1823  int expr_dims = expression.total_dims();
1824  int index_dims = num_dimss(dimss);
1825  if (expr_dims < index_dims) {
1826  error_msgs << "Indexed expression must have at least as many"
1827  << " dimensions as number of indexes supplied: "
1828  << std::endl
1829  << " indexed expression dimensionality = " << expr_dims
1830  << "; indexes supplied = " << dimss.size()
1831  << std::endl;
1832  pass = false;
1833  return;
1834  }
1835  iop.infer_type();
1836  if (iop.type_.is_ill_formed()) {
1837  error_msgs << "Indexed expression must have at least as many"
1838  << " dimensions as number of indexes supplied."
1839  << std::endl;
1840  pass = false;
1841  return;
1842  }
1843  pass = true;
1844  expression = iop;
1845  }
1846  boost::phoenix::function<add_expression_dimss> add_expression_dimss_f;
1847 
1849  expression& val, variable_map& vm,
1850  std::ostream& error_msgs, bool& pass) const {
1851  std::string name = var_expr.name_;
1852  if (name == std::string("lp__")) {
1853  error_msgs << std::endl
1854  << "ERROR (fatal): Use of lp__ is no longer supported."
1855  << std::endl
1856  << " Use target += ... statement to increment log density."
1857  << std::endl
1858  << " Use target() function to get log density."
1859  << std::endl;
1860  pass = false;
1861  return;
1862  } else if (name == std::string("params_r__")) {
1863  error_msgs << std::endl << "WARNING:" << std::endl
1864  << " Direct access to params_r__ yields an inconsistent"
1865  << " statistical model in isolation and no guarantee is"
1866  << " made that this model will yield valid inferences."
1867  << std::endl
1868  << " Moreover, access to params_r__ is unsupported"
1869  << " and the variable may be removed without notice."
1870  << std::endl;
1871  }
1872  pass = vm.exists(name);
1873  if (pass) {
1874  var_expr.set_type(vm.get_base_type(name), vm.get_num_dims(name));
1875  } else {
1876  error_msgs << "variable \"" << name << '"' << " does not exist."
1877  << std::endl;
1878  return;
1879  }
1880  val = expression(var_expr);
1881  }
1882  boost::phoenix::function<set_var_type> set_var_type_f;
1883 
1885  std::stringstream& error_msgs)
1886  : error_msgs_(error_msgs) { }
1887 
1889  error_msgs_ << "nil declarations not allowed";
1890  return false; // fail if arises
1891  }
1893  if (x.range_.has_low() || x.range_.has_high()) {
1894  error_msgs_ << "require unconstrained."
1895  << " found range constraint." << std::endl;
1896  return false;
1897  }
1898  return true;
1899  }
1901  const {
1902  if (x.range_.has_low() || x.range_.has_high()) {
1903  error_msgs_ << "require unconstrained."
1904  << " found range constraint." << std::endl;
1905  return false;
1906  }
1907  return true;
1908  }
1910  const {
1911  if (x.range_.has_low() || x.range_.has_high()) {
1912  error_msgs_ << "require unconstrained."
1913  << " found range constraint." << std::endl;
1914  return false;
1915  }
1916  return true;
1917  }
1919  const {
1920  if (x.range_.has_low() || x.range_.has_high()) {
1921  error_msgs_ << "require unconstrained."
1922  << " found range constraint." << std::endl;
1923  return false;
1924  }
1925  return true;
1926  }
1928  const {
1929  if (x.range_.has_low() || x.range_.has_high()) {
1930  error_msgs_ << "require unconstrained."
1931  << " found range constraint." << std::endl;
1932  return false;
1933  }
1934  return true;
1935  }
1937  const unit_vector_var_decl& /*x*/) const {
1938  error_msgs_ << "require unconstrained variable declaration."
1939  << " found unit_vector." << std::endl;
1940  return false;
1941  }
1943  const {
1944  error_msgs_ << "require unconstrained variable declaration."
1945  << " found simplex." << std::endl;
1946  return false;
1947  }
1949  const {
1950  error_msgs_ << "require unconstrained variable declaration."
1951  << " found ordered." << std::endl;
1952  return false;
1953  }
1955  const positive_ordered_var_decl& /*x*/) const {
1956  error_msgs_ << "require unconstrained variable declaration."
1957  << " found positive_ordered." << std::endl;
1958  return false;
1959  }
1961  const cholesky_factor_var_decl& /*x*/) const {
1962  error_msgs_ << "require unconstrained variable declaration."
1963  << " found cholesky_factor." << std::endl;
1964  return false;
1965  }
1967  const cholesky_corr_var_decl& /*x*/) const {
1968  error_msgs_ << "require unconstrained variable declaration."
1969  << " found cholesky_factor_corr." << std::endl;
1970  return false;
1971  }
1973  const cov_matrix_var_decl& /*x*/) const {
1974  error_msgs_ << "require unconstrained variable declaration."
1975  << " found cov_matrix." << std::endl;
1976  return false;
1977  }
1979  const corr_matrix_var_decl& /*x*/) const {
1980  error_msgs_ << "require unconstrained variable declaration."
1981  << " found corr_matrix." << std::endl;
1982  return false;
1983  }
1984 
1985  data_only_expression::data_only_expression(std::stringstream& error_msgs,
1986  variable_map& var_map)
1987  : error_msgs_(error_msgs),
1988  var_map_(var_map) {
1989  }
1990  bool data_only_expression::operator()(const nil& /*e*/) const {
1991  return true;
1992  }
1994  return true;
1995  }
1997  return true;
1998  }
2000  for (size_t i = 0; i < x.args_.size(); ++i)
2001  if (!boost::apply_visitor(*this, x.args_[i].expr_))
2002  return false;
2003  return true;
2004  }
2006  var_origin origin = var_map_.get_origin(x.name_);
2007  bool is_data = (origin == data_origin)
2008  || (origin == transformed_data_origin)
2009  || (origin == local_origin);
2010  if (!is_data) {
2011  error_msgs_ << "non-data variables not allowed"
2012  << " in dimension declarations."
2013  << std::endl
2014  << " found variable=" << x.name_
2015  << "; declared in block=";
2016  print_var_origin(error_msgs_, origin);
2017  error_msgs_ << std::endl;
2018  }
2019  return is_data;
2020  }
2022  return boost::apply_visitor(*this, x.y0_.expr_)
2023  && boost::apply_visitor(*this, x.theta_.expr_);
2024  }
2026  const {
2027  return boost::apply_visitor(*this, x.y0_.expr_)
2028  && boost::apply_visitor(*this, x.theta_.expr_);
2029  }
2030  bool data_only_expression::operator()(const fun& x) const {
2031  for (size_t i = 0; i < x.args_.size(); ++i)
2032  if (!boost::apply_visitor(*this, x.args_[i].expr_))
2033  return false;
2034  return true;
2035  }
2037  if (!boost::apply_visitor(*this, x.expr_.expr_))
2038  return false;
2039  for (size_t i = 0; i < x.dimss_.size(); ++i)
2040  for (size_t j = 0; j < x.dimss_[i].size(); ++j)
2041  if (!boost::apply_visitor(*this, x.dimss_[i][j].expr_))
2042  return false;
2043  return true;
2044  }
2046  return boost::apply_visitor(*this, x.expr_.expr_);
2047  }
2049  return boost::apply_visitor(*this, x.cond_.expr_)
2050  && boost::apply_visitor(*this, x.true_val_.expr_)
2051  && boost::apply_visitor(*this, x.false_val_.expr_);
2052  }
2054  return boost::apply_visitor(*this, x.left.expr_)
2055  && boost::apply_visitor(*this, x.right.expr_);
2056  }
2058  return boost::apply_visitor(*this, x.subject.expr_);
2059  }
2060 
2061  void validate_decl_constraints::operator()(const bool& allow_constraints,
2062  const bool& declaration_ok,
2063  const var_decl& var_decl,
2064  bool& pass,
2065  std::stringstream& error_msgs)
2066  const {
2067  if (!declaration_ok) {
2068  error_msgs << "Problem with declaration." << std::endl;
2069  pass = false;
2070  return; // short-circuits test of constraints
2071  }
2072  if (allow_constraints) {
2073  pass = true;
2074  return;
2075  }
2076  validate_no_constraints_vis vis(error_msgs);
2077  pass = boost::apply_visitor(vis, var_decl.decl_);
2078  }
2079  boost::phoenix::function<validate_decl_constraints>
2081 
2082  void validate_identifier::reserve(const std::string& w) {
2083  reserved_word_set_.insert(w);
2084  }
2085  bool validate_identifier::contains(const std::set<std::string>& s,
2086  const std::string& x) const {
2087  return s.find(x) != s.end();
2088  }
2089  bool validate_identifier::identifier_exists(const std::string& identifier)
2090  const {
2091  return contains(reserved_word_set_, identifier)
2092  || (contains(function_signatures::instance().key_set(), identifier)
2093  && !contains(const_fun_name_set_, identifier));
2094  }
2095 
2097  // Constant functions which can be used as identifiers
2098  const_fun_name_set_.insert("pi");
2099  const_fun_name_set_.insert("e");
2100  const_fun_name_set_.insert("sqrt2");
2101  const_fun_name_set_.insert("log2");
2102  const_fun_name_set_.insert("log10");
2103  const_fun_name_set_.insert("not_a_number");
2104  const_fun_name_set_.insert("positive_infinity");
2105  const_fun_name_set_.insert("negative_infinity");
2106  const_fun_name_set_.insert("epsilon");
2107  const_fun_name_set_.insert("negative_epsilon");
2108 
2109  // illegal identifiers
2110  reserve("for");
2111  reserve("in");
2112  reserve("while");
2113  reserve("repeat");
2114  reserve("until");
2115  reserve("if");
2116  reserve("then");
2117  reserve("else");
2118  reserve("true");
2119  reserve("false");
2120 
2121  reserve("int");
2122  reserve("real");
2123  reserve("vector");
2124  reserve("unit_vector");
2125  reserve("simplex");
2126  reserve("ordered");
2127  reserve("positive_ordered");
2128  reserve("row_vector");
2129  reserve("matrix");
2130  reserve("cholesky_factor_cov");
2131  reserve("cholesky_factor_corr");
2132  reserve("cov_matrix");
2133  reserve("corr_matrix");
2134 
2135  reserve("target");
2136 
2137  reserve("model");
2138  reserve("data");
2139  reserve("parameters");
2140  reserve("quantities");
2141  reserve("transformed");
2142  reserve("generated");
2143 
2144  reserve("var");
2145  reserve("fvar");
2146  reserve("STAN_MAJOR");
2147  reserve("STAN_MINOR");
2148  reserve("STAN_PATCH");
2149  reserve("STAN_MATH_MAJOR");
2150  reserve("STAN_MATH_MINOR");
2151  reserve("STAN_MATH_PATCH");
2152 
2153  reserve("alignas");
2154  reserve("alignof");
2155  reserve("and");
2156  reserve("and_eq");
2157  reserve("asm");
2158  reserve("auto");
2159  reserve("bitand");
2160  reserve("bitor");
2161  reserve("bool");
2162  reserve("break");
2163  reserve("case");
2164  reserve("catch");
2165  reserve("char");
2166  reserve("char16_t");
2167  reserve("char32_t");
2168  reserve("class");
2169  reserve("compl");
2170  reserve("const");
2171  reserve("constexpr");
2172  reserve("const_cast");
2173  reserve("continue");
2174  reserve("decltype");
2175  reserve("default");
2176  reserve("delete");
2177  reserve("do");
2178  reserve("double");
2179  reserve("dynamic_cast");
2180  reserve("else");
2181  reserve("enum");
2182  reserve("explicit");
2183  reserve("export");
2184  reserve("extern");
2185  reserve("false");
2186  reserve("float");
2187  reserve("for");
2188  reserve("friend");
2189  reserve("goto");
2190  reserve("if");
2191  reserve("inline");
2192  reserve("int");
2193  reserve("long");
2194  reserve("mutable");
2195  reserve("namespace");
2196  reserve("new");
2197  reserve("noexcept");
2198  reserve("not");
2199  reserve("not_eq");
2200  reserve("nullptr");
2201  reserve("operator");
2202  reserve("or");
2203  reserve("or_eq");
2204  reserve("private");
2205  reserve("protected");
2206  reserve("public");
2207  reserve("register");
2208  reserve("reinterpret_cast");
2209  reserve("return");
2210  reserve("short");
2211  reserve("signed");
2212  reserve("sizeof");
2213  reserve("static");
2214  reserve("static_assert");
2215  reserve("static_cast");
2216  reserve("struct");
2217  reserve("switch");
2218  reserve("template");
2219  reserve("this");
2220  reserve("thread_local");
2221  reserve("throw");
2222  reserve("true");
2223  reserve("try");
2224  reserve("typedef");
2225  reserve("typeid");
2226  reserve("typename");
2227  reserve("union");
2228  reserve("unsigned");
2229  reserve("using");
2230  reserve("virtual");
2231  reserve("void");
2232  reserve("volatile");
2233  reserve("wchar_t");
2234  reserve("while");
2235  reserve("xor");
2236  reserve("xor_eq");
2237 
2238  // function names declared in signatures
2240  using std::set;
2241  using std::string;
2243 
2244  set<string> fun_names = sigs.key_set();
2245  for (set<string>::iterator it = fun_names.begin();
2246  it != fun_names.end();
2247  ++it)
2248  if (!contains(const_fun_name_set_, *it))
2249  reserve(*it);
2250  }
2251 
2252  void validate_identifier::operator()(const std::string& identifier,
2253  bool& pass,
2254  std::stringstream& error_msgs) const {
2255  int len = identifier.size();
2256  if (len >= 2
2257  && identifier[len-1] == '_'
2258  && identifier[len-2] == '_') {
2259  error_msgs << "variable identifier (name) may"
2260  << " not end in double underscore (__)"
2261  << std::endl
2262  << " found identifer=" << identifier << std::endl;
2263  pass = false;
2264  return;
2265  }
2266  size_t period_position = identifier.find('.');
2267  if (period_position != std::string::npos) {
2268  error_msgs << "variable identifier may not contain a period (.)"
2269  << std::endl
2270  << " found period at position (indexed from 0)="
2271  << period_position
2272  << std::endl
2273  << " found identifier=" << identifier
2274  << std::endl;
2275  pass = false;
2276  return;
2277  }
2278  if (identifier_exists(identifier)) {
2279  error_msgs << "variable identifier (name) may not be reserved word"
2280  << std::endl
2281  << " found identifier=" << identifier
2282  << std::endl;
2283  pass = false;
2284  return;
2285  }
2286  pass = true;
2287  }
2288  boost::phoenix::function<validate_identifier> validate_identifier_f;
2289 
2290  // copies single dimension from M to N if only M declared
2293  if (is_nil(var_decl.N_))
2294  var_decl.N_ = var_decl.M_;
2295  }
2296  boost::phoenix::function<copy_square_cholesky_dimension_if_necessary>
2298 
2300  std::stringstream& /*error_msgs*/) const {
2301  r = range();
2302  }
2303  boost::phoenix::function<empty_range> empty_range_f;
2304 
2305 
2307  bool& pass,
2308  std::stringstream& error_msgs) const {
2309  if (!expr.expression_type().is_primitive_int()) {
2310  error_msgs << "expression denoting integer required; found type="
2311  << expr.expression_type() << std::endl;
2312  pass = false;
2313  return;
2314  }
2315  pass = true;
2316  }
2317  boost::phoenix::function<validate_int_expr> validate_int_expr_f;
2318 
2320  const expression& expr,
2321  bool& pass,
2322  std::stringstream& error_msgs) const {
2323  range.low_ = expr;
2324  validate_int_expr validator;
2325  validator(expr, pass, error_msgs);
2326  }
2327  boost::phoenix::function<set_int_range_lower> set_int_range_lower_f;
2328 
2330  const expression& expr,
2331  bool& pass,
2332  std::stringstream& error_msgs) const {
2333  range.high_ = expr;
2334  validate_int_expr validator;
2335  validator(expr, pass, error_msgs);
2336  }
2337  boost::phoenix::function<set_int_range_upper> set_int_range_upper_f;
2338 
2340  int var_origin, bool& pass,
2341  variable_map& var_map,
2342  std::stringstream& error_msgs)
2343  const {
2344  if (!expr.expression_type().is_primitive_int()) {
2345  error_msgs << "dimension declaration requires expression"
2346  << " denoting integer; found type="
2347  << expr.expression_type()
2348  << std::endl;
2349  pass = false;
2350  } else if (var_origin != local_origin) {
2351  data_only_expression vis(error_msgs, var_map);
2352  bool only_data_dimensions = boost::apply_visitor(vis, expr.expr_);
2353  pass = only_data_dimensions;
2354  } else {
2355  // don't need to check data vs. parameter in dimensions for
2356  // local variable declarations
2357  pass = true;
2358  }
2359  }
2360  boost::phoenix::function<validate_int_data_expr> validate_int_data_expr_f;
2361 
2363  const expression& expr,
2364  bool& pass,
2365  std::stringstream& error_msgs)
2366  const {
2367  range.low_ = expr;
2368  pass = validate_double_expr(expr, error_msgs);
2369  }
2370  boost::phoenix::function<set_double_range_lower> set_double_range_lower_f;
2371 
2373  const expression& expr,
2374  bool& pass,
2375  std::stringstream& error_msgs)
2376  const {
2377  range.high_ = expr;
2378  pass = validate_double_expr(expr, error_msgs);
2379  }
2380  boost::phoenix::function<set_double_range_upper> set_double_range_upper_f;
2381 
2382  template <typename T>
2383  void add_var::operator()(var_decl& var_decl_result, const T& var_decl,
2384  variable_map& vm, bool& pass, const var_origin& vo,
2385  std::ostream& error_msgs) const {
2386  if (vm.exists(var_decl.name_)) {
2387  // variable already exists
2388  pass = false;
2389  error_msgs << "duplicate declaration of variable, name="
2390  << var_decl.name_;
2391 
2392  error_msgs << "; attempt to redeclare as ";
2393  print_var_origin(error_msgs, vo); // FIXME -- need original vo
2394 
2395  error_msgs << "; original declaration as ";
2396  print_var_origin(error_msgs, vm.get_origin(var_decl.name_));
2397 
2398  error_msgs << std::endl;
2399  var_decl_result = var_decl;
2400  return;
2401  }
2402  if ((vo == parameter_origin || vo == transformed_parameter_origin)
2403  && var_decl.base_type_ == INT_T) {
2404  pass = false;
2405  error_msgs << "integer parameters or transformed parameters"
2406  << " are not allowed; "
2407  << " found declared type int, parameter name="
2408  << var_decl.name_
2409  << std::endl;
2410  var_decl_result = var_decl;
2411  return;
2412  }
2413  pass = true; // probably don't need to set true
2414  vm.add(var_decl.name_, var_decl, vo);
2415  var_decl_result = var_decl;
2416  }
2417  boost::phoenix::function<add_var> add_var_f;
2418 
2419  template void add_var::operator()(var_decl&, const int_var_decl&,
2420  variable_map&, bool&, const var_origin&,
2421  std::ostream&) const;
2422  template void add_var::operator()(var_decl&, const double_var_decl&,
2423  variable_map&, bool&, const var_origin&,
2424  std::ostream&) const;
2425  template void add_var::operator()(var_decl&, const vector_var_decl&,
2426  variable_map&, bool&, const var_origin&,
2427  std::ostream&) const;
2428  template void add_var::operator()(var_decl&, const row_vector_var_decl&,
2429  variable_map&, bool&, const var_origin&,
2430  std::ostream&) const;
2431  template void add_var::operator()(var_decl&, const matrix_var_decl&,
2432  variable_map&, bool&, const var_origin&,
2433  std::ostream&) const;
2434  template void add_var::operator()(var_decl&, const simplex_var_decl&,
2435  variable_map&, bool&, const var_origin&,
2436  std::ostream&) const;
2437  template void add_var::operator()(var_decl&, const unit_vector_var_decl&,
2438  variable_map&, bool&, const var_origin&,
2439  std::ostream&) const;
2440  template void add_var::operator()(var_decl&, const ordered_var_decl&,
2441  variable_map&, bool&, const var_origin&,
2442  std::ostream&) const;
2443  template void add_var::operator()(var_decl&,
2445  variable_map&, bool&, const var_origin&,
2446  std::ostream&) const;
2447  template void add_var::operator()(var_decl&,
2448  const cholesky_factor_var_decl&,
2449  variable_map&, bool&, const var_origin&,
2450  std::ostream&) const;
2451  template void add_var::operator()(var_decl&, const cholesky_corr_var_decl&,
2452  variable_map&, bool&, const var_origin&,
2453  std::ostream&) const;
2454  template void add_var::operator()(var_decl&, const cov_matrix_var_decl&,
2455  variable_map&, bool&, const var_origin&,
2456  std::ostream&) const;
2457  template void add_var::operator()(var_decl&, const corr_matrix_var_decl&,
2458  variable_map&, bool&, const var_origin&,
2459  std::ostream&) const;
2460 
2461 
2462  }
2463 }
2464 
2465 #endif
expr_type get_result_type(const std::string &name, const std::vector< expr_type > &args, std::ostream &error_msgs, bool sampling_error_style=false)
Definition: ast_def.cpp:422
expression high_
Definition: ast.hpp:478
boost::phoenix::function< validate_return_allowed > validate_return_allowed_f
void operator()(const std::string &identifier, bool &allow_sampling, int &origin) const
void operator()(expression &expr_result, const expression &expr, bool &pass, std::ostream &error_msgs) const
variable_dims var_dims_
Definition: ast.hpp:991
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)
std::string strip_ccdf_suffix(const std::string &dist_fun)
Definition: ast_def.cpp:2020
boost::phoenix::function< set_void_function > set_void_function_f
bool lhs_var_occurs_on_rhs() const
Definition: ast_def.cpp:1793
void operator()(expression &expr1, const expression &expr2, std::ostream &error_msgs) const
std::string family_
Definition: ast.hpp:220
void add(const std::string &name, const expr_type &result_type, const std::vector< expr_type > &arg_types)
Definition: ast_def.cpp:180
boost::phoenix::function< add_while_body > add_while_body_f
void operator()(const std::string &identifier, bool &pass, std::stringstream &error_msgs) const
expr_type type_
Definition: ast.hpp:436
expression_t expr_
Definition: ast.hpp:286
const int function_argument_origin
Definition: ast.hpp:84
void operator()(const expr_type &arg_type, bool &pass, std::ostream &error_msgs) const
boost::phoenix::function< set_int_range_upper > set_int_range_upper_f
const int ROW_VECTOR_T
Definition: ast.hpp:72
void operator()(const bool &allow_sample, bool &pass, std::stringstream &error_msgs) const
expr_type type_
Definition: ast.hpp:416
void operator()(const integrate_ode &ode_fun, const variable_map &var_map, bool &pass, std::ostream &error_msgs) const
boost::phoenix::function< validate_assgn > validate_assgn_f
boost::phoenix::function< set_var_type > set_var_type_f
bool has_lp_suffix(const std::string &s)
Definition: ast_def.cpp:1889
void set_fun_type(fun &fun, std::ostream &error_msgs)
std::string name_
Definition: ast.hpp:358
const int derived_origin
Definition: ast.hpp:82
void operator()(expression &e, std::vector< idx > &idxs, bool &pass, std::ostream &error_msgs) const
size_t num_dims_
Definition: ast.hpp:99
distribution dist_
Definition: ast.hpp:982
std::vector< expression > dims_
Definition: ast.hpp:564
boost::phoenix::function< set_int_range_lower > set_int_range_lower_f
std::string original_name_
Definition: ast.hpp:414
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)
Definition: ast_def.cpp:2007
boost::phoenix::function< validate_expr_type3 > validate_expr_type3_f
void operator()(std::stringstream &error_msgs) const
range truncation_
Definition: ast.hpp:983
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
Definition: ast.hpp:80
void operator()(const expression &e, bool &pass, std::ostream &error_msgs) const
boost::phoenix::function< set_fun_type_named > set_fun_type_named_f
std::set< std::string > const_fun_name_set_
bool has_cdf_suffix(const std::string &name)
Definition: ast_def.cpp:2003
boost::phoenix::function< set_no_op > set_no_op_f
void operator()(const expression &e, bool &pass) const
boost::phoenix::function< validate_conditional_op > validate_conditional_op_f
const int function_argument_origin_rng
Definition: ast.hpp:86
void operator()(function_decl_def &decl, bool &pass, std::ostream &error_msgs) const
Probability, optimization and sampling library.
size_t num_dims() const
Definition: ast_def.cpp:116
bool deprecate_fun(const std::string &old_name, const std::string &new_name, fun &f, std::ostream &msgs)
std::string get_prob_fun(const std::string &dist_name)
Definition: ast_def.cpp:1976
static function_signatures & instance()
Definition: ast_def.cpp:150
void operator()(range &range, const expression &expr, bool &pass, std::stringstream &error_msgs) 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
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: ast.hpp:415
validate_no_constraints_vis(std::stringstream &error_msgs)
void add(const std::string &name, const base_var_decl &base_decl, const var_origin &vo)
Definition: ast_def.cpp:1287
bool has_prob_fun_suffix(const std::string &name)
Definition: ast_def.cpp:1987
void operator()(var_decl &var_decl_result, const T &var_decl, variable_map &vm, bool &pass, const var_origin &vo, std::ostream &error_msgs) const
std::string get_cdf(const std::string &dist_name)
Definition: ast_def.cpp:1958
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
Definition: ast_def.cpp:1268
std::vector< statement > bodies_
Definition: ast.hpp:888
const int DOUBLE_T
Definition: ast.hpp:70
bool has_rng_suffix(const std::string &s)
Definition: ast_def.cpp:1880
bool is_primitive_int() const
Definition: ast_def.cpp:99
bool validate_double_expr(const expression &expr, std::stringstream &error_msgs)
int base_expr_type
Definition: ast.hpp:64
boost::phoenix::function< validate_int_expression > validate_int_expression_f
const int transformed_parameter_origin
Definition: ast.hpp:81
bool has_prob_suffix(const std::string &s)
expression expr_
Definition: ast.hpp:434
void operator()(std::ostream &error_msgs) const
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
base_var_decl get(const std::string &name) const
Definition: ast_def.cpp:1271
void operator()(expression &expr_result, const expression &expr, std::ostream &error_msgs) 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
boost::phoenix::function< add_conditional_condition > add_conditional_condition_f
boost::phoenix::function< empty_range > empty_range_f
bool is_no_op_statement() const
Definition: ast_def.cpp:1637
boost::phoenix::function< subtraction_expr3 > subtraction3_f
boost::phoenix::function< addition_expr3 > addition3_f
void operator()(const expression &expr, int var_origin, bool &pass, variable_map &var_map, std::stringstream &error_msgs) const
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< add_var > add_var_f
base_var_decl var_type_
Definition: ast.hpp:993
boost::phoenix::function< remove_lp_var > remove_lp_var_f
void set_type(const base_expr_type &base_type, size_t num_dims)
Definition: ast_def.cpp:1016
var_origin get_origin(const std::string &name) const
Definition: ast_def.cpp:1282
void operator()(const integrate_ode_control &ode_fun, const variable_map &var_map, bool &pass, std::ostream &error_msgs) const
void operator()(arg_decl &decl, bool &pass, variable_map &vm, std::ostream &error_msgs) const
boost::phoenix::function< exponentiation_expr > exponentiation_f
boost::phoenix::function< add_loop_identifier > add_loop_identifier_f
expression true_val_
Definition: ast.hpp:446
std::string name_
Definition: ast.hpp:413
std::string get_ccdf(const std::string &dist_name)
Definition: ast_def.cpp:1967
boost::phoenix::function< set_double_range_lower > set_double_range_lower_f
boost::phoenix::function< validate_void_return_allowed > validate_void_return_allowed_f
bool is_void() const
Definition: ast_def.cpp:110
const int function_argument_origin_lp
Definition: ast.hpp:85
int total_dims() const
Definition: ast_def.cpp:727
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()(conditional_op &cond_expr, bool &pass, std::ostream &error_msgs) const
boost::phoenix::function< validate_non_void_arg_function > validate_non_void_arg_f
expr_type expression_type() const
Definition: ast_def.cpp:707
bool contains(const std::set< std::string > &s, const std::string &x) const
void print_var_origin(std::ostream &o, const var_origin &vo)
Definition: ast_def.cpp:1227
void operator()(statement &stmt, const pos_iterator_t &begin, const pos_iterator_t &end) const
void reserve(const std::string &w)
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
boost::phoenix::function< transpose_expr > transpose_f
boost::phoenix::function< multiplication_expr > multiplication_f
bool is_nil(const expression &e)
Definition: ast_def.cpp:955
void operator()(const std::string &s, bool &pass) const
var_decl_t decl_
Definition: ast.hpp:759
expression left
Definition: ast.hpp:459
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< validate_identifier > validate_identifier_f
void operator()(expression &expression, std::vector< std::vector< stan::lang::expression > > &dimss, bool &pass, std::ostream &error_msgs) const
void remove(const std::string &name)
Definition: ast_def.cpp:1292
const int void_function_argument_origin_rng
Definition: ast.hpp:89
boost::phoenix::function< validate_int_expr > validate_int_expr_f
bool fun_name_exists(const std::string &name)
Definition: ast_def.cpp:2029
void operator()(range &range, const expression &expr, bool &pass, std::stringstream &error_msgs) const
void operator()(expression &fun_result, fun &fun, const var_origin &var_origin, bool &pass, std::ostream &error_msgs) const
boost::phoenix::function< validate_decl_constraints > validate_decl_constraints_f
bool is_primitive() const
Definition: ast_def.cpp:95
bool ends_with(const std::string &suffix, const std::string &s)
Definition: ast_def.cpp:1951
bool returns_type(const expr_type &return_type, const statement &statement, std::ostream &error_msgs)
Definition: ast_def.cpp:642
boost::phoenix::function< add_expression_dimss > add_expression_dimss_f
void operator()(expression &expr1, const expression &expr2, std::ostream &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)
Definition: ast_def.cpp:170
boost::phoenix::function< validate_int_data_expr > validate_int_data_expr_f
std::vector< idx > idxs_
Definition: ast.hpp:1020
bool is_ill_formed() const
Definition: ast_def.cpp:107
std::vector< expression > args_
Definition: ast.hpp:221
const int VOID_T
Definition: ast.hpp:68
base_expr_type type() const
Definition: ast_def.cpp:113
void operator()(const assgn &a, bool &pass, std::ostream &error_msgs) const
expr_type indexed_type(const expression &e, const std::vector< idx > &idxs)
Return the type of the expression indexed by the generalized index sequence.
Definition: ast_def.cpp:1808
void operator()(variable &var_expr, expression &val, variable_map &vm, std::ostream &error_msgs, bool &pass) const
void operator()(const expression &expr, bool &pass, std::ostream &error_msgs) const
bool has_low() const
Definition: ast_def.cpp:1162
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
int var_origin
Definition: ast.hpp:76
const int void_function_argument_origin
Definition: ast.hpp:87
boost::phoenix::function< set_omni_idx > set_omni_idx_f
void generate_expression(const expression &e, std::ostream &o)
Definition: generator.hpp:433
base_expr_type base_type_
Definition: ast.hpp:565
size_t get_num_dims(const std::string &name) const
Definition: ast_def.cpp:1279
void operator()(var_origin origin, bool &pass, std::ostream &error_msgs) const
void operator()(const std::string &name, std::string &name_local, bool &pass, variable_map &vm, std::stringstream &error_msgs) const
std::pair< expr_type, std::vector< expr_type > > function_signature_t
Definition: ast.hpp:127
bool has_non_param_var(const expression &e, const variable_map &var_map)
Definition: ast_def.cpp:925
expression rhs_
Definition: ast.hpp:1021
boost::phoenix::function< validate_allow_sample > validate_allow_sample_f
const int data_origin
Definition: ast.hpp:78
boost::phoenix::function< assign_lhs > assign_lhs_f
std::set< std::string > reserved_word_set_
void operator()(size_t &lhs) const
void operator()(std::ostream &error_msgs) const
size_t begin_line_
Definition: ast.hpp:803
boost::phoenix::function< validate_integrate_ode > validate_integrate_ode_f
const int void_function_argument_origin_lp
Definition: ast.hpp:88
base_expr_type base_type_
Definition: ast.hpp:98
boost::phoenix::function< expression_as_statement > expression_as_statement_f
expr_type infer_type_indexing(const base_expr_type &expr_base_type, size_t num_expr_dims, size_t num_index_dims)
Definition: ast_def.cpp:1080
const int INT_T
Definition: ast.hpp:69
std::string integration_function_name_
Definition: ast.hpp:387
boost::phoenix::function< validate_pmf_pdf_variate > validate_pmf_pdf_variate_f
std::string name_
Definition: ast.hpp:926
std::vector< expression > conditions_
Definition: ast.hpp:887
std::vector< arg_decl > arg_decls_
Definition: ast.hpp:941
variable lhs_var_
Definition: ast.hpp:1019
std::set< std::string > key_set() const
Definition: ast_def.cpp:499
boost::phoenix::function< add_lp_var > add_lp_var_f
void operator()(const std::string &name, variable_map &vm) const
bool has_var(const expression &e, const variable_map &var_map)
Definition: ast_def.cpp:835
boost::phoenix::function< elt_multiplication_expr > elt_multiplication_f
bool is_univariate(const expr_type &et)
expression expr_
Definition: ast.hpp:981
void operator()(const expr_type &return_type, var_origin &origin, bool &pass, std::ostream &error_msgs) const
void operator()(variable_map &vm) const
std::vector< expression > dims_
Definition: ast.hpp:324
bool has_ccdf_suffix(const std::string &name)
Definition: ast_def.cpp:2016
base_var_decl base_variable_declaration()
Definition: ast_def.cpp:525
expression low_
Definition: ast.hpp:477
void operator()(function_decl_def &decl, variable_map &vm) const
void operator()(expression &expr1, const expression &expr2, const var_origin &var_origin, bool &pass, std::ostream &error_msgs) const
boost::phoenix::function< set_allows_sampling_origin > set_allows_sampling_origin_f
std::string name_
Definition: ast.hpp:323
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< validate_int_expr_warn > validate_int_expr_warn_f
boost::phoenix::function< logical_negate_expr > logical_negate_expr_f
bool identifier_exists(const std::string &identifier) const
expression expr_
Definition: ast.hpp:992
boost::phoenix::function< validate_sample > validate_sample_f
boost::phoenix::function< increment_size_t > increment_size_t_f
const int VECTOR_T
Definition: ast.hpp:71
boost::phoenix::function< add_function_signature > add_function_signature_f
void operator()(const expression &expr, bool &pass, std::stringstream &error_msgs) const
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 is_defined(const std::string &function_name, const std::vector< expr_type > &arg_types)
boost::phoenix::function< set_double_range_upper > set_double_range_upper_f
boost::phoenix::function< add_idxs > add_idxs_f
expression right
Definition: ast.hpp:460
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)
Definition: ast_def.cpp:1992
expression subject
Definition: ast.hpp:470
std::vector< expression > args_
Definition: ast.hpp:350
const int local_origin
Definition: ast.hpp:83
void operator()(const std::string &name, const var_origin &origin_allowed, variable &v, bool &pass, const variable_map &vm, std::ostream &error_msgs) const
boost::phoenix::function< unscope_locals > unscope_locals_f
std::ostream & write_base_expr_type(std::ostream &o, base_expr_type type)
Definition: ast_def.cpp:25
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) const
void set_user_defined(const std::pair< std::string, function_signature_t > &name_sig)
Definition: ast_def.cpp:157
boost::phoenix::function< division_expr > division_f
boost::phoenix::function< unscope_variables > unscope_variables_f
void operator()(const sample &s, const variable_map &var_map, bool &pass, std::ostream &error_msgs) const
boost::phoenix::function< elt_division_expr > elt_division_f
boost::phoenix::function< identifier_to_var > identifier_to_var_f
void operator()(range &r, std::stringstream &) const
boost::phoenix::function< negate_expr > negate_expr_f
void operator()(variable_map &vm) const
void operator()(L &lhs, const R &rhs) const
boost::phoenix::function< copy_square_cholesky_dimension_if_necessary > copy_square_cholesky_dimension_if_necessary_f
boost::phoenix::function< validate_return_type > validate_return_type_f
bool is_primitive_double() const
Definition: ast_def.cpp:103
boost::phoenix::function< add_fun_var > add_fun_var_f
void operator()(expression &expr1, const expression &expr2, std::ostream &error_msgs) const
std::vector< std::vector< expression > > dimss_
Definition: ast.hpp:435
boost::phoenix::function< add_while_condition > add_while_condition_f
boost::phoenix::function< validate_assignment > validate_assignment_f
base_expr_type get_base_type(const std::string &name) const
Definition: ast_def.cpp:1276
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
Placeholder struct for boost::variant default ctors.
Definition: ast.hpp:18
boost::phoenix::function< validate_declarations > validate_declarations_f
void operator()(no_op_statement &s) const
const int ILL_FORMED_T
Definition: ast.hpp:74
boost::phoenix::function< left_division_expr > left_division_f
boost::phoenix::function< set_void_return > set_void_return_f
const int MATRIX_T
Definition: ast.hpp:73
boost::phoenix::function< modulus_expr > modulus_f
expression false_val_
Definition: ast.hpp:447
data_only_expression(std::stringstream &error_msgs, variable_map &var_map)
boost::phoenix::function< scope_lp > scope_lp_f
void operator()(conditional_statement &cs, const statement &s) const
void operator()(return_statement &s) const
const int transformed_data_origin
Definition: ast.hpp:79
boost::phoenix::function< deprecate_old_assignment_op > deprecate_old_assignment_op_f
void operator()(assignment &a, const var_origin &origin_allowed, bool &pass, variable_map &vm, std::ostream &error_msgs) const
void operator()(var_origin origin, bool &pass, std::ostream &error_msgs) const
boost::phoenix::function< validate_non_void_expression > validate_non_void_expression_f
void operator()(variable_map &vm) const
void operator()(bool &pass, const stan::lang::expression &expr, std::stringstream &error_msgs) const
bool has_high() const
Definition: ast_def.cpp:1165
void operator()(pos_iterator_t _begin, pos_iterator_t _end, pos_iterator_t _where, variable_map &vm, std::stringstream &error_msgs) const

     [ Stan Home Page ] © 2011–2016, Stan Development Team.