{"componentChunkName":"component---src-templates-post-js","path":"/huffman-python","result":{"data":{"sitePage":null},"pageContext":{"url":"/huffman-python/","relativePath":"huffman-python.md","relativeDir":"","base":"huffman-python.md","name":"huffman-python","frontmatter":{"title":"Huffman in Python","author":"Mads Buch","date":"2026-06-07","template":"post"},"html":"<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">Orig: Music to hear, why hear’st thou music sadly?</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Comp: 00100010111011111100101101110010100010100101110000111000000101101001001111111100111000101110000111000000100100011111110000101000111010101101101000100111011111100101101110010111111001011010010011100011011101</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Deco: Music to hear, why hear’st thou music sadly?</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Size uncompressed 44</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Size compressed 26</span></span></code></pre>\n<pre class=\"grvsc-container default-light\" data-language=\"python\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># This is part of my anti-atrophy tasks. As most of my professional software development</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># tasks are now is done through agentic development. This is fitness for the brain!</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">#</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Solely based on what&#39;s here: https://en.wikipedia.org/wiki/Huffman_coding</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">import</span><span class=\"mtk1\"> requests</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">import</span><span class=\"mtk1\"> pprint</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">import</span><span class=\"mtk1\"> heapq</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">import</span><span class=\"mtk1\"> math</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Fetch the file, this is our corpus</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">response = requests.get(</span><span class=\"mtk17\">&quot;https://www.gutenberg.org/cache/epub/100/pg100.txt&quot;</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">text = response.text</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Calculate frequencies</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">freqs = {}</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">count = </span><span class=\"mtk7\">0</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">for</span><span class=\"mtk1\"> c </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> text:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\"> </span><span class=\"mtk4\">not</span><span class=\"mtk1\"> c </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> freqs:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        freqs[c] = </span><span class=\"mtk7\">0</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    freqs[c] += </span><span class=\"mtk7\">1</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    count += </span><span class=\"mtk7\">1</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Normalize</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">for</span><span class=\"mtk1\"> k </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> freqs:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    freqs[k] = freqs[k] / count</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># How priority queues aka head queues work</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">p1 = []</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># heapq.heapify(p1) - No need to run for empty lists</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">heapq.heappush(p1, (</span><span class=\"mtk7\">5</span><span class=\"mtk1\">, </span><span class=\"mtk17\">&quot;not so important&quot;</span><span class=\"mtk1\">))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">heapq.heappush(p1, (</span><span class=\"mtk7\">1</span><span class=\"mtk1\">, </span><span class=\"mtk17\">&quot;Really important&quot;</span><span class=\"mtk1\">))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">heapq.heappush(p1, (</span><span class=\"mtk7\">20</span><span class=\"mtk1\">, </span><span class=\"mtk17\">&quot;Forget it&quot;</span><span class=\"mtk1\">))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">heapq.heappush(p1, (</span><span class=\"mtk7\">24</span><span class=\"mtk1\">, (</span><span class=\"mtk17\">&quot;t1&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk17\">&quot;t2&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk17\">&quot;t3&quot;</span><span class=\"mtk1\">)))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">important_task = heapq.heappop(p1)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">assert</span><span class=\"mtk1\"> important_task[</span><span class=\"mtk7\">0</span><span class=\"mtk1\">] == </span><span class=\"mtk7\">1</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># For heapq when using the tuples, it will go onto next element if the first element is identical.</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># The docs are framing the type construction as a payload to attach a payload.</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># This is over loaded semantics (shane on you, Python)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">tie_breaker = </span><span class=\"mtk7\">0</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk10\">next_tie_breaker</span><span class=\"mtk1\">():</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk4\">global</span><span class=\"mtk1\"> tie_breaker</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    tie_breaker += </span><span class=\"mtk7\">1</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> tie_breaker</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">leafs = []</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">for</span><span class=\"mtk1\"> k </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> freqs:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    heapq.heappush(leafs, (freqs[k], next_tie_breaker(), k))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Alternatively</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># leafs1 = [(v, next_tie_breaker(), k) for k, v in freqs.items()]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># heapq.heapify(leafs1)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Representing the tree:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Leaf (frq, _, symbol)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Node: (frq, _, (left, right))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">while</span><span class=\"mtk1\"> </span><span class=\"mtk4\">True</span><span class=\"mtk1\">:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\"> </span><span class=\"mtk10\">len</span><span class=\"mtk1\">(leafs) == </span><span class=\"mtk7\">1</span><span class=\"mtk1\">:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk14\">break</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    a = heapq.heappop(leafs)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    b = heapq.heappop(leafs)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    sumProp = a[</span><span class=\"mtk7\">0</span><span class=\"mtk1\">] + b[</span><span class=\"mtk7\">0</span><span class=\"mtk1\">]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    node = (sumProp, next_tie_breaker(), (a, b))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    heapq.heappush(leafs, node)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">huffman_tree = heapq.heappop(leafs)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">huffman_dict = {}</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">rev_huffman_dict = {}</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">stack = [(</span><span class=\"mtk17\">&quot;&quot;</span><span class=\"mtk1\">, huffman_tree)]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">while</span><span class=\"mtk1\"> </span><span class=\"mtk10\">len</span><span class=\"mtk1\">(stack) &gt; </span><span class=\"mtk7\">0</span><span class=\"mtk1\">:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk10\">next</span><span class=\"mtk1\"> = stack.pop()</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    match(</span><span class=\"mtk10\">next</span><span class=\"mtk1\">):</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        case (prefix, (frq, x, (left, right))):</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">            stack.append((prefix + </span><span class=\"mtk17\">&quot;1&quot;</span><span class=\"mtk1\">, left))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">            stack.append((prefix + </span><span class=\"mtk17\">&quot;0&quot;</span><span class=\"mtk1\">, right))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        case (prefix, (frq, x, symbol)):</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">            huffman_dict[prefix] = symbol</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">            rev_huffman_dict[symbol] = prefix</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        case _:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">            pprint.pprint(</span><span class=\"mtk10\">next</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Let&#39;s compress something!</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">string = </span><span class=\"mtk17\">&quot;Music to hear, why hear’st thou music sadly?&quot;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># compress</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">compressed = </span><span class=\"mtk17\">&quot;&quot;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">for</span><span class=\"mtk1\"> c </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> string:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    compressed += rev_huffman_dict[c]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">decompressed = </span><span class=\"mtk17\">&quot;&quot;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">tree_traverser = huffman_tree</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">for</span><span class=\"mtk1\"> c </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> compressed:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk3\"># Traverse the tree</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\"> c == </span><span class=\"mtk17\">&quot;1&quot;</span><span class=\"mtk1\">:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        tree_traverser = tree_traverser[</span><span class=\"mtk7\">2</span><span class=\"mtk1\">][</span><span class=\"mtk7\">0</span><span class=\"mtk1\">]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\"> c == </span><span class=\"mtk17\">&quot;0&quot;</span><span class=\"mtk1\">:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        tree_traverser = tree_traverser[</span><span class=\"mtk7\">2</span><span class=\"mtk1\">][</span><span class=\"mtk7\">1</span><span class=\"mtk1\">]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk3\"># Are we at a leaf?</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\"> </span><span class=\"mtk10\">isinstance</span><span class=\"mtk1\">(tree_traverser[</span><span class=\"mtk7\">2</span><span class=\"mtk1\">], </span><span class=\"mtk9\">str</span><span class=\"mtk1\">):</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        decompressed += tree_traverser[</span><span class=\"mtk7\">2</span><span class=\"mtk1\">]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        tree_traverser = huffman_tree</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk10\">print</span><span class=\"mtk1\">(</span><span class=\"mtk4\">f</span><span class=\"mtk17\">&quot;Orig: </span><span class=\"mtk4\">{</span><span class=\"mtk1\">string</span><span class=\"mtk4\">}</span><span class=\"mtk17\">&quot;</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk10\">print</span><span class=\"mtk1\">(</span><span class=\"mtk4\">f</span><span class=\"mtk17\">&quot;Comp: </span><span class=\"mtk4\">{</span><span class=\"mtk1\">compressed</span><span class=\"mtk4\">}</span><span class=\"mtk17\">&quot;</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk10\">print</span><span class=\"mtk1\">(</span><span class=\"mtk4\">f</span><span class=\"mtk17\">&quot;Deco: </span><span class=\"mtk4\">{</span><span class=\"mtk1\">decompressed</span><span class=\"mtk4\">}</span><span class=\"mtk17\">&quot;</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk10\">print</span><span class=\"mtk1\">(</span><span class=\"mtk4\">f</span><span class=\"mtk17\">&quot;Size uncompressed </span><span class=\"mtk4\">{</span><span class=\"mtk10\">len</span><span class=\"mtk1\">(string)</span><span class=\"mtk4\">}</span><span class=\"mtk17\">&quot;</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk10\">print</span><span class=\"mtk1\">(</span><span class=\"mtk4\">f</span><span class=\"mtk17\">&quot;Size compressed </span><span class=\"mtk4\">{</span><span class=\"mtk1\">math.ceil(</span><span class=\"mtk10\">len</span><span class=\"mtk1\">(compressed) / </span><span class=\"mtk7\">8</span><span class=\"mtk1\">)</span><span class=\"mtk4\">}</span><span class=\"mtk17\">&quot;</span><span class=\"mtk1\">) </span><span class=\"mtk3\"># We approximate here</span></span></span></code></pre>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .mtk3 { color: #008000; }\n  .default-light .mtk14 { color: #AF00DB; }\n  .default-light .mtk1 { color: #000000; }\n  .default-light .mtk17 { color: #A31515; }\n  .default-light .mtk7 { color: #09885A; }\n  .default-light .mtk4 { color: #0000FF; }\n  .default-light .mtk10 { color: #795E26; }\n  .default-light .mtk9 { color: #267F99; }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>","pages":[{"url":"/100-days-of-fibonacci-day-1-c/","relativePath":"100-days-of-fibonacci-day-1-c.md","relativeDir":"","base":"100-days-of-fibonacci-day-1-c.md","name":"100-days-of-fibonacci-day-1-c","frontmatter":{"title":"Day 1, C","subtitle":"100 Days of Fibonacci","date":"2015-12-16","template":"post"},"html":"<p>My <a href=\"/100-days-of-fibonacci\">100 days of Fibonacci</a> challenge\nstarted yesterday where I implemented the function directly recursive\nand using accumulated recursion in Haskell.\nToday I decided to take one of the most widely known\nlanguages and implement of iterative edition of the Fibonacci function.</p>\n<h2>Day 1 - C</h2>\n<p>In C I implemented Fibonacci iteratively. In the implementation I have used\nthe type <code>long</code> to store intermediate results and return values. It\nguarantees 32 bit of storage, which allows us to compute large enough results.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"c\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">unsigned</span><span class=\"mtk1\"> </span><span class=\"mtk4\">long</span><span class=\"mtk1\"> </span><span class=\"mtk10\">fib_iterative</span><span class=\"mtk1\">(</span><span class=\"mtk4\">long</span><span class=\"mtk1\"> </span><span class=\"mtk12\">n</span><span class=\"mtk1\">){</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk4\">unsigned</span><span class=\"mtk1\"> </span><span class=\"mtk4\">long</span><span class=\"mtk1\"> i=</span><span class=\"mtk7\">0</span><span class=\"mtk1\">, a=</span><span class=\"mtk7\">0</span><span class=\"mtk1\">, b=</span><span class=\"mtk7\">1</span><span class=\"mtk1\">;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">for</span><span class=\"mtk1\">(i=</span><span class=\"mtk7\">1</span><span class=\"mtk1\"> ; i&lt;=n ; i++){</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk4\">unsigned</span><span class=\"mtk1\"> </span><span class=\"mtk4\">long</span><span class=\"mtk1\"> tmp = a+b;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        a = b;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        b=tmp;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    }</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> a;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span></code></pre>\n<p>Overflows needs to be carefully thought about in C. The language\nprovides very little support for unlimited integers compared to\nyesterdays language, Haskell, which indeed had support for big integers.</p>\n<p>Haskell supports large integers by using a library which uses strings\nfor representing integers along with implementations of arithmetic\nfunctions for working on these integers. This could also have been\nimplemented in C. However, this is out of scope for this article.</p>\n<p>The full code for Fibonacci in C is available\n<a href=\"https://github.com/madsbuch/fibonacci/tree/master/c\">on Github</a>.\nBe aware that the code might change when I feel for implementing other\nalgorithms in C.</p>\n<h2>Iterative Fibonacci</h2>\n<p>The iterative Fibonacci has a linear time complexity. This is the same\nas the tail recursive implementation. These two implementations also\nhave much in common.</p>\n<p>The iterative implementation con not be implemented in pure\nfunctional languages because it requires mutable states.\nThis is seen when we change the value of <code>a</code> in the\nabove example by <code>a = b;</code>. Furthermore the <em>while</em> concept is not natural to\nfunctional programming, where operations on collections of elements\nare done recursively.</p>\n<h2>C Programming Language</h2>\n<p>C was meant to be close to the hardware. As a slight layer\non assembly programming. A language used for programming operating\nsystems and hardware driver. And so it is fast as it has a minimal\nruntime system and lets the programmer have complete control over\nthe hardware.</p>\n<p>The control is also often the downside of this language. As it provides\na limited set of abstraction, it is easy to make errors. Errors like\nbuffer overflows are not rare when looking at projects written in C.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .mtk4 { color: #0000FF; }\n  .default-light .mtk1 { color: #000000; }\n  .default-light .mtk10 { color: #795E26; }\n  .default-light .mtk12 { color: #001080; }\n  .default-light .mtk7 { color: #09885A; }\n  .default-light .mtk14 { color: #AF00DB; }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/100-days-of-fibonacci-day-10-python/","relativePath":"100-days-of-fibonacci-day-10-python.md","relativeDir":"","base":"100-days-of-fibonacci-day-10-python.md","name":"100-days-of-fibonacci-day-10-python","frontmatter":{"title":"100 Days of Fibonacci - Day 10, Python","layout":"post","video":false,"comments":false,"image":"/assets/images/cover.jpg","image2":"/assets/images/cover.jpg","date":"2016-02-20","template":"post"},"html":"<!--{ Introduction to the project | light informal fun }-->\n<p>It is yet another time for a post in my\n<a href=\"/100-days-of-fibonacci-overview\">100 days of Fibonacci</a> challenge.\nToday I am containing myself a bit and provide a link between\na concept in programming and its root in mathematics.</p>\n<!--{ Introduction to Python | light informal }-->\n<p>The concept is list comprehension in Python. Python is a widely\nused programming language especially used as an easy language\nappropriate for beginners. The languages is in particular\nclose to English. Furthermore it enforces good style by scoping by indentation.</p>\n<!--{ My take on Fibonacci in python | light informal}-->\n<p>As mentioned I zoom in on the list comprehension feature in\nPython. I relate this feature with the way sets are defined in\nmath. Lastly I talk a bit about the interpretation of infinite sets in\nprogramming.</p>\n<!--{ Implementation details of Fibonacci in Python | technical formal }-->\n<h2>Fibonacci in Python</h2>\n<!--{ Overview on the Python implementation | }-->\n<p>The implementation is the direct recursive implementation accompanied\nby dynamic programming by momoization. This as\nmy <a href=\"/100-days-of-fibonacci-day-2-java/\">Java</a> implementation. It is not\nnew to implement it like this, and simply provides a predicate to use in\nthe list comprehension.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"python\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">cache = {}</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk10\">fib</span><span class=\"mtk1\">(</span><span class=\"mtk12\">n</span><span class=\"mtk1\">):</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\"> cache.get(n, </span><span class=\"mtk4\">None</span><span class=\"mtk1\">) != </span><span class=\"mtk4\">None</span><span class=\"mtk1\"> :</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> cache[n]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\"> n == </span><span class=\"mtk7\">0</span><span class=\"mtk1\"> :</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\"> n == </span><span class=\"mtk7\">1</span><span class=\"mtk1\"> :</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    cache[n] = fib(n-</span><span class=\"mtk7\">1</span><span class=\"mtk1\">) + fib(n-</span><span class=\"mtk7\">2</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> cache[n]</span></span></span></code></pre>\n<!--{ Justification of the cache | }-->\n<p>The cache is here used as a global variable. Global variables are something one\nshould by all means avoid, but here the amount of code is small and the main\ngoal of this post is not software architecture.</p>\n<!--{ Transition to the list comprehension formulation |  }-->\n<p>From here we can know create the list of the 20 first Fibonacci numbers using\nlist comprehension in Python. We print it directly to provide an output.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"python\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk10\">print</span><span class=\"mtk1\"> [fib(x) </span><span class=\"mtk14\">for</span><span class=\"mtk1\"> x </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> </span><span class=\"mtk10\">range</span><span class=\"mtk1\">(</span><span class=\"mtk7\">20</span><span class=\"mtk1\">)]</span></span></span></code></pre>\n<p>This is a one-liner for mapping the list of numbers from 0 to 19 to the list\ntheir corresponding Fibonacci numbers.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">$ python fib.py </span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ..., 4181]</span></span></code></pre>\n<p>The implementation is available from my supporting\n<a href=\"https://github.com/madsbuch/fibonacci/tree/master/python\">Git repository</a>.</p>\n<h2>List Comprehension</h2>\n<!--{ Set initial intuition in a for loop | }-->\n<p>The notation used above looks mostly like a very compact <code>for</code>-loop. For this\nrelation to work we need to slightly open up our understanding of lists and\niteration in programming languages.</p>\n<!--{ The new understanding of lists | }-->\n<p>First we need to view a list as a set. It should be quite clear that a list\nindeed can be a set. For that to work we need operations like <em>intersection</em>,\n<em>union</em>, <em>difference</em>, etc. Any programmer should be able to quickly implement\nthose. The contrary, that lists are sets, are not given. Lists are inherently\nordered, where sets are unordered.</p>\n<!--{ New understanding of iteration | }-->\n<p>The other thing is the <code>for</code>-loop. <code>for</code>-loops are usually viewed as iterating\nall elements in a list one-by-one, and doing something to them. Instead we\ncan see it as quantification. For a second we should forget everything about\nthe order of the list and details about iterations, and just see it as we\nperform an operation on every single element in the <em>set</em>.</p>\n<p>We now have the mindset set for the following mathematical interpretation of\nthe above list comprehension to work. The following is a direct translation.</p>\n$$\n    \\{ \\ fib(x) \\ | \\ x \\in \\{0, 1, 2, 3, 4, 5, ..., 19\\} \\ \\}\n$$\n<p>which would usually be written as follows.</p>\n$$\n    \\{ \\ fib(x) \\ | \\ x \\in \\mathbb{N} \\ \\}\n$$\n<p>The reason why the last formulation is kind of futile is that Python\nuses strict evaluation: All terms are evaluated. The set of naturals is\ninfinite and hence the computation will not halt.</p>\n<p>The last formulation is, however, possible to formulate in programming.\nWe just need a lazy evaluated language. As such we can formulate it, and use\nit, in the Haskell programming language. Following works given an\nimplementation of the <code>fib</code> function:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"python\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">[fib n | n &lt;- [</span><span class=\"mtk7\">0</span><span class=\"mtk1\">..]]</span></span></span></code></pre>\n<p>And what is all this worth? Well, a lot of material programmers have to\nimplement was first formulated in a mathematical language. This relations seeks\nthe demystify the relation between programming and math. In fact it can (and\nis / will be in other posts) be argued that math and programming is in fact\nthe same thing. But for some reason the two areas are somewhat disjoint. This,\nin my opinion, largely because of two cultures in communicating.</p>\n<h2>Conclusion</h2>\n<!--{ Fibonacci was implemented | }-->\n<p>In this post i implemented Fibonacci in python. There is nothing new to\nthe implementation. It was a directly recursive implementation supported\nby a cache for speed.</p>\n<!--{ Conclusion of list comprehension | }-->\n<p>The concept elaborated in this post is list comprehension and its relationship\nthe the mathematical formulation of sets. Ultimately my message is that we as\nprogrammers should be able to both formulate and interpret in other frameworks.\nAlso those seemingly very distant frameworks, like mathematics is for\nmany programmers.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .mtk1 { color: #000000; }\n  .default-light .mtk4 { color: #0000FF; }\n  .default-light .mtk10 { color: #795E26; }\n  .default-light .mtk12 { color: #001080; }\n  .default-light .mtk14 { color: #AF00DB; }\n  .default-light .mtk7 { color: #09885A; }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/100-days-of-fibonacci-day-0-haskell/","relativePath":"100-days-of-fibonacci-day-0-haskell.md","relativeDir":"","base":"100-days-of-fibonacci-day-0-haskell.md","name":"100-days-of-fibonacci-day-0-haskell","frontmatter":{"title":"Day 0, Haskell","subtitle":"100 Days of Fibonacci","date":"2015-12-15","template":"post"},"html":"<p>Today I taught my computer architecture class for the last time.\nMy students and I discussed the complexity of the various Fibonacci\nalgorithms. Fibonacci is a function often used to test out new\nprogramming languages as it is easy to implement and can showcase\nmany techniques.</p>\n<p>Later on I worked with my friend Anders Schmidt who showed me\nPaul Flavius Nechita's 100 Days UI Challenge.\nI thought it was cool and wanted to do something similar. But\nI do not design.</p>\n<p>So I decided to <a href=\"/100-days-of-fibonacci\">program Fibonacci in 100 different programming languages</a>.\nThe idea is to showcase some typical aspects of a programming\nlanguage and implement the function in the different languages idiomatic ways.</p>\n<p>As today is the first day of this project I am going to present Fibonacci\nin Haskell. This is mostly because I recently have spend quite some time\nwith that language. The formulations of the function is going to be\nby direct recursion and recursion with accumulation.</p>\n<h2>Day 0 - Haskell</h2>\n<p>Where else would you start? Here we look at Haskell as a practical\ngeneral purpose language.</p>\n<p>In Haskell I did two different implementations. First, the\nrecursive way, which is the way we usually think of problems in\na functional context. We use pattern matching to pick out the\nbase and induction cases.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">-- Standard recursive implementation, very slow</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">fib :: Integer -&gt; Integer</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">fib 0 = 0</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">fib 1 = 1</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">fib n = fib (n - 1) + fib (n - 2)</span></span></code></pre>\n<p>However, it turns out that this solution is quite slow due to the\nexponential fanout with the two recursive calls.\nLuckily there is another way! So I implemented it using recursion\nwith an accumulator:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"hs\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">-- Using an accumulator</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">fibAcc :: Integer -&gt; Integer</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">fibAcc n = doFib n (0,1)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    where</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        doFib :: Integer -&gt; (Integer, Integer) -&gt; Integer</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        doFib 0 (a, b) = a</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        doFib n (a, b) = doFib (n-1) (a+b, a)</span></span></code></pre>\n<p>To keep the type signature identical to the other Fibonacci\nwe uses the <code>where</code> construction to wrap the actual function.</p>\n<p>Lastly we need to make a command line program that takes the number (i.e. <em>n</em>)\nthat we want to calculate $fib(n)$ for. As Haskell is pure and lazy, we\ncan't simply in line system calls like we do in c. In Haskell a monadic\nstyle is chosen.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"hs\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">-- First argument is read and parsed as Integer</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">main = do</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    a &lt;- getArgs</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    putStrLn $ show (fibAcc $ read (a !! 0) )</span></span></code></pre>\n<p>In the above example we first read the list of arguments into\n<code>a</code>, thereafter we parse the first (0th) element and calculate\nthe corresponding Fibonacci number. The result is converted\nto a string and written back to the command line.</p>\n<p>Haskell provides two ways to evaluate a program. It has its REPL\n(read-eval-print loop) where the file can be loaded and the\nprogram can be compiled.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"txt\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">$ ghci Fib.hs</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">...</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Ok, modules loaded: Main.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">*Main&gt; fib 10</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">55</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">*Main&gt; fibAcc 10</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">55</span></span></code></pre>\n<p>Alternatively Haskell can be compiled and executed:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"txt\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">$ ghc Fib.hs -o fib</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">$ ./fib 10</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">55</span></span></code></pre>\n<p>The file is available for download\n<a href=\"https://github.com/madsbuch/fibonacci/tree/master/haskell\">here</a>.</p>\n<h2>Functional Fibonacci</h2>\n<p>In this first implementation we used two techniques for implementing\nthe Fibonacci function:</p>\n<ul>\n<li><strong>Recursion:</strong> This is the typical way of thinking of problems in a\nfunctional context. In this case it has a exponential\ntime complexity. This is bad, as we know that the Fibonacci\nfunction can be implemented faster.</li>\n<li><strong>Recursion with accumulator:</strong> The idea is that we immidiatly\naccumulate partial results into an accumulator which is passed\non to the next call.\nFor Fibonacci we only care about the two\nprevious results. This approach lets us forget all intermediate\nresults and only pass forward the\ndata we need. This lets us achieve a linear time complexity.\nFurthermore many compilers provide tail call optimization. In\nthat way we have constant space consumption and do not suffer from\nstack overflows.</li>\n</ul>\n<h2>Haskell</h2>\n<p>Here we take offset in Haskell as a general purpose language.\nAmong others it is known for its extremely flexible type\nsystem which enables us to reason quite fine grained about our\nprograms.</p>\n<p>However, the concerns we have with Haskell here is how well\nit integrates with other lagnauges and the complexity of the output\ncode. As seen it is relatively easy to implement a linear\nalgorithm of Fibonacci.</p>\n<p>Functional programming has in general\nshown good progress in outputting efficient code, and for many\napplications the generally superior readability is better\nthan squeezing that last performance out of the CPU.</p>\n<p>Haskell integrates well with the rest of the system. This\nexample shows the use of command line arguments, which are fairly\nstraight forward. Furthermore Haskell supports\n<a href=\"https://en.wikipedia.org/wiki/Foreign_function_interface\">FFI</a> for\nboth importing and exporting functions.</p>\n<h2>Conclusion</h2>\n<p>I have elaborated on the Fibonacci implementation and presented it\nby two formulations: Direct recursion and recursion with an accumulator.\nThe language to carry the implementation has been Haskell. Haskell\nwas presented and swiftly evaluated.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/100-days-of-fibonacci-day-11-fixed-point/","relativePath":"100-days-of-fibonacci-day-11-fixed-point.md","relativeDir":"","base":"100-days-of-fibonacci-day-11-fixed-point.md","name":"100-days-of-fibonacci-day-11-fixed-point","frontmatter":{"title":"100 Days of Fibonacci - Day 11, Fixed Point","layout":"post","video":false,"comments":false,"date":"2016-10-11","template":"post"},"html":"<p>In this article, we will look into encoding the Fibonacci function using the\n<code>fix</code>-point combinator. This is an interesting function as it can be used\nto implement general recursion in a programming language.</p>\n<p>To set a context we first need to discuss some programming language\nfundamentals: In daily speech, programmers are used to programming\nlanguages being express <em>all possible</em> computations. This is, they are\n<a href=\"https://en.wikipedia.org/wiki/Turing_completeness\">Turing complete</a>.</p>\n<p>However, often we want a programming language to support various properties.\nSome often discussed properties are\n<a href=\"https://en.wikipedia.org/wiki/Type_safety\">type safety</a>, which entails that\nprograms that compile also runs without errors, and\n<a href=\"https://en.wikipedia.org/wiki/Normalization_property\">normalization</a>, which\nroughly says we always get a result.</p>\n<p>An often used framework for reasoning about these properties is the\nsimply typed lambda calculus (STLC) with friends. With the raw STLC,\nit is not possible to encode general recursion. Hence we need to add\na friend, which makes it possible.</p>\n<p>This friend could be the fixed point operator.</p>\n<h2>Day 11 - Fix</h2>\n<p>Generally, the <code>fix</code> operator looks as follow:</p>\n$$\n    y \\ f = f \\ y \\ f\n$$\n<p>In this article we let Haskell be our syntactical point of view. This is\nbecause we then will have an interpreter for free.</p>\n<p>In the <code>ghci</code> interpreter we may define the function as follows:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">&gt; let fix f = let x = f x in x</span></span></code></pre>\n<p>We now have a function <code>fix</code> that takes a function as the argument,\napplies it to\nitself and returns the result. If the function we apply <code>fix</code> on returns a\nvalue, then fix will return a value:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">&gt; fix (\\rec -&gt; &quot;Hello Fix!&quot;)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">&quot;Hello fix!&quot;</span></span></code></pre>\n<p>Next, if we just apply the function on itself we will get an infinite loop:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">&gt; fix (\\rec -&gt; rec)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">^CInterrupted.</span></span></code></pre>\n<p>Now, this makes sense, as we continuously attempt to apply the lambda function\non itself.</p>\n<p>In above example, we only have a single argument, <code>n</code>, which is the function\nitself. We can say that we have the recursion directly in our hands. But to\nbe able to do something interesting, like calculating the <em>i</em>'th Fibonacci\nnumber, we need to add an argument we can decrement on. First a simple\nexample.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">&gt; fix (\\rec n -&gt; if n == 0 then 0 else n + rec (n-1)) 10</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">55</span></span></code></pre>\n<p>In above example we use the <code>if</code> to return a value when we hit 0, otherwise\nwe apply the function on <em>n-1</em>. The expression simply adds all numbers from\n0 to 10.</p>\n<p>From here it is straight forward to implement the Fibonacci function. We need\nto have a nested if-statement, as we have two base cases. Otherwise, it is very\nsimilar to the function above.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">&gt; let fib = fix (\\rec n -&gt; </span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    if n == 0 then 0 else if n == 1 then 1 else rec (n-1) + rec (n-2))</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">&gt; fib 10</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">55</span></span></code></pre>\n<h2>Reflections</h2>\n<p>This is a directly recursive implementation of the Fibonacci function. This is\nindeed visible from time complexity, and it is not feasible to compute the\nFibonacci number of large numbers. It is indeed possible to augment above\nimplementation with an accumulator.</p>\n<p>Above implementation is interesting as we completely separate the recursion\nfrom the actual computation. In\n<a href=\"/100-days-of-fibonacci-day-0-haskell/\">an earlier article</a>,\nI implemented the function in Haskell using normal function syntax. Here the\ndistinction between the recursion mechanism and the calculating mechanism is\nnot as distinct.</p>\n<p>The separation is especially interesting when formally reasoning about\nprogramming languages as we can get clutter away. This, however, is much\neasier when doing this in dedicated languages.</p>\n<p>Above examples are kind of futile in Haskell, as it already supports better\nfunctionality for solving the Fibonacci function. It is, however, interesting\nto think about what a <em>minimal</em> language for solving the function would be.\nIn above implementation, we need support for <code>fix</code>, <code>if-then-else</code>, boolean\nconstructions, and natural numbers. This is four language constructions.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/100-days-of-fibonacci-day-12-julia/","relativePath":"100-days-of-fibonacci-day-12-julia.md","relativeDir":"","base":"100-days-of-fibonacci-day-12-julia.md","name":"100-days-of-fibonacci-day-12-julia","frontmatter":{"title":"100 Days of Fibonacci - Day 12, The Fibonacci Matrix","layout":"post","date":"2023-05-22","template":"post"},"html":"<p>Perspective. As in the real world, by looking at a problem from a different\nangle, we can often find solutions much faster.</p>\n<p>Day 12 is the matrix implementation of the fibonacci sequence.\nA specially constructed matrix has the property of calculating\nthe next element in the sequence upon multiplication with the previous element.</p>\n<p>Implementing the Fibonacci sequence like this has a rather special side effect:\nIt allows reducing the problem of calculating the Fibonacci sequence to\nmultiplication for which we can utilize knowledge on multiplication and make\nand calculate an element much faster than the iterative approaches.</p>\n<p>The matrix looks like this:</p>\n$$\n    Q_{fib} = \\begin{bmatrix}\n            1 & 1 \\\\\n            1 & 0\n        \\end{bmatrix}\n$$\n<p>The n<em>th</em> element of the Fibonacci sequence can then be found by reading either\nthe top-right or bottom-left element of the matric as defined by \\( Q_{fib}^n \\).</p>\n<p>Let's see an example in Julia</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">❯❯❯ julia</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">               _</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">   _       _ _(_)_     |  Documentation: https://docs.julialang.org</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  (_)     | (_) (_)    |</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">   _ _   _| |_  __ _   |  Type &quot;?&quot; for help, &quot;]?&quot; for Pkg help.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  | | | | | | |/ _` |  |</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  | | |_| | | | (_| |  |  Version 1.8.1 (2022-09-06)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> _/ |\\__&#39;_|_|_|\\__&#39;_|  |  Official https://julialang.org/ release</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">|__/                   |</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">julia&gt; Q = [1 1;1 0]</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">2×2 Matrix{Int64}:</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> 1  1</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> 1  0</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">julia&gt; Q^10</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">2×2 Matrix{Int64}:</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> 89  55</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> 55  34</span></span></code></pre>\n<p>From here it is evident that the 10th element of the fibonacci sequence is 55.</p>\n<h2>Speeding up Calculations</h2>\n<p>As calculating the <em>n</em>'th element in the Fibonacci sequence is now the same as\ntaking the exponent, we can utilize exponentiation by squaring. This method\nrelies on the insight that \\( b^n = b^{{2}^{n/2}} \\) when <em>n</em> is even - a rule\nthat can be derived from the power of a power rule:</p>\n$$\nb^{{x}^{y}} = b^{x * y}\n$$\n<p>As an example we can calculate \\( 2^{10} \\) using the method:</p>\n$$\n\\begin{align*}\n    2^{10}  &= 4^{5}  \\quad \\text{(power of power rule)} \\\\\n            &= 4 \\times (4^{4}) \\\\\n            &= 4 \\times (16^{2}) \\quad \\text{(power of power rule)} \\\\\n            &= 4 \\times 256 \\\\\n            &= 1024\n\\end{align*}\n$$\n<p>This algorithm can be implemented in Julia as the following:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"jl\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">function fast_exp(base, exp)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  result = 1</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  while exp &gt; 0</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    if exp % 2 == 0</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">      base *= base</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">      exp = exp ÷ 2</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    else</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">      result *= base</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">      exp -= 1</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    end</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  end</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  return result</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">end</span></span></code></pre>\n<p>And to see how well it works we can make a quick\ncheck on calculating the 100.000 element of the Fibonacci\nsequence.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">...</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">julia&gt; include(&quot;fast-exp.jl&quot;)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">fast_exp (generic function with 1 method)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">julia&gt; Q = [big(1) big(1);big(1) big(0)]</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">2×2 Matrix{BigInt}:</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> 1  1</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> 1  0</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">julia&gt; @time fast_exp(Q, 100000);</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  0.000767 seconds (703 allocations: 285.633 KiB)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">julia&gt; @time naive_exp(Q, 100000);</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  0.723526 seconds (3.10 M allocations: 4.097 GiB, 9.84% gc time)</span></span></code></pre>\n<p>The naive implementation is not the build in, rather the iterative\nimplementation following the naive approach. The code can be found\n<a href=\"https://github.com/madsbuch/fibonacci/blob/master/julia/fibonacci.jl\">here</a>.</p>\n<h2>Reduction and Abstraction</h2>\n<p>Key techniques when developing software is reduction and abstraction. The\nabove is an example of a reduction to an algebraic field that allows us to\nuse multiplication.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/100-days-of-fibonacci-day-3-scala/","relativePath":"100-days-of-fibonacci-day-3-scala.md","relativeDir":"","base":"100-days-of-fibonacci-day-3-scala.md","name":"100-days-of-fibonacci-day-3-scala","frontmatter":{"title":"Day 3, Scala","subtitle":"100 Days of Fibonacci","date":"2015-12-18","template":"post"},"html":"<!--{ Introduction to Scala from a JAVA point of view | }-->\n<p>Coming from yesterdays Java, Scala is a naturally the next step. It is\na functional language compiled to run on the Java Virtual Machine.\nIt integrates easily with Java programs, and as such, it is often used\nin conjunction with Java. Javas API is also available in Scala. This\nmakes it easier for Java developers to start with Scala if they\nwant to dive into functional programming.</p>\n<p>This is the 4th day in my little\n<a href=\"/100-days-of-fibonacci\">challenge</a> I am carrying\nout as a personal project. The first days elaborated on simple concepts,\nbut later on more complicated concepts such as type level programming, parallel\nprogramming and even more theoretical subjects are explored.</p>\n<h2>Day 3 - Scala</h2>\n<p>Today I present an implementation of Fibonacci in the Continuation Passing\nStyle (CPS). This can easily be done in Scala because of its functional\nnature:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"scala\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">// Fibonacci in Contiuation Passing Style</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">def fib_cps (n: Int, k: (Int =&gt; Int)) : Int = n match{</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    case 0 =&gt; k(0)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    case 1 =&gt; k(1)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    case _ =&gt; fib_cps(n-1, (a: Int) =&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        fib_cps(n-2, (b: Int) =&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">            k(a+b)))</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">}</span></span></code></pre>\n<p>In this example I have used pattern matching to keep\nit easily readable.\nThe variable <em>k</em> holds the continuation which is evaluated\nin the base cases (<code>case 0</code> and <code>case 1</code>).</p>\n<p>The above code was wrapped to make it compatible to the\nusually type signature of Fibonacci.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"scala\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">// An alias to make it satisfy the type signature Int -&gt; Int</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">def fib_cont(n: Int) = fib_cps(n, ((x: Int) =&gt; x)) </span></span></code></pre>\n<p>The main function holds a call to above. The whole thing is compiled\nand run on the command line completely as one would expect coming\nfrom Java.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">$ scalac Fib.scala </span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">$ scala Fib</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">55</span></span></code></pre>\n<p>A little quiz: Guess <em>n</em> such that $fib(n) = 55$ (I have elaborated on this\n<a href=\"/100-days-of-fibonacci-day-4-prolog/\">tomorrow</a>).</p>\n<p>The code can as usually be found on GIT\n<a href=\"https://github.com/madsbuch/fibonacci/tree/master/scala\">here</a></p>\n<h2>Continuation Passing Style</h2>\n<p>Continuation Passing Style is not easy to read, and as such it is not a\ngood way\nto implement functions, unless there is a very good reason. These reasons\ncan be applications where explicit management of the control flow is\nnecessary.</p>\n<p>Many compilers do an implicit conversion from recursive functions to\nfunctions in CPS. A reason is that all computations are wrapped in a\npackage (i.e. the <em>continuation</em>) and is put in the heap which reduces\nmaximum stack size.</p>\n<h2>Conclusion</h2>\n<p>Today I looked at 2 new things, Scala and Continuation Passing style.</p>\n<p>Scala is a great language for writing functional code that has to\nintegrate with Java projects, or for writing functional code using\nJava libraries.</p>\n<p>Continuation passing style is a way of programming which can be enormously\nuseful. Especially when writing large project where the control flow has\nto be explicitly managed.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/100-days-of-fibonacci-day-2-java/","relativePath":"100-days-of-fibonacci-day-2-java.md","relativeDir":"","base":"100-days-of-fibonacci-day-2-java.md","name":"100-days-of-fibonacci-day-2-java","frontmatter":{"title":"Day 2, Java","subtitle":"100 Days of Fibonacci","date":"2015-12-17","template":"post"},"html":"<p>In my <a href=\"/100-days-of-fibonacci\">100 days of Fibonacci</a>\nproject I have now shown three idioms for implementing algorithms:\nDirect recursion, accumulated recursion, and an iterative\napproach. Today I diving into a technique for optimizing\nfunction with many repetitive calls.</p>\n<p>Memoization can be added to an already implemented function. When\nused correctly is provides a radical speedup for a little extra\nmemory consumption. This I have showcased in Java on the directly\nrecursive Fibonacci implementation.</p>\n<h2>Day 2 - Java</h2>\n<p>Here I have implemented Fibonacci in Java using dynamic programming. The\nidea is to check whether the result has already been calculated. If\nit has so, then we return the already calculated the result, otherwise\nwe calculate the result, save it, and return.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"java\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">/**</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"> * Memoization implementation of the Fibonacci function using hash table</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"> * as container for memory entries</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"> */</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">private</span><span class=\"mtk1\"> </span><span class=\"mtk4\">static</span><span class=\"mtk1\"> </span><span class=\"mtk9\">BigInteger</span><span class=\"mtk1\"> </span><span class=\"mtk10\">fibDynamic</span><span class=\"mtk1\">(</span><span class=\"mtk9\">int</span><span class=\"mtk1\"> n, </span><span class=\"mtk9\">Hashtable</span><span class=\"mtk1\">&lt;Integer, BigInteger&gt; mem){</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk3\">// Check if we have the result</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\">(</span><span class=\"mtk12\">mem</span><span class=\"mtk1\">.</span><span class=\"mtk10\">containsKey</span><span class=\"mtk1\">(</span><span class=\"mtk14\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Integer</span><span class=\"mtk1\">(n)))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> </span><span class=\"mtk12\">mem</span><span class=\"mtk1\">.</span><span class=\"mtk10\">get</span><span class=\"mtk1\">(</span><span class=\"mtk14\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Integer</span><span class=\"mtk1\">(n));</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk3\">// Base cases</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\">(n == </span><span class=\"mtk7\">0</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> </span><span class=\"mtk14\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">BigInteger</span><span class=\"mtk1\">(</span><span class=\"mtk17\">&quot;0&quot;</span><span class=\"mtk1\">);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\">(n == </span><span class=\"mtk7\">1</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> </span><span class=\"mtk14\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">BigInteger</span><span class=\"mtk1\">(</span><span class=\"mtk17\">&quot;1&quot;</span><span class=\"mtk1\">);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk3\">// calculate and memoize Fibonacci</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk12\">mem</span><span class=\"mtk1\">.</span><span class=\"mtk10\">put</span><span class=\"mtk1\">(</span><span class=\"mtk14\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Integer</span><span class=\"mtk1\">(n), </span><span class=\"mtk10\">fibDynamic</span><span class=\"mtk1\">(n-</span><span class=\"mtk7\">1</span><span class=\"mtk1\">, mem).</span><span class=\"mtk10\">add</span><span class=\"mtk1\">(</span><span class=\"mtk10\">fibDynamic</span><span class=\"mtk1\">(n-</span><span class=\"mtk7\">2</span><span class=\"mtk1\">, mem)));</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> </span><span class=\"mtk12\">mem</span><span class=\"mtk1\">.</span><span class=\"mtk10\">get</span><span class=\"mtk1\">(n);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span></code></pre>\n<p>To keep the type signature to that of the other implementations, we build an\noverloaded wrapper method, which initializes a hash table, runs the\nrecursive function and returns its result.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"java\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">/**</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"> * Driver for the dynamic implementation of Fibonacci</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"> */</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">private</span><span class=\"mtk1\"> </span><span class=\"mtk4\">static</span><span class=\"mtk1\"> </span><span class=\"mtk9\">BigInteger</span><span class=\"mtk1\"> </span><span class=\"mtk10\">fibDynamic</span><span class=\"mtk1\">(</span><span class=\"mtk9\">int</span><span class=\"mtk1\"> n){</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk9\">Hashtable</span><span class=\"mtk1\">&lt;</span><span class=\"mtk9\">Integer</span><span class=\"mtk1\">, </span><span class=\"mtk9\">BigInteger</span><span class=\"mtk1\">&gt; </span><span class=\"mtk12\">mem</span><span class=\"mtk1\"> = </span><span class=\"mtk14\">new</span><span class=\"mtk1\"> </span><span class=\"mtk9\">Hashtable</span><span class=\"mtk1\">&lt;</span><span class=\"mtk9\">Integer</span><span class=\"mtk1\">, </span><span class=\"mtk9\">BigInteger</span><span class=\"mtk1\">&gt;(n+</span><span class=\"mtk7\">1</span><span class=\"mtk1\">);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> </span><span class=\"mtk10\">fibDynamic</span><span class=\"mtk1\">(n, mem);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span></code></pre>\n<p>The code is as usual available and can be found\non <a href=\"https://github.com/madsbuch/fibonacci/tree/master/java\">Github</a>.</p>\n<h2>Fibonacci and Dynamic Programming</h2>\n<p>Today I introduced dynamic programming. We have two ways to apply dynamic\nprogramming, either by memoization or tabulation. Memoization is the process\nof remembering intermediate results. This is exactly what I did in the\nearlier example. Here we might cache some results we never use.\nTabulation is the process of pre-calculating a table of intermediate results\nand then afterwards aggregate the result afterwards.</p>\n<p>Dynamic programming can also be seen as a method of reducing the number\nof unfolds. In this example the Fibonacci implementation is called linear\nnumber of times to <em>n</em>. In the direct recursive method, it would\nhave been exponentially in <em>n</em>.</p>\n<h2>Java, Java, Java</h2>\n<p>Java is one of the top languages used today. It is compiled to an\nintermediate representation, Java byte code, before it is ran on a Java\nvirtual machine. This makes it very portable without sharing the Java\nsource code. Furthermore there is a big body of libraries and articles\nwritten solving various problems in the language.</p>\n<p>When having an enterprise it is certainly also a big plus that\nmost programmers program in Java. It is easy to find people who\nare able to read and write Java code.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .mtk3 { color: #008000; }\n  .default-light .mtk4 { color: #0000FF; }\n  .default-light .mtk1 { color: #000000; }\n  .default-light .mtk9 { color: #267F99; }\n  .default-light .mtk10 { color: #795E26; }\n  .default-light .mtk14 { color: #AF00DB; }\n  .default-light .mtk12 { color: #001080; }\n  .default-light .mtk7 { color: #09885A; }\n  .default-light .mtk17 { color: #A31515; }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/100-days-of-fibonacci-day-4-prolog/","relativePath":"100-days-of-fibonacci-day-4-prolog.md","relativeDir":"","base":"100-days-of-fibonacci-day-4-prolog.md","name":"100-days-of-fibonacci-day-4-prolog","frontmatter":{"title":"Day 4, Prolog","subtitle":"100 Days of Fibonacci","date":"2015-12-19","template":"post"},"html":"<p><strong>Updates:</strong></p>\n<ul>\n<li>26 January 2016: Complete rewrite of the section about the Structural\nImplementation of Fibonacci.</li>\n</ul>\n<p>In my <a href=\"/100-days-of-fibonacci\">100 days of Fibonacci</a>\nproject I looked at functional and imperative language. There are however\nmany more programming paradigms today we are going to dive into\none of the most used\n<a href=\"https://en.wikipedia.org/wiki/Logic_programming\">logic programming languages</a>.</p>\n<p>Prolog is heavily used in the field of artificial intelligence. This is due\nto the natural way of formulating computations in terms of predicates.\nThis allows for concise expression of tree structures that certain data might\nexhibit.</p>\n<p>In this blog post, i show how to implement Fibonacci in the directly\nrecursive style. As we will see this gives problems when we want to\nexploit some Prolog features.</p>\n<h2>Day 4 - Prolog</h2>\n<p>In Prolog, I defined a predicate that holds both the argument and the\nresult. A predicate is something that can only be true or false.\nTherefore, the notion of return value is explicit parameters to the\npredicates in Prolog.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"pl\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">% Implementation using direct recursion.</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">fib_direct(0, 0).</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">fib_direct(1, 1).</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">fib_direct(N, R) :-</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    X1 is N-1,</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    X2 is N-2,</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    fib_direct(X1, A),</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    fib_direct(X2, B),</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    R is A + B.</span></span></span></code></pre>\n<p>In above the base cases are defined as simple predicates. The first argument\nis the nth Fibonacci number and the second is the result.</p>\n<p>By writing <code>fib_direct(0, 0).</code> we denote that the predicate predicate\n<code>fib_direct</code> is true when applied 0 for both arguments. In <code>swipl</code>\n(SWI Prolog interpreter) we can write <code>fib_direct(0, X).</code> to make\nit derive the second argument given the first argument.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"prolog\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">$ swipl fib.pl </span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">% fib.pl compiled 0.00 sec, 4 clauses</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Welcome to SWI-Prolog ...</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">For help, use ?- help(Topic). or ?- apropos(Word).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">?- fib_direct(0, R).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">R = 0 .</span></span></code></pre>\n<p>The third definition of the <code>fib_direct</code> predicate is the one that\nallows us to calculate Fibonacci for arbitrary numbers. We can again\nmake prolog derive the result by supplying N</p>\n<pre class=\"grvsc-container default-light\" data-language=\"text\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">...</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">?- fib_direct(10, R).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">R = 55 .</span></span></code></pre>\n<p>A problem here is that we use the <code>is/2</code> notation. It enforces evaluation\nof the right side and puts the result in the variable on the left side.\nThis is something we want to avoid in logical programming, as we need the\nright side to be defined. The reason is that we can not choose what to derive anymore.</p>\n<p>Yesterday I put up a little challenge to try to figure out where 55 came from.\nThis challenge is ideal to solve in Prolog as we can use the\nsame code to derive what <em>n</em> is for a given Fibonacci number. We would have\ndone something like.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"text\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">?- fib_direct(N, 55).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">ERROR: fib_direct/2: Arguments are not sufficiently instantiated</span></span></code></pre>\n<p>But as you can see it fails. This is because of the use of <code>is/2</code>.\nThe <code>is</code>-operator does explicit evaluation of the right-hand side\nbefore it check whether it equals the left-hand side.</p>\n<h2>A Structural Implementation of Fibonacci</h2>\n<p>To remove the use of that operator I have implemented Peano natural\nnumbers and defined what addition is. This allows us to implement\nFibonacci in a purely structural manner.</p>\n<p>Fibonacci is a very simple function that requires 3 constructions:\na data type (natural numbers), data aggregation (addition), and\na way to define the recursive dependencies. That last thing is done\nthrough recursively defined predicates in Prolog.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"prolog\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">% Definiition of the Peano numbers</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">nat(0).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">nat(s(_)).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">% Readable nats</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">nat_to_int(0, 0).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">nat_to_int(s(N), R) :-</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    nat_to_int(N, R1),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    R is R1 + 1.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">int_to_nat(0, 0).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">int_to_nat(X, s(R)) :-</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    X1 is X-1,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    int_to_nat(X1, R).</span></span></code></pre>\n<p>Above is the data type. The first three lines are somewhat unnecessary\nas Prologs typing discipline is implicit.</p>\n<p>The next two block display how we convert between the arithmetic integers\nand the naturals. We use pattern matching an a recursively defined predicate.\nThis construction is quite idiomatic for this problem.</p>\n<p>Net we have an implementation of addition. Again we use pattern\nmatching in two defined predicates. The first predicate is the\nbase case while the second is the induction case.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">% Definition of addition ( add(A, B, Result ))</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">add(0, B, B).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">add(s(A), B, R) :- add(A, s(B), R).</span></span></code></pre>\n<p>To understand what is happening one can think of two stacks of plates\nwhere we move all plates piecewise from the left stack to the right.\nWhen the left stack is empty, we stop and let the right one be the\nresult.</p>\n<p>The last part in this implementation is the definition of Fibonacci.\nNote that we implicitly subtract the number by 2 when we match in\nthe induction case. This is necessary as we don't have any functionality\nfor subtracting. General subtraction is not necessary as we only need to\nsubtract by 2. In the body of the predicate we add by 1, to get the subtraction\nof 1.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"prolog\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">fib_peano(0, 0).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">fib_peano(s(0), s(0)).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">fib_peano(s(s(N)), R) :-</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    fib_peano(N, A),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    fib_peano(s(N), B),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    add(A, B, R).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">% Wrapper functions dealing with all the conversions</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">fib_peano_wrapper_get_res(A, B) :-</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    int_to_nat(A, X),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    fib_peano(X, Y),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    nat_to_int(Y, B).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">fib_peano_wrapper_get_arg(A, B) :-</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    int_to_nat(B, Y),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    fib_peano(X, Y),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    nat_to_int(X, A).</span></span></code></pre>\n<p>The two last blocks are for reading arithmetic arguments. They simply\nconvert a number to the Peano structure before it run the <code>fib_peano</code>\npredicate. Lastly it converts back to arithmetic integers to make it\neasier to read the output.</p>\n<p>First we take a look at how it would look without conversion from integers.\nIt is apparent that it is rather cumbersome to practically work with numbers\nin this format.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"text\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">?- fib_peano(s(s(s(s(s(s(s(s(s(s(0)))))))))), R), nat_to_int(R, X).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">R = s(s(s(s(s(s(s(s(s(s(...)))))))))),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">X = 55 .</span></span></code></pre>\n<p>The last piece of code for this post demonstrates how the wrappers work.\nthe first example simply returns the 10th Fibonacci number.\nThe second example is more interesting. It lets us derive which\nindex the Fibonacci number 55 has.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"text\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">?- fib_peano_wrapper_get_res(10, B).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">B = 55 .</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">?- fib_peano_wrapper_get_arg(A, 55).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">A = 10 .</span></span></code></pre>\n<p>The last piece of code also solves the puzzle from\n<a href=\"/100-days-of-fibonacci-day-3-scala/\">yesterday</a>\nwhere I asked what <em>n</em> is for $fib(n) = 55$.</p>\n<p>The time and space complexity is generally not good when using this\nrepresentation. It is usually only used when reasoning about programs,\ni.e. proving different properties about programs. The idea about\nstructuring problems in terms of nothing but predicates is very\nusable, and is indeed one of the reasons why Prolog is widely used.</p>\n<p>As usual the files are available on\n<a href=\"https://github.com/madsbuch/fibonacci/tree/master/prolog\">Github</a>.</p>\n<h2>Programming Through Predicates</h2>\n<p>Logic programming has its core in predicates. We define logical\npropositions and make the subsystem derive what we need. This works\nvery well when we work on structures like lists and trees.</p>\n<p>The Fibonacci function, however, works on natural numbers which\nwe usually represent as integers for direct translation to\nmachine instructions.</p>\n<p>As seen above it was indeed possible to implement naturals structurally.\nOn a more general note, it is actually possible to model all problems in\na structural manner. This is exploited in amongst other languages\n<a href=\"/100-days-of-fibonacci-day-7-coq/\">Coq</a>.</p>\n<p>Actually, all problems may be expressed in structures. This we will\nget back to when discussing theorem provers.</p>\n<h2>Conclusion</h2>\n<p>Today I looked at Prolog. I implemented the Fibonacci function\nin a logic context. We saw that We didn't exploit Prologs ability\nto derive the argument to Fibonacci. To circumvent that, I\nbuilt a structural implementation of natural, using Peano,\nso we didn't have to make an explicit evaluation. This implementation\nhad the ability to derive what number a given Fibonacci number is,\neven though it is not practical.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .mtk1 { color: #000000; }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/100-days-of-fibonacci-day-5-spreadsheet/","relativePath":"100-days-of-fibonacci-day-5-spreadsheet.md","relativeDir":"","base":"100-days-of-fibonacci-day-5-spreadsheet.md","name":"100-days-of-fibonacci-day-5-spreadsheet","frontmatter":{"title":"Day 5, Spreadsheet","subtitle":"100 Days of Fibonacci","date":"2015-12-20","template":"post"},"html":"<p>This is an article in a series of articles. An overview of the entire\nproject can be found <a href=\"/100-days-of-fibonacci\">here</a>.</p>\n<p>Over the course of this project, small intermedios will occur.\nToday is one of these. The past days I implemented Fibonacci\nin what is considered \"tradition\" programming languages. They\nhave a formal syntax based on a grammar and an alphabet.</p>\n<p>Today I am presenting Fibonacci through a tool that many people\nuse on a daily basis. Also, people who do not come out of a\ncomputer science / engineering world.</p>\n<h2>Day 5 - Spreadsheet</h2>\n<p>I simply implemented Fibonacci by hard coding two values, 0 and 1, in\ntwo cells. These are the values we earlier on has seen as the base cases.\nThereafter I used the formula <code>=B2+B1</code> in the third cell. I could then\nsimply drag and drop that cell allowing the spreadsheet to calculate\nthe respective values.</p>\n<p><img src=\"/images/fib-spread.png\" alt=\"Spreadsheet Fibonacci\"></p>\n<p>The column left to the Fibonacci column is the index of the corresponding\nFibonacci number.</p>\n<p>I have written that this method is dynamic programming using tabulation.\nThe reason for this is that I assume this being a pre-calculated table of\nFibonacci numbers. When one has this table she can quickly look up the number\nshe needs for further calculation. There might be other classifications\nof this technique using other reasons.</p>\n<p>As usual, the file is in my Fibonacci snippet folder and can be found\n<a href=\"https://github.com/madsbuch/snippets/blob/master/fibonacci/fib.ods\">here</a>.</p>\n<h2>Spreadsheets as Programs</h2>\n<p>The main idea with this post is simply to try to broaden up the perception\nof programming. Many people program on a daily basis when they do their\nbudgeting or quickly need to calculate combined costs of some product.</p>\n<p>Programming should not be considered foreign, as it isn't. It is something\nwe all do. But more on this idea in a later post.</p>\n<h2>Conclusion</h2>\n<p>Today I demonstrated a new programming language simply by drag-and-drop.\nThe idea was to broaden up the idea about what programming is. This is not\nthe last time I do so, even though I promise to present some more complex\nconcepts before that.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/100-days-of-fibonacci-day-6-javascript/","relativePath":"100-days-of-fibonacci-day-6-javascript.md","relativeDir":"","base":"100-days-of-fibonacci-day-6-javascript.md","name":"100-days-of-fibonacci-day-6-javascript","frontmatter":{"title":"100 Days of Fibonacci - Day 6, JavaScript","layout":"post","video":false,"comments":false,"date":"2015-12-21","template":"post"},"html":"<p>This is an article in a series of articles. An overview of the entire\nproject can be found <a href=\"/100-days-of-fibonacci\">here</a>.</p>\n<p>Until now I have focused on Fibonacci as a function of some input.\nI have defined a function which returns a value when supplied an argument.\nThis yield a batch processing paradigm. However, the Fibonacci is defined as\na series, and as such it makes sense to treat it as a series.</p>\n<p>In this blog post, I am going to treat Fibonacci as an unlimited stream of\nnumbers. To carry out this idea, I am going to use JavaScript. The reason\nfor this is that JavaScript is a well-known programming language and\nbecause their event system is easy to understand. Later I will expand\nthe notion to other languages where the concept is known as streams,\nsignals, coinductive data structures, etc.</p>\n<h2>Day 6 - JavaScript</h2>\n<p>The core JavaScript implementation is like the iterative implementation\nI did <a href=\"/100-days-of-fibonacci-day-1-c/\">day 1 in C</a>.\nIt is expanded to directly emit an event carrying the result.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"js\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// Start dispatching events</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">function</span><span class=\"mtk1\"> </span><span class=\"mtk10\">startDispatcher</span><span class=\"mtk1\">(){</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">a</span><span class=\"mtk1\">=</span><span class=\"mtk7\">0</span><span class=\"mtk1\">;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">b</span><span class=\"mtk1\">=</span><span class=\"mtk7\">1</span><span class=\"mtk1\">;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">i</span><span class=\"mtk1\">=</span><span class=\"mtk7\">1</span><span class=\"mtk1\">;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk3\">//We never stop the stream</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">while</span><span class=\"mtk1\">(</span><span class=\"mtk4\">true</span><span class=\"mtk1\">){</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">tmp</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">a</span><span class=\"mtk1\">+</span><span class=\"mtk12\">b</span><span class=\"mtk1\">;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk12\">b</span><span class=\"mtk1\">=</span><span class=\"mtk12\">a</span><span class=\"mtk1\">;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk12\">a</span><span class=\"mtk1\">=</span><span class=\"mtk12\">tmp</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk3\">// emit</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk12\">emitter</span><span class=\"mtk1\">.</span><span class=\"mtk10\">emit</span><span class=\"mtk1\">(</span><span class=\"mtk17\">&#39;fib&#39;</span><span class=\"mtk1\">, {</span><span class=\"mtk17\">&quot;fibn&quot;</span><span class=\"mtk12\"> :</span><span class=\"mtk1\"> </span><span class=\"mtk12\">a</span><span class=\"mtk1\">, </span><span class=\"mtk17\">&quot;n&quot;</span><span class=\"mtk12\">:i</span><span class=\"mtk1\">});</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk12\">i</span><span class=\"mtk1\">++;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    }</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span></code></pre>\n<p>When this function is called it will emit a new event every time a\nFibonacci number has been calculated. But this does not work by itself. We\nalso need something to consume it. Following is a function that consume the\nevents and prints a <code>.</code> on the screen. When we hit <em>n = 10</em> we write the\ncorresponding element in the Fibonacci series.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"javascript\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// Listen on the event</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk12\">emitter</span><span class=\"mtk1\">.</span><span class=\"mtk10\">on</span><span class=\"mtk1\">(</span><span class=\"mtk17\">&quot;fib&quot;</span><span class=\"mtk1\"> ,</span><span class=\"mtk4\">function</span><span class=\"mtk1\">(</span><span class=\"mtk12\">e</span><span class=\"mtk1\">){</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk3\">// Only write result when n is 10</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\">(</span><span class=\"mtk12\">e</span><span class=\"mtk1\">.</span><span class=\"mtk12\">n</span><span class=\"mtk1\"> == </span><span class=\"mtk7\">10</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk12\">process</span><span class=\"mtk1\">.</span><span class=\"mtk12\">stdout</span><span class=\"mtk1\">.</span><span class=\"mtk10\">write</span><span class=\"mtk1\">(</span><span class=\"mtk17\">&quot;</span><span class=\"mtk6\">\\n</span><span class=\"mtk17\">&quot;</span><span class=\"mtk1\"> + </span><span class=\"mtk12\">e</span><span class=\"mtk1\">.</span><span class=\"mtk12\">fibn</span><span class=\"mtk1\"> + </span><span class=\"mtk17\">&quot;</span><span class=\"mtk6\">\\n</span><span class=\"mtk17\">&quot;</span><span class=\"mtk1\">);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">else</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk12\">process</span><span class=\"mtk1\">.</span><span class=\"mtk12\">stdout</span><span class=\"mtk1\">.</span><span class=\"mtk10\">write</span><span class=\"mtk1\">(</span><span class=\"mtk17\">&quot;.&quot;</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">});</span></span></span></code></pre>\n<p>When running the file through node we get following:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">$ node fib.js </span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">.........</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">55</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">...................................................................................................</span></span></code></pre>\n<p>As usual, the code is available\n<a href=\"https://github.com/madsbuch/snippets/blob/master/fibonacci/fib.js\">here</a>.</p>\n<h2>JavaScript and Event Driven Programming</h2>\n<p>JavaScript was initially made as a scripting language for browsers and\nhas gained its popularity for programming user interfaces. With faster\nprocessors and faster implementations of JavaScript the browser has got\nmore responsibility, and JavaScript has been turned into a general purpose\nprogramming language. It has been ported to run on the server (with the\nmost widespread implementation as being \"nodeJS\") and databases. It\nis now possible to develop large distributed applications in nothing but\nJavaScript, which is also done.</p>\n<p>So what about the Events? When programming user interfaces the interface\nusually stands still until a button is pressed or an input field is\nfilled. Here the subsystem fires some events whenever something meaningful\nis happening. It is then possible to add functions to be called on these\nevents.</p>\n<p>This approach has been in use for a long time. But has certain disadvantages\nwhen working on large systems. It quickly becomes messy when too many events\nare to be handled and dispatched. But certain solutions already do exists.\nLater on, we will take a look at Elm, Here we model the complete application\nand let the system handle what part of the web page is updated. And don't\nmatter. It is fast.</p>\n<h2>Conclusion</h2>\n<p>Í have introduced the notion of events which are going to support our\nunderstanding of streams and signals for reactive programming later on.\nThis I have done in JavaScript which has turned into a general purpose\nprogramming language primarily for full stack web development.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .mtk3 { color: #008000; }\n  .default-light .mtk4 { color: #0000FF; }\n  .default-light .mtk1 { color: #000000; }\n  .default-light .mtk10 { color: #795E26; }\n  .default-light .mtk12 { color: #001080; }\n  .default-light .mtk7 { color: #09885A; }\n  .default-light .mtk14 { color: #AF00DB; }\n  .default-light .mtk17 { color: #A31515; }\n  .default-light .mtk6 { color: #FF0000; }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/100-days-of-fibonacci-day-8-r/","relativePath":"100-days-of-fibonacci-day-8-r.md","relativeDir":"","base":"100-days-of-fibonacci-day-8-r.md","name":"100-days-of-fibonacci-day-8-r","frontmatter":{"title":"100 Days of Fibonacci - Day 8, R","layout":"post","video":false,"comments":false,"date":"2016-01-11","template":"post"},"html":"<p>I looked at quite some\n<a href=\"/100-days-of-fibonacci\">different approaches to the Fibonacci function</a>,\nand I start to wonder how the Fibonacci number develops with respect\nto its index. To look into this I want to make a 2D plot where the X-axis is\nthe natural numbers and the Y-axis is the corresponding Fibonacci numbers.</p>\n<p>To solve this problem I decided to use R. This is less of an obvious\nsolution. Usually one would probably have gone for a spreadsheet, but\n<a href=\"/100-days-of-fibonacci-day-5-spreadsheet/\">already did that</a>.</p>\n<p>R is a good tool for doing numeric analysis and prototyping algorithms\nfor machine learning. For that it provides good tools for visualizing\ndata. This is what I am going to elaborate a bit on here.</p>\n<h2>Day 8 - R</h2>\n<p>Usually I would have started implementing Fibonacci as a recursive\nfunction. This was also my first approach and it is certainly\npossible. It is, however, not the idiomatic way. R is an array oriented\nlanguage and as such we work on arrays. Under this paradigm it is better\nto initialize an array, and then populate it with its elements.</p>\n<p>The implementation takes offset in a function that takes <em>n</em> as\ninput and returns a list of the <em>n</em> first Fibonacci numbers.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"r\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk10\">fibList</span><span class=\"mtk1\"> &lt;- </span><span class=\"mtk14\">function</span><span class=\"mtk1\">(</span><span class=\"mtk12\">n</span><span class=\"mtk1\">){</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk12\">theList</span><span class=\"mtk1\"> &lt;- </span><span class=\"mtk4\">numeric</span><span class=\"mtk1\">(</span><span class=\"mtk12\">n</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk12\">theList</span><span class=\"mtk1\">[</span><span class=\"mtk7\">1</span><span class=\"mtk1\">] &lt;- </span><span class=\"mtk7\">1</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk12\">theList</span><span class=\"mtk1\">[</span><span class=\"mtk7\">2</span><span class=\"mtk1\">] &lt;- </span><span class=\"mtk7\">1</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk14\">for</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">i</span><span class=\"mtk1\"> </span><span class=\"mtk14\">in</span><span class=\"mtk1\"> </span><span class=\"mtk7\">3</span><span class=\"mtk4\">:</span><span class=\"mtk12\">n</span><span class=\"mtk1\">) {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk12\">theList</span><span class=\"mtk1\">[</span><span class=\"mtk12\">i</span><span class=\"mtk1\">] &lt;- </span><span class=\"mtk12\">theList</span><span class=\"mtk1\">[</span><span class=\"mtk12\">i</span><span class=\"mtk1\">-</span><span class=\"mtk7\">1</span><span class=\"mtk1\">] + </span><span class=\"mtk12\">theList</span><span class=\"mtk1\">[</span><span class=\"mtk12\">i</span><span class=\"mtk1\">-</span><span class=\"mtk7\">2</span><span class=\"mtk1\">]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  }</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk14\">return</span><span class=\"mtk1\">(</span><span class=\"mtk12\">theList</span><span class=\"mtk1\">);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Calling the function</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk12\">fibList</span><span class=\"mtk1\">(</span><span class=\"mtk7\">10</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk12\">fibList</span><span class=\"mtk1\">(</span><span class=\"mtk7\">20</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk12\">fibList</span><span class=\"mtk1\">(</span><span class=\"mtk7\">100</span><span class=\"mtk1\">)</span></span></span></code></pre>\n<p>It was run using the <code>Rscript</code> command</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">mads@mads:fibonacci$ Rscript fib.R </span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [1]  1  1  2  3  5  8 13 21 34 55</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [1]    1    1    2    3    5    8   13   21   34   55   89  144  233  377  610</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">[16]  987 1597 2584 4181 6765</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  [1] 1.000000e+00 1.000000e+00 2.000000e+00 3.000000e+00 5.000000e+00</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  [6] 8.000000e+00 1.300000e+01 2.100000e+01 3.400000e+01 5.500000e+01</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [11] 8.900000e+01 1.440000e+02 2.330000e+02 3.770000e+02 6.100000e+02</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [16] 9.870000e+02 1.597000e+03 2.584000e+03 4.181000e+03 6.765000e+03</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [21] 1.094600e+04 1.771100e+04 2.865700e+04 4.636800e+04 7.502500e+04</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [26] 1.213930e+05 1.964180e+05 3.178110e+05 5.142290e+05 8.320400e+05</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [31] 1.346269e+06 2.178309e+06 3.524578e+06 5.702887e+06 9.227465e+06</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [36] 1.493035e+07 2.415782e+07 3.908817e+07 6.324599e+07 1.023342e+08</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [41] 1.655801e+08 2.679143e+08 4.334944e+08 7.014087e+08 1.134903e+09</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [46] 1.836312e+09 2.971215e+09 4.807527e+09 7.778742e+09 1.258627e+10</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [51] 2.036501e+10 3.295128e+10 5.331629e+10 8.626757e+10 1.395839e+11</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [56] 2.258514e+11 3.654353e+11 5.912867e+11 9.567220e+11 1.548009e+12</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [61] 2.504731e+12 4.052740e+12 6.557470e+12 1.061021e+13 1.716768e+13</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [66] 2.777789e+13 4.494557e+13 7.272346e+13 1.176690e+14 1.903925e+14</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [71] 3.080615e+14 4.984540e+14 8.065155e+14 1.304970e+15 2.111485e+15</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [76] 3.416455e+15 5.527940e+15 8.944394e+15 1.447233e+16 2.341673e+16</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [81] 3.788906e+16 6.130579e+16 9.919485e+16 1.605006e+17 2.596955e+17</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [86] 4.201961e+17 6.798916e+17 1.100088e+18 1.779979e+18 2.880067e+18</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [91] 4.660047e+18 7.540114e+18 1.220016e+19 1.974027e+19 3.194043e+19</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> [96] 5.168071e+19 8.362114e+19 1.353019e+20 2.189230e+20 3.542248e+20</span></span></code></pre>\n<p>When the number is large enough R converts the list into floating point\nnumbers. This conversion is mostly OK when working with probabilities, but\ncan be fatal when we need the exact result. In above example the last element\nhas been cropped. R reports <code>354224800000000000000</code> while the 100th\nnumber is <code>354224848179261915075</code>.</p>\n<p>The type conversion does not matter when visualizing. It won't even mean a\npixel when plotted on the screen, so we will continue on and try to visualize\nthe relationship between $n$ and $fib(n)$.</p>\n<p><img src=\"/images/r-fib.png\" alt=\"100 first Fibonacci numbers.\"></p>\n<p>The relationship looks much like an exponential development. This, however,\nis a subject for a later article.</p>\n<h1>Conclusion</h1>\n<p>I implemented the recursive edition of the Fibonacci algorithm in R. This\nalgorithm is efficient, and R provides efficient subroutines for handling\nthe array.</p>\n<p>After the implementation a simple plot of the relationship between $n$ and\n$fib(n)$ was plotted. This was easily done in R, as R provides good\nabstractions for this.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .mtk10 { color: #795E26; }\n  .default-light .mtk1 { color: #000000; }\n  .default-light .mtk14 { color: #AF00DB; }\n  .default-light .mtk12 { color: #001080; }\n  .default-light .mtk4 { color: #0000FF; }\n  .default-light .mtk7 { color: #09885A; }\n  .default-light .mtk3 { color: #008000; }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/100-days-of-fibonacci-day-7-coq/","relativePath":"100-days-of-fibonacci-day-7-coq.md","relativeDir":"","base":"100-days-of-fibonacci-day-7-coq.md","name":"100-days-of-fibonacci-day-7-coq","frontmatter":{"title":"100 Days of Fibonacci - Day 7, Coq","author":"Mads Buch","date":"2016-01-04","template":"post"},"html":"<p>I have been of a couple of days over the Christmas and New Year.\nBut now it is time to start my\n<a href=\"/100-days-of-fibonacci\">100 days of Fibonacci</a>\nproject again.</p>\n<p>Today I chose Coq and I decided to look at proving properties about\nprograms. The key idea in this post is to prove that two different\nimplementations of the Fibonacci function indeed are equivalent.\nConcretely I have chosen the direct recursive and the accumulated\nrecursive implementations as the subjects.</p>\n<p>These implementations are the same\nas in <a href=\"/100-days-of-fibonacci-day-0-haskell/\">the first article</a>.\nThis is done deliberately as they are idiomatic to functional programming.\nFurthermore, they showcase quite well why this has a value. The directly\nrecursive implementation is easy to understand and stays close to both\nthe definition of Fibonacci and the common understanding of the function.\nOn the other hand, the recursive function with the accumulator is harder\nto understand but provides a significant speedup.</p>\n<p>In this article, I show that the rest of the program can be indifferent\nto which implementation is in use. This is done by proving theorems, which\nis considerably more time consuming that providing tests. The effort returns\nin favor the strongest guarantee we can have for the property, a mathematical\nproof.</p>\n<h2>Day 7 - Coq</h2>\n<p>As mentioned the two implementations are the directly recursive and\nthe recursive with accumulation. These are both implementations we have\n<a href=\"/100-days-of-fibonacci-day-0-haskell/\">seen before</a>.\nBut we use them again as they provide a good body for\nshowcasing proving program equivalence. Next we have the two implementations.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"coq\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">Fixpoint fib_direct (n : nat) : nat :=</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  match n with</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    | 0    =&gt; 0</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    | S n&#39; =&gt; match n&#39; with</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                | 0 =&gt; 1</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                | S n&#39;&#39; =&gt; fib_direct n&#39; + fib_direct n&#39;&#39;</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">              end</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  end.</span></span></code></pre>\n<p>Above is the directly recursive implementation and next is the\naccumulated recursive implementation.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"coq\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">Fixpoint fib_accumulator (n : nat) (a : nat) (b : nat) : nat :=</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  match n with</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    | 0      =&gt; b</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    | (S n&#39;) =&gt; fib_accumulator n&#39; (a+b) a</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  end.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">(* An alias to scrape away unnecessary information *)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Definition fib_acc (n : nat) := fib_accumulator n 1 0.</span></span></code></pre>\n<p>The results can be calculated by the <code>Compute</code> command. It is\nhereafter visible in the <code>*goals*</code> window (Assuming you use the\n<a href=\"https://coq.inria.fr/\">proof assistant</a>).</p>\n<pre class=\"grvsc-container default-light\" data-language=\"coq\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">Compute fib_direct 10.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Compute fib_acc 10.</span></span></code></pre>\n<p>A couple of things look a bit different. The function signature looks\nkind of weird. Furthermore, we are doing some kind of match stuff with\nsomething that looks a bit like numbers.</p>\n<p>In the function signature, we use the <code>Fixpoint</code> to denote a recursive\nfunction. This is because Coq encodes recursion in its type and\nimplement what's called iso-recursion.</p>\n<p>For integers, Coq does not use the regular atomic data types we know from\nex. Java. Instead, it has (and only has) inductive data types. In these\n<a href=\"https://en.wikipedia.org/wiki/Peano_axioms\">Peano naturals</a> has\nbeen implemented. This means that zero is represented as <code>O</code>, one\nas <code>S O</code>, two as <code>S (S O)</code> and so forth (this does not implement integers\nbut only naturals which is just fine for Fibonacci).</p>\n<p>Coq automatically expands syntactic decimal numbers to its\ninternal representation. Hence\n5 is just syntactic sugar for <code>S S S S S O</code>.\nThis is the same representation I\n<a href=\"/100-days-of-fibonacci-day-4-prolog/\">implemented in Prolog</a> earlier on.</p>\n<p>As usual, the code is available on\n<a href=\"https://github.com/madsbuch/fibonacci/tree/master/coq\">Github</a>.</p>\n<p>It is necessary to be able to reduce data to atomic pieces to do the proving\nwe want to do. This is to create a total dependency between the type and its\nvalue. Imagine Java's <code>int</code> type. This type holds, at least,\n$2^{32}$ different\nvalues without the compiler knowing which. In that case, it is not possible to\nreason about the return value of a function, which we need for the next\nsection.</p>\n<h2>Coq - proof assistant</h2>\n<p>Coq is not meant as a general-purpose programming language. In fact, it is\na proof assistant. This means that it is not possible to interact with\nthe surrounding world. It is not possible to read command line arguments or\ninvoke system calls. On the other hand, Coq can prove properties.</p>\n<p>In the above we had two different implementations of the Fibonacci\nfunction. One might wonder whether these functions always return the same\nvalue. That is, does <code>fib_direct 10</code> and <code>fib_acc 10</code> yield the same result?\nThis can easily be checked by running the code. The problem is when we\nwant to check for all values of <code>n</code>. Is it true that\n<code>fib_direct n = fib_acc n</code>? This we can actually prove in Coq, and\nso I have done.</p>\n<p>The first step I took to prove equivalence of the implementations was to\ncharacterize the Fibonacci function.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"coq\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">Definition specification_of_fibonacci (f : nat -&gt; nat) :=</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  f 0 = 0</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  /\\</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  f 1 = 1</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  /\\</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  forall n : nat,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    f (S (S n)) = f (S n) + f n.</span></span></code></pre>\n<p>This is, in fact, a proposition. Is is <em>true</em> if we have a function that behaves\nas the specification. This specification is very close to the directly\nrecursive implementation.</p>\n<p>The next thing is to prove that this definition is unambiguous. This means\nthat <em>f</em> will behave identical independent to the actual implementation. In\nCoq this looks like following (The whole code is available on\n<a href=\"https://github.com/madsbuch/fibonacci/blob/master/coq/fib.v\">Github</a>).</p>\n<pre class=\"grvsc-container default-light\" data-language=\"coq\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">Lemma there_is_only_one_fib :</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  forall f1 f2 : nat -&gt; nat,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    specification_of_fibonacci f1 -&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    specification_of_fibonacci f2 -&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    forall n : nat,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">      f1 n = f2 n.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Proof.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  unfold specification_of_fibonacci.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  intros f1 f2.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  intros [ bc_f1_0 [ bc_f1_1 ic_f1 ] ]. (* Destruct the conjunctive clauses *)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  intros [ bc_f2_0 [ bc_f2_1 ic_f2 ] ].</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">   </span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  (* strengthening the induction hypothesis *)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  assert (H_fib : forall m : nat,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                    f1 m = f2 m /\\ f1 (S m) = f2 (S m)).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  intro m.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  ...</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  apply hf.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Qed.</span></span></code></pre>\n<p>What happens above is that we specify a lemma. Given two functions, that\nboth satisfies the specification of the Fibonacci function, they yield the\nsame result for all values of <em>n</em>.</p>\n<p>After the specification of the lemma, we provide an actual proof. this is a\nseries of commands that generates a function which in turn satisfies the <em>type</em>\nof the lemma. This process is somewhat out of scope, and I will look into this\nin another post.</p>\n<p>After this, all we need to do is to prove that both the implementations indeed\nsatisfies the specification. Hereafter we can use the property that there is\nonly one Fibonacci function to prove the equivalence.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"coq\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">Lemma fib_direct_satisfies_specification :</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  specification_of_fibonacci fib_direct.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Proof.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">...</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Qed.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Lemma fib_acc_satisfies_specification :</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  specification_of_fibonacci fib_acc.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Proof.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">...</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Qed.</span></span></code></pre>\n<p>And the proof that they are equivalent</p>\n<pre class=\"grvsc-container default-light\" data-language=\"coq\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">Theorem fib_direct_is_equivalent_to_fib_acc :</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  forall n: nat,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    fib_acc n = fib_direct n.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Proof.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  intros n.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  rewrite (there_is_only_one_fib fib_acc </span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                                 fib_direct </span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                                 fib_acc_satisfies_specification</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                                 fib_direct_satisfies_specification).</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  reflexivity.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Qed.</span></span></code></pre>\n<p>The signature of <code>there_is_only_one_fib</code> was given above. We\nspecialize it with the arguments so we can rewrite our goal.\nThe goal is then rewritten with <code>forall n, fib_acc n = fib_direct n</code>.\nThis yields a goal which is trivially true. Namely\n<code>fib_direct n = fib_direct n</code>.</p>\n<p>We are now sure that for any <em>n</em> we can provide to the directly recursive\nimplementation of Fibonacci, the other implementation yields the same\nresult.</p>\n<h2>Conclusion</h2>\n<p>In this post, I implemented the Fibonacci function in the same way\nas in the first article.\nThese are idiomatic to functional programming and as such it makes sense\nto keep using them here.</p>\n<p>They key concept introduced here is the notion of proving propositions.\nI proved that the two implementations indeed behave identically. We have\nscratched the surface when it comes to the underlying theory. But\nalready saw the fruits of the efforts: The direct implementation is slow\nbut easy to understand. The other implementation is faster but slightly more\ncomplicated. We have used this to prove that they <em>always</em> return the same\nvalue for the same input.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/100-days-of-fibonacci/","relativePath":"100-days-of-fibonacci.md","relativeDir":"","base":"100-days-of-fibonacci.md","name":"100-days-of-fibonacci","frontmatter":{"title":"100 Days of Fibonacci","subtitle":"Fibonacci in a lot of different ways","img_path":"images/about.webp","template":"page"},"html":"<p>As a small project for rainy sundays I am implementing a function to\ncalculate the fibonacci sequence in various languages. The problem in itself\nis not too interesting and therefore provides a perfect vessel to look at some\npeculiarities about programming languages.</p>\n<ol>\n<li><a href=\"/100-days-of-fibonacci-day-0-haskell/\">Haskell</a>:\nThis post introduces <strong>direct recursion</strong> and <strong>accumulated recursion</strong>\nin Haskell.</li>\n<li><a href=\"/100-days-of-fibonacci-day-1-c/\">C</a>:\nC as an imperative language is introduced, and Fibonacci was implemented\nin an <strong>iterative style</strong>.</li>\n<li><a href=\"/100-days-of-fibonacci-day-2-java/\">Java</a>:\nThe Fibonacci function is implemented by means of\n<strong>dynamic programming</strong>.</li>\n<li><a href=\"/100-days-of-fibonacci-day-3-scala/\">Scala</a>:\nScala is the language, <strong>Continuation Passing style</strong> is the concept.</li>\n<li><a href=\"/100-days-of-fibonacci-day-4-prolog/\">Prolog</a>:\nFibonacci is here implemented in logic programming. Furthermore, we use\nADTs to derive the argument to the function.</li>\n<li><a href=\"/100-days-of-fibonacci-day-5-spreadsheet/\">Spreadsheet</a>:\nThe key idea is to abstract the concept of programming to include more\nthan written text.</li>\n<li><a href=\"/100-days-of-fibonacci-day-6-javascript/\">JavaScript</a>:\nIntroducing the concept of <strong>events</strong> in JavaScript. This is a base for\nthe concepts of streams, signals, etc. which will be introduced later on.</li>\n<li><a href=\"/100-days-of-fibonacci-day-7-coq/\">Coq</a>:\nThe introduction of <strong>theorem proving</strong> using a programming language.</li>\n<li><a href=\"/100-days-of-fibonacci-day-8-r/\">R</a>: Using R to plot the\nrelationship between $n$ and $fib(n)$.</li>\n<li><a href=\"/100-days-of-fibonacci-day-9-haskell-types/\">Haskell Types</a>:\nImplementing the Fibonacci function using <strong>dependent programming</strong>\nin Haskell.</li>\n<li><a href=\"/100-days-of-fibonacci-day-10-python/\">Python</a>:\nFocusing on the relationship between <strong>list comprehensions</strong> and\n<strong>set definitions</strong> in mathematics.</li>\n<li><a href=\"/100-days-of-fibonacci-day-11-fixed-point/\">Fixed Point</a>:\nIntroducing the theoretical concept of fixed points.</li>\n<li><a href=\"/100-days-of-fibonacci-day-12-julia/\">Julia</a>:\nUsing fast exponentiation.</li>\n</ol>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/100-days-of-fibonacci-day-9-haskell-types/","relativePath":"100-days-of-fibonacci-day-9-haskell-types.md","relativeDir":"","base":"100-days-of-fibonacci-day-9-haskell-types.md","name":"100-days-of-fibonacci-day-9-haskell-types","frontmatter":{"title":"100 Days of Fibonacci - Day 9, Haskell Types","excerpt":"Today I implemented Fibonacci in the Haskell type system. That means that I can get the Haskell compiler to generate a type for the n'th Fibonacci number.","date":"2016-01-29","template":"post"},"html":"<p>Haskell has a flexible type system. It actually is Turing complete\ngiven the right language extensions. This also means that we can do\narbitrary computations, which we are going to exploit in this 10th\nday in my <a href=\"/100-days-of-fibonacci/\">100 days of Fibonacci</a>\nchallenge.</p>\n<p>I already did <a href=\"/100-days-of-fibonacci-day-0-haskell/\">look at Haskell</a>.\nSo strictly speaking I should choose another language.\nHowever, I find that programming in Haskell's type system is different\nenough that I will consider it a different language.</p>\n<h2>Day 9 - Haskell Types</h2>\n<p>Today I implemented Fibonacci in the Haskell type system. That means that I\ncan get the Haskell compiler to generate a <em>type</em> for the <em>n</em>'th\nFibonacci number.</p>\n<p>I first created a datatype for representing naturals.\nThe approach as in the last part of the\n<a href=\"/100-days-of-fibonacci-day-4-prolog/\">Prolog implementation</a> and\nuse used in <a href=\"/100-days-of-fibonacci-day-7-coq/\">Coq</a> was used.\nThis representation\nbuilds on the <a href=\"https://en.wikipedia.org/wiki/Peano_axioms\">Peano axioms</a>\nand is straight forward to implement.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"hs\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">data Nat = Z | S Nat</span></span></code></pre>\n<p>This datatype is usable as a <a href=\"https://wiki.haskell.org/Kind\">kind</a>\nbecause we use the language extension <em>DataKinds</em>. A kind can be\nthought of as a type of a type.</p>\n<p>In <code>ghci</code> we can inspect the kind of the type constructor <code>Z</code> by issuing\n<code>:kind Z</code>. The result returned is <code>Z :: Nat</code>.</p>\n<p>Next I implemented addition. In Haskell we use the\nlanguage extension <em>TypeFamilies</em> to have a mechanism for implementing\ntype level functions.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"hs\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">type family Add (a :: Nat) (b :: Nat) :: Nat</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">type instance Add  Z    b = b</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">type instance Add (S a) b = S (Add a b)</span></span></code></pre>\n<p>Again we can try to inspect the kind of the type level function:\n<code>:kind Add (S Z) (S Z)</code> which yields <code>Add (S Z) (S Z) :: Nat</code> as\nexpected.</p>\n<p>To actually calculate the Fibonacci type we need Haskell to reduce the <code>Add</code>\nexpression. This is done using the <code>:kind! Add (S Z) (S Z)</code> operation.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"text\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">*FibType&gt; :kind! Add (S Z) (S Z)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Add (S Z) (S Z) :: Nat</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">= &#39;S (&#39;S &#39;Z)</span></span></code></pre>\n<p>We can now both define type level datatypes (kinds) and do operations on these.\nAlong this line I implemented Fibonacci straight forward in direct recursion.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">type family Fibonacci (n :: Nat) :: Nat</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">type instance Fibonacci Z           = Z</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">type instance Fibonacci (S Z)       = (S Z)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">type instance Fibonacci (S (S n))   = Add (Fibonacci n) (Fibonacci (S n))</span></span></code></pre>\n<p>To implement above type family I had to add the language extension\n<em>UndecidableInstances</em>. This is because of the use of the <code>Add</code> type\nfamily in the <code>Fibonacci</code> type family.</p>\n<p>In this example, however, it is easy to see that the type family will\nalways converge. <code>Add</code> converges (and does not need\n<em>UndecidableInstances</em>) and <code>Fibonacci</code> converges as the arguments\nto the recursive applications are decreasing.</p>\n<p>The last thing is to actually calculate Fibonacci. This can be done by\nissuing the <code>:kind!</code> as earlier on and read the result. The returned\nvalue is not easily readable but it is 13 as expected.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"text\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">$ ghci FibType.hs -XDataKinds</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">...</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">*FibType&gt; :kind! Fibonacci (S (S (S (S (S (S (S Z)))))))</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Fibonacci (S (S (S (S (S (S (S Z))))))) :: Nat</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">= &#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S &#39;Z))))))))))))</span></span></code></pre>\n<p>The code is as usual available on\n<a href=\"https://github.com/madsbuch/fibonacci/tree/master/haskell\">Github</a>.</p>\n<p>Types alone does, however, not make a program. We need some kind of value\nlevel implementation. In the next section I make a similar implementation\nof the type level functions on the value level. Coupling the type and the\nvalue in an one-to-one correspondence yields\n<a href=\"https://wiki.haskell.org/Dependent_type\">dependent programming</a></p>\n<p>Dependent types are used to statically reason about programs and\nput up guarantees that can be checked on compile time.\nIn this case we can set up the guarantee\nthat the code <em>actually</em> calculates the <em>n</em>'th Fibonacci number.</p>\n<h2>Implementing Dependent Typing</h2>\n<p>With dependent typing the type of a term <em>depends</em> on its value. In\nHaskell we can benefit from making some parts of the types dependent.\nThis could for example be statically sized lists to make\nmatrix operations type-safe.</p>\n<p>In this post I implement complete dependency between terms and their type.\nThe type for the Fibonacci term was implemented above. Now we just need to\nwrite some code that couples it to the value level.</p>\n<p>First we couple the datatype. I implemented a value level datatype,\n<code>SNat</code>, which embeds its size.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"hs\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">data SNat (a :: Nat) where</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    SZ   :: SNat Z</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    SS   :: SNat n -&gt; SNat (S n)</span></span></code></pre>\n<p>In the above the value level constructor has the type <code>SNat Z</code>. From\nthis type alone, we can read that it is the <em>zero</em> element.</p>\n<p>The successor value constructor has the type <code>SNat n -> SNat (S n)</code>.\nHere the type is constructed depending on which number element we\nconstruct.</p>\n<p>An important property of above datatype is the bijection between\na datatype and its type. This is what we use to statically reason about\nour programs and make sure certain guarantees are held.</p>\n<p>After that we implement the addition function</p>\n<pre class=\"grvsc-container default-light\" data-language=\"hs\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">add :: SNat m -&gt; SNat n -&gt; SNat (Add m n)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">add SZ      b = b</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">add (SS a)  b = SS (add a b) </span></span></code></pre>\n<p>The value level add function stays completely true to the type-level\nadd function and is together with the Fibonacci function below fairly\nself explanatory.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"hs\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">fibonacci :: SNat n -&gt; SNat (Fibonacci n)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">fibonacci SZ            = SZ</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">fibonacci (SS SZ)       = (SS SZ)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">fibonacci (SS (SS n))   = add (fibonacci n) (fibonacci (SS n))</span></span></code></pre>\n<p>We now build a Fibonacci function where the returned value is bijective\nto its type. Hence we are sure that what is computed at runtime is something\nwe can predict on compile time.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"text\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">$ ghci -XDataKinds FibType.hs </span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">...</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">*FibType&gt; let f = fibonacci $ SS $ SS $ SS $ SS $ SS $ SS $ SS $ SZ</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">*FibType&gt; :t f</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">f :: SNat</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">       (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S (&#39;S &#39;Z)))))))))))))</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">*FibType&gt; f</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">13</span></span></code></pre>\n<p>As expected the type of the term <em>fibonacci 7</em> has the type 13 (translated\nfrom the unary representation to decimal). After evaluating the term we also\nget the value <em>13</em>.</p>\n<h2>Applications for Dependent Programming</h2>\n<p>All above is perfectly good. But why bother writing so much more code\nto just have a dependency between the value and its type? Well, for most\napplications this is not necessary, but the technique can be used to set\nstrict guarantees in certain situations.</p>\n<p>When you know the shape of the data-types on compile-time it can be\nadvantageous to model this shape into the program. This could be\nsome uses of matrices.</p>\n<p>When implementing for example neural networks, the upper bound on\nthe topology is usually known when writing the code. In this case\none could use type safe matrix libraries to implement this functionality.</p>\n<h2>Conclusion</h2>\n<p>In this post I first implemented the Fibonacci function on the type level.\nI made a type for the <em>n</em>'th element in the Fibonacci series. This was\ndone through data kinds and type families.</p>\n<p>After this a value level implementation was implemented. It was implemented\nin such a way that its type was bijective to its return value.</p>\n<p>The dependency between the terms and the types was carried out through\nGADTs which allows us to encode the type level natural in the types for\nthe values.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/about/","relativePath":"about.md","relativeDir":"","base":"about.md","name":"about","frontmatter":{"title":"About","subtitle":"Rather do it than not","img_path":"images/about.webp","template":"page","indexOrder":1},"html":"<p><strong>Mads Buch</strong> is a Computer Scientist. Since\ngraduating his <a href=\"https://thesis.madsbuch.com\">Ms. Science in Computer Science</a>\nform Aarhus University he has\nworked with a variety af companies and developed his skills as a software\nengineer.</p>\n<p>For any inquiries contact me on <code>me [@] madsbuch [.] com</code>. Project portfolio\navailable <a href=\"/project-portfolio\">here</a>.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/abstractions/","relativePath":"abstractions.md","relativeDir":"","base":"abstractions.md","name":"abstractions","frontmatter":{"title":"When in Doubt, Do Not Abstract","author":"Mads Buch","date":"2022-01-09","template":"post","excerpt":"We should embrace the way we know that developers are changing the codebase, not the way we want them to change it. Developers seldomly have context on the entire code base, and as such, we should not create too great of an abstraction distance from the place bugs are realized to the place they are implemented."},"html":"<p>Software developers want to make abstractions, and understandably! It is an intellectual effort to do so, and a craft to elegantly implement. The reasoning behind the abstractions is to make life easier for other software developers – an admirable goal. But there are some things we tend to forget in this process as software developers.</p>\n<p>The first thing is that learning abstraction is like learning a new language. Programming languages by themselves are abstractions that are composed. Creating components, functions, classes, etc. can be seen as a means of extending this programming language. They ought to be learned, and in teams, it is a real effort to disseminate and align the use of abstraction.</p>\n<p>The next is that some abstractions do not scale with future use cases. An example could be to make a modal overlay that insists on getting a title, a dismiss callback, and a confirm callback as parameters. When writing it, it might seem like a good idea but becomes detrimental the second a developer is making a modal that does not fit that specification. One that does not require the confirm button. Instead, the modal should do only one thing – the modal, and take the entire contents as a child trusting the user of the component to be able to implement the design spec.</p>\n<p>The third, and potentially most important, is that abstractions rarely embrace the way we add features, patch bugs, or generally change a codebase. When a developer sees an error on, eg. a website, the first step is to locate the error in the code. One was is to find some text close to the error and search through the codebase for an exact match. This is incredibly effective when the buggy code is also close to the found match. But when layers upon layers of abstractions are applied, it can quite some time locate the buggy code. Finding out that the code is re-used in 3 other places in the codebase can make it virtually impossible to fix without the risk of breaking the code in other places.</p>\n<p>All this is not to say that we should not abstract. It is just to say, that we need to be careful when doing so. A good place to start is abstraction elements that have broad consensus. It is like writing an article. One is free to make up their own sections, but following the consensus of abstract, introduction, discussion, related works, conclusion just makes it easy for people to read.</p>\n<p>Using well-known abstractions like models, views, services, components, and the like for specific frameworks equally makes things easier. And then, when in doubt, do not abstract, don’t be shy of repeating yourself, and equally don’t shy of refactoring when the right abstraction immerges.</p>\n<p>And lastly, we should embrace the way we know that developers are changing the codebase, not the way we want them to change it. Developers seldomly have context on the entire code base, and as such, we should not create too great of an abstraction distance from the place bugs are realized to the place they are implemented.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/adversarial-statistics/","relativePath":"adversarial-statistics.md","relativeDir":"","base":"adversarial-statistics.md","name":"adversarial-statistics","frontmatter":{"title":"Adversarial Statistics","author":"Mads Buch","date":"2021-03-10","template":"post","excerpt":"Let an adversary add realistic data to your analysis to see if it is resilient."},"html":"<p><em>Main point: Let an adversary add realistic data to your analysis to see if it is resilient.</em></p>\n<p>At the heart of statistics is the desire to make what we could call question\n/ answer equilibrium. The question has a sound relation to that answer <em>via</em>\nthe observation. The observations should ideally be monotonically increasing\nbut ever true and the question / answer congruence should be resilient to\nadditions to the data. This requires us to be careful about the questions\nin particular and sometimes we need to amend the question to make the answer\nmore resilient.</p>\n<p>Imagine a question like \"what is the life expectancy\". We can calculate that by\ntaking the mean of all ages of all deceased people we have registered and report\nthat as the answer. However, this is not resilient as new data might alter\nthe answer because people get older. To mitigate this we can reiterate on the\nquestion and ask \"what is the life expectancy in 2020\" which would have the\nresilience to new data.</p>\n<p>A formal method is to apply an adversarial model to statistics. To do this\nwe make a system where we can prove that the addition of new data will\nchange the question / answer relation. A such formalism can be based on typed\ngraphs where we model all that entities and their relations. We prove that\nadding new entities will not change already published results.</p>\n<p>We can imagine a system that has the following entities:\n<em>Person</em>, <em>Country</em>, <em>Year</em>. These entities can be linked by\n<code>Person -- livesIn --> Country</code>, <code>Person -- deceasedIn --> Year</code>. An example\ndataset to adhere to that <em>ontology</em> could be laid out as follows</p>\n<p><img src=\"/images/deceased.png\" alt=\"deceased.png\"></p>\n<p>To answer questions in the above-pictured dataset we can use the Monte Carlo simulation technique.\nFor the first question, \"what is the life expectancy,\" we draw a deceased person\n10.000 times and average their ages. To answer the other question,\n\"what is the life expectancy in 2020\" we draw a person that has a <code>deceasedIn</code>\nrelation to 2020 10.000 times and average over their ages.</p>\n<p>For each of the questions, we invite the meanest adversary we know and allow her to\nput in new entities with one rule: The entities are realistic. Ie. as we are in\n2021 now it is not possible to add more people deceased in 2020. The adversary\nquickly figures out that she can merely decease one of the still-alive people\nto change the answer to the first question. However, for the second question\nno new relations within the specified scope would</p>\n<p>The above network would obviously be wildly bigger in reality and also include\nmany more classes of data. Data might be gathered from several different data\nsources which can be modeled using <em>multi-layer networks</em>. Furthermore,\nWhen communicating in real life one should also take\ncontext into account. Ie. If we ask \"what is the life expectancy\" it can\nreasonably be assumed that it is in the year 2020 (and not eg. 1820).</p>\n<p>I am currently building <a href=\"https://github.com/madsbuch/fog\">Fog</a> that is a Haskell\nlibrary to experiment with formalisms for skeptic reasoning. If you are\nalso curious about this field and want to talk, please reach out!</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/analyst-book-exploring-data/","relativePath":"analyst-book-exploring-data.md","relativeDir":"","base":"analyst-book-exploring-data.md","name":"analyst-book-exploring-data","frontmatter":{"title":"Analyst Book - Exploring Data","subtitle":"Using an AI platform to understand","img_path":"images/data.png","template":"page"},"html":"<p>This document is a working document using the <a href=\"AI%20platform\">/data-ai</a> to understand\nfraud patterns and build systems to find these patterns at scale. In contrary\nto previous ways to think about this, we assume no hypothesis in these workflows.</p>\n<p>We use a three-step approach to investigate this type of data: Inspect,\nstatistics, predict.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/ai-tools/","relativePath":"ai-tools.md","relativeDir":"","base":"ai-tools.md","name":"ai-tools","frontmatter":{"title":"Thoughts on AI Tools","author":"Mads Buch","date":"2021-05-08","template":"post","excerpt":"AI is hot stuff and many tools are marketed as AI tools. But what is an AI tool and how does it differentiate from a non-AI tools?"},"html":"<p>AI is hot stuff and many tools are marketed as AI tools. But what is an AI tool\nand how does it differentiate from a non-AI tool?</p>\n<p>Intervention.</p>\n<p>Intervention is the key differentiator. When developing intelligent tools, the\ninteraction should be intervention - it is, after all, intelligent. Just like\nwe delegate tasks to intelligent beings with the assumption that the person\nis capable, only to intervene when things go wrong.</p>\n<p>Let's take an example: A system to assist with accounting. The non-AI system\nlets users enter accounts for settlement, potentially recommending accounts.\nThe AI system settles accounts with a key belief that it is done correctly.\nThe user can, however, intervene when the result seems of and let the system\nknow how the correct settlement. The system should be transparent about its\nplanning and easily let the user correct settlements.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/a-practical-introduction-to-haskell-part-1/","relativePath":"a-practical-introduction-to-haskell-part-1.md","relativeDir":"","base":"a-practical-introduction-to-haskell-part-1.md","name":"a-practical-introduction-to-haskell-part-1","frontmatter":{"title":"A Practical Introduction to Haskell, Part 1","author":"Mads Buch","date":"2016-04-08","template":"post"},"html":"<!--{ Prerequisites for following along | }-->\n<p>This post series provides an introduction to Haskell through a\npractical example. It\nassumes no prior use of the language or functional programming experience.\nA basic understanding of programming, knowledge of using the terminal in Linux,\nand knowing how to install the software is, however, expected.</p>\n<!--{ Introduction to the article | grave serious formal }-->\n<p>This tutorial will walk you through setting up the environment using\n<a href=\"http://haskellstack.org\">Haskell Stack</a>, initializing projects, writing\ncode, interactively run it, test it, and build / deploy it.</p>\n<!--{ Introduction to the mini project | }-->\n<p>As a body of this investigation we are going to develop a little\ninterpreter. We will be able to do addition, multiplication, subtraction,\nand division on floating point numbers. Interpreters and compilers\noften made in functional programming languages as the paradigm provides\ngood tools for working with the structure.</p>\n<!--{ Overview of the article | }-->\n<p>A lot of material is presented in this talk. The core elements are as\nfollows:</p>\n<ol>\n<li>Motivation. Why even consider functional programming languages?</li>\n<li>Setting up the environment</li>\n<li>Functional constructions: Algebraic datatypes and functions</li>\n<li>Functional way of thinking</li>\n</ol>\n<p>In the next (still unpublished) articles we will cover the rest:</p>\n<ul>\n<li>Setting up testing</li>\n<li>Input/Output</li>\n<li>Build / Deploying</li>\n</ul>\n<p>Everything will be brief, but luckily it is greatly documented.\nSo after reading this, if you want to go further, give it away\nand search the Internet!</p>\n<h2>Why Functional programming</h2>\n<p>\"I already know Java and it provides a well paying job. Why bother\nspending time on this?\"</p>\n<!--{ Why other paradigms | }-->\n<p>It shall not be a secret: There exists many paradigms and even more\nways to solve problems (This is evident from my\n<a href=\"/100-days-of-fibonacci\">100 days of Fibonacci</a>\nchallenge). None of these methods are right or wrong, but some fit better\nin given situations than others.</p>\n<!--{ Better programmer | }-->\n<p>My proposition is that people in general become better problem\nsolvers / programmers by having multiple perspectives on the problem.\nThis is a key assumption for this presentation and can indeed be opposed.\nThis is, however, out of scope.</p>\n<!--{ Why functional | }-->\n<p>Functional programming is one of the key paradigms. The key benefits of\nfunctional programming are the inherent tools for securing code correctness\nand very good tools for architecting applications.\nThis comes from the use of types. It is furthermore extremely flexible from\nnatural use of higher order functions. This allows to build powerful\nabstractions that are type safe and still intuitive to use.</p>\n<h2>Why Haskell</h2>\n<!--{ Introduction to Haskell | }-->\n<p>Haskell is a general purpose programming language based in pure\nfunctional programming. By general purpose it is understood that no\nspecific domains of problems, or industries, are targeted rather than\nothers. This is evident from its use in everything from webframeworks\nto embedded software development for micro processors and high\nperformance scientific computing.</p>\n<!--{ Flexibility of Haskell | }-->\n<p>Haskell has mechanism for making embedded domain specific languages\n(EDSL) to target certain tasks. This adds to its flexibility as we\nare able to modify the language <em>syntax</em> to accommodate our needs. This\nis useful if one wants to make a framework for a certain industry or\nclass of problems.</p>\n<!--{ Haskell's community support | }-->\n<p>The Haskell repositories are also well equipped. We are going to use\nStackage as we use Stack. Otherwise community packages are usually pulled\nfrom Hackage.</p>\n<!--{ Developer Tool chain for Haskell | }-->\n<p>the Haskell ecosystem is being heavily developed at the moment.\nEspecially the tool chain for writing, testing, and deploying code.\nIn this post we take offset in Stack. It is a large integrated\ntool which depends on the already wide spread cabal. But enough\ntalking. The first thing to get started is to set up the environment\nand get a hold on the first couple of commands.</p>\n<h2>The Environment</h2>\n<!--{ Introduction of Haskell Stack | }-->\n<p>As mentioned we use Stack to manage our environment. Stack\nautomatically downloads and sets up the\nright compiler, builds, tests, and makes documentation for the project.\nIt is also capable of deploying using docker and much more.</p>\n<!--{ Installation of Haskell Stack | }-->\n<p>Stack can be installed on all major platform. Instructions are available\non the\n<a href=\"http://docs.haskellstack.org/en/stable/README/#how-to-install\">website</a>.</p>\n<h3>Setting up a Project</h3>\n<p>Having Stack setup we can know create our project. This is done by the\nsubcommand <code>new</code>.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">$ stack new awesomepreter</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Downloading template ...</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">...</span></span></code></pre>\n<p>Stack creates a new folder, <code>awesomepreter</code>, and populates it\nwith boilerplate code and folders.</p>\n<!--{ post setup instructions |  }-->\n<p>After the project was created we are going to make Stack download and setup\nthe compilers we need. This is done by <code>cd</code>-ing into the directory and run the sub command <code>setup</code>.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">    $ cd awesomepreter</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    awesomepreter$ stack setup</span></span></code></pre>\n<p>We can now run the interpreter, <code>stack ghci</code>, and test, <code>stack test</code>. These\nare the only two commands we are using for now.</p>\n<p>We furthermore have 3 folders in the newly created project root.</p>\n<ul>\n<li><code>app</code>:  Contains all code related to the execution. This usually includes\ncode for controlling program flow, reading arguments etc.</li>\n<li><code>src</code>:  This folder contains all the business logic.</li>\n<li><code>test</code>: All our tests are placed in this folder.</li>\n</ul>\n<h3>Packages</h3>\n<!--{ Introduction to packages |  }-->\n<p>We use packages in order to leverage on the work of other talented people.\nNobody want to reinvent the deep plate, so we use others inventions.\nFurthermore package systems are also used to organize the code we write.\nFor this organizations might setup their own package management systems.</p>\n<!--{ What we use | }-->\n<p>For this project we only use a few packages in order to test our code.\nWe use <em>HUnit</em> and some supporting packages.</p>\n<!--{ How to add packages | }-->\n<p>The packages we need are added by including following in the\n<code>awesomepreter.cabal</code> file. It has to be added to <code>build-depends</code> under the\n<code>test-suite awesomepreter-test</code> section as it is where we use it.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">    , HUnit</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    , testpack</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    , test-framework</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    , test-framework-hunit</span></span></code></pre>\n<!--{ How to apply the changes | }-->\n<p>For the changes to apply to our project we need to ask stack to solve\ndependencies. This is a rather easy process done with a single command.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">    awesomepreter$ stack solver --update-config</span></span></code></pre>\n<p>From here the packages are available in our test part of the project. But\nbefore we will start using these packages we will start implementing the\ninterpreter.</p>\n<h2>Functional Constructs</h2>\n<!--{ We start coding | }-->\n<p>Now the environment is set up for our purposes, and we will return to some\nsome programming discussing. For this section we will turn our attention\non the <code>src</code> folder, which contains the primary part of our program.</p>\n<!--{ Cleanup preparations | }-->\n<p>First thing: A bit of cleanup. In the file <code>src/Lib.hs</code> we remove the\nstring <code>(someFunc)</code> from the beginning so it says <code>module Lib where</code>.\nThis is because we don't want to consider the module system at present\ntime.</p>\n<!--{ Recap on the project we are implementing | }-->\n<p>We can now continue on with the project: We wanted to build an interpreter\nthat interprets expressions like <code>2 + 5</code> and <code>3 + 7 / 8</code>. To do this\nwe consider\nhow to model the problem, and how evaluate instances of that model to a\nresult. In functional programming we model such things in algebraic\ndatatypes (ADT) and implement the evaluation as functions on that datatype.</p>\n<h2>Algebraic Datatypes</h2>\n<!--{ How we interpret ADT in this project | }-->\n<p>In this project we see an ADT as a construction which takes an <em>operation</em>\n(the constructor) and some associated data. For the above example, <code>2 + 5</code>,\nthe operation is addition and the data is 2 and 5. Hence we make an addition\nconstructor taking the two number: <code>Add 2 5</code>.</p>\n<!--{ Arbitrarily nesting | }-->\n<p>Above is not the whole story as we need to satisfy out types. We want to\nbe able to build nested expressions, something like <code>Add 2 (Add 2 3)</code>. But\nas the number, 2 and 3, have the type <code>Int</code> and Add has some other type this\nis not possible. To get around this we make a constructor which just hold\nthe number: <code>Lit 4</code>. This is also an inhabitant of the same type as <code>Add</code>.</p>\n<!--{ This is done through ADTs | }-->\n<p>Haskell has ADTs through the <code>data</code> construction. Such a thing consists\nof a type deceleration and a number of constructors each with some\nassociated data.</p>\n<!--{ Haskell implementation | }-->\n<p>We implement this in the <code>src/Lib.hs</code> file. First we change the module\nsignature to <code>module Lib where</code> and then we append the following lines\nof code to the file.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">data ALang =</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    Lit Float</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  | Add ALang ALang</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  | Mul ALang ALang</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  | Sub ALang ALang</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  | Div ALang ALang</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  deriving Show</span></span></code></pre>\n<p>Then open the interpreter by issuing <code>stack ghci</code> from the project root.</p>\n<p>After a bit of compiling a prompt should be visible and ready to take input</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">awesomepreter$ stack ghci</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">...</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Ok, modules loaded: Lib, Main.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">*Main Lib&gt; </span></span></code></pre>\n<p>Here we can write <code>Lit 42</code> for which we will see the evaluated result\n<code>Lit 42.0</code>. Haskell did automatic conversion to floats, hence the trailing\n0.</p>\n<p>The <strong>deriving Show</strong> we added in the end of the declaration makes Haskell\nfigure out how to show instances of this data type itself.</p>\n<h2>Functions</h2>\n<p>To perform the actual interpretation of the expression we need to implement\na function. We want a function that takes an expression as input, and emits\nthe result as output.</p>\n<!--{ Pattern matching | }-->\n<p>The first construction we hit when defining functions for algebraic\ndatatypes is <strong>pattern matching</strong>.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">awesomepret (Lit x)     = x</span></span></code></pre>\n<!--{Why pattern matching | }-->\n<p>Pattern matching is used to two things: Unpacking datatypes and making\nspecialized functions for each case in the datatype, and providing a way\nto unpack data from constructors. The above\ncase is the simplest case, where we simply remove the constructor\nand return the contents.</p>\n<!--{ Recursion as iteration | }-->\n<p>In pure functional programming <strong>recursion</strong> is how we provide iteration.\nWhere we are normally used to have a language construction to provide this\nfunctionality, we here implement it on our value-level functions</p>\n<p>The this case we have a concrete tree. Each node is an operation and\nthe leaves are literals. By recursion we aggregate this tree\ninto a single value by using the build-in\noperations form Haskell.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">awesomepret (Add a b)   = (awesomepret a) + (awesomepret b)</span></span></code></pre>\n<p>Th make the expressions easier to use, we can use a <strong>let .. in ..</strong> expression\nto make intermediate values. This makes big expressions easier to read and\nwrite.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">awesomepret (Mul a b)   = let</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                            ae = awesomepret a</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                            be = awesomepret b</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                          in</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                             ae * be</span></span></code></pre>\n<p>This naturally leads to the <strong>where</strong>-expression. For our purpose this\nis also an organizational tool. It makes the code easier to read.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">awesomepret (Sub a b)   = ae - be</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  where</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    ae = awesomepret a</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    be = awesomepret b</span></span></code></pre>\n<p>Often we need to do something different based on the value of a variable.\nIn this case we need to check if we divide by 0, and throw an exception if\nwe do so. In Haskell we can use <strong>guards</strong> for this. This is strictly more elegant than in-lining an <em>if-then-else</em> expression.</p>\n<p>In our case we don't want to allow devision by 0:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"10\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">awesomepret (Div a b)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">               | (awesomepret b) == 0    = error &quot;Division by 0&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">               | otherwise               = (awesomepret a) / (awesomepret b) </span></span></code></pre>\n<p>We now have a working interpreter we can use from the Haskell REPL.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"11\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">awesomepreter$ stack ghci </span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">...</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">*Main Lib&gt; awesomepret (Add (Lit 5) (Lit 2))</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">7.0</span></span></code></pre>\n<h1>The Functional Way of Thinking</h1>\n<p>We  now have a working piece of code, and we can try to dissect it to\nfigure out how we reasoned to get to this result.</p>\n<p>The data we worked on was organized as a tree. In pure functional programming\neverything are trees. Even the program we express is a tree. When executing\nthe program these trees are reduced as much as possible. This new tree,\noften just a singly value, is the result of the computation.</p>\n<p>We also see that we have no language constructions. Everything can be put in\nvariables and passed around. This provides a high degree of flexibility. We\nhave tools for expressing powerful abstractions, but also to make completely\nunreadable code.</p>\n<h2>To Imperative Languages</h2>\n<p>Coming from an imperative language there are some main differences one should\nhave in mind.</p>\n<p>There are <strong>no sequential</strong> ordering of expressions. In pure functional\nprogramming we solely have functions. When we are not able to reduce a\nfunction expression any further, we have our result. And yes, the result\nis a function. To get greater insight in this the\n<a href=\"https://en.wikipedia.org/wiki/Lambda_calculus\">Lambda Calculus</a> is a\ngood starting point.</p>\n<p>We also have a whole new vocabulary about functions:</p>\n<ul>\n<li><strong>Higher order function:</strong> Functions can be provided as arguments and\nreturned from functions.</li>\n<li><strong>First class citizen:</strong> Functions are passed around in variables like\nordinary values.</li>\n<li><strong>Partial Functions:</strong> In the case of Haskell it means that a function does\nnot pattern match on all cases of the datatype.</li>\n</ul>\n<p>This vocabulary is also available to mainstream programming languages such\nas Java, C#, etc. But they are central to functional programming.</p>\n<p>The last difference to imperative programming is the lack of control\nstructures. That's right, we don't have any <code>while</code>-loops,\n<code>if</code>-expression, etc. But fear not. We can express the same computation\nin functional programming. For the iteration idioms we use recursion\nand for the conditionals we use pattern matching.</p>\n<h2>What I want vs. What to do</h2>\n<p>When doing imperative programming we have an explicit <em>state</em>. This state\nis concrete variables. When performing computation we modify the state\nin a linear fashion. We might concatenate a couple of strings and add\nsome numbers. In the end we return either a value, a pointer to the\ndata we made, or nothing. The last case assumes that we did some effect\nfull programming and that we are able to read the results from somewhere\nelse. This is not how we do when thinking functional programming.</p>\n<p>In functional programming we think about what we have and what we want.\nThese objects are reasoned about as inhabitants of a type. A type is here\nunderstood as set of elements.</p>\n<p>A function is a map from one type to another. A whole program is understood\nas a map from some initial element of a given type, to another. This map,\nor function, is in Haskell called <code>main</code>.</p>\n<h1>Conclusion</h1>\n<p>We have enough to formulate simple expressions and evaluate them in the\ninterpreter. Next thing, for practical Haskell programs, is to set up\nautomated testing. This is to allow great flexibility in software development.\nAfter that we also need to think about how users can access the\nfunctionality we have build. So we look into deploying.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/assumptions/","relativePath":"assumptions.md","relativeDir":"","base":"assumptions.md","name":"assumptions","frontmatter":{"title":"The Easiest-Assumptions Solution","author":"Mads Buch","date":"2021-02-04","template":"post","excerpt":"Developing solutions for problems is hard. Make it a little easier by choosing the assumptions that allows to do so."},"html":"<p><em>Main point: When confronted with the need to make assumptions on software</em>\n<em>development, pick the one that is easiest to implement.</em></p>\n<p>I am working with software and understanding data for a living. The processes\nsurrounding that are highly dependant on learning fast, predictable and\nreliable to be able to deliver before specified deadlines. This partially\nrelies on faith but mostly on an iterative approach where I move\nforward before finishing a step with the expectation of iterating back\nlater on with a deeper understanding. I embrace it and let\nit lead the way I work.</p>\n<p>Knowledge is partial and requirements vague. Hence, it is important\nto be aware of assumptions vs. knowledge. As for the assumptions we should\nalways choose those that makes our life the easiest; We should choose the\nassumption that yields the lowest lift solution.</p>\n<p>At <a href=\"https://spor.ai/\">Spor</a> we often need to make data we do not fully\nunderstand available for exploration without rails. Data is made available by\nwriting an ontology that assigns semantics to the data. As with\nother software development, we are at liberty to only implement semantics\nwe are certain about. And so we do. We choose the assumption that data we\ndo not fully understand is not important. As soon as that assumption is\nconflicted, we have new knowledge and use that to direct our efforts.</p>\n<p>At another instances I was implementing a <em>minimum viable product</em> for a client.\nThe platform was serving customers and a default assumption is that we need a\nsign-up flow for new users. I refrained from\nimplementing it as the easiest assumption was that new users did not do self\nsign-up. To this day, the platform does not allow people to enter a sign-up\nflow.</p>\n<p>Working this way has the bi-effect of recognizing that there\nare things in life that is more important than re-implementing the same feature\nover and over again. Like spending time with friends and family.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/blockchain-product-toolbox/","relativePath":"blockchain-product-toolbox.md","relativeDir":"","base":"blockchain-product-toolbox.md","name":"blockchain-product-toolbox","frontmatter":{"title":"The Blockchain Product Toolbox","author":"Mads Buch","date":"2022-09-21","template":"post","excerpt":"This article is my attempt to collect my thoughts and the use of blockchain and smart contracts for product development."},"html":"<p>Blockchain technology is unfolding for our eyes. I work with some bright people\nand we actively use blockchains as a part of our product offerings which has prompted\nthoughts. This article is my attempt to collect my thoughts and the use of\nblockchain and smart contracts for product development.</p>\n<p>I am also trying to refrain from making puristic comments in this article.\nThere are a lot of critiques being circulated about blockchain technologies,\nand mostly well founded. But like with all other technologies\nit will take decades to reach maturity and adoption.</p>\n<h2>Extending the Product Toolbox</h2>\n<p>First, we bought products in a single transaction -- a coin for an item.\nThis has developed into simple relations governed by subscriptions, retainers,\nand such. Now we are developing the blockchain economies that\nwill let us build even more fine-grained customer relations into the products.\nas a consequence, blockchains are becoming more than just a mechanism to\nmake consensus on state among disparate parties. It is representing a new\nparadigm for products.</p>\n<p><strong>Payment infrastructure:</strong> an obvious use case for blockchain technologies\nis payment infrastructure. The current downside is that it requires a bit of\nonboarding to let people pay with tokenized currencies, and depending on\nthe market segment for the product it might or might not be wise.</p>\n<p>A feasible flow would be to let users pay with a stable coin. Currently, some\nof the popular ones are USDC and USDT.</p>\n<p>This would be as an alternative to setup an agreement with a more traditional\npayment provider such as Stripe or one of the others. The value proposition\nis that there is no 3rd party risk -- the business will not rely on the payment\nprovider's service, neither technically nor exposing them to other types of\nservice disruptions.</p>\n<p><strong>The Membership NFT:</strong>\nTraditionally we have created application users by asking for email and password.\nA new tool in the toolbox is the membership NFT.\nThe idea goes as follows: The user either mints a membership NFT\nor buys one from the secondary market. When the user connects to the app using\nthe wallet that also holds the NFT token, she will get privileged access by\nthe token she holds.</p>\n<p>The value proposition of this is to allow users to retain value for their\nmembership. This also introduces an <em>alignment of incentives</em>. The user has\nan incentive to make the service provider better. This is a positive incentive\nto post truthful reviews, provide valuable and actionable product feedback,\nand take the time to do so.</p>\n<p><strong>The Application Token:</strong>\nA product can have its currency. The great thing about this is that it can\nbe programmed to cater to the need we have. Do we need a subscription at 10 cents\na day? No biggie, we just automatically make the currency reduce by 10 cents\na day.</p>\n<p><strong>The subscription model:</strong> combine the membership NFT and the application\ntoken to build a subscription service. Instead of charging the customer's\ncredit card each month, the holding of the NFT owner deducts in real-time.\nWhen it reaches 0, then it is time to top up the account. The user can\nbuy more application tokens either on a secondary market or directly from\nthe service provider to instantiate the account and get access to her\nwork.</p>\n<h2>Final Remarks</h2>\n<p>Architecting software for the above solutions would probably benefit from\nupgradable contracts which, on the other hand, limits the decentralized news\nof such a product. This is not a problem per se as the solutions sketched\nout our product add-ons.</p>\n<p>This is contrasted to open protocols where trust is central to the value\nproposition of putting things on-chain. Creating those requires another\ntype of diligence and has other worries.</p>\n<p>One of the main features of putting product features on-chain is integration\ninto the existing blockchain ecosystem. This includes letting users trade their\ntokens and NFT assets on DEXes, taking payment in the form of other\nblockchain-represented currencies, etc.</p>\n<p>For closed product features the inherent value is less than with open\nprotocols, as the trust and transparency for the code are both of lower value\nand usually circumvented. Whether the code runs decentralized or on a\ncentralized infrastructure does not make a big difference when the code can\nbe upgraded.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/building-a-scraper/","relativePath":"building-a-scraper.md","relativeDir":"","base":"building-a-scraper.md","name":"building-a-scraper","frontmatter":{"title":"Building a Scraper","author":"Mads Buch","date":"2020-12-24","template":"post","excerpt":"Scrapers are integral to data intensive applications. They span real development projects, and, as such, there are key architectural decisions to make."},"html":"<p>This is a few notes on building a scraping pipeline. A scraping pipeline is\nan application that takes data from a website and spits it out in a format\nsuitable for another project.</p>\n<p>These thoughts are based on a current project where I need to scrape various\nwebsites and build a triple store. And example is the website ug.dk that holds\nDanish educations, institutions, jobs, campuses etc. I have build a setup that\ncan create an updated triple store with fallback on old data for resources that\nis not available.</p>\n<p>To build this scraper, there are some core principles that has lead decision\nmaking.</p>\n<ol>\n<li>Take care of the host. Don't needlessly burden the site that provide data</li>\n<li>Interruption, resources might not always be available on the internet</li>\n<li>Fun, it should be fun to develop and I want to reduce iteration time.</li>\n</ol>\n<h1>Think incremental Scraping</h1>\n<p>Get a full minimal pipeline up running ASAP. From there it is a matter of\nenriching data and incrementally enhance the pipeline.</p>\n<p>Check both the code <em>and</em> the data into git. When a full version of the scraper\nis done, and data is scraped, commit the resulting data. Diffs can be used to\nsee what data has changed since last scrape or it can be used to revert data\nback when introducing bugs in the pipeline. For that reason, save data in\na human readable format eg. JSON. Make sure that serializer saves it in a\nprettified format to make beautiful diffs.</p>\n<p>Find a good tradeoff between saving entities as individual files or bundling\nthem in individual files. GitHub handles files up to 100MB, on the other hand,\nhaving several hundred thousands files can be less practical.</p>\n<h1>Separate data fetch and processing</h1>\n<p>Make a script to save data to the disk in a format that closely mimics the\nformat from the source website. Make sure that runs are non destructive,\nenriching / updating existing entities instead of destroying existing data\nand re-fetching.</p>\n<p>Make another script that takes data saved from the website and transforming\nit into the target format. Let the scraper fetch files while working on this.\nWork on this script on a subset of the source files to ensure fast iteration\ntimes.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/data-ai/","relativePath":"data-ai.md","relativeDir":"","base":"data-ai.md","name":"data-ai","frontmatter":{"title":"Data-Driven AI","subtitle":"On the ideal AI development platform","img_path":"images/data.png","template":"page"},"html":"<p>This document is a working document elaborating on a system to employ\nartificial intelligence (AI) generally. The system is based on a layered network approach\nwith an SDK for ingesting new data and an interface to interact and setup the\nAI. In the end, the goal of the system is to:</p>\n<ol>\n<li>Provide AI functionality such as decision-making and recommendations with\ncontextual awareness.</li>\n<li>Easily add new knowledge to the system</li>\n<li>Inspect the system to verify its decisions (XAI, Explainable AI).</li>\n</ol>\n<p>The system works on a number of premises </p>\n<ol>\n<li><em>Context</em> is essential to make informed decisions.</li>\n<li>Humans are efficient at inference for complex structures; the computer\namplifies and leverages that.</li>\n</ol>\n<p>The problem space is characterized by the article on <a href=\"/ai-tools\">AI Tools</a> and\nthe following case studies.</p>\n<h1>Case Studies</h1>\n<p>A number of case studies show the potential for the technology.</p>\n<h2>Detect Identity Theft</h2>\n<p>Identity theft is becoming a larger and larger problem as we move more\nfunctionality to the web. To counter this, we employ various techniques to\nsafeguard systems against identity theft and techniques to detect identity theft.</p>\n<p>The domain is highly complex to make automated solutions to, as most\nfeatures that decide whether a loan application is related to identity theft\nare not in the specific loan application itself. That is, we can not see from\nthe requested amount, the name, or other features that we deal with fraud.\nWe need to find features that decide fraud outside of the application itself.</p>\n<p>Hence, a system to support the task of constantly being able to accommodate\nnew threats should possess a number of properties:</p>\n<ol>\n<li>It should be easy for a human user to understand what the system knows.</li>\n<li>\n<p>It should be easy for a human user to teach the system something new. This\nincludes</p>\n<ul>\n<li>providing new knowledge to the system from the surrounding world, ie.\ningest data.</li>\n<li>processing existing knowledge into abstracted knowledge like coupling\nrelated emails in <a href=\"/fraud-and-networks\">Fraud and Networks</a>.</li>\n</ul>\n</li>\n</ol>\n<p>In particular, the abstracted knowledge is the important part. It is here\nwe encode what \"fraud\" means. Abstracted knowledge could be an entity called\n<em>SuspiciousEmployment</em> which is an entity that is related to a Person that\nis related to an Employment, where the employment is related to a company\nwithout any activities.</p>\n<p>Lastly, the system should be able to uncover this abstracted knowledge whenever\nnew data is provided. This both means when a batch of existing applications are\nput in and when just a single loan for a live check is provided. This is the\nservice loan providers integrate against.</p>\n<h1>Users Documentation</h1>\n<p>The system connects the work between many different stakeholder: Domain experts,\ninfrastructure specialists, etc. To make sure this is done properly we follow\na run book defined in following chapters.</p>\n<p><strong>Part 0: Working with an AI Platform</strong></p>\n<ol>\n<li>Interaction between developers and domain experts</li>\n</ol>\n<p><strong>Part 1: Analysts Book</strong></p>\n<ol>\n<li><a href=\"/analyst-book-exploring-data\">Exploring Data</a></li>\n<li>Writing Rules</li>\n</ol>\n<p><strong>Part 1: Developers Book</strong></p>\n<ol>\n<li><a href=\"/ingesting-data\">Ingesting data</a></li>\n<li>Complex data abstractions</li>\n</ol>\n<h1>Technical Overview</h1>\n<p>The short version is that the system works by juxtaposing networks. These\nnetworks represents the real world, they are also known as <em>knowledge graph</em>,\nthey can represent derivatives of the real world known as layers, or they can\nrepresent decisions, actions, etc.</p>\n<p>This provides an AI platform as laid out in\n<a href=\"/explainable-ai-with-layered-networks\">Explainable AI with Layered Networks</a>.</p>\n<h2>Architecture</h2>\n<p>The system contains following components:</p>\n<ol>\n<li>Ingestors</li>\n<li>The AI engine</li>\n<li>Applications / services.</li>\n</ol>\n<p>Ingestors take information from the surrounding world and makes it available\nto the system. The engine takes several files representing ingested data and\nintegrates it and abstracts it. Lastly the applications and services make it\navailable for actual use.</p>\n<h2>Ingestors - Creating the Knowledge Graph</h2>\n<p>Instead of creating one knowledge graph to rule them all, we create several\nknowledge graphs and maintain them individually. In order to have the highest\ndata leverage it needs to be dead simple to ingest new data.</p>\n<p>A knowledge graph has 2 components: a triple store and an ontology. The triple\nstore has the actual information while the ontology is and abstract\nunderstanding of this data.</p>\n<h2>Application</h2>\n<p>The application layer considers the interaction with other systems. Currently\nwe are developing an <em>Explorer</em> and an <em>API</em> layer for this.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/data-platform/","relativePath":"data-platform.md","relativeDir":"","base":"data-platform.md","name":"data-platform","frontmatter":{"title":"The Data Platform","author":"Mads Buch","date":"2021-03-27","template":"post","excerpt":"We need good data platforms to ingest, process, and analyze data."},"html":"<p><em>Main point: We need good data platforms to ingest, process, and analyze data.</em></p>\n<p>Understanding our surroundings is an art. Good artists need good tools,\nnot making art is not possible without good tools, but because good tools\nenable expression.</p>\n<p>The same is the case for data science. We need a good data platform. It should\nhelp us separate concerns. It should assist in and assure the quality of the\ndeliverables, and it should let people focus on their work and not too many\nother things.</p>\n<p>But what concerns are central to understanding data? The first concern is\ntaking the data from the source and process it into a format we can use.\nThis is called ingestion. Next is the intermediary representation. This acts\nas an indirection and lets us disregard the data source. this will integrate\ndata so we can work on data across data sets and wit will apply anonymity for\ndatasets where it is needed. Lastly, we need a product. This is the concern of\neither understanding the data, getting models that are trained and can be used\nin applications, or something third.</p>\n<p><strong>Ingestion:</strong>\nThe first step is to ingest data into the system. This consists of figuring out\nhow the data should be modeled and build the data package. Concerns here are\nin particular data hygiene and local integrity.</p>\n<p>An ideal data platform should give the fullest freedom to express the model.\nIe. the engineer should not be made to fit the data on an existing model.\nFurthermore, the data platform should use the <em>ontology</em> to assist with\ndata hygiene.</p>\n<p>We might even want to work with data under different ontologies. The data\nplatform should enable the ability for the user to handle multiple\ningestions of the same data modeled slightly differently to accommodate different\nneeds.</p>\n<p><strong>Internal Representation:</strong>\nThe internal representation should provide an indirection to the source.\nit should with as little effort as possible make sure that identical\nentities are resolved to the same or at least make sure that reasonable\nrelations are setup. In particular, this is a place to keep track of\n<em>provenance</em> ie. where data comes from and what intermediary steps it has been\nthrough.</p>\n<p>This is also the place to apply <em>differential privacy</em> to the data in need.\nThis ensures that the analysts can work with the data free of  friction and release\nthe results they see without further redaction. This is highly important\nwhen training statistical models such as neural networks in personally\nidentifiable data as there is a risk to covertly release sensitive data\nin the model.</p>\n<p><strong>Analysis:</strong>\nThe last part is where the data is utilized to create value. This involves\ntooling to quickly get an intuitive sense of the data, the ontology,\nhow data interrelates, what data is available, and the quality of the data.</p>\n<p>The data scientist would need tools for extracting the data for their needs.\nThis could be exemplars to train models on etc.</p>\n<p>At <a href=\"https://spor.ai\">Spor</a> we are trying to solve some of these problems.\nIn particular, we have devised methods for resolving data from disparate\ndatasets while still retaining the ability to reason about the data locally.\nWe are developing everything to work in the browser but scale to the cluster.\nWhere data resides is important. Especially when you want to work on private\ndata such as emails or personal notes.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/deorganize-yourself/","relativePath":"deorganize-yourself.md","relativeDir":"","base":"deorganize-yourself.md","name":"deorganize-yourself","frontmatter":{"title":"Deorganize Yourself","author":"Mads Buch","date":"2021-01-12","template":"post","excerpt":"We often don't need that much organization. Organizational structures should be afterthoughts."},"html":"<p><em>Main point: We often don't need that much organization. Organizational structures should be afterthoughts.</em></p>\n<p>Have you ever taken yourself in building intricate folder structures for data\nthat doesn't exist yet? And then found yourself in doubt on where to put a\nfile next time you need to add one? Or maybe you have worked on a project and\nthen, months later, you need to reenter the project structure just to find that\nyou intuition on the subjects has changed enough that the old organization does\nnot make sense anymore.</p>\n<p>Well, I am confident that it is normal and we all do it. Cleaning up is nice.\nShowing underwear in the underwear drawer also cleans out our inner mess. but\nwith most intellectual thoughts it is not so easy to put them in single boxes.\nThey usually fit into intricate networks where a thought, or all kinds of\nintellectual work, can be reached from several thought fibers.</p>\n<p>Hence, instead of organizing work into conceptual boxes, it makes sense to\nthink about rediscovery and explorability.</p>\n<p>Most operating systems are already now attempting to banish folder structures\nin favor of search mechanisms. For many it is more normal to press the\nwindows button and just search for the program or file they want\n(nostalgia alert: In my youth we had windows 98 with only nested menus with\nprograms and folders with files).</p>\n<p>Writing my own notes and my journal I basically just have all files in one big\nfolder with text files. I can search in the file-content using VS Code. I\ncan search for specific filenames and I can link files together using tags.\nHere I have 2 means of information retrieval: The exploitative where I\nremember exact strings of what I need and the more\ncontextual / explorative where I find the material based on it\nbeing \"something along the lines of ...\".</p>\n<p>I find it interesting to explore what the best tools are to explore information\nwithout knowing what I want. Currently I do that in <a href=\"https://spor.ai\">Spor</a>.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/data/","relativePath":"data.md","relativeDir":"","base":"data.md","name":"data","frontmatter":{"title":"Understanding Data","subtitle":"On the craft and art of understanding data","img_path":"images/data.png","template":"page","indexOrder":3},"html":"<!--\n\nAlright, saa hvad er pointen med denne side?\n\n1. Samle alle mine artikler om data engineering.\n2. \n\n-->\n<p>This page collects my thoughts on the theme of data science and engineering.\nData science is multi-disciplinary. Ranging from philosophical and theoretical\ntopics of ontology to practical considerations about scaling a platform to meet\nrequirements.</p>\n<p>This page is evolving. It takes my individual posts on the subjects and aggregates\nit into a coherent whole. A whole that often changes shape.</p>\n<h1>Data Characterization</h1>\n<p>We can consider everything stored on a computer data. Everything that can be\nrepresented binary. Programs, configuration files, images, sound, documents,\netc. In essence everything on a computer is data and might constitute insights\nused to solve a problem.</p>\n<p>One dimension of data characterization is the signal/noise ratio. The ration is\nsaid t be high, ie. high signal low noise, when small amounts of data is valuable\nwhile noisy data has many <em>bits</em> that does not provide data.</p>\n<p>canonical examples of high noise data would be sound and images, while a\nsemantic triple, usually, has a high signal.</p>\n<p>AI applications mostly consider high signal data. It should be noted, that the\nsignal can be increased. This is the task og machine learning. Ie. instance\nrecognition in images <em>reduces</em> noise by constructing triples. This triple\ncould be <code>image.png depicts a chair</code>.</p>\n<h1>Data Interpretation</h1>\n<p>Depending on the nature of the data there are several ways to interpret data.\nSometimes statistical aggregation suffices, other times we employ an\nexplorative approach to assess the potential.</p>\n<p>As for statistical methods, the most widely used are probably those available\nin spreadsheets. However, more generally <a href=\"/the-probability-monad/\">the Probability Monad</a>\nprovides tooling for writing these distributions as programs.</p>\n<p>Other techniques are those based on networks. Modeling data as networks\nformatted under an <em>ontology</em> can be advantageous as it lays out clear\nsemantics of the data. It clearly specifies what classes entities in the data\nbelong to and how they relate to other entities.</p>\n<p>These techniques are being used in fraud detection. <a href=\"/fraud-and-networks/\">Fraud and Networks</a>\nis an article that touches on some of the aspects of doing that. Furthermore,\nthe network approach can also be used to increase trust in statistical results.\nI investigate some of the opportunities in <a href=\"/adversarial-statistics/\">adversarial Statistics</a>.</p>\n<h1>Data Processing</h1>\n<p>Data processing concerns how to process large amounts of data. But more than\nthat is als concerns how to reuse existing assets. This is essential as it\nallows fore serendipity and finding patterns across different data sets.\nTo do this an organization can employ a data platform. A common place to put\ndata assets for cross leverage between use cases.</p>\n<p>Furthermore, an integral problem to data processing is scale. Data often comes\nin large amounts. For this, both the storage and processing architectures need\nto scale. Scaling can only to an extend be separated from the data and processing\ntechniques and is a serious concern when thinking up data processing architectures.</p>\n<h2>The Data Platform</h2>\n<p>For practical purposes the data platform is essential. Not only does it reduce\ntime when changing analysis etc. It also provides the needed foundation to\nscale data analysis to span appropriate data sets.</p>\n<ul>\n<li><a href=\"/data-platform\">The data platform</a>: A deeper walk-through on desirable\nproperties of a data platform.</li>\n</ul>\n<p>One part is about processing and ingesting data.\nThis could be loading data into data frames, scraping websites,\nutilizing APIs, but also using techniques such as entity recognition and\nlinking for extracting information from text, object recognition to extract\ninformation from image material, and speech recognition in conjunction with\nNLP techniques to get data out of auditory resources. The problems in this\nlevel are very engineering-specific. It is about keeping code up to date, etc.</p>\n<ul>\n<li><a href=\"/building-a-scraper/\">Building a Scraper</a>: Thoughts on the process of\nbuilding a scraper that needs to run continuously and provide new data.</li>\n</ul>\n<p>Next is processing it into an intermediary representation. This is necessary\nwhen the amounts of data and sources are considerable. This step does a few\nthings. First and foremost, it integrates data. It relates entities from\ndifferent data sources that are related. This is desirable as we do not want\nto do this for large amounts of data every time we want to ask a question.\nSecondly, it ensures scalability. For sufficiently large amounts of data and\nsufficiently complex pipelines, it is not feasible to run everything from\na single laptop (or phone). Problems here are on scale.</p>\n<p>Lastly, the production step. This step produces data for further use. This\ncould be for reports, it could be data for training models, or the actual\nmodels to integrate into an application. The data platform could also\nproduce endpoints for real-time data insight etc. Problems here are of semantic\nnature. How do we ensure the correctness of produced claims? Articles relevant\nto this are.</p>\n<h2>Service Architectures</h2>\n<p>There are a number of ways data is put to use. One way is through pieces of\nwritten works. This can be data journalists who make a piece using data they\ninterpret. It can also be assembling predictive models using machine-learned\nmodels.</p>\n<ul>\n<li><a href=\"/scalable-ml-service\">Scalable Machine Learning Service</a>: A presentation\nof an architecture for a service to expose an interface to online\nhosted machine learning models.</li>\n<li><a href=\"/productizing-ml-models\">Productizing Machine Learning Models</a>: On the\nsteps needed in order to put a machine learning model into production.</li>\n</ul>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/eip712-signatures/","relativePath":"eip712-signatures.md","relativeDir":"","base":"eip712-signatures.md","name":"eip712-signatures","frontmatter":{"title":"EIP-712 Signatures","author":"Mads Buch","date":"2022-11-14","template":"post","category":"technicalRef"},"html":"<p>Those pesky signatures. Hard to debug, hard to get right. Therefore, I have\ncreated a template repository for the next project needing</p>\n<h2>TL;DR</h2>\n<ol>\n<li>Clone the repo from <a href=\"https://github.com/madsbuch/eip712-demo\">GitHub</a></li>\n<li><code>npm install</code> to install dependencies</li>\n<li><code>npx hardhat test</code> to compile the contract and runs tests</li>\n<li>Read through the tests to udnerstand how to fit this to your use case</li>\n</ol>\n<h2>EIP-712 Signature and Solidity</h2>\n<p>Full source available on <a href=\"https://github.com/madsbuch/eip712-demo/blob/main/contracts/Gasless.sol\">GitHub</a></p>\n<p>As a demo we EIP-712 wrap a function with the following signature:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"sol\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">someFunc(address sender, address receiver, uint256 amount)</span></span></code></pre>\n<p>The core function is implemented as the <code>_someFunc</code> which is private to\nensure that is is not being called directly.</p>\n<p>The wrapper is a function with the same initial signature, and the a whole lot\nof fields more:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"sol\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">function someFuncGasless(</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  address sender,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  address recevier,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  uint256 amount,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  uint256 deadline,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  uint8 v,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  bytes32 r,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  bytes32 s</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">) external { ... }</span></span></code></pre>\n<p>Note the <em>deadline</em>, and <em>v</em>, <em>r</em>, <em>s</em>. These are the\nprotocol specific values needed to securely accept the function invokation\nfrom a third party signer. The nonce is verified implicitly via the signature.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"solidity\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">// Check deadline</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">require(deadline &gt;= block.timestamp, &quot;EIP712: Expired&quot;);</span></span></code></pre>\n<p>We implicitly use the <code>sender</code> and the one we expect signed the message.</p>\n<p>Next happens that packing, hashing and signing. This is the tricky part of the\nprotocol.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"solidity\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">bytes32 digest = keccak256(</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    abi.encodePacked(</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        &quot;\\x19\\x01&quot;,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        DOMAIN_SEPARATOR,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        keccak256(</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">            abi.encode(</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                SOME_FUNC_TYPEHASH,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                sender,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                keccak256(abi.encodePacked(receivers)),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                amount,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                deadline,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                nonces[sender]++</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">            )</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        )</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    )</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">);</span></span></code></pre>\n<p>This is an example of a non-nested structure. It is a good starting point to\nincremenatally add more complext datatypes to it. For full reference see\nthe <a href=\"https://eips.ethereum.org/EIPS/eip-712\">EIP-712 specification</a>.</p>\n<p>Also note the <code>nonces[sender]++</code>. This is the nonce check that inline increments\nthe nonce to ensure replay attacks are not possible.</p>\n<p>Last thing to do is to check the signature and call the function body.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"solidity\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">address recoveredAddress = ecrecover(digest, v, r, s);</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">require(</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    recoveredAddress != address(0) &amp;&amp; recoveredAddress == sender,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    &quot;EIP712: Invalid signature&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">_someFunc(sender, receivers, amount);</span></span></code></pre>\n<p>And that's it!</p>\n<h2>Crafting an EIP-712 Signature Client Side</h2>\n<p>A number of implementations of this is available in the tests that are\navailable on <a href=\"https://github.com/madsbuch/eip712-demo/blob/main/test/Gasles.test.ts\">GitHub</a>.</p>\n<p>The core is to generate a signature either using a direct ETH RPC call to the\nwalllet</p>\n<pre class=\"grvsc-container default-light\" data-language=\"ts\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">signature</span><span class=\"mtk1\"> = </span><span class=\"mtk14\">await</span><span class=\"mtk1\"> </span><span class=\"mtk12\">ethers</span><span class=\"mtk1\">.</span><span class=\"mtk12\">provider</span><span class=\"mtk1\">.</span><span class=\"mtk10\">send</span><span class=\"mtk1\">(</span><span class=\"mtk17\">&quot;eth_signTypedData_v4&quot;</span><span class=\"mtk1\">, [</span><span class=\"mtk12\">message</span><span class=\"mtk1\">.</span><span class=\"mtk12\">sender</span><span class=\"mtk1\">, </span><span class=\"mtk12\">data</span><span class=\"mtk1\">])</span></span></span></code></pre>\n<p>or by calling the experimental <code>_signTypedData</code></p>\n<pre class=\"grvsc-container default-light\" data-language=\"ts\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">signature</span><span class=\"mtk1\"> = </span><span class=\"mtk14\">await</span><span class=\"mtk1\"> </span><span class=\"mtk12\">otherAccount</span><span class=\"mtk1\">.</span><span class=\"mtk10\">_signTypedData</span><span class=\"mtk1\">(</span><span class=\"mtk12\">data</span><span class=\"mtk1\">.</span><span class=\"mtk12\">domain</span><span class=\"mtk1\">, </span><span class=\"mtk12\">data</span><span class=\"mtk1\">.</span><span class=\"mtk12\">types</span><span class=\"mtk1\">, </span><span class=\"mtk12\">data</span><span class=\"mtk1\">.</span><span class=\"mtk12\">message</span><span class=\"mtk1\">)</span></span></span></code></pre>\n<p><strong>Note:</strong> These functinos have different signatures. In particular, <code>_signTypedData</code>\ndoes not expect to have the tyoe of the domain passed.</p>\n<p>The signature is split into it's parts for cheaper processing on the EVM side.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"ts\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">r</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">signature</span><span class=\"mtk1\">.</span><span class=\"mtk10\">substring</span><span class=\"mtk1\">(</span><span class=\"mtk7\">0</span><span class=\"mtk1\">, </span><span class=\"mtk7\">66</span><span class=\"mtk1\">);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">s</span><span class=\"mtk1\"> = </span><span class=\"mtk17\">&quot;0x&quot;</span><span class=\"mtk1\"> + </span><span class=\"mtk12\">signature</span><span class=\"mtk1\">.</span><span class=\"mtk10\">substring</span><span class=\"mtk1\">(</span><span class=\"mtk7\">66</span><span class=\"mtk1\">, </span><span class=\"mtk7\">130</span><span class=\"mtk1\">);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">v</span><span class=\"mtk1\"> = </span><span class=\"mtk10\">parseInt</span><span class=\"mtk1\">(</span><span class=\"mtk12\">signature</span><span class=\"mtk1\">.</span><span class=\"mtk10\">substring</span><span class=\"mtk1\">(</span><span class=\"mtk7\">130</span><span class=\"mtk1\">, </span><span class=\"mtk7\">132</span><span class=\"mtk1\">), </span><span class=\"mtk7\">16</span><span class=\"mtk1\">);</span></span></span></code></pre>\n<p>And lastly the contract is being called by</p>\n<pre class=\"grvsc-container default-light\" data-language=\"ts\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk12\">gaslessContract</span><span class=\"mtk1\">.</span><span class=\"mtk10\">someFuncGasless</span><span class=\"mtk1\">(</span><span class=\"mtk12\">message</span><span class=\"mtk1\">.</span><span class=\"mtk12\">sender</span><span class=\"mtk1\">, </span><span class=\"mtk12\">message</span><span class=\"mtk1\">.</span><span class=\"mtk12\">receivers</span><span class=\"mtk1\">, </span><span class=\"mtk12\">message</span><span class=\"mtk1\">.</span><span class=\"mtk12\">amount</span><span class=\"mtk1\">, </span><span class=\"mtk12\">message</span><span class=\"mtk1\">.</span><span class=\"mtk12\">deadline</span><span class=\"mtk1\">, </span><span class=\"mtk12\">v</span><span class=\"mtk1\">, </span><span class=\"mtk12\">r</span><span class=\"mtk1\">, </span><span class=\"mtk12\">s</span><span class=\"mtk1\">)</span></span></span></code></pre>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .mtk4 { color: #0000FF; }\n  .default-light .mtk1 { color: #000000; }\n  .default-light .mtk12 { color: #001080; }\n  .default-light .mtk14 { color: #AF00DB; }\n  .default-light .mtk10 { color: #795E26; }\n  .default-light .mtk17 { color: #A31515; }\n  .default-light .mtk7 { color: #09885A; }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/explainable-ai-with-layered-networks/","relativePath":"explainable-ai-with-layered-networks.md","relativeDir":"","base":"explainable-ai-with-layered-networks.md","name":"explainable-ai-with-layered-networks","frontmatter":{"title":"Explainable AI with Layered Networks","author":"Mads Buch","date":"2021-05-21","template":"post","excerpt":"Explainable AI is the hype! Or maybe not so much. But depending on the use case the AI has to be explainable."},"html":"<p>Explainable AI is the hype! Or maybe not so much. But depending on the use case\nthe AI has to be explainable. Imagine if your loan broker rejected you without\nproper reason and you would have to move out of your house, or if the insurance\npremium were to be set by a black box with no real way to know what effects\nthe resulting premium. Hence the need for explainable AI systems.</p>\n<p>What is a system that provides explainable AI? It is a system that supports\ntheir decisions with compelling <em>arguments</em>. In essence, the system should\nbe able to spit out a report that presents the <em>proofs</em>. These proofs should\nbe <em>verifiable</em> without access to the AI system. But how do we create\nsuch a system?</p>\n<p>One way to create explainable AI systems is to create strong isolation between\ndifferent types of knowledge. Some knowledge is of factual nature, some\nknowledge is objects recognized in image material / sound etc. Some knowledge\nis based on trustworthy sources while others are highly circumstantial.\nA way to create this division is by using multi-layer networks.\nA layer is merely a network graph constructed in a way such that it is possible\nto consolidate individual layers and traverse the multilayered graph.</p>\n<p>Each layer has a single knowledge responsibility, such a responsibility could\nbe information about who owns which companies, or what landmarks are present\nin pictures. Furthermore, each layer includes information about provenance,\nconfidence, verification steps, etc.</p>\n<p>For the example about a layer that holds responsibility about the company ownership, we\nconstruct the lay by reading the relevant company register. We note that\nwe have high confidence in the correctness of the data represented (this\nmight be different for different jurisdictions). The verification step\nwould be to log in to the registry and very the stated ownership.</p>\n<p>For the example about landmarks present in pictures, we can construct the\nlayer by using image recognition techniques. Ie. we would contain the use of\nblack-box techniques (eg. neural networks) to <em>that layer only</em>.\nProvenance would include information about the source image,\nconfidence would be low to medium depending on the evaluation metrics of the\nalgorithm in use, and verification steps would include visual inspection of\nthe source image.</p>\n<p>When combining these layers we can do complex reasoning. We can make chains\nlike\n<code>Picture P features Landmark L</code>, <code>Landmark L has address A</code>,\n<code>picture P was taken at time T</code>, <code>Picture P was shared by Person G</code> to support\nthe conclusion that a person was present at a specific place and time.\nThis <em>syllogism</em> is verified manually such that:</p>\n<ol>\n<li>It is visually inspected that the picture indeed features the proposed landmark</li>\n<li>The landmark is checked to have the address</li>\n<li>It is checked that the proposed time of the picture being taken works out</li>\n<li>It is checked that the picture was indeed shared by <em>Person G</em>, and that it is\npossible that he was present when sharing.</li>\n</ol>\n<p>The next step would be to add <em>compound layers</em> which is a product of existing\nlayers. Besides enabling more complex reasoning for the system, they would\nalso inherit provenance, confidence, and verification from previous layers.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/elixir-extended-context-framework/","relativePath":"elixir-extended-context-framework.md","relativeDir":"","base":"elixir-extended-context-framework.md","name":"elixir-extended-context-framework","frontmatter":{"title":"The Extended Context Framework","author":"Mads Buch","date":"2022-11-01","template":"post"},"html":"<p>I have done several projects in Elixir with great teams. In the beginning,\nit is usually just about writing some code. But as more and more developers\nget onboard and more and more code is churned, erhm refactored,\nthe architecture becomes increasingly important.</p>\n<p>We tend to start with a single Phoenix app. We then see that modules start\nto grow to unbearable sizes. We need to implement various supporting services\nand internal subscribers to queues, but it is not apparent where these go. The\nnext natural step would be to employ something like Umbrella and\nmanage it as multiple microservices. However, that is one heavy beast and makes\nit harder to reuse components.</p>\n<p>The <em>extended contexts framework</em> came to be in collaboration with Aleksander\nRendtslev. We relax the ownership of the persistency layer and the web layer\nto support velocity and as a pragmatic trade-off.</p>\n<h2>Code Bases for High-Velocity Teams</h2>\n<p>We use this framework with high-velocity teams. This means a couple of things.\nFirstly, code is never staged. We trust that when the code passes tests,\nis reviewed, and is approved by the developer, it is ready for production.\nSecondly, we deploy the production environment several times a day.\nLastly, our time is prioritized towards developing software.\nIn particular, we avoid spending too much time on low-level technical\ndiscussions and trust that each individual asks if there are any questions.</p>\n<p>This approach puts particular strains on a codebase and the team:</p>\n<ol>\n<li>Consensus is hard. It takes time and reduces velocity. The codebase should\nwork with a minimal amount of consensus and support divergent understandings\nof the product we develop.</li>\n<li>There will be dead code. When pivoting features with hard deadlines it is not\nfeasible to expect complete cleanup. When dead code is left behind the code\nbase should not start to look like trash.</li>\n<li>Implementation complexity is a constant factor of the codebase.\nThis means that it takes the same amount of time today as in two years to\nimplement a feature.</li>\n</ol>\n<h2>The Extended Context Framework</h2>\n<p>The extended context framework is a tradeoff between the Umbrella-type\nmicroservice infrastructure and a single Phoenix app. We still divide code\ninto semantic chunks but build them in a way that highly supports horizontal\nscaling. The folder structure of aa implementation looks like the following.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"txt\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">/persistency</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    schemas/user.ex</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    repo.ex</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">/web</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    /controller</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    /graphql</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">/context_one</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    /actions</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        perform_some_actions.ex</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    /jobs</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        internal_subscriber.ex</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    /service</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        external_service.ex</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    /logic</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        parse_some_thing.ex</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        validate_something_else.ex</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    /workflows</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        place_reservation.ex</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    module.ex</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">/context_two</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    ...</span></span></code></pre>\n<p>On the top level, we share the persistency layer and the web layer between the contexts.\nWe do this as they tend to be very hard to divide into semantic categories –\nas a developer we often need that blog-posts object even though we are working on the discord context.\nThe same is the case with the web interface.\nThey are very shallow and would introduce unnecessary noise in the context.</p>\n<h2>The Context</h2>\n<p>A context collects functionality regarding a single semantic domain for the\napplication. Because of the context's construction, we prefer fat contexts.\nThe context consists of the following parts:</p>\n<ul>\n<li><strong>Actions:</strong> The actions do a single thing and never fetched data. These\nshould be thought of as mutations to the application state with a semantics\nlayer.</li>\n<li><strong>Jobs:</strong> Jobs set up listeners, services, cron jobs, etc.\nThey <em>dispatch</em> everything to workflows why they are slim.</li>\n<li><strong>Services:</strong> Very slim and mockable wrappers for external services. They\nimplement behaviors to allow testing and mocks to allow disabling a service\nin non-production environments.</li>\n<li><strong>Logic:</strong> Functions without state changes. Ideally, I would want to call\nthese pure functions, but for pragmatic reasons, they are able\nto call date and randomness functions.</li>\n<li><strong>Workflows:</strong> Workflows should be seen as entry points to the code base.\nThey are responsible for fetching data, mutating using actions and\ndispatching external effects using the sevices. They are called from the\ninterface and from internal and external subscribers.</li>\n<li><strong>The <code>module.ex</code> file:</strong> This file is the context external interface. This\nfile should merely contain <code>defdelegate</code>'s to the code in the others files.\nIn particular, functions that should not be exposed outside of\nthe context, should not be added to this file.</li>\n</ul>\n<h2>Principles</h2>\n<p><strong>Data fecthing:</strong> We prefer to fetch data as far up in the hierarchy as\npoassible. Some data is provided on the outer layer, that is in resolvers\n/ controllers / listeners etc. other data is fetched in the workflow.\nWe do this to avoid loading data mutliple places.</p>\n<h2>Code Changes</h2>\n<p>As developers, we spend a lot of time changing and tweaking code.\nIt is said that developers spend time \"reading code\".\nBut for most commercial projects reading the code is boring, and is an activity\nwe want to reduce. Instead, we focus on optimizing for changing the code.\nThis involves the following:</p>\n<ol>\n<li>Identify what part(s) of the code needs to be changed</li>\n<li>Identify potential side effects of this change</li>\n<li>Implement the change</li>\n<li>Verify that everything still works as we expect</li>\n</ol>\n<p>To identify what parts of the code should be changed we investigate from a\nknown endpoint. Indifferent to have painful this sounds it\nis usually the case for developers of commercial software.\nTo assist this process we architecture around two principles: 1) Reduce\nindirections and 2) Contain branching.</p>\n<p>This is where the <em>workflows</em> come in. They act as the entry point and\nare <em>directly</em> called from resolvers and controllers.</p>\n<p>Reading the workflow allows us to concisely identify the side effects of the change.\nIf the workflow uses another workflow, we need to expand our analysis to that\nworkflow also.</p>\n<p>We can then implement the change. This requires a couple of things:\nWe alter existing code, we add new code, or we do both. In a traditional\none-file-for-a-module approach we tend to have ever-expanding files. The\ncanonical answer to this is that \"if a module becomes too big, it is two modules\".\nHowever, in high-velocity environments, this is not feasible.\nInstead, we use a \"one file, one responsibility\" approach. This ensures that\nno single file needs to be split as they usually stay very small. The tradeoff\nis that we risk getting a lot of files, though this does not appear to be\nthe case in the real world.</p>\n<p>Lastly, we ensure that the changes do not introduce regressions and that\nthe new features are implemented correctly. We do this by testing. For tests,\nwe mirror the application folder hierarchy. This makes it easy for us to\nunderstand the test coverage at a glance. When a file is missing a test file,\nit is easy to create one and implement it with a single simple test. Are\ntests failing after the change, then either the test of the implementation is\ncorrected.</p>\n<h2>Final Thoughts</h2>\n<p><strong>Versatility:</strong> As pointed out by my colleague Matti, this architecture is\nnot bound to Elixir. It can be used with most languages. The main point to\nwhere this fits, is the high-velocity team settings.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/forgetting/","relativePath":"forgetting.md","relativeDir":"","base":"forgetting.md","name":"forgetting","frontmatter":{"title":"Remember to Forget","author":"Mads Buch","date":"2022-01-07","template":"post","excerpt":"We need to forget to pursue a purpose"},"html":"<p>Developing Minimum Viable Products is some of the most rewarding development work out there. We create hugely impactful projects and let inspiration and aspiration flow through our veins. In the heat of the moment, we remember to write everything down, creating vast backlogs of potential new avenues for the project.</p>\n<p>However, most of these avenues are, in the best case, indifferent to the project, and in the worst case, they become darlings we hold on to. We get an attachment and a need to put it into the project, and it gets prioritized even though the impact is not proportional to the value it brings to the project.</p>\n<p>Hence it is important to forget. To boldly erase stuff or throw the material as far away as is not possible to retrieve it again easily.</p>\n<p>The truth is that when customers are brought on to the project, plenty of people will have plenty of ideas worth pursuing. And definitely, ideas that are better and more aligned with the commercial raison d’etre of the project.</p>\n<p>Forgetting is also the often forgotten but essential aspect of learning. We need to lose focus and get rid of details detrimental to progress, whatever progress means in a given setting. We need to forget to pursue a purpose.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/fraud-and-networks/","relativePath":"fraud-and-networks.md","relativeDir":"","base":"fraud-and-networks.md","name":"fraud-and-networks","frontmatter":{"title":"Fraud and Networks","author":"Mads Buch","subtitle":"A case study on detecting identity theft in lending applications","excerpt":"The Case: As a financial institution we want to avoid fraud. Fraud is a broad term, so in this case, we focus specifically on identity theft. Ie. We want to be able to quickly detect oddities signaling that someone's identity is being used illegally.","date":"2021-03-18","template":"post"},"html":"<p>The Case: As a financial institution we want to avoid fraud. Fraud is a broad\nterm, so in this case, we focus specifically on identity theft. Ie.\nWe want to be able to quickly detect oddities signaling that someone's identity is\nbeing used illegally.\nFor this case study, we have discovered a fraud vector where applicants use\nsimilar email addresses. We want to add this check to our existing system\nwith minimal effort <em>and</em> we want to be able to let it utilize all data we\nhave.</p>\n<hr>\n<p>Fraud is interesting. Not only because we as societies lose billions of\ndollars a year, but also because fraud finds its way through the cracks.\nBoth characterization and techniques of fraud change as fast as it is profitable\nand that is fast in a globally connected world. But this also sets the bar\nhigh for the system to detect and flag fraud. It should always be able to\nembrace new patterns while not decreasing in performance. In this case study, we\nlook at network technologies and how we can horizontally expand on the\ntechniques. We also see how we implement these techniques in a way that allow\nfor horizontal performance scaling.</p>\n<p>First the data. In this example, we look at lending applications. We do not\naugment with data from other data sources although this can be added in\ntransparently later and augment the results.\nAn example of the applications could be as follows:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">CaseId,   Email,                Amount, Name,   Birth Date, Application Time</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">case007,  tanjatt@example.com,  5000,   Tanja,  1992-05-05, 2021-03-07 12:12:34</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">case008,  astriddd@example.com, 150000, Astrid, 1985-02-09, 2021-03-08 12:12:34</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">case009,  gert1970@example.com, 50000,  Gertrud,1970-01-01, 2021-03-09 12:44:54</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">case010,  gert1971@example.com, 50000,  Gertrud,1971-01-01, 2021-03-10 12:54:53</span></span></code></pre>\n<p>This yields a network where the nodes are either an <em>Application</em> that has\na case ID, an amount, and the time of application.\nThese case IDs are linked to <em>Applicants</em>. An Applicant has a name, a birth\ndate, </p>\n<p><img src=\"/images/fraud-net-1.png\" alt=\"fraud-net-1.png\"></p>\n<p>Next, we add in the email address layer. Normally emails would simply be\na data field on the entities, but to detect similar emails, we can\npull them out and build a new dedicated network that can be juxtaposed\nwith the old one.</p>\n<p>The email network is constructed such that similar email addresses are\nconnected. We say that email addresses are similar when their\n<a href=\"https://en.wikipedia.org/wiki/Levenshtein_distance\">Levenshtein distance</a> is\nat most 2. This means that two emails are considered similar when only 2 edits\nare needed to make them identical.</p>\n<p><img src=\"/images/fraud-net-2.png\" alt=\"fraud-net-2.png\"></p>\n<p>The email network and the lending applications network are connected such that\napplicants have a link to the email they possess. Now we can go from applicants\nto their respective email, go to a similar email and back to applicants again.</p>\n<p>To efficiently use this <em>product</em> network we use a random walk technique. From\na given entity, say <em>case015</em> we take a random link out. We keep navigating\naround in the network randomly until we find a new entity of a specified type.\nIn this case Application. By doing this thousand, or hundred of thousands of\ntimes we can draw a picture of how cases are interlinked. The blow picture is\na result of that.</p>\n<p><img src=\"/images/fraud-net-3.png\" alt=\"fraud-net-3.png\"></p>\n<p>This picture is the result of 1000 random walks without excluding the walks\nthat never ended in an Application. As we can see, it is quite efficient at\nfinding a network of similar emails. This network will be flagged and an\nexpert will take it on to assess an appropriate action.</p>\n<p><strong>Why This Solution?</strong>\nThere are several ways to achieve this. A more obvious way would be to issue\na <code>SELECT</code> query in SQL to get all applicants with similar emails. However, this\napproach does not scale very well with new fraud vectors and constantly has to\nbe updated. The network approach is fluid to new datasets. It will simply start\ntraversing new layers when these are added. The algorithm will find fraud\npatterns with a probability proportional to the out-degree per hop. This is\nreasonable  as we can assume that entities connected in a long chain have less\nin common. If that is not the case, we can simply add in new links to\nstrengthen a connection.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/","relativePath":"index.md","relativeDir":"","base":"index.md","name":"index","frontmatter":{"title":"Mads Buch","has_more_link":true,"more_link_text":"Keep reading","template":"home"},"html":"<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/huffman-python/","relativePath":"huffman-python.md","relativeDir":"","base":"huffman-python.md","name":"huffman-python","frontmatter":{"title":"Huffman in Python","author":"Mads Buch","date":"2026-06-07","template":"post"},"html":"<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">Orig: Music to hear, why hear’st thou music sadly?</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Comp: 00100010111011111100101101110010100010100101110000111000000101101001001111111100111000101110000111000000100100011111110000101000111010101101101000100111011111100101101110010111111001011010010011100011011101</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Deco: Music to hear, why hear’st thou music sadly?</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Size uncompressed 44</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Size compressed 26</span></span></code></pre>\n<pre class=\"grvsc-container default-light\" data-language=\"python\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># This is part of my anti-atrophy tasks. As most of my professional software development</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># tasks are now is done through agentic development. This is fitness for the brain!</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">#</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Solely based on what&#39;s here: https://en.wikipedia.org/wiki/Huffman_coding</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">import</span><span class=\"mtk1\"> requests</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">import</span><span class=\"mtk1\"> pprint</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">import</span><span class=\"mtk1\"> heapq</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">import</span><span class=\"mtk1\"> math</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Fetch the file, this is our corpus</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">response = requests.get(</span><span class=\"mtk17\">&quot;https://www.gutenberg.org/cache/epub/100/pg100.txt&quot;</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">text = response.text</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Calculate frequencies</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">freqs = {}</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">count = </span><span class=\"mtk7\">0</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">for</span><span class=\"mtk1\"> c </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> text:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\"> </span><span class=\"mtk4\">not</span><span class=\"mtk1\"> c </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> freqs:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        freqs[c] = </span><span class=\"mtk7\">0</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    freqs[c] += </span><span class=\"mtk7\">1</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    count += </span><span class=\"mtk7\">1</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Normalize</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">for</span><span class=\"mtk1\"> k </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> freqs:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    freqs[k] = freqs[k] / count</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># How priority queues aka head queues work</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">p1 = []</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># heapq.heapify(p1) - No need to run for empty lists</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">heapq.heappush(p1, (</span><span class=\"mtk7\">5</span><span class=\"mtk1\">, </span><span class=\"mtk17\">&quot;not so important&quot;</span><span class=\"mtk1\">))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">heapq.heappush(p1, (</span><span class=\"mtk7\">1</span><span class=\"mtk1\">, </span><span class=\"mtk17\">&quot;Really important&quot;</span><span class=\"mtk1\">))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">heapq.heappush(p1, (</span><span class=\"mtk7\">20</span><span class=\"mtk1\">, </span><span class=\"mtk17\">&quot;Forget it&quot;</span><span class=\"mtk1\">))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">heapq.heappush(p1, (</span><span class=\"mtk7\">24</span><span class=\"mtk1\">, (</span><span class=\"mtk17\">&quot;t1&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk17\">&quot;t2&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk17\">&quot;t3&quot;</span><span class=\"mtk1\">)))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">important_task = heapq.heappop(p1)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">assert</span><span class=\"mtk1\"> important_task[</span><span class=\"mtk7\">0</span><span class=\"mtk1\">] == </span><span class=\"mtk7\">1</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># For heapq when using the tuples, it will go onto next element if the first element is identical.</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># The docs are framing the type construction as a payload to attach a payload.</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># This is over loaded semantics (shane on you, Python)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">tie_breaker = </span><span class=\"mtk7\">0</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk10\">next_tie_breaker</span><span class=\"mtk1\">():</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk4\">global</span><span class=\"mtk1\"> tie_breaker</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    tie_breaker += </span><span class=\"mtk7\">1</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> tie_breaker</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">leafs = []</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">for</span><span class=\"mtk1\"> k </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> freqs:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    heapq.heappush(leafs, (freqs[k], next_tie_breaker(), k))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Alternatively</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># leafs1 = [(v, next_tie_breaker(), k) for k, v in freqs.items()]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># heapq.heapify(leafs1)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Representing the tree:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Leaf (frq, _, symbol)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Node: (frq, _, (left, right))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">while</span><span class=\"mtk1\"> </span><span class=\"mtk4\">True</span><span class=\"mtk1\">:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\"> </span><span class=\"mtk10\">len</span><span class=\"mtk1\">(leafs) == </span><span class=\"mtk7\">1</span><span class=\"mtk1\">:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk14\">break</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    a = heapq.heappop(leafs)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    b = heapq.heappop(leafs)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    sumProp = a[</span><span class=\"mtk7\">0</span><span class=\"mtk1\">] + b[</span><span class=\"mtk7\">0</span><span class=\"mtk1\">]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    node = (sumProp, next_tie_breaker(), (a, b))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    heapq.heappush(leafs, node)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">huffman_tree = heapq.heappop(leafs)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">huffman_dict = {}</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">rev_huffman_dict = {}</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">stack = [(</span><span class=\"mtk17\">&quot;&quot;</span><span class=\"mtk1\">, huffman_tree)]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">while</span><span class=\"mtk1\"> </span><span class=\"mtk10\">len</span><span class=\"mtk1\">(stack) &gt; </span><span class=\"mtk7\">0</span><span class=\"mtk1\">:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk10\">next</span><span class=\"mtk1\"> = stack.pop()</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    match(</span><span class=\"mtk10\">next</span><span class=\"mtk1\">):</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        case (prefix, (frq, x, (left, right))):</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">            stack.append((prefix + </span><span class=\"mtk17\">&quot;1&quot;</span><span class=\"mtk1\">, left))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">            stack.append((prefix + </span><span class=\"mtk17\">&quot;0&quot;</span><span class=\"mtk1\">, right))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        case (prefix, (frq, x, symbol)):</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">            huffman_dict[prefix] = symbol</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">            rev_huffman_dict[symbol] = prefix</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        case _:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">            pprint.pprint(</span><span class=\"mtk10\">next</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># Let&#39;s compress something!</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">string = </span><span class=\"mtk17\">&quot;Music to hear, why hear’st thou music sadly?&quot;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\"># compress</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">compressed = </span><span class=\"mtk17\">&quot;&quot;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">for</span><span class=\"mtk1\"> c </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> string:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    compressed += rev_huffman_dict[c]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">decompressed = </span><span class=\"mtk17\">&quot;&quot;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">tree_traverser = huffman_tree</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">for</span><span class=\"mtk1\"> c </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> compressed:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk3\"># Traverse the tree</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\"> c == </span><span class=\"mtk17\">&quot;1&quot;</span><span class=\"mtk1\">:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        tree_traverser = tree_traverser[</span><span class=\"mtk7\">2</span><span class=\"mtk1\">][</span><span class=\"mtk7\">0</span><span class=\"mtk1\">]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\"> c == </span><span class=\"mtk17\">&quot;0&quot;</span><span class=\"mtk1\">:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        tree_traverser = tree_traverser[</span><span class=\"mtk7\">2</span><span class=\"mtk1\">][</span><span class=\"mtk7\">1</span><span class=\"mtk1\">]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk3\"># Are we at a leaf?</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">if</span><span class=\"mtk1\"> </span><span class=\"mtk10\">isinstance</span><span class=\"mtk1\">(tree_traverser[</span><span class=\"mtk7\">2</span><span class=\"mtk1\">], </span><span class=\"mtk9\">str</span><span class=\"mtk1\">):</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        decompressed += tree_traverser[</span><span class=\"mtk7\">2</span><span class=\"mtk1\">]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        tree_traverser = huffman_tree</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk10\">print</span><span class=\"mtk1\">(</span><span class=\"mtk4\">f</span><span class=\"mtk17\">&quot;Orig: </span><span class=\"mtk4\">{</span><span class=\"mtk1\">string</span><span class=\"mtk4\">}</span><span class=\"mtk17\">&quot;</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk10\">print</span><span class=\"mtk1\">(</span><span class=\"mtk4\">f</span><span class=\"mtk17\">&quot;Comp: </span><span class=\"mtk4\">{</span><span class=\"mtk1\">compressed</span><span class=\"mtk4\">}</span><span class=\"mtk17\">&quot;</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk10\">print</span><span class=\"mtk1\">(</span><span class=\"mtk4\">f</span><span class=\"mtk17\">&quot;Deco: </span><span class=\"mtk4\">{</span><span class=\"mtk1\">decompressed</span><span class=\"mtk4\">}</span><span class=\"mtk17\">&quot;</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk10\">print</span><span class=\"mtk1\">(</span><span class=\"mtk4\">f</span><span class=\"mtk17\">&quot;Size uncompressed </span><span class=\"mtk4\">{</span><span class=\"mtk10\">len</span><span class=\"mtk1\">(string)</span><span class=\"mtk4\">}</span><span class=\"mtk17\">&quot;</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk10\">print</span><span class=\"mtk1\">(</span><span class=\"mtk4\">f</span><span class=\"mtk17\">&quot;Size compressed </span><span class=\"mtk4\">{</span><span class=\"mtk1\">math.ceil(</span><span class=\"mtk10\">len</span><span class=\"mtk1\">(compressed) / </span><span class=\"mtk7\">8</span><span class=\"mtk1\">)</span><span class=\"mtk4\">}</span><span class=\"mtk17\">&quot;</span><span class=\"mtk1\">) </span><span class=\"mtk3\"># We approximate here</span></span></span></code></pre>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .mtk3 { color: #008000; }\n  .default-light .mtk14 { color: #AF00DB; }\n  .default-light .mtk1 { color: #000000; }\n  .default-light .mtk17 { color: #A31515; }\n  .default-light .mtk7 { color: #09885A; }\n  .default-light .mtk4 { color: #0000FF; }\n  .default-light .mtk10 { color: #795E26; }\n  .default-light .mtk9 { color: #267F99; }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/ingesting-data/","relativePath":"ingesting-data.md","relativeDir":"","base":"ingesting-data.md","name":"ingesting-data","frontmatter":{"title":"Ingesting Data","author":"Mads Buch","img_path":"images/data.png","template":"page"},"html":"<p>Ingesting data is a crucial part of building AI systems. AI systems require to\nbe able to do two things: Use context and abstract. The context comes from\nthe ingested data.</p>\n<p>Ingesting is the process of interpreting the data into something the AI engine\nunderstands. Traditionally this meant fitting the data onto a specific model.\nAnother approach, however, is to ingest in terms of relations, data fields,\nand think in <em>modalities</em>.</p>\n<h2>Modality</h2>\n<p>Think multi-modal learning. The idea is that information is experienced under\na particular modality. Such that a person can be experienced visually,\nauditorially, be felt, smelled, etc.</p>\n<p>The story is the same regarding pieces of information about an entity. It\ncan exist in terms of text, location, imagery, time, etc. The fundamental\nrealization is that entities span multiple modalities and that modalities\nrepresent the interface to the surrounding world.</p>\n<h2>Types of Ingestion</h2>\n<p>In the simplest for ingestion happens as data transform from a simple source\ninto an internal representation. An example is tabular data with no need for\na context to understand and ingest it.</p>\n<p>For more complex cases, we need contextual information to ingest it.\nThe canonical example of this is entity linking when extracting data\nfrom written text.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/introducing-a-second-brain/","relativePath":"introducing-a-second-brain.md","relativeDir":"","base":"introducing-a-second-brain.md","name":"introducing-a-second-brain","frontmatter":{"title":"Introducing Second Brains","author":"Mads Buch","subtitle":"Ongoing thoughts on second brains","excerpt":"Seconds brains have reached quite the hype. Both with tools like RoamResearch, Obsidian and friends. Needless to say, it is a good tool. Since I started building my second brain, I have been journalizing more often than not.","date":"2021-01-06","thumb_img_path":"images/sb-graph.png","content_img_path":"images/sb-graph.png","template":"post"},"html":"<p><em>Main point: Second brains are great in particular because they require no structure.</em></p>\n<p>Seconds brains have reached quite the hype. Both with tools like RoamResearch,\nObsidian and friends. Needless to say, it is a good tool. Since I started\nbuilding my second brain, I have been journalizing more often than not. This\nis healthy for me.</p>\n<p>On of the strength about second brains is that they are naturally without\nstructure. That is, thoughts do not need to go into particular folders. They\ncan go in todays journal. If it makes sense, they might occupy their own file.\nThey might be constituted in a tag, and expanded to a full file whenever\nthere is juice enough to do so.</p>\n<p>This is not to say that cleanups and organization is not possible and should not\nbe aspired to. It is just another process, saved for another time. Like a\nboring sunday afternoon with nothing more important to do, whenever they come.\nA deep consequence of thinking this way is also the acceptance that thoughts\nand ways of doing thins change on a structural level. For a personal second\nbrain this is perfect. Tools I need naturally emerge from my text. When I\ndon't need the tools anymore, they evaporate.</p>\n<p>One aspect I am especially keen about is what leverage I will be able to get:\nWill I be able to derive some deeper truths about myself? Will I be able to\nfind insights I was not able to see before? Currently I am merely producing\nthe data. Along with this, my work with <a href=\"https://spor.ai/\">Spor.ai</a> hopefully\nwill bring forward tools needed for such inspection.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/knowledge-graph-based-research/","relativePath":"knowledge-graph-based-research.md","relativeDir":"","base":"knowledge-graph-based-research.md","name":"knowledge-graph-based-research","frontmatter":{"title":"Knowledge Based Research and Vague Ideas","author":"Mads Buch","subtitle":"On managing, disseminating, and condensing knowledge","excerpt":"Knowledge graphs based research give overview on vague ideas, assists knowledge discovery, and is a strong collaboration tool.","date":"2021-02-17","template":"post"},"html":"<p><em>Main point: Knowledge graphs based research gives overview on vague ideas,</em>\n<em>assists knowledge discovery, and is a strong collaboration tool.</em></p>\n<p>Currently I am researching for at proposal on a project within machine learning\nand type theory. The idea, however, is not entirely condensed and figuring out\nwhere to pick the low hanging fruits is difficult. My process has often been\nthat I create different documents that represent different aspects of the idea.\nIn the end I eventually get lost in my own production to move on to more\npressing tasks to forget about the project for weeks at the time.</p>\n<p>This represents a class of ideas are also some that I get back to infrequently.\nI often revisit them after a long walk where I got some realization.\nObviously, at that point I do not have the head space to put the new insight\nin context and rework old material.\nSo it'll often just get chucked in there for later review. When I finally reach\nthat point where I cam do a later review, the task might still be daunting.\nEven worse, one might have miscalculated the actual effort to clean up. In\neither direction. Sometimes I thought I had much more material than was actually\nthe case.</p>\n<p>This is a part of the create process and should not be discourages. But I\ndefinitely think there is room for tooling around it. This is why we are\ncreating <a href=\"https://spor.ai/\">Spor Research</a>. It is a tool that solves the\noverview and context problem. In particular we remove the meat of the research\nto leave on the skeletal structures. It is then significantly easier to\nfigure out where to add in new insights, where to put a weekends worth of\neffort, and to serendipitously discover new connections.</p>\n<p>First and foremost we integrate the tool with seconds brains,\nZettelkasten, and linked notes. This is based on the assumption that research\nstarts with what we know ourselves.</p>\n<p>Secondly we integrate with public sources such as Wikipedia, arXiv (my hood),\nand business databases. This allows to explorer the context in-app. We have\nthe idea that we need to remove barriers on thinking up search queries. So if\nwe can just remove a fraction of dead-end searches we see that as an impact.</p>\n<p>Lastly, we also know that research is not carried out individually. We also\nknow that it is a big ask to make colleagues read and provide feedback on\ncomplicated texts. For these tasks we work on ways to share a research\noverview and also provide information on what participant of the teams has\ncontext and specific knowledge on each part.</p>\n<p>We share updates on the progress on our <a href=\"https://www.linkedin.com/company/spor-ai\">LinkedIn Page</a>.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/lambda-calculus-interpreter/","relativePath":"lambda-calculus-interpreter.md","relativeDir":"","base":"lambda-calculus-interpreter.md","name":"lambda-calculus-interpreter","frontmatter":{"title":"Lambda Calculus Interpreter","author":"Mads Buch","date":"2022-11-01","template":"post","category":"technicalRef"},"html":"<p><em>Draft</em></p>\n<p>Lambda calculus, often regarded as the cornerstone of computational theory,\nis a mathematical system of function abstraction and application.\nSince its inception, it has served as a research vehicle.\nBeyond its theoretical significance, lambda calculus is also the\nfoundation for many modern programming languages. Furthermore, its longstanding\nhistory and impact have made it a well-studied field.</p>\n<p>One of the languages particularly building on the lambda calculus is Haskell.\nHaskell is a functional language that has captured the attention of developers for\nits elegance and mathematical soundness.</p>\n<h3>The Interpreter</h3>\n<p>Clone the repository, make sure you have the latest version of haskell stack\ninstalled. It will take care of the rest from there.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">$ stack build</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">$ stack test</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">$ stack exec lambda-exe</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Lambda Calculus Interpreter!</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">λ: (\\x.x) (\\y.y y)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">(λy.(y) (y))</span></span></code></pre>\n<p>In particular this example executed that lambda expressions <code>(\\x.x) (\\y.y y)</code>.\nLambda calculus works in the way that the expressions is reduced until it\ncan not be reduced anymore. The resulting term is the value. This differs from\n\"normal\" programming languages where we would expect to get a string or a number\nout.</p>\n<p>The core insight is that everything can be encoded as a lambda term.</p>\n<h2>Lambda Calculus</h2>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/masters-thesis/","relativePath":"masters-thesis.md","relativeDir":"","base":"masters-thesis.md","name":"masters-thesis","frontmatter":{"title":"Masters Thesis","subtitle":"Formalizing Differential Privacy","template":"page"},"html":"<p>As a part of my degree as Masters in Science in Computer science I wrote my\nmasters thesis on the subject of formalizing differential privacy.</p>\n<p>The outcome what a <a href=\"https://thesis.madsbuch.com/thesis.pdf\">thesis report</a> and\na <a href=\"https://thesis.madsbuch.com/\">thesis site</a>.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/leadership-imperatives/","relativePath":"leadership-imperatives.md","relativeDir":"","base":"leadership-imperatives.md","name":"leadership-imperatives","frontmatter":{"title":"Leadership and Imperatives","author":"Mads Buch","date":"2021-09-15","template":"post","excerpt":""},"html":"<p>Here's an idea: Leadership is about managing imperatives.</p>\n<p>Well, not just leadership as in corporate leadership, but also people management and collaboration.</p>\n<p>This idea has some consequences. First of all, managing imperatives also requires one to know what needs to be done. It would appear that a foundational craft in leadership is the ability to produce and articulate <em>units of imperatives</em>.</p>\n<p>Another craft is the ability to agree on imperatives. There are several ways to do that. From the simple \"you are being paid to do this\" the the \"We have an implicit alignment of imperatives based on the organizational values.\" From the \"I appreciate your help on this matter\" to \"This is a part of your job.\" And the choice of strategy is orders up depending on the mandate, goodwill, and other hardly measurable elements a relation possesses.</p>\n<p>Also, some imperatives are constructive, while some a destructive. Mixing up internally imposed and externally imposed imperatives can have dire consequences for the mental health of people. Letting people think they have a choice, when in fact, they might not be sustainable. This is worth keeping in mind when the leader creates sustainable organizations.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/library-vs-diy/","relativePath":"library-vs-diy.md","relativeDir":"","base":"library-vs-diy.md","name":"library-vs-diy","frontmatter":{"title":"Library vs. Do It Yourself","author":"Mads Buch","subtitle":"On learning and libraries","excerpt":"One reason to not use external libraries is based on the learning outcome of the task","date":"2021-03-06","template":"post"},"html":"<p><em>Main point: One reason to not use external libraries is based on the learning outcome of the task.</em></p>\n<p>This is one of the persistent questions when developing software. When should\nwe get an external library to solve problems and when should we write the\nfunctionality ourselves. The answer has many facets: What is the security\nrequirement, does it add or reduce complexity, does it pose a recruitment\nrisk, and, in my case, the goal of writing software.</p>\n<p>I have recently started to implement my own Haskell library to do probabilistic\nprogramming and explore data. There already exists a couple of alternatives\nthough I still insist on making my own. The main reason is that I am working\nwith the cores of probabilistic programming and need to develop my abstract\nunderstanding of the field rather than a concrete competence to use a tool.\nI opt-in for the abstract understanding as professional opportunities within\nHaskell are scarce (even more so in Scandinavia) and I can therefore leverage\nthe abstract understanding cross-technology better.</p>\n<p>Had this project been developed in a language like Java or C# then it would\nprobably have been wiser to focus on the problem domain, like how to analyze\nspecific data, and defer the development of the tools. In particular, because it\nis easier to leverage tool competencies professionally writing in these\nlanguages.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/minimal-psi-rust/","relativePath":"minimal-psi-rust.md","relativeDir":"","base":"minimal-psi-rust.md","name":"minimal-psi-rust","frontmatter":{"title":"Minimal Private Set Intersection Implementation in Rust","author":"Mads Buch","date":"2023-08-27","template":"post"},"html":"<p>As a toy weekend project I have implemented a minimal version of the private set\nintersection protocol (PSI) in Rust using homomorphic encryption. There are\nnumerous ways to implement protocols to achieve this. Though homomorphic\nencryption is interesting as it gives as asymmetric implementation it is\nalso resource intensive.</p>\n<p>Private set intersection is interesting in a new realm of <em>privacy</em> engineering.\nWith more regulation companies have the responsibility to not reveal data. A\nplethora of new techniques attempts to make that practical.</p>\n<p>The main applications are in the ad-industry where companies want to\nintegrate data from different ad-companies without holding to any personally\nidentifieble material. There are also applications in the anti-money laundering\nindustry where banks want to ensure that people are not present in various\nregistries without revealing the person they consider to said registries.</p>\n<p>Anyways, this is my short exposition from a airport terminal in Munich.\nThe code can be found <a href=\"https://github.com/madsbuch/rust-psi-poc\">here</a>.</p>\n<h2>Homomorphic Encypritoin Background Speed Run</h2>\n<p><strong>Homomorphic Encryption</strong> is the name for the schemes that allow running\ncomputations on encrypted data, which in the game of mathematics means addition\nand multiplication. There are a number of schemes and it is an\nentire research field. To keep this simple, we will merely focus on the\nproperties that we need:</p>\n$$\n\\begin{align*}\n    E(a) - E(b)  &= E(a - b) \\\\\n    E(a) \\times E(b)  &= E(a \\times b) \\\\\n    r \\times E(b)  &= E(r \\times b)\n\\end{align*}\n$$\n<p>Where \\( E(x) \\) is the enrypted value of x.\nThese are the ones we will focus on.</p>\n<h2>The HE PSI Insight</h2>\n<p>Prerquisite for this, elements are fixed-width integers. This make entirely\nsense for this field as it is <em>set intersection</em> we are solving. All classes\nof entities can reasonably be hashed to a fixed-width integer.</p>\n<p>So! Alice has the number a and Bob has the number b. They want to test whether\nthey have the same number without revealing the numbers (unless they actually\nhave the same number).</p>\n<ol>\n<li>Alice generates \\( E(a) \\) and sends it to Bob. This is secure as \\( a \\) is encrypted.</li>\n<li>Bob calculates \\( E(a) - b \\) which we know is the same as \\( E(a - b) \\)\nIf Alice decrypts that value and get 0, we know that \\( a \\) and \\( b \\)\nare the same. This is <strong>not</strong> secure: Alice can derive Bobs value \\( b \\)!</li>\n<li>Bob needs to return \\( E(a - b)^r \\) where r is some private non-zero value.\nThis is secure, as Alice can not derive Bobs value \\( b \\) as she does not\nknow r.</li>\n</ol>\n<h2>Implementation in Rust</h2>\n<p>To my best knowledge, there are no Rust implementation for tensored\nhomomorphic libraries. It is a tad out of scope to implement that, so for\nthe exposition, we will experiment with the library\n<a href=\"https://docs.zama.ai/tfhe-rs\">TFHE-rs</a>.</p>\n<p>For this to work, the client encrypts the value it wants to check intersection\non and send the encrypted value to the server. In this example, the server only\nallow a single value. But it should be clear that we can run the algorithm\nmultiple times to find the intersection between a client set and a server set.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"rs\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// Hash to test</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">let</span><span class=\"mtk1\"> client_hash = </span><span class=\"mtk7\">123456u32</span><span class=\"mtk1\">;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// Hash is encrypted using the (private) client_key and sent to the server</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">let</span><span class=\"mtk1\"> e_client_hash = FheUint32::</span><span class=\"mtk10\">try_encrypt</span><span class=\"mtk1\">(client_hash, &amp;client_key)?;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk10\">println!</span><span class=\"mtk1\">(</span><span class=\"mtk17\">&quot;CLIENT: Encrypts the value and sends it to the server&quot;</span><span class=\"mtk1\">);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// Let the server to its work</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">let</span><span class=\"mtk1\"> server_response = </span><span class=\"mtk10\">server</span><span class=\"mtk1\">(e_client_hash);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// Decrypt the returned value</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">let</span><span class=\"mtk1\"> clear_res: </span><span class=\"mtk4\">u32</span><span class=\"mtk1\"> = server_response.</span><span class=\"mtk10\">decrypt</span><span class=\"mtk1\">(&amp;client_key);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">if</span><span class=\"mtk1\"> clear_res == </span><span class=\"mtk7\">0</span><span class=\"mtk1\"> {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">   </span><span class=\"mtk10\">println!</span><span class=\"mtk1\">(</span><span class=\"mtk17\">&quot;CLIENT: value is in the database 👏&quot;</span><span class=\"mtk1\">);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">} </span><span class=\"mtk14\">else</span><span class=\"mtk1\"> {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">   </span><span class=\"mtk10\">println!</span><span class=\"mtk1\">(</span><span class=\"mtk17\">&quot;CLIENT: value is NOT in the database 👎&quot;</span><span class=\"mtk1\">);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span></code></pre>\n<p>By virtue of the client secret being encrypted, the server has no knowledge of\nit. It will work on the encrypted value.</p>\n<p>The server calculates \\( ((c - db_1) * (c - db_2) * (c - db_{...}))^r \\),\nor ideally should. In this test, we merely multiply on <em>r</em>. This would be\nnon grata for production usage.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"rs\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// Represent a database by a list</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">let</span><span class=\"mtk1\"> server_hashes = </span><span class=\"mtk10\">vec!</span><span class=\"mtk1\">[...];</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// Generate a number to disguise server values</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">let</span><span class=\"mtk1\"> server_r: </span><span class=\"mtk4\">u32</span><span class=\"mtk1\"> = </span><span class=\"mtk10\">random</span><span class=\"mtk1\">();</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// Encrypt the values so we can make homomorphic operations on them. This</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// is not secure and does not require the client key.</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">let</span><span class=\"mtk1\"> e_server_r = FheUint32::</span><span class=\"mtk10\">encrypt_trivial</span><span class=\"mtk1\">(server_r);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">let</span><span class=\"mtk1\"> e_server_hashes = server_hashes</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">   .</span><span class=\"mtk10\">iter</span><span class=\"mtk1\">()</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">   .</span><span class=\"mtk10\">map</span><span class=\"mtk1\">(|&amp;server_hash| FheUint32::</span><span class=\"mtk10\">encrypt_trivial</span><span class=\"mtk1\">(server_hash));</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// calculate the difference between the server values and the client values.</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// If they are identical, the result will be 0. Multiply it all together, if</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// a single value was zero, the resulting value is 0</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">let</span><span class=\"mtk1\"> test_equal = e_server_hashes</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">   .</span><span class=\"mtk10\">map</span><span class=\"mtk1\">(|e_server_hash| &amp;e_client_value - &amp;e_server_hash)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">   .</span><span class=\"mtk10\">fold</span><span class=\"mtk1\">(FheUint32::</span><span class=\"mtk10\">encrypt_trivial</span><span class=\"mtk1\">(</span><span class=\"mtk7\">1</span><span class=\"mtk1\">), |acc, e| &amp;acc * &amp;e);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// Exponentiate on r to avoid releasing information about the servers database</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// Note: We multiply instead of taking the power as no exponentiation function</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">// is present and iteratively adding would be too computationally expensive.</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">return</span><span class=\"mtk1\"> &amp;test_equal * &amp;e_server_r;</span></span></span></code></pre>\n<p>The resulting value can be decrypted and tested by the client.</p>\n<h2>Privacy Engineering</h2>\n<p>This piece of technology fits in the realm of privacy engineering. It is a sub\nproblem of doing generalized privacy preserving operations.</p>\n<p>In particular this is one tool out of several that allows to release\nonly specific information in a privacy preserving manner, in this case the\nidentity of an intersecting set.</p>\n<p>Other tools exists for other types of data. Homomorphic encryption allows for\na broad range of protocols to be build on it due to its general nature.\nUnfortunately the schemes are still slow and for some schemes values decay\nquickly making elaborate computation difficult.</p>\n<p>Another tehnique to release data in a privacy preserving way is by using\n<a href=\"/masters-thesis\">differential privacy</a>. There are different schemes for\ndifferential privacy though the key idea to to add noise in a way to disquises\nthe original values. In comparison to homomorphic encryption schemes,\ndifferential pricavy works when release data for aggregate analysis.</p>\n<h2>References</h2>\n<p>A more practical way would be to take the same path as the BitML people and\nimplement the scheme using <a href=\"https://github.com/OpenMined/TenSEAL\">TenSEAL</a>\nin Python or another vectorized implementation of homomorphic encryption.</p>\n<p>To further understand this field, I can really recommend BitML's article:\n<a href=\"https://bit-ml.github.io/blog/post/private-set-intersection-an-implementation-in-python/\">Private Set Intersection from Homomorphic Encryption: A Python Implementation</a>.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .mtk3 { color: #008000; }\n  .default-light .mtk4 { color: #0000FF; }\n  .default-light .mtk1 { color: #000000; }\n  .default-light .mtk7 { color: #09885A; }\n  .default-light .mtk10 { color: #795E26; }\n  .default-light .mtk17 { color: #A31515; }\n  .default-light .mtk14 { color: #AF00DB; }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/probabilities-and-statements/","relativePath":"probabilities-and-statements.md","relativeDir":"","base":"probabilities-and-statements.md","name":"probabilities-and-statements","frontmatter":{"title":"Probabilities and Statements","author":"Mads Buch","subtitle":"On the probability of dying","excerpt":"One should be skeptic when presented by statistics. One way to articulate well-founded skepticism is by following 3 steps of reasoning.","date":"2021-03-04","template":"post"},"html":"<p>Main point: One should be skeptic when presented by statistics. One way to\narticulate well-founded skepticism is by following 3 steps of reasoning.</p>\n<blockquote>\n<p>The average American is expected to live seventy-three years. Therefore if\nyou are sixty-eight you can expect to live five more years, and should plan\naccordingly.</p>\n</blockquote>\n<p>The above quote is from Nassim Talebs book <em>Fooled by Randomness</em> where he quotes\na journalist for those exact words. Taleb points out the error that one\nconclusion has been moved to another context. The conclusion has been <em>lifted</em>\ninto a <em>higher order</em> without taking that order into account. For this exact example,\nwe see that the expected years to live are used both in a conclusion for all\nAmericans and Americans who <em>already</em> have reached the age of 68.</p>\n<p>These pitfalls can be detected and assessed and we can build small programs that\ncan validate statements and to what extent they can be trusted. Like Taleb I\nuse Monte Carlo simulations and follow 3 easy steps to do so:</p>\n<ol>\n<li>Build a distribution that yields the elements of interest in the statements,\nin this case, dead people.</li>\n<li>Read the statement carefully and extract conditionals.</li>\n<li>Build new specialized distributions taking conditionals into account.</li>\n</ol>\n<p>I have done so just with Danish deaths and not the American ones.</p>\n<h1>Sampling the Death Realm</h1>\n<p>From the Danish institute for statistics, I can download a list of deaths\nfrom 2020<sup id=\"fnref-dst\"><a href=\"#fn-dst\" class=\"footnote-ref\">dst</a></sup>. The information I get is gender, age, year of death, and, by\ninference, that they are Danish nationals. The list looks like following</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">observations = [(0, Men),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                ...</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                (73, Women),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                (73, Women),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                (73, Women),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                (74, Men),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                (74, Men),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                (74, Men),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                (74, Men),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                ...</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                ]</span></span></code></pre>\n<p>where the first number is the age and the middle element the gender. The list\nlong. exactly 54.645 deaths were recorded in Denmark in 2020. From that\nlist we simply draw a random element, that's it. We now have our distribution.\nI like to think of this as the <code>deathRealm</code>. In Haskell it can be defined as\nfollows<sup id=\"fnref-monad\"><a href=\"#fn-monad\" class=\"footnote-ref\">monad</a></sup>.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">deathRealmRaw :: P (Age, Gender)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">deathRealmRaw = uniform observations</span></span></code></pre>\n<p>When we want to calculate the expected age we simply draw 10.000 elements from\nthat list at random and average over their age. When I do that I get\n77.65 years as the expected value. This is a bit different from the Americans'\nexpected age but the idea is the same.</p>\n<h1>The Statement</h1>\n<p>The statement of interest from the quote was the second half:</p>\n<blockquote>\n<p>Therefore if you are sixty-eight you can expect to live five more years, and\nshould plan accordingly.</p>\n</blockquote>\n<p>Understanding the orders is the craft as there might some indirect ones and\nsome direct ones. Regardless, it is mentioned that\n<em>you are 68 years</em> old, so we will at least incorporate that into our\ndistribution. This is done swiftly by discarding all deaths that do not\nsatisfy that claim that the person is 68 years old. To be a bit more general,\nI built a function that can do this for any provided age.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">deathRealmPerAge :: Age -&gt; P (Age, Gender)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">deathRealmPerAge age = do</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  (sampledAge, gender) &lt;- deathRealm</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  if sampledAge &gt;= age</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    then return (sampledAge, gender)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    else deathRealmPerAge age</span></span></code></pre>\n<p>In the code we draw a sample, check if it is more that the declared age. If so\nthen we uge it, otherwise we try again. Again we draw 10.000 elements and take\nthe average of their age.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">&gt; expectedAge $ deathRealmPerAge 68</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">82.76</span></span></code></pre>\n<p>From here we see that expected age is more than 5 year more. Ie. if you have\ndocumented that you are able to reach a certain age, you are expected to\nbecome a bit older.</p>\n<p>Naturally one is not able to build computational models and evaluate statements\non the fly. But the main takeaway from Nassim Talebs example is the implicit\nlift that was done. Doing that should give som form og skepticism.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>\n<div class=\"footnotes\">\n<hr>\n<ol>\n<li id=\"fn-dst\">\n<p><a href=\"https://www.dst.dk/en/Statistik/emner/befolkning-og-valg/doedsfald-og-middellevetid/doedsfald\">The death statistics in Denmark for 2020</a></p>\n<a href=\"#fnref-dst\" class=\"footnote-backref\">↩</a>\n</li>\n<li id=\"fn-monad\">\n<p>I have use the <a href=\"https://www.madsbuch.com/the-probability-monad/\">probability monad</a> to model the problems.</p>\n<a href=\"#fnref-monad\" class=\"footnote-backref\">↩</a>\n</li>\n</ol>\n</div>"},{"url":"/productizing-ml-models/","relativePath":"productizing-ml-models.md","relativeDir":"","base":"productizing-ml-models.md","name":"productizing-ml-models","frontmatter":{"title":"Productizing Machine Learning Models","author":"Mads Buch","subtitle":"On making machine learning models available for production","date":"2021-04-30","template":"post","excerpt":"The data engineer understands the architecture of the production system. She rewrites the notebook into an architecturally coherent unit that is being packaged as an image. This image is being deployed to the production system."},"html":"<p>Let's set up a scenario: We have a company that builds models for machine\ntranslation. The data scientists in work in their own Python notebooks\nand the data engineers take these notebooks and implement them as production-ready\nmodels that can do inference. This process looks something like the following:\nThe data engineer understands the architecture of the production system. She\nrewrites the notebook into an architecturally coherent unit that is being\npackaged as an image. This image is being deployed to the production system.</p>\n<p>As an experiment, we are going to do just that. As the example notebook, we use the\nPyTorch tutorial on\n<a href=\"https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html\">sequence to sequence learning</a>.\nThis notebook needs to be made production-ready for the <a href=\"https://github.com/madsbuch/scalable-ml-service\">scalable ml service</a>\nproject as the inference engine for doing French to English translations.</p>\n<p>The commit introducing this change is available <a href=\"https://github.com/madsbuch/scalable-ml-service/commit/36e32c6883abb521e5d895ef6726f37853749d71\">here</a>.</p>\n<h1>Hard Constraints</h1>\n<p>Step one is to make it possible to run the model on the infrastructure using the\nlowest lift. This requires that we export the notebook\nto a stand-alone Python file, we make wrapper code that makes a minimal\ninterface to the transport layer and we package it in a way so that we can\ndeploy it to the infrastructure.</p>\n<p>We prioritize getting the system to a working state. This means that we defer\nconcerns about architectural soundness of the code, brittle parsing,\nreadability, etc. We simply need to some something working in a production-ready\nstate (Hey! Sometimes this is even enough, why we should never anticipate\nwork).</p>\n<p>The transport layer consists of a RabbitMQ message broker. There is a queue\nfor doing RPC calls which we hook into. In particular, we are listening to the\n<code>rpc_queue</code>-queue. As for the current architecture, the messages sent over\nthe queue are merely the string to be translated and the translation is sent\nback.</p>\n<p>To respond to messages we simply use the evaluate function the original author\nhad already put in place and make sure it never fails.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"py\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk10\">translateSentence</span><span class=\"mtk1\">(</span><span class=\"mtk12\">input_sentence</span><span class=\"mtk1\">):</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">try</span><span class=\"mtk1\">:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        output_words, attentions = evaluate(encoder1, attn_decoder1, input_sentence)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> </span><span class=\"mtk17\">&#39; &#39;</span><span class=\"mtk1\">.join(output_words)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">except</span><span class=\"mtk1\">:</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> </span><span class=\"mtk17\">&quot;Could not translate. Try &#39;je vais domir .&#39;&quot;</span></span></span></code></pre>\n<p>Lastly, we package everything using a Dockerfile in order to run it as a\ncontainer on the infrastructure. This yields and up and running state and\nwe can translate.</p>\n<h1>Clean Up</h1>\n<p>Playing around with the minimal setup clearly yields a number of problems.</p>\n<ul>\n<li>Translation of <code>je vais dormir .</code> translates into <code>i am going to sleep . &#x3C;EOS></code>:\nWe definitely do not want these special symbols in the output string.</li>\n<li>Translation of <code>Je vais dormir.</code> simply fails. The tokenization is wonky and\nneeds to be updated.</li>\n<li>The code is kind of messy. We carry over source data files in order to build\ndictionaries etc. on the go. We want to clean up that a bit also.</li>\n<li>We need to distribute the training data in the image even though we a not\ntraining anything.</li>\n</ul>\n<p>To carry out these refactorings, we do the following:</p>\n<ol>\n<li>Remove unused code. The notebook is filled with code to train and evaluate.\nAt this point, we have decided, that the model is something we want to put\ninto production, and the training and evaluation is done.</li>\n<li>Isolate model architecture from sentence parsing and translation, and transport\nlayer code. We do this mostly to ensure good architectural practices.\nThis will increase readability.</li>\n<li>Implement parsing for input strings. This is simply done by using the same\n<code>normalize</code> function as was used on the training data.</li>\n<li>Serialize the <code>Lang</code> objects: In order not to re-parse the raw training data\nto build an internal representation every time an image boots, we merely\nsave the dumped representations and deserialize it whenever the image boots.</li>\n</ol>\n<p>We now have an image that translates strings such as <code>Je vais dormir.</code> which\nseems much more natural.</p>\n<h1>Testing</h1>\n<p>Should we test this code? Or probably a better question: How should we test this\ncode?</p>\n<p>As for unit testing, there are arguments for and against it. The arguments\nfor are in particular those of dogmatic nature: We should test because it is\nbest practice. The arguments against are those that this should probably be\nseen as an asset. It is mostly model weights packaged just with enough code\nto run inference and push it back to the client. The production prepared image\ncan be sent back to the data scientist for her to do some final verifications\nthat the code is indeed preserving semantics.</p>\n<p>However, we do want to test this image. In particular we want to test it as\na black box. That we can spn it up, that it returns sensible results when\ninvoked, and that it panics properly when subjected to erroneous inputs, ie.\nthat it closes down so that the infrastructure can spin new instances up.</p>\n<h1>Final Remarks</h1>\n<p>This is by far a final implementation of a translation model. The output is\nstill not syntactically up to speed. It misses proper casing and spacing\nbetween symbols. Furthermore, it would be advantageous to implement a system\nthat guesses misspelled words. Eg. when writing <code>Je vais formir</code> the system\nshould be able to correct the <code>f</code> to a <code>d</code>_in the <code>formir</code> word. These\nchanges are not going to be implemented in the post.</p>\n<p>Furthermore, this image is self-contained and non-persistent. In the\nindustry, it is seen that inference images pull their model weights from\na data store before spinning up. In my opinion, the better solution is to use the\nimage registry for this. There are a number of reasons:</p>\n<ol>\n<li>We get versioning for free</li>\n<li>Model weights and the model architecture are tightly coupled. This will\nreduce this risk of runtime errors by mismatching weights and architecture\ncode. </li>\n<li>Fewer components in the code.</li>\n</ol>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .mtk4 { color: #0000FF; }\n  .default-light .mtk1 { color: #000000; }\n  .default-light .mtk10 { color: #795E26; }\n  .default-light .mtk12 { color: #001080; }\n  .default-light .mtk14 { color: #AF00DB; }\n  .default-light .mtk17 { color: #A31515; }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/now/","relativePath":"now.md","relativeDir":"","base":"now.md","name":"now","frontmatter":{"title":"Now","subtitle":"Rather do it than not","img_path":"images/about.webp","template":"page","indexOrder":2},"html":"<p>Updated: Noevmber 11th 2022</p>\n<p>I have relocated to Lisbon to be with the Arkive team and develop the tech\nneeded for the first decentralized museum.</p>\n<p>I still spend a considerable amount of time writing. I aspire to release more of\nthe notes I have laying around.</p>\n<p>Aside from doing fulltime software engineering, my work is more and more focused\non the society and how decentralized technologies can work in tendam with existing\ntechnologies.</p>\n<p>Otherwise, by all means write me on <code>me [@] madsbuch [.] com</code> and say hi!</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/programming-languages/","relativePath":"programming-languages.md","relativeDir":"","base":"programming-languages.md","name":"programming-languages","frontmatter":{"title":"Programming Languages","subtitle":"Subject on programming languages","img_path":"images/about.webp","template":"page"},"html":"<p>I consider programming languages one of my healthy hobbies. being able to\nclearly formulate logical arguments is of utmost importance as an engineer.\nAfter all, when we claim we are able to implement a solution it should be\npossible to implement. Programming languages span many different areas.</p>\n<p>The first aspect of programming languages is semantics. What can we say about\nprograms? How do we build programming languages where we can guarantee\nproperties about a program. The relevant article here are</p>\n<ul>\n<li><a href=\"/proving-stuff-in-haskell\">Proving Stuff in Haskell</a>: Basics on what it\nmeans to prove properties in a programming language.</li>\n</ul>\n<p>Patterns in programming is also interesting. The programming language always\nexposes patterns. These are typically functions, ie. abstraction and\napplication, but can also be iteration and other control structures along\nwith sequencing as we see in imperative programming languages.</p>\n<ul>\n<li><a href=\"/the-probability-monad\">The Probability Monad</a>: Monads are a ubiquitous\npattern in Haskell as it is a way to cope with Haskell's lazy evaluation\nin situations where order matters, such as reading from stdin. But it is\nalso a useful abstraction over other objects such as probabilities.</li>\n</ul>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/programming-lesson-1-on-variables/","relativePath":"programming-lesson-1-on-variables.md","relativeDir":"","base":"programming-lesson-1-on-variables.md","name":"programming-lesson-1-on-variables","frontmatter":{"title":"Programming Lesson 1 - One Variable, One Job","author":"Mads Buch","categories":"Programming Lessons","date":"2019-12-13","template":"post"},"html":"<p>Lesson #1 from the real world</p>\n<p>Often, it can be tempting to let a single variable have multiple functions.\nSuch as letting the absence (or null value) denote some boolean decision.\nAn example of such could be for a <code>uid</code> field on an object denoting feedback.\nHere, the initial attempt to model the object could be to let the absence of the field denote whether the user wants notification about feedback received.</p>\n<p>However, this makes it hard for another developer to enter developments on the project.\nIf she is tasked with adding reminders for feedback, she will be in trouble: Who is the user, and what is the notification state of the said user?\nThe <code>uid</code> field on the feedback object is optional (nullable), and the comment says that absence means that the user does not wish to receive notifications.</p>\n<p>Now the field has one more meaning:\nIf defined, there will be sent a reminder, and the requester will be notified about new feedback.\nOtherwise, no notification and no reminders are sent.</p>\n<p>Instead, allow multiple fields.\nAvoid conflating diverse meanings into the same field and keep side effects to single fields to a minimum.\nThis enables faster engineering onboarding.</p>\n<p><em>Please remember always to have compassion for other programmers and be</em>\n<em>curious about lessons.</em>\n<em>An obvious point for one programmer might be an epiphany for another.</em></p>\n<p>Over and out\nMads</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/programming-lesson-2-tests-and-streams/","relativePath":"programming-lesson-2-tests-and-streams.md","relativeDir":"","base":"programming-lesson-2-tests-and-streams.md","name":"programming-lesson-2-tests-and-streams","frontmatter":{"title":"Programming Lesson 2 - Tests and Streams","author":"Mads Buch","categories":"Programming Lessons","date":"2021-08-05","template":"post"},"html":"<p>Lesson #2 from the real world</p>\n<p>Working with streams is hard.\nIn particular, they are not as composable as I initially thought.\nA small peculiarity is closing streams: I had a stream sink in <code>stdout</code> and kept getting an error.\nIt turns out that closing a stream in the library cascaded.\nHence, it also closed the <code>stdout</code> stream.\nNaturally, my testing library kept writing to <code>stdout</code> which, in turn, yielded a write after close error.</p>\n<p>It turns out the solution was to apply good testing habits: A test should not write to <code>stdout</code>.\nInstead, a simple sink was written, and the stream was directed to it.\nThat allowed the increased benefit of measuring the stream and adding assertions to have the stream is expected to behave.</p>\n<p>Sometimes problems can be solved by progressing over them. Weird.</p>\n<p><em>Please remember always to have compassion for other programmers and be</em>\n<em>curious about lessons.</em>\n<em>An obvious point for one programmer might be an epiphany for another.</em></p>\n<p>Over and out\nMads</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/project-portfolio/","relativePath":"project-portfolio.md","relativeDir":"","base":"project-portfolio.md","name":"project-portfolio","frontmatter":{"title":"Project Portfolio","template":"page"},"html":"<p>This page contains the projects and examples I have been doing.</p>\n<p><strong>Current Projects:</strong></p>\n<ul>\n<li><strong>Gossiper.io:</strong></li>\n<li><strong><a href=\"/masters-thesis\">Masters of Science in Computer Science</a>:</strong> My academic\nbackground is that of a computer scientist specializing in semantics for\nprogramming languages.</li>\n<li><strong>Spor.ai:</strong> Working on exploratory research. The goal is to develop state of\nthe art techniques and tools to understand what data tells us about the real\nworld.</li>\n</ul>\n<p><strong>Expositions:</strong></p>\n<ul>\n<li><strong><a href=\"/lambda-calculus-interpreter\">Lambda Interpreter</a>:</strong> A lambda calculus interpreter written in Haskell.\nNothing seriously, purely educational. It is idiomatically written using\nHaskell Stack and implements a test suite. (<a href=\"https://github.com/madsbuch/lambda\">GitHub Link</a>)</li>\n<li><strong><a href=\"/scalable-ml-service\">Scalable ML Service</a>:</strong> An implementation of a\nsystem to carry out translations. This includes the entire stack. Implemented\nas an education vehicle to explorer scalable architectures.\n(<a href=\"https://github.com/madsbuch/scalable-ml-service\">GitHub Link</a>)</li>\n</ul>\n<p><strong>Writings:</strong></p>\n<ul>\n<li><strong><a href=\"/100-days-of-fibonacci\">100 Days of Fibonacci</a>:</strong> A project to explorer\naspects of thought and programming. The ambition is to hit the 100 days\neventually.</li>\n<li>\n<p><strong><a href=\"/\">This blog</a>:</strong> General writings. I am in particular proud of following\npieces:</p>\n<ul>\n<li><a href=\"/proving-stuff-in-haskell\">Proving Stuff in Haskell</a></li>\n<li><a href=\"/the-probability-monad\">The Probability Monad</a></li>\n</ul>\n</li>\n</ul>\n<p><strong>Legacy:</strong></p>\n<ul>\n<li><strong>Finansmaskinen:</strong> My very first startup. The landscape for accountance\nsoftware was not very developed. It developed a lot while I was on it, and\nI went on to other adeventures.</li>\n</ul>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/protocols-and-blockchains/","relativePath":"protocols-and-blockchains.md","relativeDir":"","base":"protocols-and-blockchains.md","name":"protocols-and-blockchains","frontmatter":{"title":"Protocols and the Blockchain","author":"Mads Buch","date":"2022-09-24","template":"post","excerpt":"Blockchains can guarantee that no votes are dropped. This is essential to have an honest voting system."},"html":"<p>The most exciting property of the blockchains is that they allow us to bake state\ndirectly into open, transparent protocols. This means that we can build\nprograms where everyone can, not just read the source code, but also trust\nthat the running program adheres to that source code.</p>\n<p>This means that we can build open protocols without needing to trust any single\nparticipant. Not just for its working, but also its state.</p>\n<p>So what types of protocols is this useful for?</p>\n<ul>\n<li><strong>Voting:</strong> Let participants vote on proposals, elections, etc.</li>\n<li><strong>Currencies:</strong> Transfer currency between people.</li>\n<li><strong>Validations:</strong> Participants validate that something empirical is there.</li>\n</ul>\n<p>The protocols listed above are special in the sense that they require two\nthings. 1) soundness of interaction: only I can vote or transfer money on my\nbehalf (unless I explicitly allow otherwise) and 2) completeness of\ninteractions: When I cast a vote or transfer money, it stays like that.\nBlockchain protocols allow developers to make protocols to adhere to these\nelements.</p>\n<h2>The Parts</h2>\n<p><strong>Stateful Protocols:</strong> Blockchains allow for states directly <em>on</em> the protocol.\nThis allows us not only to make sure that the state we have recorded is\nauthentic but also that no state has been lost.</p>\n<p>We have long been able to ensure the integrity of messages using cryptographic\nsignatures. What blockchains allow us is to record the completeness of messages\nby recording them in blocks that are committed and can not be altered.</p>\n<p>The canonical example is voting: Using traditional cryptography, we can ensure\nthat votes that are cast are authentic. But recording these with a single\nauthority would also allow votes to be dropped without anybody noticing.\nBlockchains can guarantee that no votes are dropped. This is essential to\nhave an honest voting system.</p>\n<p><strong>Transparency:</strong> Open source software solved the source-code transparency.\nFor all public projects on GitHub, we can see their source code. But that does\nnot guarantee that the code the computers run is the same as the source code.</p>\n<p>Blockchains solve this problem by distributing the source code to all nodes\nthe blockchain is running on. Every time somebody interacts with the\non-chain program all nodes run the code and verify they get the same result.</p>\n<p><strong>The support DAO:</strong> Programs need upgrades. If the program runs a currency,\nwe might need to change the interest rate. If it runs a provenance protocol\nwe need to be able to change provenance events when a new consensus is achieved.\nTo do this, protocols set up their own DAOs or distributed autonomous\norganizations, which are the stewards of the protocol. They discuss and decide\non the changes to go in. How DAOs work is different from DAO to DAO. Currently,\nthey tend to use a governance token that can be used for voting and is\ntradable on a secondary market.</p>\n<!-- **The incentive structure:** -->\n<h2>Final Thoughts</h2>\n<p><strong>Centralizedness of the blockchain:</strong> Folks raise concerns about\nto what extent blockchains are centralized. There have been several examples\nof blockchains that can be controlled by a single entity.\nThis is a huge concern for open protocols when transactions are\nbeing rolled back. But this is also an isolated concern. The blockchain\nis the technical infrastructure and can be changed. Ie. these concerns should\nnot be a game-stopper for the development of this technology. However,\nit should be considered another problem that is being solved as we speak.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/proving-stuff-in-haskell/","relativePath":"proving-stuff-in-haskell.md","relativeDir":"","base":"proving-stuff-in-haskell.md","name":"proving-stuff-in-haskell","frontmatter":{"title":"Proving Stuff in Haskell","author":"Mads Buch","date":"2016-10-27","template":"post"},"html":"<p><em>Main point: Haskell has the capacity to carry out proofs as languages.</em></p>\n<p><em>updated april 2021 for clarity.</em></p>\n<p>This article is my give on the relationship between mathematical proofs and\nprogramming languages. Many details on specific implementation have been\nleft out with the aim for clarity and conceptual coherency.</p>\n<p>The source used in this article is available as\n<a href=\"https://gist.github.com/madsbuch/12043c4ad1c1fd0a80008ffb443e29d7\">a Gist</a>.</p>\n<h2>Proofs and Programming</h2>\n<p>What is a proof? A proof is a series of deductive arguments, such that the\nproposition is justified. This description might seem quite\nabstract, so let us look at a concrete example using the Peano naturals for\nrepresenting natural numbers (here written in number syntax).</p>\n$$\n    1+1 = 2\n$$\n<p>As the proposition has no quantifiers, we can directly begin to reduce on\ndefined operations. For Peano numerals addition (+) is well defined and we\ncarry out steps as long as it is possible. In the end we should arrive at\nsomething akin to</p>\n$$\n    2 = 2\n$$\n<p>We still have a mathematical object, a propositional claim.\nWe know from the Peano axioms, that\nsyntactical equivalence satisfies reflexivity, symmetry, and transitivity.\nHenceforth the properties of equality are satisfied, and we end our\ndeductive sequence. QED.</p>\n<p>This can be translated into programming utilizing the type system to verify that\nwe are not \"cheating\" on deductive arguments. This translation has basis in\nwhat known as the <a href=\"https://en.wikipedia.org/wiki/Curry%E2%80%93Howard_correspondence\">Curry–Howard correspondence</a>.\nFrom that we know that propositions corresponds to types and proofs corresponds\nto programs. Alright, so we need to make an expression that has above\nproposition as its type, and an implementation that satisfies the type.</p>\n<h3>In Haskell</h3>\n<p>First, we need to define our objects: Peano naturals and Equality. We implement\nPeano naturals the usual way. I elaborate on this in a\n<a href=\"/100-days-of-fibonacci-day-9-haskell-types/\">previous post</a>.\nEquality is defined as follows.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">data :~: where</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  Refl :: a :~: a</span></span></code></pre>\n<p>We can see that the value <code>Refl</code> is the only inhabitant of the type <code>a :~: b</code>.\nFurthermore, it only inhabits the type when <code>a</code> and <code>b</code> are identical.\nConcerning the Curry-Howard correspondence, the <code>Refl</code> value also\nhas the unit type - it is not possible to attach further data to\nthe constructor.</p>\n<p>We have defined equality in terms of reflection. However, we need equality\nalso to satisfy symmetry and transitivity. In the source, we have the code\nneeded for that.</p>\n<p>We now want to make the type for our proof, or, the equivalent to the\nproposition stated above:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">onePlusOneEqualsTwo :: Add (S Z) (S Z) :~: S (S Z)</span></span></code></pre>\n<p>The <code>Add</code> in the type definition is a type family defined in the source. It is\ndefined as we would usually define addition over Peano naturals.</p>\n<p>To prove it we need to make an inhabitant to that type. The program\nis very simple\nfor this case as the Haskell compiler reduces the type level expression\nper semantics of type families.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">onePlusOneEqualsTwo = Refl</span></span></code></pre>\n<p>As it compiles<sup id=\"fnref-compiler\"><a href=\"#fn-compiler\" class=\"footnote-ref\">compiler</a></sup> it shows that Haskell is content with the proof.</p>\n<h2>Reusing Proofs</h2>\n<p>Reusing proofs seems  like a convenience, though it is completely\nnecessary for some classes of proofs. These are namely the\n<em>proofs by induction</em> which we will have a look at later. For this to work\nwe need to be able to lift a proof into Haskell's type solver.</p>\n<p>We do that by the <code>gcastWith</code> operator. This operator has the type\nsignature <code>gcastWith :: a :~: b -> (a ~ b => r) -> r</code>. It should be read\nas something like: Given a proof that <code>a</code> equals <code>b</code>, use that fact to decide\nif the type <code>r</code> is solvable and return the proof for <code>r</code>.</p>\n<h2>Quantifiers</h2>\n<p>We want to abstract our proofs. In proving terminology, we do this through\nquantifiers.</p>\n<p>To have a more graspable problem that includes only quantification,\nwithout induction, we detour to boolean algebra. Here we can\ntry to formalize De Morgan's theorem:</p>\n$$\n    \\forall a, b \\in : \\lnot( a \\land b ) = \\lnot a \\lor  \\lnot b\n$$\n<p>here <em>a</em> and <em>b</em> can only assume two values, <em>true</em> and <em>false</em>.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">deMorgan :: SBool a -&gt; SBool b -&gt; Not (And a b) :~: Or (Not a) (Not b)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">deMorgan STrue STrue   = Refl -- The first case</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">deMorgan STrue SFalse  = Refl</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">deMorgan SFalse STrue  = Refl</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">deMorgan SFalse SFalse = Refl</span></span></code></pre>\n<p>We simply provide an inhabitant to the type based on\npattern matching. This is the same as proving by case analysis.</p>\n<p>To understand what goes on we instantiate the type expression\nin each case. Afterward, the compiler reduces per the semantics\nof the <code>Not</code>, <code>And</code>, and <code>Or</code> type families. In the first case we instantiate\nthe type expression such that.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">deMorgan :: SBool Tru -&gt; SBool Tru -&gt; Not (And Tru Tru) :~: Or (Not Tru) (Not Tru)</span></span></code></pre>\n<p>This instantiation is from the value <code>STrue</code> which has the type <code>SBool Tru</code>.\nThe compiler then reduces the expression and derives that\n<code>Refl :: Fls :~: Fls</code>. From the interpreter, we get that.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"text\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">*Proof&gt; :t deMorgan STrue STrue</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">deMorgan STrue STrue :: &#39;Fls :~: &#39;Fls</span></span></code></pre>\n<h2>Proof Reuse</h2>\n<h2>Induction</h2>\n<p>Many interesting properties we want to reason about includes unbound data.\nThat is, the data we think about is inductively defined. We now go back to the\nexamples considering natural numbers as they are a good medium for\ndiscussing inductively defined data.</p>\n<p><code>plus_id_r</code> is the property that adding zero to\n<em>n</em> on the right side is the identity of <em>n</em>. <code>plus_id_l</code> is when we add 0\non the left side.</p>\n$$\n    n+0 = n \\ \\text{(plus_id_r)}\n$$\n$$\n    0+n = n \\ \\text{(plus_id_l)}\n$$\n<p><code>plus_id_l</code> is given directly from our definition of addition. But <code>plus_id_r</code>\nneeds to be proven, and we can do this inductively using following code.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"hs\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">plus_id_r :: forall n. SNat n -&gt; (Add n Z) :~: n</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">plus_id_r Zero = Refl</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">plus_id_r (Succ x) = gcastWith (plus_id_r x) Refl</span></span></code></pre>\n<p>The first case is the base case. We know that the value is <code>Zero</code> and\nhence we can derive the type to <code>Z</code>. It is immediately visible that <code>Refl</code>\ninhabits the type <code>Refl Z Z</code>.</p>\n<p>The next case is the induction case. Here we fold out the value such that if\n<code>n = Succ x</code>, then <code>x = n-1</code> - we reduce this on our argument. We justify that\n<code>Refl</code> also is an inhabitant in this case by calling <code>plus_id_r</code> on the\nreduced value.</p>\n<h2>Discussion</h2>\n<p>It is indeed possible to prove stuff in Haskell. But it is not further\npractical. The reason is in particular because the language\nis not designed with\nthe constructions we need, such as dependent types. We simulate\nthem through GADTs.</p>\n<p>The closest languages to Haskell that are suited for\nthis is languages such as Idris and Gallina (Coq). They have all the facilities\nneeded for incorporating proofs into one's code.</p>\n<p>If one has a software\ndevelopment background firmly grounded in OOP (Java, C#), it requires quite\nsome time to wrap one's head around the new way to understand types.</p>\n<p>That we can do above is mostly of academic interest: How do make <em>sure</em> that\ncertain compilers indeed do what they should do etc. But the techniques are\nbecoming steadily more accessible to all programmers.</p>\n<p>New languages, like Idris, come with type constructions to formally\nreason about our software.</p>\n<h2>Final Remarks</h2>\n<p>First, thanks to <a href=\"http://askarov.net/\">Aslan Askarov</a> for providing\nvaluable feedback on this article. It has been incorporated to provide a\nmore coherent article.</p>\n<p>This article was a precursor for a presentation at a\n<a href=\"http://www.sparetimeteaching.dk/about.php\">spare time teaching</a> event\nat Aarhus university. Thanks to them for letting me host the presentation.</p>\n<p>Alexandros Peitsinis has written a much more in-depth article on this subject.\nIf this article intrigued then take a look at <a href=\"https://alexpeits.github.io/posts/2018-09-27-haskell-proofs.html\">his work</a>.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>\n<div class=\"footnotes\">\n<hr>\n<ol>\n<li id=\"fn-compiler\">\n<p>Well, we need to set some compiler flags to make sure that all cases are covered.</p>\n<a href=\"#fnref-compiler\" class=\"footnote-backref\">↩</a>\n</li>\n</ol>\n</div>"},{"url":"/questions-and-user-stories/","relativePath":"questions-and-user-stories.md","relativeDir":"","base":"questions-and-user-stories.md","name":"questions-and-user-stories","frontmatter":{"title":"Questions and User Stories","author":"Mads Buch","subtitle":"On ensuring stakeholder alignment for data scientist","excerpt":"A question is to a data scientist what a user is to an application developer.","date":"2020-12-13","template":"post"},"html":"<p>Questions are to Data Science what User Stories are to Applications. That's it.</p>\n<p>When developing user-turned applications, user stories is a tool to make sure\nthe implemented user interface is aligned with the users expectations.\nThey focus the development towards what is important and allows to filter\nout tasks that are not supporting the user stories.</p>\n<p>The same is the case for data science: A team puts forward a number a questions.\nSome grand questions, and some smaller one that lead to the answer to the\ngrand one. Having those questions in mind always aligns the data scientist\nwith their stakeholders.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/reliability-engineering-in-startups/","relativePath":"reliability-engineering-in-startups.md","relativeDir":"","base":"reliability-engineering-in-startups.md","name":"reliability-engineering-in-startups","frontmatter":{"title":"Reliability Engineering in Startups","author":"Mads Buch","date":"2024-03-09","template":"post"},"html":"<p><em>Draft</em></p>\n<p>I am currently working in a startup where a part of the value proposition is a resilient product.\nThe product is used in an interactive setting between a seller and a buyer and manages compliance requirements in this phase.\nAs such we can not afford to be down in this phase.</p>\n<p>Startups are typically characterized by being resource constraint such that all team memebers need to do diverse tasks.\nFurthermore, the requirements tend to chance fast</p>\n<h2>The 6 Shields in Resilient Software</h2>\n<p>We have, at least, 6 different shields we can use when considering reliability.\nEach of the shields will defend certain attacks an adversary can have on the product.</p>\n<p>The shields are as the following</p>\n<ul>\n<li>Types</li>\n<li>Tests</li>\n<li>Architecture</li>\n<li>Requirements</li>\n<li>Monitoring</li>\n<li>Processes</li>\n</ul>\n<p>For each of these shields a team can decide to invest more or less resources into it.</p>\n<p>In startups the key is to manage the amount of effort that foes into each shield.\nThe resources allocated should be proportional to the requirements from the business.\nNot more, not less.</p>\n<h2>Choose your Tier</h2>\n<p>For each shield there is the free tiers, the paid tier and the enterprise tier.</p>\n<p>The free tier is where it takes the same amount of resources to do it as to not do it - given a software developer that has basic training.</p>\n<p>The paid tier is where certain rules are applied in shield as a response to certain issues that has been observed.</p>\n<p>The enterprise tier it to dogmatically use state of the art techniques or to go above basic requirements in order to solve large classes of issues that has never been experiences.</p>\n<p>For the typing shield: The free tier is to use a types programming language and stick to its typing primitives.</p>\n<p>The paid tier is to model business logic in types to the extend that this removes issues that has been seen in production.</p>\n<p>The enterprise tier for typing would be to use special languages or DSLs to model parts of the domains in order to ensure that incorrect code can not be deployed.</p>\n<ul>\n<li>\n<p>Types</p>\n<ul>\n<li>Free: Use typed language.</li>\n<li>Paid: Model business requirements in types.</li>\n<li>Enterprise: Use DSLs or delegate code to strongly types languages.</li>\n</ul>\n</li>\n<li>\n<p>Tests:</p>\n<ul>\n<li>Free: Inline asserts or limited testing framework.</li>\n<li>Paid: Maintain test coverage at a set level.</li>\n<li>Enterprise: Implement multiple types of tests ranging from asserts, unit tests, end to end tests, etc. and maintain coverage targets for each.</li>\n</ul>\n</li>\n<li>\n<p>Architecture</p>\n<ul>\n<li>Free: Project level consistency in coding patters.</li>\n<li>Paid: Architectural principles that relieves certain types of common errors.</li>\n<li>Enterprise: Follow well defined architectures for the system level.</li>\n</ul>\n</li>\n<li>\n<p>Requirements:</p>\n<ul>\n<li>Free: A whish list of features.</li>\n<li>Paid: Note down important requirements, for larger projects jut down requirements docs.</li>\n<li>Enterprise: Write complete requirement documentation with reviews before starting development.</li>\n</ul>\n</li>\n<li>\n<p>Monitoring</p>\n<ul>\n<li>Free: Write to console and check the provider metrics overview.</li>\n<li>Paid: Have explicit tools to manage logs and performance monitoring.</li>\n<li>Enterprise: Full fledges monitoring tools with on-call and complete alerting packages.</li>\n</ul>\n</li>\n<li>\n<p>Processes:</p>\n<ul>\n<li>Free: Have a todo list.</li>\n<li>Paid: Use some sort of task management tool, use releases as needed, etc.</li>\n<li>Enterprise: Full fledges process management using frameworks like Scrum and friends.</li>\n</ul>\n</li>\n</ul>\n<h2>When to Move Tiers?</h2>\n<p>Moving tiers is a response to two things: Resource allocation and need.</p>\n<p>The free tiers are typical for new startups or small projects with small teams.</p>\n<p>The paid tiers are typically a response a growing team or a requirement to develop features faster.</p>\n<p>The enterprise tier is called so because it is often in enterprises these are needed.\nThey are characterized by being resilient to employee churn. But they are also known to such out the lives of the team participants.</p>\n<p>The main thing is to understand the associated costs by moving up a tier.\nFor a lot of owners and projects managers in might seem to be beneficial to employ the enterprise tier on the process when starting new projects.\nHowever, the result will often be alienated teams with low performance and a lot of asks on cleaning up code.</p>\n<h2>The Pseudo Shield</h2>\n<p>Reliability issues can often be solved using various shields.\nA common example would be to use typing, but rely on some sort of manual testing to catch the errors occurring from the missing types.</p>\n<p>Process shields are typically easier to setup. They don't require changing code and their risk is low.\nFurthermore, these can typically be done by non- engineering members of the teams.</p>\n<p>The down side, however, is that a team can only lift so much process. So process mitigations ought to move into the other shields as time pass.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/reusing-insights-with-ai/","relativePath":"reusing-insights-with-ai.md","relativeDir":"","base":"reusing-insights-with-ai.md","name":"reusing-insights-with-ai","frontmatter":{"title":"Reusing Insights with AI","author":"Mads Buch","subtitle":null,"date":"2021-07-13","template":"post","excerpt":"..."},"html":"<p>You work in a big organization, thousands of suppliers, and tens of thousands of\ncontracts. Your goal is to save some bucks in the company. But how do you do\nthat? Start by reading one contract after another and see if there are new\nsuppliers with better deals? This requires an extensive toolbox!</p>\n<p>In the company, a project to find similarities for products is commenced. The\nidea is that we can figure out whether two products serve the same purpose\nif they result from the same need. When that is discovered, we can\ndecide to prioritize the cheaper option over the more expensive one.</p>\n<p>The project goes well. The algorithm is developed, a huge list of obsolete\nproducts is constructed and implemented in the procurement system. Now it will\nflag the cheaper option to the user every time the expensive one is requested.</p>\n<p>But was that it? Is the code for the product abandoned now? The final asset was\nthe code that recommends the cheaper option, but there is much more value\nto take home from the constructed code. Imagine that another team, later on,\nneeds to find some suppliers for a given product to support a need. The\norganization has the policy to get quotes from at least five suppliers to get the\nbest price. Now, they decide to set up an algorithm that\ncan find similarities for products to find related products to get a quote on.\nSomething a system to reuse existing insights could have saved.</p>\n<p>In order to reuse these insights, a knowledge graph based system could have\nbeen employed. The code written in the first project would have been made into\na pipeline that takes products already present in the knowledge base and sets\nup new relations based on their similarity. Eg.\n<code>Pen A -- canBeExchangedFor --> Pen B</code> or\n<code>HeadPhone A -- isSimilarTo --> HeadPhone B</code>.</p>\n<!-- Insert picture of the new layer here -->\n<p>This layer is committed to the layer repository for the organization such that\nwhenever the next team comes around and ask the system to generically find\nrelated suppliers, the system will utilize these product relations to make\nrecommendations.</p>\n<!-- Insert picture of -->\n<p>When the knowledge base is constructed correctly we can keep adding new insights\nwithout cluttering anything up. The knowledge base should realize a\nmulti-ontological view of the world such that it can reuse insights from one\ndomain in another domain without the system feels unstructured to work with.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/sample-code-guidelines/","relativePath":"sample-code-guidelines.md","relativeDir":"","base":"sample-code-guidelines.md","name":"sample-code-guidelines","frontmatter":{"title":"Sample Code Guidelines for a Typescript Project","template":"page"},"html":"<p>This is a set of demo code guidelines for a Typescript project. They are used to exemplify how I go about writing code guidelines.</p>\n<p>While Typescript is only rarely mentioned, each language have weaknesses and strengths. Code guidelines are a set of rules that in\nparticular mitigates the weaknesses of a language.</p>\n<ul>\n<li><a href=\"#code-locality\">Code Locality</a></li>\n<li><a href=\"#occams-razor-for-code\">Occam's Razor for Code</a></li>\n<li><a href=\"#prefer-records-over-lists-for-local-data-structures\">Prefer Records over Lists for Local Data Structures</a></li>\n<li><a href=\"#prefer-mutations-to-not-return-the-entity-in-question\">Prefer Mutations to Not Return the Entity in Question</a></li>\n</ul>\n<h2>Code Locality</h2>\n<p><strong>Context:</strong> The entire code base</p>\n<p><strong>Advice:</strong>\nPrefer to keep code close to where it is used. Eg, when writing a utility\nfunction that turns an enum \"FINANCE\" into the string \"Finance\" refrain\nfrom putting that into a utility code. Instead put it in the same file\nwhere it is used.</p>\n<p>Some qualification of this rule:</p>\n<ol>\n<li>If the file is big, create a <code>utility.ts</code> (or <code>_utility.ts</code> in the page hierarchy)\nand save it there.</li>\n<li>\n<p>If the semantically equivalent function is need somewhere else in the code, find\na place that is between these place.</p>\n<ul>\n<li>If this is in two apps, put it in a package</li>\n<li>If that is on two pages, put it in the junction of the hierarchy</li>\n<li>etc.</li>\n</ul>\n</li>\n</ol>\n<h2>Occam's Razor for Code</h2>\n<p><strong>Advice:</strong> When there are two competing solutions that solve the same problem. Choose the simplest one.</p>\n<h2>Prefer Records over Lists for Local Data Structures</h2>\n<p><strong>Context:</strong> Local and complex data structures used for rich user interaction.</p>\n<p><strong>Anti-context:</strong> Database models, API data structures.</p>\n<p><strong>Advice:</strong>\nPrefers using sub objects indexed using a intrinsic ID of each elements.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"ts\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">[</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  { </span><span class=\"mtk12\">id:</span><span class=\"mtk1\"> </span><span class=\"mtk17\">&quot;abc&quot;</span><span class=\"mtk1\">,</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk12\">elements:</span><span class=\"mtk1\"> [</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">      { </span><span class=\"mtk12\">id:</span><span class=\"mtk1\"> </span><span class=\"mtk17\">&quot;123&quot;</span><span class=\"mtk1\">,</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk12\">field:</span><span class=\"mtk1\"> </span><span class=\"mtk17\">&quot;value&quot;</span><span class=\"mtk1\"> }</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    ]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  }</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">];</span></span></span></code></pre>\n<p>vs.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"ts\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">{</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&quot;abc&quot;</span><span class=\"mtk1\">: {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk12\">id:</span><span class=\"mtk1\"> </span><span class=\"mtk17\">&quot;abc&quot;</span><span class=\"mtk1\">,</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk12\">elements:</span><span class=\"mtk1\"> {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">      </span><span class=\"mtk17\">&quot;123&quot;</span><span class=\"mtk12\">:</span><span class=\"mtk1\"> {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk12\">id:</span><span class=\"mtk1\"> </span><span class=\"mtk17\">&quot;123&quot;</span><span class=\"mtk1\">,</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span><span class=\"mtk12\">field:</span><span class=\"mtk1\"> </span><span class=\"mtk17\">&quot;value&quot;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">      }</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    }</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  }</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span></code></pre>\n<p>The former requires complex iteration if we want to update the value in a type safe manner while\nthe latter requires <code>v[\"abc\"].elements[\"123\"].field = \"New Value\"</code>.</p>\n<p><strong>Extra notes:</strong></p>\n<ul>\n<li>If we need a list, we can always <code>Object.values(v)</code> and we are back!</li>\n<li>If there are no intrinsic ID, generate one using <code>UUIDv4</code></li>\n</ul>\n<h2>Prefer Mutations to Not Return the Entity in Question</h2>\n<p><strong>Context:</strong> API development (GraphQL here)</p>\n<p><strong>Advice:</strong> Prefer that API that POST or Mutate data does not return the entity in question.</p>\n<p><strong>Background:</strong> Systems at scale takes time to propagate change. This is usually not long time\nbut enough that we don't want to wait for it being written to the database before we can respond\nin the API.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .mtk1 { color: #000000; }\n  .default-light .mtk12 { color: #001080; }\n  .default-light .mtk17 { color: #A31515; }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/planning/","relativePath":"planning.md","relativeDir":"","base":"planning.md","name":"planning","frontmatter":{"title":"When there is nothing to do, there is planning to do","author":"Mads Buch","date":"2022-01-06","template":"post","excerpt":"Maybe the first item on the ignition list is to build the ignition list?"},"html":"<blockquote>\n<p>Let all your effort be directed to something, let it keep that end in view.\nIt is not activity that disrupts people, but false conceptions of things that\ndrive them mad.</p>\n</blockquote>\n<p>– Seneca, on tranquility of life, 12.5</p>\n<p>One of the hardest problems of building something large is reducing it to\nmanageable bits and pieces that can be made in the time between meetings for\nactual work, or in the after-hours for side gigs and spare time projects.</p>\n<p>One thing that often puts off people working is not having any of these\nmanageable bits to finish up. Another thing that can put of working on a\nproject is being at the mercy of the road map already planned 10\nyears ahead.</p>\n<p>It’s a balancing act.</p>\n<p>One thing that is for sure is that important projects need traction.\nThey need regular attention, also when everything else in life comes\ncrashing down. When a project gets attention, it is so much easier to get\ncaught up in the project the days when the stars align and we get into the flow.</p>\n<p>Maybe the trick is to have an “ignition list”. A list of small pieces of work\nthat can be done in half an hour or so. For the stressful days, this is merely\nwhat is done. For the other days, these items are what ignites the flow process.</p>\n<p>It is kind of leaving a small fixable syntax error in the code overnight.\nThis ensures something easy to get started with in the morning bootstrapping\nthe day.</p>\n<p>Maybe the first item on the ignition list is to build the ignition list?</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/scalable-ml-service/","relativePath":"scalable-ml-service.md","relativeDir":"","base":"scalable-ml-service.md","name":"scalable-ml-service","frontmatter":{"title":"Scalable Machine Learning Service","author":"Mads Buch","subtitle":"To deploy large-scale machine-learned models, we need to be careful about our architecture.","date":"2021-04-05","template":"post","excerpt":"Machine learned models can be really big, like the multi-billion weight GPT models; there is a chance they contain sensitive data, and their output needs to be sanitized or something third. This post is on the fundamentals of a scalable ML architecture."},"html":"<!-- First, a word of caution. Before this is even released it might be obsolete.\nA critique of the churn in software technologies merits a book in itself. -->\n<p>Machine learning is becoming ubiquitous. More and more services rely on\nit, and while embedded systems increasingly can run the models\nthemselves, there are good reasons to execute models in the cloud. Models can\nbe huge, like the multi-billion weight GPT models; there is a chance\nthey contain sensitive data, and their output needs to be sanitized or something\nthird. This post is on the fundamentals of a scalable ML architecture.</p>\n<p>As a demo, I have implemented the core of a scalable system to build on online\nAPI to execute a model. The architecture is relatively straightforward: We use an\nElixir frontend, which is expected to carry out responsibilities like\nauthentication etc. We use RabbitMQ as a queue system and a Python service\nto execute the models.</p>\n<h1>The Walk Though</h1>\n<p>First, the code is available at <a href=\"https://github.com/madsbuch/scalable-ml-service\">GitHub</a>.\nPrincipally, there are three components to pay attention to: The Elixir GraphQl\nAPI server, the Python model runner, and the infrastructure defined in\nrespective docker files and the <code>docker-compose.yml</code>.</p>\n<p>The application server is a standard Elixir Phoenix setup using\nAbsinthe to handle the GraphQl interface and AMQP to handle the interface\nto the RabbitMQ message broker. One of the things to keep in mind for services\nthat are a part of a more extensive setup is that we need to develop it and test it\nin isolation. Hence the module that implements functions for RPC over RabbitMq\nid mocked and has an <em>IO</em> implementation that merely echoes what it gets. This\nyields a solution where the only code that can not be tested on the service\nlevel is the production implemented of the RPC module, which is sufficient.</p>\n<p>We use a machine-learned model written in Python using PyTorch. The example\ncode is moved over and augmented by the glue, making it responsive\nto new requests in the queue.</p>\n<p>Lastly, everything is orchestrated using docker-compose. this includes\n<code>Docker</code> files for each project and a <code>docker-compose.yml</code> file to start\neverything.</p>\n<h1>Concerns</h1>\n<p>When developing complex solutions, there are a plethora of concerns to be\nthoughtful about. These are simply some of them.</p>\n<p><strong>API design:</strong> The API has a single query to translate. This choice is mainly\nfor convenience and can certainly be challenged. We would probably use mutations\nfor ML model invocations as they have sufficient side effects. For some models,\nit might return different outputs per invocation, and for all cases, it incurs\na const to invoke why we probably want to avoid letting clients re-invoke\nseveral times as retry strategies.</p>\n<p><strong>Indirection:</strong> Using a queue system introduces indirection. Signals are sent,\nand we wait for the return. These systems do not have any runtime call stack,\nand it is even less so possible to follow a call in the code editor. Ie.\nIndirection makes code harder to read. Use indirection sparingly and only when\nthere is a good reason.</p>\n<p><strong>Scaling:</strong> The core decisions have been made for this architecture\nto scale. We can freely add as many model runners to the system as we see fit.\nA natural next step would be to add auto-scaling to the model runners in order\nto respond fast in peak time and save money when demand is low.</p>\n<p><strong>Models:</strong> In this example, the model weights have simply been added\nto the docker image. If the model is updated, it is an orchestration concern\nto make sure that the running containers are updated. This should be as\nstraightforward as just starting new containers and signal the old ones to shut down.</p>\n<p>However, if we have large enough churn on the models, say we have individual\nmodels per user, we probably need another strategy. Depending on the use case,\nwe might let all the models run in parallel and have multiple topics in\nRabbitMQ or we can download models as files on demand and cache them.</p>\n<h1>Final Thoughts</h1>\n<p>This implementation is representative and by no means production-ready.\nA lot of tweaking is needed to be done. However, it shows the basic\nstructure for services that has a disconnect between resource-intensive parts\nand light resources.</p>\n<p>When developing these types of infrastructures, it is <em>expected</em> that they will\nchange when exposed to real-life usage patterns. Furthermore, this simple\nexample touches on many different technologies. In a scale-up scenario, this\nsystem would probably be managed by at least three people: the data scientist/engineer\ndeveloping the model runner, an application engineer developing the API\nservice, and somebody with the infrastructure as their primary concern.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/spor/","relativePath":"spor.md","relativeDir":"","base":"spor.md","name":"spor","frontmatter":{"title":"Spor","template":"page"},"html":"<p>Spor was a, now closed, knowledge base startup.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/tech-debt-tax/","relativePath":"tech-debt-tax.md","relativeDir":"","base":"tech-debt-tax.md","name":"tech-debt-tax","frontmatter":{"title":"Tech Debt is Tech Tax","author":"Mads Buch","date":"2023-06-17","template":"post"},"html":"<p>There is this saying about tech debt: When you move fast in a software team,\nyou build up debt that you must pay off later. The engineering team uses this\nto get a break and align the current understanding of the product with the code.</p>\n<p>The TL;DR of this post is that it is often inaccurate to talk about tech debt.\nInstead, we should consider the tax a project accrues –\nA self-incurred tax based on the decisions and culture of an organization.</p>\n<h2>Tech Debt</h2>\n<p>Traditionally, tech debt is understood as the \"short-cuts\" an engineer makes\nto reach a solution. Sometimes these are actually short-cuts, but rarely.\nThere is usually a deeper meaning to this introduction of tech debt and it can\nbe divided into the following categories:</p>\n<ol>\n<li><strong>Architectural incoherence:</strong> New code does not follow the architectural\ncontracts for the code base. The reason is typically that the architecture\ncan't handle the proposed change, or the engineer is not yet trained in how\nto make the changes within the architectural confines. The consequence, or\n\"interest payment,\" is lower productivity.</li>\n<li><strong>Low performance:</strong> A feature's performance is sub-par. This can also be due to\nan operational discrepancy, ie. the infrastructure needs change. The reason\nis typically an attempt to make complex code more verbose or because\nthe optimal solution was not yet found. The consequence is reduced\nscalability for the system.</li>\n<li><strong>Requirement discrepancy:</strong> The feature does not implement the proposed design\nor requirements in full. The reasons are typically that the requirements or\ndesign requires for time than dedicated to the feature. For me, it is\ntypically because of injustification of the time spend and the value provided.</li>\n</ol>\n<p>These elements are all completely natural and will happen in all software\nprojects regardless of size. However, I think this notion of debt is flawed.\n\"Debt\" can be fully settled however \"tech debt\" can not.\nDebt has a single-dimensional target: to reach 0 in owed funds. The above issues\ndoes not.</p>\n<p>Though the word debt somewhat works, I think the notion of a tech-tax is more\nfitting for a number of reasons:</p>\n<ol>\n<li>It is an organization decision how much resources it wants to spend towards\ntaxes.</li>\n<li>It is inherently on-going.</li>\n<li>It is understood as necesaarry but also something that inhibits business\ngrowth</li>\n</ol>\n<h2>The Engineering Tax</h2>\n<p>Software engineers need to prioritize tasks that does not yield business value.\nThese are a tax. But how is the tax decided?</p>\n<p><strong>Architecture tax:</strong> In some teams, members have a high variance\non architectural aspiration leading to architectural incoherency.\nSome teams can handle spaghetti code while others struggle to even release\nminor updates or onboard new engineers. They should pay more in taxes on the\narchitectural incoherence.</p>\n<p>A team should allow incoherence and learn from it.\nI spend some hours a week to remove unused code, and do various refactorings.\nBut to what extend this should be done is teams specific? Some teams prefer\nmore individual freedom while others prefer stronger rules on the architecture.\nKnowing what the architectural target is, is a good start, without being\ndogmatic about implementation.</p>\n<p><strong>Infrastructure tax:</strong> The infrastructure is necessary as a software\nproduct does not run without it. But there is a vast difference in running\na static website on Netlify and setting up an entire cluster using\nTerraform and Kubernetes. However, a company is forced to pay the\ninfrastructure tax when the product is being used. The more usage,\nthe higher tax.</p>\n<p>In my experience, to manage the infrastructure tax, the infrastructure should\nbe implemented for realistic usage (as opposed to ideal or aspirational).\nThere are no reasons to build out\na microservice architecture before it has been proven that there is a need. Make\na monolith. When the product is successful, there are usually also more funds\nto pay extra on this tax.</p>\n<p><strong>Requirement tax:</strong> Requirements change. The app changes. Everything changes.\nMaking sure that all new and existing features live up to requirements, and\nchanging requirements, imposes a tech tax. In particular in larger organizations\nwith dedicated QA teams to discover these discrepancies.</p>\n<p>To counter this tax, take one step back and look at who you are developing for\nand what you are developing. Are satisfying requirements becoming a bureaucratic\nexercise? Or does it solve actual business needs? Product leads should appreciate\nwhen engineers push back on features or requirements. Take the time to really\nunderstand why a requirement is important.</p>\n<h2>Taxes are ... Taxing</h2>\n<p>Most software engineers get exhausted by doing things that do not move the\nneedle. Spending days implementing an animation widget that is seen by only\na handful of people. Maintaining a Kubernetes cluster with less than a single\ndaily visitor. Not only are these bad business expenses, but they are also\nhard for the software engineer.</p>\n<p>People tend to enjoy meaningful work more. Managing tax will yield a better\nproduct for less cost at higher flexibility and higher velocity.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/tech-lead-philosophy/","relativePath":"tech-lead-philosophy.md","relativeDir":"","base":"tech-lead-philosophy.md","name":"tech-lead-philosophy","frontmatter":{"title":"Tech Lead Philosophy","template":"page"},"html":"<p>This document outlines my philosophy on technical leadership. Its purpose is twofold:</p>\n<ol>\n<li>Establish a methodology I use for technical leadership (why it is written).</li>\n<li>Establish accountability for my technical leadership (why it is public).</li>\n</ol>\n<h2>Principles</h2>\n<h3>Acceptance</h3>\n<p>Not everything is always perfect. It's entirely okay for not all aspects of the project to be constantly perfect. Priorities are set in collaboration with stakeholders, and aspects of the project that do not directly align with key objectives should be accepted or validated against the priorities.</p>\n<p><strong>Case:</strong> Instability:</p>\n<p><strong>Problem:</strong> The platform is unstable.</p>\n<p><strong>Business Objectives:</strong> Feature velocity.</p>\n<p><strong>Approach:</strong> Initially, it must be accepted that the platform is not stable. However, it should constantly be validated whether the instability hinders business objectives of feature velocity. If so, the platform stability should be adjusted to a state where it no longer obstructs the objective, but no more than that.</p>\n<p><strong>Stakeholder Alignment:</strong> We don't have a stable solution as long as this is not prioritized. It can be prioritized, but it will affect other objectives or require more resources.</p>\n<h3>Direction</h3>\n<p>Leading a project is about setting direction. For example, if there are inappropriate constructions in the code, more suitable patterns should be devised. The new patterns should be documented, disseminated, and integrated.</p>\n<p><strong>Case:</strong> Component Properties</p>\n<p><strong>Problem:</strong> Bugs often occur because properties are not set on components. Our data model allows a lot of data to be optional, and a decision was previously made to accept optional properties on components deeper in the hierarchy.</p>\n<p><strong>Approach:</strong> Find a pattern that eliminates the error vector inherent in having optionality. This can be done by creating a sum type that either takes the optional value or a \"value does not exist\" value.</p>\n<p><strong>Stakeholder Alignment:</strong> Co-developers should know that this is the construction we will use in the future. It should be clear that if a property on an object is not optional, this pattern should be used.</p>\n<h3>Compassion</h3>\n<p>Software development is a journey. Decisions have previously been made consciously or not, and oftentimes we see the effects later when the decision has had severe impacts. However, it is important to understand the grounds for the decisions that have been made. This should be done in a way that evokes no defense. It is important to be conscious and compassionate about the situation and open-minded about the reasons.</p>\n<h3>Ownership</h3>\n<p>Each team member generally own their own code. This is something to be proud of and something to be responsible for.</p>\n<h3>Anti-Dogmatism</h3>\n<p>Not all projects should be led in the same way. A significant part of managing a project is identifying gaps and filling them. In particular, these gaps are highly dependent on the resources available to a project and the objectives of the projects.</p>\n<h2>Methods</h2>\n<p>The landscape of methodology is large. Some work well in certain teams and contexts. I have had success using the following methods:</p>\n<h3>Use proposals and RFCs</h3>\n<p>...to work on ideas from inception to buy-in.</p>\n<ul>\n<li>Proposals provide agency to each individual team member as a tool to participate in a debate that transcends development.</li>\n<li>Proposals are more flexible than stories/epics and allow recording of less condensed ideas.</li>\n<li>Proposals are particularly well-suited for remote situations where they record information that is mostly just discussed.</li>\n</ul>\n<h3>Have Code Guidelines</h3>\n<p>Writing code is like playing the piano. A pianist does not consider each individual key strokes but rely on practiced chord progressions.</p>\n<p>In software development team, nobody gets to be Beethoven. Instead we share and maintain a list on what good chord progressions are - the code guidelines.</p>\n<p>When writing software the importance of rule-adherence is as the following:</p>\n<ol>\n<li>Type safety: Important, non negotiable.</li>\n<li>Linting rules: Can be circumvented when they obstruct a better way to write code.</li>\n<li>Code guidelines: Lists out preferred way to deal with situations in an abstract way.</li>\n</ol>\n<p>Code guidelines take into account that there might be certain circumstances where this does not make sense.</p>\n<p>I work with code guidelines are demonstrated <a href=\"/sample-code-guidelines\">in my sample code guidelines for a typescript project</a>.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/statistics-knowledge-graph/","relativePath":"statistics-knowledge-graph.md","relativeDir":"","base":"statistics-knowledge-graph.md","name":"statistics-knowledge-graph","frontmatter":{"title":"Proposal - Statistics on the Knowledge Graph","author":"Mads Buch","subtitle":"Execute probabilistic programs on the knowledge graph.","excerpt":"If we are a bit careful about provenance, we can view a knowledge graph as the virtual machine for a probabilistic program with the ontology defining the semantics.","date":"2021-06-27","template":"post"},"html":"<p>TL;DR: If we are a bit careful about provenance, we can view a knowledge graph as the virtual machine for a probabilistic program with the ontology defining the semantics.</p>\n<p>The fundamental idea is that we can write programs in a simple probabilistic programming language. As such, we require the probability monad as the only composition pattern for the language. We allow functions, written as <em>distributions</em>, to draw from themselves to yield the possibility of traversing inductive structures. In the end, we wite distributions over entities.</p>\n<p>We provide an <em>expectation</em> function. This function takes a distribution and a measure function and returns the average measure over the distribution. An example could be on companies, and the measure function would yield 1.0 when the company is bankrupt and 0.0 when it is not. The result would yield the weighted proportion of bankrupt companies in the distribution.</p>\n<p>Build-in operations are based on the <em>meta ontology</em>. Here we define what values properties of an ontology can take. An example is a <em>text</em>-field. This will have an <code>equality</code> function. Another property could be a <em>location</em>-field. These could have an <code>in-range</code> operator such that we can write <code>in-range (location company) (location address) 50m</code> or the like.</p>\n<p>We augment the system with two basic distributions — one for the ontology classes and one for entities. The first one yields random elements at uniform distribution from a class, the second one yields exactly and only the entity from which it has been constructed — It is the singleton distribution.</p>\n<p>Furthermore, relations between classes in the ontology represent functions from distributions to distributions. A scenario is when we have a knowledge graph representing companies with a <code>hasEmployee</code> relation liking companies to persons. The language would provide a function <code>hasEmployee : Dist Company -> Dist Person</code>, which from a distribution over companies returns a distribution over persons.</p>\n<p>Lastly, error handling: Distributions might be empty. Handling those empty distributions in different ways can have a tangible impact on the resulting distributions, and we need to be careful about how we do it. In the initial version of the language, we handle these situations explicitly in order to surface decisions.</p>\n<p>Outside of the core language, there are some concerns we also need to address in the execution system. One such is provenance. The statistical results are only as precise as the data program is run on. A library is made available to create the dataset (execution runtime). This library comes with many tools to ensure the soundness and completeness w.r.t. to the data source and ways to indicate the soundness and completeness.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/the-mvp-engineer/","relativePath":"the-mvp-engineer.md","relativeDir":"","base":"the-mvp-engineer.md","name":"the-mvp-engineer","frontmatter":{"title":"The MVP Engineer","author":"Mads Buch","subtitle":"An engineer creating minimum viable products.","excerpt":"An MVP engineer helps scope a product, build the product to verify its business potential, set up a team and lastly scope a road map for further product development.","date":"2021-02-23","template":"post"},"html":"<p><em>Main point: The MVP engineer is a part of the initial team who scopes out a product, builds the first iteration of the product to verify its \"raison d'etre\".</em></p>\n<p>A <em>minimum viable product</em> is a minimal implementation of an idea. An\nimplementation that can verify the value-add of the idea. This first\nimplementation should be cost effective while implementing the core features\nand functionality required to decide whether to continue on developing the\nproduct or to reject the hypothesis.</p>\n<p>The MVP engineer is one of the core people on the team making this happen. This\nperson undertakes the technical responsibility for this phase in the product\ncycle.</p>\n<p>Before the MVP engineer comes almost nothing. The only thing that precedes the\nMVP engineer is the decision that a product should be made and some vague\nideas on what kind of value the product should provide. The MVP engineer will assist\nin scoping initial versions and build them to validate the business potential.</p>\n<p>The MVP engineer is an expert in this initial phase of a product life cycle. </p>\n<p>Technically, the MVP engineer is a one-stop-shop and does what it takes to\nbuild the product: Application development, continuous integration, external\nintegrations, machine learning, smart contract development, etc. The person\npossesses all competences required to build this initial product that\nshowcases <em>viability</em>.</p>\n<p>Consequently, this also means not attending to a lot of other concerns. What\nconcerns these are are unique to the project. An example is scaling concerns\nthat are deferred to the point where the product scale is eminent.</p>\n<p>Another one is the architectural elegance, or architectural clarity.\nThe point is, that MVPs tend to pivot heavily in the initial phases.\nInvesting in the architecture at this points risks imposing\n<em>the sunk cost fallacy</em> – Nobody likes to throw out a big investment.</p>\n<p>The MVP engineer makes it easy to <em>kill your darlings</em> and retains the\n<em>project scope</em> at a level where this is easy.</p>\n<p>Should the project reach a validation point where further development is needed\nthen the MVP engineer takes the project into the next phase.\nThis is important as knowledge needs to be handed over and a team culture\nneeds to be built around the mission. Hence, the MVP engineer will do\nrecruiting and engineering interviews, and hire for the new technical roles.\nThe engineer will also disseminate knowledge about the tech stack and the\nproject to new participants to ensure the productivity of the team.</p>\n<p>The last thing the MVP engineer does is scoping further development not to\nleave the product in a vacuum and also recognizing the elements that were\npreviously not attended to, but are important to ensure the success of\nthe product.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/three-pillars-of-software-development/","relativePath":"three-pillars-of-software-development.md","relativeDir":"","base":"three-pillars-of-software-development.md","name":"three-pillars-of-software-development","frontmatter":{"title":"The 3 Pillars of Software Development","subtitle":"On implementation, tests, and verification","excerpt":"Decomposing the testing process quickly shows that it consists of two components. First a specification is formulated, which is later verified.","date":"2014-10-01","template":"post"},"html":"<p><em>Test driven development is for many programmers a well-known concept.\nTests are written, and procedures are implemented trying to satisfy the tests\nproviding in a little green mark. When the green mark is not easily obtainable,\ndivide and conquer is applied with more and easier tests.\nIn this article, I try to decompose the process of testing to that of specifying\nand verifying. I relate this notion to places where verification are\nmathematically used to bind a specification and an implementation.</em></p>\n<h2>Introduction</h2>\n<p>In the software development community, it has become obvious that software has to\nbe tested. The reasons are in plural: Keeping a maintainable codebase, assurance\nof software correctness, up-to-date documentation, continuous\n<em>something</em><sup id=\"fnref-cstar\"><a href=\"#fn-cstar\" class=\"footnote-ref\">cstar</a></sup>, etc.</p>\n<p>For me, the two most important reasons to do software testing are the following:</p>\n<ol>\n<li>Forced reasoning: To be able to express a specification, knowledge about the\nproblem and the problem domain has to be understood.</li>\n<li>Correctness assurance: Proper tests are an assurance that specifications are\nmet. This, among other things, allows for continuous integration which is\ncrucial in projects needing to be flexible.</li>\n</ol>\n<h3>Testing as verifying a specification</h3>\n<p>In the above I did a slight transition from using the term testing to use the\nterms specifying and verifying. Decomposing the testing process quickly\nshows that it consists of two components. First a specification is formulated,\nwhich is later verified against the actual implementation.</p>\n<p>This split is necessary to be able to talk about the process in a broader term.\nWithin proven software, specifications are mathematically proven to be\nimplemented in a correct manner, hence the term testing is too vague (at least in\nmy consciousness) as we do not necessarily test a finite set of the input for a\ngiven procedure.</p>\n<h3>The premises</h3>\n<p>Implicitly, and now explicitly, I advocate testing. My opinion is that testing\nshould take up as many resources as implementing. Of course, this opinion is not\nstrict, and many conditions are in play when deciding the number of resources\nspent on verifying code. I trust the reader in knowing when to test. At least,\nit is not the main aim for this article.</p>\n<h3>Examples</h3>\n<p>I have drawn examples from two worlds: One from the engineering world\nrepresented by examples in Java. The other one is Coq<sup id=\"fnref-coq\"><a href=\"#fn-coq\" class=\"footnote-ref\">coq</a></sup>. Coq is a theorem\nprover performing formal proof verification. That means that we can be sure what\nis expressed also holds.</p>\n<h2>Three Pillars</h2>\n<p>The premise for the article is now set, and the three pillars can be\nintroduced. I will allow to start with the specification. Intuitively it might seem\nmore obvious to start with implementation as it is the most commonly thought of\npart of the programming process. But for being able to start implementing, a\nspecification of some form must be present, implicitly or explicitly.</p>\n<p>After the specification, the implementation is introduced and at least they are\nbound using the verification.</p>\n<h3>Specification</h3>\n<p>Specifications ranges from the informal human readable text string used to\nspecify the name of a test function in a given test suite, to a formal definition.\nNo matter by which means the specifications is expressed, the process of\nwriting it is still very important. It forces reasoning about the application.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"java\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">import</span><span class=\"mtk1\"> org.junit.*;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\"> </span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">class</span><span class=\"mtk1\"> </span><span class=\"mtk9\">TestAddition</span><span class=\"mtk1\"> {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  @</span><span class=\"mtk9\">Test</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk9\">void</span><span class=\"mtk1\"> </span><span class=\"mtk10\">testThatAddAdds</span><span class=\"mtk1\">(){</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk3\">//[...]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  }</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span></code></pre>\n<p>The above is an example drawn from Java, where a method is named to specify what\nwe are going to test. The function name is here seen as the specification for\nwhat we are going to verify. It is given that the specification is informally\ndefined.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">Definition specification_of_addition (f: nat -&gt; nat -&gt; nat) :=</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  (forall b : nat,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">     f 0 b = b)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  /\\</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  (forall a b : nat,</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">     f (S a) b = f a (S b)).</span></span></code></pre>\n<p>This example of a specification is, on the other hand, thoroughly expressed in\na specific syntax.</p>\n<p>It should be noted that there is a difference on the two examples. The first one is\naddition in Java's type system, integers with a given precision. In Coq, we\nshow that it holds for all natural numbers.</p>\n<h3>Implementation</h3>\n<p>The implementation is, usually, the well-known part. Following is a suggestion\nfor some code that implements their specification. So far, though, you only have\nmy word that what I have expressed is also what is implemented.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"java\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">class</span><span class=\"mtk1\"> </span><span class=\"mtk9\">Addition</span><span class=\"mtk1\"> {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk9\">int</span><span class=\"mtk1\"> </span><span class=\"mtk10\">add</span><span class=\"mtk1\">(</span><span class=\"mtk9\">int</span><span class=\"mtk1\"> </span><span class=\"mtk12\">a</span><span class=\"mtk1\">, </span><span class=\"mtk9\">int</span><span class=\"mtk1\"> </span><span class=\"mtk12\">b</span><span class=\"mtk1\">){</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> a + b;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  }</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span></code></pre>\n<p>The Java example should come as no surprise.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">Fixpoint add a b : nat :=</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  match a with</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    | 0    =&gt; b</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    | S a&#39; =&gt; add a&#39; (S b)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  end.</span></span></code></pre>\n<p>The implementation in Coq is on the other hand not as straight forward. Two type\nconstructors are given: $S : nat \\rightarrow nat$ and $O : nat$ (which have the\nalias 0). The addition is here build on top of this.</p>\n<h3>Verification</h3>\n<p>For the last part, we have the verification. Here we bind the implementation to\nthe specification.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"java\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">class</span><span class=\"mtk1\"> </span><span class=\"mtk9\">TestAddition</span><span class=\"mtk1\"> {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  @</span><span class=\"mtk9\">Test</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk9\">void</span><span class=\"mtk1\"> </span><span class=\"mtk10\">testThatAddAdds</span><span class=\"mtk1\">(){</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk3\">//Assignment</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk9\">Addition</span><span class=\"mtk1\"> </span><span class=\"mtk12\">adder</span><span class=\"mtk1\"> = </span><span class=\"mtk14\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Addition</span><span class=\"mtk1\">();</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk3\">//Action</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk9\">int</span><span class=\"mtk1\"> </span><span class=\"mtk12\">shouldBe5</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">adder</span><span class=\"mtk1\">.</span><span class=\"mtk10\">add</span><span class=\"mtk1\">(</span><span class=\"mtk7\">2</span><span class=\"mtk1\">, </span><span class=\"mtk7\">3</span><span class=\"mtk1\">),</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        shouldBe7 = </span><span class=\"mtk12\">adder</span><span class=\"mtk1\">.</span><span class=\"mtk10\">add</span><span class=\"mtk1\">(-</span><span class=\"mtk7\">2</span><span class=\"mtk1\">, </span><span class=\"mtk7\">9</span><span class=\"mtk1\">),</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        shouldBe5 = </span><span class=\"mtk12\">adder</span><span class=\"mtk1\">.</span><span class=\"mtk10\">add</span><span class=\"mtk1\">(</span><span class=\"mtk7\">8</span><span class=\"mtk1\">, -</span><span class=\"mtk7\">3</span><span class=\"mtk1\">),</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        shouldBeMinus7 = </span><span class=\"mtk12\">adder</span><span class=\"mtk1\">.</span><span class=\"mtk10\">add</span><span class=\"mtk1\">(-</span><span class=\"mtk7\">4</span><span class=\"mtk1\">, -</span><span class=\"mtk7\">3</span><span class=\"mtk1\">);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">        </span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk3\">//Assert</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk10\">assertEquals</span><span class=\"mtk1\">(shouldBe5, </span><span class=\"mtk7\">5</span><span class=\"mtk1\">);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk10\">assertEquals</span><span class=\"mtk1\">(shouldBe7, </span><span class=\"mtk7\">7</span><span class=\"mtk1\">);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk10\">assertEquals</span><span class=\"mtk1\">(shouldBe5, </span><span class=\"mtk7\">5</span><span class=\"mtk1\">);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk10\">assertEquals</span><span class=\"mtk1\">(shouldBeMinus7, -</span><span class=\"mtk7\">7</span><span class=\"mtk1\">);</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  }</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span></code></pre>\n<p>For the Java, you still have to accept that you only have my word for\ncorrectness regarding the specification. As the specification is completely\ninformal, the assertions is a product of my interpretation of the specification\nand programming skills.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">Lemma add_satisfies_specification_of_addition :</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  specification_of_addition add.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Proof.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  unfold specification_of_addition.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  split.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    intro b.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    unfold add.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    reflexivity.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    </span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    intros a b.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    induction a as [|a&#39; IHa&#39;].</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">      unfold add.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">      reflexivity.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">      </span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">      unfold add.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">      fold add.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">      reflexivity.</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">Qed.</span></span></code></pre>\n<p>For Coq, on the other hand, the specification are read by the theorem prover.\nHere I provide a <em>certificate</em> that binds the specification to the\nimplementation. Coq afterwards verifies that this certificate is valid.</p>\n<h2>Discussion</h2>\n<p>This is one way of relating the testing process to the process of building\nformally verified software. Testing should be applied in both cases, as\nspecifications need to be verified. This is a tool for humans to verify\n<em>semantic preservation</em> when communicating their thoughts to a computer, hence\nthe KISS principle<sup id=\"fnref-kiss\"><a href=\"#fn-kiss\" class=\"footnote-ref\">kiss</a></sup> applies; It is, after all, simpler to invoke a function\nwith a given input and provide the expected result than formulating a formal specification.</p>\n<h2>Further Reading</h2>\n<p>My offset in this article was a basic approach to testing using Java and formal\nverification using Coq. That said, all mainstream languages have their own means\nof testing. For formal verification other tools exist. This is evident from the\nexisting project, e.g. the seL4 microkernel does not use Coq for the verification\npart.</p>\n<p>Resources for formal verification using Coq are plenty. Some good ones are\nSoftware Foundations<sup id=\"fnref-softfound\"><a href=\"#fn-softfound\" class=\"footnote-ref\">softfound</a></sup> and Certified Programming with Dependent\nTypes<sup id=\"fnref-cpdt\"><a href=\"#fn-cpdt\" class=\"footnote-ref\">cpdt</a></sup>.</p>\n<p>There are also some interesting projects using the formal verification on very\nreal problems. The <strong>CompCert</strong><sup id=\"fnref-compcert\"><a href=\"#fn-compcert\" class=\"footnote-ref\">compcert</a></sup> certified compiler is a formally\nproven compiler for C.</p>\n<p>In a bigger perspective, the <strong>seL4</strong><sup id=\"fnref-sel4\"><a href=\"#fn-sel4\" class=\"footnote-ref\">sel4</a></sup> is a formally proven microkernel.</p>\n<p>Why are these necessary? one might ask. We have the GNU compiler and Linux. They\nare both stable and performs quite well? The thing is that some applications\ncan't afford that a bug is not found until running that very specific instance\nof code.</p>\n<h2>Conclusion</h2>\n<p>Even though it might seem kind of magic that we can formally verify a piece of\nsoftware, we still have to remember what we verify. We do not verify that the\nsoftware does what it is supposed to do. We verify that the software does\nwhat we have specified in the specification.</p>\n<p>By this, I emphasise the importance of doing simple and easily understandable\ntesting, were one can easily verify that the output is correct. I encourage\npeople to play with their software and try to break it. After all, they could\nhave expressed the specification wrong.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .mtk4 { color: #0000FF; }\n  .default-light .mtk1 { color: #000000; }\n  .default-light .mtk9 { color: #267F99; }\n  .default-light .mtk10 { color: #795E26; }\n  .default-light .mtk3 { color: #008000; }\n  .default-light .mtk12 { color: #001080; }\n  .default-light .mtk14 { color: #AF00DB; }\n  .default-light .mtk7 { color: #09885A; }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>\n<div class=\"footnotes\">\n<hr>\n<ol>\n<li id=\"fn-coq\">\n<p><a href=\"http://coq.inria.fr/\">Coq website</a></p>\n<a href=\"#fnref-coq\" class=\"footnote-backref\">↩</a>\n</li>\n<li id=\"fn-softfound\">\n<p>Benjamin C. Pierce et al., <em><a href=\"http://www.cis.upenn.edu/~bcpierce/sf/current/index.html\">Software Foundations </a></em></p>\n<a href=\"#fnref-softfound\" class=\"footnote-backref\">↩</a>\n</li>\n<li id=\"fn-cpdt\">\n<p>Adam Chlipala, <em><a href=\"http://adam.chlipala.net/cpdt/\">Certified Programming with Dependent Types</a></em></p>\n<a href=\"#fnref-cpdt\" class=\"footnote-backref\">↩</a>\n</li>\n<li id=\"fn-compcert\">\n<p><a href=\"http://compcert.inria.fr/\">CompCert website</a></p>\n<a href=\"#fnref-compcert\" class=\"footnote-backref\">↩</a>\n</li>\n<li id=\"fn-sel4\">\n<p><a href=\"http://sel4.systems/\">seL4 website</a></p>\n<a href=\"#fnref-sel4\" class=\"footnote-backref\">↩</a>\n</li>\n<li id=\"fn-kiss\">\n<p><a href=\"http://en.wikipedia.org/wiki/KISS_principle\">en.wikipedia.org/wiki/KISS_principle</a></p>\n<a href=\"#fnref-kiss\" class=\"footnote-backref\">↩</a>\n</li>\n<li id=\"fn-cstar\">\n<p>Integration, delivery, deployment, etc.</p>\n<a href=\"#fnref-cstar\" class=\"footnote-backref\">↩</a>\n</li>\n</ol>\n</div>"},{"url":"/types-for-the-eager-language-learner/","relativePath":"types-for-the-eager-language-learner.md","relativeDir":"","base":"types-for-the-eager-language-learner.md","name":"types-for-the-eager-language-learner","frontmatter":{"title":"On Types for the Eager Language Learner","author":"Mads Buch","date":"2016-06-12","template":"post"},"html":"<p>Recently I started writing a\n<a href=\"https://leanpub.com/types-for-the-eager-language-learner\">book on Leanpub</a>.\nThis was a first and, frankly, I had\nno real idea about what to write about.</p>\n<p>Presently I have spend quite some time studying type systems. That includes\nboth formal studies and the Haskell type system. I found that after i began\nto enjoy increased knowledge about types, I became a much better programmer\nin languages like JavaScript, Java, and C#.</p>\n<p>Furthermore I saw that my ventures into new languages was strictly more\nsuccessful. It was much easier to learn a new language when I had the\nability to boil everything down to the typing concepts.</p>\n<p>My thinking and research began. I concluded that the type system is\na latent framework for reasoning which is similar across programming\nlanguages. After learning types the link between Java interfaces and\nHaskell type classes was a much more coherent unit.</p>\n<p>My initial ventures into types was through a hard path. I spend hours with\nPierces books and numerous papers. The lingo is in most\ncases more confusing than\nresolving, and it was very hard to link it to any existing knowledge.</p>\n<p>Therefor I decided to write a book that explains type theory for the\nprogrammer who has a strong foundation in languages such as JavaScript\nand Java. Programmers who haven't ventured into Haskell or other\nfunctional programming languages, but really want to consider these languages.</p>\n<h1>The Book</h1>\n<p>The book is written using the lean methodology. This means that it will\nbe gradually published, and that I am very keen on getting feedback all the\ntime.</p>\n<p>I will publish a chapter when it is initially finished for publishing. That\nis when I am content with the writing, have done copy writing and the chapter\nis coherent. </p>\n<p>A published chapter is not written in stone. This means that I am more than\nwilling to rewrite a chapter when I get good arguments on alternative material.</p>\n<p>As for the pricing I have initially decided that the book will cost 5$ pr.\nchapter. This means that the book will increase in value when more chapters\nare added. I find this quite fair and it also incentivises to buy the book\nearly on, which gives me more time to finish it.</p>\n<h1>The Progress</h1>\n<p>Following is my progress on the book. Even this is not written in stone.\nSo if anybody wants a special chapter, please let me know, and I might\nchange the curriculum of the book.</p>\n<table>\n<thead>\n<tr>\n<th>Chapters</th>\n<th align=\"center\">Current Status</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Introduction</td>\n<td align=\"center\">Writing</td>\n</tr>\n<tr>\n<td>Basic Types</td>\n<td align=\"center\">Writing</td>\n</tr>\n<tr>\n<td>Intermedio - Application</td>\n<td align=\"center\">Staging</td>\n</tr>\n<tr>\n<td>Polymorphic Types</td>\n<td align=\"center\">Staging</td>\n</tr>\n<tr>\n<td>Existensial Types</td>\n<td align=\"center\">Staging</td>\n</tr>\n<tr>\n<td>Reference Types</td>\n<td align=\"center\">Staging</td>\n</tr>\n<tr>\n<td>Intermedio - Application</td>\n<td align=\"center\">Staging</td>\n</tr>\n<tr>\n<td>Sub Typing</td>\n<td align=\"center\">Staging</td>\n</tr>\n</tbody>\n</table>\n<p>The book is available on\n<a href=\"https://leanpub.com/types-for-the-eager-language-learner\">leanpub.com/types-for-the-eager-language-learner</a>. I appreciate any encouragement.\nThat does not have to be of monetary art, but just a word that my work is\nappreciated.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/types-with-ts-io/","relativePath":"types-with-ts-io.md","relativeDir":"","base":"types-with-ts-io.md","name":"types-with-ts-io","frontmatter":{"title":"Types with IO-TS","author":"Mads Buch","date":"2021-08-08","template":"post","category":"technicalRef"},"html":"<p>Type safe programming is often broken on the edge to the real world. Typescript\ndoes not come with any batteries to mitigate this. A cleverly designed library\ncalled <a href=\"https://gcanti.github.io/io-ts/\"><code>io-ts</code></a> fixes this problem. This is\nhow I use it:</p>\n<p>A generic parser function:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"ts\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">import</span><span class=\"mtk1\"> { </span><span class=\"mtk12\">pipe</span><span class=\"mtk1\"> } </span><span class=\"mtk14\">from</span><span class=\"mtk1\"> </span><span class=\"mtk17\">&quot;fp-ts/lib/function&quot;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">import</span><span class=\"mtk1\"> { </span><span class=\"mtk12\">fold</span><span class=\"mtk1\"> } </span><span class=\"mtk14\">from</span><span class=\"mtk1\"> </span><span class=\"mtk17\">&#39;fp-ts/lib/Either&#39;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">import</span><span class=\"mtk1\"> { </span><span class=\"mtk12\">Decoder</span><span class=\"mtk1\">, </span><span class=\"mtk12\">Errors</span><span class=\"mtk1\"> } </span><span class=\"mtk14\">from</span><span class=\"mtk1\"> </span><span class=\"mtk17\">&quot;io-ts&quot;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">export</span><span class=\"mtk1\"> </span><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk10\">parse</span><span class=\"mtk1\"> = &lt;</span><span class=\"mtk9\">A</span><span class=\"mtk1\">&gt;(</span><span class=\"mtk12\">codec</span><span class=\"mtk1\">: </span><span class=\"mtk9\">Decoder</span><span class=\"mtk1\">&lt;</span><span class=\"mtk9\">any</span><span class=\"mtk1\">, </span><span class=\"mtk9\">A</span><span class=\"mtk1\">&gt;, </span><span class=\"mtk12\">value</span><span class=\"mtk1\">: </span><span class=\"mtk9\">any</span><span class=\"mtk1\">): </span><span class=\"mtk9\">A</span><span class=\"mtk1\"> </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk3\">// failure handler</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk10\">onLeft</span><span class=\"mtk1\"> = (</span><span class=\"mtk12\">errors</span><span class=\"mtk1\">: </span><span class=\"mtk9\">Errors</span><span class=\"mtk1\">) </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk14\">throw</span><span class=\"mtk1\"> </span><span class=\"mtk4\">new</span><span class=\"mtk1\"> </span><span class=\"mtk9\">Error</span><span class=\"mtk1\">(</span><span class=\"mtk17\">`Could not parse </span><span class=\"mtk4\">${</span><span class=\"mtk9\">JSON</span><span class=\"mtk18\">.</span><span class=\"mtk10\">stringify</span><span class=\"mtk18\">(</span><span class=\"mtk12\">errors</span><span class=\"mtk18\">)</span><span class=\"mtk4\">}</span><span class=\"mtk17\">`</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  }</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk14\">return</span><span class=\"mtk1\"> </span><span class=\"mtk10\">pipe</span><span class=\"mtk1\">(</span><span class=\"mtk12\">codec</span><span class=\"mtk1\">.</span><span class=\"mtk10\">decode</span><span class=\"mtk1\">(</span><span class=\"mtk12\">value</span><span class=\"mtk1\">), </span><span class=\"mtk10\">fold</span><span class=\"mtk1\">(</span><span class=\"mtk12\">onLeft</span><span class=\"mtk1\">, </span><span class=\"mtk12\">v</span><span class=\"mtk1\"> </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> </span><span class=\"mtk12\">v</span><span class=\"mtk1\">))</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span></code></pre>\n<p>A quick example of rewriting typescript types into their equivalent <code>io-ts</code>\ndefinitions goes as follows:</p>\n<p><strong>Before:</strong></p>\n<pre class=\"grvsc-container default-light\" data-language=\"ts\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">type</span><span class=\"mtk1\"> </span><span class=\"mtk9\">User</span><span class=\"mtk1\"> = {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk12\">name</span><span class=\"mtk1\">: </span><span class=\"mtk9\">string</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk12\">uid</span><span class=\"mtk1\">: </span><span class=\"mtk9\">string</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk12\">description</span><span class=\"mtk1\">?: </span><span class=\"mtk9\">string</span><span class=\"mtk1\"> | </span><span class=\"mtk9\">undefined</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk12\">metadata</span><span class=\"mtk1\">: {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    [</span><span class=\"mtk12\">m</span><span class=\"mtk1\">: </span><span class=\"mtk9\">string</span><span class=\"mtk1\">]: </span><span class=\"mtk9\">string</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  }</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk12\">gender</span><span class=\"mtk1\">: </span><span class=\"mtk17\">&quot;male&quot;</span><span class=\"mtk1\"> | </span><span class=\"mtk17\">&quot;female&quot;</span><span class=\"mtk1\"> | </span><span class=\"mtk17\">&quot;other&quot;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span></code></pre>\n<p><strong>After:</strong></p>\n<pre class=\"grvsc-container default-light\" data-language=\"ts\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">User</span><span class=\"mtk1\"> = </span><span class=\"mtk10\">intersection</span><span class=\"mtk1\">([</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk10\">type</span><span class=\"mtk1\">({</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk12\">name:</span><span class=\"mtk1\"> </span><span class=\"mtk12\">string</span><span class=\"mtk1\">,</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk12\">uid:</span><span class=\"mtk1\"> </span><span class=\"mtk12\">string</span><span class=\"mtk1\">,</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk12\">metadata:</span><span class=\"mtk1\"> </span><span class=\"mtk10\">record</span><span class=\"mtk1\">(</span><span class=\"mtk12\">string</span><span class=\"mtk1\">, </span><span class=\"mtk12\">string</span><span class=\"mtk1\">),</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk12\">gender:</span><span class=\"mtk1\"> </span><span class=\"mtk10\">union</span><span class=\"mtk1\">([</span><span class=\"mtk10\">literal</span><span class=\"mtk1\">(</span><span class=\"mtk17\">&quot;male&quot;</span><span class=\"mtk1\">), </span><span class=\"mtk10\">literal</span><span class=\"mtk1\">(</span><span class=\"mtk17\">&quot;female&quot;</span><span class=\"mtk1\">), </span><span class=\"mtk10\">literal</span><span class=\"mtk1\">(</span><span class=\"mtk17\">&quot;other&quot;</span><span class=\"mtk1\">)])</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  }),</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk10\">partial</span><span class=\"mtk1\">({</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk12\">description:</span><span class=\"mtk1\"> </span><span class=\"mtk12\">string</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  })</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">])</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk4\">type</span><span class=\"mtk1\"> </span><span class=\"mtk9\">User</span><span class=\"mtk1\"> = </span><span class=\"mtk9\">TypeOf</span><span class=\"mtk1\">&lt;</span><span class=\"mtk4\">typeof</span><span class=\"mtk1\"> </span><span class=\"mtk9\">User</span><span class=\"mtk1\">&gt;</span></span></span></code></pre>\n<p>And lastly we do type safe decoding as follows:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"ts\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk14\">try</span><span class=\"mtk1\"> {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">u</span><span class=\"mtk1\"> = </span><span class=\"mtk10\">parse</span><span class=\"mtk1\">(</span><span class=\"mtk12\">User</span><span class=\"mtk1\">, ...)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  ...</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">} </span><span class=\"mtk14\">catch</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">e</span><span class=\"mtk1\">) {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk9\">console</span><span class=\"mtk1\">.</span><span class=\"mtk10\">error</span><span class=\"mtk1\">(</span><span class=\"mtk17\">&quot;Could not parse&quot;</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span></code></pre>\n<p>Couple of notes:</p>\n<ul>\n<li>We overload the name such that it can both be used as a type and as a decoder</li>\n<li>The types of <code>Usr</code> and <code>User</code> are mutually assignable. This means that\nnot changes will have to be made to the rest of the application.</li>\n<li>The resulting variable <code>u</code> is correctly typed.</li>\n</ul>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .mtk14 { color: #AF00DB; }\n  .default-light .mtk1 { color: #000000; }\n  .default-light .mtk12 { color: #001080; }\n  .default-light .mtk17 { color: #A31515; }\n  .default-light .mtk4 { color: #0000FF; }\n  .default-light .mtk10 { color: #795E26; }\n  .default-light .mtk9 { color: #267F99; }\n  .default-light .mtk3 { color: #008000; }\n  .default-light .mtk18 { color: #000000FF; }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/the-probability-monad/","relativePath":"the-probability-monad.md","relativeDir":"","base":"the-probability-monad.md","name":"the-probability-monad","frontmatter":{"title":"The Probability Monad","author":"Mads Buch","layout":"post","video":false,"comments":false,"papers":["ramsey2012"],"categories":"Probabilistic Programming","date":"2016-12-03","template":"post"},"html":"<p><em>Main point: Programs can be interpreted as probabilistic distributions using the probability monad. This yields, amongst other things, foundations for easier to understand statistical models.</em></p>\n<p><em>updated february 2021 for clarity.</em></p>\n<!-- \nHvor er det interessant? Hvilken motiverende vinkel skal jeg laegge paa?\n\n* Bygge probabilistike modeller for at teste hypoteser?\n* Machine learning - Nej, for vi taler ikke om inferens\n\n-->\n<p>A probability distribution is in fact a program. Thinking about it in this\nway makes them considerable more understandable and easier to work with. now\na probabilistic model is simply a program that make all assumptions clear as\nthe sun - no more assumption that data adheres to certain distributions.</p>\n<p>In order to <em>lift</em> programs into a probability distribution we use the\nprobability monad. It has for a long time been known that probability\ndistributions are monads. This article builds on the work by\nRamsey and Pfeffer on\n<a href=\"https://www.cs.tufts.edu/comp/150PP/archive/norman-ramsey/pmonad.pdf\">Stochastic Lambda Calculus and Monads of Probability Distributions</a></p>\n<p>The source for this article is available as a\n<a href=\"https://gist.github.com/madsbuch/5a8a1fc9b70621dd93dd70058754b126\">gist on Github</a>.</p>\n<h1>Probabilities</h1>\n<p>I tend to think of a probability distribution as a black box. A mysterious\nthing we will never be able to completely open. However, we do have different\nlenses that lets us see aspects of the distribution and lets us understand it.\nThese lenses, from here on queries, are the <em>support</em> query, the <em>expectation</em> query,\nand the <em>sample</em> query.</p>\n<p>The support of a probability distribution is simply the set of elements in the\ndistribution which are probable. Ie. the elements with a non-zero probability of\noccurring.</p>\n<p>The expectation query is implemented as a function that takes a probability\ndistribution, and function representing an <em>event</em> and returns the mean\nthat said event will happen. For the famous dice example where we want to\ncalculate the expectation of rolling a six, the function will return 1 for\nsix and 0 for all others. Taking the mean yields 1/6 with is the expectation\nfor rolling a six.</p>\n<p>The sample query yields one of the elements from the support with a given\nprobability. The samples are usually not that interesting in small quantities\nand we need to be able to draw thousands. But when we do that we are in fact\nable to say interesting things about distributions that are <em>recursive</em> or\nhave infinite support. This happens easily when we lift entire programs into a\nprobability distribution.</p>\n<p>When constructing probability distributions we need an intuitive way to tap\ninto all the randomness. When working with discrete distributions we can\ndefine</p>\n<p>Lastly, we also need an operation for building distributions. In this work,\nwe take offset in a single function called <code>choose</code> which act as a construct\nthat binary chooses between two distribution with a certain probability. It\nis easy to understand. A coin toss is implement as <code>choose 0.5 Tail Head</code>.\nFrom here it is merely about convincing oneself that we can approximate all\ndistributions with arbitrary precision using that single operator.</p>\n<h1>A Monad</h1>\n<p>A monad is a mathematical structure with its roots in\ncategory theory. Monads can be understood as an abstraction over a\ncomputational context. This computational context is in this case the\nconcrete probabilities. Ie. notions about categories and their explicit\nprobabilities etc.</p>\n<p>The monad needs to implement two operations: The <code>bind</code> operation for\ncomposition and the return operation to lift values into the monad.\nIn the Haskell syntax these functions are declared as follows</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">(&gt;&gt;=)  :: m a -&gt; (a -&gt; m b) -&gt; m b -- pronounced bind</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">return :: a -&gt; m a</span></span></code></pre>\n<p>In terms of probability distribution these should be read respectively as\n<code>draw</code> and as <em>make this single value a distribution</em>. Admittedly, the\ndistribution <code>return 4</code> is not in particular interesting as we will always\ndraw the number 4 with a 100% probability.</p>\n<p>The raw probability distribution furthermore implements the <code>choose</code> function.\nThis is the function</p>\n<!-- \nHer er en interessant erfaring jeg skal have med mig:\n\nTidligere havde jeg holdt dette afsnit til udelukkende at snakke om monader.\nDer er let i forhold til at at saette kapitlet i kasser. Nu har jeg taget en\nbredere diskussion hvor probability monaden ogsaa bliver introduceret. Det\ngoer det lettere at forstaa monader.\n-->\n<h1>Class Hierarchy</h1>\n<p>To get an overview of the implementation of the probability monad\nit makes sense to look at the class hierarchy we are considering.</p>\n<p>The following figure shows the monads we are implementing.</p>\n<p><svg height=\"330\" width=\"100%\"><defs><marker id=\"triangle\" viewBox=\"0 0 14 14\" refX=\"0\" refY=\"5\" markerUnits=\"strokeWidth\" markerWidth=\"10\" markerHeight=\"10\" orient=\"auto\"><path d=\"M 0 0 L 10 5 L 0 10 z\"></path></marker></defs><line x1=\"4\" x2=\"12\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"8\" y2=\"16\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"8\" x2=\"16\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"16\" x2=\"24\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"24\" x2=\"32\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"32\" x2=\"40\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"40\" x2=\"48\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"48\" x2=\"56\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"56\" x2=\"64\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"64\" x2=\"72\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"72\" x2=\"80\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"80\" x2=\"88\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"88\" x2=\"96\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"96\" x2=\"104\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"104\" x2=\"112\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"112\" x2=\"120\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"120\" x2=\"128\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"128\" x2=\"136\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"136\" x2=\"144\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"144\" x2=\"152\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"152\" x2=\"160\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"160\" x2=\"168\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"168\" x2=\"176\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"176\" x2=\"184\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"184\" x2=\"192\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"192\" x2=\"200\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"200\" x2=\"208\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"208\" x2=\"216\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"216\" x2=\"224\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"224\" x2=\"232\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"232\" x2=\"240\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"240\" x2=\"248\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"248\" x2=\"256\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"256\" x2=\"264\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"264\" x2=\"272\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"272\" x2=\"280\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"280\" x2=\"288\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"288\" x2=\"296\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"296\" x2=\"304\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"304\" x2=\"312\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"312\" x2=\"320\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"320\" x2=\"328\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"328\" x2=\"332\" y1=\"8\" y2=\"8\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"332\" x2=\"332\" y1=\"8\" y2=\"16\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"16\" y2=\"32\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"332\" x2=\"332\" y1=\"16\" y2=\"32\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"32\" y2=\"48\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><text x=\"150\" y=\"44\" style=\"font-size:14px;font-family:monospace\">M</text><text x=\"158\" y=\"44\" style=\"font-size:14px;font-family:monospace\">o</text><text x=\"166\" y=\"44\" style=\"font-size:14px;font-family:monospace\">n</text><text x=\"174\" y=\"44\" style=\"font-size:14px;font-family:monospace\">a</text><text x=\"182\" y=\"44\" style=\"font-size:14px;font-family:monospace\">d</text><line x1=\"332\" x2=\"332\" y1=\"32\" y2=\"48\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"48\" y2=\"64\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"332\" x2=\"332\" y1=\"48\" y2=\"64\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"64\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"12\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"8\" x2=\"16\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"16\" x2=\"24\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"24\" x2=\"32\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"32\" x2=\"40\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"40\" x2=\"48\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"48\" x2=\"56\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"56\" x2=\"64\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"64\" x2=\"72\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"72\" x2=\"80\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"80\" x2=\"88\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"88\" x2=\"96\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"96\" x2=\"104\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"104\" x2=\"112\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"112\" x2=\"120\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"120\" x2=\"128\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"128\" x2=\"136\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"136\" x2=\"144\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"144\" x2=\"152\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"152\" x2=\"160\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"160\" x2=\"168\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"168\" x2=\"172\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"172\" x2=\"180\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"172\" x2=\"172\" y1=\"72\" y2=\"80\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"176\" x2=\"184\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"184\" x2=\"192\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"192\" x2=\"200\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"200\" x2=\"208\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"208\" x2=\"216\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"216\" x2=\"224\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"224\" x2=\"232\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"232\" x2=\"240\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"240\" x2=\"248\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"248\" x2=\"256\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"256\" x2=\"264\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"264\" x2=\"272\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"272\" x2=\"280\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"280\" x2=\"288\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"288\" x2=\"296\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"296\" x2=\"304\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"304\" x2=\"312\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"312\" x2=\"320\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"320\" x2=\"328\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"332\" x2=\"332\" y1=\"64\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"328\" x2=\"332\" y1=\"72\" y2=\"72\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"172\" x2=\"172\" y1=\"80\" y2=\"96\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"172\" x2=\"172\" y1=\"96\" y2=\"112\" style=\"stroke: rgb(0,0,0);stroke-width:1\" marker-end=\"url(#triangle)\"></line><line x1=\"4\" x2=\"12\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"120\" y2=\"128\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"8\" x2=\"16\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"16\" x2=\"24\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"24\" x2=\"32\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"32\" x2=\"40\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"40\" x2=\"48\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"48\" x2=\"56\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"56\" x2=\"64\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"64\" x2=\"72\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"72\" x2=\"80\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"80\" x2=\"88\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"88\" x2=\"96\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"96\" x2=\"104\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"104\" x2=\"112\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"112\" x2=\"120\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"120\" x2=\"128\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"128\" x2=\"136\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"136\" x2=\"144\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"144\" x2=\"152\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"152\" x2=\"160\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"160\" x2=\"168\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"168\" x2=\"176\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"176\" x2=\"184\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"184\" x2=\"192\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"192\" x2=\"200\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"200\" x2=\"208\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"208\" x2=\"216\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"216\" x2=\"224\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"224\" x2=\"232\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"232\" x2=\"240\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"240\" x2=\"248\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"248\" x2=\"256\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"256\" x2=\"264\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"264\" x2=\"272\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"272\" x2=\"280\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"280\" x2=\"288\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"288\" x2=\"296\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"296\" x2=\"304\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"304\" x2=\"312\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"312\" x2=\"320\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"320\" x2=\"328\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"328\" x2=\"336\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"336\" x2=\"340\" y1=\"120\" y2=\"120\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"340\" x2=\"340\" y1=\"120\" y2=\"128\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"128\" y2=\"144\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"340\" x2=\"340\" y1=\"128\" y2=\"144\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"144\" y2=\"160\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><text x=\"94\" y=\"156\" style=\"font-size:14px;font-family:monospace\">P</text><text x=\"102\" y=\"156\" style=\"font-size:14px;font-family:monospace\">r</text><text x=\"110\" y=\"156\" style=\"font-size:14px;font-family:monospace\">o</text><text x=\"118\" y=\"156\" style=\"font-size:14px;font-family:monospace\">b</text><text x=\"126\" y=\"156\" style=\"font-size:14px;font-family:monospace\">a</text><text x=\"134\" y=\"156\" style=\"font-size:14px;font-family:monospace\">b</text><text x=\"142\" y=\"156\" style=\"font-size:14px;font-family:monospace\">i</text><text x=\"150\" y=\"156\" style=\"font-size:14px;font-family:monospace\">l</text><text x=\"158\" y=\"156\" style=\"font-size:14px;font-family:monospace\">i</text><text x=\"166\" y=\"156\" style=\"font-size:14px;font-family:monospace\">t</text><text x=\"174\" y=\"156\" style=\"font-size:14px;font-family:monospace\">y</text><text x=\"190\" y=\"156\" style=\"font-size:14px;font-family:monospace\">M</text><text x=\"198\" y=\"156\" style=\"font-size:14px;font-family:monospace\">o</text><text x=\"206\" y=\"156\" style=\"font-size:14px;font-family:monospace\">n</text><text x=\"214\" y=\"156\" style=\"font-size:14px;font-family:monospace\">a</text><text x=\"222\" y=\"156\" style=\"font-size:14px;font-family:monospace\">d</text><line x1=\"340\" x2=\"340\" y1=\"144\" y2=\"160\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"160\" y2=\"176\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"340\" x2=\"340\" y1=\"160\" y2=\"176\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"176\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"12\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"8\" x2=\"16\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"16\" x2=\"24\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"24\" x2=\"32\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"32\" x2=\"40\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"40\" x2=\"48\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"48\" x2=\"56\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"56\" x2=\"60\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"60\" x2=\"68\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"60\" x2=\"60\" y1=\"184\" y2=\"192\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"64\" x2=\"72\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"72\" x2=\"80\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"80\" x2=\"88\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"88\" x2=\"96\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"96\" x2=\"104\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"104\" x2=\"112\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"112\" x2=\"120\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"120\" x2=\"128\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"128\" x2=\"136\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"136\" x2=\"144\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"144\" x2=\"152\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"152\" x2=\"160\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"160\" x2=\"168\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"168\" x2=\"176\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"176\" x2=\"180\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"180\" x2=\"188\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"180\" x2=\"180\" y1=\"184\" y2=\"192\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"184\" x2=\"192\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"192\" x2=\"200\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"200\" x2=\"208\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"208\" x2=\"216\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"216\" x2=\"224\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"224\" x2=\"232\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"232\" x2=\"240\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"240\" x2=\"248\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"248\" x2=\"256\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"256\" x2=\"264\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"264\" x2=\"272\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"272\" x2=\"280\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"280\" x2=\"288\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"288\" x2=\"296\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"296\" x2=\"300\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"300\" x2=\"308\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"300\" x2=\"300\" y1=\"184\" y2=\"192\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"304\" x2=\"312\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"312\" x2=\"320\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"320\" x2=\"328\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"328\" x2=\"336\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"340\" x2=\"340\" y1=\"176\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"336\" x2=\"340\" y1=\"184\" y2=\"184\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"60\" x2=\"60\" y1=\"192\" y2=\"208\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"180\" x2=\"180\" y1=\"192\" y2=\"208\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"300\" x2=\"300\" y1=\"192\" y2=\"208\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"60\" x2=\"60\" y1=\"208\" y2=\"224\" style=\"stroke: rgb(0,0,0);stroke-width:1\" marker-end=\"url(#triangle)\"></line><line x1=\"180\" x2=\"180\" y1=\"208\" y2=\"224\" style=\"stroke: rgb(0,0,0);stroke-width:1\" marker-end=\"url(#triangle)\"></line><line x1=\"300\" x2=\"300\" y1=\"208\" y2=\"224\" style=\"stroke: rgb(0,0,0);stroke-width:1\" marker-end=\"url(#triangle)\"></line><line x1=\"4\" x2=\"12\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"232\" y2=\"240\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"8\" x2=\"16\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"16\" x2=\"24\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"24\" x2=\"32\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"32\" x2=\"40\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"40\" x2=\"48\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"48\" x2=\"56\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"56\" x2=\"64\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"64\" x2=\"72\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"72\" x2=\"80\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"80\" x2=\"88\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"88\" x2=\"96\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"96\" x2=\"104\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"104\" x2=\"112\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"112\" x2=\"116\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"116\" x2=\"116\" y1=\"232\" y2=\"240\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"140\" x2=\"148\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"140\" x2=\"140\" y1=\"232\" y2=\"240\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"144\" x2=\"152\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"152\" x2=\"160\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"160\" x2=\"168\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"168\" x2=\"176\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"176\" x2=\"184\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"184\" x2=\"192\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"192\" x2=\"200\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"200\" x2=\"208\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"208\" x2=\"216\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"216\" x2=\"220\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"220\" x2=\"220\" y1=\"232\" y2=\"240\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"244\" x2=\"252\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"244\" x2=\"244\" y1=\"232\" y2=\"240\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"248\" x2=\"256\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"256\" x2=\"264\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"264\" x2=\"272\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"272\" x2=\"280\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"280\" x2=\"288\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"288\" x2=\"296\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"296\" x2=\"304\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"304\" x2=\"312\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"312\" x2=\"320\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"320\" x2=\"328\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"328\" x2=\"336\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"336\" x2=\"340\" y1=\"232\" y2=\"232\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"340\" x2=\"340\" y1=\"232\" y2=\"240\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"240\" y2=\"256\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"116\" x2=\"116\" y1=\"240\" y2=\"256\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"140\" x2=\"140\" y1=\"240\" y2=\"256\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"220\" x2=\"220\" y1=\"240\" y2=\"256\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"244\" x2=\"244\" y1=\"240\" y2=\"256\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"340\" x2=\"340\" y1=\"240\" y2=\"256\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"256\" y2=\"272\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><text x=\"14\" y=\"268\" style=\"font-size:14px;font-family:monospace\">E</text><text x=\"22\" y=\"268\" style=\"font-size:14px;font-family:monospace\">x</text><text x=\"30\" y=\"268\" style=\"font-size:14px;font-family:monospace\">p</text><text x=\"38\" y=\"268\" style=\"font-size:14px;font-family:monospace\">e</text><text x=\"46\" y=\"268\" style=\"font-size:14px;font-family:monospace\">c</text><text x=\"54\" y=\"268\" style=\"font-size:14px;font-family:monospace\">t</text><text x=\"62\" y=\"268\" style=\"font-size:14px;font-family:monospace\">a</text><text x=\"70\" y=\"268\" style=\"font-size:14px;font-family:monospace\">t</text><text x=\"78\" y=\"268\" style=\"font-size:14px;font-family:monospace\">i</text><text x=\"86\" y=\"268\" style=\"font-size:14px;font-family:monospace\">o</text><text x=\"94\" y=\"268\" style=\"font-size:14px;font-family:monospace\">n</text><line x1=\"116\" x2=\"116\" y1=\"256\" y2=\"272\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"140\" x2=\"140\" y1=\"256\" y2=\"272\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><text x=\"150\" y=\"268\" style=\"font-size:14px;font-family:monospace\">S</text><text x=\"158\" y=\"268\" style=\"font-size:14px;font-family:monospace\">u</text><text x=\"166\" y=\"268\" style=\"font-size:14px;font-family:monospace\">p</text><text x=\"174\" y=\"268\" style=\"font-size:14px;font-family:monospace\">p</text><text x=\"182\" y=\"268\" style=\"font-size:14px;font-family:monospace\">o</text><text x=\"190\" y=\"268\" style=\"font-size:14px;font-family:monospace\">r</text><text x=\"198\" y=\"268\" style=\"font-size:14px;font-family:monospace\">t</text><line x1=\"220\" x2=\"220\" y1=\"256\" y2=\"272\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"244\" x2=\"244\" y1=\"256\" y2=\"272\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><text x=\"262\" y=\"268\" style=\"font-size:14px;font-family:monospace\">S</text><text x=\"270\" y=\"268\" style=\"font-size:14px;font-family:monospace\">a</text><text x=\"278\" y=\"268\" style=\"font-size:14px;font-family:monospace\">m</text><text x=\"286\" y=\"268\" style=\"font-size:14px;font-family:monospace\">p</text><text x=\"294\" y=\"268\" style=\"font-size:14px;font-family:monospace\">l</text><text x=\"302\" y=\"268\" style=\"font-size:14px;font-family:monospace\">e</text><line x1=\"340\" x2=\"340\" y1=\"256\" y2=\"272\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"272\" y2=\"288\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><text x=\"38\" y=\"284\" style=\"font-size:14px;font-family:monospace\">M</text><text x=\"46\" y=\"284\" style=\"font-size:14px;font-family:monospace\">o</text><text x=\"54\" y=\"284\" style=\"font-size:14px;font-family:monospace\">n</text><text x=\"62\" y=\"284\" style=\"font-size:14px;font-family:monospace\">a</text><text x=\"70\" y=\"284\" style=\"font-size:14px;font-family:monospace\">d</text><line x1=\"116\" x2=\"116\" y1=\"272\" y2=\"288\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"140\" x2=\"140\" y1=\"272\" y2=\"288\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><text x=\"158\" y=\"284\" style=\"font-size:14px;font-family:monospace\">M</text><text x=\"166\" y=\"284\" style=\"font-size:14px;font-family:monospace\">o</text><text x=\"174\" y=\"284\" style=\"font-size:14px;font-family:monospace\">n</text><text x=\"182\" y=\"284\" style=\"font-size:14px;font-family:monospace\">a</text><text x=\"190\" y=\"284\" style=\"font-size:14px;font-family:monospace\">d</text><line x1=\"220\" x2=\"220\" y1=\"272\" y2=\"288\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"244\" x2=\"244\" y1=\"272\" y2=\"288\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><text x=\"262\" y=\"284\" style=\"font-size:14px;font-family:monospace\">M</text><text x=\"270\" y=\"284\" style=\"font-size:14px;font-family:monospace\">o</text><text x=\"278\" y=\"284\" style=\"font-size:14px;font-family:monospace\">n</text><text x=\"286\" y=\"284\" style=\"font-size:14px;font-family:monospace\">a</text><text x=\"294\" y=\"284\" style=\"font-size:14px;font-family:monospace\">d</text><line x1=\"340\" x2=\"340\" y1=\"272\" y2=\"288\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"288\" y2=\"304\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"116\" x2=\"116\" y1=\"288\" y2=\"304\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"140\" x2=\"140\" y1=\"288\" y2=\"304\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"220\" x2=\"220\" y1=\"288\" y2=\"304\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"244\" x2=\"244\" y1=\"288\" y2=\"304\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"340\" x2=\"340\" y1=\"288\" y2=\"304\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"4\" y1=\"304\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"4\" x2=\"12\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"8\" x2=\"16\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"16\" x2=\"24\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"24\" x2=\"32\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"32\" x2=\"40\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"40\" x2=\"48\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"48\" x2=\"56\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"56\" x2=\"64\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"64\" x2=\"72\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"72\" x2=\"80\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"80\" x2=\"88\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"88\" x2=\"96\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"96\" x2=\"104\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"104\" x2=\"112\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"116\" x2=\"116\" y1=\"304\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"112\" x2=\"116\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"140\" x2=\"140\" y1=\"304\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"140\" x2=\"148\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"144\" x2=\"152\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"152\" x2=\"160\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"160\" x2=\"168\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"168\" x2=\"176\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"176\" x2=\"184\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"184\" x2=\"192\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"192\" x2=\"200\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"200\" x2=\"208\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"208\" x2=\"216\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"220\" x2=\"220\" y1=\"304\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"216\" x2=\"220\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"244\" x2=\"244\" y1=\"304\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"244\" x2=\"252\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"248\" x2=\"256\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"256\" x2=\"264\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"264\" x2=\"272\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"272\" x2=\"280\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"280\" x2=\"288\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"288\" x2=\"296\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"296\" x2=\"304\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"304\" x2=\"312\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"312\" x2=\"320\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"320\" x2=\"328\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"328\" x2=\"336\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"340\" x2=\"340\" y1=\"304\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line><line x1=\"336\" x2=\"340\" y1=\"312\" y2=\"312\" stroke=\"rgb(0,0,0)\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"mitter\"></line></svg></p>\n<p>The lowermost three boxes are the most interesting. Those are the queries, the\noperations we can use on a distribution to make sense of it.</p>\n<h1>The Expectation Monad</h1>\n<p>This section elaborates on a measure theoretic approach to the\nprobability monad. We represent the distribution as a continuation\nthat takes a measure function and returns an expectation. </p>\n<p>First, we will take a look at our monad type.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">-- Probability Monad</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">newtype PExp a = P (( a -&gt; Double) -&gt; Double)</span></span></code></pre>\n<p>It is worth looking into what happens here. We have the constructor\n<code>PExp</code>, that takes a function, a <em>measure function</em>, and gives the\nexpectation.</p>\n<p>The monadic structure of probability distributions is in this setting\nimplemented as follows.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">instance Monad P where</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  return x = P (\\h -&gt; h x)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  (P d) &gt;&gt;= k =</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">       P (\\h -&gt; let apply (P f) arg = f arg</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                    g x = apply (k x) h</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">                  in d g)</span></span></code></pre>\n<p>From here the expectation monad is easily implemented as the whole\nmonad type is built around it.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">instance ExpMonad PExp where</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  expectation h (PExp d) = d h</span></span></code></pre>\n<p>We could now start to do experiments base on this. But we would much\nrather like to play with <code>support</code> and <code>sample</code> also. These, however,\nare quite difficult to implement using the type of <code>PExp</code> so we are\ngoing to attack this from another angle.</p>\n<h1>Generalizing</h1>\n<p>The above implementation of the monad type is not very suited for other than\nthe expectation query. To make something more suited, we will try to stay true\nto the paper and implement a type such that we can hold data as it is defined</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">data P a where</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    R :: a -&gt; P a</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    B :: P a -&gt; (a -&gt; P b) -&gt; P b</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    C :: Probability -&gt; P a -&gt; P a -&gt; P a</span></span></code></pre>\n<p>The quick reader will see that we now use GADTs instead or ordinary ADTs. This\nis due to the <em>Bind</em> constructor. This poses a change of the type, and can not\nbe implemented by ADTs.</p>\n<p>In the instances for the monad and probability monad we simply defer everything\nto used directly by the queries.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">-- P is a monad</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">instance Monad P where</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  return x = R x</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  d &gt;&gt;= k  = B d k</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">-- P is a probability monad</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">instance ProbabilityMonad P where</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  choose p d1 d2 = C p d1 d2</span></span></code></pre>\n<p>This allows for definitions of the queries to be directly copied from the\npaper. In addition to the <code>expectation</code> query we had before, we can now also\ndo <code>support</code> and <code>sampling</code>.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">instance SupportMonad P where</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  support (R x) = [x]</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  support (B d k) = concat [support (k x) | x &lt;- support d]</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  support (C p d1 d2) = support d1 ++ support d2</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">...</span></span></code></pre>\n<p>For the full source I once again refer to the\n<a href=\"https://gist.github.com/madsbuch/5a8a1fc9b70621dd93dd70058754b126\">gist on Github</a>..</p>\n<h1>Making Distributions</h1>\n<p>In this section we will take a look on how to construct some interesting\ndistributions and how the queries work on them.</p>\n<p>As often when working with probabilities we will look on dices. To make\nit easy we make a data type deriving <code>Enum</code> for easy enumeration.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">data Dice = One | Two | Three | Four | Five | Six</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">  deriving (Enum, Eq, Show, Read, Ord)</span></span></code></pre>\n<p>We can from here make a uniform distribution over the sides of a dice\nlike following.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">dist :: P Dice</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">dist = uniform [One .. Six]</span></span></code></pre>\n<p>The <code>uniform</code> method is a simple function that makes a uniform distribution\nover a list using the <code>choose</code> function and the length of the list.</p>\n<p>A support query can be performed as follows.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">example01a =</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    let dist :: P Dice</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        dist = uniform [One .. Six]</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    in support dist</span></span></code></pre>\n<p>Yielding the result of <code>[One,Two,Three,Four,Five,Six]</code>. To create a posterior\ndistribution from a prior, we use the bind. This translates into elegant and\neasily readable dependent distributions.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"10\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">example02a =</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    let dist :: P Dice</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">        dist = do</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">          d &lt;- uniform [One .. Six]</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">          return (if d == Six then One else d)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">    in support dist</span></span></code></pre>\n<p>This yields the result <code>[One,Two,Three,Four,Five,One]</code> Note how <code>One</code> appears\ntwice. This make immediate sense from the definition of the distribution\nthough we would have expected duplications removed. However, this is quite\neasily done in Haskell.</p>\n<p>The last example in the gist is one of the more interesting. The distribution\ndefined is potentially infinite. Intuitively it counts up with a probability\nof a half and continues doing that.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"11\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">walk x =</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">   do bit &lt;- uniform [True, False]</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">      if bit then</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">          return x</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">      else walk (x + 1)</span></span></code></pre>\n<p>So what happens when we throw the various queries on it?</p>\n<p>The <strong>support</strong> query should return a list of all potential outcomes of the\ndistribution, and so it does. running <code>example03a</code> returns\n<code>[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,^CInterrupted.</code>. If not\nstopped it would continue counting up.</p>\n<p>An important remark here is that\norder of the <code>True</code> and <code>False</code> in the inner distribution. If we switch it\nsuch that the inner distribution reads <code>uniform [False, True]</code> it will never\nreturn anything. Intuitively one can understand it as the last element is\nunfold first. In the current ordering the walk function creates a list which\nis reducible though infinite.</p>\n<p>Secondly we  have the <strong>expectation</strong> query. issuing this yields an infinite\ncomputation without any result. This is because we attempt to unroll the full\nstructure to get the expectation of an event.</p>\n<p>Lastly we have the <strong>sample</strong> query. This query works completely as expected yielding a list of samples. Issuing <code>example03c</code> will return\n<code>[0,1,3,0,0,2,0,1,0,1]</code> which intuitively makes sense.</p>\n<p>Though the expectation query is not possible through the current implementation\nwe could actually still get an idea about the expectations using the Monte\nCarlo Technique. Sampling 10.000 elements of the walk distribution and taking\nthe frequency gives us following.</p>\n<pre class=\"grvsc-container default-light\" data-language=\"haskell\" data-index=\"12\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">&gt; mc</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">[(0,4937),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> (1,2483),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> (2,1294),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> (3,630),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> (4,326),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> (5,159),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> (6,87),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> (7,34),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> (8,27),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> (9,10),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> (10,5),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> (11,5),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> (12,2),</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"> (15,1)]</span></span></code></pre>\n<p>Which is what we would expect.</p>\n<h1>Concluding Remarks</h1>\n<p>We have discussed Ramsey and Pfeffer's paper in this article. It provides a\nfundamental understanding based in programming languages on distributions and\nprobabilities. It is interesting because it makes languages like Hakaru much\neasier to understand. Furthermore it also greatly simplifies the hole world of\nprobabilities.  </p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"},{"url":"/why-we-dont-need-authorization/","relativePath":"why-we-dont-need-authorization.md","relativeDir":"","base":"why-we-dont-need-authorization.md","name":"why-we-dont-need-authorization","frontmatter":{"title":"Why We Don’t Need Authorization","author":"Mads Buch","date":"2022-01-02","template":"post","excerpt":"Yet"},"html":"<p>A comment on doing what's necessary when necessary.</p>\n<p>Image this: You start out on a new project. You don't entirely know what you want to build, but you have a rough idea.\nThe energy is fierce and it is just about getting started. You think to your self: \"I might as well just get a couple of things in that I know I will need\". You have an idea, that probably a user needs to be able to create a user. And so the work begins!</p>\n<p>Something similar happened when I worked with a previous client.</p>\n<p>We were implementing a minimum viable product (MVP) based on their previous work. While scoping out the initial layout\nwe added in the sign-up flow for users. Upon deliberation with the client, we found that it was not strictly needed,\nso we removed it from the scope.</p>\n<p>To this day I am quite sure it still has not been implemented and is not needed. Money saved, more time with the family!</p>\n<p>Anyways, on the contrary, case complexities arise, we spend time tweaking the passwordless signup flow. We implement JWT cause, hey, we want to support microservices, and so on.</p>\n<p>As is usual, we lose interest in the problem, or we have to attend something else for a while, and the idea leaves us and flies to another person, or even worse, never gets built. Now managers are stuck with their inefficient ways of doing roadmaps and the scientists will need to figure out another way to organize their knowledge.</p>\n<p>An alternative is, at least in the beginning, to remove authorization from the idea and focus on what is essential. This requires us to make some realization, to change the vision, to build something different. This is hard, we need to get rid of our darling. But how is this possible?</p>\n<p>Say you want to make a shared word editor. Something like Google Docs. Would this be possible without an authorization system? Indeed it is. Actually, the predecessor to Google Docs was made without authorization. One would merely go to a website, start a new document, and share the link with their friends. The security would be in the random name of the document. Good enough for enterprise and national secrets? Merely, but good enough to showcase collaborative editing which has since been refined, with fully featured authorization into successful products.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>"},{"url":"/objects-and-state/","relativePath":"objects-and-state.md","relativeDir":"","base":"objects-and-state.md","name":"objects-and-state","frontmatter":{"title":"Objects and State","author":"Mads Buch","subtitle":"On objects vs. pure functions","excerpt":"Object oriented programming is inherently stateful. This fits well for some applications and can confuse and reduce reliability for others.","date":"2021-02-20","template":"post"},"html":"<p><em>Main point: Object oriented programming is inherently stateful. This fits well for some applications and can confuse and reduce reliability for others.</em></p>\n<p>Take a look at following code snippet:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">optimizer = new SGD(...)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">model1.train(example, optimizer)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">model2.train(example, optimizer)</span></span></code></pre>\n<p>A question bids: Should i initialize an optimizer for each model, or can I\nexpect the optimizer to be stateless and merely reference static functions?</p>\n<p>From the above example, it is not clear if the newly initialized object is stateful\nor stateless. Whether a computation unit is stateful or not is architecturally\nimportant. In the above example, the difference would yield bugs that are near\nimpossible to locate.</p>\n<p>On the other hand, take a look at following example:</p>\n<pre class=\"grvsc-container default-light\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\">optimizer = lambda x : ...</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">model1.train(example, optimizer)</span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\">model2.train(example, optimizer)</span></span></code></pre>\n<p>In that example, it is much more clear that the optimizer indeed is pure (if\nthat was intended). However, this is not idiomatic in a language like Python.\nFurthermore, we are not ensured that the <code>optimizer</code> function as defined is\nstateless. In languages like Python, JavaScript, Typescript, variables\noutside of scope can be mutated.</p>\n<p>In some languages, like Haskell, all functions are pure and all state has to\nbe supplied directly to the function. Here mutable structure can be implemented\nin monads. But when just skimming a program, the programmer always knows what\ndata a function has access to.</p>\n<p><strong>Pure functions</strong> are nice to use when statelessness is guaranteed. They are\nversatile, can be reused and worked into programs in flexible ways. The\nknowledge that a function is pure gives us peace at night. It is easier to\nverify that we did not overlook something.</p>\n<p><strong>Stateful objects</strong> are useful when working on complex data structures\nthat benefit from mutability. The class definitions allow for a clear separation\nbetween a clean interface a messy internal state. Classes provide the tools\nto ensure only operations that satisfy the invariants are publicly available.\nKey examples are tensors where we don't want to be wary about access to\nindividual elements but want to expose functions like addition, subtraction,\nmultiplication, dot product, etc.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .default-light {\n    background-color: #FFFFFF;\n    color: #000000;\n  }\n  .default-light .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(0, 0, 0, 0.05));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(0, 0, 0, 0.2));\n  }\n</style>"}],"site":{"siteMetadata":{"siteUrl":"https://www.madsbuch.com","description":"Mads Buch - A personal blog","header":{"title":"Mads Buch","tagline":"A personal blog","background_img":"images/header-bg.webp","has_nav":true,"nav_links":[{"label":"Home","url":"/","style":"link"},{"label":"About","url":"/about","style":"link"},{"label":"Now","url":"/now","style":"link"},{"label":"Understanding Data","url":"/data","style":"link"}],"has_social":true,"social_links":[{"label":"Mastodon","url":"https://functional.cafe/@madsbuch","style":"icon","icon_class":"fa-mastodon","new_window":true},{"label":"GitHub","url":"https://github.com/madsbuch","style":"icon","icon_class":"fa-github","new_window":true},{"label":"LinkedIn","url":"https://www.linkedin.com/in/madsbuch/","style":"icon","icon_class":"fa-linkedin","new_window":true}]},"footer":{"content":"&copy; All rights reserved.","links":[]},"palette":"yellow","title":"Mads Buch [dot] Com"},"pathPrefix":"","data":{}}}},"staticQueryHashes":[]}