<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Passing Curiosity: Posts tagged monoids</title>
    <link href="https://passingcuriosity.com/tags/monoids/monoids.xml" rel="self" />
    <link href="https://passingcuriosity.com" />
    <id>https://passingcuriosity.com/tags/monoids/monoids.xml</id>
    <author>
        <name>Thomas Sutton</name>
        
        <email>me@thomas-sutton.id.au</email>
        
    </author>
    <updated>2013-05-29T00:00:00Z</updated>
    <entry>
    <title>FP-Syd, May 2013</title>
    <link href="https://passingcuriosity.com/2013/fp-syd-may/" />
    <id>https://passingcuriosity.com/2013/fp-syd-may/</id>
    <published>2013-05-29T00:00:00Z</published>
    <updated>2013-05-29T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>Sounds like BayHAC was interesting.</p>
<h2 id="jed-on-variance-in-scala">Jed on variance (in Scala)</h2>
<p>Variance is essentially about substitution or sub-typing. A thing that produces
a <code>Super</code> may be replaced with a thing that takes <code>Sub</code>. A think that that
takes <code>Sub</code> may be replaces by a thing that takes `Super.</p>
<blockquote>
<p><code>Rat -&gt; Real &lt;: Int -&gt; Complex</code></p>
</blockquote>
<p>Reference to</p>
<blockquote>
<p>Be liberal in what you accept and conservative in what you produce.</p>
</blockquote>
<p>Use site variance in Java</p>
<pre><code>final function Option&lt;a&gt;</code></pre>
<p>In Scala:</p>
<pre><code>List[+A]

Function[-T1, +R]

Kleisli[M[+_], -A, +B]</code></pre>
<p>Eg:</p>
<pre><code>class GPar
class Par extends GPar
class Child extends Par

class Box[+A]

def foo(x:Bor[Par]): 

foo(Box[Child])
foo(Box[GPar]) // error</code></pre>
<p>f</p>
<pre><code>trait Box[+A] {
	def get: A
	def take(a: A) // Error
}

trait Box[-A] {
	def get: A
	def take(a: A)
}</code></pre>
<p>Functor</p>
<pre><code>trait Functor[F[_]] {
	def map[A,B](a: F[A])(f: A =&gt; B): F[B]
}</code></pre>
<p>This is actually a contra-variant functor.</p>
<pre><code>trait Contravariant[F[_]] {
	def contramap[A,B](r: R[A])(f: B=&gt;A): F[B]
}</code></pre>
<p>Variance in mutable values:</p>
<blockquote>
<p>Two type variables. One goes up (things coming out?), one goes down (things
going in?).</p>
</blockquote>
<p>http://biosimilarity.blogspot.com.au/2011/05/of-monads-and-games.html</p>
<h2 id="tim-on-monoid">Tim on monoid</h2>
<pre><code>class Monoid a where
    mempty :: a
    mappend :: a -&gt; a -&gt; a</code></pre>
<p>Obeys laws</p>
<pre><code>mappend a mempty == a
mappend mempty a == a
mappend a (mappend b c) == mappend (mappend a b) c</code></pre>
<p>Given types may have multiple monoids: numbers have (+, 0) and (*, 1).</p>
<p>Define monoids to do min, max, count on the pattern of <code>Sum</code> and <code>Product</code>.
Have a smart constructor for each</p>
<p>The foldable type-class makes fold polymorphic.</p>
<pre><code>class Foldable t where
    foldMap :: Monoid m =&gt; (a -&gt; m) -&gt; t a -&gt; m</code></pre>
<p>Use the monoids we defined earlier:</p>
<pre><code>foldMap sum as
foldMap count as
foldMap max as</code></pre>
<p>The <code>mappend</code> operation distributes through structures like tuple types too.
This lets apply multiple monoids with one traversal.</p>
<pre><code>a2 :: Applicative a =&gt; a b -&gt; a c -&gt; a (b, c)
a2 b c = (,) &lt;$&gt; b &lt;*&gt; c

&gt; foldMap (a2 min max) as
(Min 1, Max 456)</code></pre>
<p>This doesn’t let us do Mean very nicely. We need to unpack the pair at the end
and calculate the actual</p>
<p>Have an additional <code>Aggregation</code> class (superclass of <code>Monoid</code>) which has an
associated type for the result which is extracted from the aggregation.</p>
<p>So we can make a version of foldMap which can finish and unwrap the
aggregation:</p>
<pre><code>afoldMap :: (Foldable t, Aggregation a) =&gt; (v -&gt; a) -&gt; t v -&gt; AggResult a
afoldMap f vs = AggResult (foldMap f vs)</code></pre>
<p>Combine the aggregation and monoid to apply a filter during the single
traversal.</p>
<p>Two monoids for maps:</p>
<ol type="1">
<li><p>Replace the values</p></li>
<li><p>Insist the values are monoids too and append them.</p></li>
</ol>
<p>Compose accessors with the “smart” constructors and apply them to calculate
statistics over a stream of records.</p>
<h2 id="amos-robinson-on-optimising-purely-functional-loops">Amos Robinson on optimising purely functional loops</h2>
<p>Live at UNSW and looking at some optimisation problems. SpecConstr (constructor
specialisation) is part of GHC (phase? optimisation?); has some problems,
particularly with stream fusion, DPH, etc.</p>
<p>Dot product (pairwise multiple two vectors and sum the results) is a motivating
example. We want to write that as:</p>
<pre><code>dotp as bs = fold (+) 0 $ zipWith (*) as bs</code></pre>
<p>But we want to actually execute:</p>
<pre><code>dotp as bs = go 0 0
  where
    go i acc
    | i &gt; V.length as
    = acc
    | otherwise
    = go (i+1) (acc + (as!i * bs!i))</code></pre>
<p>(Assuming that the Ints aren’t boxed, etc.)</p>]]></summary>
</entry>

</feed>
