{"id":362,"date":"2017-09-23T00:13:18","date_gmt":"2017-09-22T16:13:18","guid":{"rendered":"https:\/\/colliot.me\/?p=362"},"modified":"2017-11-20T10:24:29","modified_gmt":"2017-11-20T02:24:29","slug":"%e3%80%90c-%e6%a8%a1%e6%9d%bf%e5%85%83%e7%bc%96%e7%a8%8b%e5%85%a5%e9%97%a8%e3%80%91%e5%9c%a8%e7%bc%96%e8%af%91%e6%9c%9f%e5%ae%9e%e7%8e%b0-peano-%e6%95%b0","status":"publish","type":"post","link":"https:\/\/colliot.org\/en\/2017\/09\/%e3%80%90c-%e6%a8%a1%e6%9d%bf%e5%85%83%e7%bc%96%e7%a8%8b%e5%85%a5%e9%97%a8%e3%80%91%e5%9c%a8%e7%bc%96%e8%af%91%e6%9c%9f%e5%ae%9e%e7%8e%b0-peano-%e6%95%b0\/","title":{"rendered":"Implement Compile Time Peano Numbers &#8211; A Primer in C++ Template Metaprogramming"},"content":{"rendered":"<p><\/p>\n<h3>Fundamentals<\/h3>\n<h4>Function on Types<\/h4>\n<p>We all know that C++ templates can receive types as &#8220;parameters&#8221;. In the same spirit we could can &#8220;return values&#8221;s, so that we can form a function. The basic paradigm is the following:<\/p>\n<pre class=\"prettyprint lang-c_cpp\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">template&lt;class T&gt;\r\nstruct Computed {\r\n  using type = T;\r\n}<\/pre>\n<p>In this way we constructed a type level function named <code>Computed<\/code>, with one parameter\u00a0 and result the parameter itself. We use the function as <code>Computed&lt;float&gt;::type<\/code>, the result being <code>float<\/code>\u00a0itself.<\/p>\n<p><!--more--><\/p>\n<p>Why an extra <code>struct<\/code>wrapper instead of directly defining the &#8220;function&#8221;? That&#8217;s because partialization on <code>using<\/code>\u00a0is not allowed. Such codes are invalid:<\/p>\n<pre class=\"prettyprint lang-c_cpp\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">template &lt;class T&gt;\r\nusing computed&lt;T&gt; = T;\r\n\r\ntemplate&lt;&gt;\r\nusing computed&lt;int&gt; = double;<\/pre>\n<div>\n<div>As of why this isn&#8217;t supported by C++, I&#8217;m not sure.<\/div>\n<\/div>\n<h4>Partialization<\/h4>\n<div>\n<div>What is partialization? You can take it as &#8220;pattern matching&#8221; in Haskell.<\/div>\n<\/div>\n<pre class=\"prettyprint lang-c_cpp\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">template&lt;class T&gt;\r\nstruct Computed {\r\n  using type = T;\r\n}\r\n\r\ntemplate&lt;&gt;\r\nstruct Computed&lt;int&gt; {\r\n  using type = double;\r\n}<\/pre>\n<div>\n<div>Thus, <code>Computed&lt;bool&gt;::type<\/code> would become <code>bool<\/code>, while <code>Computed&lt;int&gt;::type<\/code> would (specially) be <code>double<\/code>. Certainly, there are a set of rules for the matching process. E.g. the most &#8220;specific&#8221; case gets matched first. This is somewhat from Haskell, where the first-defined case comes first. The equivalent code in Haskell is:<\/div>\n<\/div>\n<pre class=\"prettyprint lang-haskell\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">data Type = Bool | Double | Int\r\n\r\ncomputed :: Type -&gt; Type\r\ncomputed Int = Double\r\ncomputed t = t<\/pre>\n<div>\n<div>With this correspondence in mind, if you are familiar with the implementation of Peano numbers in Haskell, then you are ready to do it in C++. If you aren&#8217;t, don&#8217;t worry. We are going to explore the Peano numbers next.<\/div>\n<\/div>\n<h3>Peano numbers<\/h3>\n<div>\n<div>What are the Peano numbers? They are natural numbers defined with &#8220;induction&#8221;. More precisely, it would be a system of axioms that behaves like the &#8220;natural&#8221; numbers in naive intuition, a.k.a. a possible axiomatization of the natural numbers. There are only two symbols in this system, a <code>Zero<\/code> for <code>0<\/code>, and a <code>Succ<\/code> for the successive. Thus <code>1<\/code> would be <code>Succ&lt;Zero&gt;<\/code>, <code>2<\/code> would be <code>Succ&lt;Succ&lt;Zero&gt;&gt;<\/code>, and so on. This is but the heuristic demonstration of &#8220;induction&#8221;.<\/div>\n<div>We can represent the two symbols in C++ as follows:<\/div>\n<\/div>\n<pre class=\"prettyprint lang-c_cpp\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">struct Peano {};\r\nstruct Zero: Peano {};\r\ntemplate&lt;class T&gt;\r\nstruct Succ: Peano {};<\/pre>\n<div>\n<div>Then what is &#8220;addition&#8221;? Starting from an example. We are need to define a template (type level function) with two parameters:<\/div>\n<\/div>\n<pre class=\"prettyprint lang-c_cpp\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">template&lt;class T1, class T2&gt;\r\nstruct Add {\r\n  using type = ???;\r\n}<\/pre>\n<div>\n<div>satisfying the laws in intuition, such as <code>2+1=3<\/code>, or <code>Add&lt;Succ&lt;Succ&lt;Zero&gt;&gt;, Succ&lt;Zero&gt;&gt;::type = Succ&lt;Succ&lt;Succ&lt;Zero&gt;&gt;&gt;<\/code>. Certainly the latter expression is not valid C++, since there is no equality operator for types. Instead, we should use the <code>std::is_same&lt;T1, T2&gt;<\/code> function, which is predefined in <code>&lt;type_traits&gt;<\/code>. Or you can implement it yourself, again using partialization. This is a possible implementation:<\/div>\n<\/div>\n<pre class=\"prettyprint lang-c_cpp\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">template&lt;class T, class U&gt;\r\nstruct is_same {\r\n  static constexpr bool value = false;\r\n};\r\n \r\ntemplate&lt;class T&gt;\r\nstruct is_same&lt;T, T&gt; {\r\n  static constexpr bool value = true;\r\n};<\/pre>\n<div>\n<div>Then how do we actually implement the addition? We can certainly implement a partialization for each specific case. An example is:<\/div>\n<\/div>\n<pre class=\"prettyprint lang-c_cpp\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">template&lt;&gt;\r\nstruct Add&lt;Succ&lt;Succ&lt;Zero&gt;&gt;, Succ&lt;Zero&gt;&gt; {\r\n  using type = Succ&lt;Succ&lt;Succ&lt;Zero&gt;&gt;&gt;;\r\n}<\/pre>\n<div>\n<div>Such exhaustion could actually work, since most C++ compilers only allow a finite level of nested templates (255 for clang by default). But such a &#8220;solution&#8221; is too silly to accept. Indeed, addition can be covered with only 2 rules: <code>0 + b = b, (Succ a) + b = Succ (a + b)<\/code>. In C++ they are:<\/div>\n<\/div>\n<pre class=\"prettyprint lang-c_cpp\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">template&lt;class T1, class T2&gt;\r\nstruct Add;\r\n\r\ntemplate&lt;class T&gt;\r\nstruct Add&lt;Zero, T&gt; {\r\n  using type = T;\r\n};\r\n\r\ntemplate&lt;class T1, class T2&gt;\r\nstruct Add&lt;Succ&lt;T1&gt;, T2&gt; {\r\n  using type = Succ&lt;typename Add&lt;T1, T2&gt;::type&gt;\r\n};<\/pre>\n<div>\n<div>Notice that <code>typename<\/code> &#8212; the compiler doesn&#8217;t know whether the <code>::type<\/code> is a member type or a member variable, so we need this keyword as a hint.<\/div>\n<div>Hereby we&#8217;ve finished the definition of addition. We can do some test to verify if <code>std::is_same&lt;Add&lt;Succ&lt;Succ&lt;Zero&gt;&gt;, Succ&lt;Zero&gt;&gt;::type, Succ&lt;Succ&lt;Succ&lt;Zero&gt;&gt;&gt;&gt;::value == true<\/code> or so. Such nesting of <code>Succ<\/code> is somewhat hard to read, so you can simply write a template to generate Peano types from integers:<\/div>\n<\/div>\n<pre class=\"prettyprint lang-c_cpp\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">template&lt;int v&gt;\r\nstruct peano {\r\n  using type = Succ&lt;typename peano&lt;v - 1&gt;::type&gt;;\r\n};\r\n\r\ntemplate&lt;&gt;\r\nstruct peano&lt;0&gt; {\r\n  using type = Zero;\r\n};<\/pre>\n<p>Then you can simplify it to <code>Add&lt;peano&lt;2&gt;::type, peano&lt;1&gt;::type&gt;::type<\/code> and <code>peano&lt;3&gt;::type<\/code>. The other opeartions like subtraction, multiplication and division can be similarly implemented, which are left as exercises.<\/p>\n<h3>Exercises:<\/h3>\n<div>\n<div>Finish the exercises for arithmetic, comparison and parity at <a href=\"https:\/\/www.codewars.com\/kata\/peano-numbers\/train\/cpp\">Peano numbers | Codewars<\/a>\u00a0 and pass the tests.<\/div>\n<\/div>\n<h3>Afterwords<\/h3>\n<p>We can further &#8220;prove&#8221; the assertions in our intuition, such addition is commutative, multiplication is distributive over addition, and so on. We may talk about this later.<\/p>\n<div><\/div>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>Fundamentals Function on Types We all know that C++ templates can receive types as &#8220;parameters&#8221;. In the same spirit we could can &#8220;return values&#8221;s, so that we can form a function. The basic paradigm is the following: template&lt;class T&gt; struct Computed { using type = T; } In this way we constructed a type level &hellip; <a href=\"https:\/\/colliot.org\/en\/2017\/09\/%e3%80%90c-%e6%a8%a1%e6%9d%bf%e5%85%83%e7%bc%96%e7%a8%8b%e5%85%a5%e9%97%a8%e3%80%91%e5%9c%a8%e7%bc%96%e8%af%91%e6%9c%9f%e5%ae%9e%e7%8e%b0-peano-%e6%95%b0\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Implement Compile Time Peano Numbers &#8211; A Primer in C++ Template Metaprogramming&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":370,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[26,28,27,29],"_links":{"self":[{"href":"https:\/\/colliot.org\/en\/wp-json\/wp\/v2\/posts\/362"}],"collection":[{"href":"https:\/\/colliot.org\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/colliot.org\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/colliot.org\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/colliot.org\/en\/wp-json\/wp\/v2\/comments?post=362"}],"version-history":[{"count":8,"href":"https:\/\/colliot.org\/en\/wp-json\/wp\/v2\/posts\/362\/revisions"}],"predecessor-version":[{"id":426,"href":"https:\/\/colliot.org\/en\/wp-json\/wp\/v2\/posts\/362\/revisions\/426"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/colliot.org\/en\/wp-json\/wp\/v2\/media\/370"}],"wp:attachment":[{"href":"https:\/\/colliot.org\/en\/wp-json\/wp\/v2\/media?parent=362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/colliot.org\/en\/wp-json\/wp\/v2\/categories?post=362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/colliot.org\/en\/wp-json\/wp\/v2\/tags?post=362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}