<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Passing Curiosity: Posts tagged typing</title>
    <link href="https://passingcuriosity.com/tags/typing/typing.xml" rel="self" />
    <link href="https://passingcuriosity.com" />
    <id>https://passingcuriosity.com/tags/typing/typing.xml</id>
    <author>
        <name>Thomas Sutton</name>
        
        <email>me@thomas-sutton.id.au</email>
        
    </author>
    <updated>2013-07-24T00:00:00Z</updated>
    <entry>
    <title>FP-Syd, July 2013</title>
    <link href="https://passingcuriosity.com/2013/fpsyd-july/" />
    <id>https://passingcuriosity.com/2013/fpsyd-july/</id>
    <published>2013-07-24T00:00:00Z</published>
    <updated>2013-07-24T00:00:00Z</updated>
    <summary type="html"><![CDATA[<h2 id="accelerate-with-foreign-functions">Accelerate with foreign functions</h2>
<p>More on accelerate. Looking at using it with other GPGPU frameworks. Two
distinct problems:</p>
<ol type="1">
<li><p>Calling CUDA C programs from Accelerate.</p></li>
<li><p>Calling Accelerate from CUDA C programs.</p></li>
</ol>
<h3 id="calling-cuda-c-from-accelerate">Calling CUDA C from Accelerate</h3>
<blockquote>
<p>Smooth Life is Conway’s Game of Life generalised to confinuous domains.</p>
<p>Lots of variants (magic numbers). This was smooth life “L”.</p>
<p>Gliders can move in any direction.</p>
</blockquote>
<p>Relies on FFT as part of in implementation. Write own FFT in Accelerate <em>or</em>
use cuFFT library.</p>
<p>The cuFFT library wants pointers to GPU memory as parameters to its function,
but Accelerate is high-level (no pointers into GPU memory). So added a new
operation:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ot">foreignAcc ::</span> (<span class="dt">Arrays</span> arr, <span class="dt">Arrays</span> res, <span class="dt">Foreign</span> ff)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>           <span class="ot">=&gt;</span> ff arr res <span class="co">-- ^ The foriegn code</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>           <span class="ot">-&gt;</span> (<span class="dt">Acc</span> arr <span class="ot">-&gt;</span> <span class="dt">Acc</span> res) <span class="co">-- ^ The pure equivalent</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>           <span class="ot">-&gt;</span> <span class="dt">Acc</span> res</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>           <span class="ot">-&gt;</span> <span class="dt">Acc</span> res</span></code></pre></div>
<p>Each backend needs to provide its own instance of Foreign; subclass of Typable2
to avoid the expression problem (so that backends can use their own types for
the foreign stuff).</p>
<p>Use an “abstract” monad CIO, which is like IO but has a few new operations:
<code>allocateArray</code>, <code>devicePtrsOfArray</code>, <code>peekArray</code>, <code>pokeArray</code>. Here “abstract”
is just “private parts are private”.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ot">doFFT ::</span> <span class="dt">Acc</span> (<span class="dt">Array</span> <span class="dt">DIM2</span> <span class="dt">Complex</span>)</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>      <span class="ot">-&gt;</span> <span class="dt">Acc</span> (<span class="dt">Array</span> <span class="dt">DIM2</span> <span class="dt">Complex</span>)</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>doFFT arr <span class="ot">=</span> foreignAcc (<span class="dt">CuForeign</span> foreignFFT)</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>                       pureFFT</span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>                       arr</span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>    <span class="kw">where</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>      pureFFT arr <span class="ot">=</span> <span class="op">...</span> a slow, <span class="fu">pure</span> <span class="dt">Accelerate</span> <span class="dt">FFT</span> <span class="op">...</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a>      foriegnFFT arr <span class="ot">=</span> <span class="op">...</span></span></code></pre></div>
<p>You can nest calls to <code>foreignAcc</code> in the pure branch to offer implementations
for multiple backends.</p>
<h3 id="calling-accelerate-programs-from-c">Calling Accelerate programs from C</h3>
<p><code>foreignAccModule</code> is a piece of Template Haskell magic which, when a module is
compiled, generates a C header file for the module.</p>
<p>Additional Template Haskell functions <code>exportAfun1</code>, <code>exportAfun2</code>, etc. are
used to export specific functions (the <code>dotp</code> function in this example).</p>
<p>Generates two C functions from <code>dotp</code>:</p>
<ul>
<li><p><code>dotp_compile(...)</code> compiles an Accelerate program (the <code>dotp</code> program).</p></li>
<li><p><code>dotp_run(...)</code> executes a compiled Accelerate program (the <code>dotp</code> program).</p></li>
</ul>
<h2 id="how-mark-writes-haskell">How Mark writes Haskell</h2>
<p>Demo of how “I” write Haskell.</p>
<blockquote>
<p>If you use vim you are bad.</p>
</blockquote>
<p>Programming has two cultures:</p>
<ol type="1">
<li><p>Tools-oriented cultures like Java, with lots of IDEs, etc.</p></li>
<li><p>Language-oriented cultures like Haskell.</p></li>
</ol>
<p>ghcmod? is a tool for Haskell; supports emacs <em>and</em> vim. Does recompilation and
test on save, integrates with Hoogle (insert module import statements).</p>
<p>Ruby tool called guard. DSL to watch file system changes and do various things
pass code through compiler for fast feedback about compiler errors. Run it in a
window beside emacs and see things happen as you save!</p>
<p>The doctest library allows you to write quickcheck properties in haddock
comments.</p>
<p>Similar: hdevtools for vim. Has a persistent server.</p>
<h2 id="ben-talking-about-ddc">Ben talking about DDC</h2>
<blockquote>
<p>Note to self: the way you’ve captured the typing rules in this section is
truly horrible. Please remember LaTeX and figure out how you want to render
it with Pandoc &amp; Hakyll.</p>
</blockquote>
<p>Pushing to make DDC be something like LLVM for functional programming
languages; a generally applicable core language.</p>
<h3 id="typing-application">Typing application</h3>
<p><span class="math display">\[\large\frac{
	\Gamma \vdash M :: t_{1} \rightarrow t_{2}
	\qquad
	\Gamma \vdash N :: t_{1}
}{
	\Gamma \vdash M N :: t_{2}
}\]</span></p>
<p>Evaluation</p>
<ol type="1">
<li><p>M reduces to a value (an abstraction).</p></li>
<li><p>N reduces to a value</p></li>
<li><p>Substitution N into M.</p></li>
<li><p>M[N/x] is new value to reduce.</p></li>
</ol>
<p>Is there something in the typing rule which represents all four of these
stages? Not really. (From me: should it be linear logic)</p>
<p>Perhaps adding effects to types.</p>
<p><span class="math display">\[\large\frac{
  \Gamma \vdash M :: t_{1} \rightarrow t_{2} ; e1
  \qquad
  \Gamma \vdash N :: t_{1} ; e2
}{
  \Gamma \vdash M N :: t_{3} ; e_{1} \vee e_{2} \vee e_{3}
}\]</span></p>
<blockquote>
<p>Where <span class="math inline">\(\vee\)</span> is a lattice join:</p>
<p><span class="math inline">\(e_{1} \vee e_{2} = e_{2} \vee e_{1}\)</span></p>
<p><span class="math inline">\(e_{1} \vee e_{1} = e_{1}\)</span></p>
</blockquote>
<p>Now there’s something in the typing rule which represents each phase:</p>
<ol type="1">
<li>Is e1</li>
<li>Is e2</li>
<li>Is e3?</li>
<li>Is the join of them?</li>
</ol>
<h3 id="monads">Monads</h3>
<blockquote>
<p>People who think Haskell is cool might have heard of this thing called a
monad.</p>
</blockquote>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">return</span><span class="ot"> ::</span> a <span class="ot">-&gt;</span> m a</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="ot">bind ::</span> m a <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> m b) <span class="ot">-&gt;</span> m b</span></code></pre></div>
<p>There’s no good way to abstract over monads: each <code>m</code> must be a single monad.
You can use monad transformers, but they suck.</p>
<p>An effect system allows you to write something like</p>
<p><span class="math display">\[\large foo :: Int \xrightarrow{State \vee IO} Int\]</span></p>
<p>instead of choosing between options like:</p>
<p><span class="math display">\[\large foo :: Int \rightarrow \text{State Int}\]</span></p>
<p><span class="math display">\[\large foo :: Int \rightarrow \text{IO Int}\]</span></p>
<p><span class="math display">\[\large foo :: Int \rightarrow \text{StateT s IO Int}\]</span></p>
<h3 id="impact-on-kinds">Impact on kinds</h3>
<p>Adding effects forces us to change the kind of the <code>-&gt;</code>. In Haskell:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="ot">(-&gt;) ::</span> <span class="op">*</span> <span class="ot">-&gt;</span> <span class="op">*</span> <span class="ot">-&gt;</span> <span class="op">*</span></span></code></pre></div>
<p>In an effectful language, every arrow has an effect component:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="ot">(-&gt;) ::</span> <span class="op">*</span> <span class="ot">-&gt;</span> <span class="op">*</span> <span class="ot">-&gt;</span> <span class="dt">Effect</span> <span class="ot">-&gt;</span> <span class="op">*</span></span></code></pre></div>
<p>Could maybe (and Ben’s PhD thesis does) add a <code>Pure</code> effect to non-effectful
arrows.</p>
<p>It also changes the nature of application (just substitution) and mixing the
“other stuff” in.</p>
<h3 id="value-only-languages">Value-only languages</h3>
<p>What if we remove substitution (can only apply values to values):</p>
<p><span class="math display">\[\large\frac{
  \Gamma \vdash v_{1} :: t_{1} \xrightarrow{e_{1}} t_{2} ; \bot
  \qquad
  \Gamma \vdash v_{2} :: t_{1} ; \bot
}{
  \Gamma \vdash v_{1} v_{2} :: t_{2} ; e_{2}
}\]</span></p>
<p>(Note, if you haven’t already, that an effect of <span class="math inline">\(\bot\)</span> is pure.)</p>
<p>So:</p>
<p><span class="math display">\[\large foo :: Int \rightarrow \text{S (State}\vee\text{IO) Int}\]</span></p>
<p>Here <code>S</code> is a suspended computation which, when invoked, will perform some
actions and return an integer.</p>
<p>Introducing a suspended computation:</p>
<p><span class="math display">\[\large\frac{
  \Gamma \vdash M :: t_{1} ; e_{1}
}{
  \Gamma \vdash suspend M :: S e_{1} t_{1} ; \bot
}\]</span></p>
<p>Running a suspended computation:</p>
<p><span class="math display">\[\large\frac{
\Gamma \vdash M :: S e_{1} t; e_{2}
}{
\Gamma \vdash run M :: t ; e_{2} \vee e_{1}
}\]</span></p>
<p>Also need:</p>
<p><span class="math display">\[\large\frac{
\Gamma, x : t_1 \vdash M :: t_2 ; \bot
}{
\Gamma \vdash (\lambda (x:t_1) . M) :: t_1 \rightarrow t_2 ; \bot
}\]</span></p>
<p><span class="math display">\[\large\frac{
\Gamma \in x:t_1
}{
\Gamma \vdash x :: t_1 ; \bot
}\]</span></p>
<p>And application
<span class="math display">\[\large\frac{
  \Gamma \vdash M :: t_1 \rightarrow t2; e_1
  \qquad
  \Gamma \vdash N :: t_1 ; e_2
}{
  \Gamma \vdash M N :: t_2 ; e_1 \vee e_2
}\]</span></p>
<p>Claim that this system is better than the original effect system which forces
us to add an effect to the arrow kind (our arrow has the original pure type)
and also the Haskell approach with monads.</p>
<p>Use <code>suspend</code> within a lambda abstraction.</p>
<pre><code>\lambda(x:T1). suspend ....</code></pre>
<p>It’s obvious where to insert <code>suspend</code> as abstration bodies must be pure.</p>
<p><code>suspend</code> and <code>run</code> are syntactic; generated by compiler for source language.</p>
<h3 id="qa">Q&amp;A</h3>
<p>Constructive logics have judgements like this (M terminates and proves A is
true):</p>
<p><span class="math display">\[\large\Gamma \vdash M :: \text{A true}\]</span></p>
<p>Also have a judgement like this (<em>if</em> M terminates, it proves A):</p>
<p><span class="math display">\[\large\Gamma \vdash M :: \text{A lax}\]</span></p>
<p>And:</p>
<p><span class="math display">\[\large\frac{
  \Gamma \vdash M :: \text{A lax}
}{
  \Gamma \vdash box M :: \Box \text{A true}
}\]</span></p>
<p>(See PL summer school video?)</p>
<p>See also the Haskell Symposium paper (papers!) doing extensible effects in
Haskell.</p>
<h2 id="yow-conference">Yow! Conference</h2>
<p>There’s a potential for members to get a discounted ticket (group rate). Talk
to Jed about it.</p>]]></summary>
</entry>

</feed>
