Stan  1.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs 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  default:
44  o << "UNKNOWN";
45  }
46  return o;
47  }
48 
49  // expr_type ctors and methods
51  : base_type_(ILL_FORMED_T),
52  num_dims_(0) {
53  }
55  : base_type_(base_type),
56  num_dims_(0) {
57  }
59  size_t num_dims)
60  : base_type_(base_type),
61  num_dims_(num_dims) {
62  }
63  bool expr_type::operator==(const expr_type& et) const {
64  return base_type_ == et.base_type_
65  && num_dims_ == et.num_dims_;
66  }
67  bool expr_type::operator!=(const expr_type& et) const {
68  return !(*this == et);
69  }
70  bool expr_type::is_primitive() const {
71  return is_primitive_int()
73  }
75  return base_type_ == INT_T
76  && num_dims_ == 0U;
77  }
79  return base_type_ == DOUBLE_T
80  && num_dims_ == 0U;
81  }
82  bool expr_type::is_ill_formed() const {
83  return base_type_ == ILL_FORMED_T;
84  }
86  return base_type_;
87  }
88  size_t expr_type::num_dims() const {
89  return num_dims_;
90  }
91 
92  std::ostream& operator<<(std::ostream& o, const expr_type& et) {
93  write_base_expr_type(o,et.type());
94  if (et.num_dims() > 0)
95  o << '[' << et.num_dims() << ']';
96  return o;
97  }
98 
100  if (!et.is_primitive())
101  return expr_type();
102  return et;
103  }
104 
106  const expr_type& et2) {
107  if (!et1.is_primitive() || !et2.is_primitive())
108  return expr_type();
109  return et1.type() == DOUBLE_T ? et1 : et2;
110  }
111 
113  // FIXME: for threaded models, requires double-check lock
114  if (!sigs_)
115  sigs_ = new function_signatures;
116  return *sigs_;
117  }
118  void function_signatures::add(const std::string& name,
119  const expr_type& result_type,
120  const std::vector<expr_type>& arg_types) {
121  sigs_map_[name].push_back(function_signature_t(result_type,arg_types));
122 
123  }
124  void function_signatures::add(const std::string& name,
125  const expr_type& result_type) {
126  std::vector<expr_type> arg_types;
127  add(name,result_type,arg_types);
128  }
129  void function_signatures::add(const std::string& name,
130  const expr_type& result_type,
131  const expr_type& arg_type) {
132  std::vector<expr_type> arg_types;
133  arg_types.push_back(arg_type);
134  add(name,result_type,arg_types);
135  }
136  void function_signatures::add(const std::string& name,
137  const expr_type& result_type,
138  const expr_type& arg_type1,
139  const expr_type& arg_type2) {
140  std::vector<expr_type> arg_types;
141  arg_types.push_back(arg_type1);
142  arg_types.push_back(arg_type2);
143  add(name,result_type,arg_types);
144  }
145  void function_signatures::add(const std::string& name,
146  const expr_type& result_type,
147  const expr_type& arg_type1,
148  const expr_type& arg_type2,
149  const expr_type& arg_type3) {
150  std::vector<expr_type> arg_types;
151  arg_types.push_back(arg_type1);
152  arg_types.push_back(arg_type2);
153  arg_types.push_back(arg_type3);
154  add(name,result_type,arg_types);
155  }
156  void function_signatures::add(const std::string& name,
157  const expr_type& result_type,
158  const expr_type& arg_type1,
159  const expr_type& arg_type2,
160  const expr_type& arg_type3,
161  const expr_type& arg_type4) {
162  std::vector<expr_type> arg_types;
163  arg_types.push_back(arg_type1);
164  arg_types.push_back(arg_type2);
165  arg_types.push_back(arg_type3);
166  arg_types.push_back(arg_type4);
167  add(name,result_type,arg_types);
168  }
169  void function_signatures::add(const std::string& name,
170  const expr_type& result_type,
171  const expr_type& arg_type1,
172  const expr_type& arg_type2,
173  const expr_type& arg_type3,
174  const expr_type& arg_type4,
175  const expr_type& arg_type5) {
176  std::vector<expr_type> arg_types;
177  arg_types.push_back(arg_type1);
178  arg_types.push_back(arg_type2);
179  arg_types.push_back(arg_type3);
180  arg_types.push_back(arg_type4);
181  arg_types.push_back(arg_type5);
182  add(name,result_type,arg_types);
183  }
184  void function_signatures::add_nullary(const::std::string& name) {
185  add(name,DOUBLE_T);
186  }
187  void function_signatures::add_unary(const::std::string& name) {
188  add(name,DOUBLE_T,DOUBLE_T);
189  }
190  void function_signatures::add_binary(const::std::string& name) {
192  }
193  void function_signatures::add_ternary(const::std::string& name) {
195  }
196  void function_signatures::add_quaternary(const::std::string& name) {
198  }
200  const std::vector<expr_type>& call_args,
201  const std::vector<expr_type>& sig_args) {
202  if (call_args.size() != sig_args.size()) {
203  return -1; // failure
204  }
205  int num_promotions = 0;
206  for (size_t i = 0; i < call_args.size(); ++i) {
207  if (call_args[i] == sig_args[i]) {
208  continue;
209  } else if (call_args[i].is_primitive_int()
210  && sig_args[i].is_primitive_double()) {
211  ++num_promotions;
212  } else {
213  return -1; // failed match
214  }
215  }
216  return num_promotions;
217  }
219  const std::string& name,
220  const std::vector<expr_type>& args,
221  std::ostream& error_msgs) {
222  std::vector<function_signature_t> signatures = sigs_map_[name];
223  size_t match_index = 0;
224  size_t min_promotions = std::numeric_limits<size_t>::max();
225  size_t num_matches = 0;
226 
227  for (size_t i = 0; i < signatures.size(); ++i) {
228  int promotions = num_promotions(args,signatures[i].second);
229  if (promotions < 0) continue; // no match
230  size_t promotions_ui = static_cast<size_t>(promotions);
231  if (promotions_ui < min_promotions) {
232  min_promotions = promotions_ui;
233  match_index = i;
234  num_matches = 1;
235  } else if (promotions_ui == min_promotions) {
236  ++num_matches;
237  }
238  }
239 
240  if (num_matches == 1) {
241  return signatures[match_index].first;
242  } else if (num_matches == 0) {
243  error_msgs << "no matches for function name=\"" << name << "\""
244  << std::endl;
245  } else {
246  error_msgs << num_matches << " matches with "
247  << min_promotions << " integer promotions "
248  << "for function name=\"" << name << "\"" << std::endl;
249  }
250  for (size_t i = 0; i < args.size(); ++i)
251  error_msgs << " arg " << i << " type=" << args[i] << std::endl;
252  return expr_type(); // ill-formed dummy
253  }
254  function_signatures::function_signatures() {
256  }
257  function_signatures* function_signatures::sigs_ = 0;
258 
259 
260 
262  statements::statements(const std::vector<var_decl>& local_decl,
263  const std::vector<statement>& stmts)
264  : local_decl_(local_decl),
265  statements_(stmts) {
266  }
267 
269  return expr_type();
270  }
271  // template <typename T>
272  // expr_type expression_type_vis::operator()(const T& e) const {
273  // return e.type_;
274  // }
276  return e.type_;
277  }
279  return e.type_;
280  }
282  return e.type_;
283  }
285  return e.type_;
286  }
288  return e.type_;
289  }
291  return e.type_;
292  }
294  return e.type_;
295  }
296 
298  : expr_(nil()) {
299  }
301  : expr_(e.expr_) {
302  }
305  return boost::apply_visitor(vis,expr_);
306  }
307  // template <typename Expr>
308  // expression::expression(const Expr& expr) : expr_(expr) { }
309 
310  expression::expression(const expression_t& expr) : expr_(expr) { }
311  expression::expression(const nil& expr) : expr_(expr) { }
312  expression::expression(const int_literal& expr) : expr_(expr) { }
313  expression::expression(const double_literal& expr) : expr_(expr) { }
314  expression::expression(const variable& expr) : expr_(expr) { }
315  expression::expression(const fun& expr) : expr_(expr) { }
316  expression::expression(const index_op& expr) : expr_(expr) { }
317  expression::expression(const binary_op& expr) : expr_(expr) { }
318  expression::expression(const unary_op& expr) : expr_(expr) { }
319 
320  printable::printable() : printable_("") { }
321  printable::printable(const expression& expr) : printable_(expr) { }
322  printable::printable(const std::string& msg) : printable_(msg) { }
324  : printable_(printable) { }
326  : printable_(printable.printable_) { }
327 
328  bool is_nil_op::operator()(const nil& x) const { return true; }
329  bool is_nil_op::operator()(const int_literal& x) const { return false; }
330  bool is_nil_op::operator()(const double_literal& x) const { return false; }
331  bool is_nil_op::operator()(const variable& x) const { return false; }
332  bool is_nil_op::operator()(const fun& x) const { return false; }
333  bool is_nil_op::operator()(const index_op& x) const { return false; }
334  bool is_nil_op::operator()(const binary_op& x) const { return false; }
335  bool is_nil_op::operator()(const unary_op& x) const { return false; }
336 
337  // template <typename T>
338  // bool is_nil_op::operator()(const T& x) const { return false; }
339 
340  bool is_nil(const expression& e) {
341  is_nil_op ino;
342  return boost::apply_visitor(ino,e.expr_);
343  }
344 
345  variable_dims::variable_dims() { } // req for FUSION_ADAPT
346  variable_dims::variable_dims(std::string const& name,
347  std::vector<expression> const& dims)
348  : name_(name),
349  dims_(dims) {
350  }
351 
352 
354  : type_(INT_T) {
355  }
357  : val_(val),
358  type_(INT_T) {
359  }
361  : val_(il.val_),
362  type_(il.type_) {
363  }
365  val_ = il.val_;
366  type_ = il.type_;
367  return *this;
368  }
369 
370 
372  : type_(DOUBLE_T,0U) {
373  }
375  : val_(val),
376  type_(DOUBLE_T,0U) {
377  }
379  val_ = dl.val_;
380  type_ = dl.type_;
381  return *this;
382  }
383 
384 
386  variable::variable(std::string name) : name_(name) { }
387  void variable::set_type(const base_expr_type& base_type,
388  size_t num_dims) {
389  type_ = expr_type(base_type, num_dims);
390  }
391 
392 
393  fun::fun() { }
394  fun::fun(std::string const& name,
395  std::vector<expression> const& args)
396  : name_(name),
397  args_(args) {
398  infer_type();
399  }
401  // FIXME: remove this useless function and any calls to it
402  }
403 
404 
405  size_t total_dims(const std::vector<std::vector<expression> >& dimss) {
406  size_t total = 0U;
407  for (size_t i = 0; i < dimss.size(); ++i)
408  total += dimss[i].size();
409  return total;
410  }
411 
412 
414  size_t num_expr_dims,
415  size_t num_index_dims) {
416  if (num_index_dims <= num_expr_dims)
417  return expr_type(expr_base_type,num_expr_dims - num_index_dims);
418  if (num_index_dims == (num_expr_dims + 1)) {
419  if (expr_base_type == VECTOR_T || expr_base_type == ROW_VECTOR_T)
420  return expr_type(DOUBLE_T,0U);
421  if (expr_base_type == MATRIX_T)
422  return expr_type(ROW_VECTOR_T,0U);
423  }
424  if (num_index_dims == (num_expr_dims + 2))
425  if (expr_base_type == MATRIX_T)
426  return expr_type(DOUBLE_T,0U);
427 
428  // error condition, result expr_type has is_ill_formed() = true
429  return expr_type();
430  }
431 
433  size_t num_index_dims) {
435  expr.expression_type().num_dims(),
436  num_index_dims);
437  }
438 
439 
442  const std::vector<std::vector<expression> >& dimss)
443  : expr_(expr),
444  dimss_(dimss) {
445  infer_type();
446  }
449  }
450 
453  char op,
454  const expression& right)
455  : op(op),
456  left(left),
457  right(right),
458  type_(promote_primitive(left.expression_type(),
459  right.expression_type())) {
460  }
461 
462 
464  expression const& subject)
465  : op(op),
466  subject(subject),
467  type_(promote_primitive(subject.expression_type())) {
468  }
469 
470 
473  expression const& high)
474  : low_(low),
475  high_(high) {
476  }
477  bool range::has_low() const {
478  return !is_nil(low_.expr_);
479  }
480  bool range::has_high() const {
481  return !is_nil(high_.expr_);
482  }
483 
484  void print_var_origin(std::ostream& o, const var_origin& vo) {
485  if (vo == data_origin)
486  o << "data";
487  else if (vo == transformed_data_origin)
488  o << "transformed data";
489  else if (vo == parameter_origin)
490  o << "parameter";
491  else if (vo == transformed_parameter_origin)
492  o << "transformed parameter";
493  else if (vo == derived_origin)
494  o << "derived";
495  else if (vo == local_origin)
496  o << "local";
497  else
498  o << "UNKNOWN ORIGIN";
499  }
500 
501 
504  : base_type_(base_type) {
505  }
506  base_var_decl::base_var_decl(const std::string& name,
507  const std::vector<expression>& dims,
508  const base_expr_type& base_type)
509  : name_(name),
510  dims_(dims),
511  base_type_(base_type) {
512  }
513 
514  bool variable_map::exists(const std::string& name) const {
515  return map_.find(name) != map_.end();
516  }
517  base_var_decl variable_map::get(const std::string& name) const {
518  if (!exists(name))
519  throw std::invalid_argument("variable does not exist");
520  return map_.find(name)->second.first;
521  }
522  base_expr_type variable_map::get_base_type(const std::string& name) const {
523  return get(name).base_type_;
524  }
525  size_t variable_map::get_num_dims(const std::string& name) const {
526  return get(name).dims_.size();
527  }
528  var_origin variable_map::get_origin(const std::string& name) const {
529  if (!exists(name))
530  throw std::invalid_argument("variable does not exist");
531  return map_.find(name)->second.second;
532  }
533  void variable_map::add(const std::string& name,
534  const base_var_decl& base_decl,
535  const var_origin& vo) {
536  map_[name] = range_t(base_decl,vo);
537  }
538  void variable_map::remove(const std::string& name) {
539  map_.erase(name);
540  }
541 
543  : base_var_decl(INT_T)
544  { }
545 
547  std::string const& name,
548  std::vector<expression> const& dims)
549  : base_var_decl(name,dims,INT_T),
550  range_(range)
551  { }
552 
553 
554 
557  { }
558 
560  std::string const& name,
561  std::vector<expression> const& dims)
562  : base_var_decl(name,dims,DOUBLE_T),
563  range_(range)
564  { }
565 
568  { }
569 
571  std::string const& name,
572  std::vector<expression> const& dims)
573  : base_var_decl(name,dims,VECTOR_T),
574  K_(K)
575  { }
576 
579  { }
580 
582  std::string const& name,
583  std::vector<expression> const& dims)
584  : base_var_decl(name,dims,VECTOR_T),
585  K_(K) {
586  }
587 
590  { }
591 
593  std::string const& name,
594  std::vector<expression> const& dims)
595  : base_var_decl(name,dims,VECTOR_T),
596  K_(K) {
597  }
598 
600 
602  expression const& M,
603  std::string const& name,
604  std::vector<expression> const& dims)
605  : base_var_decl(name,dims,VECTOR_T),
606  range_(range),
607  M_(M) {
608  }
609 
612  expression const& N,
613  std::string const& name,
614  std::vector<expression> const& dims)
615  : base_var_decl(name,dims,ROW_VECTOR_T),
616  range_(range),
617  N_(N) {
618  }
619 
622  expression const& M,
623  expression const& N,
624  std::string const& name,
625  std::vector<expression> const& dims)
626  : base_var_decl(name,dims,MATRIX_T),
627  range_(range),
628  M_(M),
629  N_(N) {
630  }
631 
632 
634  }
636  std::string const& name,
637  std::vector<expression> const& dims)
638  : base_var_decl(name,dims,MATRIX_T),
639  K_(K) {
640  }
641 
644  std::string const& name,
645  std::vector<expression> const& dims)
646  : base_var_decl(name,dims,MATRIX_T),
647  K_(K) {
648  }
649 
650 
651 
652 
654  std::string name_vis::operator()(const nil& x) const {
655  return ""; // fail if arises
656  }
657  std::string name_vis::operator()(const int_var_decl& x) const {
658  return x.name_;
659  }
660  std::string name_vis::operator()(const double_var_decl& x) const {
661  return x.name_;
662  }
663  std::string name_vis::operator()(const vector_var_decl& x) const {
664  return x.name_;
665  }
666  std::string name_vis::operator()(const row_vector_var_decl& x) const {
667  return x.name_;
668  }
669  std::string name_vis::operator()(const matrix_var_decl& x) const {
670  return x.name_;
671  }
672  std::string name_vis::operator()(const simplex_var_decl& x) const {
673  return x.name_;
674  }
675  std::string name_vis::operator()(const ordered_var_decl& x) const {
676  return x.name_;
677  }
678  std::string name_vis::operator()(const positive_ordered_var_decl& x) const {
679  return x.name_;
680  }
681  std::string name_vis::operator()(const cov_matrix_var_decl& x) const {
682  return x.name_;
683  }
684  std::string name_vis::operator()(const corr_matrix_var_decl& x) const {
685  return x.name_;
686  }
687 
688 
689 
690 
691 
692 
693  var_decl::var_decl(const var_decl_t& decl) : decl_(decl) { }
694  var_decl::var_decl() : decl_(nil()) { }
695  var_decl::var_decl(const nil& decl) : decl_(decl) { }
696  var_decl::var_decl(const int_var_decl& decl) : decl_(decl) { }
697  var_decl::var_decl(const double_var_decl& decl) : decl_(decl) { }
698  var_decl::var_decl(const vector_var_decl& decl) : decl_(decl) { }
699  var_decl::var_decl(const row_vector_var_decl& decl) : decl_(decl) { }
700  var_decl::var_decl(const matrix_var_decl& decl) : decl_(decl) { }
701  var_decl::var_decl(const simplex_var_decl& decl) : decl_(decl) { }
702  var_decl::var_decl(const ordered_var_decl& decl) : decl_(decl) { }
703  var_decl::var_decl(const positive_ordered_var_decl& decl) : decl_(decl) { }
704  var_decl::var_decl(const cov_matrix_var_decl& decl) : decl_(decl) { }
705  var_decl::var_decl(const corr_matrix_var_decl& decl) : decl_(decl) { }
706 
707  // template <typename Decl>
708  // var_decl::var_decl(Decl const& decl) : decl_(decl) { }
709 
710 
711 
712  std::string var_decl::name() const {
713  return boost::apply_visitor(name_vis(),decl_);
714  }
715 
716  statement::statement() : statement_(nil()) { }
717  statement::statement(const statement_t& st) : statement_(st) { }
718 
719  statement::statement(const nil& st) : statement_(st) { }
720  statement::statement(const assignment& st) : statement_(st) { }
721  statement::statement(const sample& st) : statement_(st) { }
722  statement::statement(const statements& st) : statement_(st) { }
723  statement::statement(const for_statement& st) : statement_(st) { }
724  statement::statement(const print_statement& st) : statement_(st) { }
725  statement::statement(const no_op_statement& st) : statement_(st) { }
726 
727  // template <typename Statement>
728  // statement::statement(const Statement& statement)
729  // : statement_(statement) {
730  // }
731 
732 
733 
734 
736  }
738  range& range,
739  statement& stmt)
740  : variable_(variable),
741  range_(range),
742  statement_(stmt) {
743  }
744 
746 
747  print_statement::print_statement(const std::vector<printable>& printables)
748  : printables_(printables) {
749  }
750 
752  program::program(const std::vector<var_decl>& data_decl,
753  const std::pair<std::vector<var_decl>,
754  std::vector<statement> >& derived_data_decl,
755  const std::vector<var_decl>& parameter_decl,
756  const std::pair<std::vector<var_decl>,
757  std::vector<statement> >& derived_decl,
758  const statement& st,
759  const std::pair<std::vector<var_decl>,
760  std::vector<statement> >& generated_decl)
761  : data_decl_(data_decl),
762  derived_data_decl_(derived_data_decl),
763  parameter_decl_(parameter_decl),
764  derived_decl_(derived_decl),
765  statement_(st),
766  generated_decl_(generated_decl) {
767  }
768 
770  }
772  distribution& dist)
773  : expr_(e),
774  dist_(dist) {
775  }
776  bool sample::is_ill_formed() const {
778  || ( truncation_.has_low()
779  && expr_.expression_type()
781  || ( truncation_.has_high()
782  && expr_.expression_type()
784  }
785 
787  }
789  expression& expr)
790  : var_dims_(var_dims),
791  expr_(expr) {
792  }
793 
795  expr_ = binary_op(expr_, '+', rhs);
796  return *this;
797  }
798 
800  expr_ = binary_op(expr_, '-', rhs);
801  return *this;
802  }
803 
805  expr_ = binary_op(expr_, '*', rhs);
806  return *this;
807  }
808 
810  expr_ = binary_op(expr_, '/', rhs);
811  return *this;
812  }
813 
814 
815  }
816 }
817 
818 
819 #endif

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