• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5<title>Chapter 47. Boost.YAP</title>
6<link rel="stylesheet" href="../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="libraries.html" title="Part I. The Boost C++ Libraries (BoostBook Subset)">
10<link rel="prev" href="xpressive/appendices.html" title="Appendices">
11<link rel="next" href="boost_yap/manual.html" title="Manual">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../boost.png"></td>
16<td align="center"><a href="../../index.html">Home</a></td>
17<td align="center"><a href="../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<a accesskey="p" href="xpressive/appendices.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="boost_yap/manual.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
25</div>
26<div class="chapter">
27<div class="titlepage"><div>
28<div><h2 class="title">
29<a name="yap"></a>Chapter 47. Boost.YAP</h2></div>
30<div><div class="author"><h3 class="author">
31<span class="firstname">Zach</span> <span class="surname">Laine</span>
32</h3></div></div>
33<div><p class="copyright">Copyright © 2018 T. Zachary Laine</p></div>
34<div><div class="legalnotice">
35<a name="yap.legal"></a><p>
36        Distributed under the Boost Software License, Version 1.0. (See accompanying
37        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
38      </p>
39</div></div>
40</div></div>
41<div class="toc">
42<p><b>Table of Contents</b></p>
43<dl class="toc">
44<dt><span class="section"><a href="yap.html#boost_yap.introduction">Introduction</a></span></dt>
45<dt><span class="section"><a href="boost_yap/manual.html">Manual</a></span></dt>
46<dd><dl>
47<dt><span class="section"><a href="boost_yap/manual.html#boost_yap.manual.an_expression_template_primer">An Expression
48      Template Primer</a></span></dt>
49<dt><span class="section"><a href="boost_yap/manual.html#boost_yap.manual.the_yap_way">The YAP Way</a></span></dt>
50<dt><span class="section"><a href="boost_yap/manual.html#boost_yap.manual.expressions">Expressions</a></span></dt>
51<dt><span class="section"><a href="boost_yap/manual.html#boost_yap.manual.mix_and_match_expression_templates">Mix-and-Match
52      Expression Templates</a></span></dt>
53<dt><span class="section"><a href="boost_yap/manual.html#boost_yap.manual.kinds_of_expressions">Kinds of Expressions</a></span></dt>
54<dt><span class="section"><a href="boost_yap/manual.html#boost_yap.manual.operators">Operators</a></span></dt>
55<dt><span class="section"><a href="boost_yap/manual.html#boost_yap.manual.transforming_expressions">Transforming
56      Expressions</a></span></dt>
57<dt><span class="section"><a href="boost_yap/manual.html#boost_yap.manual.evaluating_expressions">Evaluating Expressions</a></span></dt>
58<dt><span class="section"><a href="boost_yap/manual.html#boost_yap.manual.operator_macros">Operator Macros</a></span></dt>
59<dt><span class="section"><a href="boost_yap/manual.html#boost_yap.manual.how_expression_operands_are_treated">How
60      Expression Operands Are Treated</a></span></dt>
61<dt><span class="section"><a href="boost_yap/manual.html#boost_yap.manual.printing">Printing</a></span></dt>
62<dt><span class="section"><a href="boost_yap/manual.html#boost_yap.manual.examples">Examples</a></span></dt>
63<dt><span class="section"><a href="boost_yap/manual.html#boost_yap.manual.header_organization">Header Organization</a></span></dt>
64<dt><span class="section"><a href="boost_yap/manual.html#boost_yap.manual.configuration">Configuration</a></span></dt>
65<dt><span class="section"><a href="boost_yap/manual.html#boost_yap.manual.object_code">Object Code</a></span></dt>
66</dl></dd>
67<dt><span class="section"><a href="boost_yap/concepts.html">Concepts</a></span></dt>
68<dt><span class="section"><a href="boost_yap/compiler_support.html">Compiler Support</a></span></dt>
69<dt><span class="section"><a href="boost_yap/dependencies.html">Dependencies</a></span></dt>
70<dt><span class="section"><a href="yap/reference.html">Reference</a></span></dt>
71<dd><dl><dt><span class="section"><a href="yap/reference.html#headers">Headers</a></span></dt></dl></dd>
72<dt><span class="section"><a href="boost_yap/rationale.html">Rationale</a></span></dt>
73</dl>
74</div>
75<div class="section">
76<div class="titlepage"><div><div><h2 class="title" style="clear: both">
77<a name="boost_yap.introduction"></a><a class="link" href="yap.html#boost_yap.introduction" title="Introduction">Introduction</a>
78</h2></div></div></div>
79<p>
80      "I like to start documentation with a quote. A nice, pithy one."
81    </p>
82<p>
83      <span class="emphasis"><em><span class="bold"><strong>— Eric Niebler (paraphrased)</strong></span></em></span>
84    </p>
85<h4>
86<a name="boost_yap.introduction.h0"></a>
87      <span class="phrase"><a name="boost_yap.introduction.motivation"></a></span><a class="link" href="yap.html#boost_yap.introduction.motivation">Motivation</a>
88    </h4>
89<p>
90      Expression templates are rad. They are used in lots of libraries; here are
91      just three of the most impressive:
92    </p>
93<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
94<li class="listitem">
95          <a href="http://boost-spirit.com" target="_top">Boost.Spirit</a> allows you to
96          write an EBNF-style grammar that gets transformed into a PEG parser.
97        </li>
98<li class="listitem">
99          <a href="http://eigen.tuxfamily.org" target="_top">Eigen</a> allows you to do
100          linear algebra using a very natural and mathematical expression syntax
101          that <a href="http://eigen.tuxfamily.org" target="_top">Eigen</a> uses to heavily
102          optimize your expressions.
103        </li>
104<li class="listitem">
105          <a href="https://www.lri.fr/~falcou/nt2" target="_top">NT2</a> takes slightly
106          modified MatLab code and allows it to be parsed and run as highly optimized
107          C++ code.
108        </li>
109</ul></div>
110<p>
111      However, this can come at a high cost. Expression templates are costly to implement
112      and maintain. Each of <a href="http://eigen.tuxfamily.org" target="_top">Eigen</a>
113      and Boost.Ublas has a large volume of complex expression template code that
114      cannot be reused elsewhere.
115    </p>
116<p>
117      With the language facilities available in the C++14 and C++17 standards, an
118      expression template library is now straightforward to write and use, and has
119      very reasonable compile times.
120    </p>
121<p>
122      As a quick example, let's say we are doing a bit of matrix math, and we write
123      this statement:
124    </p>
125<pre class="programlisting"><span class="identifier">D</span> <span class="special">=</span> <span class="identifier">A</span> <span class="special">*</span> <span class="identifier">B</span> <span class="special">+</span> <span class="identifier">C</span><span class="special">;</span>
126</pre>
127<p>
128      in which all the variables are matrices. It turns out that making a temporary
129      for <code class="computeroutput"><span class="identifier">A</span> <span class="special">*</span>
130      <span class="identifier">B</span></code> and then another temporary for
131      the resulting product plus <code class="computeroutput"><span class="identifier">C</span></code>
132      is very inefficient. Most matrix math libraries will have a single function
133      that does it in one go:
134    </p>
135<pre class="programlisting"><span class="identifier">mul_add_assign</span><span class="special">(</span><span class="identifier">D</span><span class="special">,</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">,</span> <span class="identifier">C</span><span class="special">);</span>
136</pre>
137<p>
138      If you use a matrix library that offers both kinds of syntax, you have to notice
139      when some bit of operator-using code should be replaced with some more efficient
140      function; this is tedious and error-prone. If the library does not provide
141      the operator syntax at all, only providing the more-efficient function calls,
142      code using the library is a lot less writable and readable.
143    </p>
144<p>
145      Using Boost.YAP, you can write some library code that enables expressions like
146      <code class="computeroutput"><span class="identifier">D</span> <span class="special">=</span>
147      <span class="identifier">A</span> <span class="special">*</span> <span class="identifier">B</span> <span class="special">+</span> <span class="identifier">C</span></code>
148      to be automatically transformed into expressions like <code class="computeroutput"><span class="identifier">mul_add_assign</span><span class="special">(</span><span class="identifier">D</span><span class="special">,</span>
149      <span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">,</span> <span class="identifier">C</span><span class="special">)</span></code>.
150    </p>
151<p>
152      Consider another example. Many of us have used Unix command line tools to remove
153      duplicate lines in a file:
154    </p>
155<pre class="programlisting"><span class="identifier">sort</span> <span class="identifier">file_with_duplicates</span> <span class="special">|</span> <span class="identifier">uniq</span> <span class="special">&gt;</span> <span class="identifier">file_without_duplicates</span>
156</pre>
157<p>
158      We can do something very similar with the standard algorithms, of course:
159    </p>
160<p>
161</p>
162<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">v1</span> <span class="special">=</span> <span class="special">{</span><span class="number">0</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">7</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="number">3</span><span class="special">,</span> <span class="number">8</span><span class="special">};</span>
163<span class="identifier">std</span><span class="special">::</span><span class="identifier">sort</span><span class="special">(</span><span class="identifier">v1</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">v1</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>
164<span class="keyword">auto</span> <span class="identifier">it</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique</span><span class="special">(</span><span class="identifier">v1</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">v1</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>
165<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="keyword">const</span> <span class="identifier">v2</span><span class="special">(</span><span class="identifier">v1</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">it</span><span class="special">);</span>
166<span class="identifier">assert</span><span class="special">(</span><span class="identifier">v2</span> <span class="special">==</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;({</span><span class="number">0</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">3</span><span class="special">,</span> <span class="number">7</span><span class="special">,</span> <span class="number">8</span><span class="special">}));</span>
167</pre>
168<p>
169    </p>
170<p>
171      However, it would be much better if our code did exactly that, but with a more
172      concise syntax:
173    </p>
174<p>
175</p>
176<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">v1</span> <span class="special">=</span> <span class="special">{</span><span class="number">0</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">7</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="number">3</span><span class="special">,</span> <span class="number">8</span><span class="special">};</span>
177<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="keyword">const</span> <span class="identifier">v2</span> <span class="special">=</span> <span class="identifier">sort</span><span class="special">(</span><span class="identifier">v1</span><span class="special">)</span> <span class="special">|</span> <span class="identifier">unique</span><span class="special">;</span>
178<span class="identifier">assert</span><span class="special">(</span><span class="identifier">v2</span> <span class="special">==</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;({</span><span class="number">0</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">3</span><span class="special">,</span> <span class="number">7</span><span class="special">,</span> <span class="number">8</span><span class="special">}));</span>
179</pre>
180<p>
181    </p>
182<p>
183      This looks much more similar to the Unix command line above. (Let's pretend
184      that <a href="https://github.com/ericniebler/range-v3" target="_top">Range-v3</a>
185      doesn't already do almost exactly this.)
186    </p>
187<p>
188      Boost.YAP can be used to do both of these things, in a pretty small amount
189      of code. In fact, you can jump right into the <a class="link" href="boost_yap/manual.html#boost_yap.manual.examples.pipable_algorithms" title="Pipable Algorithms">Pipable
190      Algorithms</a> example if you want to see how the second one can be implemented.
191    </p>
192<h4>
193<a name="boost_yap.introduction.h1"></a>
194      <span class="phrase"><a name="boost_yap.introduction.features"></a></span><a class="link" href="yap.html#boost_yap.introduction.features">Features</a>
195    </h4>
196<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
197<li class="listitem">
198          Simple <a class="link" href="boost_yap/concepts.html#boost_yap.concepts.expressiontemplate">ExpressionTemplate</a>
199          and <a class="link" href="boost_yap/concepts.html#boost_yap.concepts.expression">Expression</a> concepts
200          easily modeled by user code. Member and non-member functions on <a class="link" href="boost_yap/concepts.html#boost_yap.concepts.expressiontemplate">ExpressionTemplates</a>
201          and <a class="link" href="boost_yap/concepts.html#boost_yap.concepts.expression">Expressions</a> can
202          be added with compact macros, and a reference template that models <a class="link" href="boost_yap/concepts.html#boost_yap.concepts.expressiontemplate">ExpressionTemplate</a>
203          exists for prototyping or experimentation.
204        </li>
205<li class="listitem">
206          Evaluation of Boost.YAP expressions matches the semantics of builtin C++
207          expressions as closely as possible. This leads to clearer understanding
208          of the semantics of expression evaluation, because the definitions are
209          local to the types involved.
210        </li>
211<li class="listitem">
212          Expressions may be transformed explicitly in a user-defined way. This is
213          accomplished with overloaded call operators in a transform class, which
214          are matched against subexpressions in the overall expression. While these
215          member functions may transform a subexpression into anything, a common
216          pattern is to transform only some subexpressions into either new subexpressions
217          or appropriate values and to leave other subexpressions unchanged. This
218          <code class="computeroutput"><span class="identifier">evaluate</span><span class="special">(</span><span class="identifier">transform</span><span class="special">(</span><span class="identifier">expr</span><span class="special">))</span></code>
219          idiom is expected to be one of the most common ways of using Yap to manipulate
220          and evaluate expressions.
221        </li>
222<li class="listitem">
223          Functions that operate on or create expressions. Functions are provided
224          (and used within Boost.YAP) that manipulate expressions or their subexpressions.
225          These simplify the process of writing user-defined transforms, for example.
226        </li>
227</ul></div>
228</div>
229</div>
230<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
231<td align="left"><p><small>Last revised: August 11, 2020 at 15:03:11 GMT</small></p></td>
232<td align="right"><div class="copyright-footer"></div></td>
233</tr></table>
234<hr>
235<div class="spirit-nav">
236<a accesskey="p" href="xpressive/appendices.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="boost_yap/manual.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
237</div>
238</body>
239</html>
240