group__group-Monoid.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <!--
  2. Copyright Louis Dionne 2013-2017
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  5. -->
  6. <!-- boost-no-inspect -->
  7. <!-- HTML header for doxygen 1.8.9.1-->
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  9. <html xmlns="http://www.w3.org/1999/xhtml">
  10. <head>
  11. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  12. <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  13. <meta name="generator" content="Doxygen 1.8.15"/>
  14. <title>Boost.Hana: Monoid</title>
  15. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  16. <script type="text/javascript" src="jquery.js"></script>
  17. <script type="text/javascript" src="dynsections.js"></script>
  18. <link href="navtree.css" rel="stylesheet" type="text/css"/>
  19. <script type="text/javascript" src="resize.js"></script>
  20. <script type="text/javascript" src="navtreedata.js"></script>
  21. <script type="text/javascript" src="navtree.js"></script>
  22. <script type="text/javascript">
  23. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  24. $(document).ready(initResizable);
  25. /* @license-end */</script>
  26. <link href="search/search.css" rel="stylesheet" type="text/css"/>
  27. <script type="text/javascript" src="search/searchdata.js"></script>
  28. <script type="text/javascript" src="search/search.js"></script>
  29. <script type="text/javascript">
  30. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  31. $(document).ready(function() { init_search(); });
  32. /* @license-end */
  33. </script>
  34. <script type="text/x-mathjax-config">
  35. MathJax.Hub.Config({
  36. extensions: ["tex2jax.js"],
  37. jax: ["input/TeX","output/HTML-CSS"],
  38. });
  39. // Copyright Louis Dionne 2013-2017
  40. // Distributed under the Boost Software License, Version 1.0.
  41. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  42. MathJax.Hub.Config({
  43. "HTML-CSS": {
  44. linebreaks: {
  45. automatic: true,
  46. width: "75% container"
  47. }
  48. }
  49. });
  50. </script><script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
  51. <link href="doxygen.css" rel="stylesheet" type="text/css" />
  52. <!-- Additional javascript for drawing charts. -->
  53. <script type="text/javascript" src="highcharts.js"></script>
  54. <script type="text/javascript" src="highcharts-data.js"></script>
  55. <script type="text/javascript" src="highcharts-exporting.js"></script>
  56. <script type="text/javascript" src="chart.js"></script>
  57. <script type="text/javascript" src="hana.js"></script>
  58. </head>
  59. <body>
  60. <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
  61. <div id="titlearea">
  62. <table cellspacing="0" cellpadding="0">
  63. <tbody>
  64. <tr style="height: 56px;">
  65. <td id="projectlogo"><img alt="Logo" src="Boost.png"/></td>
  66. <td style="padding-left: 0.5em;">
  67. <div id="projectname">Boost.Hana
  68. &#160;<span id="projectnumber">1.6.0</span>
  69. </div>
  70. <div id="projectbrief">Your standard library for metaprogramming</div>
  71. </td>
  72. <td> <div id="MSearchBox" class="MSearchBoxInactive">
  73. <span class="left">
  74. <img id="MSearchSelect" src="search/mag_sel.png"
  75. onmouseover="return searchBox.OnSearchSelectShow()"
  76. onmouseout="return searchBox.OnSearchSelectHide()"
  77. alt=""/>
  78. <input type="text" id="MSearchField" value="Search" accesskey="S"
  79. onfocus="searchBox.OnSearchFieldFocus(true)"
  80. onblur="searchBox.OnSearchFieldFocus(false)"
  81. onkeyup="searchBox.OnSearchFieldChange(event)"/>
  82. </span><span class="right">
  83. <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
  84. </span>
  85. </div>
  86. </td>
  87. </tr>
  88. </tbody>
  89. </table>
  90. </div>
  91. <!-- end header part -->
  92. <!-- Generated by Doxygen 1.8.15 -->
  93. <script type="text/javascript">
  94. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  95. var searchBox = new SearchBox("searchBox", "search",false,'Search');
  96. /* @license-end */
  97. </script>
  98. </div><!-- top -->
  99. <div id="side-nav" class="ui-resizable side-nav-resizable">
  100. <div id="nav-tree">
  101. <div id="nav-tree-contents">
  102. <div id="nav-sync" class="sync"></div>
  103. </div>
  104. </div>
  105. <div id="splitbar" style="-moz-user-select:none;"
  106. class="ui-resizable-handle">
  107. </div>
  108. </div>
  109. <script type="text/javascript">
  110. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  111. $(document).ready(function(){initNavTree('group__group-Monoid.html','');});
  112. /* @license-end */
  113. </script>
  114. <div id="doc-content">
  115. <!-- window showing the filter options -->
  116. <div id="MSearchSelectWindow"
  117. onmouseover="return searchBox.OnSearchSelectShow()"
  118. onmouseout="return searchBox.OnSearchSelectHide()"
  119. onkeydown="return searchBox.OnSearchSelectKey(event)">
  120. </div>
  121. <!-- iframe showing the search results (closed by default) -->
  122. <div id="MSearchResultsWindow">
  123. <iframe src="javascript:void(0)" frameborder="0"
  124. name="MSearchResults" id="MSearchResults">
  125. </iframe>
  126. </div>
  127. <div class="header">
  128. <div class="summary">
  129. <a href="#var-members">Variables</a> </div>
  130. <div class="headertitle">
  131. <div class="title">Monoid<div class="ingroups"><a class="el" href="group__group-concepts.html">Concepts</a></div></div> </div>
  132. </div><!--header-->
  133. <div class="contents">
  134. <a name="details" id="details"></a><h2 class="groupheader">Description</h2>
  135. <p>The <code>Monoid</code> concept represents data types with an associative binary operation that has an identity. </p>
  136. <p>Specifically, a <a href="http://en.wikipedia.org/wiki/Monoid">Monoid</a> is a basic algebraic structure typically used in mathematics to construct more complex algebraic structures like <code>Group</code>s, <code>Ring</code>s and so on. They are useful in several contexts, notably to define the properties of numbers in a granular way. At its core, a <code>Monoid</code> is a set <code>S</code> of objects along with a binary operation (let's say <code>+</code>) that is associative and that has an identity in <code>S</code>. There are many examples of <code>Monoid</code>s:</p><ul>
  137. <li>strings with concatenation and the empty string as the identity</li>
  138. <li>integers with addition and <code>0</code> as the identity</li>
  139. <li>integers with multiplication and <code>1</code> as the identity</li>
  140. <li>many others...</li>
  141. </ul>
  142. <p>As you can see with the integers, there are some sets that can be viewed as a monoid in more than one way, depending on the choice of the binary operation and identity. The method names used here refer to the monoid of integers under addition; <code>plus</code> is the binary operation and <code>zero</code> is the identity element of that operation.</p>
  143. <h2>Minimal complete definition </h2>
  144. <p><code>plus</code> and <code>zero</code> satisfying the laws</p>
  145. <h2>Laws </h2>
  146. <p>For all objects <code>x</code>, <code>y</code> and <code>z</code> of a <code>Monoid</code> <code>M</code>, the following laws must be satisfied: </p><div class="fragment"><div class="line"><a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(zero&lt;M&gt;(), x) == x <span class="comment">// left zero</span></div><div class="line"><a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(x, zero&lt;M&gt;()) == x <span class="comment">// right zero</span></div><div class="line"><a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(x, <a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(y, z)) == <a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(<a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(x, y), z) <span class="comment">// associativity</span></div></div><!-- fragment --><h2>Concrete models </h2>
  147. <p><code><a class="el" href="structboost_1_1hana_1_1integral__constant.html" title="Compile-time value of an integral type.">hana::integral_constant</a></code></p>
  148. <h2>Free model for non-boolean arithmetic data types </h2>
  149. <p>A data type <code>T</code> is arithmetic if <code>std::is_arithmetic&lt;T&gt;::value</code> is true. For a non-boolean arithmetic data type <code>T</code>, a model of <code>Monoid</code> is automatically defined by setting </p><div class="fragment"><div class="line"><a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(x, y) = (x + y)</div><div class="line">zero&lt;T&gt;() = static_cast&lt;T&gt;(0)</div></div><!-- fragment --><blockquote class="doxtable">
  150. <h4>Rationale for not making <code>bool</code> a <code>Monoid</code> by default</h4>
  151. <p>First, it makes no sense whatsoever to define an additive <code>Monoid</code> over the <code>bool</code> type. Also, it could make sense to define a <code>Monoid</code> with logical conjunction or disjunction. However, C++ allows <code>bool</code>s to be added, and the method names of this concept really suggest addition. In line with the principle of least surprise, no model is provided by default. </p>
  152. </blockquote>
  153. <h2>Structure-preserving functions </h2>
  154. <p>Let <code>A</code> and <code>B</code> be two <code>Monoid</code>s. A function <code>f : A -&gt; B</code> is said to be a <a href="http://en.wikipedia.org/wiki/Monoid#Monoid_homomorphisms">Monoid morphism</a> if it preserves the monoidal structure between <code>A</code> and <code>B</code>. Rigorously, for all objects <code>x, y</code> of data type <code>A</code>, </p><div class="fragment"><div class="line">f(<a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(x, y)) == <a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(f(x), f(y))</div><div class="line">f(zero&lt;A&gt;()) == zero&lt;B&gt;()</div></div><!-- fragment --><p> Functions with these properties interact nicely with <code>Monoid</code>s, which is why they are given such a special treatment. </p>
  155. <table class="memberdecls">
  156. <tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="var-members"></a>
  157. Variables</h2></td></tr>
  158. <tr class="memitem:gaeb5d4a1e967e319712f9e4791948896c"><td class="memItemLeft" align="right" valign="top">constexpr auto&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">boost::hana::plus</a></td></tr>
  159. <tr class="memdesc:gaeb5d4a1e967e319712f9e4791948896c"><td class="mdescLeft">&#160;</td><td class="mdescRight">Associative binary operation on a <code>Monoid</code>. <a href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">More...</a><br /></td></tr>
  160. <tr class="separator:gaeb5d4a1e967e319712f9e4791948896c"><td class="memSeparator" colspan="2">&#160;</td></tr>
  161. <tr class="memitem:gad459ac17b6bab8ead1cae7de0032f3c6"><td class="memTemplParams" colspan="2">template&lt;typename M &gt; </td></tr>
  162. <tr class="memitem:gad459ac17b6bab8ead1cae7de0032f3c6"><td class="memTemplItemLeft" align="right" valign="top">constexpr auto&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="group__group-Monoid.html#gad459ac17b6bab8ead1cae7de0032f3c6">boost::hana::zero</a></td></tr>
  163. <tr class="memdesc:gad459ac17b6bab8ead1cae7de0032f3c6"><td class="mdescLeft">&#160;</td><td class="mdescRight">Identity of <code>plus</code>. <a href="group__group-Monoid.html#gad459ac17b6bab8ead1cae7de0032f3c6">More...</a><br /></td></tr>
  164. <tr class="separator:gad459ac17b6bab8ead1cae7de0032f3c6"><td class="memSeparator" colspan="2">&#160;</td></tr>
  165. </table>
  166. <h2 class="groupheader">Variable Documentation</h2>
  167. <a id="gaeb5d4a1e967e319712f9e4791948896c"></a>
  168. <h2 class="memtitle"><span class="permalink"><a href="#gaeb5d4a1e967e319712f9e4791948896c">&#9670;&nbsp;</a></span>plus</h2>
  169. <div class="memitem">
  170. <div class="memproto">
  171. <table class="memname">
  172. <tr>
  173. <td class="memname">constexpr auto boost::hana::plus</td>
  174. </tr>
  175. </table>
  176. </div><div class="memdoc">
  177. <p><code>#include &lt;<a class="el" href="fwd_2plus_8hpp.html">boost/hana/fwd/plus.hpp</a>&gt;</code></p>
  178. <b>Initial value:</b><div class="fragment"><div class="line">= [](<span class="keyword">auto</span>&amp;&amp; x, <span class="keyword">auto</span>&amp;&amp; y) -&gt; decltype(<span class="keyword">auto</span>) {</div><div class="line"> <span class="keywordflow">return</span> tag-dispatched;</div><div class="line"> }</div></div><!-- fragment -->
  179. <p>Associative binary operation on a <code>Monoid</code>. </p>
  180. <dl class="params"><dt>Parameters</dt><dd>
  181. <table class="params">
  182. <tr><td class="paramname">x,y</td><td>Two objects to combine with the <code>Monoid</code>'s binary operation.</td></tr>
  183. </table>
  184. </dd>
  185. </dl>
  186. <h2>Cross-type version of the method </h2>
  187. <p>The <code>plus</code> method is "overloaded" to handle distinct data types with certain properties. Specifically, <code>plus</code> is defined for <em>distinct</em> data types <code>A</code> and <code>B</code> such that</p><ol type="1">
  188. <li><code>A</code> and <code>B</code> share a common data type <code>C</code>, as determined by the <code>common</code> metafunction</li>
  189. <li><code>A</code>, <code>B</code> and <code>C</code> are all <code>Monoid</code>s when taken individually</li>
  190. <li><code>to&lt;C&gt; : A -&gt; B</code> and <code>to&lt;C&gt; : B -&gt; C</code> are <code>Monoid</code>-embeddings, as determined by the <code>is_embedding</code> metafunction.</li>
  191. </ol>
  192. <p>The definition of <code>plus</code> for data types satisfying the above properties is obtained by setting </p><div class="fragment"><div class="line"><a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(x, y) = <a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(to&lt;C&gt;(x), to&lt;C&gt;(y))</div></div><!-- fragment --><h2>Example </h2>
  193. <div class="fragment"><div class="line"><span class="comment">// Copyright Louis Dionne 2013-2017</span></div><div class="line"><span class="comment">// Distributed under the Boost Software License, Version 1.0.</span></div><div class="line"><span class="comment">// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="assert_8hpp.html">boost/hana/assert.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="equal_8hpp.html">boost/hana/equal.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="integral__constant_8hpp.html">boost/hana/integral_constant.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="plus_8hpp.html">boost/hana/plus.hpp</a>&gt;</span></div><div class="line"><span class="keyword">namespace </span>hana = <a class="code" href="namespaceboost_1_1hana.html">boost::hana</a>;</div><div class="line"></div><div class="line"></div><div class="line"><a class="code" href="group__group-assertions.html#ga2c5006540936d9f8880e3a39f4fcc035">BOOST_HANA_CONSTANT_CHECK</a>(<a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">hana::plus</a>(hana::int_c&lt;3&gt;, hana::int_c&lt;5&gt;) == hana::int_c&lt;8&gt;);</div><div class="line">static_assert(<a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">hana::plus</a>(1, 2) == 3, <span class="stringliteral">&quot;&quot;</span>);</div><div class="line">static_assert(<a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">hana::plus</a>(1.5f, 2.4) == 3.9, <span class="stringliteral">&quot;&quot;</span>);</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main() { }</div></div><!-- fragment -->
  194. </div>
  195. </div>
  196. <a id="gad459ac17b6bab8ead1cae7de0032f3c6"></a>
  197. <h2 class="memtitle"><span class="permalink"><a href="#gad459ac17b6bab8ead1cae7de0032f3c6">&#9670;&nbsp;</a></span>zero</h2>
  198. <div class="memitem">
  199. <div class="memproto">
  200. <div class="memtemplate">
  201. template&lt;typename M &gt; </div>
  202. <table class="memname">
  203. <tr>
  204. <td class="memname">constexpr auto boost::hana::zero</td>
  205. </tr>
  206. </table>
  207. </div><div class="memdoc">
  208. <p><code>#include &lt;<a class="el" href="fwd_2zero_8hpp.html">boost/hana/fwd/zero.hpp</a>&gt;</code></p>
  209. <b>Initial value:</b><div class="fragment"><div class="line">= []() -&gt; decltype(<span class="keyword">auto</span>) {</div><div class="line"> <span class="keywordflow">return</span> tag-dispatched;</div><div class="line"> }</div></div><!-- fragment -->
  210. <p>Identity of <code>plus</code>. </p>
  211. <dl class="tparams"><dt>Template Parameters</dt><dd>
  212. <table class="tparams">
  213. <tr><td class="paramname">M</td><td>The tag (a <code>Monoid</code>) of the returned identity.</td></tr>
  214. </table>
  215. </dd>
  216. </dl>
  217. <h2>Example </h2>
  218. <div class="fragment"><div class="line"><span class="comment">// Copyright Louis Dionne 2013-2017</span></div><div class="line"><span class="comment">// Distributed under the Boost Software License, Version 1.0.</span></div><div class="line"><span class="comment">// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="assert_8hpp.html">boost/hana/assert.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="equal_8hpp.html">boost/hana/equal.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="integral__constant_8hpp.html">boost/hana/integral_constant.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="zero_8hpp.html">boost/hana/zero.hpp</a>&gt;</span></div><div class="line"><span class="keyword">namespace </span>hana = <a class="code" href="namespaceboost_1_1hana.html">boost::hana</a>;</div><div class="line"></div><div class="line"></div><div class="line"><a class="code" href="group__group-assertions.html#ga2c5006540936d9f8880e3a39f4fcc035">BOOST_HANA_CONSTANT_CHECK</a>(<a class="code" href="group__group-Monoid.html#gad459ac17b6bab8ead1cae7de0032f3c6">hana::zero</a>&lt;hana::integral_constant_tag&lt;int&gt;&gt;() == hana::int_c&lt;0&gt;);</div><div class="line">static_assert(hana::zero&lt;long&gt;() == 0l, <span class="stringliteral">&quot;&quot;</span>);</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main() { }</div></div><!-- fragment -->
  219. </div>
  220. </div>
  221. </div><!-- contents -->
  222. </div><!-- doc-content -->
  223. <!--
  224. Copyright Louis Dionne 2013-2017
  225. Distributed under the Boost Software License, Version 1.0.
  226. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  227. -->
  228. <!-- boost-no-inspect -->
  229. <!-- HTML footer for doxygen 1.8.9.1-->
  230. <!-- start footer part -->
  231. <div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  232. <ul>
  233. </ul>
  234. </div>
  235. </body>
  236. </html>