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