
Boost : 
Subject: Re: [boost] [yap] Reannouncing Yap
From: Zach Laine (whatwasthataddress_at_[hidden])
Date: 20170331 15:20:36
Thanks, Edward. I keep mistakenly thinking that "expression template
library" is a sufficient explanation on the Boost list, even though I know
that this is not the case.
Yap allows you to capture a C++ expression as an expression tree that can
be subsequently transformed and/or evaluated. For instance, here is some
enduser code using lazy vectors. The lazy_vector type and the associated
operations are defined using the Yap library. This is mostly a copypaste
from one of the Yap examples, here:
https://tzlaine.github.io/yap/doc/html/boost_yap__proposed_/manual/examples/lazy_vector.html
int main ()
{
// A lazy vector contains a std::vector<double> value
lazy_vector v1{std::vector<double>(4, 1.0)};
lazy_vector v2{std::vector<double>(4, 2.0)};
lazy_vector v3{std::vector<double>(4, 3.0)};
// This statement does not create a temporary vector, and
// only uses the elements v2[2] and v3[2]. It also generates
// the exact same object code as "x2[2] + x3[2]", where x2
// and x3 are nonlazy, plain old std::vector<double>s.
double d1 = (v2 + v3)[2];
std::cout << d1 << "\n";
// This statement does an elementwise operation, creating
// no temporaries.
v1 += v2  v3;
std::cout << '{' << v1[0] << ',' << v1[1]
<< ',' << v1[2] << ',' << v1[3] << '}' << "\n";
// This expression is disallowed because it does not conform to the
// implicit grammar. operator+= is only defined on terminals, not
// arbitrary expressions.
// (v2 + v3) += v1;
return 0;
}
The Yap code that you must write in order to make this enduser code work
is fairly small:
// This transform turns a terminal of std::vector<double> into a terminal
// containing the nth double in that vector. Think of it as turning our
// expression of vectors into an expression of scalars.
struct take_nth
{
boost::yap::terminal<lazy_vector_expr, double>
operator() (boost::yap::terminal<lazy_vector_expr, std::vector<double>>
const & expr);
std::size_t n;
};
// A custom expression template that defines lazy + and  operators that
// produce expressions, and an eager [] operator that returns the nth
element
// of the expression.
template <boost::yap::expr_kind Kind, typename Tuple>
struct lazy_vector_expr
{
using this_type = lazy_vector_expr<Kind, Tuple>;
static const boost::yap::expr_kind kind = Kind;
Tuple elements;
BOOST_YAP_USER_BINARY_OPERATOR_MEMBER(plus, this_type,
::lazy_vector_expr)
BOOST_YAP_USER_BINARY_OPERATOR_MEMBER(minus, this_type,
::lazy_vector_expr)
// Note that this does not return an expression; it is greedily
evaluated.
auto operator[] (std::size_t n) const;
};
template <boost::yap::expr_kind Kind, typename Tuple>
auto lazy_vector_expr<Kind, Tuple>::operator[] (std::size_t n) const
{ return boost::yap::evaluate(boost::yap::transform(*this, take_nth{n})); }
boost::yap::terminal<lazy_vector_expr, double>
take_nth::operator() (boost::yap::terminal<lazy_vector_expr,
std::vector<double>> const & expr)
{
double x = boost::yap::value(expr)[n];
return boost::yap::make_terminal<lazy_vector_expr,
double>(std::move(x));
}
// In order to define the += operator with the semantics we want, it's
// convenient to derive a terminal type from a terminal instantiation of
// lazy_vector_expr. note that we could have written a template
// specialization here instead  either one would work. That would of
course
// have required more typing.
struct lazy_vector :
lazy_vector_expr<
boost::yap::expr_kind::terminal,
boost::hana::tuple<std::vector<double>>
>
{
lazy_vector () {}
explicit lazy_vector (std::vector<double> && vec)
{ elements = boost::hana::tuple<std::vector<double>>(std::move(vec)); }
template <boost::yap::expr_kind Kind, typename Tuple>
lazy_vector & operator+= (lazy_vector_expr<Kind, Tuple> const & rhs)
{
std::vector<double> & this_vec = boost::yap::value(*this);
for (int i = 0, size = (int)this_vec.size(); i < size; ++i) {
this_vec[i] += rhs[i];
}
return *this;
}
};
More details and way more examples can be found at:
Zach
On Thu, Mar 30, 2017 at 8:02 PM, Edward Diener via Boost <
boost_at_[hidden]> wrote:
> On 3/18/2017 6:55 PM, Zach Laine via Boost wrote:
>
>> I posted 23 months ago about Yap, an expression template library I've
>> written that I intend to propose for Boost.
>>
>> This is just a reminder that the library exists, and where to find it.
>>
>> I'm giving a talk about it at C++Now 2017, and some time after that I
>> intend to submit it to the queue. Louis Dionne has offered to serve as
>> review manager when the time comes.
>>
>> You can find the main repo on GitHub:
>>
>> https://github.com/tzlaine/yap
>>
>> And online docs are here:
>>
>> https://tzlaine.github.io/yap
>>
>
> You might want to mention here what the purpose of the library is and when
> it might be used by developers, in order to interest others.
>
>
>> Zach
>>
>
>
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman
> /listinfo.cgi/boost
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk