Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
term_grammar_def.hpp
Go to the documentation of this file.
1 #ifndef STAN__GM__PARSER__TERM_GRAMMAR_DEF__HPP
2 #define STAN__GM__PARSER__TERM_GRAMMAR_DEF__HPP
3 
4 #include <cstddef>
5 #include <iomanip>
6 #include <iostream>
7 #include <istream>
8 #include <map>
9 #include <set>
10 #include <sstream>
11 #include <string>
12 #include <utility>
13 #include <vector>
14 #include <stdexcept>
15 
16 #include <boost/spirit/include/qi.hpp>
17 // FIXME: get rid of unused include
18 #include <boost/spirit/include/phoenix_core.hpp>
19 #include <boost/spirit/include/phoenix_function.hpp>
20 #include <boost/spirit/include/phoenix_fusion.hpp>
21 #include <boost/spirit/include/phoenix_object.hpp>
22 #include <boost/spirit/include/phoenix_operator.hpp>
23 #include <boost/spirit/include/phoenix_stl.hpp>
24 
25 #include <boost/lexical_cast.hpp>
26 #include <boost/fusion/include/adapt_struct.hpp>
27 #include <boost/fusion/include/std_pair.hpp>
28 #include <boost/config/warning_disable.hpp>
29 #include <boost/spirit/include/qi.hpp>
30 #include <boost/spirit/include/qi_numeric.hpp>
31 #include <boost/spirit/include/classic_position_iterator.hpp>
32 #include <boost/spirit/include/phoenix_core.hpp>
33 #include <boost/spirit/include/phoenix_function.hpp>
34 #include <boost/spirit/include/phoenix_fusion.hpp>
35 #include <boost/spirit/include/phoenix_object.hpp>
36 #include <boost/spirit/include/phoenix_operator.hpp>
37 #include <boost/spirit/include/phoenix_stl.hpp>
38 #include <boost/spirit/include/support_multi_pass.hpp>
39 #include <boost/tuple/tuple.hpp>
40 #include <boost/variant/apply_visitor.hpp>
41 #include <boost/variant/recursive_variant.hpp>
42 
43 #include <stan/gm/ast.hpp>
47 
49  (stan::gm::expression, expr_)
50  (std::vector<std::vector<stan::gm::expression> >,
51  dimss_) );
52 
54  (std::string, system_function_name_)
58  (stan::gm::expression, theta_)
60  (stan::gm::expression, x_int_) );
61 
63  (std::string, name_)
64  (std::vector<stan::gm::expression>, args_) );
65 
67  (int,val_)
68  (stan::gm::expr_type,type_));
69 
71  (double,val_)
72  (stan::gm::expr_type,type_) );
73 
74 
75 namespace stan {
76 
77  namespace gm {
78 
79 
81 
82  template <typename T1, typename T2, typename T3, typename T4>
83  struct result { typedef void type; };
84 
85  void operator()(const integrate_ode& ode_fun,
86  const variable_map& var_map,
87  bool& pass,
88  std::ostream& error_msgs) const {
89  pass = true;
90 
91  // test function argument type
92  expr_type sys_result_type(DOUBLE_T,1);
93  std::vector<expr_type> sys_arg_types;
94  sys_arg_types.push_back(expr_type(DOUBLE_T,0));
95  sys_arg_types.push_back(expr_type(DOUBLE_T,1));
96  sys_arg_types.push_back(expr_type(DOUBLE_T,1));
97  sys_arg_types.push_back(expr_type(DOUBLE_T,1));
98  sys_arg_types.push_back(expr_type(INT_T,1));
99  function_signature_t system_signature(sys_result_type, sys_arg_types);
101  .is_defined(ode_fun.system_function_name_,system_signature)) {
102  error_msgs << "first argument to integrate_ode must be a function with signature"
103  << " (real, real[], real[], real[], int[]) : real[] ";
104  pass = false;
105  }
106 
107  // test regular argument types
108  if (ode_fun.y0_.expression_type() != expr_type(DOUBLE_T,1)) {
109  error_msgs << "second argument to integrate_ode must be type real[]"
110  << " for intial system state"
111  << "; found type="
112  << ode_fun.y0_.expression_type()
113  << ". ";
114  pass = false;
115  }
116  if (!ode_fun.t0_.expression_type().is_primitive()) {
117  error_msgs << "third argument to integrate_ode must be type real or int"
118  << " for initial time"
119  << "; found type="
120  << ode_fun.t0_.expression_type()
121  << ". ";
122  pass = false;
123  }
124  if (ode_fun.ts_.expression_type() != expr_type(DOUBLE_T,1)) {
125  error_msgs << "fourth argument to integrate_ode must be type real[]"
126  << " for requested solution times"
127  << "; found type="
128  << ode_fun.ts_.expression_type()
129  << ". ";
130  pass = false;
131  }
132  if (ode_fun.theta_.expression_type() != expr_type(DOUBLE_T,1)) {
133  error_msgs << "fifth argument to integrate_ode must be type real[]"
134  << " for parameters"
135  << "; found type="
136  << ode_fun.theta_.expression_type()
137  << ". ";
138  pass = false;
139  }
140  if (ode_fun.x_.expression_type() != expr_type(DOUBLE_T,1)) {
141  error_msgs << "sixth argument to integrate_ode must be type real[]"
142  << " for real data;"
143  << " found type="
144  << ode_fun.x_.expression_type()
145  << ". ";
146  pass = false;
147  }
148  if (ode_fun.x_int_.expression_type() != expr_type(INT_T,1)) {
149  error_msgs << "seventh argument to integrate_ode must be type int[]"
150  << " for integer data;"
151  << " found type="
152  << ode_fun.x_int_.expression_type()
153  << ". ";
154  pass = false;
155  }
156 
157  // test data-only variables do not have parameters
158  if (has_var(ode_fun.t0_, var_map)) {
159  error_msgs << "third argument to integrate_ode (initial times)"
160  << " must be data only and not reference parameters";
161  pass = false;
162  }
163  if (has_var(ode_fun.ts_, var_map)) {
164  error_msgs << "fourth argument to integrate_ode (solution times)"
165  << " must be data only and not reference parameters";
166  pass = false;
167  }
168  if (has_var(ode_fun.x_, var_map)) {
169  error_msgs << "fifth argument to integrate_ode (real data)"
170  << " must be data only and not reference parameters";
171  pass = false;
172  }
173  }
174  };
175  boost::phoenix::function<validate_integrate_ode> validate_integrate_ode_f;
176 
177  struct set_fun_type {
178  template <typename T1, typename T2>
179  struct result { typedef fun type; };
180 
182  std::ostream& error_msgs) const {
183  std::vector<expr_type> arg_types;
184  for (size_t i = 0; i < fun.args_.size(); ++i)
185  arg_types.push_back(fun.args_[i].expression_type());
187  arg_types,
188  error_msgs);
189  return fun;
190  }
191  };
192  boost::phoenix::function<set_fun_type> set_fun_type_f;
193 
194 
196  template <typename T1, typename T2, typename T3, typename T4, typename T5>
197  struct result { typedef void type; };
198 
199  void operator()(expression& fun_result,
200  fun& fun,
201  const var_origin& var_origin,
202  bool& pass,
203  std::ostream& error_msgs) const {
204  std::vector<expr_type> arg_types;
205  for (size_t i = 0; i < fun.args_.size(); ++i)
206  arg_types.push_back(fun.args_[i].expression_type());
208  arg_types,
209  error_msgs);
210 
211 
212  if (has_rng_suffix(fun.name_)) {
213  if (!( var_origin == derived_origin
214  || var_origin == function_argument_origin_rng )) {
215  error_msgs << "random number generators only allowed in"
216  << " generated quantities block or"
217  << " user-defined functions with names ending in _rng"
218  << "; found function=" << fun.name_
219  << " in block=";
220  print_var_origin(error_msgs,var_origin);
221  error_msgs << std::endl;
222  pass = false;
223  return;
224  }
225  }
226 
227  if (has_lp_suffix(fun.name_)) {
228  // modified function_argument_origin to add _lp because
229  // that's only viable context
230  if (!( var_origin == parameter_origin
231  || var_origin == transformed_parameter_origin
232  || var_origin == function_argument_origin_lp
233  || var_origin == local_origin )) {
234  error_msgs << "lp suffixed functions only allowed in"
235  << " transformed parameter, function argument, or model"
236 
237  << " in block=";
238  print_var_origin(error_msgs,var_origin);
239  error_msgs << std::endl;
240  pass = false;
241  return;
242  }
243  }
244 
245  if (fun.name_ == "abs"
246  && fun.args_.size() > 0
247  && fun.args_[0].expression_type().is_primitive_double()) {
248  error_msgs << "Warning: Function abs(real) is deprecated."
249  << std::endl
250  << " It will be removed in a future release."
251  << std::endl
252  << " Use fabs(real) instead."
253  << std::endl << std::endl;
254  }
255 
256  if (fun.name_ == "lkj_cov_log") {
257  error_msgs << "Warning: the lkj_cov_log() function"
258  << " is deprecated. It will be removed in Stan 3."
259  << std::endl
260  << "Code LKJ covariance in terms of an lkj_corr()"
261  << " distribution on a correlation matrix"
262  << " and independent lognormals on the scales."
263  << std::endl << std::endl;
264  }
265 
266  fun_result = fun;
267  pass = true;
268  }
269  };
270  boost::phoenix::function<set_fun_type_named> set_fun_type_named_f;
271 
273  template <typename T1, typename T2, typename T3, typename T4, typename T5>
274  struct result { typedef void type; };
275 
276  void operator()(expression& expr1,
277  const expression& expr2,
278  const var_origin& var_origin,
279  bool& pass,
280  std::ostream& error_msgs) const {
281 
282  if (!expr1.expression_type().is_primitive()
283  || !expr2.expression_type().is_primitive()) {
284  error_msgs << "arguments to ^ must be primitive (real or int)"
285  << "; cannot exponentiate "
286  << expr1.expression_type()
287  << " by "
288  << expr2.expression_type()
289  << " in block=";
290  print_var_origin(error_msgs,var_origin);
291  error_msgs << std::endl;
292  pass = false;
293  return;
294  }
295  std::vector<expression> args;
296  args.push_back(expr1);
297  args.push_back(expr2);
298  set_fun_type sft;
299  fun f("pow",args);
300  sft(f,error_msgs);
301  expr1 = expression(f);
302  }
303  };
304  boost::phoenix::function<exponentiation_expr> exponentiation_f;
305 
307  template <typename T1, typename T2, typename T3>
308  struct result { typedef void type; };
309 
310  void operator()(expression& expr1,
311  const expression& expr2,
312  std::ostream& error_msgs) const {
313 
314  if (expr1.expression_type().is_primitive()
315  && expr2.expression_type().is_primitive()) {
316  expr1 *= expr2;;
317  return;
318  }
319  std::vector<expression> args;
320  args.push_back(expr1);
321  args.push_back(expr2);
322  set_fun_type sft;
323  fun f("multiply",args);
324  sft(f,error_msgs);
325  expr1 = expression(f);
326  }
327  };
328  boost::phoenix::function<multiplication_expr> multiplication_f;
329 
330  void generate_expression(const expression& e, std::ostream& o);
331 
332  struct division_expr {
333  template <typename T1, typename T2, typename T3>
334  struct result { typedef void type; };
335 
336  void operator()(expression& expr1,
337  const expression& expr2,
338  std::ostream& error_msgs) const {
339  if (expr1.expression_type().is_primitive()
340  && expr2.expression_type().is_primitive()
342  || expr2.expression_type().is_primitive_double())) {
343  expr1 /= expr2;
344  return;
345  }
346  std::vector<expression> args;
347  args.push_back(expr1);
348  args.push_back(expr2);
349  set_fun_type sft;
350  if (expr1.expression_type().is_primitive_int()
351  && expr2.expression_type().is_primitive_int()) {
352  // result might be assigned to real - generate warning
353  error_msgs << "Warning: integer division implicitly rounds to integer."
354  << " Found int division: ";
355  generate_expression(expr1.expr_,error_msgs);
356  error_msgs << " / ";
357  generate_expression(expr2.expr_,error_msgs);
358  error_msgs << std::endl
359  << " Positive values rounded down, negative values rounded up or down"
360  << " in platform-dependent way."
361  << std::endl;
362 
363  fun f("divide",args);
364  sft(f,error_msgs);
365  expr1 = expression(f);
366  return;
367  }
368  if ((expr1.expression_type().type() == MATRIX_T
369  || expr1.expression_type().type() == ROW_VECTOR_T)
370  && expr2.expression_type().type() == MATRIX_T) {
371  fun f("mdivide_right",args);
372  sft(f,error_msgs);
373  expr1 = expression(f);
374  return;
375  }
376  fun f("divide",args);
377  sft(f,error_msgs);
378  expr1 = expression(f);
379  return;
380  }
381  };
382  boost::phoenix::function<division_expr> division_f;
383 
384  struct modulus_expr {
385  template <typename T1, typename T2, typename T3, typename T4>
386  struct result { typedef void type; };
387 
388  void operator()(expression& expr1,
389  const expression& expr2,
390  bool& pass,
391  std::ostream& error_msgs) const {
392  if (!expr1.expression_type().is_primitive_int()
393  && !expr2.expression_type().is_primitive_int()) {
394  error_msgs << "both operands of % must be int"
395  << "; cannot modulo "
396  << expr1.expression_type()
397  << " by "
398  << expr2.expression_type();
399  error_msgs << std::endl;
400  pass = false;
401  return;
402  }
403  std::vector<expression> args;
404  args.push_back(expr1);
405  args.push_back(expr2);
406  set_fun_type sft;
407  fun f("modulus",args);
408  sft(f,error_msgs);
409  expr1 = expression(f);
410  }
411  };
412  boost::phoenix::function<modulus_expr> modulus_f;
413 
415  template <typename T1, typename T2, typename T3>
416  struct result { typedef void type; };
417 
418  void operator()(expression& expr1,
419  const expression& expr2,
420  std::ostream& error_msgs) const {
421  std::vector<expression> args;
422  args.push_back(expr1);
423  args.push_back(expr2);
424  set_fun_type sft;
425  if (expr1.expression_type().type() == MATRIX_T
426  && (expr2.expression_type().type() == VECTOR_T
427  || expr2.expression_type().type() == MATRIX_T)) {
428  fun f("mdivide_left",args);
429  sft(f,error_msgs);
430  expr1 = expression(f);
431  return;
432  }
433  fun f("divide_left",args); // this doesn't exist, so will
434  // throw error on purpose
435  sft(f,error_msgs);
436  expr1 = expression(f);
437  }
438  };
439  boost::phoenix::function<left_division_expr> left_division_f;
440 
442  template <typename T1, typename T2, typename T3>
443  struct result { typedef void type; };
444 
445  void operator()(expression& expr1,
446  const expression& expr2,
447  std::ostream& error_msgs) const {
448 
449  if (expr1.expression_type().is_primitive()
450  && expr2.expression_type().is_primitive()) {
451  expr1 *= expr2;
452  return;
453  }
454  std::vector<expression> args;
455  args.push_back(expr1);
456  args.push_back(expr2);
457  set_fun_type sft;
458  fun f("elt_multiply",args);
459  sft(f,error_msgs);
460  expr1 = expression(f);
461  }
462  };
463  boost::phoenix::function<elt_multiplication_expr> elt_multiplication_f;
464 
466  template <typename T1, typename T2, typename T3>
467  struct result { typedef void type; };
468 
469  void operator()(expression& expr1,
470  const expression& expr2,
471  std::ostream& error_msgs) const {
472 
473  if (expr1.expression_type().is_primitive()
474  && expr2.expression_type().is_primitive()) {
475  expr1 /= expr2;
476  return;
477  }
478  std::vector<expression> args;
479  args.push_back(expr1);
480  args.push_back(expr2);
481  set_fun_type sft;
482  fun f("elt_divide",args);
483  sft(f,error_msgs);
484  expr1 = expression(f);
485  }
486  };
487  boost::phoenix::function<elt_division_expr> elt_division_f;
488 
489  // Cut-and-Paste from Spirit examples, including comment: We
490  // should be using expression::operator-. There's a bug in phoenix
491  // type deduction mechanism that prevents us from doing
492  // so. Phoenix will be switching to BOOST_TYPEOF. In the meantime,
493  // we will use a phoenix::function below:
494  struct negate_expr {
495  template <typename T1, typename T2, typename T3, typename T4>
496  struct result { typedef void type; };
497 
498  void operator()(expression& expr_result,
499  const expression& expr,
500  bool& pass,
501  std::ostream& error_msgs) const {
502  if (expr.expression_type().is_primitive()) {
503  expr_result = expression(unary_op('-', expr));
504  return;
505  }
506  std::vector<expression> args;
507  args.push_back(expr);
508  set_fun_type sft;
509  fun f("minus",args);
510  sft(f,error_msgs);
511  expr_result = expression(f);
512  }
513  };
514  boost::phoenix::function<negate_expr> negate_expr_f;
515 
517  template <typename T1, typename T2, typename T3>
518  struct result { typedef void type; };
519 
520  void operator()(expression& expr_result,
521  const expression& expr,
522  std::ostream& error_msgs) const {
523  if (!expr.expression_type().is_primitive()) {
524  error_msgs << "logical negation operator ! only applies to int or real types; ";
525  expr_result = expression();
526  }
527  std::vector<expression> args;
528  args.push_back(expr);
529  set_fun_type sft;
530  fun f("logical_negation",args);
531  sft(f,error_msgs);
532  expr_result = expression(f);
533  }
534  };
535  boost::phoenix::function<logical_negate_expr> logical_negate_expr_f;
536 
537  struct transpose_expr {
538  template <typename T1, typename T2>
539  struct result { typedef expression type; };
540 
542  std::ostream& error_msgs) const {
543 
544  if (expr.expression_type().is_primitive()) {
545  return expr; // transpose of basic is self -- works?
546  }
547  std::vector<expression> args;
548  args.push_back(expr);
549  set_fun_type sft;
550  fun f("transpose",args);
551  sft(f,error_msgs);
552  return expression(f);
553  }
554  };
555  boost::phoenix::function<transpose_expr> transpose_f;
556 
558  template <typename T1, typename T2, typename T3, typename T4>
559  struct result { typedef void type; };
561  std::vector<std::vector<stan::gm::expression> >& dimss,
562  bool& pass,
563  std::ostream& error_msgs) const {
564  index_op iop(expression,dimss);
565  iop.infer_type();
566  if (iop.type_.is_ill_formed()) {
567  error_msgs << "indexes inappropriate for expression." << std::endl;
568  pass = false;
569  }
570  pass = true;
571  expression = iop;
572  }
573  };
574  boost::phoenix::function<add_expression_dimss> add_expression_dimss_f;
575 
576  struct set_var_type {
577  template <typename T1, typename T2, typename T3, typename T4>
578  struct result { typedef variable type; };
580  variable_map& vm,
581  std::ostream& error_msgs,
582  bool& pass) const {
583  std::string name = var_expr.name_;
584  if (!vm.exists(name)) {
585  pass = false;
586  error_msgs << "variable \"" << name << '"' << " does not exist."
587  << std::endl;
588  return var_expr;
589  }
590  if (name == std::string("lp__")) {
591  error_msgs << std::endl
592  << "WARNING:"
593  << std::endl
594  << " Direct use of variable lp__ is deprecated and will be removed in a future release."
595  << std::endl
596  << " Please use increment_log_prob(u) in place of of lp__ <- lp__ + u."
597  << std::endl;
598  }
599  pass = true;
600  var_expr.set_type(vm.get_base_type(name),vm.get_num_dims(name));
601  return var_expr;
602  }
603  };
604  boost::phoenix::function<set_var_type> set_var_type_f;
605 
607  template <typename T1, typename T2>
608  struct result { typedef bool type; };
609 
610  bool operator()(const expression& expr,
611  std::stringstream& error_msgs) const {
612  if (!expr.expression_type().is_primitive_int()) {
613  error_msgs << "expression denoting integer required; found type="
614  << expr.expression_type() << std::endl;
615  return false;
616  }
617  return true;
618  }
619  };
620  boost::phoenix::function<validate_int_expr3> validate_int_expr3_f;
621 
622 
623 
624  template <typename Iterator>
626  std::stringstream& error_msgs,
628  : term_grammar::base_type(term_r),
629  var_map_(var_map),
630  error_msgs_(error_msgs),
631  expression_g(eg)
632  {
633  using boost::spirit::qi::_1;
634  using boost::spirit::qi::char_;
635  using boost::spirit::qi::double_;
636  using boost::spirit::qi::eps;
637  using boost::spirit::qi::int_;
638  using boost::spirit::qi::lexeme;
639  using boost::spirit::qi::lit;
640  using boost::spirit::qi::_pass;
641  using boost::spirit::qi::_val;
642  using boost::spirit::qi::labels::_r1;
643 
644  // _r1 : var_origin
645 
646  term_r.name("term");
647  term_r
648  = ( negated_factor_r(_r1)
649  [_val = _1]
650  >> *( (lit('*') > negated_factor_r(_r1)
651  [multiplication_f(_val,_1,
652  boost::phoenix::ref(error_msgs_))])
653  | (lit('/') > negated_factor_r(_r1)
654  [division_f(_val,_1,
655  boost::phoenix::ref(error_msgs_))])
656  | (lit('%') > negated_factor_r(_r1)
657  [modulus_f(_val,_1,_pass,
658  boost::phoenix::ref(error_msgs_))])
659  | (lit('\\') > negated_factor_r(_r1)
660  [left_division_f(_val,_1,
661  boost::phoenix::ref(error_msgs_))])
662  | (lit(".*") > negated_factor_r(_r1)
663  [elt_multiplication_f(_val,_1,
664  boost::phoenix::ref(error_msgs_))])
665  | (lit("./") > negated_factor_r(_r1)
666  [elt_division_f(_val,_1,
667  boost::phoenix::ref(error_msgs_))])
668  )
669  )
670  ;
671 
673  = lit('-') >> negated_factor_r(_r1)
674  [negate_expr_f(_val,_1,_pass,boost::phoenix::ref(error_msgs_))]
675  | lit('!') >> negated_factor_r(_r1)
676  [logical_negate_expr_f(_val,_1,boost::phoenix::ref(error_msgs_))]
677  | lit('+') >> negated_factor_r(_r1) [_val = _1]
678  | exponentiated_factor_r(_r1) [_val = _1]
679  | indexed_factor_r(_r1) [_val = _1];
680 
681 
682  exponentiated_factor_r.name("(optionally) exponentiated factor");
684  = ( indexed_factor_r(_r1) [_val = _1]
685  >> lit('^')
686  > negated_factor_r(_r1)
687  [exponentiation_f(_val,_1,_r1,_pass,
688  boost::phoenix::ref(error_msgs_))]
689  )
690  ;
691 
692  indexed_factor_r.name("(optionally) indexed factor [sub]");
694  = factor_r(_r1) [_val = _1]
695  > * (
696  (+dims_r(_r1))
697  [add_expression_dimss_f(_val, _1, _pass,
698  boost::phoenix::ref(error_msgs_))]
699  |
700  lit("'")
701  [_val = transpose_f(_val, boost::phoenix::ref(error_msgs_))]
702  )
703  ;
704 
705  integrate_ode_r.name("solve ode");
707  %= lit("integrate_ode")
708  > lit('(')
709  > identifier_r // system function name (function only)
710  > lit(',')
711  > expression_g(_r1) // y0
712  > lit(',')
713  > expression_g(_r1) // t0 (data only)
714  > lit(',')
715  > expression_g(_r1) // ts (data only)
716  > lit(',')
717  > expression_g(_r1) // theta
718  > lit(',')
719  > expression_g(_r1) // x (data only)
720  > lit(',')
721  > expression_g(_r1) // x_int (data only)
722  > lit(')') [validate_integrate_ode_f(_val,
723  boost::phoenix::ref(var_map_),
724  _pass,
725  boost::phoenix::ref(error_msgs_))];
726 
727  factor_r.name("factor");
728  factor_r =
729  integrate_ode_r(_r1) [_val = _1]
730  |
731  fun_r(_r1) [set_fun_type_named_f(_val,_1,_r1,_pass,
732  boost::phoenix::ref(error_msgs_))]
733  | variable_r [_val = set_var_type_f(_1,boost::phoenix::ref(var_map_),
734  boost::phoenix::ref(error_msgs_),
735  _pass)]
736  | int_literal_r [_val = _1]
737  | double_literal_r [_val = _1]
738  | ( lit('(')
739  > expression_g(_r1) [_val = _1]
740  > lit(')') )
741  ;
742 
743  int_literal_r.name("integer literal");
745  %= int_
746  >> !( lit('.')
747  | lit('e')
748  | lit('E') );
749 
750 
751  double_literal_r.name("real literal");
753  %= double_;
754 
755 
756  fun_r.name("function and argument expressions");
757  fun_r
758  %= identifier_r // no test yet on valid naming
759  >> args_r(_r1);
760 
761 
762  identifier_r.name("identifier (expression grammar)");
764  %= lexeme[char_("a-zA-Z")
765  >> *char_("a-zA-Z0-9_.")];
766 
767 
768  args_r.name("function argument expressions");
769  args_r
770  %= (lit('(') >> lit(')'))
771  | ( ( lit('(')
772  >> (expression_g(_r1) % ',') )
773  > lit(')') )
774  ;
775 
776 
777  dims_r.name("array dimensions");
778  dims_r
779  %= lit('[')
780  > (expression_g(_r1)
781  [_pass = validate_int_expr3_f(_1,boost::phoenix::ref(error_msgs_))]
782  % ',')
783  > lit(']')
784  ;
785 
786 
787  variable_r.name("variable expression");
788  variable_r
789  %= identifier_r
790  > !lit('('); // negative lookahead to prevent failure in
791  // fun to try to evaluate as variable [cleaner
792  // error msgs]
793 
794  }
795  }
796 }
797 
798 #endif
variable_map & var_map_
expression ts_
Definition: ast.hpp:336
boost::phoenix::function< validate_integrate_ode > validate_integrate_ode_f
void generate_expression(const expression &e, std::ostream &o)
Definition: generator.hpp:249
boost::spirit::qi::rule< Iterator, variable(), whitespace_grammar< Iterator > > variable_r
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > term_r
std::string name_
Definition: ast.hpp:351
variable operator()(variable &var_expr, variable_map &vm, std::ostream &error_msgs, bool &pass) const
const int parameter_origin
Definition: ast.hpp:415
expr_type type_
Definition: ast.hpp:353
const int DOUBLE_T
Definition: ast.hpp:66
static function_signatures & instance()
Definition: ast_def.cpp:143
expression y0_
Definition: ast.hpp:334
const int local_origin
Definition: ast.hpp:418
fun operator()(fun &fun, std::ostream &error_msgs) const
void operator()(expression &expression, std::vector< std::vector< stan::gm::expression > > &dimss, bool &pass, std::ostream &error_msgs) const
expression x_int_
Definition: ast.hpp:339
boost::phoenix::function< multiplication_expr > multiplication_f
bool is_primitive_int() const
Definition: ast_def.cpp:97
void operator()(expression &expr1, const expression &expr2, const var_origin &var_origin, bool &pass, std::ostream &error_msgs) const
boost::spirit::qi::rule< Iterator, int_literal(), whitespace_grammar< Iterator > > int_literal_r
bool is_primitive_double() const
Definition: ast_def.cpp:101
stan::gm::expression_grammar< Iterator > & expression_g
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
BOOST_FUSION_ADAPT_STRUCT(stan::gm::index_op,(stan::gm::expression, expr_)(std::vector< std::vector< stan::gm::expression > >, dimss_))
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > exponentiated_factor_r
boost::phoenix::function< negate_expr > negate_expr_f
bool operator()(const expression &expr, std::stringstream &error_msgs) const
boost::spirit::qi::rule< Iterator, double_literal(), whitespace_grammar< Iterator > > double_literal_r
void operator()(expression &fun_result, fun &fun, const var_origin &var_origin, bool &pass, std::ostream &error_msgs) const
void operator()(const integrate_ode &ode_fun, const variable_map &var_map, bool &pass, std::ostream &error_msgs) const
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
boost::spirit::qi::rule< Iterator, integrate_ode(var_origin), whitespace_grammar< Iterator > > integrate_ode_r
void operator()(expression &expr1, const expression &expr2, std::ostream &error_msgs) const
boost::phoenix::function< validate_int_expr3 > validate_int_expr3_f
size_t get_num_dims(const std::string &name) const
Definition: ast_def.cpp:999
expression_t expr_
Definition: ast.hpp:252
base_expr_type type() const
Definition: ast_def.cpp:111
term_grammar(variable_map &var_map, std::stringstream &error_msgs, expression_grammar< Iterator > &eg)
const int derived_origin
Definition: ast.hpp:417
boost::spirit::qi::rule< Iterator, fun(var_origin), whitespace_grammar< Iterator > > fun_r
boost::phoenix::function< logical_negate_expr > logical_negate_expr_f
expression t0_
Definition: ast.hpp:335
expression theta_
Definition: ast.hpp:337
boost::spirit::qi::rule< Iterator, std::vector< expression >var_origin), whitespace_grammar< Iterator > > args_r
const int VECTOR_T
Definition: ast.hpp:67
const int MATRIX_T
Definition: ast.hpp:69
expr_type type_
Definition: ast.hpp:373
boost::phoenix::function< set_fun_type_named > set_fun_type_named_f
boost::phoenix::function< modulus_expr > modulus_f
void operator()(expression &expr1, const expression &expr2, bool &pass, std::ostream &error_msgs) const
void operator()(expression &expr1, const expression &expr2, std::ostream &error_msgs) const
void operator()(expression &expr1, const expression &expr2, std::ostream &error_msgs) const
boost::phoenix::function< exponentiation_expr > exponentiation_f
bool has_var(const expression &e, const variable_map &var_map)
Definition: ast_def.cpp:675
std::stringstream & error_msgs_
boost::phoenix::function< add_expression_dimss > add_expression_dimss_f
boost::phoenix::function< elt_division_expr > elt_division_f
boost::phoenix::function< division_expr > division_f
void operator()(expression &expr_result, const expression &expr, bool &pass, std::ostream &error_msgs) const
bool has_lp_suffix(const std::string &s)
Definition: ast_def.cpp:1415
boost::phoenix::function< elt_multiplication_expr > elt_multiplication_f
boost::spirit::qi::rule< Iterator, std::string(), whitespace_grammar< Iterator > > identifier_r
std::string system_function_name_
Definition: ast.hpp:333
boost::phoenix::function< set_fun_type > set_fun_type_f
base_expr_type get_base_type(const std::string &name) const
Definition: ast_def.cpp:996
void operator()(expression &expr1, const expression &expr2, std::ostream &error_msgs) const
boost::spirit::qi::rule< Iterator, boost::spirit::qi::locals< bool >, expression(var_origin), whitespace_grammar< Iterator > > factor_r
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:86
bool is_ill_formed() const
Definition: ast_def.cpp:105
const int function_argument_origin_lp
Definition: ast.hpp:420
const int INT_T
Definition: ast.hpp:65
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > negated_factor_r
const int transformed_parameter_origin
Definition: ast.hpp:416
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > indexed_factor_r
boost::phoenix::function< transpose_expr > transpose_f
boost::phoenix::function< set_var_type > set_var_type_f
expr_type get_result_type(const std::string &name, const std::vector< expr_type > &args, std::ostream &error_msgs)
Definition: ast_def.cpp:329
void operator()(expression &expr1, const expression &expr2, std::ostream &error_msgs) const
void operator()(expression &expr_result, const expression &expr, std::ostream &error_msgs) const
std::string name_
Definition: ast.hpp:324
expression operator()(const expression &expr, std::ostream &error_msgs) const
bool exists(const std::string &name) const
Definition: ast_def.cpp:988
boost::phoenix::function< left_division_expr > left_division_f
expr_type expression_type() const
Definition: ast_def.cpp:570
void set_type(const base_expr_type &base_type, size_t num_dims)
Definition: ast_def.cpp:829
int var_origin
Definition: ast.hpp:411
bool is_primitive() const
Definition: ast_def.cpp:93
boost::spirit::qi::rule< Iterator, std::vector< expression >var_origin), whitespace_grammar< Iterator > > dims_r
std::pair< expr_type, std::vector< expr_type > > function_signature_t
Definition: ast.hpp:104

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