concepts.qbk 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. [section:concepts Concepts]
  2. [heading Expression]
  3. _Expr_ is the central concept in _yap_. It must contain at least an _kind_
  4. and a _tuple_ of values.
  5. Here is a summary of the requirements on _Expr_. In the tables below, `E` is
  6. a type that models _Expr_; `e` is an object of type `E`; `Tuple` is an
  7. instantiation of _tuple_; and `t` is an object of type `Tuple`.
  8. [table Expression Requirements
  9. [[Expression] [Type] [Description] [Notes]]
  10. [[`E::kind`] [_kind_] [ The kind of expression `E` is. ] [ Must be a compile-time constant. ]]
  11. [[`e.elements`] [`Tuple`] [ The child expressions of `e`. ] [ The types of the elements must be appropriate to the kind of the expression. ]]
  12. [[`E e{t}`] [] [ Construction/initialization of `e`. ] [ `t` must be stored in `e.elements`. ]]
  13. ]
  14. As stated above, the `elements` data member must match the kind of the expression:
  15. [table Expression Requirements
  16. [[`E::kind`] [`hana::size(e.elements)`] [Possible Tuple Element Types] [Notes]]
  17. [[`expr_kind::expr_ref`] [1] [ Any non-_expr_ref_ _Expr_. ] []]
  18. [[`expr_kind::terminal`] [1] [ Any non-_Expr_. ] [A terminal with a _placeholder_ value will be treated as a placeholder. ]]
  19. [[Any unary operator] [1] [ Any _Expr_. ] []]
  20. [[Any binary operator] [2] [ Any _Expr_. ] []]
  21. [[`expr_kind::if_else`] [3] [ Any _Expr_. ] []]
  22. [[`expr_kind::call`] [Any number >= 1.] [ Any _Expr_. ] []]
  23. ]
  24. [heading ExpressionTemplate]
  25. _ExprTmpl_ is any template with two parameters that, when instantiated with an
  26. _kind_ and a _tuple_, results in an _Expr_.
  27. [heading Transform]
  28. _xform_ takes a _XForm_ as its second parameter. A _XForm_ is a _Callable_
  29. that takes expressions and returns values of unconstrained type. There are
  30. two sorts of overloads _XForm_ may use: _ExprXForm_ and _TagXForm_.
  31. A _XForm_ may have any number of overloads, including none.
  32. [heading ExpressionTransform]
  33. _ExprXForm_ takes an _Expr_ as its only parameter. Here are some examples.
  34. This one takes any _Expr_:
  35. struct xform
  36. {
  37. template <typename Expr>
  38. auto operator() (Expr const & expr)
  39. {
  40. // ...
  41. }
  42. };
  43. This one takes any type of _Expr_ that satisfies the constraints imposed by
  44. its template parameters:
  45. template <typename Expr1, typename Expr2, typename Expr3>
  46. decltype(auto) xform (
  47. boost::yap::expression<
  48. boost::yap::expr_kind::plus,
  49. boost::hana::tuple<
  50. boost::yap::expression<
  51. boost::yap::expr_kind::multiplies,
  52. boost::hana::tuple<
  53. Expr1,
  54. Expr2
  55. >
  56. >,
  57. Expr3
  58. >
  59. > const & expr
  60. ) {
  61. // ...
  62. }
  63. This one takes only a specific type:
  64. decltype(auto) xform (
  65. decltype(term<number>{{0.0}} * number{} + number{}) const & expr
  66. ) {
  67. // ...
  68. }
  69. [heading TagTransform]
  70. _TagXForm_ takes a tag-type as its first parameter, and the individual
  71. elements of an expression as the remaining parameters.
  72. Tags are named such that the tag for an expression with _kind_
  73. `expr_kind::foo` is named `foo_tag`. Here are some examples.
  74. This one takes any terminal that contains a `user::number` (or reference to
  75. such a terminal):
  76. struct xform
  77. {
  78. decltype(auto) operator() (boost::yap::terminal_tag, user::number const & n)
  79. {
  80. // ...
  81. }
  82. };
  83. This one takes any plus expression that contains a pair of `user::number`
  84. terminals (or references to terminals):
  85. decltype(auto) xform (boost::yap::plus_tag, user::number lhs, user::number rhs)
  86. {
  87. // ...
  88. }
  89. This one takes any negation expression:
  90. struct xform
  91. {
  92. template <typename Expr>
  93. decltype(auto) operator() (boost::yap::negate_tag, Expr const & expr)
  94. {
  95. // ...
  96. }
  97. };
  98. This one takes any call expression with two terminals (or references to
  99. terminals) containing values convertible to `double`:
  100. struct xform
  101. {
  102. decltype(auto) operator() (boost::yap::call_tag, tag_type, double a, double b)
  103. {
  104. // ...
  105. }
  106. }
  107. [endsect]