<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>technobable</title>
    <description>programing languages and understanding computers
</description>
    <link>http://rahne.si/</link>
    <atom:link href="http://rahne.si/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Wed, 14 Jan 2026 07:32:31 +0100</pubDate>
    <lastBuildDate>Wed, 14 Jan 2026 07:32:31 +0100</lastBuildDate>
    <generator>Jekyll v4.3.4</generator>
    
      <item>
        <title>Solving Steve Ballmer&apos;s Interview Riddle with Game Theory</title>
        <description>&lt;h1 id=&quot;the-quest&quot;&gt;The quest&lt;/h1&gt;

&lt;p&gt;Imagine that one day, billionaire Steve Ballmer invites you to join his mission. Before you can sign on, he poses a challenge:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“I am thinking of an integer between 1 and 100. You must guess the number. After each guess, I will tell you if my number is higher or lower. Each guess costs you $1. You must keep playing until you get it right.” &lt;sup id=&quot;fnref:ytsteve&quot;&gt;&lt;a href=&quot;#fn:ytsteve&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot; role=&quot;doc-noteref&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The question is: &lt;strong&gt;How much money should Steve expect to get from you on average?&lt;/strong&gt;&lt;/p&gt;

&lt;h4 id=&quot;take-a-deep-breath&quot;&gt;Take a deep breath&lt;/h4&gt;
&lt;p&gt;…and ponder the implications of an adversary who &lt;em&gt;wants&lt;/em&gt; you to lose.&lt;/p&gt;

&lt;h1 id=&quot;a-simpler-problem&quot;&gt;A Simpler Problem&lt;/h1&gt;

&lt;p&gt;Let’s start with only 3 numbers. What is the expected number of questions?&lt;/p&gt;

&lt;h4 id=&quot;steve-as-the-honest-randomizer&quot;&gt;Steve as the honest randomizer&lt;/h4&gt;
&lt;p&gt;If we assume Steve picks a number \((1, 2, \text{ or } 3)\) uniformly at random, our best strategy is to guess &lt;strong&gt;2&lt;/strong&gt;.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;If correct (1/3 probability): 1 guess.&lt;/li&gt;
  &lt;li&gt;If incorrect (2/3 probability): 2 guesses (since the “higher/lower” hint reveals the remaining number).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Expected value is:&lt;/p&gt;

\[E_{uniform} = 1 \cdot P(\text{1st try}) + 2 \cdot P(\text{2nd try})\]

\[E_{uniform}(3) = 1 \cdot \left(\frac{1}{3}\right) + 2 \cdot \left(\frac{2}{3}\right) = \frac{5}{3} \approx 1.667\]

&lt;h4 id=&quot;steve-as-the-tricky-adversary&quot;&gt;Steve as the tricky adversary&lt;/h4&gt;

&lt;p&gt;Steve didn’t become a billionaire by being dumb. If he knows you will always guess number 2 in first try, he will &lt;em&gt;avoid&lt;/em&gt; picking it. Instead, he can pick 1 or 3, forcing you to always pay $2. In this scenario, your \(E_v\) against his best response becomes 2.0.&lt;/p&gt;

&lt;p&gt;For the 3-number game, the optimal strategies for both players are probabilistic (mixed strategies).&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Steve’s Strategy: To maximize your cost, Steve should pick 1 and 3 with 40% probability each, and 2 only 20% of the time.&lt;/li&gt;
  &lt;li&gt;Your Strategy: To minimize your loss, you should guess 2 60% of the time, and 1 or 3 20% of the time each.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Under these optimal conditions, the game settles at a value of $1.80 per game. This is the Nash equilibrium. Neither player can improve their outcome by changing their strategy alone.&lt;/p&gt;

&lt;h1 id=&quot;solving-with-linear-programming&quot;&gt;Solving with Linear Programming&lt;/h1&gt;

&lt;p&gt;We can model this zero-sum game using a payoff matrix \(A\), where Steve (player 1) chooses rows \(x\) and you (player 2) choose columns \(y\).&lt;/p&gt;

&lt;p&gt;The objective is to find:&lt;/p&gt;

\[\min_y \max_x (x^T A y) \quad \text{subject to}  \quad B x = b,  \quad  Cy = c,  \quad  x,y \ge 0\]

&lt;p&gt;This can be transformed into a standard Linear Programming (LP) problem: &lt;sup id=&quot;fnref:stgl&quot;&gt;&lt;a href=&quot;#fn:stgl&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot; role=&quot;doc-noteref&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

\[\max (c&apos;z)  \quad \text{subject to}  \quad Bx = b,  \quad C&apos;z \le A&apos;x\]

&lt;h4 id=&quot;from-game-tree-to-game-graph&quot;&gt;From Game Tree to Game Graph&lt;/h4&gt;

&lt;p&gt;For a 3-number game, the strategy tree is manageable. As \(n\) grows, the number of possible strategy paths in game tree blows up exponentially, making \(n=100\) impossible to solve as a raw tree.&lt;/p&gt;

&lt;p&gt;We can reduce the state space by recognizing that the game is a &lt;strong&gt;Directed Acyclic Graph (DAG)&lt;/strong&gt;. A strategy for guessing a range \([a, b]\) does not depend on &lt;em&gt;how&lt;/em&gt; you narrowed it down to that range.&lt;/p&gt;

&lt;p&gt;The resulting game graph is a “folded” game tree once the tree is converted into &lt;strong&gt;sequence form&lt;/strong&gt;.&lt;sup id=&quot;fnref:stgl:1&quot;&gt;&lt;a href=&quot;#fn:stgl&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot; role=&quot;doc-noteref&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Nodes: Represented as \((a, b)\), the remaining range. There are \(O(n^2)\) such nodes.&lt;/li&gt;
  &lt;li&gt;Edges: Represented as \((a, b, m)\), where \(m\) is the number you guess from strategy\((a, b)\). There are \(O(n^3)\) such vertices.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints&lt;/strong&gt; are written for each node such that the sum of incoming vertices equals the sum of outgoing vertices. The sum of root strategies is 1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Payout matrix:&lt;/strong&gt; \(A\) is constructed such that column \(j\) corresponds to the index \((a,b,k)\). We have \(A_{s,j} = 1\) when \(s\) is the number Steve picked, provided \(a \le s \le b\). When \(s\) is outside the closed interval \([a,b]\), we have \(A_{s,j} = 0\).&lt;/p&gt;

&lt;h4 id=&quot;symmetries&quot;&gt;Symmetries&lt;/h4&gt;

&lt;p&gt;We can further reduce the game graph by encoding symmetries. For a game of size \(n\) (indexed from \(0\) to \(n−1\)), the value at vertex \((a,b,k)\) is identical to the value at vertex \((n−1−b,n−1−a,n−1−k)\). From Steve’s perspective, the probability of picking number x is equal to the probability of picking number \((n−1)−x\). Encoding these symmetries significantly reduces the number of variables the LP solver must handle.&lt;/p&gt;

&lt;h1 id=&quot;result&quot;&gt;Result&lt;/h1&gt;

&lt;p&gt;By feeding this graph-encoded LP into a solver, we find the exact value for \(n=100\):&lt;/p&gt;

\[E_v(100) = \frac{296}{51} \approx 5.8039\]

&lt;p&gt;Interestingly, Steve’s optimal distribution follows a specific pattern: he favors the “edges” of the search space. As noted by &lt;a href=&quot;https://bowaggoner.com/blahg/2024/09-06-adversarial-binary-search/&quot;&gt;Bo Waggoner&lt;/a&gt;, the probability of Steve picking \(1\) or \(100\) is \(\frac{2}{102}\), while the inner numbers \(2\) through \(99\) are picked with a probability of \(\frac{1}{102}\).&lt;/p&gt;

&lt;p&gt;The solver takes approximately 20 minutes on a modern desktop to reach this result.&lt;/p&gt;

&lt;p&gt;Note: When Steve picks each number uniformly with probability \(\frac{1}{100}\) we have&lt;/p&gt;

\[E_{uniform}(100) =  1 \cdot \frac{1}{100} + 2 \cdot \frac{2}{100} + 3 \cdot \frac{4}{100} + 4 \cdot \frac{8}{100} + 5 \cdot \frac{16}{100} + 6 \cdot \frac{32}{100} + 7 \cdot \frac{100-63}{100} = \frac{29}{5} = 5.8\]

&lt;p&gt;Code for solver is available here: &lt;a href=&quot;https://github.com/ra1u/steve-game&quot;&gt;github.com/ra1u/steve-game&lt;/a&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://possiblywrong.wordpress.com/2024/09/04/analysis-of-adversarial-binary-search-game/&quot;&gt;Analysis of Adversarial Binary Search Game&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://quuxplusone.github.io/blog/2024/09/04/the-game-is-flawed/&quot;&gt;Nash Equilibria in Ballmer’s Interview Game&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://bowaggoner.com/blahg/2024/09-06-adversarial-binary-search/&quot;&gt;Adversarial Binary Search (How to Fleece Ballmer)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:ytsteve&quot;&gt;
      &lt;p&gt;Steve Ballmer explains the riddle on &lt;a href=&quot;https://www.youtube.com/watch?v=svCYbkS0Sjk&amp;amp;t=35s&quot;&gt;YouTube&lt;/a&gt; &lt;a href=&quot;#fnref:ytsteve&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:stgl&quot;&gt;
      &lt;p&gt;D. Koller, N. Megiddo, and B. von Stengel (1996), &lt;a href=&quot;http://www.maths.lse.ac.uk/personal/stengel/TEXTE/geb1996b.pdf&quot;&gt;Efficient computation of equilibria for extensive two-person games.&lt;/a&gt; &lt;a href=&quot;#fnref:stgl&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt; &lt;a href=&quot;#fnref:stgl:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Wed, 07 Jan 2026 14:37:00 +0100</pubDate>
        <link>http://rahne.si/optimisation/2026/01/07/steve-ballmer-interview.html</link>
        <guid isPermaLink="true">http://rahne.si/optimisation/2026/01/07/steve-ballmer-interview.html</guid>
        
        
        <category>optimisation</category>
        
      </item>
    
      <item>
        <title>Automatc frequency domain analysis of digital ciruits</title>
        <description>&lt;p&gt;This post is about design and analysis of digital circuits using haskell
as a language and clash as tool to generate digital circuits.&lt;/p&gt;

&lt;h3 id=&quot;main-idea&quot;&gt;Main Idea&lt;/h3&gt;

&lt;p&gt;Frequency analysis can be preformed for digital circuits using only
adders, multiplies and register. Intuitive reasoning to get response of
single frequency from such circuitry as black box goes as. We insert
Signal of complex numbers in circuitry and from resulting complex signal
observe phase and amplitude. Registry in this setup is no more than
rotation of phase or phase delay that depends on Nyquist frequency of
input signal.&lt;/p&gt;

&lt;p&gt;Analysis is not done in time, but directly in frequency domain. For one
input frequency we get amplitude and phase of output. Approach works
best for acyclic circuits, both linear (e.g. &lt;a href=&quot;https://en.wikipedia.org/wiki/Finite_impulse_response&quot;&gt;fir
filter&lt;/a&gt;) and
nonlinear circuits (e.g. frequency mixer). It can be used for linear
cyclic circuits for example &lt;a href=&quot;https://en.wikipedia.org/wiki/Infinite_impulse_response&quot;&gt;iir
filter&lt;/a&gt;.
Anaysis result can be presented as
&lt;a href=&quot;https://en.wikipedia.org/wiki/Bode_plot&quot;&gt;Bode&lt;/a&gt; or
&lt;a href=&quot;https://en.wikipedia.org/wiki/Nyquist_plot&quot;&gt;Nyquist&lt;/a&gt; plot. Experimental
hacked up implementation is available at
&lt;a href=&quot;https://github.com/ra1u/frequency-response&quot;&gt;github&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;example-of-use&quot;&gt;Example of use&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://www.clash-lang.org/&quot;&gt;Clash&lt;/a&gt; compiler can be used to both design
digital circuitry and analyze them using the great language Haskell.
Both in time and frequency domain. Analysis in time domain is part of
standard library that ships with distribution.&lt;/p&gt;

&lt;p&gt;We can describe fir filter in language haskell for example as:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fir coeffs x = dotp coeffs (windowP x)
  where
    dotp as bs = sum (zipWith (*) as bs)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This description enables generating digital circuit by compiling
in vhdl or verilog language and than burn this in FPGA or in ASIC.&lt;/p&gt;

&lt;p&gt;We begin by generating fir coeficients. For example by using python library 
&lt;a href=&quot;https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.firwin2.html&quot;&gt;scipy&lt;/a&gt;.
There is also haskell library &lt;a href=&quot;https://hackage.haskell.org/package/dsp&quot;&gt;dsp&lt;/a&gt; that
can do same.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; from scipy import signal as s
&amp;gt;&amp;gt;&amp;gt; s.firwin2(101,freq=[0,0.5,0.55,1],gain=[1,1,1e-5,1e-5],nfreqs=1024).tolist()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;this generates 101 fir coefficient-s with gain 1 in interval [0,0.55]
and gain 1e-5 in interval [0.55,1] where interval represents Nyquist
frequency. Nyquist frequency is frequency scaled from 0 to 1/2 of sampling frequency.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;firCoef = -6.478374934754401e-05 :&amp;gt; 6.739792048055188e-05 :&amp;gt; 5.077367641329531e-05 :&amp;gt; -7.20036873194064e-05 :&amp;gt; -3.7245153312760294e-05 :&amp;gt; 7.249856664305871e-05 :&amp;gt; 2.2414720687380852e-05 :&amp;gt; -6.155684145140234e-05 :&amp;gt; -7.664810105984746e-06 :&amp;gt; 2.85804248573473e-05 :&amp;gt; -3.6152309490646324e-08 :&amp;gt; 3.86526486767599e-05 :&amp;gt; -1.4006284245813091e-05 :&amp;gt; -0.00015120836046040963 :&amp;gt; 7.361032727845754e-05 :&amp;gt; 0.0003152579566734551 :&amp;gt; -0.00021195524189591408 :&amp;gt; -0.0005277210648487832 :&amp;gt; 0.0004703572783638681 :&amp;gt; 0.0007717574734980716 :&amp;gt; -0.0008954627919597703 :&amp;gt; -0.001012627857164383 :&amp;gt; 0.0015349575580967327 :&amp;gt; 0.0011943982184525286 :&amp;gt; -0.002432123228258949 :&amp;gt; -0.0012377957439342097 :&amp;gt; 0.0036197818250699866 :&amp;gt; 0.0010392111520942885 :&amp;gt; -0.005114324388627113 :&amp;gt; -0.0004703430147278584 :&amp;gt; 0.00691059332253219 :&amp;gt; -0.0006228059544207712 :&amp;gt; -0.008978362677236961 :&amp;gt; 0.0024241985527292402 :&amp;gt; 0.011261032711201649 :&amp;gt; -0.005160469774066745 :&amp;gt; -0.013676935125896208 :&amp;gt; 0.00913292632920996 :&amp;gt; 0.016123357472055417 :&amp;gt; -0.014794308598846265 :&amp;gt; -0.01848307417265467 :&amp;gt; 0.02293937546874407 :&amp;gt; 0.02063285839843857 :&amp;gt; -0.03523405731548666 :&amp;gt; -0.022453185292383104 :&amp;gt; 0.05602454640129296 :&amp;gt; 0.02383815920968249 :&amp;gt; -0.10138553138188472 :&amp;gt; -0.024704631737024998 :&amp;gt; 0.3167115011351708 :&amp;gt; 0.5250045350057191 :&amp;gt; 0.3167115011351717 :&amp;gt; -0.024704631737024647 :&amp;gt; -0.10138553138188545 :&amp;gt; 0.023838159209682245 :&amp;gt; 0.05602454640129442 :&amp;gt; -0.02245318529238117 :&amp;gt; -0.03523405731548606 :&amp;gt; 0.020632858398438054 :&amp;gt; 0.02293937546874406 :&amp;gt; -0.018483074172654032 :&amp;gt; -0.014794308598846393 :&amp;gt; 0.01612335747205424 :&amp;gt; 0.009132926329209059 :&amp;gt; -0.013676935125896086 :&amp;gt; -0.005160469774066614 :&amp;gt; 0.011261032711201845 :&amp;gt; 0.002424198552728965 :&amp;gt; -0.008978362677237601 :&amp;gt; -0.0006228059544210945 :&amp;gt; 0.006910593322532333 :&amp;gt; -0.0004703430147276999 :&amp;gt; -0.005114324388627051 :&amp;gt; 0.0010392111520945455 :&amp;gt; 0.0036197818250703288 :&amp;gt; -0.001237795743934232 :&amp;gt; -0.0024321232282593064 :&amp;gt; 0.0011943982184523566 :&amp;gt; 0.0015349575580969096 :&amp;gt; -0.0010126278571641848 :&amp;gt; -0.0008954627919598041 :&amp;gt; 0.000771757473497906 :&amp;gt; 0.00047035727836372234 :&amp;gt; -0.0005277210648488565 :&amp;gt; -0.0002119552418958731 :&amp;gt; 0.0003152579566736044 :&amp;gt; 7.361032727858147e-05 :&amp;gt; -0.0001512083604604247 :&amp;gt; -1.4006284245899888e-05 :&amp;gt; 3.865264867673136e-05 :&amp;gt; -3.6152309459163916e-08 :&amp;gt; 2.8580424857355213e-05 :&amp;gt; -7.66481010600899e-06 :&amp;gt; -6.15568414514097e-05 :&amp;gt; 2.24147206873927e-05 :&amp;gt; 7.249856664305469e-05 :&amp;gt; -3.724515331277384e-05 :&amp;gt; -7.200368731939395e-05 :&amp;gt; 5.0773676413330495e-05 :&amp;gt; 6.739792048057118e-05 :&amp;gt; -6.478374934755455e-05 :&amp;gt; Nil
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We define our circuitry as&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;firCircuit x = fir firCoef x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Result of calculating response of circuit firCircuit can be presented as
following graph&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/data/fir_freq_response.png&quot; alt=&quot;Analysis of circuitry describing fir filter&quot; /&gt;&lt;/p&gt;

&lt;p&gt;x axis represent frequency from 0 to Nyquist frequency and y is gain in
dB. More details regarding ploting is available in testing code example
available at &lt;a href=&quot;https://github.com/ra1u/frequency-response&quot;&gt;github&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;details&quot;&gt;Details&lt;/h3&gt;

&lt;p&gt;Analysis can be precisely performed on acyclic circuits for both linear
and some nonlinear systems. By applying iterations it is possible to
analyze also cyclic circuits. Only restriction is that
circutry is defined by input type that is both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Num&lt;/code&gt; and
&lt;a href=&quot;/programming/clash/fpga/2016/12/23/iso-circuits.html#prependable&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Prependable&lt;/code&gt;&lt;/a&gt;.
Prepend is using polymorphic version of register called prepend.&lt;/p&gt;

&lt;p&gt;Naive idea I started to work from is, that we push inside signal of
complex numbers. Addition is addition of complex numbers and similar for
multiplication. This should work fine at least for &lt;a href=&quot;https://en.wikipedia.org/wiki/Linear_time-invariant_theory&quot;&gt;linear
circuits&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Cyclic circuits can be are analyzed getting more values from output
stream and waiting for result to converge. It works for analyzing iir
filters.&lt;/p&gt;

&lt;p&gt;For mixing/nonlinear circuits multiplication of spectrum is needed.
Multiplying two &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sin&lt;/code&gt; functions is expressed as following identity.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sin(a) * sin(b) = 1/2 (cos(a-b) - cos(a+b))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;or when dealing in single frequency we get.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sin(x + a) * sin(x + b) == 
1/2 (cos(a - b) - cos(2x + a + b)) ==
1/2 (sin(a - b + pi/2) - sin(2x + a + b + pi/2))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Multiplying two signals on same frequency one with phase &lt;em&gt;a&lt;/em&gt; and other
with phase &lt;em&gt;b&lt;/em&gt; results in signal decomposed in DC components and other
with signal having double frequency.&lt;/p&gt;

&lt;p&gt;Haskell type that holds phases on multiple harmonics can be
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map Int (Complex Double)&lt;/code&gt; Where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt; represents harmonics and
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Complex Double&lt;/code&gt; size and phase of a signal at that harmonics frequency.
In general &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Num&lt;/code&gt; class is needed for such type. That are function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt;.&lt;/p&gt;

&lt;h4 id=&quot;acyclic-circuits&quot;&gt;acyclic circuits&lt;/h4&gt;

&lt;p&gt;What remains for such system is phase rotation. When we delay
signal using register, we need to tell register how to delay input spectrum.&lt;/p&gt;

&lt;p&gt;If circuit is acyclic, single value wrapped in type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AnalyzerAcyclic&lt;/code&gt; is
enough for exact analysis. This represents type of input stream and is
type used as input and output of circuit.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;data AnalyzerAcyclic s = 
        AnalyzerAcyclic {unAnalyzerAcyclic :: (Maybe (s -&amp;gt; s) , s)}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;First type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Maybe (s -&amp;gt; s)&lt;/code&gt; is spectrum transfer function. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s -&amp;gt; s&lt;/code&gt; is
function for rotating input used by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;registerP&lt;/code&gt; function is wrapped in
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Maybe&lt;/code&gt;, because instances of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AnalyzerAcyclic&lt;/code&gt; for example
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fromInteger&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fromRational&lt;/code&gt; are unable to provide better than value
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Nothing&lt;/code&gt;. For example function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(+)&lt;/code&gt; requires forwarding valid
transform function as output. Second type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s&lt;/code&gt; is input spectrum for
example &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map Int (Complex Double)&lt;/code&gt; and represents amplitudes and phases
on all harmonics frequencies.&lt;/p&gt;

&lt;h4 id=&quot;cyclic-circuits&quot;&gt;cyclic circuits&lt;/h4&gt;

&lt;p&gt;Simple example of cyclic circuit would be&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;integrator sig = r where 
    r = registerP 0 r + sig
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;registerP is polymorphic version of register called prepend from class
Prepenable with origin from &lt;a href=&quot;/programming/clash/fpga/2016/12/23/iso-circuits.html#prependable&quot;&gt;prevous
post&lt;/a&gt;
and lack of theoretical foundations. Some care is required to avoid
bottom or infinite loop that would happen using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AnalyzerAcyclic&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Current solution is that stream is used as input. In output stream we
wait for values to converge. Type that works for our case is.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;data Analyzer s = Analyzer {unAnalyzer :: ([Maybe (s -&amp;gt; s)] , Signal s)}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;First value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Maybe (s -&amp;gt; s)]&lt;/code&gt; is list of spectrum transfer functions,
that is function that rotates input signal when register is reached.
Second type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal s&lt;/code&gt; is stream of spectrum. For &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;registryP&lt;/code&gt; is
required to retrieve spectrum transformation in &lt;a href=&quot;https://en.wikipedia.org/wiki/Breadth-first_search&quot;&gt;breadth-first
order&lt;/a&gt;. Traversing
to spectrum rotate function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s -&amp;gt; s&lt;/code&gt; in depth first order, would halt
evaluation and would reach bottom.&lt;/p&gt;

&lt;p&gt;For this type we  need to implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ǹum&lt;/code&gt; instance.&lt;/p&gt;

&lt;p&gt;Example of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt; is.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;da + db = FreqAn (zipWith (&amp;lt;|&amp;gt;) fa fb, liftA2 (+) a b) where
         (fa,a) = unFreqAn da
         (fb,b) = unFreqAn db
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That is, spectrum transfer function is generated by merging two spectrum
transfer functions one from argument &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;da&lt;/code&gt; and other from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;db&lt;/code&gt; input. Addition
is done trough Applicative instance of underlaying input type that 
needs to be instance of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Applicative&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;what-next&quot;&gt;What next&lt;/h3&gt;

&lt;p&gt;Experimental library and some playground code in test.hs is available at
&lt;a href=&quot;https://github.com/ra1u/frequency-response&quot;&gt;github&lt;/a&gt;. It is not yet
clear how to make &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Functor&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Applicative&lt;/code&gt; instance of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Analyzer&lt;/code&gt; and
family. Also Prependable typclass as used here and previously to
describe parallel circuits seems nice and useful, but has no theoretical
foundations I am aware of and for extra only law it I came up with is
broken in implementation of AnalyzerAcyclic. It is not clear what kind
of nonlinear circuits beside frequency mixers are possible to analyze
using such approach. There is missing bunch of utility functions and
overall many questions remains open.&lt;/p&gt;

&lt;p&gt;Fell free to reach me over git or irc at irc.freenode.com. I am hanging
on channels #haskell and #clash-lang under nick ralu but you can also
send mails on address starting with luka and ending with name of this
domain.&lt;/p&gt;

</description>
        <pubDate>Sat, 15 Jul 2017 15:37:00 +0200</pubDate>
        <link>http://rahne.si/programming/clash/fpga/2017/07/15/frequency-anaysis.html</link>
        <guid isPermaLink="true">http://rahne.si/programming/clash/fpga/2017/07/15/frequency-anaysis.html</guid>
        
        
        <category>programming</category>
        
        <category>clash</category>
        
        <category>fpga</category>
        
      </item>
    
      <item>
        <title>Isomorphism of digital circuits</title>
        <description>&lt;p&gt;Building software in haskell is frequently expressing problem in one
domain and then transfer it into another. Previous post &lt;a href=&quot;/programming/clash/fpga/2016/02/28/remote-core-call.html&quot;&gt;Remote fpga
call&lt;/a&gt; was
about using type system to use and define circuits in both hardware and
software.&lt;/p&gt;

&lt;p&gt;Plan for this post is to show how to define circuits (using
&lt;a href=&quot;/programming/2016/02/28/funcional-hadware.html&quot;&gt;clash&lt;/a&gt; in haskell) for
two different and related domains. One domain are circuits using
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal a&lt;/code&gt; that represent periodic clock domain where value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; (like
Bool or Signed 5) changes at each clock edge. Frequency and polarity of
clock change are not relevant, but they are same for whole circuitry.
This are synchronous digital circuits where whole circuitry operates on
same clock.&lt;/p&gt;

&lt;p&gt;Other domain is about digital synchronous circuits that at each clock
receives and process &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; values of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt;. We call such domain multi
data domain or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MultiSignal&lt;/code&gt;. Type of this data stream is
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal (Vec n a)&lt;/code&gt; where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec n a&lt;/code&gt; is homogeneous list of exactly &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;
elements each having type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt;. Such signals represents for example
serialized data received over high speed bus or
&lt;a href=&quot;https://en.wikipedia.org/wiki/Analog-to-digital_converter&quot;&gt;ADC&lt;/a&gt; that in
one clock cycle sends more than one sample acquired at higher frequency.&lt;/p&gt;

&lt;p&gt;We are on quest to figure out how to express circuits in both Signal and
MultiSignal domain.&lt;/p&gt;

&lt;h2 id=&quot;whole-idea-summary&quot;&gt;Whole idea summary&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MultiSignal n a&lt;/code&gt; is defined as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal (Vec n a)&lt;/code&gt; as explained
earlier. After providing instance of Functor and Applicative for such
type new type-class &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Prependable&lt;/code&gt; is required that generalize over
delay/register with instance for both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Multisignal n&lt;/code&gt;. This
few type-classes are enough to describe synchronous circuits in both
domains (Signal and MultiSignal). Finally there are few examples
on how to implement primitive building blocks in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Multisignal n&lt;/code&gt;
as defined with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal&lt;/code&gt; in clash-prelude library. Library&lt;/p&gt;

&lt;h2 id=&quot;functor&quot;&gt;Functor&lt;/h2&gt;

&lt;p&gt;Note: If you are familiar with Functors and Applicative you can skip to &lt;a href=&quot;#prependable&quot;&gt;prependable&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will say that
&lt;a href=&quot;hackage.haskell.org/package/clash-prelude/docs/CLaSH-Signal.html#t:Signal&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal&lt;/code&gt;&lt;/a&gt;
has property of a functor. Functor &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; as used in haskell defines
function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap&lt;/code&gt; of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(a -&amp;gt; b) -&amp;gt; f a -&amp;gt; f b&lt;/code&gt;. Additionally it
requires that law &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap id == id&lt;/code&gt; holds.&lt;/p&gt;

&lt;p&gt;When using with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal&lt;/code&gt; function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap&lt;/code&gt; has type of.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fmap :: (a -&amp;gt; b) -&amp;gt; Signal a -&amp;gt; Signal b
-- or equivalently
fmap :: (a -&amp;gt; b) -&amp;gt; (Signal a -&amp;gt; Signal b)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap&lt;/code&gt; converts combinatorial circuit having type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a -&amp;gt; b&lt;/code&gt; to
synchronous circuit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal a -&amp;gt; Signal b&lt;/code&gt;. For example, combinatorial
function that tels if value is positive can be naively expressed as.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;isPositive x = x &amp;gt;= 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Type signature of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isPositive&lt;/code&gt; is deduced automatically by compiler as
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(Ord a, Num a) =&amp;gt; a -&amp;gt; Bool&lt;/code&gt;. This tels us that such circuit can be
synthesized for any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; that is both
&lt;a href=&quot;http://hackage.haskell.org/package/base/docs/Data-Ord.html#t:Ord&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ord&lt;/code&gt;&lt;/a&gt;-erable
and can be compared against. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; additionally needs to be
&lt;a href=&quot;http://hackage.haskell.org/package/base/docs/Prelude.html#t:Num&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Num&lt;/code&gt;&lt;/a&gt;-ber.
Only those types that has property of Num can be compared against &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;.
Function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isPositive&lt;/code&gt; takes value of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; and returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bool&lt;/code&gt;ean.&lt;/p&gt;

&lt;p&gt;Additionally we can manually define less generic and more restrictive
type signature. To define for input that is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signed 6&lt;/code&gt; for example.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;isPositive :: Signed 6 -&amp;gt; Bool
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Composing combinatorial circuits with synchronous circuits requires that
we change function of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signed 6 -&amp;gt; Bool&lt;/code&gt; to function of type
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal (Signed 6) -&amp;gt; Signal Bool&lt;/code&gt; .&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;isPositiveSig :: Signal (Signed 6) -&amp;gt; Signal Bool
isPositiveSig = fmap isPositive
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Functor for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal&lt;/code&gt; with function fmap provide transformation from
combinatorial circuit to synchronous. Functor additionally requires that
law &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap id == id&lt;/code&gt; holds, where id is identity function implemented as
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id x = x&lt;/code&gt;. This law guarantees that Signal behaves well and one can not
implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap&lt;/code&gt; using
&lt;a href=&quot;http://hackage.haskell.org/package/clash-prelude-0.10.14/docs/CLaSH-Signal.html#v:register&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;register&lt;/code&gt;&lt;/a&gt;
without breaking such law. Transformation from combinatorial to
synchronous circuit is trivial in hardware level. What happens is that
only type signature changes, but transformed circuitry remains same.&lt;/p&gt;

&lt;p&gt;Before going with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MultiSignal&lt;/code&gt; let’s take a look in functor properties
of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec n&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec n a&lt;/code&gt; is homogeneous list of elements with length &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;
and type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt;. For example &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec 5 (Signed 6)&lt;/code&gt; is array of 5 elements each
having type of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signed 6&lt;/code&gt;. Functor &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec n&lt;/code&gt; has function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap&lt;/code&gt;
with type&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fmap :: (a -&amp;gt; b) -&amp;gt; Vec n a -&amp;gt; Vec n b
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isPositive :: Signed 6 -&amp;gt; Bool&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec 5 (Signed 6)&lt;/code&gt; we can
make &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec 5 Bool&lt;/code&gt; using fmap&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; isArrayPositve :: Vec 5 (Signed 6) -&amp;gt; Vec 5 Bool
 isArrayPositve arr = fmap isPositive arr
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;or also&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; isArrayPositve :: Vec 5 (Signed 6) -&amp;gt; Vec 5 Bool 
 isArrayPositve = fmap isPositive
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Fmap is function that transform function operating over single value
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signed 6 -&amp;gt; Bool&lt;/code&gt; in function operating over vector
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec 5 (Signed 6) -&amp;gt; Vec 5 Bool&lt;/code&gt;. In this case fmap makes 5 instances of
function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isPositive&lt;/code&gt; in hardware to generate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isArrayPositve&lt;/code&gt;. We trust
implementor (or check source) of library that fmap for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec n&lt;/code&gt; obeys law
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap id == id&lt;/code&gt;. That is, transforming each element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec n a&lt;/code&gt; with
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt; is same as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec n a&lt;/code&gt; applied on function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;functor-for-multisignal&quot;&gt;Functor for MultiSignal&lt;/h3&gt;

&lt;p&gt;First we define type&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;data MultiSignal n a = MultiSignal {unMultiSignal :: Signal (Vec n a) }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MultiSignal&lt;/code&gt; is similar to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec&lt;/code&gt; and parametrized over 2 types. First
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; is type level length, because length has to be known at compile time
and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; is underlying type. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MultiSignal&lt;/code&gt; on left side of = is type
definition and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MultiSignal&lt;/code&gt; on right side of = is constructor having type
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal (Vec n a) -&amp;gt; MultiSignal n a&lt;/code&gt; . To get &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal (Vec n a)&lt;/code&gt; from
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MultiSignal n a&lt;/code&gt; there is
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unMultiSignal :: MultiSignal n a -&amp;gt; Signal (Vec n a)&lt;/code&gt; available.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MultiSignal&lt;/code&gt; has property of being Functor. We define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MultiSignal&lt;/code&gt;
being &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Functor&lt;/code&gt; as&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;instance Functor (MultiSignal n) where
    fmap f s = MultiSignal ( fmap ( fmap f ) ( unMultiSignal s ) )
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We first apply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unMultiSignal&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s&lt;/code&gt; so we get &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal (Vec n a)&lt;/code&gt;
from type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MultiSignal n a&lt;/code&gt;. There are two &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap&lt;/code&gt; used here. Leftmost is
over &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal&lt;/code&gt; where rightmost fmap is over &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec&lt;/code&gt; . Resulting type of
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap ( fmap f ) ( unMultiSignal s )&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal (Vec n a)&lt;/code&gt; and we
finally apply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MultiSignal&lt;/code&gt; constructor to make result type
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MultiSignal n a&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;applicative&quot;&gt;Applicative&lt;/h2&gt;

&lt;p&gt;So far we have functor that enables converting combinatorial circuits of
type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a -&amp;gt; b&lt;/code&gt; to synchronous having type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal a -&amp;gt; Signal b&lt;/code&gt; . What
remains is extending such concept to converting functions having more
than one arrow. For example transforming &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a -&amp;gt; b -&amp;gt; c -&amp;gt; d&lt;/code&gt; in
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal a -&amp;gt; Signal b -&amp;gt; Signal c -&amp;gt; Signal d&lt;/code&gt;. Additionally we need
concept of delaying &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal&lt;/code&gt; using &lt;a href=&quot;https://en.wikipedia.org/wiki/Flip-flop_%28electronics%29#D_flip-flop&quot;&gt;register or d flip
flop&lt;/a&gt;.
Applicative enables transforming from combinatorial to synchronous
circuits for arbitrary number of arrows.&lt;/p&gt;

&lt;p&gt;Applicative class requires two functions, to be defined.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Functor f =&amp;gt; Applicative f where
    pure :: a -&amp;gt; f a
    (&amp;lt;*&amp;gt;) :: f (a -&amp;gt; b) -&amp;gt; f a -&amp;gt; f b
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For signal we need&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-- For Signal
pure :: a -&amp;gt; Signal a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;pure changes value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal a&lt;/code&gt;. On each clock change we get same
value. Example: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pure True&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal Bool&lt;/code&gt; having boolean True as value on each
clock.&lt;/p&gt;

&lt;p&gt;Function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&amp;lt;*&amp;gt;)&lt;/code&gt; can be used as either operator or function depending on
parenthesis so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&amp;lt;*&amp;gt;) a b&lt;/code&gt; used as function is same as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a &amp;lt;*&amp;gt; b&lt;/code&gt; when
used as operator.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-- For Signal
(&amp;lt;*&amp;gt;) :: Signal (a -&amp;gt; b) -&amp;gt; Signal a -&amp;gt; Signal b
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;First argument has type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal (a -&amp;gt; b)&lt;/code&gt;. It represents a function that
is changing with clock signal. For any class that is Applicative we have
available functions&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;liftA  :: Applicative f =&amp;gt; (a -&amp;gt; b) -&amp;gt; f a -&amp;gt; f b
liftA2 :: Applicative f =&amp;gt; (a -&amp;gt; b -&amp;gt; c) -&amp;gt; f a -&amp;gt; f b -&amp;gt; f c
liftA3 :: Applicative f =&amp;gt; (a -&amp;gt; b -&amp;gt; c -&amp;gt; d) -&amp;gt; f a -&amp;gt; f b -&amp;gt; f c -&amp;gt; f d
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Using liftAx with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal&lt;/code&gt; enables converting combinatorial circuits having x
inputs in synchronous circuits. For &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; being &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal&lt;/code&gt; function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;liftA2&lt;/code&gt; becomes&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;liftA2 ::  (a -&amp;gt; b -&amp;gt; c) -&amp;gt; Signal a -&amp;gt; Signal b -&amp;gt; Signal c
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;liftA2 can be implemented as&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;liftA2 f s1 s2 = (pure f) &amp;lt;*&amp;gt; s1 &amp;lt;*&amp;gt; s2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Intermediate types are&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;f                      :: a -&amp;gt; b -&amp;gt; c
pure f                 :: Signal (a -&amp;gt; b -&amp;gt; c)
(pure f) &amp;lt;*&amp;gt; s1        :: Signal (b -&amp;gt; c)
(pure f) &amp;lt;*&amp;gt; s1 &amp;lt;*&amp;gt; s2 :: Signal c
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal (a -&amp;gt; b -&amp;gt; c)&lt;/code&gt; is same as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal (a -&amp;gt; (b -&amp;gt; c))&lt;/code&gt; and this is
why &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(pure f) &amp;lt;*&amp;gt; s1&lt;/code&gt; has type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal (b -&amp;gt; c)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using this pattern liftA5 is expressed&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;liftA5 f s1 s2 s3 s4 s5 = (pure f) &amp;lt;*&amp;gt; s1 &amp;lt;*&amp;gt; s2 &amp;lt;*&amp;gt; s3 &amp;lt;*&amp;gt; s4 &amp;lt;*&amp;gt; s5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Function liftAx transform combinatorial circuit having arbitrary number
of arrows in synchronous circuit. In hardware level such transformation
is trivial and what happens is that just type of function change to make
compiler happy.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec n&lt;/code&gt; is also an Applicative and works similar compared to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal&lt;/code&gt;.
For &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec n&lt;/code&gt; liftA2 when used with Vec has type.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;liftA2 :: (a -&amp;gt; b -&amp;gt; c) -&amp;gt; Vec n a -&amp;gt; Vec n b -&amp;gt; Vec n c
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;liftA2 for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec n&lt;/code&gt; works in way that takes values at same index to
produce value at same index in resulting vector using passed function.
As with Functor there are &lt;a href=&quot;https://hackage.haskell.org/package/base/docs/Control-Applicative.html#t:Applicative&quot;&gt;Applicative functor
laws&lt;/a&gt;
that should hold for every implementation of Applicative.&lt;/p&gt;

&lt;p&gt;Applicative functor instance of Signal and Vec are similar to reason about.
Both works in way, that each value at some place represent value as a result of other values
at that place. For example&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;liftA2 (+) (1:&amp;gt;2:&amp;gt;3:&amp;gt;Nil) (3:&amp;gt;5:&amp;gt;7:&amp;gt;Nil) == (4:&amp;gt;7:&amp;gt;10:&amp;gt;Nil)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;multisignal-as-applicative-functor&quot;&gt;MultiSignal as applicative functor&lt;/h3&gt;

&lt;p&gt;Implementing instance of Applicative functor for MultiSignal enables all
required tools for transforming combinatorial circuits in domain of
MultiSignal. For example liftA2 is defined in terms of Applicative
for MultiSignal as&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;liftA2 :: (a-&amp;gt;b-&amp;gt;c) -&amp;gt; MultiSignal n a -&amp;gt; MultiSignal n b -&amp;gt; MultiSignal n c
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We need to define two functions&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pure :: a -&amp;gt; MultiSignal n a
(&amp;lt;*&amp;gt;) :: MultiSignal n (a -&amp;gt; b) -&amp;gt; MultiSignal n a -&amp;gt; MultiSignal n a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Implementation for Applicative&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;instance KnownNat n =&amp;gt; Applicative (MultiSignal n) where
   pure x = MultiSignal (pure (pure x))
   f &amp;lt;*&amp;gt; s = MultiSignal  (fmap (&amp;lt;*&amp;gt;) fu &amp;lt;*&amp;gt; su) where
      fu = unMultiSignal f
      su = unMultiSignal s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;prependable&quot;&gt;Prependable&lt;/h2&gt;

&lt;p&gt;Prependable is typeclass to generalize over
&lt;a href=&quot;http://hackage.haskell.org/package/clash-prelude-0.10.14/docs/CLaSH-Signal.html#v:register&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;register&lt;/code&gt;&lt;/a&gt;
. We would like to use single function that can be used to define delay
for both Signal and MultiSignal. Prependable works as something that
prepends element to the stream.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Prependable f where
    prepend :: a -&amp;gt; f a -&amp;gt; f a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Defining &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prepend&lt;/code&gt; for Signal is just register.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;instance Prependable Signal where
    prepend = register
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;for MultiSignal: value from register we shift to vector. What pop out at shift
we store in register.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;instance (KnownNat n, n ~ (m+1)) =&amp;gt; Prependable (MultiSignal n) where
    prepend x (MultiSignal s) = 
        MultiSignal $ liftA2 (+&amp;gt;&amp;gt;) (register x (fmap last s)) s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;there are two functions used from CLaSH.Prelude. One is
&lt;a href=&quot;https://hackage.haskell.org/package/clash-prelude/docs/CLaSH-Sized-Vector.html#v:-43--62--62-&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(+&amp;gt;&amp;gt;) :: KnownNat n =&amp;gt; a -&amp;gt; Vec n a -&amp;gt; Vec n a&lt;/code&gt;&lt;/a&gt;
and other&lt;br /&gt;
&lt;a href=&quot;https://hackage.haskell.org/package/clash-prelude/docs/CLaSH-Sized-Vector.html#v:last&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last :: Vec (n + 1) a -&amp;gt; a Source&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What about Prependable laws, you may ask. Instances of Prependable are
like &lt;a href=&quot;https://en.wikibooks.org/wiki/Haskell/Foldable&quot;&gt;Foldable&lt;/a&gt;.
Foldable enables function
&lt;a href=&quot;https://hackage.haskell.org/package/base/docs/Data-Foldable.html#v:toList&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;toList&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;toList (prep a ax) == a : toList ax
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Foldable instance of Signal and MultiSignal hardly has representation in
hardware, because it requires iterating whole stream that is infinite.
For example, calculating sum as single value of infinitely long stream
is not what we can do, but that is what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foldr1 (+)&lt;/code&gt; express. I am still
looking for better way to express Prepenadable and figure out better
name.&lt;/p&gt;

&lt;h2 id=&quot;isomorphic-circuits&quot;&gt;Isomorphic circuits&lt;/h2&gt;

&lt;p&gt;Whole idea is that we express circuits using Functor, Applicative and
Prependable, than synthesize for both Signal and MultiSignal n domain. We
have seen how we can change combinatoric circuits in new domain. In this
new domain (for example Signal or MultiSignal) we can use prepend as a
function that works as register.&lt;/p&gt;

&lt;p&gt;Example of simple circuit is accumulator. This is circuitry that outputs sum
of all previously received inputs.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;         +-------+
         |       |   +------------+
input ---+  ADD  |   |  prepend   |
         |  (+)  +---+ (register) +----+------ output
      +--+       |   |            |    |
      |  |       |   +------------+    |
      |  +-------+                     |
      +--------------------------------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And this is how we define such circuit using using new tools.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;acc input = output where
    output = prepend 0 (liftA2 (+) input output)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we can defer instantiation of such circuitry to the point where we 
define type signature.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;topEntity :: MultiSignal 2 (Signed 6) -&amp;gt;  MultiSignal 2 (Signed 6)
topEntity = acc

                        +----------+
                6 bit   |          |
            ------------+   ADD    |                         
                        | Signed 6 +------------------+             
                   +----+          |                  |     output 
     input         |    |          |                  |   2 * Signed 6         
    2 * Signed 6   |    +----------+                  |    +--------
                   |                                  |    |             
                   +---------------------------+      +----|--------
                        +----------+           |      |    |
                        |          |   +-----+ |      |    |
            ------------+   ADD    |   |     | |      |    |      
                        | Signed 6 +---+ REG +-+-----------+    
                   +----+          |   |6 bit|        |             
                   |    |          |   +-----+        |           
                   |    +----------+                  |
                   |                                  |
                   +----------------------------------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is how digital &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;acc&lt;/code&gt; circuit is instantiated in
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MultiSignal 2 (Signed 6)&lt;/code&gt;. Each wire represent 6 bit bus. At each clock
we process two synchronous values.&lt;/p&gt;

&lt;p&gt;This looks like free optimization technique, all one need is trade space
for speed. Right? Well it is not that easy. As we can see signal needs
in worst case propagate trough both adders in single clock cycle to
reach register REG. This makes maximal operating frequency approx twice
lower compared to circuitry operating in Signal or MultiSignal 1.
Improvement can be probably found by exploiting associative properties
of addition and is something to look into in future.&lt;/p&gt;

&lt;h3 id=&quot;mr-mealy--mr-moore&quot;&gt;mr. Mealy &amp;amp; mr. Moore&lt;/h3&gt;

&lt;p&gt;Example of
&lt;a href=&quot;https://hackage.haskell.org/package/clash-prelude/docs/CLaSH-Prelude-Mealy.html#v:mealy&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mealy :: (s -&amp;gt; i -&amp;gt; (s, o)) -&amp;gt; s -&amp;gt; Signal i -&amp;gt; Signal o&lt;/code&gt;&lt;/a&gt;
as expressed using Prependable.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mealyP f d i = o where
   r = liftA2 f (prepend d s) i
   s = fmap fst r
   o = fmap snd r
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Compiler automatically deduce type as&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mealyP  :: (Prependable f, Applicative f)
        =&amp;gt; (s -&amp;gt; i -&amp;gt; (s, o)) -&amp;gt; s -&amp;gt; f i -&amp;gt; f o
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This tells us that we can use mealyP on any type that is Prependable and
Applicative. Here we use&lt;/p&gt;

&lt;p&gt;We can also in similar terms define
&lt;a href=&quot;https://hackage.haskell.org/package/clash-prelude/docs/CLaSH-Prelude-Moore.html#v:moore&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;moore :: (s -&amp;gt; i -&amp;gt; s) -&amp;gt; (s -&amp;gt; o) -&amp;gt; s -&amp;gt; Signal i -&amp;gt; Signal o&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mooreP :: (Prependable f, Applicative f) 
       =&amp;gt; (s -&amp;gt; i -&amp;gt; s) -&amp;gt; (s -&amp;gt; o) -&amp;gt; s -&amp;gt; f i -&amp;gt; f o
mooreP fs fo s i = fmap fo r where
     r = liftA2 fs (prepend s r) i
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And we can also express fir filter nearly same way as it is on front
page of &lt;a href=&quot;http://www.clash-lang.org/#example&quot;&gt;clash-lang.org&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fir coeffs x = dotp coeffs (windowP x)
  where
    dotp as bs = sum (zipWith (*) as bs)
    windowP  x = iterateI (prepend def) x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Only difference is that we had to define windowP that works over
Prependable instead of Signal. Additionally it requires that MultiSignal
is instance of Num. Further details are available in
&lt;a href=&quot;https://hackage.haskell.org/package/clash-multisignal&quot;&gt;clash-multisignal&lt;/a&gt; library.&lt;/p&gt;

&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Using few simple concepts we separate composition and construction of
circuits. There is
&lt;a href=&quot;https://hackage.haskell.org/package/clash-multisignal&quot;&gt;clash-multisignal&lt;/a&gt;
available and can be used as starting point to play with such
structures.&lt;/p&gt;

</description>
        <pubDate>Fri, 23 Dec 2016 14:37:00 +0100</pubDate>
        <link>http://rahne.si/programming/clash/fpga/2016/12/23/iso-circuits.html</link>
        <guid isPermaLink="true">http://rahne.si/programming/clash/fpga/2016/12/23/iso-circuits.html</guid>
        
        
        <category>programming</category>
        
        <category>clash</category>
        
        <category>fpga</category>
        
      </item>
    
      <item>
        <title>Remote fpga call</title>
        <description>&lt;h1 id=&quot;fpga-core-in-cλash&quot;&gt;Fpga core in CλaSH&lt;/h1&gt;

&lt;p&gt;Lets make matrix multiplication as Fpga core in language
&lt;a href=&quot;http://www.clash-lang.org/&quot;&gt;CλaSH&lt;/a&gt;. Clash is basically
&lt;a href=&quot;https://www.haskell.org/&quot;&gt;Haskell&lt;/a&gt; programming language where
&lt;a href=&quot;https://www.haskell.org/ghc/&quot;&gt;Ghc&lt;/a&gt; compiler is hacked to generate
either verilog, vhdl or system verilog source that can be used by tools
(Xilinx Vivado for example) to synthesize digital circuit. Both clash
and ghc are free and open sourced.&lt;/p&gt;

&lt;p&gt;Multiplication of 2 matrices size 3x3 and using 16 bit arithemtics can
be defined as&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;type M3x3 = Vec 3 (Vec 3  (Signed 16))

matrixMultiply3x3 :: M3x3 -&amp;gt; M3x3 -&amp;gt; M3x3
matrixMultiply3x3 =  fmap ( mulLine (traverse id b)) a
    where 
        mulLine a b = fmap (fold (+) . zipWith (*) b) a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Great. Fpga core! Instead of having instructions run by conventional
processor, we have digital circuit that performs multiplication
instantly. Time required for copletion depends only on timing for signal
propagation trough logic gates.&lt;/p&gt;

&lt;p&gt;To test and use this we need to get data in and out of Fpga. This core
defines 2 * 3 * 3 * 16 = 288 bits input and 144 bits as output.
Wiring all pins directly on output of chip is not an option. Bus
infrastructure to get data in and out of fpga is requred.&lt;/p&gt;

&lt;p&gt;RedPitaya is already using bus. I am using RedPitaya because it is Fpga
board I got. It comes with sources for Fpga. It is running on Xilinx
Zynq Soc that has both Fpga and Arm Cortex A9. Arm is running Debian OS
and has 512 MB of RAM and Fpga has 28k Logic Cells and 18k LUTs approx.&lt;/p&gt;

&lt;h3 id=&quot;lambdaya-bus-libraray&quot;&gt;Lambdaya-bus libraray&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/ra1u/lambdaya-bus&quot;&gt;Lambdaya-bus&lt;/a&gt; library provides
both bus core for fpga and client code for communicating with core from
application.&lt;/p&gt;

&lt;p&gt;On fpga side we need function that takes core as argument and creates
bus. We aim for something with this kind of signature.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;core2bus :: (Signal a -&amp;gt; Signal b) -&amp;gt; Signal BusIn -&amp;gt; Signal BusOut
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That is, we have function that consumes core and provides bus interface.
We are close to this, expect ahem, we have to deal with meta programing.
The way this to work, compiler must know how large type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; are
and how to divide them in 32 bit chunks.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;simpleBus :: Signal BusIn -&amp;gt; Signal BusOut
simpleBus = busBuild $(bTQ matrixMul3x3sig) matrixMul3x3sig
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is how at current state one defines transformation from core to
bus. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$(bTQ matrixMul3x3sig)&lt;/code&gt; is template haskell that works like macro
to help deducing proper types as serialization.&lt;/p&gt;

&lt;p&gt;It is similar from client side.&lt;/p&gt;

&lt;p&gt;From
&lt;a href=&quot;https://github.com/ra1u/lambdaya-bus/blob/master/examples/MatrixMultiply/Client.hs&quot;&gt;example&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;multiply :: (M3x3,M3x3) -&amp;gt; NetworkFpgaSetGet M3x3
multiply = callCore $(bTQ matrixMul3x3sig) 5 0 5 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Magic numbers &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5 0 5 0&lt;/code&gt; are page and starting offset for writing and
reading from bus. There is some kick start tutrial availabe at
&lt;a href=&quot;https://github.com/ra1u/lambdaya-bus&quot;&gt;github&lt;/a&gt; together with presented example.&lt;/p&gt;

&lt;p&gt;One can run this same code also directly from Arm without networking
overhead. Typeclass
&lt;a href=&quot;https://hackage.haskell.org/package/Lambdaya-0.2.0.0.1/docs/System-RedPitaya-Fpga.html#t:FpgaSetGet&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FpgaSetGet&lt;/code&gt;&lt;/a&gt;
in this case writes directly on bus.&lt;/p&gt;

&lt;p&gt;Porting this library on other Fpga requires implementing
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FpgaSetGet&lt;/code&gt; on client side . There are 4 functions this class is
implementing, but only 2 (one set and one get) is required. On Fpga side
 bus with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal BusIn -&amp;gt; Signal BusOut&lt;/code&gt; is needed.&lt;/p&gt;

&lt;h3 id=&quot;fpga-bus&quot;&gt;Fpga bus&lt;/h3&gt;

&lt;p&gt;Verilog implementation is using extra bits as signals that user must
explicitly check to figure out whatever data is available and similar at
reading. When there is read request on bus, user must explicitly set bit
(either low or high depends on documentation) to indicate that data are
written on bus. Haskell enables expressing this in rich type system with
same overhead, more expressiveness and less options to make this wrong.&lt;/p&gt;

&lt;p&gt;To tell whatever data is available use type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Maybe d&lt;/code&gt;. That mean that
value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d&lt;/code&gt; (for example data) can be either valid &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5&lt;/code&gt; and has value
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Just 5&lt;/code&gt; or data is not available and value is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Nothing&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Our simple bus we are implementing in clash is than&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;type BusIn = Maybe (FullAddress,ReadWrite,FullDataIn)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Compared to verilog similar interface is provided, except that
information about data availability is encoded in type. That means that
value can be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Nothing&lt;/code&gt; or for example &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Just (0x40500000,Write,0x10)&lt;/code&gt;.
This make reasoning and checking whenever data is available simpler and
less error prone. We are unable to not check for data availability
because code that does not handle type
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Maybe (FullAddress,ReadWrite,FullDataIn)&lt;/code&gt; will not compile. This
approach eliminates kind of errors when mixing up polarity or forgetting
about this at all.&lt;/p&gt;

