Stan  2.10.0
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  void
158  std::pair<std::string,
160  name_sig) {
161  user_defined_set_.insert(name_sig);
162  }
163  bool
164  function_signatures::is_user_defined(const std::pair<std::string,
166  name_sig) {
167  return user_defined_set_.find(name_sig) != user_defined_set_.end();
168  }
169  bool
170  function_signatures::is_defined(const std::string& name,
171  const function_signature_t& sig) {
172  if (sigs_map_.find(name) == sigs_map_.end())
173  return false;
174  const std::vector<function_signature_t> sigs = sigs_map_[name];
175  for (size_t i = 0; i < sigs.size(); ++i)
176  if (sig.second == sigs[i].second)
177  return true;
178  return false;
179  }
180  void function_signatures::add(const std::string& name,
181  const expr_type& result_type,
182  const std::vector<expr_type>& arg_types) {
183  sigs_map_[name].push_back(function_signature_t(result_type, arg_types));
184  }
185  void function_signatures::add(const std::string& name,
186  const expr_type& result_type) {
187  std::vector<expr_type> arg_types;
188  add(name, result_type, arg_types);
189  }
190  void function_signatures::add(const std::string& name,
191  const expr_type& result_type,
192  const expr_type& arg_type) {
193  std::vector<expr_type> arg_types;
194  arg_types.push_back(arg_type);
195  add(name, result_type, arg_types);
196  }
197  void function_signatures::add(const std::string& name,
198  const expr_type& result_type,
199  const expr_type& arg_type1,
200  const expr_type& arg_type2) {
201  std::vector<expr_type> arg_types;
202  arg_types.push_back(arg_type1);
203  arg_types.push_back(arg_type2);
204  add(name, result_type, arg_types);
205  }
206  void function_signatures::add(const std::string& name,
207  const expr_type& result_type,
208  const expr_type& arg_type1,
209  const expr_type& arg_type2,
210  const expr_type& arg_type3) {
211  std::vector<expr_type> arg_types;
212  arg_types.push_back(arg_type1);
213  arg_types.push_back(arg_type2);
214  arg_types.push_back(arg_type3);
215  add(name, result_type, arg_types);
216  }
217  void function_signatures::add(const std::string& name,
218  const expr_type& result_type,
219  const expr_type& arg_type1,
220  const expr_type& arg_type2,
221  const expr_type& arg_type3,
222  const expr_type& arg_type4) {
223  std::vector<expr_type> arg_types;
224  arg_types.push_back(arg_type1);
225  arg_types.push_back(arg_type2);
226  arg_types.push_back(arg_type3);
227  arg_types.push_back(arg_type4);
228  add(name, result_type, arg_types);
229  }
230  void function_signatures::add(const std::string& name,
231  const expr_type& result_type,
232  const expr_type& arg_type1,
233  const expr_type& arg_type2,
234  const expr_type& arg_type3,
235  const expr_type& arg_type4,
236  const expr_type& arg_type5) {
237  std::vector<expr_type> arg_types;
238  arg_types.push_back(arg_type1);
239  arg_types.push_back(arg_type2);
240  arg_types.push_back(arg_type3);
241  arg_types.push_back(arg_type4);
242  arg_types.push_back(arg_type5);
243  add(name, result_type, arg_types);
244  }
245  void function_signatures::add(const std::string& name,
246  const expr_type& result_type,
247  const expr_type& arg_type1,
248  const expr_type& arg_type2,
249  const expr_type& arg_type3,
250  const expr_type& arg_type4,
251  const expr_type& arg_type5,
252  const expr_type& arg_type6) {
253  std::vector<expr_type> arg_types;
254  arg_types.push_back(arg_type1);
255  arg_types.push_back(arg_type2);
256  arg_types.push_back(arg_type3);
257  arg_types.push_back(arg_type4);
258  arg_types.push_back(arg_type5);
259  arg_types.push_back(arg_type6);
260  add(name, result_type, arg_types);
261  }
262  void function_signatures::add(const std::string& name,
263  const expr_type& result_type,
264  const expr_type& arg_type1,
265  const expr_type& arg_type2,
266  const expr_type& arg_type3,
267  const expr_type& arg_type4,
268  const expr_type& arg_type5,
269  const expr_type& arg_type6,
270  const expr_type& arg_type7) {
271  std::vector<expr_type> arg_types;
272  arg_types.push_back(arg_type1);
273  arg_types.push_back(arg_type2);
274  arg_types.push_back(arg_type3);
275  arg_types.push_back(arg_type4);
276  arg_types.push_back(arg_type5);
277  arg_types.push_back(arg_type6);
278  arg_types.push_back(arg_type7);
279  add(name, result_type, arg_types);
280  }
281  void function_signatures::add_nullary(const::std::string& name) {
282  add(name, DOUBLE_T);
283  }
284  void function_signatures::add_unary(const::std::string& name) {
285  add(name, DOUBLE_T, DOUBLE_T);
286  }
287  void function_signatures::add_binary(const::std::string& name) {
288  add(name, DOUBLE_T, DOUBLE_T, DOUBLE_T);
289  }
290  void function_signatures::add_ternary(const::std::string& name) {
292  }
293  void function_signatures::add_quaternary(const::std::string& name) {
295  }
297  const std::vector<expr_type>& call_args,
298  const std::vector<expr_type>& sig_args) {
299  if (call_args.size() != sig_args.size()) {
300  return -1; // failure
301  }
302  int num_promotions = 0;
303  for (size_t i = 0; i < call_args.size(); ++i) {
304  if (call_args[i] == sig_args[i]) {
305  continue;
306  } else if (call_args[i].is_primitive_int()
307  && sig_args[i].is_primitive_double()) {
308  ++num_promotions;
309  } else {
310  return -1; // failed match
311  }
312  }
313  return num_promotions;
314  }
315  int function_signatures::get_signature_matches(const std::string& name,
316  const std::vector<expr_type>& args,
317  function_signature_t& signature) {
318  if (!has_key(name)) return 0;
319  std::vector<function_signature_t> signatures = sigs_map_[name];
320  size_t min_promotions = std::numeric_limits<size_t>::max();
321  size_t num_matches = 0;
322  for (size_t i = 0; i < signatures.size(); ++i) {
323  signature = signatures[i];
324  int promotions = num_promotions(args, signature.second);
325  if (promotions < 0) continue; // no match
326  size_t promotions_ui = static_cast<size_t>(promotions);
327  if (promotions_ui < min_promotions) {
328  min_promotions = promotions_ui;
329  num_matches = 1;
330  } else if (promotions_ui == min_promotions) {
331  ++num_matches;
332  }
333  }
334  return num_matches;
335  }
336 
337  bool is_binary_operator(const std::string& name) {
338  return name == "add"
339  || name == "subtract"
340  || name == "multiply"
341  || name == "divide"
342  || name == "modulus"
343  || name == "mdivide_left"
344  || name == "mdivide_right"
345  || name == "elt_multiply"
346  || name == "elt_divide";
347  }
348 
349  bool is_unary_operator(const std::string& name) {
350  return name == "minus"
351  || name == "logical_negation";
352  }
353 
354  bool is_unary_postfix_operator(const std::string& name) {
355  return name == "transpose";
356  }
357 
358  bool is_operator(const std::string& name) {
359  return is_binary_operator(name)
360  || is_unary_operator(name)
361  || is_unary_postfix_operator(name);
362  }
363 
364  std::string fun_name_to_operator(const std::string& name) {
365  // binary infix (pow handled by parser)
366  if (name == "add") return "+";
367  if (name == "subtract") return "-";
368  if (name == "multiply") return "*";
369  if (name == "divide") return "/";
370  if (name == "modulus") return "%";
371  if (name == "mdivide_left") return "\\";
372  if (name == "mdivide_right") return "/";
373  if (name == "elt_multiply") return ".*";
374  if (name == "elt_divide") return "./";
375 
376  // unary prefix (+ handled by parser)
377  if (name == "minus") return "-";
378  if (name == "logical_negation") return "!";
379 
380  // unary suffix
381  if (name == "transpose") return "'";
382 
383  // none of the above
384  return "ERROR";
385  }
386 
387  void print_signature(const std::string& name,
388  const std::vector<expr_type>& arg_types,
389  bool sampling_error_style,
390  std::ostream& msgs) {
391  static size_t OP_SIZE = std::string("operator").size();
392  msgs << " ";
393  if (name.size() > OP_SIZE && name.substr(0, OP_SIZE) == "operator") {
394  std::string operator_name = name.substr(OP_SIZE);
395  if (arg_types.size() == 2) {
396  msgs << arg_types[0] << " " << operator_name << " " << arg_types[1]
397  << std::endl;
398  return;
399  } else if (arg_types.size() == 1) {
400  if (operator_name == "'") // exception for postfix
401  msgs << arg_types[0] << operator_name << std::endl;
402  else
403  msgs << operator_name << arg_types[0] << std::endl;
404  return;
405  } else {
406  // should not be reachable due to operator grammar
407  // continue on purpose to get more info to user if this happens
408  msgs << "Operators must have 1 or 2 arguments." << std::endl;
409  }
410  }
411  if (sampling_error_style && arg_types.size() > 0)
412  msgs << arg_types[0] << " ~ ";
413  msgs << name << "(";
414  size_t start = sampling_error_style ? 1 : 0;
415  for (size_t j = start; j < arg_types.size(); ++j) {
416  if (j > start) msgs << ", ";
417  msgs << arg_types[j];
418  }
419  msgs << ")" << std::endl;
420  }
421 
423  const std::vector<expr_type>& args,
424  std::ostream& error_msgs,
425  bool sampling_error_style) {
426  std::vector<function_signature_t> signatures = sigs_map_[name];
427  size_t match_index = 0;
428  size_t min_promotions = std::numeric_limits<size_t>::max();
429  size_t num_matches = 0;
430 
431  std::string display_name;
432  if (is_operator(name)) {
433  display_name = "operator" + fun_name_to_operator(name);
434  } else if (sampling_error_style && ends_with("_log", name)) {
435  display_name = name.substr(0, name.size() - 4);
436  } else if (sampling_error_style
437  && (ends_with("_lpdf", name) || ends_with("_lcdf", name))) {
438  display_name = name.substr(0, name.size() - 5);
439  } else {
440  display_name = name;
441  }
442 
443  for (size_t i = 0; i < signatures.size(); ++i) {
444  int promotions = num_promotions(args, signatures[i].second);
445  if (promotions < 0) continue; // no match
446  size_t promotions_ui = static_cast<size_t>(promotions);
447  if (promotions_ui < min_promotions) {
448  min_promotions = promotions_ui;
449  match_index = i;
450  num_matches = 1;
451  } else if (promotions_ui == min_promotions) {
452  ++num_matches;
453  }
454  }
455 
456  if (num_matches == 1)
457  return signatures[match_index].first;
458 
459  // all returns after here are for ill-typed input
460 
461  if (num_matches == 0) {
462  error_msgs << "No matches for: "
463  << std::endl << std::endl;
464  } else {
465  error_msgs << "Ambiguous: "
466  << num_matches << " matches with "
467  << min_promotions << " integer promotions for: "
468  << std::endl;
469  }
470  print_signature(display_name, args, sampling_error_style, error_msgs);
471 
472  if (signatures.size() == 0) {
473  error_msgs << std::endl
474  << (sampling_error_style ? "Distribution " : "Function ")
475  << display_name << " not found.";
476  if (sampling_error_style)
477  error_msgs << " Require function with _lpdf or _lpmf or _log suffix";
478  error_msgs << std::endl;
479  } else {
480  error_msgs << std::endl
481  << "Available argument signatures for "
482  << display_name << ":" << std::endl << std::endl;
483 
484  for (size_t i = 0; i < signatures.size(); ++i) {
485  print_signature(display_name, signatures[i].second,
486  sampling_error_style, error_msgs);
487  }
488  error_msgs << std::endl;
489  }
490 
491  return expr_type(); // ill-formed dummy
492  }
493 
494  function_signatures::function_signatures() {
495 #include <stan/lang/function_signatures.h> // NOLINT
496  }
497 
498  std::set<std::string>
500  using std::map;
501  using std::set;
502  using std::string;
503  using std::vector;
504  set<std::string> result;
505  for (map<string, vector<function_signature_t> >::const_iterator
506  it = sigs_map_.begin();
507  it != sigs_map_.end();
508  ++it)
509  result.insert(it->first);
510  return result;
511  }
512 
513  bool function_signatures::has_key(const std::string& key) const {
514  return sigs_map_.find(key) != sigs_map_.end();
515  }
516 
517  function_signatures* function_signatures::sigs_ = 0;
518 
520  arg_decl::arg_decl(const expr_type& arg_type,
521  const std::string& name)
522  : arg_type_(arg_type),
523  name_(name) {
524  }
526  std::vector<expression> dims;
527  for (size_t i = 0; i < arg_type_.num_dims_; ++i)
528  dims.push_back(expression(int_literal(0))); // dummy value 0
529  return base_var_decl(name_, dims, arg_type_.base_type_);
530  }
531 
534  const std::string& name,
535  const std::vector<arg_decl>& arg_decls,
536  const statement& body)
537 
538  : return_type_(return_type),
539  name_(name),
540  arg_decls_(arg_decls),
541  body_(body) {
542  }
543 
545  function_decl_defs::function_decl_defs(const std::vector<function_decl_def>&
546  decl_defs)
547  : decl_defs_(decl_defs) {
548  }
549 
551  std::ostream& error_msgs)
552  : return_type_(return_type),
553  error_msgs_(error_msgs) {
554  }
555  bool returns_type_vis::operator()(const nil& st) const {
556  error_msgs_ << "Expecting return, found nil statement."
557  << std::endl;
558  return false;
559  }
560  bool returns_type_vis::operator()(const assignment& st) const {
561  error_msgs_ << "Expecting return, found assignment statement."
562  << std::endl;
563  return false;
564  }
565  bool returns_type_vis::operator()(const assgn& st) const {
566  error_msgs_ << "Expecting return, found assignment statement."
567  << std::endl;
568  return false;
569  }
570  bool returns_type_vis::operator()(const sample& st) const {
571  error_msgs_ << "Expecting return, found sampling statement."
572  << std::endl;
573  return false;
574  }
576  increment_log_prob_statement& t) const {
577  error_msgs_ << "Expecting return, found increment_log_prob statement."
578  << std::endl;
579  return false;
580  }
581  bool returns_type_vis::operator()(const expression& st) const {
582  error_msgs_ << "Expecting return, found increment_log_prob statement."
583  << std::endl;
584  return false;
585  }
587  error_msgs_ << "Expecting return, found print statement."
588  << std::endl;
589  return false;
590  }
592  error_msgs_ << "Expecting return, found reject statement."
593  << std::endl;
594  return false;
595  }
597  error_msgs_ << "Expecting return, found no_op statement."
598  << std::endl;
599  return false;
600  }
601  // recursive cases
602  bool returns_type_vis::operator()(const statements& st) const {
603  // last statement in sequence must return type
604  if (st.statements_.size() == 0) {
605  error_msgs_ << ("Expecting return, found"
606  " statement sequence with empty body.")
607  << std::endl;
608  return false;
609  }
610  return returns_type(return_type_, st.statements_.back(), error_msgs_);
611  }
613  // body must end in appropriate return
615  }
617  // body must end in appropriate return
619  }
621  conditional_statement& st) const {
622  // all condition bodies must end in appropriate return
623  if (st.bodies_.size() != (st.conditions_.size() + 1)) {
624  error_msgs_ << ("Expecting return, found conditional"
625  " without final else.")
626  << std::endl;
627  return false;
628  }
629  for (size_t i = 0; i < st.bodies_.size(); ++i)
631  return false;
632  return true;
633  }
635  // return checked for type
636  return return_type_ == VOID_T
638  "Returned expression does not match return type",
639  error_msgs_);
640  }
641 
642  bool returns_type(const expr_type& return_type,
643  const statement& statement,
644  std::ostream& error_msgs) {
645  if (return_type == VOID_T)
646  return true;
647  returns_type_vis vis(return_type, error_msgs);
648  return boost::apply_visitor(vis, statement.statement_);
649  }
650 
651 
652 
654  statements::statements(const std::vector<var_decl>& local_decl,
655  const std::vector<statement>& stmts)
656  : local_decl_(local_decl),
657  statements_(stmts) {
658  }
659 
661  return expr_type();
662  }
664  return e.type_;
665  }
667  return e.type_;
668  }
670  return e.type_;
671  }
673  return e.type_;
674  }
676  return expr_type(DOUBLE_T, 2);
677  }
678  expr_type
680  return expr_type(DOUBLE_T, 2);
681  }
683  return e.type_;
684  }
686  return e.type_;
687  }
689  return e.type_;
690  }
692  return e.type_;
693  }
695  return e.type_;
696  }
698  return e.type_;
699  }
700 
702  : expr_(nil()) {
703  }
705  : expr_(e.expr_) {
706  }
709  return boost::apply_visitor(vis, expr_);
710  }
711 
712  expression::expression(const expression_t& expr) : expr_(expr) { }
713  expression::expression(const nil& expr) : expr_(expr) { }
714  expression::expression(const int_literal& expr) : expr_(expr) { }
715  expression::expression(const double_literal& expr) : expr_(expr) { }
716  expression::expression(const array_literal& expr) : expr_(expr) { }
717  expression::expression(const variable& expr) : expr_(expr) { }
718  expression::expression(const integrate_ode& expr) : expr_(expr) { }
719  expression::expression(const integrate_ode_control& expr) : expr_(expr) { }
720  expression::expression(const fun& expr) : expr_(expr) { }
721  expression::expression(const index_op& expr) : expr_(expr) { }
722  expression::expression(const index_op_sliced& expr) : expr_(expr) { }
723  expression::expression(const conditional_op& expr) : expr_(expr) { }
724  expression::expression(const binary_op& expr) : expr_(expr) { }
725  expression::expression(const unary_op& expr) : expr_(expr) { }
726 
728  int sum = expression_type().num_dims_;
729  if (expression_type().type() == VECTOR_T)
730  ++sum;
731  if (expression_type().type() == ROW_VECTOR_T)
732  ++sum;
733  if (expression_type().type() == MATRIX_T)
734  sum += 2;
735  return sum;
736  }
737 
738  printable::printable() : printable_("") { }
739  printable::printable(const expression& expr) : printable_(expr) { }
740  printable::printable(const std::string& msg) : printable_(msg) { }
742  : printable_(printable) { }
744  : printable_(printable.printable_) { }
745 
747  : var_map_(var_map) {
748  }
749  bool contains_var::operator()(const nil& e) const {
750  return false;
751  }
752  bool contains_var::operator()(const int_literal& e) const {
753  return false;
754  }
756  return false;
757  }
759  for (size_t i = 0; i < e.args_.size(); ++i)
760  if (boost::apply_visitor(*this, e.args_[i].expr_))
761  return true;
762  return false;
763  }
764  bool contains_var::operator()(const variable& e) const {
766  return vo == parameter_origin
768  || (vo == local_origin && e.type_.base_type_ != INT_T);
769  }
770  bool contains_var::operator()(const fun& e) const {
771  for (size_t i = 0; i < e.args_.size(); ++i)
772  if (boost::apply_visitor(*this, e.args_[i].expr_))
773  return true;
774  return false;
775  }
777  // only init state and params may contain vars
778  return boost::apply_visitor(*this, e.y0_.expr_)
779  || boost::apply_visitor(*this, e.theta_.expr_);
780  }
782  // only init state and params may contain vars
783  return boost::apply_visitor(*this, e.y0_.expr_)
784  || boost::apply_visitor(*this, e.theta_.expr_);
785  }
786  bool contains_var::operator()(const index_op& e) const {
787  return boost::apply_visitor(*this, e.expr_.expr_);
788  }
790  return boost::apply_visitor(*this, e.expr_.expr_);
791  }
793  return boost::apply_visitor(*this, e.cond_.expr_)
794  || boost::apply_visitor(*this, e.true_val_.expr_)
795  || boost::apply_visitor(*this, e.false_val_.expr_);
796  }
797  bool contains_var::operator()(const binary_op& e) const {
798  return boost::apply_visitor(*this, e.left.expr_)
799  || boost::apply_visitor(*this, e.right.expr_);
800  }
801  bool contains_var::operator()(const unary_op& e) const {
802  return boost::apply_visitor(*this, e.subject.expr_);
803  }
804 
805  bool is_linear_function(const std::string& name) {
806  return name == "add"
807  || name == "block"
808  || name == "append_col"
809  || name == "col"
810  || name == "cols"
811  || name == "diagonal"
812  || name == "head"
813  || name == "minus"
814  || name == "negative_infinity"
815  || name == "not_a_number"
816  || name == "append_row"
817  || name == "rep_matrix"
818  || name == "rep_row_vector"
819  || name == "rep_vector"
820  || name == "row"
821  || name == "rows"
822  || name == "positive_infinity"
823  || name == "segment"
824  || name == "subtract"
825  || name == "sum"
826  || name == "tail"
827  || name == "to_vector"
828  || name == "to_row_vector"
829  || name == "to_matrix"
830  || name == "to_array_1d"
831  || name == "to_array_2d"
832  || name == "transpose";
833  }
834 
835  bool has_var(const expression& e,
836  const variable_map& var_map) {
837  contains_var vis(var_map);
838  return boost::apply_visitor(vis, e.expr_);
839  }
840 
842  : var_map_(var_map) {
843  }
844  bool contains_nonparam_var::operator()(const nil& e) const {
845  return false;
846  }
848  return false;
849  }
851  return false;
852  }
854  for (size_t i = 0; i < e.args_.size(); ++i)
855  if (boost::apply_visitor(*this, e.args_[i].expr_))
856  return true;
857  return false;
858  }
861  return (vo == transformed_parameter_origin
862  || vo == local_origin);
863  }
865  // if any vars, return true because integration will be nonlinear
866  return boost::apply_visitor(*this, e.y0_.expr_)
867  || boost::apply_visitor(*this, e.theta_.expr_);
868  }
870  const {
871  // if any vars, return true because integration will be nonlinear
872  return boost::apply_visitor(*this, e.y0_.expr_)
873  || boost::apply_visitor(*this, e.theta_.expr_);
874  }
875  bool contains_nonparam_var::operator()(const fun& e) const {
876  // any function applied to non-linearly transformed var
877  for (size_t i = 0; i < e.args_.size(); ++i)
878  if (boost::apply_visitor(*this, e.args_[i].expr_))
879  return true;
880  // non-linear function applied to var
881  if (!is_linear_function(e.name_)) {
882  for (size_t i = 0; i < e.args_.size(); ++i)
883  if (has_var(e.args_[i], var_map_))
884  return true;
885  }
886  return false;
887  }
889  return boost::apply_visitor(*this, e.expr_.expr_);
890  }
892  return boost::apply_visitor(*this, e.expr_.expr_);
893  }
894 
899  return true;
900  return false;
901  }
902 
904  if (e.op == "||"
905  || e.op == "&&"
906  || e.op == "=="
907  || e.op == "!="
908  || e.op == "<"
909  || e.op == "<="
910  || e.op == ">"
911  || e.op == ">=")
912  return true;
915  return true;
916  if (e.op == "*" || e.op == "/")
917  return has_var(e.left, var_map_) && has_var(e.right, var_map_);
918  return false;
919  }
921  // only negation, which is linear, so recurse
923  }
924 
926  const variable_map& var_map) {
927  contains_nonparam_var vis(var_map);
928  return boost::apply_visitor(vis, e.expr_);
929  }
930 
931  bool is_nil_op::operator()(const nil& /*x*/) const { return true; }
932  bool is_nil_op::operator()(const int_literal& /*x*/) const { return false; }
933  bool is_nil_op::operator()(const double_literal& /* x */) const {
934  return false;
935  }
937  const { return false; }
938  bool is_nil_op::operator()(const variable& /* x */) const { return false; }
939  bool is_nil_op::operator()(const integrate_ode& /* x */) const {
940  return false;
941  }
942  bool is_nil_op::operator()(const integrate_ode_control& /* x */) const {
943  return false;
944  }
945  bool is_nil_op::operator()(const fun& /* x */) const { return false; }
946  bool is_nil_op::operator()(const index_op& /* x */) const { return false; }
947  bool is_nil_op::operator()(const index_op_sliced& /* x */) const {
948  return false;
949  }
950  bool is_nil_op::operator()(const conditional_op& /* x */) const {
951  return false; }
952  bool is_nil_op::operator()(const binary_op& /* x */) const { return false; }
953  bool is_nil_op::operator()(const unary_op& /* x */) const { return false; }
954 
955  bool is_nil(const expression& e) {
956  is_nil_op ino;
957  return boost::apply_visitor(ino, e.expr_);
958  }
959 
960  variable_dims::variable_dims() { } // req for FUSION_ADAPT
961  variable_dims::variable_dims(std::string const& name,
962  std::vector<expression> const& dims)
963  : name_(name),
964  dims_(dims) {
965  }
966 
967 
969  : type_(INT_T) {
970  }
972  : val_(val),
973  type_(INT_T) {
974  }
976  : val_(il.val_),
977  type_(il.type_) {
978  }
980  val_ = il.val_;
981  type_ = il.type_;
982  return *this;
983  }
984 
985 
987  : type_(DOUBLE_T, 0U) {
988  }
990  : val_(val),
991  type_(DOUBLE_T, 0U) {
992  }
994  val_ = dl.val_;
995  type_ = dl.type_;
996  return *this;
997  }
998 
999 
1001  : args_(),
1002  type_(DOUBLE_T, 1U) {
1003  }
1004  array_literal::array_literal(const std::vector<expression>& args)
1005  : args_(args),
1006  type_() { // ill-formed w/o help
1007  }
1009  args_ = al.args_;
1010  type_ = al.type_;
1011  return *this;
1012  }
1013 
1015  variable::variable(std::string name) : name_(name) { }
1016  void variable::set_type(const base_expr_type& base_type,
1017  size_t num_dims) {
1018  type_ = expr_type(base_type, num_dims);
1019  }
1020 
1022  integrate_ode::integrate_ode(const std::string& integration_function_name,
1023  const std::string& system_function_name,
1024  const expression& y0,
1025  const expression& t0,
1026  const expression& ts,
1027  const expression& theta,
1028  const expression& x,
1029  const expression& x_int)
1030  : integration_function_name_(integration_function_name),
1031  system_function_name_(system_function_name),
1032  y0_(y0),
1033  t0_(t0),
1034  ts_(ts),
1035  theta_(theta),
1036  x_(x),
1037  x_int_(x_int) {
1038  }
1039 
1042  const std::string& integration_function_name,
1043  const std::string& system_function_name,
1044  const expression& y0,
1045  const expression& t0,
1046  const expression& ts,
1047  const expression& theta,
1048  const expression& x,
1049  const expression& x_int,
1050  const expression& rel_tol,
1051  const expression& abs_tol,
1052  const expression& max_num_steps)
1053  : integration_function_name_(integration_function_name),
1054  system_function_name_(system_function_name),
1055  y0_(y0),
1056  t0_(t0),
1057  ts_(ts),
1058  theta_(theta),
1059  x_(x),
1060  x_int_(x_int),
1061  rel_tol_(rel_tol),
1062  abs_tol_(abs_tol),
1063  max_num_steps_(max_num_steps) {
1064  }
1065 
1066  fun::fun() { }
1067  fun::fun(std::string const& name,
1068  std::vector<expression> const& args)
1069  : name_(name),
1070  args_(args) {
1071  }
1072 
1073  size_t total_dims(const std::vector<std::vector<expression> >& dimss) {
1074  size_t total = 0U;
1075  for (size_t i = 0; i < dimss.size(); ++i)
1076  total += dimss[i].size();
1077  return total;
1078  }
1079 
1081  size_t num_expr_dims,
1082  size_t num_index_dims) {
1083  if (num_index_dims <= num_expr_dims)
1084  return expr_type(expr_base_type, num_expr_dims - num_index_dims);
1085  if (num_index_dims == (num_expr_dims + 1)) {
1086  if (expr_base_type == VECTOR_T || expr_base_type == ROW_VECTOR_T)
1087  return expr_type(DOUBLE_T, 0U);
1088  if (expr_base_type == MATRIX_T)
1089  return expr_type(ROW_VECTOR_T, 0U);
1090  }
1091  if (num_index_dims == (num_expr_dims + 2))
1092  if (expr_base_type == MATRIX_T)
1093  return expr_type(DOUBLE_T, 0U);
1094 
1095  // error condition, result expr_type has is_ill_formed() = true
1096  return expr_type();
1097  }
1098 
1100  size_t num_index_dims) {
1102  expr.expression_type().num_dims(),
1103  num_index_dims);
1104  }
1105 
1106 
1109  const std::vector<std::vector<expression> >& dimss)
1110  : expr_(expr),
1111  dimss_(dimss) {
1112  infer_type();
1113  }
1116  }
1117 
1120  const std::vector<idx>& idxs)
1121  : expr_(expr), idxs_(idxs), type_(indexed_type(expr_, idxs_)) { }
1124  }
1125 
1128  const expression& true_val,
1129  const expression& false_val)
1130  : cond_(cond),
1131  true_val_(true_val),
1132  false_val_(false_val),
1133  type_(promote_primitive(true_val.expression_type(),
1134  false_val.expression_type())) {
1135  }
1136 
1139  const std::string& op,
1140  const expression& right)
1141  : op(op),
1142  left(left),
1143  right(right),
1144  type_(promote_primitive(left.expression_type(),
1145  right.expression_type())) {
1146  }
1147 
1149  expression const& subject)
1150  : op(op),
1151  subject(subject),
1152  type_(promote_primitive(subject.expression_type())) {
1153  }
1154 
1155 
1158  expression const& high)
1159  : low_(low),
1160  high_(high) {
1161  }
1162  bool range::has_low() const {
1163  return !is_nil(low_.expr_);
1164  }
1165  bool range::has_high() const {
1166  return !is_nil(high_.expr_);
1167  }
1168 
1170  uni_idx::uni_idx(const expression& idx) : idx_(idx) { }
1171 
1173  multi_idx::multi_idx(const expression& idxs) : idxs_(idxs) { }
1174 
1176 
1178  lb_idx::lb_idx(const expression& lb) : lb_(lb) { }
1179 
1181  ub_idx::ub_idx(const expression& ub) : ub_(ub) { }
1182 
1185  : lb_(lb), ub_(ub) {
1186  }
1187 
1188  idx::idx() { }
1189 
1190  idx::idx(const uni_idx& i) : idx_(i) { }
1191  idx::idx(const multi_idx& i) : idx_(i) { }
1192  idx::idx(const omni_idx& i) : idx_(i) { }
1193  idx::idx(const lb_idx& i) : idx_(i) { }
1194  idx::idx(const ub_idx& i) : idx_(i) { }
1195  idx::idx(const lub_idx& i) : idx_(i) { }
1196 
1197 
1200  return false;
1201  }
1203  return true;
1204  }
1206  return true;
1207  }
1209  return true;
1210  }
1212  return true;
1213  }
1215  return true;
1216  }
1217 
1218  bool is_multi_index(const idx& idx) {
1220  return boost::apply_visitor(v, idx.idx_);
1221  }
1222 
1223  bool is_data_origin(const var_origin& vo) {
1224  return vo == data_origin || vo == transformed_data_origin;
1225  }
1226 
1227  void print_var_origin(std::ostream& o, const var_origin& vo) {
1228  if (vo == model_name_origin)
1229  o << "model name";
1230  else if (vo == data_origin)
1231  o << "data";
1232  else if (vo == transformed_data_origin)
1233  o << "transformed data";
1234  else if (vo == parameter_origin)
1235  o << "parameter";
1236  else if (vo == transformed_parameter_origin)
1237  o << "transformed parameter";
1238  else if (vo == derived_origin)
1239  o << "generated quantities";
1240  else if (vo == local_origin)
1241  o << "local";
1242  else if (vo == function_argument_origin)
1243  o << "function argument";
1244  else if (vo == function_argument_origin_lp)
1245  o << "function argument '_lp' suffixed";
1246  else if (vo == function_argument_origin_rng)
1247  o << "function argument '_rng' suffixed";
1248  else if (vo == void_function_argument_origin)
1249  o << "void function argument";
1250  else if (vo == void_function_argument_origin_lp)
1251  o << "void function argument '_lp' suffixed";
1252  else if (vo == void_function_argument_origin_rng)
1253  o << "void function argument '_rng' suffixed";
1254  else
1255  o << "UNKNOWN ORIGIN=" << vo;
1256  }
1257 
1258 
1261  : base_type_(base_type) {
1262  }
1263  base_var_decl::base_var_decl(const std::string& name,
1264  const std::vector<expression>& dims,
1265  const base_expr_type& base_type)
1266  : name_(name), dims_(dims), base_type_(base_type) { }
1267 
1268  bool variable_map::exists(const std::string& name) const {
1269  return map_.find(name) != map_.end();
1270  }
1271  base_var_decl variable_map::get(const std::string& name) const {
1272  if (!exists(name))
1273  throw std::invalid_argument("variable does not exist");
1274  return map_.find(name)->second.first;
1275  }
1276  base_expr_type variable_map::get_base_type(const std::string& name) const {
1277  return get(name).base_type_;
1278  }
1279  size_t variable_map::get_num_dims(const std::string& name) const {
1280  return get(name).dims_.size();
1281  }
1282  var_origin variable_map::get_origin(const std::string& name) const {
1283  if (!exists(name))
1284  throw std::invalid_argument("variable does not exist");
1285  return map_.find(name)->second.second;
1286  }
1287  void variable_map::add(const std::string& name,
1288  const base_var_decl& base_decl,
1289  const var_origin& vo) {
1290  map_[name] = range_t(base_decl, vo);
1291  }
1292  void variable_map::remove(const std::string& name) {
1293  map_.erase(name);
1294  }
1295 
1297  : base_var_decl(INT_T)
1298  { }
1299 
1301  std::string const& name,
1302  std::vector<expression> const& dims)
1303  : base_var_decl(name, dims, INT_T),
1304  range_(range)
1305  { }
1306 
1307 
1308 
1311  { }
1312 
1314  std::string const& name,
1315  std::vector<expression> const& dims)
1316  : base_var_decl(name, dims, DOUBLE_T),
1317  range_(range)
1318  { }
1319 
1322  { }
1323 
1325  std::string const& name,
1326  std::vector<expression> const& dims)
1327  : base_var_decl(name, dims, VECTOR_T),
1328  K_(K)
1329  { }
1330 
1333  { }
1334 
1336  std::string const& name,
1337  std::vector<expression> const& dims)
1338  : base_var_decl(name, dims, VECTOR_T),
1339  K_(K)
1340  { }
1341 
1344  { }
1345 
1347  std::string const& name,
1348  std::vector<expression> const& dims)
1349  : base_var_decl(name, dims, VECTOR_T),
1350  K_(K) {
1351  }
1352 
1355  { }
1356 
1358  std::string const& name,
1359  std::vector<expression> const& dims)
1360  : base_var_decl(name, dims, VECTOR_T),
1361  K_(K) {
1362  }
1363 
1365 
1367  expression const& M,
1368  std::string const& name,
1369  std::vector<expression> const& dims)
1370  : base_var_decl(name, dims, VECTOR_T),
1371  range_(range),
1372  M_(M) {
1373  }
1374 
1377  expression const& N,
1378  std::string const& name,
1379  std::vector<expression> const& dims)
1380  : base_var_decl(name, dims, ROW_VECTOR_T),
1381  range_(range),
1382  N_(N) {
1383  }
1384 
1387  expression const& M,
1388  expression const& N,
1389  std::string const& name,
1390  std::vector<expression> const& dims)
1391  : base_var_decl(name, dims, MATRIX_T),
1392  range_(range),
1393  M_(M),
1394  N_(N) {
1395  }
1396 
1397 
1399  : base_var_decl(MATRIX_T) {
1400  }
1402  expression const& N,
1403  std::string const& name,
1404  std::vector<expression> const& dims)
1405  : base_var_decl(name, dims, MATRIX_T),
1406  M_(M),
1407  N_(N) {
1408  }
1409 
1411  : base_var_decl(MATRIX_T) {
1412  }
1414  std::string const& name,
1415  std::vector<expression> const& dims)
1416  : base_var_decl(name, dims, MATRIX_T),
1417  K_(K) {
1418  }
1419 
1421  }
1423  std::string const& name,
1424  std::vector<expression> const& dims)
1425  : base_var_decl(name, dims, MATRIX_T),
1426  K_(K) {
1427  }
1428 
1431  std::string const& name,
1432  std::vector<expression> const& dims)
1433  : base_var_decl(name, dims, MATRIX_T),
1434  K_(K) {
1435  }
1436 
1437 
1438 
1439 
1441  std::string name_vis::operator()(const nil& /* x */) const {
1442  return ""; // fail if arises
1443  }
1444  std::string name_vis::operator()(const int_var_decl& x) const {
1445  return x.name_;
1446  }
1447  std::string name_vis::operator()(const double_var_decl& x) const {
1448  return x.name_;
1449  }
1450  std::string name_vis::operator()(const vector_var_decl& x) const {
1451  return x.name_;
1452  }
1453  std::string name_vis::operator()(const row_vector_var_decl& x) const {
1454  return x.name_;
1455  }
1456  std::string name_vis::operator()(const matrix_var_decl& x) const {
1457  return x.name_;
1458  }
1459  std::string name_vis::operator()(const unit_vector_var_decl& x) const {
1460  return x.name_;
1461  }
1462  std::string name_vis::operator()(const simplex_var_decl& x) const {
1463  return x.name_;
1464  }
1465  std::string name_vis::operator()(const ordered_var_decl& x) const {
1466  return x.name_;
1467  }
1468  std::string name_vis::operator()(const positive_ordered_var_decl& x) const {
1469  return x.name_;
1470  }
1471  std::string name_vis::operator()(const cholesky_factor_var_decl& x) const {
1472  return x.name_;
1473  }
1474  std::string name_vis::operator()(const cholesky_corr_var_decl& x) const {
1475  return x.name_;
1476  }
1477  std::string name_vis::operator()(const cov_matrix_var_decl& x) const {
1478  return x.name_;
1479  }
1480  std::string name_vis::operator()(const corr_matrix_var_decl& x) const {
1481  return x.name_;
1482  }
1483 
1486  const {
1487  return base_var_decl(); // should not be called
1488  }
1490  const {
1491  return x.base_type_;
1492  }
1494  const {
1495  return x.base_type_;
1496  }
1498  const {
1499  return x.base_type_;
1500  }
1502  const row_vector_var_decl& x) const {
1503  return x.base_type_;
1504  }
1506  const {
1507  return x.base_type_;
1508  }
1510  const unit_vector_var_decl& x) const {
1511  return x.base_type_;
1512  }
1514  const simplex_var_decl& x) const {
1515  return x.base_type_;
1516  }
1518  const ordered_var_decl& x) const {
1519  return x.base_type_;
1520  }
1522  const positive_ordered_var_decl& x) const {
1523  return x.base_type_;
1524  }
1526  const cholesky_factor_var_decl& x) const {
1527  return x.base_type_;
1528  }
1530  const cholesky_corr_var_decl& x) const {
1531  return x.base_type_;
1532  }
1534  const cov_matrix_var_decl& x) const {
1535  return x.base_type_;
1536  }
1538  const corr_matrix_var_decl& x) const {
1539  return x.base_type_;
1540  }
1541 
1542 
1543 
1544  // can't template out in .cpp file
1545 
1546  var_decl::var_decl(const var_decl_t& decl) : decl_(decl) { }
1547  var_decl::var_decl() : decl_(nil()) { }
1548  var_decl::var_decl(const nil& decl) : decl_(decl) { }
1549  var_decl::var_decl(const int_var_decl& decl) : decl_(decl) { }
1550  var_decl::var_decl(const double_var_decl& decl) : decl_(decl) { }
1551  var_decl::var_decl(const vector_var_decl& decl) : decl_(decl) { }
1552  var_decl::var_decl(const row_vector_var_decl& decl) : decl_(decl) { }
1553  var_decl::var_decl(const matrix_var_decl& decl) : decl_(decl) { }
1554  var_decl::var_decl(const unit_vector_var_decl& decl) : decl_(decl) { }
1555  var_decl::var_decl(const simplex_var_decl& decl) : decl_(decl) { }
1556  var_decl::var_decl(const ordered_var_decl& decl) : decl_(decl) { }
1557  var_decl::var_decl(const positive_ordered_var_decl& decl) : decl_(decl) { }
1558  var_decl::var_decl(const cholesky_factor_var_decl& decl) : decl_(decl) { }
1559  var_decl::var_decl(const cholesky_corr_var_decl& decl) : decl_(decl) { }
1560  var_decl::var_decl(const cov_matrix_var_decl& decl) : decl_(decl) { }
1561  var_decl::var_decl(const corr_matrix_var_decl& decl) : decl_(decl) { }
1562 
1563  std::string var_decl::name() const {
1564  return boost::apply_visitor(name_vis(), decl_);
1565  }
1566 
1568  return boost::apply_visitor(var_decl_base_type_vis(), decl_);
1569  }
1570 
1571  statement::statement() : statement_(nil()) { }
1572 
1573  statement::statement(const statement_t& st) : statement_(st) { }
1574  statement::statement(const nil& st) : statement_(st) { }
1575  statement::statement(const assignment& st) : statement_(st) { }
1576  statement::statement(const assgn& st) : statement_(st) { }
1577  statement::statement(const sample& st) : statement_(st) { }
1579  : statement_(st) {
1580  }
1581  statement::statement(const statements& st) : statement_(st) { }
1582  statement::statement(const expression& st) : statement_(st) { }
1583  statement::statement(const for_statement& st) : statement_(st) { }
1584  statement::statement(const while_statement& st) : statement_(st) { }
1585  statement::statement(const conditional_statement& st) : statement_(st) { }
1586  statement::statement(const print_statement& st) : statement_(st) { }
1587  statement::statement(const reject_statement& st) : statement_(st) { }
1588  statement::statement(const return_statement& st) : statement_(st) { }
1589  statement::statement(const no_op_statement& st) : statement_(st) { }
1590 
1591 
1592  bool is_no_op_statement_vis::operator()(const nil& st) const {
1593  return false;
1594  }
1596  return false;
1597  }
1599  return false;
1600  }
1602  return false;
1603  }
1605  const increment_log_prob_statement& t) const {
1606  return false;
1607  }
1609  return false;
1610  }
1612  return false;
1613  }
1615  return false;
1616  }
1618  const conditional_statement& st) const {
1619  return false;
1620  }
1622  return false;
1623  }
1625  return false;
1626  }
1628  return false;
1629  }
1631  return true;
1632  }
1634  return false;
1635  }
1636 
1639  return boost::apply_visitor(vis, statement_);
1640  }
1641 
1643  }
1645  const expression& log_prob)
1646  : log_prob_(log_prob) {
1647  }
1648 
1650  }
1652  range& range,
1653  statement& stmt)
1654  : variable_(variable),
1655  range_(range),
1656  statement_(stmt) {
1657  }
1658 
1660  }
1662  const statement& body)
1663  : condition_(condition),
1664  body_(body) {
1665  }
1666 
1668  }
1669  conditional_statement
1670  ::conditional_statement(const std::vector<expression>& conditions,
1671  const std::vector<statement>& bodies)
1672  : conditions_(conditions),
1673  bodies_(bodies) {
1674  }
1675 
1678  : return_value_(expr) {
1679  }
1680 
1682 
1683  print_statement::print_statement(const std::vector<printable>& printables)
1684  : printables_(printables) {
1685  }
1686 
1688 
1689  reject_statement::reject_statement(const std::vector<printable>& printables)
1690  : printables_(printables) {
1691  }
1692 
1694  program::program(const std::vector<function_decl_def>& function_decl_defs,
1695  const std::vector<var_decl>& data_decl,
1696  const std::pair<std::vector<var_decl>,
1697  std::vector<statement> >& derived_data_decl,
1698  const std::vector<var_decl>& parameter_decl,
1699  const std::pair<std::vector<var_decl>,
1700  std::vector<statement> >& derived_decl,
1701  const statement& st,
1702  const std::pair<std::vector<var_decl>,
1703  std::vector<statement> >& generated_decl)
1704  : function_decl_defs_(function_decl_defs),
1705  data_decl_(data_decl),
1706  derived_data_decl_(derived_data_decl),
1707  parameter_decl_(parameter_decl),
1708  derived_decl_(derived_decl),
1709  statement_(st),
1710  generated_decl_(generated_decl) {
1711  }
1712 
1714  }
1716  distribution& dist)
1717  : expr_(e),
1718  dist_(dist) {
1719  }
1720  bool sample::is_ill_formed() const {
1722  || (truncation_.has_low()
1724  || (truncation_.has_high()
1725  && expr_.expression_type()
1727  }
1728 
1730  }
1732  expression& expr)
1733  : var_dims_(var_dims),
1734  expr_(expr) {
1735  }
1736 
1738  : var_name_(e.name_) {
1739  }
1740 
1741  bool var_occurs_vis::operator()(const nil& st) const {
1742  return false;
1743  }
1745  return false;
1746  }
1748  return false;
1749  }
1751  return false; // TODO(carpenter): update for array_literal
1752  }
1753  bool var_occurs_vis::operator()(const variable& e) const {
1754  return var_name_ == e.name_;
1755  }
1756  bool var_occurs_vis::operator()(const fun& e) const {
1757  for (size_t i = 0; i < e.args_.size(); ++i)
1758  if (boost::apply_visitor(*this, e.args_[i].expr_))
1759  return true;
1760  return false;
1761  }
1763  return false; // no refs persist out of integrate_ode() call
1764  }
1766  return false; // no refs persist out of integrate_ode_control() call
1767  }
1768  bool var_occurs_vis::operator()(const index_op& e) const {
1769  // refs only persist out of expression, not indexes
1770  return boost::apply_visitor(*this, e.expr_.expr_);
1771  }
1773  return boost::apply_visitor(*this, e.expr_.expr_);
1774  }
1776  return boost::apply_visitor(*this, e.cond_.expr_)
1777  || boost::apply_visitor(*this, e.true_val_.expr_)
1778  || boost::apply_visitor(*this, e.false_val_.expr_);
1779  }
1780  bool var_occurs_vis::operator()(const binary_op& e) const {
1781  return boost::apply_visitor(*this, e.left.expr_)
1782  || boost::apply_visitor(*this, e.right.expr_);
1783  }
1784  bool var_occurs_vis::operator()(const unary_op& e) const {
1785  return boost::apply_visitor(*this, e.subject.expr_);
1786  }
1787 
1789  assgn::assgn(const variable& lhs_var, const std::vector<idx>& idxs,
1790  const expression& rhs)
1791  : lhs_var_(lhs_var), idxs_(idxs), rhs_(rhs) { }
1792 
1794  var_occurs_vis vis(lhs_var_);
1795  return boost::apply_visitor(vis, rhs_.expr_);
1796  }
1797 
1809  const std::vector<idx>& idxs) {
1810  expr_type e_type = e.expression_type();
1811 
1812  base_expr_type base_type = e_type.base_type_;
1813  size_t base_dims = e_type.num_dims_;
1814  size_t unindexed_dims = base_dims;
1815  size_t out_dims = 0U;
1816  size_t i = 0;
1817  for ( ; unindexed_dims > 0 && i < idxs.size(); ++i, --unindexed_dims)
1818  if (is_multi_index(idxs[i]))
1819  ++out_dims;
1820  if (idxs.size() - i == 0) {
1821  return expr_type(base_type, out_dims + unindexed_dims);
1822  } else if (idxs.size() - i == 1) {
1823  if (base_type == MATRIX_T) {
1824  if (is_multi_index(idxs[i]))
1825  return expr_type(MATRIX_T, out_dims);
1826  else
1827  return expr_type(ROW_VECTOR_T, out_dims);
1828  } else if (base_type == VECTOR_T) {
1829  if (is_multi_index(idxs[i]))
1830  return expr_type(VECTOR_T, out_dims);
1831  else
1832  return expr_type(DOUBLE_T, out_dims);
1833  } else if (base_type == ROW_VECTOR_T) {
1834  if (is_multi_index(idxs[i]))
1835  return expr_type(ROW_VECTOR_T, out_dims);
1836  else
1837  return expr_type(DOUBLE_T, out_dims);
1838  } else {
1839  return expr_type(ILL_FORMED_T, 0U);
1840  }
1841  } else if (idxs.size() - i == 2) {
1842  if (base_type == MATRIX_T) {
1843  if (is_multi_index(idxs[i]) && is_multi_index(idxs[i + 1]))
1844  return expr_type(MATRIX_T, out_dims);
1845  else if (is_multi_index(idxs[i]))
1846  return expr_type(VECTOR_T, out_dims);
1847  else if (is_multi_index(idxs[i + 1]))
1848  return expr_type(ROW_VECTOR_T, out_dims);
1849  else
1850  return expr_type(DOUBLE_T, out_dims);
1851  } else {
1852  return expr_type(ILL_FORMED_T, 0U);
1853  }
1854  } else {
1855  return expr_type(ILL_FORMED_T, 0U);
1856  }
1857  }
1858 
1859 
1861  expr_ = binary_op(expr_, "+", rhs);
1862  return *this;
1863  }
1864 
1866  expr_ = binary_op(expr_, "-", rhs);
1867  return *this;
1868  }
1869 
1871  expr_ = binary_op(expr_, "*", rhs);
1872  return *this;
1873  }
1874 
1876  expr_ = binary_op(expr_, "/", rhs);
1877  return *this;
1878  }
1879 
1880  bool has_rng_suffix(const std::string& s) {
1881  int n = s.size();
1882  return n > 4
1883  && s[n-1] == 'g'
1884  && s[n-2] == 'n'
1885  && s[n-3] == 'r'
1886  && s[n-4] == '_';
1887  }
1888 
1889  bool has_lp_suffix(const std::string& s) {
1890  int n = s.size();
1891  return n > 3
1892  && s[n-1] == 'p'
1893  && s[n-2] == 'l'
1894  && s[n-3] == '_';
1895  }
1896 
1897  bool is_user_defined(const std::string& name,
1898  const std::vector<expression>& args) {
1899  std::vector<expr_type> arg_types;
1900  for (size_t i = 0; i < args.size(); ++i)
1901  arg_types.push_back(args[i].expression_type());
1903  int matches
1905  .get_signature_matches(name, arg_types, sig);
1906  if (matches != 1)
1907  return false; // reall shouldn't come up; throw instead?
1908  std::pair<std::string, function_signature_t>
1909  name_sig(name, sig);
1911  }
1912 
1913  bool is_user_defined_prob_function(const std::string& name,
1914  const expression& variate,
1915  const std::vector<expression>& params) {
1916  std::vector<expression> variate_params;
1917  variate_params.push_back(variate);
1918  for (size_t i = 0; i < params.size(); ++i)
1919  variate_params.push_back(params[i]);
1920  return is_user_defined(name, variate_params);
1921  }
1922 
1923  bool is_user_defined(const fun& fx) {
1924  return is_user_defined(fx.name_, fx.args_);
1925  }
1926 
1927  bool is_assignable(const expr_type& l_type,
1928  const expr_type& r_type,
1929  const std::string& failure_message,
1930  std::ostream& error_msgs) {
1931  bool assignable = true;
1932  if (l_type.num_dims_ != r_type.num_dims_) {
1933  assignable = false;
1934  error_msgs << "Mismatched array dimensions.";
1935  }
1936  if (l_type.base_type_ != r_type.base_type_
1937  && (!(l_type.base_type_ == DOUBLE_T
1938  && r_type.base_type_ == INT_T))) {
1939  assignable = false;
1940  error_msgs << "Base type mismatch. ";
1941  }
1942  if (!assignable)
1943  error_msgs << failure_message
1944  << std::endl
1945  << " LHS type = " << l_type
1946  << "; RHS type = " << r_type
1947  << std::endl;
1948  return assignable;
1949  }
1950 
1951  bool ends_with(const std::string& suffix,
1952  const std::string& s) {
1953  size_t idx = s.rfind(suffix);
1954  return idx != std::string::npos
1955  && idx == (s.size() - suffix.size());
1956  }
1957 
1958  std::string get_cdf(const std::string& dist_name) {
1959  if (function_signatures::instance().has_key(dist_name + "_cdf_log"))
1960  return dist_name + "_cdf_log";
1961  else if (function_signatures::instance().has_key(dist_name + "_lcdf"))
1962  return dist_name + "_lcdf";
1963  else
1964  return dist_name;
1965  }
1966 
1967  std::string get_ccdf(const std::string& dist_name) {
1968  if (function_signatures::instance().has_key(dist_name + "_ccdf_log"))
1969  return dist_name + "_ccdf_log";
1970  else if (function_signatures::instance().has_key(dist_name + "_lccdf"))
1971  return dist_name + "_lccdf";
1972  else
1973  return dist_name;
1974  }
1975 
1976  std::string get_prob_fun(const std::string& dist_name) {
1977  if (function_signatures::instance().has_key(dist_name + "_log"))
1978  return dist_name + "_log";
1979  else if (function_signatures::instance().has_key(dist_name + "_lpdf"))
1980  return dist_name + "_lpdf";
1981  else if (function_signatures::instance().has_key(dist_name + "_lpmf"))
1982  return dist_name + "_lpmf";
1983  else
1984  return dist_name;
1985  }
1986 
1987  bool has_prob_fun_suffix(const std::string& fname) {
1988  return ends_with("_lpdf", fname) || ends_with("_lpmf", fname)
1989  || ends_with("_log", fname);
1990  }
1991 
1992  std::string strip_prob_fun_suffix(const std::string& fname) {
1993  if (ends_with("_lpdf", fname))
1994  return fname.substr(0, fname.size() - 5);
1995  else if (ends_with("_lpmf", fname))
1996  return fname.substr(0, fname.size() - 5);
1997  else if (ends_with("_log", fname))
1998  return fname.substr(0, fname.size() - 4);
1999  else
2000  return fname;
2001  }
2002 
2003  bool has_cdf_suffix(const std::string& fname) {
2004  return ends_with("_lcdf", fname) || ends_with("_cdf_log", fname);
2005  }
2006 
2007  std::string strip_cdf_suffix(const std::string& fname) {
2008  if (ends_with("_lcdf", fname))
2009  return fname.substr(0, fname.size() - 5);
2010  else if (ends_with("_cdf_log", fname))
2011  return fname.substr(0, fname.size() - 8);
2012  else
2013  return fname;
2014  }
2015 
2016  bool has_ccdf_suffix(const std::string& fname) {
2017  return ends_with("_lccdf", fname) || ends_with("_ccdf_log", fname);
2018  }
2019 
2020  std::string strip_ccdf_suffix(const std::string& fname) {
2021  if (ends_with("_lccdf", fname))
2022  return fname.substr(0, fname.size() - 6);
2023  else if (ends_with("_ccdf_log", fname))
2024  return fname.substr(0, fname.size() - 9);
2025  else
2026  return fname;
2027  }
2028 
2029  bool fun_name_exists(const std::string& name) {
2030  return function_signatures::instance().has_key(name);
2031  }
2032 
2033 
2034 
2035  }
2036 }
2037 #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
var_occurs_vis(const variable &e)
Definition: ast_def.cpp:1737
std::string strip_ccdf_suffix(const std::string &dist_fun)
Definition: ast_def.cpp:2020
base_var_decl base_decl() const
Definition: ast_def.cpp:1567
bool lhs_var_occurs_on_rhs() const
Definition: ast_def.cpp:1793
void add(const std::string &name, const expr_type &result_type, const std::vector< expr_type > &arg_types)
Definition: ast_def.cpp:180
expr_type type_
Definition: ast.hpp:436
expression_t expr_
Definition: ast.hpp:286
const int function_argument_origin
Definition: ast.hpp:84
const variable_map & var_map_
Definition: ast.hpp:1056
const int ROW_VECTOR_T
Definition: ast.hpp:72
expr_type type_
Definition: ast.hpp:416
bool is_operator(const std::string &name)
Definition: ast_def.cpp:358
bool has_lp_suffix(const std::string &s)
Definition: ast_def.cpp:1889
std::string name_
Definition: ast.hpp:358
const int derived_origin
Definition: ast.hpp:82
std::vector< statement > statements_
Definition: ast.hpp:213
contains_var(const variable_map &var_map)
Definition: ast_def.cpp:746
size_t num_dims_
Definition: ast.hpp:99
expression & operator-=(const expression &rhs)
Definition: ast_def.cpp:1865
std::string strip_cdf_suffix(const std::string &dist_fun)
Definition: ast_def.cpp:2007
range truncation_
Definition: ast.hpp:983
const int parameter_origin
Definition: ast.hpp:80
bool has_cdf_suffix(const std::string &name)
Definition: ast_def.cpp:2003
const int function_argument_origin_rng
Definition: ast.hpp:86
Probability, optimization and sampling library.
size_t total_dims(const std::vector< std::vector< expression > > &dimss)
Definition: ast_def.cpp:1073
size_t num_dims() const
Definition: ast_def.cpp:116
bool operator()(const nil &e) const
Definition: ast_def.cpp:1741
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:1976
static function_signatures & instance()
Definition: ast_def.cpp:150
bool is_multi_index(const idx &idx)
Definition: ast_def.cpp:1218
bool is_unary_postfix_operator(const std::string &name)
Definition: ast_def.cpp:354
std::pair< base_var_decl, var_origin > range_t
Definition: ast.hpp:574
std::vector< expression > args_
Definition: ast.hpp:415
expr_type type_
Definition: ast.hpp:471
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
std::string get_cdf(const std::string &dist_name)
Definition: ast_def.cpp:1958
bool exists(const std::string &name) const
Definition: ast_def.cpp:1268
bool operator!=(const expr_type &et) const
Definition: ast_def.cpp:72
std::vector< statement > bodies_
Definition: ast.hpp:888
bool is_ill_formed() const
Definition: ast_def.cpp:1720
const int DOUBLE_T
Definition: ast.hpp:70
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:1927
bool has_rng_suffix(const std::string &s)
Definition: ast_def.cpp:1880
int num_promotions(const std::vector< expr_type > &call_args, const std::vector< expr_type > &sig_args)
Definition: ast_def.cpp:296
bool is_primitive_int() const
Definition: ast_def.cpp:99
base_var_decl operator()(const nil &x) const
Definition: ast_def.cpp:1485
int base_expr_type
Definition: ast.hpp:64
int get_signature_matches(const std::string &name, const std::vector< expr_type > &args, function_signature_t &signature)
Definition: ast_def.cpp:315
const int transformed_parameter_origin
Definition: ast.hpp:81
expression expr_
Definition: ast.hpp:434
statement_t statement_
Definition: ast.hpp:802
base_var_decl get(const std::string &name) const
Definition: ast_def.cpp:1271
bool is_no_op_statement() const
Definition: ast_def.cpp:1637
void add_binary(const ::std::string &name)
Definition: ast_def.cpp:287
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
expr_type type_
Definition: ast.hpp:359
contains_nonparam_var(const variable_map &var_map)
Definition: ast_def.cpp:841
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
bool is_void() const
Definition: ast_def.cpp:110
const int function_argument_origin_lp
Definition: ast.hpp:85
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:757
expr_type type_
Definition: ast.hpp:461
int total_dims() const
Definition: ast_def.cpp:727
bool is_unary_operator(const std::string &name)
Definition: ast_def.cpp:349
expr_type expression_type() const
Definition: ast_def.cpp:707
const variable_map & var_map_
Definition: ast.hpp:1078
void print_var_origin(std::ostream &o, const var_origin &vo)
Definition: ast_def.cpp:1227
void add_unary(const ::std::string &name)
Definition: ast_def.cpp:284
expression & operator*=(const expression &rhs)
Definition: ast_def.cpp:1870
bool is_nil(const expression &e)
Definition: ast_def.cpp:955
var_decl_t decl_
Definition: ast.hpp:759
array_literal & operator=(const array_literal &al)
Definition: ast_def.cpp:1008
expression left
Definition: ast.hpp:459
bool operator()(const nil &e) const
Definition: ast_def.cpp:844
void remove(const std::string &name)
Definition: ast_def.cpp:1292
const int void_function_argument_origin_rng
Definition: ast.hpp:89
void add_nullary(const ::std::string &name)
Definition: ast_def.cpp:281
bool fun_name_exists(const std::string &name)
Definition: ast_def.cpp:2029
const int model_name_origin
Definition: ast.hpp:77
bool is_primitive() const
Definition: ast_def.cpp:95
bool has_key(const std::string &key) const
Definition: ast_def.cpp:513
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
expression & operator+=(const expression &rhs)
Definition: ast_def.cpp:1860
bool is_user_defined_prob_function(const std::string &name, const expression &variate, const std::vector< expression > &params)
Definition: ast_def.cpp:1913
bool is_defined(const std::string &name, const function_signature_t &sig)
Definition: ast_def.cpp:170
void add_quaternary(const ::std::string &name)
Definition: ast_def.cpp:293
bool operator<(const expr_type &et) const
Definition: ast_def.cpp:75
bool is_ill_formed() const
Definition: ast_def.cpp:107
bool operator()(const uni_idx &i) const
Definition: ast_def.cpp:1199
const int VOID_T
Definition: ast.hpp:68
base_expr_type type() const
Definition: ast_def.cpp:113
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
bool has_low() const
Definition: ast_def.cpp:1162
bool is_user_defined(const std::pair< std::string, function_signature_t > &name_sig)
Definition: ast_def.cpp:164
std::string name_
Definition: ast.hpp:563
bool operator()(const nil &e) const
Definition: ast_def.cpp:749
int var_origin
Definition: ast.hpp:76
expr_type promote_primitive(const expr_type &et)
Definition: ast_def.cpp:132
const int void_function_argument_origin
Definition: ast.hpp:87
base_expr_type base_type_
Definition: ast.hpp:565
expression return_value_
Definition: ast.hpp:915
size_t get_num_dims(const std::string &name) const
Definition: ast_def.cpp:1279
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:387
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< print_statement >, boost::recursive_wrapper< reject_statement >, boost::recursive_wrapper< return_statement >, boost::recursive_wrapper< no_op_statement > > statement_t
Definition: ast.hpp:800
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:127
std::string operator()(const nil &x) const
Definition: ast_def.cpp:1441
boost::variant< boost::recursive_wrapper< std::string >, boost::recursive_wrapper< expression > > printable_t
Definition: ast.hpp:293
bool has_non_param_var(const expression &e, const variable_map &var_map)
Definition: ast_def.cpp:925
expression rhs_
Definition: ast.hpp:1021
const int data_origin
Definition: ast.hpp:78
const int void_function_argument_origin_lp
Definition: ast.hpp:88
base_expr_type base_type_
Definition: ast.hpp:98
std::vector< idx > idxs_
Definition: ast.hpp:552
bool is_binary_operator(const std::string &name)
Definition: ast_def.cpp:337
bool is_data_origin(const var_origin &vo)
Definition: ast_def.cpp:1223
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
bool operator()(const nil &st) const
Definition: ast_def.cpp:555
std::string name_
Definition: ast.hpp:926
std::vector< expression > conditions_
Definition: ast.hpp:887
statement statement_
Definition: ast.hpp:878
variable lhs_var_
Definition: ast.hpp:1019
std::set< std::string > key_set() const
Definition: ast_def.cpp:499
bool has_var(const expression &e, const variable_map &var_map)
Definition: ast_def.cpp:835
bool is_linear_function(const std::string &name)
Definition: ast_def.cpp:805
int_literal & operator=(const int_literal &il)
Definition: ast_def.cpp:979
expression expr_
Definition: ast.hpp:981
const std::string var_name_
Definition: ast.hpp:1001
bool operator==(const expr_type &et) const
Definition: ast_def.cpp:68
bool operator>=(const expr_type &et) const
Definition: ast_def.cpp:90
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
expr_type operator()(const nil &e) const
Definition: ast_def.cpp:660
bool operator>(const expr_type &et) const
Definition: ast_def.cpp:85
void add_ternary(const ::std::string &name)
Definition: ast_def.cpp:290
const int VECTOR_T
Definition: ast.hpp:71
double_literal & operator=(const double_literal &dl)
Definition: ast_def.cpp:993
expression right
Definition: ast.hpp:460
std::string strip_prob_fun_suffix(const std::string &dist_fun)
Definition: ast_def.cpp:1992
idx_t idx_
Definition: ast.hpp:535
expression subject
Definition: ast.hpp:470
std::vector< expression > args_
Definition: ast.hpp:350
const int local_origin
Definition: ast.hpp:83
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:257
std::ostream & write_base_expr_type(std::ostream &o, base_expr_type type)
Definition: ast_def.cpp:25
bool operator()(const nil &st) const
Definition: ast_def.cpp:1592
void set_user_defined(const std::pair< std::string, function_signature_t > &name_sig)
Definition: ast_def.cpp:157
bool is_user_defined(const std::string &name, const std::vector< expression > &args)
Definition: ast_def.cpp:1897
expr_type arg_type_
Definition: ast.hpp:925
std::string op
Definition: ast.hpp:458
returns_type_vis(const expr_type &return_type, std::ostream &error_msgs)
Definition: ast_def.cpp:550
bool is_primitive_double() const
Definition: ast_def.cpp:103
std::vector< std::vector< expression > > dimss_
Definition: ast.hpp:435
std::string name() const
Definition: ast_def.cpp:1563
base_expr_type get_base_type(const std::string &name) const
Definition: ast_def.cpp:1276
std::string fun_name_to_operator(const std::string &name)
Definition: ast_def.cpp:364
Placeholder struct for boost::variant default ctors.
Definition: ast.hpp:18
std::map< std::string, range_t > map_
Definition: ast.hpp:586
const int ILL_FORMED_T
Definition: ast.hpp:74
const int MATRIX_T
Definition: ast.hpp:73
expression false_val_
Definition: ast.hpp:447
expression & operator/=(const expression &rhs)
Definition: ast_def.cpp:1875
unary_op(char op, expression const &subject)
Definition: ast_def.cpp:1148
const int transformed_data_origin
Definition: ast.hpp:79
bool operator()(const nil &x) const
Definition: ast_def.cpp:931
std::ostream & error_msgs_
Definition: ast.hpp:846
bool has_high() const
Definition: ast_def.cpp:1165

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