Stan  2.13.1
probability, sampling & optimization
ast_def.cpp
Go to the documentation of this file.
1 #ifndef STAN_LANG_AST_DEF_CPP
2 #define STAN_LANG_AST_DEF_CPP
3 
4 #include <boost/variant/apply_visitor.hpp>
5 #include <boost/variant/recursive_variant.hpp>
6 
7 #include <stan/lang/ast.hpp>
8 
9 #include <cstddef>
10 #include <limits>
11 #include <climits>
12 #include <istream>
13 #include <iostream>
14 #include <map>
15 #include <set>
16 #include <stdexcept>
17 #include <string>
18 #include <utility>
19 #include <vector>
20 
21 namespace stan {
22 
23  namespace lang {
24 
25  std::ostream& write_base_expr_type(std::ostream& o, base_expr_type type) {
26  switch (type) {
27  case INT_T :
28  o << "int";
29  break;
30  case DOUBLE_T :
31  o << "real";
32  break;
33  case VECTOR_T :
34  o << "vector";
35  break;
36  case ROW_VECTOR_T :
37  o << "row vector";
38  break;
39  case MATRIX_T :
40  o << "matrix";
41  break;
42  case ILL_FORMED_T :
43  o << "ill formed";
44  break;
45  case VOID_T :
46  o << "void";
47  break;
48  default:
49  o << "UNKNOWN";
50  }
51  return o;
52  }
53 
54  // expr_type ctors and methods
56  : base_type_(ILL_FORMED_T),
57  num_dims_(0) {
58  }
60  : base_type_(base_type),
61  num_dims_(0) {
62  }
64  size_t num_dims)
65  : base_type_(base_type),
66  num_dims_(num_dims) {
67  }
68  bool expr_type::operator==(const expr_type& et) const {
69  return base_type_ == et.base_type_
70  && num_dims_ == et.num_dims_;
71  }
72  bool expr_type::operator!=(const expr_type& et) const {
73  return !(*this == et);
74  }
75  bool expr_type::operator<(const expr_type& et) const {
76  return (base_type_ < et.base_type_)
77  || (base_type_ == et.base_type_
78  && num_dims_ < et.num_dims_);
79  }
80  bool expr_type::operator<=(const expr_type& et) const {
81  return (base_type_ < et.base_type_)
82  || (base_type_ == et.base_type_
83  && num_dims_ <= et.num_dims_);
84  }
85  bool expr_type::operator>(const expr_type& et) const {
86  return (base_type_ > et.base_type_)
87  || (base_type_ == et.base_type_
88  && num_dims_ > et.num_dims_);
89  }
90  bool expr_type::operator>=(const expr_type& et) const {
91  return (base_type_ > et.base_type_)
92  || (base_type_ == et.base_type_
93  && num_dims_ >= et.num_dims_);
94  }
95  bool expr_type::is_primitive() const {
96  return is_primitive_int()
98  }
100  return base_type_ == INT_T
101  && num_dims_ == 0U;
102  }
104  return base_type_ == DOUBLE_T
105  && num_dims_ == 0U;
106  }
108  return base_type_ == ILL_FORMED_T;
109  }
110  bool expr_type::is_void() const {
111  return base_type_ == VOID_T;
112  }
114  return base_type_;
115  }
116  size_t expr_type::num_dims() const {
117  return num_dims_;
118  }
119 
120  // output matches unsized types used to declare functions
121  std::ostream& operator<<(std::ostream& o, const expr_type& et) {
122  write_base_expr_type(o, et.type());
123  if (et.num_dims() > 0) {
124  o << '[';
125  for (size_t i = 1; i < et.num_dims(); ++i)
126  o << ",";
127  o << ']';
128  }
129  return o;
130  }
131 
133  if (!et.is_primitive())
134  return expr_type();
135  return et;
136  }
137 
139  const expr_type& et2) {
140  if (!et1.is_primitive() || !et2.is_primitive())
141  return expr_type();
142  return et1.type() == DOUBLE_T ? et1 : et2;
143  }
144 
146  if (sigs_ == 0) return;
147  delete sigs_;
148  sigs_ = 0;
149  }
151  // FIXME: for threaded models, requires double-check lock
152  if (!sigs_)
153  sigs_ = new function_signatures;
154  return *sigs_;
155  }
156 
157  void
159  std::pair<std::string,
161  name_sig) {
162  user_defined_set_.insert(name_sig);
163  }
164 
165  bool
166  function_signatures::is_user_defined(const std::pair<std::string,
168  name_sig) {
169  return user_defined_set_.find(name_sig) != user_defined_set_.end();
170  }
171 
172  bool function_signatures::is_defined(const std::string& name,
173  const function_signature_t& sig) {
174  if (sigs_map_.find(name) == sigs_map_.end())
175  return false;
176  const std::vector<function_signature_t> sigs = sigs_map_[name];
177  for (size_t i = 0; i < sigs.size(); ++i)
178  if (sig.second == sigs[i].second)
179  return true;
180  return false;
181  }
182 
184  const {
185  using std::map;
186  using std::string;
187  using std::vector;
188  map<string, vector<function_signature_t> >::const_iterator it
189  = sigs_map_.find(fun);
190  if (it == sigs_map_.end())
191  return false;
192  const vector<function_signature_t> sigs = it->second;
193  for (size_t i = 0; i < sigs.size(); ++i) {
194  if (sigs[i].second.size() == 0
195  || sigs[i].second[0].base_type_ != INT_T)
196  return false;
197  }
198  return true;
199  }
200 
201  void function_signatures::add(const std::string& name,
202  const expr_type& result_type,
203  const std::vector<expr_type>& arg_types) {
204  sigs_map_[name].push_back(function_signature_t(result_type, arg_types));
205  }
206  void function_signatures::add(const std::string& name,
207  const expr_type& result_type) {
208  std::vector<expr_type> arg_types;
209  add(name, result_type, arg_types);
210  }
211  void function_signatures::add(const std::string& name,
212  const expr_type& result_type,
213  const expr_type& arg_type) {
214  std::vector<expr_type> arg_types;
215  arg_types.push_back(arg_type);
216  add(name, result_type, arg_types);
217  }
218  void function_signatures::add(const std::string& name,
219  const expr_type& result_type,
220  const expr_type& arg_type1,
221  const expr_type& arg_type2) {
222  std::vector<expr_type> arg_types;
223  arg_types.push_back(arg_type1);
224  arg_types.push_back(arg_type2);
225  add(name, result_type, arg_types);
226  }
227  void function_signatures::add(const std::string& name,
228  const expr_type& result_type,
229  const expr_type& arg_type1,
230  const expr_type& arg_type2,
231  const expr_type& arg_type3) {
232  std::vector<expr_type> arg_types;
233  arg_types.push_back(arg_type1);
234  arg_types.push_back(arg_type2);
235  arg_types.push_back(arg_type3);
236  add(name, result_type, arg_types);
237  }
238  void function_signatures::add(const std::string& name,
239  const expr_type& result_type,
240  const expr_type& arg_type1,
241  const expr_type& arg_type2,
242  const expr_type& arg_type3,
243  const expr_type& arg_type4) {
244  std::vector<expr_type> arg_types;
245  arg_types.push_back(arg_type1);
246  arg_types.push_back(arg_type2);
247  arg_types.push_back(arg_type3);
248  arg_types.push_back(arg_type4);
249  add(name, result_type, arg_types);
250  }
251  void function_signatures::add(const std::string& name,
252  const expr_type& result_type,
253  const expr_type& arg_type1,
254  const expr_type& arg_type2,
255  const expr_type& arg_type3,
256  const expr_type& arg_type4,
257  const expr_type& arg_type5) {
258  std::vector<expr_type> arg_types;
259  arg_types.push_back(arg_type1);
260  arg_types.push_back(arg_type2);
261  arg_types.push_back(arg_type3);
262  arg_types.push_back(arg_type4);
263  arg_types.push_back(arg_type5);
264  add(name, result_type, arg_types);
265  }
266  void function_signatures::add(const std::string& name,
267  const expr_type& result_type,
268  const expr_type& arg_type1,
269  const expr_type& arg_type2,
270  const expr_type& arg_type3,
271  const expr_type& arg_type4,
272  const expr_type& arg_type5,
273  const expr_type& arg_type6) {
274  std::vector<expr_type> arg_types;
275  arg_types.push_back(arg_type1);
276  arg_types.push_back(arg_type2);
277  arg_types.push_back(arg_type3);
278  arg_types.push_back(arg_type4);
279  arg_types.push_back(arg_type5);
280  arg_types.push_back(arg_type6);
281  add(name, result_type, arg_types);
282  }
283  void function_signatures::add(const std::string& name,
284  const expr_type& result_type,
285  const expr_type& arg_type1,
286  const expr_type& arg_type2,
287  const expr_type& arg_type3,
288  const expr_type& arg_type4,
289  const expr_type& arg_type5,
290  const expr_type& arg_type6,
291  const expr_type& arg_type7) {
292  std::vector<expr_type> arg_types;
293  arg_types.push_back(arg_type1);
294  arg_types.push_back(arg_type2);
295  arg_types.push_back(arg_type3);
296  arg_types.push_back(arg_type4);
297  arg_types.push_back(arg_type5);
298  arg_types.push_back(arg_type6);
299  arg_types.push_back(arg_type7);
300  add(name, result_type, arg_types);
301  }
302  void function_signatures::add_nullary(const::std::string& name) {
303  add(name, DOUBLE_T);
304  }
305  void function_signatures::add_unary(const::std::string& name) {
306  add(name, DOUBLE_T, DOUBLE_T);
307  }
309  name) {
310  for (size_t i = 0; i < 8; ++i) {
311  add(name, expr_type(DOUBLE_T, i), expr_type(INT_T, i));
312  add(name, expr_type(DOUBLE_T, i), expr_type(DOUBLE_T, i));
313  add(name, expr_type(VECTOR_T, i), expr_type(VECTOR_T, i));
314  add(name, expr_type(ROW_VECTOR_T, i), expr_type(ROW_VECTOR_T, i));
315  add(name, expr_type(MATRIX_T, i), expr_type(MATRIX_T, i));
316  }
317  }
318  void function_signatures::add_binary(const::std::string& name) {
319  add(name, DOUBLE_T, DOUBLE_T, DOUBLE_T);
320  }
321  void function_signatures::add_ternary(const::std::string& name) {
322  add(name, DOUBLE_T, DOUBLE_T, DOUBLE_T, DOUBLE_T);
323  }
324  void function_signatures::add_quaternary(const::std::string& name) {
325  add(name, DOUBLE_T, DOUBLE_T, DOUBLE_T, DOUBLE_T, DOUBLE_T);
326  }
328  const std::vector<expr_type>& call_args,
329  const std::vector<expr_type>& sig_args) {
330  if (call_args.size() != sig_args.size()) {
331  return -1; // failure
332  }
333  int num_promotions = 0;
334  for (size_t i = 0; i < call_args.size(); ++i) {
335  if (call_args[i] == sig_args[i]) {
336  continue;
337  } else if (call_args[i].is_primitive_int()
338  && sig_args[i].is_primitive_double()) {
339  ++num_promotions;
340  } else {
341  return -1; // failed match
342  }
343  }
344  return num_promotions;
345  }
346  int function_signatures::get_signature_matches(const std::string& name,
347  const std::vector<expr_type>& args,
348  function_signature_t& signature) {
349  if (!has_key(name)) return 0;
350  std::vector<function_signature_t> signatures = sigs_map_[name];
351  size_t min_promotions = std::numeric_limits<size_t>::max();
352  size_t num_matches = 0;
353  for (size_t i = 0; i < signatures.size(); ++i) {
354  signature = signatures[i];
355  int promotions = num_promotions(args, signature.second);
356  if (promotions < 0) continue; // no match
357  size_t promotions_ui = static_cast<size_t>(promotions);
358  if (promotions_ui < min_promotions) {
359  min_promotions = promotions_ui;
360  num_matches = 1;
361  } else if (promotions_ui == min_promotions) {
362  ++num_matches;
363  }
364  }
365  return num_matches;
366  }
367 
368  bool is_binary_operator(const std::string& name) {
369  return name == "add"
370  || name == "subtract"
371  || name == "multiply"
372  || name == "divide"
373  || name == "modulus"
374  || name == "mdivide_left"
375  || name == "mdivide_right"
376  || name == "elt_multiply"
377  || name == "elt_divide";
378  }
379 
380  bool is_unary_operator(const std::string& name) {
381  return name == "minus"
382  || name == "logical_negation";
383  }
384 
385  bool is_unary_postfix_operator(const std::string& name) {
386  return name == "transpose";
387  }
388 
389  bool is_operator(const std::string& name) {
390  return is_binary_operator(name)
391  || is_unary_operator(name)
392  || is_unary_postfix_operator(name);
393  }
394 
395  std::string fun_name_to_operator(const std::string& name) {
396  // binary infix (pow handled by parser)
397  if (name == "add") return "+";
398  if (name == "subtract") return "-";
399  if (name == "multiply") return "*";
400  if (name == "divide") return "/";
401  if (name == "modulus") return "%";
402  if (name == "mdivide_left") return "\\";
403  if (name == "mdivide_right") return "/";
404  if (name == "elt_multiply") return ".*";
405  if (name == "elt_divide") return "./";
406 
407  // unary prefix (+ handled by parser)
408  if (name == "minus") return "-";
409  if (name == "logical_negation") return "!";
410 
411  // unary suffix
412  if (name == "transpose") return "'";
413 
414  // none of the above
415  return "ERROR";
416  }
417 
418  void print_signature(const std::string& name,
419  const std::vector<expr_type>& arg_types,
420  bool sampling_error_style,
421  std::ostream& msgs) {
422  static size_t OP_SIZE = std::string("operator").size();
423  msgs << " ";
424  if (name.size() > OP_SIZE && name.substr(0, OP_SIZE) == "operator") {
425  std::string operator_name = name.substr(OP_SIZE);
426  if (arg_types.size() == 2) {
427  msgs << arg_types[0] << " " << operator_name << " " << arg_types[1]
428  << std::endl;
429  return;
430  } else if (arg_types.size() == 1) {
431  if (operator_name == "'") // exception for postfix
432  msgs << arg_types[0] << operator_name << std::endl;
433  else
434  msgs << operator_name << arg_types[0] << std::endl;
435  return;
436  } else {
437  // should not be reachable due to operator grammar
438  // continue on purpose to get more info to user if this happens
439  msgs << "Operators must have 1 or 2 arguments." << std::endl;
440  }
441  }
442  if (sampling_error_style && arg_types.size() > 0)
443  msgs << arg_types[0] << " ~ ";
444  msgs << name << "(";
445  size_t start = sampling_error_style ? 1 : 0;
446  for (size_t j = start; j < arg_types.size(); ++j) {
447  if (j > start) msgs << ", ";
448  msgs << arg_types[j];
449  }
450  msgs << ")" << std::endl;
451  }
452 
454  const std::vector<expr_type>& args,
455  std::ostream& error_msgs,
456  bool sampling_error_style) {
457  std::vector<function_signature_t> signatures = sigs_map_[name];
458  size_t match_index = 0;
459  size_t min_promotions = std::numeric_limits<size_t>::max();
460  size_t num_matches = 0;
461 
462  std::string display_name;
463  if (is_operator(name)) {
464  display_name = "operator" + fun_name_to_operator(name);
465  } else if (sampling_error_style && ends_with("_log", name)) {
466  display_name = name.substr(0, name.size() - 4);
467  } else if (sampling_error_style
468  && (ends_with("_lpdf", name) || ends_with("_lcdf", name))) {
469  display_name = name.substr(0, name.size() - 5);
470  } else {
471  display_name = name;
472  }
473 
474  for (size_t i = 0; i < signatures.size(); ++i) {
475  int promotions = num_promotions(args, signatures[i].second);
476  if (promotions < 0) continue; // no match
477  size_t promotions_ui = static_cast<size_t>(promotions);
478  if (promotions_ui < min_promotions) {
479  min_promotions = promotions_ui;
480  match_index = i;
481  num_matches = 1;
482  } else if (promotions_ui == min_promotions) {
483  ++num_matches;
484  }
485  }
486 
487  if (num_matches == 1)
488  return signatures[match_index].first;
489 
490  // all returns after here are for ill-typed input
491 
492  if (num_matches == 0) {
493  error_msgs << "No matches for: "
494  << std::endl << std::endl;
495  } else {
496  error_msgs << "Ambiguous: "
497  << num_matches << " matches with "
498  << min_promotions << " integer promotions for: "
499  << std::endl;
500  }
501  print_signature(display_name, args, sampling_error_style, error_msgs);
502 
503  if (signatures.size() == 0) {
504  error_msgs << std::endl
505  << (sampling_error_style ? "Distribution " : "Function ")
506  << display_name << " not found.";
507  if (sampling_error_style)
508  error_msgs << " Require function with _lpdf or _lpmf or _log suffix";
509  error_msgs << std::endl;
510  } else {
511  error_msgs << std::endl
512  << "Available argument signatures for "
513  << display_name << ":" << std::endl << std::endl;
514 
515  for (size_t i = 0; i < signatures.size(); ++i) {
516  print_signature(display_name, signatures[i].second,
517  sampling_error_style, error_msgs);
518  }
519  error_msgs << std::endl;
520  }
521  return expr_type(); // ill-formed dummy
522  }
523 
524  function_signatures::function_signatures() {
525 #include <stan/lang/function_signatures.h> // NOLINT
526  }
527 
528  bool function_signatures::has_user_defined_key(const std::string& key)
529  const {
530  using std::pair;
531  using std::set;
532  using std::string;
533  for (set<pair<string, function_signature_t> >::const_iterator
534  it = user_defined_set_.begin();
535  it != user_defined_set_.end();
536  ++it) {
537  if (it->first == key)
538  return true;
539  }
540  return false;
541  }
542 
543  std::set<std::string> function_signatures::key_set() const {
544  using std::map;
545  using std::set;
546  using std::string;
547  using std::vector;
548  set<string> result;
549  for (map<string, vector<function_signature_t> >::const_iterator
550  it = sigs_map_.begin();
551  it != sigs_map_.end();
552  ++it)
553  result.insert(it->first);
554  return result;
555  }
556 
557  bool function_signatures::has_key(const std::string& key) const {
558  return sigs_map_.find(key) != sigs_map_.end();
559  }
560 
561  function_signatures* function_signatures::sigs_ = 0;
562 
564  arg_decl::arg_decl(const expr_type& arg_type,
565  const std::string& name)
566  : arg_type_(arg_type),
567  name_(name) {
568  }
570  std::vector<expression> dims;
571  for (size_t i = 0; i < arg_type_.num_dims_; ++i)
572  dims.push_back(expression(int_literal(0))); // dummy value 0
573  return base_var_decl(name_, dims, arg_type_.base_type_);
574  }
575 
578  const std::string& name,
579  const std::vector<arg_decl>& arg_decls,
580  const statement& body)
581 
582  : return_type_(return_type),
583  name_(name),
584  arg_decls_(arg_decls),
585  body_(body) {
586  }
587 
589  function_decl_defs::function_decl_defs(const std::vector<function_decl_def>&
590  decl_defs)
591  : decl_defs_(decl_defs) {
592  }
593 
595  std::ostream& error_msgs)
596  : return_type_(return_type),
597  error_msgs_(error_msgs) {
598  }
599  bool returns_type_vis::operator()(const nil& st) const {
600  error_msgs_ << "Expecting return, found nil statement."
601  << std::endl;
602  return false;
603  }
604  bool returns_type_vis::operator()(const assignment& st) const {
605  error_msgs_ << "Expecting return, found assignment statement."
606  << std::endl;
607  return false;
608  }
609  bool returns_type_vis::operator()(const assgn& st) const {
610  error_msgs_ << "Expecting return, found assignment statement."
611  << std::endl;
612  return false;
613  }
614  bool returns_type_vis::operator()(const sample& st) const {
615  error_msgs_ << "Expecting return, found sampling statement."
616  << std::endl;
617  return false;
618  }
620  increment_log_prob_statement& t) const {
621  error_msgs_ << "Expecting return, found increment_log_prob statement."
622  << std::endl;
623  return false;
624  }
625  bool returns_type_vis::operator()(const expression& st) const {
626  error_msgs_ << "Expecting return, found increment_log_prob statement."
627  << std::endl;
628  return false;
629  }
631  error_msgs_ << "Expecting return, found print statement."
632  << std::endl;
633  return false;
634  }
636  error_msgs_ << "Expecting return, found reject statement."
637  << std::endl;
638  return false;
639  }
641  error_msgs_ << "Expecting return, found no_op statement."
642  << std::endl;
643  return false;
644  }
645  // recursive cases
646  bool returns_type_vis::operator()(const statements& st) const {
647  // last statement in sequence must return type
648  if (st.statements_.size() == 0) {
649  error_msgs_ << ("Expecting return, found"
650  " statement sequence with empty body.")
651  << std::endl;
652  return false;
653  }
654  return returns_type(return_type_, st.statements_.back(), error_msgs_);
655  }
657  // body must end in appropriate return
659  }
661  // body must end in appropriate return
663  }
665  const {
666  // break/continue OK only as end of nested loop in void return
667  bool pass = (return_type_ == VOID_T);
668  if (!pass)
669  error_msgs_ << "statement " << st.generate_
670  << " does not match return type";
671  return pass;
672  }
674  conditional_statement& st) const {
675  // all condition bodies must end in appropriate return
676  if (st.bodies_.size() != (st.conditions_.size() + 1)) {
677  error_msgs_ << ("Expecting return, found conditional"
678  " without final else.")
679  << std::endl;
680  return false;
681  }
682  for (size_t i = 0; i < st.bodies_.size(); ++i)
684  return false;
685  return true;
686  }
688  // return checked for type
689  return return_type_ == VOID_T
691  "Returned expression does not match return type",
692  error_msgs_);
693  }
694 
695  bool returns_type(const expr_type& return_type,
696  const statement& statement,
697  std::ostream& error_msgs) {
698  if (return_type == VOID_T)
699  return true;
700  returns_type_vis vis(return_type, error_msgs);
701  return boost::apply_visitor(vis, statement.statement_);
702  }
703 
704 
705 
707  statements::statements(const std::vector<var_decl>& local_decl,
708  const std::vector<statement>& stmts)
709  : local_decl_(local_decl),
710  statements_(stmts) {
711  }
712 
714  return expr_type();
715  }
717  return e.type_;
718  }
720  return e.type_;
721  }
723  return e.type_;
724  }
726  return e.type_;
727  }
729  return expr_type(DOUBLE_T, 2);
730  }
731  expr_type
733  return expr_type(DOUBLE_T, 2);
734  }
736  return e.type_;
737  }
739  return e.type_;
740  }
742  return e.type_;
743  }
745  return e.type_;
746  }
748  return e.type_;
749  }
751  return e.type_;
752  }
753 
755  : expr_(nil()) {
756  }
758  : expr_(e.expr_) {
759  }
762  return boost::apply_visitor(vis, expr_);
763  }
764 
765  expression::expression(const expression_t& expr) : expr_(expr) { }
766  expression::expression(const nil& expr) : expr_(expr) { }
767  expression::expression(const int_literal& expr) : expr_(expr) { }
768  expression::expression(const double_literal& expr) : expr_(expr) { }
769  expression::expression(const array_literal& expr) : expr_(expr) { }
770  expression::expression(const variable& expr) : expr_(expr) { }
771  expression::expression(const integrate_ode& expr) : expr_(expr) { }
773  expression::expression(const fun& expr) : expr_(expr) { }
774  expression::expression(const index_op& expr) : expr_(expr) { }
776  expression::expression(const conditional_op& expr) : expr_(expr) { }
777  expression::expression(const binary_op& expr) : expr_(expr) { }
778  expression::expression(const unary_op& expr) : expr_(expr) { }
779 
781  int sum = expression_type().num_dims_;
782  if (expression_type().type() == VECTOR_T)
783  ++sum;
784  if (expression_type().type() == ROW_VECTOR_T)
785  ++sum;
786  if (expression_type().type() == MATRIX_T)
787  sum += 2;
788  return sum;
789  }
790 
791  printable::printable() : printable_(std::string()) { }
792  printable::printable(const expression& expr) : printable_(expr) { }
793  printable::printable(const std::string& msg) : printable_(msg) { }
795  : printable_(printable) { }
797  : printable_(printable.printable_) { }
798 
800  : var_map_(var_map) {
801  }
802  bool contains_var::operator()(const nil& e) const {
803  return false;
804  }
805  bool contains_var::operator()(const int_literal& e) const {
806  return false;
807  }
809  return false;
810  }
812  for (size_t i = 0; i < e.args_.size(); ++i)
813  if (boost::apply_visitor(*this, e.args_[i].expr_))
814  return true;
815  return false;
816  }
817  bool contains_var::operator()(const variable& e) const {
819  return vo == parameter_origin
821  || (vo == local_origin && e.type_.base_type_ != INT_T);
822  }
823  bool contains_var::operator()(const fun& e) const {
824  for (size_t i = 0; i < e.args_.size(); ++i)
825  if (boost::apply_visitor(*this, e.args_[i].expr_))
826  return true;
827  return false;
828  }
830  // only init state and params may contain vars
831  return boost::apply_visitor(*this, e.y0_.expr_)
832  || boost::apply_visitor(*this, e.theta_.expr_);
833  }
835  // only init state and params may contain vars
836  return boost::apply_visitor(*this, e.y0_.expr_)
837  || boost::apply_visitor(*this, e.theta_.expr_);
838  }
839  bool contains_var::operator()(const index_op& e) const {
840  return boost::apply_visitor(*this, e.expr_.expr_);
841  }
843  return boost::apply_visitor(*this, e.expr_.expr_);
844  }
846  return boost::apply_visitor(*this, e.cond_.expr_)
847  || boost::apply_visitor(*this, e.true_val_.expr_)
848  || boost::apply_visitor(*this, e.false_val_.expr_);
849  }
850  bool contains_var::operator()(const binary_op& e) const {
851  return boost::apply_visitor(*this, e.left.expr_)
852  || boost::apply_visitor(*this, e.right.expr_);
853  }
854  bool contains_var::operator()(const unary_op& e) const {
855  return boost::apply_visitor(*this, e.subject.expr_);
856  }
857 
858  bool is_linear_function(const std::string& name) {
859  return name == "add"
860  || name == "block"
861  || name == "append_col"
862  || name == "col"
863  || name == "cols"
864  || name == "diagonal"
865  || name == "head"
866  || name == "minus"
867  || name == "negative_infinity"
868  || name == "not_a_number"
869  || name == "append_row"
870  || name == "rep_matrix"
871  || name == "rep_row_vector"
872  || name == "rep_vector"
873  || name == "row"
874  || name == "rows"
875  || name == "positive_infinity"
876  || name == "segment"
877  || name == "subtract"
878  || name == "sum"
879  || name == "tail"
880  || name == "to_vector"
881  || name == "to_row_vector"
882  || name == "to_matrix"
883  || name == "to_array_1d"
884  || name == "to_array_2d"
885  || name == "transpose";
886  }
887 
888  bool has_var(const expression& e,
889  const variable_map& var_map) {
890  contains_var vis(var_map);
891  return boost::apply_visitor(vis, e.expr_);
892  }
893 
895  : var_map_(var_map) {
896  }
897  bool contains_nonparam_var::operator()(const nil& e) const {
898  return false;
899  }
901  return false;
902  }
904  return false;
905  }
907  for (size_t i = 0; i < e.args_.size(); ++i)
908  if (boost::apply_visitor(*this, e.args_[i].expr_))
909  return true;
910  return false;
911  }
914  return (vo == transformed_parameter_origin
915  || vo == local_origin);
916  }
918  // if any vars, return true because integration will be nonlinear
919  return boost::apply_visitor(*this, e.y0_.expr_)
920  || boost::apply_visitor(*this, e.theta_.expr_);
921  }
923  const {
924  // if any vars, return true because integration will be nonlinear
925  return boost::apply_visitor(*this, e.y0_.expr_)
926  || boost::apply_visitor(*this, e.theta_.expr_);
927  }
928  bool contains_nonparam_var::operator()(const fun& e) const {
929  // any function applied to non-linearly transformed var
930  for (size_t i = 0; i < e.args_.size(); ++i)
931  if (boost::apply_visitor(*this, e.args_[i].expr_))
932  return true;
933  // non-linear function applied to var
934  if (!is_linear_function(e.name_)) {
935  for (size_t i = 0; i < e.args_.size(); ++i)
936  if (has_var(e.args_[i], var_map_))
937  return true;
938  }
939  return false;
940  }
942  return boost::apply_visitor(*this, e.expr_.expr_);
943  }
945  return boost::apply_visitor(*this, e.expr_.expr_);
946  }
947 
952  return true;
953  return false;
954  }
955 
957  if (e.op == "||"
958  || e.op == "&&"
959  || e.op == "=="
960  || e.op == "!="
961  || e.op == "<"
962  || e.op == "<="
963  || e.op == ">"
964  || e.op == ">=")
965  return true;
968  return true;
969  if (e.op == "*" || e.op == "/")
970  return has_var(e.left, var_map_) && has_var(e.right, var_map_);
971  return false;
972  }
974  // only negation, which is linear, so recurse
976  }
977 
979  const variable_map& var_map) {
980  contains_nonparam_var vis(var_map);
981  return boost::apply_visitor(vis, e.expr_);
982  }
983 
984  bool is_nil_op::operator()(const nil& /*x*/) const { return true; }
985  bool is_nil_op::operator()(const int_literal& /*x*/) const { return false; }
986  bool is_nil_op::operator()(const double_literal& /* x */) const {
987  return false;
988  }
990  const { return false; }
991  bool is_nil_op::operator()(const variable& /* x */) const { return false; }
992  bool is_nil_op::operator()(const integrate_ode& /* x */) const {
993  return false;
994  }
995  bool is_nil_op::operator()(const integrate_ode_control& /* x */) const {
996  return false;
997  }
998  bool is_nil_op::operator()(const fun& /* x */) const { return false; }
999  bool is_nil_op::operator()(const index_op& /* x */) const { return false; }
1000  bool is_nil_op::operator()(const index_op_sliced& /* x */) const {
1001  return false;
1002  }
1003  bool is_nil_op::operator()(const conditional_op& /* x */) const {
1004  return false; }
1005  bool is_nil_op::operator()(const binary_op& /* x */) const { return false; }
1006  bool is_nil_op::operator()(const unary_op& /* x */) const { return false; }
1007 
1008  bool is_nil(const expression& e) {
1009  is_nil_op ino;
1010  return boost::apply_visitor(ino, e.expr_);
1011  }
1012 
1013  variable_dims::variable_dims() { } // req for FUSION_ADAPT
1014  variable_dims::variable_dims(std::string const& name,
1015  std::vector<expression> const& dims)
1016  : name_(name),
1017  dims_(dims) {
1018  }
1019 
1020 
1022  : type_(INT_T) {
1023  }
1025  : val_(val),
1026  type_(INT_T) {
1027  }
1029  : val_(il.val_),
1030  type_(il.type_) {
1031  }
1033  val_ = il.val_;
1034  type_ = il.type_;
1035  return *this;
1036  }
1037 
1038 
1040  : type_(DOUBLE_T, 0U) {
1041  }
1043  : val_(val),
1044  type_(DOUBLE_T, 0U) {
1045  }
1047  val_ = dl.val_;
1048  type_ = dl.type_;
1049  return *this;
1050  }
1051 
1052 
1054  : args_(),
1055  type_(DOUBLE_T, 1U) {
1056  }
1057  array_literal::array_literal(const std::vector<expression>& args)
1058  : args_(args),
1059  type_() { // ill-formed w/o help
1060  }
1062  args_ = al.args_;
1063  type_ = al.type_;
1064  return *this;
1065  }
1066 
1068  variable::variable(std::string name) : name_(name) { }
1069  void variable::set_type(const base_expr_type& base_type,
1070  size_t num_dims) {
1071  type_ = expr_type(base_type, num_dims);
1072  }
1073 
1075  integrate_ode::integrate_ode(const std::string& integration_function_name,
1076  const std::string& system_function_name,
1077  const expression& y0,
1078  const expression& t0,
1079  const expression& ts,
1080  const expression& theta,
1081  const expression& x,
1082  const expression& x_int)
1083  : integration_function_name_(integration_function_name),
1084  system_function_name_(system_function_name),
1085  y0_(y0),
1086  t0_(t0),
1087  ts_(ts),
1088  theta_(theta),
1089  x_(x),
1090  x_int_(x_int) {
1091  }
1092 
1095  const std::string& integration_function_name,
1096  const std::string& system_function_name,
1097  const expression& y0,
1098  const expression& t0,
1099  const expression& ts,
1100  const expression& theta,
1101  const expression& x,
1102  const expression& x_int,
1103  const expression& rel_tol,
1104  const expression& abs_tol,
1105  const expression& max_num_steps)
1106  : integration_function_name_(integration_function_name),
1107  system_function_name_(system_function_name),
1108  y0_(y0),
1109  t0_(t0),
1110  ts_(ts),
1111  theta_(theta),
1112  x_(x),
1113  x_int_(x_int),
1114  rel_tol_(rel_tol),
1115  abs_tol_(abs_tol),
1116  max_num_steps_(max_num_steps) {
1117  }
1118 
1119  fun::fun() { }
1120  fun::fun(std::string const& name,
1121  std::vector<expression> const& args)
1122  : name_(name),
1123  args_(args) {
1124  }
1125 
1126  size_t total_dims(const std::vector<std::vector<expression> >& dimss) {
1127  size_t total = 0U;
1128  for (size_t i = 0; i < dimss.size(); ++i)
1129  total += dimss[i].size();
1130  return total;
1131  }
1132 
1134  size_t num_expr_dims,
1135  size_t num_index_dims) {
1136  if (num_index_dims <= num_expr_dims)
1137  return expr_type(expr_base_type, num_expr_dims - num_index_dims);
1138  if (num_index_dims == (num_expr_dims + 1)) {
1139  if (expr_base_type == VECTOR_T || expr_base_type == ROW_VECTOR_T)
1140  return expr_type(DOUBLE_T, 0U);
1141  if (expr_base_type == MATRIX_T)
1142  return expr_type(ROW_VECTOR_T, 0U);
1143  }
1144  if (num_index_dims == (num_expr_dims + 2))
1145  if (expr_base_type == MATRIX_T)
1146  return expr_type(DOUBLE_T, 0U);
1147 
1148  // error condition, result expr_type has is_ill_formed() = true
1149  return expr_type();
1150  }
1151 
1153  size_t num_index_dims) {
1155  expr.expression_type().num_dims(),
1156  num_index_dims);
1157  }
1158 
1159 
1162  const std::vector<std::vector<expression> >& dimss)
1163  : expr_(expr),
1164  dimss_(dimss) {
1165  infer_type();
1166  }
1169  }
1170 
1173  const std::vector<idx>& idxs)
1174  : expr_(expr), idxs_(idxs), type_(indexed_type(expr_, idxs_)) { }
1177  }
1178 
1181  const expression& true_val,
1182  const expression& false_val)
1183  : cond_(cond),
1184  true_val_(true_val),
1185  false_val_(false_val),
1186  type_(promote_primitive(true_val.expression_type(),
1187  false_val.expression_type())) {
1188  }
1189 
1192  const std::string& op,
1193  const expression& right)
1194  : op(op),
1195  left(left),
1196  right(right),
1197  type_(promote_primitive(left.expression_type(),
1198  right.expression_type())) {
1199  }
1200 
1202  expression const& subject)
1203  : op(op),
1204  subject(subject),
1205  type_(promote_primitive(subject.expression_type())) {
1206  }
1207 
1208 
1211  expression const& high)
1212  : low_(low),
1213  high_(high) {
1214  }
1215  bool range::has_low() const {
1216  return !is_nil(low_.expr_);
1217  }
1218  bool range::has_high() const {
1219  return !is_nil(high_.expr_);
1220  }
1221 
1223  uni_idx::uni_idx(const expression& idx) : idx_(idx) { }
1224 
1226  multi_idx::multi_idx(const expression& idxs) : idxs_(idxs) { }
1227 
1229 
1231  lb_idx::lb_idx(const expression& lb) : lb_(lb) { }
1232 
1234  ub_idx::ub_idx(const expression& ub) : ub_(ub) { }
1235 
1238  : lb_(lb), ub_(ub) {
1239  }
1240 
1241  idx::idx() { }
1242 
1243  idx::idx(const uni_idx& i) : idx_(i) { }
1244  idx::idx(const multi_idx& i) : idx_(i) { }
1245  idx::idx(const omni_idx& i) : idx_(i) { }
1246  idx::idx(const lb_idx& i) : idx_(i) { }
1247  idx::idx(const ub_idx& i) : idx_(i) { }
1248  idx::idx(const lub_idx& i) : idx_(i) { }
1249 
1250 
1253  return false;
1254  }
1256  return true;
1257  }
1259  return true;
1260  }
1262  return true;
1263  }
1265  return true;
1266  }
1268  return true;
1269  }
1270 
1271  bool is_multi_index(const idx& idx) {
1273  return boost::apply_visitor(v, idx.idx_);
1274  }
1275 
1276  bool is_data_origin(const var_origin& vo) {
1277  return vo == data_origin || vo == transformed_data_origin;
1278  }
1279 
1280  bool is_fun_origin(const var_origin& vo) {
1281  return vo == function_argument_origin
1287  }
1288 
1289  void print_var_origin(std::ostream& o, const var_origin& vo) {
1290  if (vo == model_name_origin)
1291  o << "model name";
1292  else if (vo == data_origin)
1293  o << "data";
1294  else if (vo == transformed_data_origin)
1295  o << "transformed data";
1296  else if (vo == parameter_origin)
1297  o << "parameter";
1298  else if (vo == transformed_parameter_origin)
1299  o << "transformed parameter";
1300  else if (vo == derived_origin)
1301  o << "generated quantities";
1302  else if (vo == local_origin)
1303  o << "local";
1304  else if (vo == function_argument_origin)
1305  o << "function argument";
1306  else if (vo == function_argument_origin_lp)
1307  o << "function argument '_lp' suffixed";
1308  else if (vo == function_argument_origin_rng)
1309  o << "function argument '_rng' suffixed";
1310  else if (vo == void_function_argument_origin)
1311  o << "void function argument";
1312  else if (vo == void_function_argument_origin_lp)
1313  o << "void function argument '_lp' suffixed";
1314  else if (vo == void_function_argument_origin_rng)
1315  o << "void function argument '_rng' suffixed";
1316  else
1317  o << "UNKNOWN ORIGIN=" << vo;
1318  }
1319 
1320 
1323  : base_type_(base_type) {
1324  }
1325  base_var_decl::base_var_decl(const std::string& name,
1326  const std::vector<expression>& dims,
1327  const base_expr_type& base_type)
1328  : name_(name), dims_(dims), base_type_(base_type) { }
1329  base_var_decl::base_var_decl(const std::string& name,
1330  const std::vector<expression>& dims,
1331  const base_expr_type& base_type,
1332  const expression& def)
1333  : name_(name), dims_(dims), base_type_(base_type), def_(def) { }
1334 
1335  bool variable_map::exists(const std::string& name) const {
1336  return map_.find(name) != map_.end();
1337  }
1338 
1339  base_var_decl variable_map::get(const std::string& name) const {
1340  if (!exists(name))
1341  throw std::invalid_argument("variable does not exist");
1342  return map_.find(name)->second.first;
1343  }
1344 
1345  base_expr_type variable_map::get_base_type(const std::string& name) const {
1346  return get(name).base_type_;
1347  }
1348 
1349  size_t variable_map::get_num_dims(const std::string& name) const {
1350  return get(name).dims_.size();
1351  }
1352 
1353  var_origin variable_map::get_origin(const std::string& name) const {
1354  if (!exists(name))
1355  throw std::invalid_argument("variable does not exist");
1356  return map_.find(name)->second.second;
1357  }
1358 
1359  void variable_map::add(const std::string& name,
1360  const base_var_decl& base_decl,
1361  const var_origin& vo) {
1362  map_[name] = range_t(base_decl, vo);
1363  }
1364 
1365  void variable_map::remove(const std::string& name) {
1366  map_.erase(name);
1367  }
1368 
1370  : base_var_decl(INT_T)
1371  { }
1373  std::string const& name,
1374  std::vector<expression> const& dims,
1375  expression const& def)
1376  : base_var_decl(name, dims, INT_T, def),
1377  range_(range)
1378  { }
1379 
1380 
1383  { }
1385  std::string const& name,
1386  std::vector<expression> const& dims,
1387  expression const& def)
1388  : base_var_decl(name, dims, DOUBLE_T, def),
1389  range_(range)
1390  { }
1391 
1394  { }
1395 
1397  std::string const& name,
1398  std::vector<expression> const& dims)
1399  : base_var_decl(name, dims, VECTOR_T),
1400  K_(K)
1401  { }
1402 
1405  { }
1406 
1408  std::string const& name,
1409  std::vector<expression> const& dims)
1410  : base_var_decl(name, dims, VECTOR_T),
1411  K_(K)
1412  { }
1413 
1416  { }
1417 
1419  std::string const& name,
1420  std::vector<expression> const& dims)
1421  : base_var_decl(name, dims, VECTOR_T),
1422  K_(K) {
1423  }
1424 
1427  { }
1428 
1430  std::string const& name,
1431  std::vector<expression> const& dims)
1432  : base_var_decl(name, dims, VECTOR_T),
1433  K_(K) {
1434  }
1435 
1437 
1439  expression const& M,
1440  std::string const& name,
1441  std::vector<expression> const& dims,
1442  expression const& def)
1443  : base_var_decl(name, dims, VECTOR_T, def),
1444  range_(range),
1445  M_(M) {
1446  }
1447 
1450  expression const& N,
1451  std::string const& name,
1452  std::vector<expression> const& dims,
1453  expression const& def)
1454  : base_var_decl(name, dims, ROW_VECTOR_T, def),
1455  range_(range),
1456  N_(N) {
1457  }
1458 
1461  expression const& M,
1462  expression const& N,
1463  std::string const& name,
1464  std::vector<expression> const& dims,
1465  expression const& def)
1466  : base_var_decl(name, dims, MATRIX_T, def),
1467  range_(range),
1468  M_(M),
1469  N_(N) {
1470  }
1471 
1473  : base_var_decl(MATRIX_T) {
1474  }
1476  expression const& N,
1477  std::string const& name,
1478  std::vector<expression> const& dims)
1479  : base_var_decl(name, dims, MATRIX_T),
1480  M_(M),
1481  N_(N) {
1482  }
1483 
1485  : base_var_decl(MATRIX_T) {
1486  }
1488  std::string const& name,
1489  std::vector<expression> const& dims)
1490  : base_var_decl(name, dims, MATRIX_T),
1491  K_(K) {
1492  }
1493 
1495  }
1497  std::string const& name,
1498  std::vector<expression> const& dims)
1499  : base_var_decl(name, dims, MATRIX_T),
1500  K_(K) {
1501  }
1502 
1505  std::string const& name,
1506  std::vector<expression> const& dims)
1507  : base_var_decl(name, dims, MATRIX_T),
1508  K_(K) {
1509  }
1510 
1511 
1513  std::string name_vis::operator()(const nil& /* x */) const {
1514  return ""; // fail if arises
1515  }
1516  std::string name_vis::operator()(const int_var_decl& x) const {
1517  return x.name_;
1518  }
1519  std::string name_vis::operator()(const double_var_decl& x) const {
1520  return x.name_;
1521  }
1522  std::string name_vis::operator()(const vector_var_decl& x) const {
1523  return x.name_;
1524  }
1525  std::string name_vis::operator()(const row_vector_var_decl& x) const {
1526  return x.name_;
1527  }
1528  std::string name_vis::operator()(const matrix_var_decl& x) const {
1529  return x.name_;
1530  }
1531  std::string name_vis::operator()(const unit_vector_var_decl& x) const {
1532  return x.name_;
1533  }
1534  std::string name_vis::operator()(const simplex_var_decl& x) const {
1535  return x.name_;
1536  }
1537  std::string name_vis::operator()(const ordered_var_decl& x) const {
1538  return x.name_;
1539  }
1540  std::string name_vis::operator()(const positive_ordered_var_decl& x) const {
1541  return x.name_;
1542  }
1543  std::string name_vis::operator()(const cholesky_factor_var_decl& x) const {
1544  return x.name_;
1545  }
1546  std::string name_vis::operator()(const cholesky_corr_var_decl& x) const {
1547  return x.name_;
1548  }
1549  std::string name_vis::operator()(const cov_matrix_var_decl& x) const {
1550  return x.name_;
1551  }
1552  std::string name_vis::operator()(const corr_matrix_var_decl& x) const {
1553  return x.name_;
1554  }
1555 
1558  const {
1559  return base_var_decl(); // should not be called
1560  }
1562  const {
1563  return x.base_type_;
1564  }
1566  const {
1567  return x.base_type_;
1568  }
1570  const {
1571  return x.base_type_;
1572  }
1574  const row_vector_var_decl& x) const {
1575  return x.base_type_;
1576  }
1578  const {
1579  return x.base_type_;
1580  }
1582  const unit_vector_var_decl& x) const {
1583  return x.base_type_;
1584  }
1586  const simplex_var_decl& x) const {
1587  return x.base_type_;
1588  }
1590  const ordered_var_decl& x) const {
1591  return x.base_type_;
1592  }
1594  const positive_ordered_var_decl& x) const {
1595  return x.base_type_;
1596  }
1598  const cholesky_factor_var_decl& x) const {
1599  return x.base_type_;
1600  }
1602  const cholesky_corr_var_decl& x) const {
1603  return x.base_type_;
1604  }
1606  const cov_matrix_var_decl& x) const {
1607  return x.base_type_;
1608  }
1610  const corr_matrix_var_decl& x) const {
1611  return x.base_type_;
1612  }
1613 
1615  std::vector<expression> var_decl_dims_vis::operator()(const nil& /* x */)
1616  const {
1617  return std::vector<expression>(); // should not be called
1618  }
1619  std::vector<expression> var_decl_dims_vis::operator()(const int_var_decl& x)
1620  const {
1621  return x.dims_;
1622  }
1623  std::vector<expression> var_decl_dims_vis::operator()(
1624  const double_var_decl& x)
1625  const {
1626  return x.dims_;
1627  }
1628  std::vector<expression> var_decl_dims_vis::operator()(
1629  const vector_var_decl& x)
1630  const {
1631  return x.dims_;
1632  }
1633  std::vector<expression> var_decl_dims_vis::operator()(
1634  const row_vector_var_decl& x) const {
1635  return x.dims_;
1636  }
1637  std::vector<expression> var_decl_dims_vis::operator()(
1638  const matrix_var_decl& x)
1639  const {
1640  return x.dims_;
1641  }
1642  std::vector<expression> var_decl_dims_vis::operator()(
1643  const unit_vector_var_decl& x) const {
1644  return x.dims_;
1645  }
1646  std::vector<expression> var_decl_dims_vis::operator()(
1647  const simplex_var_decl& x) const {
1648  return x.dims_;
1649  }
1650  std::vector<expression> var_decl_dims_vis::operator()(
1651  const ordered_var_decl& x) const {
1652  return x.dims_;
1653  }
1654  std::vector<expression> var_decl_dims_vis::operator()(
1655  const positive_ordered_var_decl& x) const {
1656  return x.dims_;
1657  }
1658  std::vector<expression> var_decl_dims_vis::operator()(
1659  const cholesky_factor_var_decl& x) const {
1660  return x.dims_;
1661  }
1662  std::vector<expression> var_decl_dims_vis::operator()(
1663  const cholesky_corr_var_decl& x) const {
1664  return x.dims_;
1665  }
1666  std::vector<expression> var_decl_dims_vis::operator()(
1667  const cov_matrix_var_decl& x) const {
1668  return x.dims_;
1669  }
1670  std::vector<expression> var_decl_dims_vis::operator()(
1671  const corr_matrix_var_decl& x) const {
1672  return x.dims_;
1673  }
1674 
1677  const {
1678  return false; // should not be called
1679  }
1681  const {
1682  return !is_nil(x.def_);
1683  }
1685  const {
1686  return !is_nil(x.def_);
1687  }
1689  const {
1690  return !is_nil(x.def_);
1691  }
1693  const row_vector_var_decl& x) const {
1694  return !is_nil(x.def_);
1695  }
1697  const {
1698  return !is_nil(x.def_);
1699  }
1701  const unit_vector_var_decl& x) const {
1702  return !is_nil(x.def_);
1703  }
1705  const simplex_var_decl& x) const {
1706  return !is_nil(x.def_);
1707  }
1709  const ordered_var_decl& x) const {
1710  return !is_nil(x.def_);
1711  }
1713  const positive_ordered_var_decl& x) const {
1714  return !is_nil(x.def_);
1715  }
1717  const cholesky_factor_var_decl& x) const {
1718  return !is_nil(x.def_);
1719  }
1721  const cholesky_corr_var_decl& x) const {
1722  return !is_nil(x.def_);
1723  }
1725  const cov_matrix_var_decl& x) const {
1726  return !is_nil(x.def_);
1727  }
1729  const corr_matrix_var_decl& x) const {
1730  return !is_nil(x.def_);
1731  }
1732 
1733 
1736  const {
1737  return expression(); // should not be called
1738  }
1740  const {
1741  return x.def_;
1742  }
1744  const {
1745  return x.def_;
1746  }
1748  const {
1749  return x.def_;
1750  }
1752  const row_vector_var_decl& x) const {
1753  return x.def_;
1754  }
1756  const {
1757  return x.def_;
1758  }
1760  const unit_vector_var_decl& x) const {
1761  return x.def_;
1762  }
1764  const simplex_var_decl& x) const {
1765  return x.def_;
1766  }
1768  const ordered_var_decl& x) const {
1769  return x.def_;
1770  }
1772  const positive_ordered_var_decl& x) const {
1773  return x.def_;
1774  }
1776  const cholesky_factor_var_decl& x) const {
1777  return x.def_;
1778  }
1780  const cholesky_corr_var_decl& x) const {
1781  return x.def_;
1782  }
1784  const cov_matrix_var_decl& x) const {
1785  return x.def_;
1786  }
1788  const corr_matrix_var_decl& x) const {
1789  return x.def_;
1790  }
1791 
1792 
1793  // can't template out in .cpp file
1794 
1795  var_decl::var_decl(const var_decl_t& decl) : decl_(decl) { }
1797  var_decl::var_decl(const nil& decl) : decl_(decl) { }
1798  var_decl::var_decl(const int_var_decl& decl) : decl_(decl) { }
1799  var_decl::var_decl(const double_var_decl& decl) : decl_(decl) { }
1800  var_decl::var_decl(const vector_var_decl& decl) : decl_(decl) { }
1802  var_decl::var_decl(const matrix_var_decl& decl) : decl_(decl) { }
1804  var_decl::var_decl(const simplex_var_decl& decl) : decl_(decl) { }
1805  var_decl::var_decl(const ordered_var_decl& decl) : decl_(decl) { }
1811 
1812  std::string var_decl::name() const {
1813  return boost::apply_visitor(name_vis(), decl_);
1814  }
1815 
1817  return boost::apply_visitor(var_decl_base_type_vis(), decl_);
1818  }
1819 
1820  std::vector<expression> var_decl::dims() const {
1821  return boost::apply_visitor(var_decl_dims_vis(), decl_);
1822  }
1823 
1824  bool var_decl::has_def() const {
1825  return boost::apply_visitor(var_decl_has_def_vis(), decl_);
1826  }
1827 
1829  return boost::apply_visitor(var_decl_def_vis(), decl_);
1830  }
1831 
1832  statement::statement() : statement_(nil()) { }
1833 
1835  statement::statement(const nil& st) : statement_(st) { }
1840  : statement_(st) {
1841  }
1847  : statement_(st) { }
1853 
1854 
1855  bool is_no_op_statement_vis::operator()(const nil& st) const {
1856  return false;
1857  }
1859  return false;
1860  }
1862  return false;
1863  }
1865  return false;
1866  }
1868  const increment_log_prob_statement& t) const {
1869  return false;
1870  }
1872  return false;
1873  }
1875  return false;
1876  }
1878  return false;
1879  }
1881  const conditional_statement& st) const {
1882  return false;
1883  }
1885  return false;
1886  }
1887  bool
1889  const {
1890  return false;
1891  }
1893  return false;
1894  }
1896  return false;
1897  }
1899  return true;
1900  }
1902  return false;
1903  }
1904 
1907  return boost::apply_visitor(vis, statement_);
1908  }
1909 
1911  }
1913  const expression& log_prob)
1914  : log_prob_(log_prob) {
1915  }
1916 
1918  }
1920  range& range,
1921  statement& stmt)
1922  : variable_(variable),
1923  range_(range),
1924  statement_(stmt) {
1925  }
1926 
1929  const statement& body)
1930  : condition_(condition),
1931  body_(body) {
1932  }
1933 
1936  : generate_(s) { }
1937 
1939  }
1941  ::conditional_statement(const std::vector<expression>& conditions,
1942  const std::vector<statement>& bodies)
1943  : conditions_(conditions),
1944  bodies_(bodies) {
1945  }
1946 
1949  : return_value_(expr) {
1950  }
1951 
1953 
1954  print_statement::print_statement(const std::vector<printable>& printables)
1955  : printables_(printables) {
1956  }
1957 
1959 
1960  reject_statement::reject_statement(const std::vector<printable>& printables)
1961  : printables_(printables) {
1962  }
1963 
1965  program::program(const std::vector<function_decl_def>& function_decl_defs,
1966  const std::vector<var_decl>& data_decl,
1967  const std::pair<std::vector<var_decl>,
1968  std::vector<statement> >& derived_data_decl,
1969  const std::vector<var_decl>& parameter_decl,
1970  const std::pair<std::vector<var_decl>,
1971  std::vector<statement> >& derived_decl,
1972  const statement& st,
1973  const std::pair<std::vector<var_decl>,
1974  std::vector<statement> >& generated_decl)
1975  : function_decl_defs_(function_decl_defs),
1976  data_decl_(data_decl),
1977  derived_data_decl_(derived_data_decl),
1978  parameter_decl_(parameter_decl),
1979  derived_decl_(derived_decl),
1980  statement_(st),
1981  generated_decl_(generated_decl) {
1982  }
1983 
1985  }
1987  distribution& dist)
1988  : expr_(e),
1989  dist_(dist) {
1990  }
1991  bool sample::is_ill_formed() const {
1993  || (truncation_.has_low()
1995  || (truncation_.has_high()
1996  && expr_.expression_type()
1998  }
1999  bool sample::is_discrete() const {
2000  return is_discrete_;
2001  }
2002 
2004  }
2006  expression& expr)
2007  : var_dims_(var_dims),
2008  expr_(expr) {
2009  }
2010 
2012  : var_name_(e.name_) {
2013  }
2014 
2015  bool var_occurs_vis::operator()(const nil& st) const {
2016  return false;
2017  }
2019  return false;
2020  }
2022  return false;
2023  }
2025  return false; // TODO(carpenter): update for array_literal
2026  }
2027  bool var_occurs_vis::operator()(const variable& e) const {
2028  return var_name_ == e.name_;
2029  }
2030  bool var_occurs_vis::operator()(const fun& e) const {
2031  for (size_t i = 0; i < e.args_.size(); ++i)
2032  if (boost::apply_visitor(*this, e.args_[i].expr_))
2033  return true;
2034  return false;
2035  }
2037  return false; // no refs persist out of integrate_ode() call
2038  }
2040  return false; // no refs persist out of integrate_ode_control() call
2041  }
2042  bool var_occurs_vis::operator()(const index_op& e) const {
2043  // refs only persist out of expression, not indexes
2044  return boost::apply_visitor(*this, e.expr_.expr_);
2045  }
2047  return boost::apply_visitor(*this, e.expr_.expr_);
2048  }
2050  return boost::apply_visitor(*this, e.cond_.expr_)
2051  || boost::apply_visitor(*this, e.true_val_.expr_)
2052  || boost::apply_visitor(*this, e.false_val_.expr_);
2053  }
2054  bool var_occurs_vis::operator()(const binary_op& e) const {
2055  return boost::apply_visitor(*this, e.left.expr_)
2056  || boost::apply_visitor(*this, e.right.expr_);
2057  }
2058  bool var_occurs_vis::operator()(const unary_op& e) const {
2059  return boost::apply_visitor(*this, e.subject.expr_);
2060  }
2061 
2063  assgn::assgn(const variable& lhs_var, const std::vector<idx>& idxs,
2064  const expression& rhs)
2065  : lhs_var_(lhs_var), idxs_(idxs), rhs_(rhs) { }
2066 
2068  var_occurs_vis vis(lhs_var_);
2069  return boost::apply_visitor(vis, rhs_.expr_);
2070  }
2071 
2083  const std::vector<idx>& idxs) {
2084  expr_type e_type = e.expression_type();
2085 
2086  base_expr_type base_type = e_type.base_type_;
2087  size_t base_dims = e_type.num_dims_;
2088  size_t unindexed_dims = base_dims;
2089  size_t out_dims = 0U;
2090  size_t i = 0;
2091  for ( ; unindexed_dims > 0 && i < idxs.size(); ++i, --unindexed_dims)
2092  if (is_multi_index(idxs[i]))
2093  ++out_dims;
2094  if (idxs.size() - i == 0) {
2095  return expr_type(base_type, out_dims + unindexed_dims);
2096  } else if (idxs.size() - i == 1) {
2097  if (base_type == MATRIX_T) {
2098  if (is_multi_index(idxs[i]))
2099  return expr_type(MATRIX_T, out_dims);
2100  else
2101  return expr_type(ROW_VECTOR_T, out_dims);
2102  } else if (base_type == VECTOR_T) {
2103  if (is_multi_index(idxs[i]))
2104  return expr_type(VECTOR_T, out_dims);
2105  else
2106  return expr_type(DOUBLE_T, out_dims);
2107  } else if (base_type == ROW_VECTOR_T) {
2108  if (is_multi_index(idxs[i]))
2109  return expr_type(ROW_VECTOR_T, out_dims);
2110  else
2111  return expr_type(DOUBLE_T, out_dims);
2112  } else {
2113  return expr_type(ILL_FORMED_T, 0U);
2114  }
2115  } else if (idxs.size() - i == 2) {
2116  if (base_type == MATRIX_T) {
2117  if (is_multi_index(idxs[i]) && is_multi_index(idxs[i + 1]))
2118  return expr_type(MATRIX_T, out_dims);
2119  else if (is_multi_index(idxs[i]))
2120  return expr_type(VECTOR_T, out_dims);
2121  else if (is_multi_index(idxs[i + 1]))
2122  return expr_type(ROW_VECTOR_T, out_dims);
2123  else
2124  return expr_type(DOUBLE_T, out_dims);
2125  } else {
2126  return expr_type(ILL_FORMED_T, 0U);
2127  }
2128  } else {
2129  return expr_type(ILL_FORMED_T, 0U);
2130  }
2131  }
2132 
2133 
2135  expr_ = binary_op(expr_, "+", rhs);
2136  return *this;
2137  }
2138 
2140  expr_ = binary_op(expr_, "-", rhs);
2141  return *this;
2142  }
2143 
2145  expr_ = binary_op(expr_, "*", rhs);
2146  return *this;
2147  }
2148 
2150  expr_ = binary_op(expr_, "/", rhs);
2151  return *this;
2152  }
2153 
2154  bool has_rng_suffix(const std::string& s) {
2155  int n = s.size();
2156  return n > 4
2157  && s[n-1] == 'g'
2158  && s[n-2] == 'n'
2159  && s[n-3] == 'r'
2160  && s[n-4] == '_';
2161  }
2162 
2163  bool has_lp_suffix(const std::string& s) {
2164  int n = s.size();
2165  return n > 3
2166  && s[n-1] == 'p'
2167  && s[n-2] == 'l'
2168  && s[n-3] == '_';
2169  }
2170 
2171  bool is_user_defined(const std::string& name,
2172  const std::vector<expression>& args) {
2173  std::vector<expr_type> arg_types;
2174  for (size_t i = 0; i < args.size(); ++i)
2175  arg_types.push_back(args[i].expression_type());
2177  int matches
2179  .get_signature_matches(name, arg_types, sig);
2180  if (matches != 1)
2181  return false; // reall shouldn't come up; throw instead?
2182  std::pair<std::string, function_signature_t>
2183  name_sig(name, sig);
2185  }
2186 
2187  bool is_user_defined_prob_function(const std::string& name,
2188  const expression& variate,
2189  const std::vector<expression>& params) {
2190  std::vector<expression> variate_params;
2191  variate_params.push_back(variate);
2192  for (size_t i = 0; i < params.size(); ++i)
2193  variate_params.push_back(params[i]);
2194  return is_user_defined(name, variate_params);
2195  }
2196 
2197  bool is_user_defined(const fun& fx) {
2198  return is_user_defined(fx.name_, fx.args_);
2199  }
2200 
2201  bool is_assignable(const expr_type& l_type,
2202  const expr_type& r_type,
2203  const std::string& failure_message,
2204  std::ostream& error_msgs) {
2205  bool assignable = true;
2206  if (l_type.num_dims_ != r_type.num_dims_) {
2207  assignable = false;
2208  error_msgs << "Mismatched array dimensions.";
2209  }
2210  if (l_type.base_type_ != r_type.base_type_
2211  && (!(l_type.base_type_ == DOUBLE_T
2212  && r_type.base_type_ == INT_T))) {
2213  assignable = false;
2214  error_msgs << "Base type mismatch. ";
2215  }
2216  if (!assignable)
2217  error_msgs << failure_message
2218  << std::endl
2219  << " LHS type = " << l_type
2220  << "; RHS type = " << r_type
2221  << std::endl;
2222  return assignable;
2223  }
2224 
2225  bool ends_with(const std::string& suffix,
2226  const std::string& s) {
2227  size_t idx = s.rfind(suffix);
2228  return idx != std::string::npos
2229  && idx == (s.size() - suffix.size());
2230  }
2231 
2232  std::string get_cdf(const std::string& dist_name) {
2233  if (function_signatures::instance().has_key(dist_name + "_cdf_log"))
2234  return dist_name + "_cdf_log";
2235  else if (function_signatures::instance().has_key(dist_name + "_lcdf"))
2236  return dist_name + "_lcdf";
2237  else
2238  return dist_name;
2239  }
2240 
2241  std::string get_ccdf(const std::string& dist_name) {
2242  if (function_signatures::instance().has_key(dist_name + "_ccdf_log"))
2243  return dist_name + "_ccdf_log";
2244  else if (function_signatures::instance().has_key(dist_name + "_lccdf"))
2245  return dist_name + "_lccdf";
2246  else
2247  return dist_name;
2248  }
2249 
2250  std::string get_prob_fun(const std::string& dist_name) {
2251  if (function_signatures::instance().has_key(dist_name + "_log"))
2252  return dist_name + "_log";
2253  else if (function_signatures::instance().has_key(dist_name + "_lpdf"))
2254  return dist_name + "_lpdf";
2255  else if (function_signatures::instance().has_key(dist_name + "_lpmf"))
2256  return dist_name + "_lpmf";
2257  else
2258  return dist_name;
2259  }
2260 
2261  bool has_prob_fun_suffix(const std::string& fname) {
2262  return ends_with("_lpdf", fname) || ends_with("_lpmf", fname)
2263  || ends_with("_log", fname);
2264  }
2265 
2266  std::string strip_prob_fun_suffix(const std::string& fname) {
2267  if (ends_with("_lpdf", fname))
2268  return fname.substr(0, fname.size() - 5);
2269  else if (ends_with("_lpmf", fname))
2270  return fname.substr(0, fname.size() - 5);
2271  else if (ends_with("_log", fname))
2272  return fname.substr(0, fname.size() - 4);
2273  else
2274  return fname;
2275  }
2276 
2277  bool has_cdf_suffix(const std::string& fname) {
2278  return ends_with("_lcdf", fname) || ends_with("_cdf_log", fname);
2279  }
2280 
2281  std::string strip_cdf_suffix(const std::string& fname) {
2282  if (ends_with("_lcdf", fname))
2283  return fname.substr(0, fname.size() - 5);
2284  else if (ends_with("_cdf_log", fname))
2285  return fname.substr(0, fname.size() - 8);
2286  else
2287  return fname;
2288  }
2289 
2290  bool has_ccdf_suffix(const std::string& fname) {
2291  return ends_with("_lccdf", fname) || ends_with("_ccdf_log", fname);
2292  }
2293 
2294  std::string strip_ccdf_suffix(const std::string& fname) {
2295  if (ends_with("_lccdf", fname))
2296  return fname.substr(0, fname.size() - 6);
2297  else if (ends_with("_ccdf_log", fname))
2298  return fname.substr(0, fname.size() - 9);
2299  else
2300  return fname;
2301  }
2302 
2303  bool fun_name_exists(const std::string& name) {
2304  return function_signatures::instance().has_key(name);
2305  }
2306 
2307 
2308 
2309  }
2310 }
2311 #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:453
expression high_
Definition: ast.hpp:485
bool operator()(const uni_idx &i) const
Definition: ast_def.cpp:1252
var_occurs_vis(const variable &e)
Definition: ast_def.cpp:2011
bool is_primitive_int() const
Definition: ast_def.cpp:99
std::string strip_ccdf_suffix(const std::string &dist_fun)
Definition: ast_def.cpp:2294
bool is_ill_formed() const
Definition: ast_def.cpp:107
void add(const std::string &name, const expr_type &result_type, const std::vector< expr_type > &arg_types)
Definition: ast_def.cpp:201
expr_type type_
Definition: ast.hpp:443
expression_t expr_
Definition: ast.hpp:293
const int function_argument_origin
Definition: ast.hpp:85
bool is_no_op_statement() const
Definition: ast_def.cpp:1905
const variable_map & var_map_
Definition: ast.hpp:1143
const int ROW_VECTOR_T
Definition: ast.hpp:73
expr_type type_
Definition: ast.hpp:423
std::string integration_function_name_
Definition: ast.hpp:374
bool is_operator(const std::string &name)
Definition: ast_def.cpp:389
bool has_def() const
Definition: ast_def.cpp:1824
bool has_lp_suffix(const std::string &s)
Definition: ast_def.cpp:2163
std::string name_
Definition: ast.hpp:365
const int derived_origin
Definition: ast.hpp:83
std::vector< statement > statements_
Definition: ast.hpp:220
std::vector< expression > operator()(const nil &x) const
Definition: ast_def.cpp:1615
contains_var(const variable_map &var_map)
Definition: ast_def.cpp:799
bool is_fun_origin(const var_origin &vo)
Definition: ast_def.cpp:1280
std::vector< expression > dims_
Definition: ast.hpp:570
expression & operator-=(const expression &rhs)
Definition: ast_def.cpp:2139
std::string strip_cdf_suffix(const std::string &dist_fun)
Definition: ast_def.cpp:2281
expression ub_
Definition: ast.hpp:512
range truncation_
Definition: ast.hpp:1068
bool has_low() const
Definition: ast_def.cpp:1215
const int parameter_origin
Definition: ast.hpp:81
int total_dims() const
Definition: ast_def.cpp:780
bool has_cdf_suffix(const std::string &name)
Definition: ast_def.cpp:2277
const int function_argument_origin_rng
Definition: ast.hpp:87
bool operator()(const nil &st) const
Definition: ast_def.cpp:599
Probability, optimization and sampling library.
size_t total_dims(const std::vector< std::vector< expression > > &dimss)
Definition: ast_def.cpp:1126
bool operator<=(const expr_type &et) const
Definition: ast_def.cpp:80
std::string get_prob_fun(const std::string &dist_name)
Definition: ast_def.cpp:2250
static function_signatures & instance()
Definition: ast_def.cpp:150
size_t num_dims() const
Definition: ast_def.cpp:116
bool is_multi_index(const idx &idx)
Definition: ast_def.cpp:1271
bool is_unary_postfix_operator(const std::string &name)
Definition: ast_def.cpp:385
std::pair< base_var_decl, var_origin > range_t
Definition: ast.hpp:585
bool is_ill_formed() const
Definition: ast_def.cpp:1991
std::vector< expression > args_
Definition: ast.hpp:422
expr_type type_
Definition: ast.hpp:478
void add(const std::string &name, const base_var_decl &base_decl, const var_origin &vo)
Definition: ast_def.cpp:1359
bool has_prob_fun_suffix(const std::string &name)
Definition: ast_def.cpp:2261
std::string get_cdf(const std::string &dist_name)
Definition: ast_def.cpp:2232
expr_type operator()(const nil &e) const
Definition: ast_def.cpp:713
bool operator<(const expr_type &et) const
Definition: ast_def.cpp:75
std::vector< statement > bodies_
Definition: ast.hpp:967
const int DOUBLE_T
Definition: ast.hpp:71
bool is_assignable(const expr_type &l_type, const expr_type &r_type, const std::string &failure_message, std::ostream &error_msgs)
Definition: ast_def.cpp:2201
bool has_rng_suffix(const std::string &s)
Definition: ast_def.cpp:2154
Template specification of functions in std for Stan.
int num_promotions(const std::vector< expr_type > &call_args, const std::vector< expr_type > &sig_args)
Definition: ast_def.cpp:327
bool is_primitive_double() const
Definition: ast_def.cpp:103
std::vector< printable > printables_
Definition: ast.hpp:988
int base_expr_type
Definition: ast.hpp:65
int get_signature_matches(const std::string &name, const std::vector< expr_type > &args, function_signature_t &signature)
Definition: ast_def.cpp:346
const int transformed_parameter_origin
Definition: ast.hpp:82
expression expr_
Definition: ast.hpp:441
statement_t statement_
Definition: ast.hpp:878
void add_binary(const ::std::string &name)
Definition: ast_def.cpp:318
bool operator()(const nil &x) const
Definition: ast_def.cpp:984
bool has_high() const
Definition: ast_def.cpp:1218
expr_type expression_type() const
Definition: ast_def.cpp:760
bool operator()(const nil &e) const
Definition: ast_def.cpp:2015
void set_type(const base_expr_type &base_type, size_t num_dims)
Definition: ast_def.cpp:1069
expr_type type_
Definition: ast.hpp:366
contains_nonparam_var(const variable_map &var_map)
Definition: ast_def.cpp:894
expression true_val_
Definition: ast.hpp:453
std::string name_
Definition: ast.hpp:420
bool operator!=(const expr_type &et) const
Definition: ast_def.cpp:72
std::string get_ccdf(const std::string &dist_name)
Definition: ast_def.cpp:2241
expression operator()(const nil &x) const
Definition: ast_def.cpp:1735
const int function_argument_origin_lp
Definition: ast.hpp:86
boost::variant< boost::recursive_wrapper< nil >, boost::recursive_wrapper< int_var_decl >, boost::recursive_wrapper< double_var_decl >, boost::recursive_wrapper< vector_var_decl >, boost::recursive_wrapper< row_vector_var_decl >, boost::recursive_wrapper< matrix_var_decl >, boost::recursive_wrapper< simplex_var_decl >, boost::recursive_wrapper< unit_vector_var_decl >, boost::recursive_wrapper< ordered_var_decl >, boost::recursive_wrapper< positive_ordered_var_decl >, boost::recursive_wrapper< cholesky_factor_var_decl >, boost::recursive_wrapper< cholesky_corr_var_decl >, boost::recursive_wrapper< cov_matrix_var_decl >, boost::recursive_wrapper< corr_matrix_var_decl > > var_decl_t
Definition: ast.hpp:832
expr_type type_
Definition: ast.hpp:468
bool has_user_defined_key(const std::string &name) const
Definition: ast_def.cpp:528
bool is_unary_operator(const std::string &name)
Definition: ast_def.cpp:380
const variable_map & var_map_
Definition: ast.hpp:1180
void print_var_origin(std::ostream &o, const var_origin &vo)
Definition: ast_def.cpp:1289
void add_unary(const ::std::string &name)
Definition: ast_def.cpp:305
expression & operator*=(const expression &rhs)
Definition: ast_def.cpp:2144
bool is_nil(const expression &e)
Definition: ast_def.cpp:1008
var_decl_t decl_
Definition: ast.hpp:833
array_literal & operator=(const array_literal &al)
Definition: ast_def.cpp:1061
expression left
Definition: ast.hpp:466
void remove(const std::string &name)
Definition: ast_def.cpp:1365
base_var_decl base_decl() const
Definition: ast_def.cpp:1816
const int void_function_argument_origin_rng
Definition: ast.hpp:90
bool discrete_first_arg(const std::string &name) const
Definition: ast_def.cpp:183
void add_nullary(const ::std::string &name)
Definition: ast_def.cpp:302
bool fun_name_exists(const std::string &name)
Definition: ast_def.cpp:2303
const int model_name_origin
Definition: ast.hpp:78
bool is_discrete() const
Definition: ast_def.cpp:1999
bool operator>(const expr_type &et) const
Definition: ast_def.cpp:85
bool exists(const std::string &name) const
Definition: ast_def.cpp:1335
size_t get_num_dims(const std::string &name) const
Definition: ast_def.cpp:1349
bool ends_with(const std::string &suffix, const std::string &s)
Definition: ast_def.cpp:2225
bool returns_type(const expr_type &return_type, const statement &statement, std::ostream &error_msgs)
Definition: ast_def.cpp:695
expression & operator+=(const expression &rhs)
Definition: ast_def.cpp:2134
bool operator()(const nil &st) const
Definition: ast_def.cpp:1855
bool operator()(const nil &x) const
Definition: ast_def.cpp:1676
boost::variant< boost::recursive_wrapper< nil >, boost::recursive_wrapper< assignment >, boost::recursive_wrapper< assgn >, boost::recursive_wrapper< sample >, boost::recursive_wrapper< increment_log_prob_statement >, boost::recursive_wrapper< expression >, boost::recursive_wrapper< statements >, boost::recursive_wrapper< for_statement >, boost::recursive_wrapper< conditional_statement >, boost::recursive_wrapper< while_statement >, boost::recursive_wrapper< break_continue_statement >, boost::recursive_wrapper< print_statement >, boost::recursive_wrapper< reject_statement >, boost::recursive_wrapper< return_statement >, boost::recursive_wrapper< no_op_statement > > statement_t
Definition: ast.hpp:877
bool is_user_defined_prob_function(const std::string &name, const expression &variate, const std::vector< expression > &params)
Definition: ast_def.cpp:2187
bool is_defined(const std::string &name, const function_signature_t &sig)
Definition: ast_def.cpp:172
bool lhs_var_occurs_on_rhs() const
Definition: ast_def.cpp:2067
void add_quaternary(const ::std::string &name)
Definition: ast_def.cpp:324
base_expr_type type() const
Definition: ast_def.cpp:113
const int VOID_T
Definition: ast.hpp:69
var_origin get_origin(const std::string &name) const
Definition: ast_def.cpp:1353
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:2082
bool is_user_defined(const std::pair< std::string, function_signature_t > &name_sig)
Definition: ast_def.cpp:166
std::string name_
Definition: ast.hpp:569
int var_origin
Definition: ast.hpp:77
std::string name() const
Definition: ast_def.cpp:1812
expr_type promote_primitive(const expr_type &et)
Definition: ast_def.cpp:132
const int void_function_argument_origin
Definition: ast.hpp:88
base_expr_type base_type_
Definition: ast.hpp:571
bool operator>=(const expr_type &et) const
Definition: ast_def.cpp:90
void print_signature(const std::string &name, const std::vector< expr_type > &arg_types, bool sampling_error_style, std::ostream &msgs)
Definition: ast_def.cpp:418
std::ostream & operator<<(std::ostream &o, const expr_type &et)
Definition: ast_def.cpp:121
std::pair< expr_type, std::vector< expr_type > > function_signature_t
Definition: ast.hpp:131
boost::variant< boost::recursive_wrapper< std::string >, boost::recursive_wrapper< expression > > printable_t
Definition: ast.hpp:300
bool has_non_param_var(const expression &e, const variable_map &var_map)
Returns true if the specified expression contains a variable that is defined as a transformed paramet...
Definition: ast_def.cpp:978
expression rhs_
Definition: ast.hpp:1108
const int data_origin
Definition: ast.hpp:79
const int void_function_argument_origin_lp
Definition: ast.hpp:89
base_expr_type base_type_
Definition: ast.hpp:102
expression def() const
Definition: ast_def.cpp:1828
std::vector< idx > idxs_
Definition: ast.hpp:559
bool is_binary_operator(const std::string &name)
Definition: ast_def.cpp:368
printable_t printable_
Definition: ast.hpp:308
bool is_data_origin(const var_origin &vo)
Definition: ast_def.cpp:1276
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:1133
const int INT_T
Definition: ast.hpp:70
std::string name_
Definition: ast.hpp:1011
std::vector< expression > conditions_
Definition: ast.hpp:966
std::string system_function_name_
Definition: ast.hpp:375
statement statement_
Definition: ast.hpp:957
std::string operator()(const nil &x) const
Definition: ast_def.cpp:1513
variable lhs_var_
Definition: ast.hpp:1106
bool has_var(const expression &e, const variable_map &var_map)
Returns true if the specified expression contains a variable that is defined as a parameter...
Definition: ast_def.cpp:888
bool is_linear_function(const std::string &name)
Definition: ast_def.cpp:858
int_literal & operator=(const int_literal &il)
Definition: ast_def.cpp:1032
expression expr_
Definition: ast.hpp:1066
const std::string var_name_
Definition: ast.hpp:1088
bool is_primitive() const
Definition: ast_def.cpp:95
base_var_decl operator()(const nil &x) const
Definition: ast_def.cpp:1557
bool has_ccdf_suffix(const std::string &name)
Definition: ast_def.cpp:2290
base_var_decl base_variable_declaration()
Definition: ast_def.cpp:569
expression low_
Definition: ast.hpp:484
void add_ternary(const ::std::string &name)
Definition: ast_def.cpp:321
std::set< std::string > key_set() const
Definition: ast_def.cpp:543
const int VECTOR_T
Definition: ast.hpp:72
double_literal & operator=(const double_literal &dl)
Definition: ast_def.cpp:1046
expression right
Definition: ast.hpp:467
std::string strip_prob_fun_suffix(const std::string &dist_fun)
Definition: ast_def.cpp:2266
bool has_key(const std::string &key) const
Definition: ast_def.cpp:557
idx_t idx_
Definition: ast.hpp:542
expression subject
Definition: ast.hpp:477
std::vector< expression > args_
Definition: ast.hpp:357
const int local_origin
Definition: ast.hpp:84
boost::variant< boost::recursive_wrapper< nil >, boost::recursive_wrapper< int_literal >, boost::recursive_wrapper< double_literal >, boost::recursive_wrapper< array_literal >, boost::recursive_wrapper< variable >, boost::recursive_wrapper< integrate_ode >, boost::recursive_wrapper< integrate_ode_control >, boost::recursive_wrapper< fun >, boost::recursive_wrapper< index_op >, boost::recursive_wrapper< index_op_sliced >, boost::recursive_wrapper< conditional_op >, boost::recursive_wrapper< binary_op >, boost::recursive_wrapper< unary_op > > expression_t
Definition: ast.hpp:264
std::ostream & write_base_expr_type(std::ostream &o, base_expr_type type)
Definition: ast_def.cpp:25
void set_user_defined(const std::pair< std::string, function_signature_t > &name_sig)
Definition: ast_def.cpp:158
bool is_void() const
Definition: ast_def.cpp:110
bool is_user_defined(const std::string &name, const std::vector< expression > &args)
Definition: ast_def.cpp:2171
void add_unary_vectorized(const ::std::string &name)
Definition: ast_def.cpp:308
bool operator==(const expr_type &et) const
Definition: ast_def.cpp:68
expr_type arg_type_
Definition: ast.hpp:1010
std::string op
Definition: ast.hpp:465
returns_type_vis(const expr_type &return_type, std::ostream &error_msgs)
Definition: ast_def.cpp:594
std::vector< std::vector< expression > > dimss_
Definition: ast.hpp:442
bool operator()(const nil &e) const
Definition: ast_def.cpp:802
std::string fun_name_to_operator(const std::string &name)
Definition: ast_def.cpp:395
Placeholder struct for boost::variant default ctors.
Definition: ast.hpp:18
const int ILL_FORMED_T
Definition: ast.hpp:75
bool operator()(const nil &e) const
Definition: ast_def.cpp:897
const int MATRIX_T
Definition: ast.hpp:74
expression false_val_
Definition: ast.hpp:454
base_expr_type get_base_type(const std::string &name) const
Definition: ast_def.cpp:1345
expression & operator/=(const expression &rhs)
Definition: ast_def.cpp:2149
unary_op(char op, expression const &subject)
Definition: ast_def.cpp:1201
const int transformed_data_origin
Definition: ast.hpp:80
base_var_decl get(const std::string &name) const
Definition: ast_def.cpp:1339
std::vector< expression > dims() const
Definition: ast_def.cpp:1820
std::ostream & error_msgs_
Definition: ast.hpp:924

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