Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ast_def.cpp
Go to the documentation of this file.
1 #ifndef __STAN__GM__AST_DEF_HPP__
2 #define __STAN__GM__AST_DEF_HPP__
3 
4 #include <boost/variant/apply_visitor.hpp>
5 #include <boost/variant/recursive_variant.hpp>
6 
7 #include <cstddef>
8 #include <limits>
9 #include <climits>
10 #include <iostream>
11 #include <map>
12 #include <stdexcept>
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
17 #include <stan/gm/ast.hpp>
18 
19 namespace stan {
20 
21  namespace gm {
22 
23  std::ostream& write_base_expr_type(std::ostream& o, base_expr_type type) {
24  switch (type) {
25  case INT_T :
26  o << "int";
27  break;
28  case DOUBLE_T :
29  o << "real";
30  break;
31  case VECTOR_T :
32  o << "vector";
33  break;
34  case ROW_VECTOR_T :
35  o << "row vector";
36  break;
37  case MATRIX_T :
38  o << "matrix";
39  break;
40  case ILL_FORMED_T :
41  o << "ill formed";
42  break;
43  case VOID_T :
44  o << "void";
45  break;
46  default:
47  o << "UNKNOWN";
48  }
49  return o;
50  }
51 
52  // expr_type ctors and methods
54  : base_type_(ILL_FORMED_T),
55  num_dims_(0) {
56  }
58  : base_type_(base_type),
59  num_dims_(0) {
60  }
62  size_t num_dims)
63  : base_type_(base_type),
64  num_dims_(num_dims) {
65  }
66  bool expr_type::operator==(const expr_type& et) const {
67  return base_type_ == et.base_type_
68  && num_dims_ == et.num_dims_;
69  }
70  bool expr_type::operator!=(const expr_type& et) const {
71  return !(*this == et);
72  }
73  bool expr_type::operator<(const expr_type& et) const {
74  return (base_type_ < et.base_type_)
75  || ( base_type_ == et.base_type_
76  && num_dims_ < et.num_dims_ );
77  }
78  bool expr_type::operator<=(const expr_type& et) const {
79  return (base_type_ < et.base_type_)
80  || ( base_type_ == et.base_type_
81  && num_dims_ <= et.num_dims_ );
82  }
83  bool expr_type::operator>(const expr_type& et) const {
84  return (base_type_ > et.base_type_)
85  || ( base_type_ == et.base_type_
86  && num_dims_ > et.num_dims_ );
87  }
88  bool expr_type::operator>=(const expr_type& et) const {
89  return (base_type_ > et.base_type_)
90  || ( base_type_ == et.base_type_
91  && num_dims_ >= et.num_dims_ );
92  }
93  bool expr_type::is_primitive() const {
94  return is_primitive_int()
96  }
98  return base_type_ == INT_T
99  && num_dims_ == 0U;
100  }
102  return base_type_ == DOUBLE_T
103  && num_dims_ == 0U;
104  }
106  return base_type_ == ILL_FORMED_T;
107  }
108  bool expr_type::is_void() const {
109  return base_type_ == VOID_T;
110  }
112  return base_type_;
113  }
114  size_t expr_type::num_dims() const {
115  return num_dims_;
116  }
117 
118  std::ostream& operator<<(std::ostream& o, const expr_type& et) {
119  write_base_expr_type(o,et.type());
120  if (et.num_dims() > 0)
121  o << '[' << et.num_dims() << ']';
122  return o;
123  }
124 
126  if (!et.is_primitive())
127  return expr_type();
128  return et;
129  }
130 
132  const expr_type& et2) {
133  if (!et1.is_primitive() || !et2.is_primitive())
134  return expr_type();
135  return et1.type() == DOUBLE_T ? et1 : et2;
136  }
137 
139  if (sigs_ == 0) return;
140  delete sigs_;
141  sigs_ = 0;
142  }
144  // FIXME: for threaded models, requires double-check lock
145  if (!sigs_)
146  sigs_ = new function_signatures;
147  return *sigs_;
148  }
149  void
151  std::pair<std::string,
153  name_sig) {
154  user_defined_set_.insert(name_sig);
155  }
156  bool
157  function_signatures::is_user_defined(const std::pair<std::string,
159  name_sig) {
160  return user_defined_set_.find(name_sig) != user_defined_set_.end();
161  }
162  bool
163  function_signatures::is_defined(const std::string& name,
164  const function_signature_t& sig) {
165  const std::vector<function_signature_t> sigs = sigs_map_[name];
166  for (size_t i = 0; i < sigs.size(); ++i)
167  if (sig.second == sigs[i].second)
168  return true;
169  return false;
170  }
171  void function_signatures::add(const std::string& name,
172  const expr_type& result_type,
173  const std::vector<expr_type>& arg_types) {
174  sigs_map_[name].push_back(function_signature_t(result_type,arg_types));
175 
176  }
177  void function_signatures::add(const std::string& name,
178  const expr_type& result_type) {
179  std::vector<expr_type> arg_types;
180  add(name,result_type,arg_types);
181  }
182  void function_signatures::add(const std::string& name,
183  const expr_type& result_type,
184  const expr_type& arg_type) {
185  std::vector<expr_type> arg_types;
186  arg_types.push_back(arg_type);
187  add(name,result_type,arg_types);
188  }
189  void function_signatures::add(const std::string& name,
190  const expr_type& result_type,
191  const expr_type& arg_type1,
192  const expr_type& arg_type2) {
193  std::vector<expr_type> arg_types;
194  arg_types.push_back(arg_type1);
195  arg_types.push_back(arg_type2);
196  add(name,result_type,arg_types);
197  }
198  void function_signatures::add(const std::string& name,
199  const expr_type& result_type,
200  const expr_type& arg_type1,
201  const expr_type& arg_type2,
202  const expr_type& arg_type3) {
203  std::vector<expr_type> arg_types;
204  arg_types.push_back(arg_type1);
205  arg_types.push_back(arg_type2);
206  arg_types.push_back(arg_type3);
207  add(name,result_type,arg_types);
208  }
209  void function_signatures::add(const std::string& name,
210  const expr_type& result_type,
211  const expr_type& arg_type1,
212  const expr_type& arg_type2,
213  const expr_type& arg_type3,
214  const expr_type& arg_type4) {
215  std::vector<expr_type> arg_types;
216  arg_types.push_back(arg_type1);
217  arg_types.push_back(arg_type2);
218  arg_types.push_back(arg_type3);
219  arg_types.push_back(arg_type4);
220  add(name,result_type,arg_types);
221  }
222  void function_signatures::add(const std::string& name,
223  const expr_type& result_type,
224  const expr_type& arg_type1,
225  const expr_type& arg_type2,
226  const expr_type& arg_type3,
227  const expr_type& arg_type4,
228  const expr_type& arg_type5) {
229  std::vector<expr_type> arg_types;
230  arg_types.push_back(arg_type1);
231  arg_types.push_back(arg_type2);
232  arg_types.push_back(arg_type3);
233  arg_types.push_back(arg_type4);
234  arg_types.push_back(arg_type5);
235  add(name,result_type,arg_types);
236  }
237  void function_signatures::add(const std::string& name,
238  const expr_type& result_type,
239  const expr_type& arg_type1,
240  const expr_type& arg_type2,
241  const expr_type& arg_type3,
242  const expr_type& arg_type4,
243  const expr_type& arg_type5,
244  const expr_type& arg_type6) {
245  std::vector<expr_type> arg_types;
246  arg_types.push_back(arg_type1);
247  arg_types.push_back(arg_type2);
248  arg_types.push_back(arg_type3);
249  arg_types.push_back(arg_type4);
250  arg_types.push_back(arg_type5);
251  arg_types.push_back(arg_type6);
252  add(name,result_type,arg_types);
253  }
254  void function_signatures::add(const std::string& name,
255  const expr_type& result_type,
256  const expr_type& arg_type1,
257  const expr_type& arg_type2,
258  const expr_type& arg_type3,
259  const expr_type& arg_type4,
260  const expr_type& arg_type5,
261  const expr_type& arg_type6,
262  const expr_type& arg_type7) {
263  std::vector<expr_type> arg_types;
264  arg_types.push_back(arg_type1);
265  arg_types.push_back(arg_type2);
266  arg_types.push_back(arg_type3);
267  arg_types.push_back(arg_type4);
268  arg_types.push_back(arg_type5);
269  arg_types.push_back(arg_type6);
270  arg_types.push_back(arg_type7);
271  add(name,result_type,arg_types);
272  }
273  void function_signatures::add_nullary(const::std::string& name) {
274  add(name,DOUBLE_T);
275  }
276  void function_signatures::add_unary(const::std::string& name) {
277  add(name,DOUBLE_T,DOUBLE_T);
278  }
279  void function_signatures::add_binary(const::std::string& name) {
281  }
282  void function_signatures::add_ternary(const::std::string& name) {
284  }
285  void function_signatures::add_quaternary(const::std::string& name) {
287  }
289  const std::vector<expr_type>& call_args,
290  const std::vector<expr_type>& sig_args) {
291  if (call_args.size() != sig_args.size()) {
292  return -1; // failure
293  }
294  int num_promotions = 0;
295  for (size_t i = 0; i < call_args.size(); ++i) {
296  if (call_args[i] == sig_args[i]) {
297  continue;
298  } else if (call_args[i].is_primitive_int()
299  && sig_args[i].is_primitive_double()) {
300  ++num_promotions;
301  } else {
302  return -1; // failed match
303  }
304  }
305  return num_promotions;
306  }
307  int function_signatures::get_signature_matches(const std::string& name,
308  const std::vector<expr_type>& args,
309  function_signature_t& signature) {
310 
311  std::vector<function_signature_t> signatures = sigs_map_[name];
312  size_t min_promotions = std::numeric_limits<size_t>::max();
313  size_t num_matches = 0;
314  for (size_t i = 0; i < signatures.size(); ++i) {
315  signature = signatures[i];
316  int promotions = num_promotions(args,signature.second);
317  if (promotions < 0) continue; // no match
318  size_t promotions_ui = static_cast<size_t>(promotions);
319  if (promotions_ui < min_promotions) {
320  min_promotions = promotions_ui;
321  num_matches = 1;
322  } else if (promotions_ui == min_promotions) {
323  ++num_matches;
324  }
325  }
326  return num_matches;
327  }
328 
330  const std::string& name,
331  const std::vector<expr_type>& args,
332  std::ostream& error_msgs) {
333  std::vector<function_signature_t> signatures = sigs_map_[name];
334  size_t match_index = 0;
335  size_t min_promotions = std::numeric_limits<size_t>::max();
336  size_t num_matches = 0;
337 
338  for (size_t i = 0; i < signatures.size(); ++i) {
339  int promotions = num_promotions(args,signatures[i].second);
340  if (promotions < 0) continue; // no match
341  size_t promotions_ui = static_cast<size_t>(promotions);
342  if (promotions_ui < min_promotions) {
343  min_promotions = promotions_ui;
344  match_index = i;
345  num_matches = 1;
346  } else if (promotions_ui == min_promotions) {
347  ++num_matches;
348  }
349  }
350 
351  if (num_matches == 1) {
352  return signatures[match_index].first;
353  } else if (num_matches == 0) {
354  error_msgs << "no matches for function name=\"" << name << "\""
355  << std::endl;
356  } else {
357  error_msgs << num_matches << " matches with "
358  << min_promotions << " integer promotions "
359  << "for function name=\"" << name << "\"" << std::endl;
360  }
361  for (size_t i = 0; i < args.size(); ++i)
362  error_msgs << " arg " << i << " type=" << args[i] << std::endl;
363 
364  error_msgs << "available function signatures for "
365  << name << ":" << std::endl;
366  for (size_t i = 0; i < signatures.size(); ++i) {
367  error_msgs << i << ". " << name << "(";
368  for (size_t j = 0; j < signatures[i].second.size(); ++j) {
369  if (j > 0) error_msgs << ", ";
370  error_msgs << signatures[i].second[j];
371  }
372  error_msgs << ") : " << signatures[i].first << std::endl;
373  }
374  return expr_type(); // ill-formed dummy
375  }
376  function_signatures::function_signatures() {
377 #include <stan/gm/function_signatures.h>
378  }
379  std::set<std::string>
381  using std::map;
382  using std::set;
383  using std::string;
384  using std::vector;
385  set<string> result;
386  for (map<string,vector<function_signature_t> >::const_iterator
387  it = sigs_map_.begin();
388  it != sigs_map_.end();
389  ++it)
390  result.insert(it->first);
391  return result;
392  }
393 
394  function_signatures* function_signatures::sigs_ = 0;
395 
396 
398  arg_decl::arg_decl(const expr_type& arg_type,
399  const std::string& name)
400  : arg_type_(arg_type),
401  name_(name) {
402  }
404  std::vector<expression> dims;
405  for (size_t i = 0; i < arg_type_.num_dims_; ++i)
406  dims.push_back(expression(int_literal(0))); // dummy value 0
408  }
409 
412  const std::string& name,
413  const std::vector<arg_decl>& arg_decls,
414  const statement& body)
415 
416  : return_type_(return_type),
417  name_(name),
418  arg_decls_(arg_decls),
419  body_(body) {
420  }
421 
423  function_decl_defs::function_decl_defs(const std::vector<function_decl_def>&
424  decl_defs)
425  : decl_defs_(decl_defs) {
426  }
427 
429  std::ostream& error_msgs)
430  : return_type_(return_type),
431  error_msgs_(error_msgs) {
432  }
433  bool returns_type_vis::operator()(const nil& st) const {
434  error_msgs_ << "Expecting return, found nil statement."
435  << std::endl;
436  return false;
437  }
438  bool returns_type_vis::operator()(const assignment& st) const {
439  error_msgs_ << "Expecting return, found assignment statement."
440  << std::endl;
441  return false;
442  }
443  bool returns_type_vis::operator()(const sample& st) const {
444  error_msgs_ << "Expecting return, found sampling statement."
445  << std::endl;
446  return false;
447  }
449  increment_log_prob_statement& t) const {
450  error_msgs_ << "Expecting return, found increment_log_prob statement."
451  << std::endl;
452  return false;
453  }
454  bool returns_type_vis::operator()(const expression& st) const {
455  error_msgs_ << "Expecting return, found increment_log_prob statement."
456  << std::endl;
457  return false;
458  }
460  error_msgs_ << "Expecting return, found print statement."
461  << std::endl;
462  return false;
463  }
465  error_msgs_ << "Expecting return, found reject statement."
466  << std::endl;
467  return false;
468  }
470  error_msgs_ << "Expecting return, found no_op statement."
471  << std::endl;
472  return false;
473  }
474  // recursive cases
475  bool returns_type_vis::operator()(const statements& st) const {
476  // last statement in sequence must return type
477  if (st.statements_.size() == 0) {
478  error_msgs_ << ( "Expecting return, found"
479  " statement sequence with empty body." )
480  << std::endl;
481  return false;
482  }
483  return returns_type(return_type_, st.statements_.back(), error_msgs_);
484  }
486  // body must end in appropriate return
488  }
490  // body must end in appropriate return
492  }
494  conditional_statement& st) const {
495  // all condition bodies must end in appropriate return
496  if (st.bodies_.size() != (st.conditions_.size() + 1)) {
497  error_msgs_ << ( "Expecting return, found conditional"
498  " without final else.")
499  << std::endl;
500  return false;
501  }
502  for (size_t i = 0; i < st.bodies_.size(); ++i)
504  return false;
505  return true;
506  }
508  // return checked for type
509  return return_type_ == VOID_T
511  "Returned expression does not match return type",
512  error_msgs_);
513  }
514 
516  const statement& statement,
517  std::ostream& error_msgs) {
518  if (return_type == VOID_T)
519  return true;
520  returns_type_vis vis(return_type,error_msgs);
521  return boost::apply_visitor(vis,statement.statement_);
522  }
523 
524 
525 
527  statements::statements(const std::vector<var_decl>& local_decl,
528  const std::vector<statement>& stmts)
529  : local_decl_(local_decl),
530  statements_(stmts) {
531  }
532 
534  return expr_type();
535  }
537  return e.type_;
538  }
540  return e.type_;
541  }
543  return e.type_;
544  }
546  return e.type_;
547  }
549  return expr_type(DOUBLE_T,2);
550  }
552  return e.type_;
553  }
555  return e.type_;
556  }
558  return e.type_;
559  }
561  return e.type_;
562  }
563 
565  : expr_(nil()) {
566  }
568  : expr_(e.expr_) {
569  }
572  return boost::apply_visitor(vis,expr_);
573  }
574  // template <typename Expr>
575  // expression::expression(const Expr& expr) : expr_(expr) { }
576 
577  expression::expression(const expression_t& expr) : expr_(expr) { }
578  expression::expression(const nil& expr) : expr_(expr) { }
579  expression::expression(const int_literal& expr) : expr_(expr) { }
580  expression::expression(const double_literal& expr) : expr_(expr) { }
581  expression::expression(const array_literal& expr) : expr_(expr) { }
582  expression::expression(const variable& expr) : expr_(expr) { }
583  expression::expression(const integrate_ode& expr) : expr_(expr) { }
584  expression::expression(const fun& expr) : expr_(expr) { }
585  expression::expression(const index_op& expr) : expr_(expr) { }
586  expression::expression(const binary_op& expr) : expr_(expr) { }
587  expression::expression(const unary_op& expr) : expr_(expr) { }
588 
589  printable::printable() : printable_("") { }
590  printable::printable(const expression& expr) : printable_(expr) { }
591  printable::printable(const std::string& msg) : printable_(msg) { }
593  : printable_(printable) { }
595  : printable_(printable.printable_) { }
596 
598  : var_map_(var_map) {
599  }
600  bool contains_var::operator()(const nil& e) const {
601  return false;
602  }
604  return false;
605  }
607  return false;
608  }
610  for (size_t i = 0; i < e.args_.size(); ++i)
611  if (boost::apply_visitor(*this,e.args_[i].expr_))
612  return true;
613  return false;
614  }
615  bool contains_var::operator()(const variable& e) const {
617  return ( vo == parameter_origin
619  || vo == local_origin );
620  }
621  bool contains_var::operator()(const fun& e) const {
622  for (size_t i = 0; i < e.args_.size(); ++i)
623  if (boost::apply_visitor(*this,e.args_[i].expr_))
624  return true;
625  return false;
626  }
628  // only init state and params may contain vars
629  return boost::apply_visitor(*this, e.y0_.expr_)
630  || boost::apply_visitor(*this, e.theta_.expr_)
631  ;
632  }
633  bool contains_var::operator()(const index_op& e) const {
634  return boost::apply_visitor(*this,e.expr_.expr_);
635  }
636  bool contains_var::operator()(const binary_op& e) const {
637  return boost::apply_visitor(*this,e.left.expr_)
638  || boost::apply_visitor(*this,e.right.expr_);
639  }
640  bool contains_var::operator()(const unary_op& e) const {
641  return boost::apply_visitor(*this,e.subject.expr_);
642  }
643 
644  bool is_linear_function(const std::string& name) {
645  return name == "add"
646  || name == "block"
647  || name == "append_col"
648  || name == "col"
649  || name == "cols"
650  || name == "diagonal"
651  || name == "head"
652  || name == "minus"
653  || name == "negative_infinity"
654  || name == "not_a_number"
655  || name == "append_row"
656  || name == "rep_matrix"
657  || name == "rep_row_vector"
658  || name == "rep_vector"
659  || name == "row"
660  || name == "rows"
661  || name == "positive_infinity"
662  || name == "segment"
663  || name == "subtract"
664  || name == "sum"
665  || name == "tail"
666  || name == "to_vector"
667  || name == "to_row_vector"
668  || name == "to_matrix"
669  || name == "to_array_1d"
670  || name == "to_array_2d"
671  || name == "transpose"
672  ;
673  }
674 
675  bool has_var(const expression& e,
676  const variable_map& var_map) {
677  contains_var vis(var_map);
678  return boost::apply_visitor(vis,e.expr_);
679  }
680 
682  : var_map_(var_map) {
683  }
685  return false;
686  }
688  return false;
689  }
691  return false;
692  }
694  for (size_t i = 0; i < e.args_.size(); ++i)
695  if (boost::apply_visitor(*this,e.args_[i].expr_))
696  return true;
697  return false;
698  }
701  return ( vo == transformed_parameter_origin
702  || vo == local_origin );
703  }
705  // if any vars, return true because integration will be nonlinear
706  return boost::apply_visitor(*this, e.y0_.expr_)
707  || boost::apply_visitor(*this, e.theta_.expr_)
708  ;
709  }
711  // any function applied to non-linearly transformed var
712  for (size_t i = 0; i < e.args_.size(); ++i)
713  if (boost::apply_visitor(*this,e.args_[i].expr_))
714  return true;
715  // non-linear function applied to var
716  if (!is_linear_function(e.name_)) {
717  for (size_t i = 0; i < e.args_.size(); ++i)
718  if (has_var(e.args_[i],var_map_))
719  return true;
720  }
721  return false;
722  }
724  return boost::apply_visitor(*this,e.expr_.expr_);
725  }
727  if (e.op == "||"
728  || e.op == "&&"
729  || e.op == "=="
730  || e.op == "!="
731  || e.op == "<"
732  || e.op == "<="
733  || e.op == ">"
734  || e.op == ">=")
735  return true;
738  return true;
739  if (e.op == "*" || e.op == "/")
740  return has_var(e.left,var_map_) && has_var(e.right,var_map_);
741  return false;
742  }
744  // only negation, which is linear, so recurse
746  }
747 
749  const variable_map& var_map) {
750  contains_nonparam_var vis(var_map);
751  return boost::apply_visitor(vis,e.expr_);
752  }
753 
754  bool is_nil_op::operator()(const nil& /*x*/) const { return true; }
755  bool is_nil_op::operator()(const int_literal& /*x*/) const { return false; }
756  bool is_nil_op::operator()(const double_literal& /* x */) const { return false; }
757  bool is_nil_op::operator()(const array_literal& /* x */) const { return false; }
758  bool is_nil_op::operator()(const variable& /* x */) const { return false; }
759  bool is_nil_op::operator()(const integrate_ode& /* x */) const { return false; }
760  bool is_nil_op::operator()(const fun& /* x */) const { return false; }
761  bool is_nil_op::operator()(const index_op& /* x */) const { return false; }
762  bool is_nil_op::operator()(const binary_op& /* x */) const { return false; }
763  bool is_nil_op::operator()(const unary_op& /* x */) const { return false; }
764 
765  // template <typename T>
766  // bool is_nil_op::operator()(const T& /* x */) const { return false; }
767 
768  bool is_nil(const expression& e) {
769  is_nil_op ino;
770  return boost::apply_visitor(ino,e.expr_);
771  }
772 
773  variable_dims::variable_dims() { } // req for FUSION_ADAPT
774  variable_dims::variable_dims(std::string const& name,
775  std::vector<expression> const& dims)
776  : name_(name),
777  dims_(dims) {
778  }
779 
780 
782  : type_(INT_T) {
783  }
785  : val_(val),
786  type_(INT_T) {
787  }
789  : val_(il.val_),
790  type_(il.type_) {
791  }
793  val_ = il.val_;
794  type_ = il.type_;
795  return *this;
796  }
797 
798 
800  : type_(DOUBLE_T,0U) {
801  }
803  : val_(val),
804  type_(DOUBLE_T,0U) {
805  }
807  val_ = dl.val_;
808  type_ = dl.type_;
809  return *this;
810  }
811 
812 
814  : args_(),
815  type_(DOUBLE_T,1U) {
816  }
817  array_literal::array_literal(const std::vector<expression>& args)
818  : args_(args),
819  type_() { // ill-formed w/o help
820  }
822  args_ = al.args_;
823  type_ = al.type_;
824  return *this;
825  }
826 
828  variable::variable(std::string name) : name_(name) { }
829  void variable::set_type(const base_expr_type& base_type,
830  size_t num_dims) {
831  type_ = expr_type(base_type, num_dims);
832  }
833 
834 
836  integrate_ode::integrate_ode(const std::string& system_function_name,
837  const expression& y0,
838  const expression& t0,
839  const expression& ts,
840  const expression& theta,
841  const expression& x,
842  const expression& x_int)
843  : system_function_name_(system_function_name),
844  y0_(y0),
845  t0_(t0),
846  ts_(ts),
847  theta_(theta),
848  x_(x),
849  x_int_(x_int) {
850  }
851 
852 
853  fun::fun() { }
854  fun::fun(std::string const& name,
855  std::vector<expression> const& args)
856  : name_(name),
857  args_(args) {
858  infer_type();
859  }
861  // FIXME: remove this useless function and any calls to it
862  }
863 
864 
865  size_t total_dims(const std::vector<std::vector<expression> >& dimss) {
866  size_t total = 0U;
867  for (size_t i = 0; i < dimss.size(); ++i)
868  total += dimss[i].size();
869  return total;
870  }
871 
872 
874  size_t num_expr_dims,
875  size_t num_index_dims) {
876  if (num_index_dims <= num_expr_dims)
877  return expr_type(expr_base_type,num_expr_dims - num_index_dims);
878  if (num_index_dims == (num_expr_dims + 1)) {
879  if (expr_base_type == VECTOR_T || expr_base_type == ROW_VECTOR_T)
880  return expr_type(DOUBLE_T,0U);
881  if (expr_base_type == MATRIX_T)
882  return expr_type(ROW_VECTOR_T,0U);
883  }
884  if (num_index_dims == (num_expr_dims + 2))
885  if (expr_base_type == MATRIX_T)
886  return expr_type(DOUBLE_T,0U);
887 
888  // error condition, result expr_type has is_ill_formed() = true
889  return expr_type();
890  }
891 
893  size_t num_index_dims) {
895  expr.expression_type().num_dims(),
896  num_index_dims);
897  }
898 
899 
902  const std::vector<std::vector<expression> >& dimss)
903  : expr_(expr),
904  dimss_(dimss) {
905  infer_type();
906  }
909  }
910 
913  const std::string& op,
914  const expression& right)
915  : op(op),
916  left(left),
917  right(right),
918  type_(promote_primitive(left.expression_type(),
919  right.expression_type())) {
920  }
921 
922 
924  expression const& subject)
925  : op(op),
926  subject(subject),
927  type_(promote_primitive(subject.expression_type())) {
928  }
929 
930 
933  expression const& high)
934  : low_(low),
935  high_(high) {
936  }
937  bool range::has_low() const {
938  return !is_nil(low_.expr_);
939  }
940  bool range::has_high() const {
941  return !is_nil(high_.expr_);
942  }
943 
944  void print_var_origin(std::ostream& o, const var_origin& vo) {
945  if (vo == model_name_origin)
946  o << "model name";
947  else if (vo == data_origin)
948  o << "data";
949  else if (vo == transformed_data_origin)
950  o << "transformed data";
951  else if (vo == parameter_origin)
952  o << "parameter";
953  else if (vo == transformed_parameter_origin)
954  o << "transformed parameter";
955  else if (vo == derived_origin)
956  o << "generated quantities";
957  else if (vo == local_origin)
958  o << "local";
959  else if (vo == function_argument_origin)
960  o << "function argument";
961  else if (vo == function_argument_origin_lp)
962  o << "function argument '_lp' suffixed";
963  else if (vo == function_argument_origin_rng)
964  o << "function argument '_rng' suffixed";
965  else if (vo == void_function_argument_origin)
966  o << "void function argument";
967  else if (vo == void_function_argument_origin_lp)
968  o << "void function argument '_lp' suffixed";
969  else if (vo == void_function_argument_origin_rng)
970  o << "void function argument '_rng' suffixed";
971  else
972  o << "UNKNOWN ORIGIN=" << vo;
973  }
974 
975 
978  : base_type_(base_type) {
979  }
980  base_var_decl::base_var_decl(const std::string& name,
981  const std::vector<expression>& dims,
982  const base_expr_type& base_type)
983  : name_(name),
984  dims_(dims),
985  base_type_(base_type) {
986  }
987 
988  bool variable_map::exists(const std::string& name) const {
989  return map_.find(name) != map_.end();
990  }
991  base_var_decl variable_map::get(const std::string& name) const {
992  if (!exists(name))
993  throw std::invalid_argument("variable does not exist");
994  return map_.find(name)->second.first;
995  }
996  base_expr_type variable_map::get_base_type(const std::string& name) const {
997  return get(name).base_type_;
998  }
999  size_t variable_map::get_num_dims(const std::string& name) const {
1000  return get(name).dims_.size();
1001  }
1002  var_origin variable_map::get_origin(const std::string& name) const {
1003  if (!exists(name))
1004  throw std::invalid_argument("variable does not exist");
1005  return map_.find(name)->second.second;
1006  }
1007  void variable_map::add(const std::string& name,
1008  const base_var_decl& base_decl,
1009  const var_origin& vo) {
1010  map_[name] = range_t(base_decl,vo);
1011  }
1012  void variable_map::remove(const std::string& name) {
1013  map_.erase(name);
1014  }
1015 
1017  : base_var_decl(INT_T)
1018  { }
1019 
1021  std::string const& name,
1022  std::vector<expression> const& dims)
1023  : base_var_decl(name,dims,INT_T),
1024  range_(range)
1025  { }
1026 
1027 
1028 
1031  { }
1032 
1034  std::string const& name,
1035  std::vector<expression> const& dims)
1036  : base_var_decl(name,dims,DOUBLE_T),
1037  range_(range)
1038  { }
1039 
1042  { }
1043 
1045  std::string const& name,
1046  std::vector<expression> const& dims)
1047  : base_var_decl(name,dims,VECTOR_T),
1048  K_(K)
1049  { }
1050 
1053  { }
1054 
1056  std::string const& name,
1057  std::vector<expression> const& dims)
1058  : base_var_decl(name,dims,VECTOR_T),
1059  K_(K)
1060  { }
1061 
1064  { }
1065 
1067  std::string const& name,
1068  std::vector<expression> const& dims)
1069  : base_var_decl(name,dims,VECTOR_T),
1070  K_(K) {
1071  }
1072 
1075  { }
1076 
1078  std::string const& name,
1079  std::vector<expression> const& dims)
1080  : base_var_decl(name,dims,VECTOR_T),
1081  K_(K) {
1082  }
1083 
1085 
1087  expression const& M,
1088  std::string const& name,
1089  std::vector<expression> const& dims)
1090  : base_var_decl(name,dims,VECTOR_T),
1091  range_(range),
1092  M_(M) {
1093  }
1094 
1097  expression const& N,
1098  std::string const& name,
1099  std::vector<expression> const& dims)
1100  : base_var_decl(name,dims,ROW_VECTOR_T),
1101  range_(range),
1102  N_(N) {
1103  }
1104 
1107  expression const& M,
1108  expression const& N,
1109  std::string const& name,
1110  std::vector<expression> const& dims)
1111  : base_var_decl(name,dims,MATRIX_T),
1112  range_(range),
1113  M_(M),
1114  N_(N) {
1115  }
1116 
1117 
1119  : base_var_decl(MATRIX_T) {
1120  }
1122  expression const& N,
1123  std::string const& name,
1124  std::vector<expression> const& dims)
1125  : base_var_decl(name,dims,MATRIX_T),
1126  M_(M),
1127  N_(N) {
1128  }
1129 
1131  : base_var_decl(MATRIX_T) {
1132  }
1134  std::string const& name,
1135  std::vector<expression> const& dims)
1136  : base_var_decl(name,dims,MATRIX_T),
1137  K_(K) {
1138  }
1139 
1141  }
1143  std::string const& name,
1144  std::vector<expression> const& dims)
1145  : base_var_decl(name,dims,MATRIX_T),
1146  K_(K) {
1147  }
1148 
1151  std::string const& name,
1152  std::vector<expression> const& dims)
1153  : base_var_decl(name,dims,MATRIX_T),
1154  K_(K) {
1155  }
1156 
1157 
1158 
1159 
1161  std::string name_vis::operator()(const nil& /* x */) const {
1162  return ""; // fail if arises
1163  }
1164  std::string name_vis::operator()(const int_var_decl& x) const {
1165  return x.name_;
1166  }
1167  std::string name_vis::operator()(const double_var_decl& x) const {
1168  return x.name_;
1169  }
1170  std::string name_vis::operator()(const vector_var_decl& x) const {
1171  return x.name_;
1172  }
1173  std::string name_vis::operator()(const row_vector_var_decl& x) const {
1174  return x.name_;
1175  }
1176  std::string name_vis::operator()(const matrix_var_decl& x) const {
1177  return x.name_;
1178  }
1179  std::string name_vis::operator()(const unit_vector_var_decl& x) const {
1180  return x.name_;
1181  }
1182  std::string name_vis::operator()(const simplex_var_decl& x) const {
1183  return x.name_;
1184  }
1185  std::string name_vis::operator()(const ordered_var_decl& x) const {
1186  return x.name_;
1187  }
1188  std::string name_vis::operator()(const positive_ordered_var_decl& x) const {
1189  return x.name_;
1190  }
1191  std::string name_vis::operator()(const cholesky_factor_var_decl& x) const {
1192  return x.name_;
1193  }
1194  std::string name_vis::operator()(const cholesky_corr_var_decl& x) const {
1195  return x.name_;
1196  }
1197  std::string name_vis::operator()(const cov_matrix_var_decl& x) const {
1198  return x.name_;
1199  }
1200  std::string name_vis::operator()(const corr_matrix_var_decl& x) const {
1201  return x.name_;
1202  }
1203 
1204 
1205  // can't template out in .cpp file
1206 
1207  var_decl::var_decl(const var_decl_t& decl) : decl_(decl) { }
1208  var_decl::var_decl() : decl_(nil()) { }
1209  var_decl::var_decl(const nil& decl) : decl_(decl) { }
1210  var_decl::var_decl(const int_var_decl& decl) : decl_(decl) { }
1211  var_decl::var_decl(const double_var_decl& decl) : decl_(decl) { }
1212  var_decl::var_decl(const vector_var_decl& decl) : decl_(decl) { }
1213  var_decl::var_decl(const row_vector_var_decl& decl) : decl_(decl) { }
1214  var_decl::var_decl(const matrix_var_decl& decl) : decl_(decl) { }
1215  var_decl::var_decl(const unit_vector_var_decl& decl) : decl_(decl) { }
1216  var_decl::var_decl(const simplex_var_decl& decl) : decl_(decl) { }
1217  var_decl::var_decl(const ordered_var_decl& decl) : decl_(decl) { }
1218  var_decl::var_decl(const positive_ordered_var_decl& decl) : decl_(decl) { }
1219  var_decl::var_decl(const cholesky_factor_var_decl& decl) : decl_(decl) { }
1220  var_decl::var_decl(const cholesky_corr_var_decl& decl) : decl_(decl) { }
1221  var_decl::var_decl(const cov_matrix_var_decl& decl) : decl_(decl) { }
1222  var_decl::var_decl(const corr_matrix_var_decl& decl) : decl_(decl) { }
1223 
1224  std::string var_decl::name() const {
1225  return boost::apply_visitor(name_vis(),decl_);
1226  }
1227 
1228  statement::statement() : statement_(nil()) { }
1229 
1230  statement::statement(const statement_t& st) : statement_(st) { }
1231  statement::statement(const nil& st) : statement_(st) { }
1232  statement::statement(const assignment& st) : statement_(st) { }
1233  statement::statement(const sample& st) : statement_(st) { }
1234  statement::statement(const increment_log_prob_statement& st) : statement_(st) { }
1235  statement::statement(const statements& st) : statement_(st) { }
1236  statement::statement(const expression& st) : statement_(st) { }
1237  statement::statement(const for_statement& st) : statement_(st) { }
1238  statement::statement(const while_statement& st) : statement_(st) { }
1239  statement::statement(const conditional_statement& st) : statement_(st) { }
1240  statement::statement(const print_statement& st) : statement_(st) { }
1241  statement::statement(const reject_statement& st) : statement_(st) { }
1242  statement::statement(const return_statement& st) : statement_(st) { }
1243  statement::statement(const no_op_statement& st) : statement_(st) { }
1244 
1245 
1246  bool is_no_op_statement_vis::operator()(const nil& st) const {
1247  return false;
1248  }
1250  return false;
1251  }
1253  return false;
1254  }
1256  return false;
1257  }
1259  return false;
1260  }
1262  return false;
1263  }
1265  return false;
1266  }
1268  return false;
1269  }
1271  return false;
1272  }
1274  return false;
1275  }
1277  return false;
1278  }
1280  return true;
1281  }
1283  return false;
1284  }
1285 
1288  return boost::apply_visitor(vis,statement_);
1289  }
1290 
1292  }
1294  : log_prob_(log_prob) {
1295  }
1296 
1298  }
1300  range& range,
1301  statement& stmt)
1302  : variable_(variable),
1303  range_(range),
1304  statement_(stmt) {
1305  }
1306 
1308  }
1310  const statement& body)
1311  : condition_(condition),
1312  body_(body) {
1313  }
1314 
1316  }
1318  ::conditional_statement(const std::vector<expression>& conditions,
1319  const std::vector<statement>& bodies)
1320  : conditions_(conditions),
1321  bodies_(bodies) {
1322  }
1323 
1326  : return_value_(expr) {
1327  }
1328 
1330 
1331  print_statement::print_statement(const std::vector<printable>& printables)
1332  : printables_(printables) {
1333  }
1334 
1336 
1337  reject_statement::reject_statement(const std::vector<printable>& printables)
1338  : printables_(printables) {
1339  }
1340 
1342  program::program(const std::vector<function_decl_def>& function_decl_defs,
1343  const std::vector<var_decl>& data_decl,
1344  const std::pair<std::vector<var_decl>,
1345  std::vector<statement> >& derived_data_decl,
1346  const std::vector<var_decl>& parameter_decl,
1347  const std::pair<std::vector<var_decl>,
1348  std::vector<statement> >& derived_decl,
1349  const statement& st,
1350  const std::pair<std::vector<var_decl>,
1351  std::vector<statement> >& generated_decl)
1352  : function_decl_defs_(function_decl_defs),
1353  data_decl_(data_decl),
1354  derived_data_decl_(derived_data_decl),
1355  parameter_decl_(parameter_decl),
1356  derived_decl_(derived_decl),
1357  statement_(st),
1358  generated_decl_(generated_decl) {
1359  }
1360 
1362  }
1364  distribution& dist)
1365  : expr_(e),
1366  dist_(dist) {
1367  }
1368  bool sample::is_ill_formed() const {
1370  || ( truncation_.has_low()
1371  && expr_.expression_type()
1373  || ( truncation_.has_high()
1374  && expr_.expression_type()
1376  }
1377 
1379  }
1381  expression& expr)
1382  : var_dims_(var_dims),
1383  expr_(expr) {
1384  }
1385 
1387  expr_ = binary_op(expr_, "+", rhs);
1388  return *this;
1389  }
1390 
1392  expr_ = binary_op(expr_, "-", rhs);
1393  return *this;
1394  }
1395 
1397  expr_ = binary_op(expr_, "*", rhs);
1398  return *this;
1399  }
1400 
1402  expr_ = binary_op(expr_, "/", rhs);
1403  return *this;
1404  }
1405 
1406  bool has_rng_suffix(const std::string& s) {
1407  int n = s.size();
1408  return n > 4
1409  && s[n-1] == 'g'
1410  && s[n-2] == 'n'
1411  && s[n-3] == 'r'
1412  && s[n-4] == '_';
1413  }
1414 
1415  bool has_lp_suffix(const std::string& s) {
1416  int n = s.size();
1417  return n > 3
1418  && s[n-1] == 'p'
1419  && s[n-2] == 'l'
1420  && s[n-3] == '_';
1421  }
1422 
1423  bool is_user_defined(const std::string& name,
1424  const std::vector<expression>& args) {
1425  std::vector<expr_type> arg_types;
1426  for (size_t i = 0; i < args.size(); ++i)
1427  arg_types.push_back(args[i].expression_type());
1429  int matches
1431  .get_signature_matches(name,arg_types,sig);
1432  if (matches != 1)
1433  return false; // reall shouldn't come up; throw instead?
1434  std::pair<std::string, function_signature_t>
1435  name_sig(name, sig);
1437  }
1438 
1439  bool is_user_defined_prob_function(const std::string& name,
1440  const expression& variate,
1441  const std::vector<expression>& params) {
1442  std::vector<expression> variate_params;
1443  variate_params.push_back(variate);
1444  for (size_t i = 0; i < params.size(); ++i)
1445  variate_params.push_back(params[i]);
1446  return is_user_defined(name,variate_params);
1447  }
1448 
1449  bool is_user_defined(const fun& fx) {
1450  return is_user_defined(fx.name_, fx.args_);
1451  }
1452 
1453  bool is_assignable(const expr_type& l_type,
1454  const expr_type& r_type,
1455  const std::string& failure_message,
1456  std::ostream& error_msgs) {
1457  bool assignable = true;
1458  if (l_type.num_dims_ != r_type.num_dims_) {
1459  assignable = false;
1460  error_msgs << "Mismatched array dimensions.";
1461  }
1462  if (l_type.base_type_ != r_type.base_type_
1463  && (! (l_type.base_type_ == DOUBLE_T && r_type.base_type_ == INT_T))) {
1464  assignable = false;
1465  error_msgs << "Base type mismatch. ";
1466  }
1467  if (!assignable)
1468  error_msgs << failure_message
1469  << std::endl
1470  << " LHS type = " << l_type
1471  << "; RHS type = " << r_type
1472  << std::endl;
1473  return assignable;
1474  }
1475 
1476 
1477  bool ends_with(const std::string& suffix,
1478  const std::string& s) {
1479  size_t idx = s.rfind(suffix);
1480  return idx != std::string::npos
1481  && idx == (s.size() - suffix.size());
1482  }
1483 
1484  }
1485 }
1486 
1487 
1488 #endif
bool operator==(const expr_type &et) const
Definition: ast_def.cpp:66
Placeholder struct for boost::variant default ctors.
Definition: ast.hpp:18
expression & operator/=(const expression &rhs)
Definition: ast_def.cpp:1401
bool is_user_defined(const std::pair< std::string, function_signature_t > &name_sig)
Definition: ast_def.cpp:157
bool is_no_op_statement() const
Definition: ast_def.cpp:1286
void add_unary(const ::std::string &name)
Definition: ast_def.cpp:276
bool ends_with(const std::string &suffix, const std::string &s)
Definition: ast_def.cpp:1477
int N_
std::vector< expression > conditions_
Definition: ast.hpp:730
std::string name_
Definition: ast.hpp:351
void add(const std::string &name, const expr_type &result_type, const std::vector< expr_type > &arg_types)
Definition: ast_def.cpp:171
expression right
Definition: ast.hpp:385
base_expr_type base_type_
Definition: ast.hpp:75
const int data_origin
Definition: ast.hpp:413
void add_nullary(const ::std::string &name)
Definition: ast_def.cpp:273
const int parameter_origin
Definition: ast.hpp:415
void add(const std::string &name, const base_var_decl &base_decl, const var_origin &vo)
Definition: ast_def.cpp:1007
expr_type type_
Definition: ast.hpp:353
bool is_user_defined_prob_function(const std::string &name, const expression &variate, const std::vector< expression > &params)
Definition: ast_def.cpp:1439
bool is_defined(const std::string &name, const function_signature_t &sig)
Definition: ast_def.cpp:163
const int DOUBLE_T
Definition: ast.hpp:66
std::string name_
Definition: ast.hpp:769
static function_signatures & instance()
Definition: ast_def.cpp:143
expression y0_
Definition: ast.hpp:334
void infer_type()
Definition: ast_def.cpp:860
const int local_origin
Definition: ast.hpp:418
std::map< std::string, range_t > map_
Definition: ast.hpp:452
boost::variant< boost::recursive_wrapper< std::string >, boost::recursive_wrapper< expression > > printable_t
Definition: ast.hpp:259
expr_type type_
Definition: ast.hpp:396
std::vector< std::vector< expression > > dimss_
Definition: ast.hpp:372
bool operator>=(const expr_type &et) const
Definition: ast_def.cpp:88
bool is_primitive_int() const
Definition: ast_def.cpp:97
expr_type type_
Definition: ast.hpp:386
void set_user_defined(const std::pair< std::string, function_signature_t > &name_sig)
Definition: ast_def.cpp:150
bool has_low() const
Definition: ast_def.cpp:937
expression high_
Definition: ast.hpp:403
bool is_primitive_double() const
Definition: ast_def.cpp:101
const int ROW_VECTOR_T
Definition: ast.hpp:68
void print_var_origin(std::ostream &o, const var_origin &vo)
Definition: ast_def.cpp:944
expr_type return_type_
Definition: ast.hpp:689
const int function_argument_origin
Definition: ast.hpp:419
bool is_user_defined(const std::string &name, const std::vector< expression > &args)
Definition: ast_def.cpp:1423
expression & operator-=(const expression &rhs)
Definition: ast_def.cpp:1391
double max(const double a, const double b)
Definition: max.hpp:7
Metaprogram to calculate the base scalar return type resulting from promoting all the scalar types of...
Definition: traits.hpp:398
int get_signature_matches(const std::string &name, const std::vector< expr_type > &args, function_signature_t &signature)
Definition: ast_def.cpp:307
expression & operator*=(const expression &rhs)
Definition: ast_def.cpp:1396
size_t total_dims(const std::vector< std::vector< expression > > &dimss)
Definition: ast_def.cpp:865
const int void_function_argument_origin_rng
Definition: ast.hpp:424
expression return_value_
Definition: ast.hpp:758
const int function_argument_origin_rng
Definition: ast.hpp:421
bool has_rng_suffix(const std::string &s)
Definition: ast_def.cpp:1406
std::vector< expression > args_
Definition: ast.hpp:352
void add_binary(const ::std::string &name)
Definition: ast_def.cpp:279
bool operator()(const nil &e) const
Definition: ast_def.cpp:600
bool is_ill_formed() const
Definition: ast_def.cpp:1368
expression expr_
Definition: ast.hpp:819
size_t get_num_dims(const std::string &name) const
Definition: ast_def.cpp:999
std::vector< expression > args_
Definition: ast.hpp:316
expression_t expr_
Definition: ast.hpp:252
std::ostream & error_msgs_
Definition: ast.hpp:690
size_t num_dims_
Definition: ast.hpp:76
base_expr_type type() const
Definition: ast_def.cpp:111
bool has_non_param_var(const expression &e, const variable_map &var_map)
Definition: ast_def.cpp:748
expression subject
Definition: ast.hpp:395
const int derived_origin
Definition: ast.hpp:417
std::ostream & operator<<(std::ostream &o, const expr_type &et)
Definition: ast_def.cpp:118
std::vector< statement > bodies_
Definition: ast.hpp:731
expr_type type_
Definition: ast.hpp:299
bool is_linear_function(const std::string &name)
Definition: ast_def.cpp:644
expression theta_
Definition: ast.hpp:337
int num_promotions(const std::vector< expr_type > &call_args, const std::vector< expr_type > &sig_args)
Definition: ast_def.cpp:288
const int VECTOR_T
Definition: ast.hpp:67
const int MATRIX_T
Definition: ast.hpp:69
expr_type promote_primitive(const expr_type &et)
Definition: ast_def.cpp:125
expr_type type_
Definition: ast.hpp:373
void dims(const T &x, std::vector< int > &result)
Definition: dims.hpp:13
returns_type_vis(const expr_type &return_type, std::ostream &error_msgs)
Definition: ast_def.cpp:428
expression low_
Definition: ast.hpp:402
std::string name() const
Definition: ast_def.cpp:1224
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< fun >, boost::recursive_wrapper< index_op >, boost::recursive_wrapper< binary_op >, boost::recursive_wrapper< unary_op > > expression_t
Definition: ast.hpp:227
contains_nonparam_var(const variable_map &var_map)
Definition: ast_def.cpp:681
const int void_function_argument_origin_lp
Definition: ast.hpp:423
size_t num_dims() const
Definition: ast_def.cpp:114
statement statement_
Definition: ast.hpp:721
bool has_var(const expression &e, const variable_map &var_map)
Definition: ast_def.cpp:675
expression left
Definition: ast.hpp:384
expression expr_
Definition: ast.hpp:371
std::string op
Definition: ast.hpp:383
expr_type type_
Definition: ast.hpp:325
std::string name_
Definition: ast.hpp:429
const int void_function_argument_origin
Definition: ast.hpp:422
void add_quaternary(const ::std::string &name)
Definition: ast_def.cpp:285
const int model_name_origin
Definition: ast.hpp:412
bool has_lp_suffix(const std::string &s)
Definition: ast_def.cpp:1415
boost::variant< boost::recursive_wrapper< nil >, boost::recursive_wrapper< assignment >, 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:648
array_literal & operator=(const array_literal &al)
Definition: ast_def.cpp:821
std::set< std::string > key_set() const
Definition: ast_def.cpp:380
contains_var(const variable_map &var_map)
Definition: ast_def.cpp:597
base_expr_type get_base_type(const std::string &name) const
Definition: ast_def.cpp:996
bool operator()(const nil &e) const
Definition: ast_def.cpp:684
const int transformed_data_origin
Definition: ast.hpp:414
int base_expr_type
Definition: ast.hpp:60
bool is_nil(const expression &e)
Definition: ast_def.cpp:768
statement_t statement_
Definition: ast.hpp:650
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:86
var_decl_t decl_
Definition: ast.hpp:609
bool operator!=(const expr_type &et) const
Definition: ast_def.cpp:70
range truncation_
Definition: ast.hpp:821
int size(const std::vector< T > &x)
Definition: size.hpp:11
bool is_ill_formed() const
Definition: ast_def.cpp:105
void remove(const std::string &name)
Definition: ast_def.cpp:1012
base_var_decl base_variable_declaration()
Definition: ast_def.cpp:403
bool has_high() const
Definition: ast_def.cpp:940
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:1453
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:873
bool operator()(const nil &x) const
Definition: ast_def.cpp:754
const int function_argument_origin_lp
Definition: ast.hpp:420
const int INT_T
Definition: ast.hpp:65
bool operator>(const expr_type &et) const
Definition: ast_def.cpp:83
unary_op(char op, expression const &subject)
Definition: ast_def.cpp:923
expression & operator+=(const expression &rhs)
Definition: ast_def.cpp:1386
expr_type operator()(const nil &e) const
Definition: ast_def.cpp:533
const int transformed_parameter_origin
Definition: ast.hpp:416
bool operator<(const expr_type &et) const
Definition: ast_def.cpp:73
bool operator<=(const expr_type &et) const
Definition: ast_def.cpp:78
double_literal & operator=(const double_literal &dl)
Definition: ast_def.cpp:806
std::pair< base_var_decl, var_origin > range_t
Definition: ast.hpp:440
std::string operator()(const nil &x) const
Definition: ast_def.cpp:1161
expr_type get_result_type(const std::string &name, const std::vector< expr_type > &args, std::ostream &error_msgs)
Definition: ast_def.cpp:329
std::string name_
Definition: ast.hpp:324
double dist(const std::vector< double > &x, const std::vector< double > &y)
Definition: dist.hpp:11
bool exists(const std::string &name) const
Definition: ast_def.cpp:988
std::ostream & write_base_expr_type(std::ostream &o, base_expr_type type)
Definition: ast_def.cpp:23
expr_type expression_type() const
Definition: ast_def.cpp:570
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:607
base_var_decl get(const std::string &name) const
Definition: ast_def.cpp:991
bool returns_type(const expr_type &return_type, const statement &statement, std::ostream &error_msgs)
Definition: ast_def.cpp:515
void set_type(const base_expr_type &base_type, size_t num_dims)
Definition: ast_def.cpp:829
const variable_map & var_map_
Definition: ast.hpp:870
int_literal & operator=(const int_literal &il)
Definition: ast_def.cpp:792
expr_type arg_type_
Definition: ast.hpp:768
int var_origin
Definition: ast.hpp:411
bool operator()(const nil &st) const
Definition: ast_def.cpp:433
void add_ternary(const ::std::string &name)
Definition: ast_def.cpp:282
std::vector< statement > statements_
Definition: ast.hpp:187
bool is_primitive() const
Definition: ast_def.cpp:93
var_origin get_origin(const std::string &name) const
Definition: ast_def.cpp:1002
const variable_map & var_map_
Definition: ast.hpp:851
const int ILL_FORMED_T
Definition: ast.hpp:70
bool is_void() const
Definition: ast_def.cpp:108
int M_
const int VOID_T
Definition: ast.hpp:64
bool operator()(const nil &st) const
Definition: ast_def.cpp:1246
std::pair< expr_type, std::vector< expr_type > > function_signature_t
Definition: ast.hpp:104

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