&lt;p&gt;It is similar when writing data on bus&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;type BusOut = Maybe FullDataOut
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That is data from core is either valid and is for example &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Just 42&lt;/code&gt; or
is not valid and is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Nothing&lt;/code&gt;. Working with such type express intent 
much more clear.&lt;/p&gt;

&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Main pitch for Clash is that it enables developing more ambitious
projects. Abstractions Haskell enables might seems scary at first, but
one gets feeling about how cores are generated, just like experienced
C++ programmer can get grasp of what kind of machine code he can expect.
Most if not all of Haskell abstractions come for free in clash and
allows describing both intent and circuitry, not just circuitry.&lt;/p&gt;

&lt;p&gt;Since haskell is one of few languages having ability to run effectively
on both fpga and on pc, we can nicely mix fpga and client side, and I
wish that further impovements of lambdaya-bus library will go in further
directions to simplify this approach even more. State of lambdaya-bus at
this stage with version 0.0.0.2 is just proof of concept as something that
can be demonstrated to work.&lt;/p&gt;

</description>
        <pubDate>Sun, 28 Feb 2016 14:37:00 +0100</pubDate>
        <link>http://rahne.si/programming/clash/fpga/2016/02/28/remote-core-call.html</link>
        <guid isPermaLink="true">http://rahne.si/programming/clash/fpga/2016/02/28/remote-core-call.html</guid>
        
        
        <category>programming</category>
        
        <category>clash</category>
        
        <category>fpga</category>
        
      </item>
    
      <item>
        <title>Functional hardware</title>
        <description>&lt;h1 id=&quot;programming-fpga-using-haskell&quot;&gt;Programming FPGA using Haskell&lt;/h1&gt;

&lt;p&gt;Here is very short introduction to functional programming language
&lt;a href=&quot;https://www.haskell.org/&quot;&gt;Haskel&lt;/a&gt; and functional reasoning. For this we
will use &lt;a href=&quot;http://www.clash-lang.org/&quot;&gt;CλaSH&lt;/a&gt; compiler, that is extension
of Haskell language being able to convert large set of Haskell code in
either VHLD, Verilog or SystemVerilog for now. Installation instructions
are available in &lt;a href=&quot;http://hackage.haskell.org/package/clash-prelude/docs/CLaSH-Tutorial.html#g:2&quot;&gt;hackage
doc&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you need just quick peek in examples, there is &lt;a href=&quot;https://github.com/ra1u/clash-tutorial/tree/master/src&quot;&gt;source
code&lt;/a&gt; available
of all examples presented here and is compiled in VHLD, Verilog and
SystemVerilog. Code is automatically generated, but can be used as quick
check of what kind of circuit code describes and what to expect from
CλaSH compiler.&lt;/p&gt;

&lt;p&gt;Premise is that circuits described functional way are considered simpler
to reason about, compose and reuse. We are able to describe topology
trough functional composition on abstract way without sacrificing about
reasoning, how may registries are in use and where logic gates are. This
approach allows describing similar if not same circuits as with Verilog
and VHDL using new and fresh approach allowing composing more ambitious
synchronous digital circuits.&lt;/p&gt;

&lt;p&gt;Descriptions is mathematical in way that is trying to use tools from
mathematical theory about composition. It is common to see relations
described with mathematical laws.&lt;/p&gt;

&lt;h1 id=&quot;function-application&quot;&gt;Function application&lt;/h1&gt;

&lt;p&gt;Before running coding we should explain how calling functions in Haskell works.
In most languages if function takes 2 arguments &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; it is called
like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f(x,y)&lt;/code&gt; where equivalent for this in Haskell is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f\ x\ y&lt;/code&gt; . This
come with advantage because Haskell supports partially applied functions
where only one out of two arguments is applied. Lets say that signature
of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int -&amp;gt; Int -&amp;gt; Bool&lt;/code&gt;. We can interpret this as - it takes two
integers &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; and it returns boolean. When only first argument &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;
is applied it returns function whit hremaining signature. When applying
on function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int -&amp;gt; Int -&amp;gt; Bool&lt;/code&gt; first &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt; remaining is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int -&amp;gt; Bool&lt;/code&gt;.
That is function that takes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt; and it returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bool&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-- function of 2 Int, that telse if 
-- 1st argument is greater then 2nd
greater :: Int -&amp;gt; Int -&amp;gt; Bool 
greater x y = x &amp;gt; y

-- takes y and tels whatever it is greater than 2 
isgt2 :: Int -&amp;gt; Bool 
isgt2 y = greater 2 y
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;or less verbose as partially applied&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;isgt2 :: Int -&amp;gt; Bool
isgt2 = greater 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;One way to think about functions in Haskell is that each function takes
only one argument and it returns one value that can be function.
Signature of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;greater&lt;/code&gt; can be seen as
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;greater :: Int -&amp;gt; ( Int -&amp;gt; Bool )&lt;/code&gt;. So it takes an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt; and it returns
function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int -&amp;gt; Bool&lt;/code&gt;. That is function that takes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt; and returns
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bool&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In Haskell every function returns something. Pure functions, does not
execute any action, but just transform input data and return relevant
result. Pure functions, has important property that they always returns
same output for same input and so they don’t have access to some
external time changing data or internal state. In Haskell there is
strong force to separate effects from pure functions.&lt;/p&gt;

&lt;h1 id=&quot;function-composition&quot;&gt;Function composition&lt;/h1&gt;

&lt;p&gt;Functional programming is about composition. Here it is example of
signatures of 2 functions.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;funcA2B :: a -&amp;gt; b
funcB2C :: b -&amp;gt; c
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;funcA2B&lt;/code&gt; is function that takes type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; and returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt;, one example
is that it takes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt; and returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bool&lt;/code&gt;.&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;funcB2C&lt;/code&gt; takes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; and it
returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c&lt;/code&gt;. For example it takes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bool&lt;/code&gt; and returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bool&lt;/code&gt;. Having this
we are able to create function&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;funcA2C :: a -&amp;gt; c
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;by composing together &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;funcA2B&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;funcB2C&lt;/code&gt; to get &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;funcA2C&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For composing two functions we need function&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;composeFunc :: (b-&amp;gt;c) -&amp;gt; (a-&amp;gt;b) -&amp;gt; a -&amp;gt; c
composeFunc f g x = f (g x)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;First we apply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g&lt;/code&gt; and then we apply result on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt;.&lt;br /&gt;
Moving back to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;composeFunc&lt;/code&gt; it takes 3 arguments. First two are
functions, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; with signature &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b-&amp;gt;c&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g&lt;/code&gt; with signature &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a-&amp;gt;b&lt;/code&gt;, 3rd
param &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; is of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt;. Function returns type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c&lt;/code&gt; and is same type
that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; returns. Looking differently it takes two functions that is
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b-&amp;gt;c&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a-&amp;gt;b&lt;/code&gt; and it returns function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a -&amp;gt; c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;minus1 x = x - 1
largeNum x = x &amp;gt; 3

superLargeNum = composeFunc largeNum minus1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;superLargeNum&lt;/code&gt; is function composed by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;largeNum&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;minus1&lt;/code&gt;.Type of
each function is well defined, but we can either define it manually or
compiler can automatically deduce it.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-- simplified types
minus1 :: a -&amp;gt; a
largeNum :: a -&amp;gt; Bool
superLargeNum :: a -&amp;gt; Bool 

-- actual types
minus1 :: Num a =&amp;gt; a -&amp;gt; a
largeNum :: (Num a, Ord a) =&amp;gt; a -&amp;gt; Bool
superLargeNum :: (Num a, Ord a) =&amp;gt; a -&amp;gt; Bool
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can read this, that function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;superLargeNum&lt;/code&gt; transforms type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; into
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bool&lt;/code&gt;. Type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; has to be defined for class &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Num&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ord&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Num&lt;/code&gt; is
class that has defined function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(-)&lt;/code&gt; as minus operator. (among others)
and class &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ord&lt;/code&gt; has defined &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&amp;gt;)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;composeFunc&lt;/code&gt; is commonly used and in Haskell it is defined as operator
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(.)&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(.) = composeFunc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Using this, one can alternatively define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;superLargeNum&lt;/code&gt; as&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;superLargeNum = largeNum . minus1
-- or 
superLargeNum = (&amp;gt; 3) . (- 1) 
-- or
superLargeNum x = (x - 1) &amp;gt; 3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Operator &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt; comes from function composition as defined in mathematics f
∘ g . Here we have showed implemention of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt; as in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;composeFunc&lt;/code&gt;, but
sometimes instead of implementation we get only laws. For function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(.)&lt;/code&gt;
holds.&lt;/p&gt;

&lt;p&gt;If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt; is identity function that returns argument&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;id :: a -&amp;gt; a
id x = x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;then function composition laws for ∘ are&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;id . f == f . id == f
f . g . h == (f . g) . h == f . (g . h)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Instead of having function implementation available we sometimes have
available laws that function obeys. It helps reasoning about how
function behaves without knowing what it does. Laws also goes hand to
hand with implementation.&lt;/p&gt;

&lt;h1 id=&quot;functor&quot;&gt;Functor&lt;/h1&gt;

&lt;p&gt;Lets take for example type in CλaSH called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal a&lt;/code&gt;. This represent
type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; changing with clock. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal&lt;/code&gt; is concrete example of functor
and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; is type embedded in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Other than function composition of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal a&lt;/code&gt; there is different
composition available for functor.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;funcA2B :: a -&amp;gt; b
signalA :: Signal a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If we have function of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a -&amp;gt; b&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal a&lt;/code&gt;, than functor
enables us to get &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal b&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fmap :: (a -&amp;gt; b) -&amp;gt; Signal a -&amp;gt; Signal b
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Functor obeys two laws&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fmap id == id
fmap (p . q) == (fmap p) . (fmap q)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Or equivalently when applying signal.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fmap id signal == signal
fmap (p . q) signal == fmap p (fmap q signal)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Knowing this we can aswer next question. Does &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap&lt;/code&gt; over &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal&lt;/code&gt; uses
any registries as for example D flip-flop?&lt;/p&gt;

&lt;p&gt;Answer is, if that would be case, neither of laws would hold. Form first
law it would imply that on left we have signal delayed by 1 clock, and
on right non delayed signal and they are not same. Second law would
imply delay by 1 clock on left where right side made 2 delays, each by
one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap&lt;/code&gt; calls.&lt;/p&gt;

