Stan  1.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
ast.hpp
Go to the documentation of this file.
1 #ifndef __STAN__GM__AST_HPP__
2 #define __STAN__GM__AST_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 namespace stan {
18 
19  namespace gm {
20 
23  struct nil { };
24 
25  // components of abstract syntax tree
26  struct assignment;
27  struct binary_op;
28  struct distribution;
29  struct double_var_decl;
30  struct double_literal;
31  struct expression;
32  struct for_statement;
33  struct fun;
34  struct identifier;
35  struct index_op;
36  struct int_literal;
37  struct inv_var_decl;
38  struct matrix_var_decl;
39  struct no_op_statement;
40  struct ordered_var_decl;
42  struct print_statement;
43  struct program;
44  struct range;
45  struct row_vector_var_decl;
46  struct sample;
47  struct simplex_var_decl;
48  struct statement;
49  struct statements;
50  struct unary_op;
51  struct variable;
52  struct variable_dims;
53  struct var_decl;
54  struct var_type;
56 
57  // forward declarable enum hack (can't fwd-decl enum)
58  typedef int base_expr_type;
59  const int INT_T = 1;
60  const int DOUBLE_T = 2;
61  const int VECTOR_T = 3;
62  const int ROW_VECTOR_T = 4;
63  const int MATRIX_T = 5;
64  const int ILL_FORMED_T = 6;
65 
66  std::ostream& write_base_expr_type(std::ostream& o, base_expr_type type);
67 
68  struct expr_type {
70  size_t num_dims_;
71  expr_type();
72  expr_type(const base_expr_type base_type);
73  expr_type(const base_expr_type base_type,
74  size_t num_dims);
75  bool operator==(const expr_type& et) const;
76  bool operator!=(const expr_type& et) const;
77  bool is_primitive() const;
78  bool is_primitive_int() const;
79  bool is_primitive_double() const;
80  bool is_ill_formed() const;
81  base_expr_type type() const;
82  size_t num_dims() const;
83  };
84 
85  std::ostream& operator<<(std::ostream& o, const expr_type& et);
86 
88 
90  const expr_type& et2);
91 
92  typedef std::pair<expr_type, std::vector<expr_type> > function_signature_t;
93 
95  public:
96  static function_signatures& instance();
97  void add(const std::string& name,
98  const expr_type& result_type,
99  const std::vector<expr_type>& arg_types);
100  void add(const std::string& name,
101  const expr_type& result_type);
102  void add(const std::string& name,
103  const expr_type& result_type,
104  const expr_type& arg_type);
105  void add(const std::string& name,
106  const expr_type& result_type,
107  const expr_type& arg_type1,
108  const expr_type& arg_type2);
109  void add(const std::string& name,
110  const expr_type& result_type,
111  const expr_type& arg_type1,
112  const expr_type& arg_type2,
113  const expr_type& arg_type3);
114  void add(const std::string& name,
115  const expr_type& result_type,
116  const expr_type& arg_type1,
117  const expr_type& arg_type2,
118  const expr_type& arg_type3,
119  const expr_type& arg_type4);
120  void add(const std::string& name,
121  const expr_type& result_type,
122  const expr_type& arg_type1,
123  const expr_type& arg_type2,
124  const expr_type& arg_type3,
125  const expr_type& arg_type4,
126  const expr_type& arg_type5);
127  void add_nullary(const::std::string& name);
128  void add_unary(const::std::string& name);
129  void add_binary(const::std::string& name);
130  void add_ternary(const::std::string& name);
131  void add_quaternary(const::std::string& name);
132  int num_promotions(const std::vector<expr_type>& call_args,
133  const std::vector<expr_type>& sig_args);
134  expr_type get_result_type(const std::string& name,
135  const std::vector<expr_type>& args,
136  std::ostream& error_msgs);
137  private:
140  std::map<std::string, std::vector<function_signature_t> > sigs_map_;
141  static function_signatures* sigs_; // init below outside of class
142  };
143 
144  struct statements {
145  std::vector<var_decl> local_decl_;
146  std::vector<statement> statements_;
147  statements();
148  statements(const std::vector<var_decl>& local_decl,
149  const std::vector<statement>& stmts);
150  };
151 
152 
153  struct distribution {
154  std::string family_;
155  std::vector<expression> args_;
156  };
157 
158  struct expression_type_vis : public boost::static_visitor<expr_type> {
159  expr_type operator()(const nil& e) const;
160  expr_type operator()(const int_literal& e) const;
161  expr_type operator()(const double_literal& e) const;
162  expr_type operator()(const variable& e) const;
163  expr_type operator()(const fun& e) const;
164  expr_type operator()(const index_op& e) const;
165  expr_type operator()(const binary_op& e) const;
166  expr_type operator()(const unary_op& e) const;
167  // template <typename T> expr_type operator()(const T& e) const;
168  };
169 
170  struct expression;
171 
172  struct expression {
173  typedef boost::variant<boost::recursive_wrapper<nil>,
174  boost::recursive_wrapper<int_literal>,
175  boost::recursive_wrapper<double_literal>,
176  boost::recursive_wrapper<variable>,
177  boost::recursive_wrapper<fun>,
178  boost::recursive_wrapper<index_op>,
179  boost::recursive_wrapper<binary_op>,
180  boost::recursive_wrapper<unary_op> >
182 
183  expression();
184  expression(const expression& e);
185 
186  // template <typename Expr> expression(const Expr& expr);
187  expression(const nil& expr);
188  expression(const int_literal& expr);
189  expression(const double_literal& expr);
190  expression(const variable& expr);
191  expression(const fun& expr);
192  expression(const index_op& expr);
193  expression(const binary_op& expr);
194  expression(const unary_op& expr);
195  expression(const expression_t& expr_);
196 
197 
198 
199  expr_type expression_type() const;
200 
201  expression& operator+=(const expression& rhs);
202  expression& operator-=(const expression& rhs);
203  expression& operator*=(const expression& rhs);
204  expression& operator/=(const expression& rhs);
205 
207  };
208 
209  struct printable {
210  typedef boost::variant<boost::recursive_wrapper<std::string>,
211  boost::recursive_wrapper<expression> >
213 
214  printable();
216  printable(const std::string& msg);
218  printable(const printable& printable);
219 
221  };
222 
223  struct is_nil_op : public boost::static_visitor<bool> {
224  bool operator()(const nil& x) const;
225  bool operator()(const int_literal& x) const;
226  bool operator()(const double_literal& x) const;
227  bool operator()(const variable& x) const;
228  bool operator()(const fun& x) const;
229  bool operator()(const index_op& x) const;
230  bool operator()(const binary_op& x) const;
231  bool operator()(const unary_op& x) const;
232 
233  // template <typename T>
234  // bool operator()(const T& x) const;
235  };
236 
237  bool is_nil(const expression& e);
238 
239  struct variable_dims {
240  std::string name_;
241  std::vector<expression> dims_;
242  variable_dims();
243  variable_dims(std::string const& name,
244  std::vector<expression> const& dims);
245  };
246 
247 
248  struct int_literal {
249  int val_;
251  int_literal();
252  int_literal(int val);
253  int_literal(const int_literal& il);
254  int_literal& operator=(const int_literal& il);
255  };
256 
257 
258  struct double_literal {
259  double val_;
261  double_literal();
262  double_literal(double val);
264  };
265 
266  struct variable {
267  std::string name_;
269  variable();
270  variable(std::string name);
271  void set_type(const base_expr_type& base_type,
272  size_t num_dims);
273  };
274 
275  struct fun {
276  std::string name_;
277  std::vector<expression> args_;
279  fun();
280  fun(std::string const& name,
281  std::vector<expression> const& args);
282  void infer_type(); // FIXME: is this used anywhere?
283  };
284 
285  size_t total_dims(const std::vector<std::vector<expression> >& dimss);
286 
287  expr_type infer_type_indexing(const base_expr_type& expr_base_type,
288  size_t num_expr_dims,
289  size_t num_index_dims);
290 
292  size_t num_index_dims);
293 
294 
295  struct index_op {
297  std::vector<std::vector<expression> > dimss_;
299  index_op();
300  // vec of vec for e.g., e[1,2][3][4,5,6]
301  index_op(const expression& expr,
302  const std::vector<std::vector<expression> >& dimss);
303  void infer_type();
304  };
305 
306 
307  struct binary_op {
308  char op;
312  binary_op();
313  binary_op(const expression& left,
314  char op,
315  const expression& right);
316  };
317 
318  struct unary_op {
319  char op;
322  unary_op(char op,
323  expression const& subject);
324  };
325 
326  struct range {
329  range();
330  range(expression const& low,
331  expression const& high);
332  bool has_low() const;
333  bool has_high() const;
334  };
335 
336  typedef int var_origin;
337  const int data_origin = 1;
338  const int transformed_data_origin = 2;
339  const int parameter_origin = 3;
341  const int derived_origin = 5;
342  const int local_origin = 6;
343 
344  // enum var_origin {
345  // data_origin,
346  // transformed_data_origin,
347  // parameter_origin,
348  // transformed_parameter_origin,
349  // derived_origin,
350  // local_origin
351  // };
352 
353  // std::ostream& operator<<(std::ostream& o, const var_origin& vo) {
354  // if (vo == data_origin)
355  // o << "data";
356  // else if (vo == transformed_data_origin)
357  // o << "transformed data";
358  // else if (vo == parameter_origin)
359  // o << "parameter";
360  // else if (vo == transformed_parameter_origin)
361  // o << "transformed parameter";
362  // else if (vo == derived_origin)
363  // o << "derived";
364  // else if (vo == local_origin)
365  // o << "local";
366  // else
367  // o << "UNKNOWN ORIGIN";
368  // return o;
369  // }
370 
371 
372 
373 
374  void print_var_origin(std::ostream& o, const var_origin& vo);
375 
376  // FIXME: replace < with print_var_ori
377  // std::ostream& operator<<(std::ostream& o, const var_origin& vo);
378 
379 
380  struct base_var_decl {
381  std::string name_;
382  std::vector<expression> dims_;
384  base_var_decl();
385  base_var_decl(const base_expr_type& base_type);
386  base_var_decl(const std::string& name,
387  const std::vector<expression>& dims,
388  const base_expr_type& base_type);
389  };
390 
391  struct variable_map {
392  typedef std::pair<base_var_decl,var_origin> range_t;
393  std::map<std::string, range_t> map_;
394  bool exists(const std::string& name) const;
395  base_var_decl get(const std::string& name) const;
396  base_expr_type get_base_type(const std::string& name) const;
397  size_t get_num_dims(const std::string& name) const;
398  var_origin get_origin(const std::string& name) const;
399  void add(const std::string& name,
400  const base_var_decl& base_decl,
401  const var_origin& vo);
402  void remove(const std::string& name);
403  };
404 
405  struct int_var_decl : public base_var_decl {
407  int_var_decl();
408  int_var_decl(range const& range,
409  std::string const& name,
410  std::vector<expression> const& dims);
411  };
412 
413 
414  struct double_var_decl : public base_var_decl {
416  double_var_decl();
417  double_var_decl(range const& range,
418  std::string const& name,
419  std::vector<expression> const& dims);
420  };
421 
422  struct simplex_var_decl : public base_var_decl {
425  simplex_var_decl(expression const& K,
426  std::string const& name,
427  std::vector<expression> const& dims);
428  };
429 
430  struct ordered_var_decl : public base_var_decl {
433  ordered_var_decl(expression const& K,
434  std::string const& name,
435  std::vector<expression> const& dims);
436  };
437 
442  std::string const& name,
443  std::vector<expression> const& dims);
444  };
445 
446  struct vector_var_decl : public base_var_decl {
449  vector_var_decl();
450  vector_var_decl(range const& range,
451  expression const& M,
452  std::string const& name,
453  std::vector<expression> const& dims);
454  };
455 
461  expression const& N,
462  std::string const& name,
463  std::vector<expression> const& dims);
464  };
465 
466  struct matrix_var_decl : public base_var_decl {
470  matrix_var_decl();
471  matrix_var_decl(range const& range,
472  expression const& M,
473  expression const& N,
474  std::string const& name,
475  std::vector<expression> const& dims);
476  };
477 
478 
479 
480 
481 
486  std::string const& name,
487  std::vector<expression> const& dims);
488  };
489 
490 
491 
496  std::string const& name,
497  std::vector<expression> const& dims);
498  };
499 
500 
501 
502  struct name_vis : public boost::static_visitor<std::string> {
503  name_vis();
504  std::string operator()(const nil& x) const;
505  std::string operator()(const int_var_decl& x) const;
506  std::string operator()(const double_var_decl& x) const;
507  std::string operator()(const vector_var_decl& x) const;
508  std::string operator()(const row_vector_var_decl& x) const;
509  std::string operator()(const matrix_var_decl& x) const;
510  std::string operator()(const simplex_var_decl& x) const;
511  std::string operator()(const ordered_var_decl& x) const;
512  std::string operator()(const positive_ordered_var_decl& x) const;
513  std::string operator()(const cov_matrix_var_decl& x) const;
514  std::string operator()(const corr_matrix_var_decl& x) const;
515  };
516 
517 
518 
519 
520  struct var_decl {
521  typedef boost::variant<boost::recursive_wrapper<nil>,
522  boost::recursive_wrapper<int_var_decl>,
523  boost::recursive_wrapper<double_var_decl>,
524  boost::recursive_wrapper<vector_var_decl>,
525  boost::recursive_wrapper<row_vector_var_decl>,
526  boost::recursive_wrapper<matrix_var_decl>,
527  boost::recursive_wrapper<simplex_var_decl>,
528  boost::recursive_wrapper<ordered_var_decl>,
529  boost::recursive_wrapper<positive_ordered_var_decl>,
530  boost::recursive_wrapper<cov_matrix_var_decl>,
531  boost::recursive_wrapper<corr_matrix_var_decl> >
533 
535 
536  var_decl();
537 
538  // template <typename Decl>
539  // var_decl(Decl const& decl);
540  var_decl(const var_decl_t& decl);
541  var_decl(const nil& decl);
542  var_decl(const int_var_decl& decl);
543  var_decl(const double_var_decl& decl);
544  var_decl(const vector_var_decl& decl);
545  var_decl(const row_vector_var_decl& decl);
546  var_decl(const matrix_var_decl& decl);
547  var_decl(const simplex_var_decl& decl);
548  var_decl(const ordered_var_decl& decl);
549  var_decl(const positive_ordered_var_decl& decl);
550  var_decl(const cov_matrix_var_decl& decl);
551  var_decl(const corr_matrix_var_decl& decl);
552 
553  std::string name() const;
554  };
555 
556  struct statement {
557  typedef boost::variant<boost::recursive_wrapper<nil>,
558  boost::recursive_wrapper<assignment>,
559  boost::recursive_wrapper<sample>,
560  boost::recursive_wrapper<statements>,
561  boost::recursive_wrapper<for_statement>,
562  boost::recursive_wrapper<print_statement>,
563  boost::recursive_wrapper<no_op_statement> >
565 
567 
568  statement();
569  statement(const statement_t& st);
570 
571  statement(const nil& st);
572  statement(const assignment& st);
573  statement(const sample& st);
574  statement(const statements& st);
575  statement(const for_statement& st);
576  statement(const print_statement& st);
577  statement(const no_op_statement& st);
578 
579  // template <typename Statement>
580  // statement(const Statement& statement);
581  };
582 
583 
584  struct for_statement {
585  std::string variable_;
588  for_statement();
589  for_statement(std::string& variable,
590  range& range,
591  statement& stmt);
592  };
593 
595  std::vector<printable> printables_;
596  print_statement();
597  print_statement(const std::vector<printable>& printables);
598  };
599 
600 
602  // no op, no data
603  };
604 
605 
606 
607  struct program {
608  std::vector<var_decl> data_decl_;
609  std::pair<std::vector<var_decl>,std::vector<statement> >
611  std::vector<var_decl> parameter_decl_;
612  std::pair<std::vector<var_decl>,std::vector<statement> >
615  std::pair<std::vector<var_decl>,std::vector<statement> > generated_decl_;
616  program();
617  program(const std::vector<var_decl>& data_decl,
618  const std::pair<std::vector<var_decl>,
619  std::vector<statement> >& derived_data_decl,
620  const std::vector<var_decl>& parameter_decl,
621  const std::pair<std::vector<var_decl>,
622  std::vector<statement> >& derived_decl,
623  const statement& st,
624  const std::pair<std::vector<var_decl>,
625  std::vector<statement> >& generated_decl);
626 
627 
628  };
629 
630  struct sample {
634  sample();
636  distribution& dist);
637  bool is_ill_formed() const;
638  };
639 
640  struct assignment {
641  variable_dims var_dims_; // lhs_var[dim0,...,dimN-1]
642  expression expr_; // = rhs
643  base_var_decl var_type_; // type of lhs_var
644  assignment();
645  assignment(variable_dims& var_dims,
646  expression& expr);
647  };
648 
649  // FIXME: is this next necessary dependency?
650  // from generator.hpp
651  void generate_expression(const expression& e, std::ostream& o);
652 
653 
654  }
655 }
656 
657 #endif

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