&lt;p&gt;To generate core we define function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;topEntity&lt;/code&gt;. Generic type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; should
be concrete and in this case we will use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signed 7&lt;/code&gt; what is 7 bit signed
integer.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;topEntity :: Signal (Signed 7) -&amp;gt; Signal Bool
topEntity signal = fmap superLargeNum signal 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Executing command fmap can be used as operator. That is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;$&amp;gt;&lt;/code&gt;.
Alternatively we can implement topEntity also as&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;topEntity signal = superLargeNum &amp;lt;$&amp;gt; signal
-- or point free version
topEntity = fmap superLargeNum
-- or using fmap as operator
topEntity = superLargeNum `fmap` signal
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It is hard to tell witch version is most common among haskellers, but we
can be sure, that due to laws all of them behaves same.&lt;/p&gt;

&lt;p&gt;Compiling this exact into Verilog generates code as expected.&lt;/p&gt;

&lt;h1 id=&quot;mealy-machinery&quot;&gt;Mealy machinery&lt;/h1&gt;

&lt;p&gt;For generating output based on previous stored value we can use
&lt;a href=&quot;http://hackage.haskell.org/package/clash-prelude/docs/CLaSH-Prelude-Mealy.html#v:mealy&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mealy&lt;/code&gt;&lt;/a&gt;
function. Lets implement integrator using mealy machine. With integrator
we have in mind function that takes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal a&lt;/code&gt; and produces &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal a&lt;/code&gt;
and is representing sum of all previous values including current.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;integrator signal = mealy mf 0 signal
    where 
        mf state input = (nextstate,output)
            where
                nextstate = state + input
                output = nextstate
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href=&quot;http://hackage.haskell.org/package/clash-prelude/docs/CLaSH-Prelude-Mealy.html#v:mealy&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mealy&lt;/code&gt;&lt;/a&gt;
is function that takes 3 arguments. First parameter is a function on how
to generate next state from state and signal value. Signature of this
function is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s -&amp;gt; i -&amp;gt; (s,o)&lt;/code&gt;. In our code that is function called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mf&lt;/code&gt;.
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mf&lt;/code&gt; is defined in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;where&lt;/code&gt; block for local visibility so can not be used
out of&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integrator&lt;/code&gt; . What this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mf&lt;/code&gt; function does it takes 2 arguments &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt;
and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;input&lt;/code&gt; and it returns tuple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(nextstate,output)&lt;/code&gt;. Next clock cycle
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nextstate&lt;/code&gt; will be passed again in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mf&lt;/code&gt; as argument &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt;. 2nd
argument of integrator &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; is initial state value. Generated core will
provide reset signal that sets state to initial value.&lt;/p&gt;

&lt;p&gt;If we now need to create FPGA core, we need to provide &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;topEntity&lt;/code&gt;. Here
it is full source instantiated with Fixed point arithmetics. We
arbitrarily choose &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SFixed 2 8&lt;/code&gt;, This is 10 bit signed fixed point
number. It is representing rational numbers from -2 &amp;lt;= x &amp;lt; 2.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;module Integrator where
import CLaSH.Prelude

integrator signal = mealy mf 0 signal
    where 
        mf state input = (nextstate,output)
            where
                nextstate = state + input
                output = nextstate

topEntity :: Signal (SFixed 2 8) -&amp;gt; Signal (SFixed 2 8)
topEntity = integrator
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integrator&lt;/code&gt; can be used in other modules, to build larger and more
ambitious circuits. For example to make integrator of integrated signal
so that output of first integrator is feed in second one.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;intInt = integrator . integrator
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;vector&quot;&gt;Vector&lt;/h1&gt;

&lt;p&gt;&lt;a href=&quot;https://hackage.haskell.org/package/clash-prelude/docs/CLaSH-Sized-Vector.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec&lt;/code&gt;&lt;/a&gt;
is sized vector with compile type defined size holding elements of same
type. It is similar to tuple, except that all elements are of same type.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec 5 (Signed 7)&lt;/code&gt; is type that holds 5 elements of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signed 7&lt;/code&gt; what
is 7 bit signed number. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec&lt;/code&gt; is also an functor.&lt;/p&gt;

&lt;p&gt;For example&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vs :: Vec 6 (Signed 9)
vs = 1 :&amp;gt; 2 :&amp;gt; 3 :&amp;gt; 4 :&amp;gt; 5 :&amp;gt; 6 :&amp;gt; Nil

vb :: Vec 6 Bool
vb = fmap (&amp;gt; 3) vs  -- that is &amp;lt;False,False,False,True,True,True&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Operator &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&amp;gt;&lt;/code&gt; is right-associativity operator that prepends vector on
right with value on left. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Nil&lt;/code&gt; is empty vector and is used, because
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&amp;gt;&lt;/code&gt; expects vector on right side.&lt;/p&gt;

&lt;p&gt;Size of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec&lt;/code&gt; is defined at compile time and can be generic. Lets take
look on one of many functions from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec&lt;/code&gt; libraray that is
&lt;a href=&quot;https://hackage.haskell.org/package/clash-prelude/docs/CLaSH-Sized-Vector.html#v:replicate&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;replicate&lt;/code&gt;&lt;/a&gt;
and has prototype&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;replicate :: SNat n -&amp;gt; a -&amp;gt; Vec n a
-- &quot;replicate n a&quot; returns a vector that has `n` copies of `a`.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec 5 a&lt;/code&gt; is not same as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec 6 a&lt;/code&gt; and it means that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;replicate&lt;/code&gt; returns
different type based on argument &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;. For this to work, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; is of type
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SNat x&lt;/code&gt; and is different type for each x. Value that has type
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signed 5&lt;/code&gt; as 5 bit signed value can be -2 or 7, where value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d3&lt;/code&gt; that
has type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SNat 3&lt;/code&gt; represents number 3 as unique type.&lt;/p&gt;

&lt;h1 id=&quot;moving-average&quot;&gt;Moving average&lt;/h1&gt;

&lt;p&gt;Moving average is simple low pass FIR filter expressed as sum of last
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; values. We will deliberately skip division in our example, so we
will in reality talk about moving sum.&lt;/p&gt;

&lt;p&gt;Here is straight forward and naive option to describe moving average
using
&lt;a href=&quot;http://hackage.haskell.org/package/clash-prelude/docs/CLaSH-Prelude-Mealy.html#v:mealy&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mealy&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;movingAvarage n signal = mealy mf (replicate n 0) signal
    where
        mf state input = (nexts,output)
            where 
                nexts = input :&amp;gt; init state
                output = fold (+) nexts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;First argument &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; is number of accumulated value is also size of Vec
used as internal state storage. Type of internal state is deduced as
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec n a&lt;/code&gt; and is deduced from type of initial state value
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;replicate n 0&lt;/code&gt; that returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec&lt;/code&gt; that returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; copies of 0.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://hackage.haskell.org/package/clash-prelude/docs/CLaSH-Sized-Vector.html#v:init&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init :: Vec (n + 1) a -&amp;gt; Vec n a&lt;/code&gt;&lt;/a&gt;
is function that takes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec&lt;/code&gt; with size &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n+1&lt;/code&gt; and returns all except last
element that is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec&lt;/code&gt; of size &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nexts&lt;/code&gt; expressed as next state is vector right shifted by previous
value and perpended &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&amp;gt;&lt;/code&gt; with current clock &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;input&lt;/code&gt; value. Operation
works as fifo shift register.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fold&lt;/code&gt; is function best expressed with same picture as in
&lt;a href=&quot;https://hackage.haskell.org/package/clash-prelude/docs/CLaSH-Sized-Vector.html#v:fold&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;manuals&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From signature we can conclude that
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fold :: (a -&amp;gt; a -&amp;gt; a) -&amp;gt; Vec (n + 1) a -&amp;gt; a&lt;/code&gt; takes 2 arguments. First
is function of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(a -&amp;gt; a -&amp;gt; a)&lt;/code&gt; an second argument is of type
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vec (n + 1) a&lt;/code&gt;. Function is than applied over vector in tree like
structure, enabling minimum latency.
&lt;img src=&quot;/data/fold.svg&quot; alt=&quot;fold over vector&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Function f from out case is function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(+)&lt;/code&gt; (in haskell operator in
brakets is used as function). and has prototype &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a -&amp;gt; a -&amp;gt; a&lt;/code&gt; and is
applied over &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt;.&lt;/p&gt;

&lt;h1 id=&quot;there-is-better&quot;&gt;There is better&lt;/h1&gt;

&lt;p&gt;Lets try some optimization of moving average from previous section.
Observing totals we can conclude we don’t need that many adders. Total
output is changed at each clock only by value that enters fifo from one
side and value that pops out of fifo on other. At each clock we need to
subtract value that pops out and add value that enters from total. This
comes at expanse of 2 extra sized registers (previous solution can be
reduced by 1 sized register) but requires only 2 sized adders.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;movingAvarage2 n signal = mealy mf ((replicate n 0),0) signal
    where
        mf (vec,total) input = ((nextVec,nextTot),output)
            where 
                output = total + input - (last vec)
                nextVec = input :&amp;gt; init vec
                nextTot = output
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mf&lt;/code&gt; has at this point signature &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(Vec n a,a) -&amp;gt; a -&amp;gt; ((Vec n a,a),a)&lt;/code&gt;.
Internal state type is tuple where first value is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vec&lt;/code&gt; used as fifo and
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;total accumulated in fifp.&lt;/code&gt;vec&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;and&lt;/code&gt;total&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;are unpacked from tuple in input argument,  so we are able to access this value, without unpacking them in body. Equivalent, but more verbose&lt;/code&gt;mf`
implemetaion is sketched as&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mf state input = ...
    where 
        vec = fst state -- first element of tuple
        total = snd state -- second element of tuple
        output = ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;circuit-composition&quot;&gt;Circuit composition&lt;/h1&gt;

&lt;p&gt;Great power of composition comes from separating logic into reusable
parts and then composing them together in larger blocks. Function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(.)&lt;/code&gt;
and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fold&lt;/code&gt; are good examples we have used so far. Now lets try something
similar, by designing feedback loop.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;          +---+    +---+
input --&amp;gt;-| f |--&amp;gt;-| g |--&amp;gt;-+--&amp;gt;-- output 
          +---+    +---+    |
            |               ∨
            ∧      +---+    |
            +----&amp;lt;-| h |-&amp;lt;--+
                   +---+     
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;First lets figure out prototypes of each function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;f :: Signal a -&amp;gt; Signal b -&amp;gt; Signal c
g :: Signal c -&amp;gt; Signal d
h :: Signal d -&amp;gt; Signal b
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and feedback loop can be written directly from circuit diagram as&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;feedbackLoop :: (Signal a -&amp;gt; Signal b -&amp;gt; Signal c) 
                -&amp;gt; (Signal c -&amp;gt; Signal d)
                -&amp;gt; (Signal d -&amp;gt; Signal b)
                -&amp;gt; Signal a 
                -&amp;gt; Signal d
feedbackLoop f g h input = out where
    out = g fout
    fout = f input (h out)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If user has available functions that works over primitive values like
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signed&lt;/code&gt; instead &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal (Signed 16)&lt;/code&gt; for example, he can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fold&lt;/code&gt;.
For example if&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;g :: Signed 16 -&amp;gt; Signed 16
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gs&lt;/code&gt; working over signal would be constructed from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gs :: Signal (Signed 16) -&amp;gt; Signal (Signed 16) 
gs = fmap g
-- or more verbose
gs sig = fmap g sig
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For function over 2 input arguments we can use library function liftA2&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;liftA2 :: (a -&amp;gt; b -&amp;gt; c) -&amp;gt; functor a -&amp;gt; functor b -&amp;gt; functor c
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;functor&lt;/code&gt; is in our example &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal&lt;/code&gt; For example to get function
minus &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(-)&lt;/code&gt; to operate over &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Signal&lt;/code&gt; we need to apply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;liftA2&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;signalMinus :: Signal a -&amp;gt; Signal a -&amp;gt; Signal a
signalMinus = liftA2 (-)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To construct negative feedback looking like&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;                   +---+
input --&amp;gt;-( - )--&amp;gt;-| g |--&amp;gt;-+--&amp;gt;-- output 
            |      +---+    |
            ∧               ∨ 
            +--------&amp;lt;------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;feedbackLoop&lt;/code&gt;,&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;negativeFeedback :: Num a =&amp;gt; 
                    (Signal a -&amp;gt; Signal a) -&amp;gt; 
                    Signal a -&amp;gt;
                    Signal a
negativeFeedback g = feedbackLoop (liftA2 (-)) g id
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We have replaced &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h&lt;/code&gt; by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; argument is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;liftA2 (-)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There is subtle issue with this and in some cases this circuit can not
be synthesized. That is because we have feedback loop, so we have made
sure that there is delay in g function. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g&lt;/code&gt; function is for example
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap id&lt;/code&gt; so it works like wire this circuit is not stable any
more. To overcome this, we have to introduce delay in feedback. Function
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h&lt;/code&gt; from previous diagram, becomes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;register 0&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt;.
function&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;negativeFeedback :: Num a =&amp;gt; (Signal a -&amp;gt; Signal a) -&amp;gt; Signal a -&amp;gt; Signal a
negativeFeedback g = feedbackLoop (liftA2 (-)) g (register 0)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href=&quot;http://hackage.haskell.org/package/clash-prelude/docs/CLaSH-Signal.html#v:register&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;register&lt;/code&gt; :: a -&amp;gt; Signal a -&amp;gt; Signal
a&lt;/a&gt;
is a function, that takes inital value, that outputs this value before
remaining of stream. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;register&lt;/code&gt; is available in CλaSH libraray, but can
be implemented using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mealy&lt;/code&gt; as&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;register initialValue signal = mealy mf initialValue 
    where 
        mf prevS input = (input,prevS)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;What is even more surprising is that it can be done even vice versa.
That is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mealey&lt;/code&gt; can be expressed in terms of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;register&lt;/code&gt;. For details
check examples on
&lt;a href=&quot;https://github.com/ra1u/clash-tutorial/tree/master/src&quot;&gt;github&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;There are lot of features that were no covered. But are invaluable
during development&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;repl, (read eval print loop) that allows interactive development,
type deduction&lt;/li&gt;
  &lt;li&gt;ability to simulate in repl or compile in CPU native executable&lt;/li&gt;
  &lt;li&gt;testebenches that generates VHDL / Verilog / Systemverilog and can
be used directly in IDE&lt;/li&gt;
  &lt;li&gt;full featured library, (ram and rom access and initalisation,
multiple clock domains, type safe delays … )&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From my and experience of many it takes lot of time and practice to
grasp new programming language, what Haskell for those not familiar with
functional programming certainly is. Reader with interest will be able
to find lot of free resources available online for all levels of
experience with open and active community.&lt;/p&gt;
</description>
        <pubDate>Sun, 28 Feb 2016 14:37:00 +0100</pubDate>
        <link>http://rahne.si/programming/2016/02/28/funcional-hadware.html</link>
        <guid isPermaLink="true">http://rahne.si/programming/2016/02/28/funcional-hadware.html</guid>
        
        
        <category>programming</category>
        
      </item>
    
      <item>
        <title>Generic C</title>
        <description>&lt;p&gt;Common way to write &lt;a href=&quot;https://en.wikipedia.org/wiki/Generic_programming&quot;&gt;generic&lt;/a&gt; 
functions in C is to use macros. Problem with macros is that that they
are hard to read, and hard to support.
 Just take &lt;a href=&quot;http://lxr.free-electrons.com/source/include/linux/kernel.h?v=3.16#L716&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max&lt;/code&gt;&lt;/a&gt;
for example and it gets worse in this terms when complex functions are required.
&lt;a href=&quot;https://github.com/stefanct/sglib/blob/master/sglib.h&quot;&gt;sglib&lt;/a&gt; is another example.
This code just does not feel C any more.&lt;/p&gt;

&lt;p&gt;Approach in generic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max&lt;/code&gt; without macros is:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;//use max with integers
typedef int generic_max_t;

//this inserts static max() 
//that accepts generic_max_t as type
#include &quot;max_generic.h&quot;  

main(){
  int a=1;
  int b=2;
  int m = max(a,b);
  printf(&quot;%i&quot;,m);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and this is implementation of “max_generic.h”&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;static generic_max_t max(generic_max_t a, generic_max_t b){
  if(a&amp;gt;=b)
    return a;
  else
    return b;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Generic functions are implemeted with generic type. In this
example &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;generic_max_t&lt;/code&gt; was used. Type is first 
instatiated with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;typedef int generic_max_t;&lt;/code&gt; and after that generic code in injected using
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#include&lt;/code&gt; directive that works like copy-paste. For this to work, all generic 
functions should be declared as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;static&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Advantage of such code is that is pure C without funny macros.
Code is reusable with different types, but unfortunatly only once
in each translation unit.&lt;/p&gt;

&lt;p&gt;Here is another example of generic ring buffer with non trivial type to store.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;//usage example
#include &amp;lt;stdio.h&amp;gt;


typedef struct {
    double x;
    double y;
    double z;
}point_t;

//we want point_t to be element of our circular buffer
typedef point_t circular_element_t;

//at this point we inject static code that accepts circular_element_t as type
#include &quot;circular.generic.c&quot;  

circular_element_t buffer_space[20];

circular_buffer_t circular_buffer = {
  .buffer = buffer_space,
  .length = sizeof(buffer_space)/sizeof(buffer_space[0]),
};

int main(){
  generic_curcular_buffer_init(&amp;amp;circular_buffer);
  point_t p = {1,2,3};
  generic_curcular_buffer_push(&amp;amp;circular_buffer,&amp;amp;p);
  p.x=p.y=p.z=0;
  generic_curcular_buffer_pop(&amp;amp;circular_buffer,&amp;amp;p);  
  printf(&quot;%f %f %f&quot;,p.x,p.y,p.z);
  return 0;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and implementation&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// file: circular.generic.c
// generic ring buffer
//
// extrnaly defined type is 
// circular_element_t;

typedef struct{
  circular_element_t * buffer;
  int length;  
  int push;
  int pop;  
} circular_buffer_t;
static void generic_curcular_buffer_init(circular_buffer_t * cb){
  cb-&amp;gt;push = 0;
  cb-&amp;gt;pop = 0;
}
//return 0 or sucess or -1 on error
static 
int generic_curcular_buffer_push(circular_buffer_t * cb, 
                                 const circular_element_t* element)
{
  int push = cb-&amp;gt;push+1;
  if(push == cb-&amp;gt;length) {
    push  = 0;
  }
  if(push == cb-&amp;gt;pop){
    return -1; //full
  }
  cb-&amp;gt;buffer[cb-&amp;gt;push] = *element;
  cb-&amp;gt;push = push;
  return 0;
}
//return 0 or sucess or -1 if empty
static
int generic_curcular_buffer_pop(circular_buffer_t * cb, 
                                circular_element_t* element)
{
  if( cb-&amp;gt;pop ==  cb-&amp;gt;push) {
    return -1; //empty
  }
  int pop = cb-&amp;gt;pop+1;
  if(pop == cb-&amp;gt;length){
    pop=0;
  }
  *element = cb-&amp;gt;buffer[cb-&amp;gt;pop];
  cb-&amp;gt;pop = pop;
  return 0;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Another example whith reuse of generic code, is availabe
on &lt;a href=&quot;https://github.com/ra1u/generic_c&quot;&gt;github&lt;/a&gt;&lt;/p&gt;

</description>
        <pubDate>Thu, 08 Jan 2015 14:37:00 +0100</pubDate>
        <link>http://rahne.si/programming/2015/01/08/generic-c.html</link>
        <guid isPermaLink="true">http://rahne.si/programming/2015/01/08/generic-c.html</guid>
        
        
        <category>programming</category>
        
      </item>
    
      <item>
        <title>Memoization in C++</title>
        <description>&lt;p&gt;Memoization is very useful technique for developing algorithms. Python3 has native support for &lt;a href=&quot;https://docs.python.org/3/library/functools.html#functools.lru_cache&quot;&gt;memoization&lt;/a&gt; and here is how it is used.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;functools&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lru_cache&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@lru_cache&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#memoization
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Similar feature can be implemeted in c++ . Here is an example on how to use.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c--&quot; data-lang=&quot;c++&quot;&gt;&lt;span class=&quot;n&quot;&gt;MEMOIZATION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint64_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;originaly defined &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uint64_t fib(int n)&lt;/code&gt; has to be wrapped in macro that takes 3 parameters. &lt;em&gt;return_type, func_name&lt;/em&gt; and &lt;em&gt;arguments&lt;/em&gt;.
so it becomes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MEMOIZATION(uint64_t, fib,(int n))&lt;/code&gt;  From user’s perspective it looks similar to python’s version.&lt;/p&gt;

&lt;p&gt;Here is full implementation of macro MEMOIZATION, Memo class and helper function. This implementation is not same as lru_cache
where old items in cache are discarded once limit is reached.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c--&quot; data-lang=&quot;c++&quot;&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt;  &lt;span class=&quot;cpf&quot;&gt;&amp;lt;map&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt;  &lt;span class=&quot;cpf&quot;&gt;&amp;lt;tuple&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#define MEMOIZATION(ret,name,params) \
  ret name##nonmemo params; \
  auto name = memo_wrapper(name##nonmemo);\
  ret name##nonmemo params
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Memo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Memo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;    
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;funcpoint_t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tuple_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;map_t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tuple_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;Memo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;funcpoint_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mFp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){}&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
       &lt;span class=&quot;n&quot;&gt;tuple_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...);&lt;/span&gt;
       &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;it&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
       &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;it&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
       &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mFp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...))).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;funcpoint_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mFp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;map_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Memo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 
&lt;span class=&quot;n&quot;&gt;memo_wrapper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...)&lt;/span&gt;  &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Memo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

</description>
        <pubDate>Sun, 21 Sep 2014 22:49:53 +0200</pubDate>
        <link>http://rahne.si/programming/2014/09/21/memoization.html</link>
        <guid isPermaLink="true">http://rahne.si/programming/2014/09/21/memoization.html</guid>
        
        
        <category>programming</category>
        
      </item>
    
  </channel>
</rss>
