Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So backticks turn a binary function into an infix operator, making</p> <pre><code>x `op` y </code></pre> <p>equivalent to</p> <pre><code>op x y </code></pre> <p>So <code>op</code> needs to be of type <code>a -&gt; b -&gt; c</code> where <code>x :: a</code> and <code>y :: b</code>.</p> <p>In your case, <code>op</code> was <code>mempty</code>, with the type <code>Monoid m =&gt; m</code>. But we know it to be of the form <code>a -&gt; b -&gt; c</code>, so substitute that and you get (this is no longer valid syntax) <code>Monoid (a -&gt; b -&gt; c) =&gt; a -&gt; b -&gt; c</code>, because we can substitute that <code>m</code> for anything as long as the constraint holds.</p> <p>Now we know (due to the instance declaration) that any function of the form <code>s -&gt; t</code>, where <code>t</code> is a Monoid, is a Monoid itself, and we also know that <code>a -&gt; b -&gt; c</code> is really <code>a -&gt; (b -&gt; c)</code>, i.e. a function taking one argument and returning another function. So if we substitute <code>a</code> for <code>s</code> and <code>(b -&gt; c)</code> for <code>t</code>, the we fulfill the Monoid instance, if <code>t</code> is a Monoid. Of course, <code>t</code> is <code>(b -&gt; c)</code>, so we can apply the same Monoid instance again (with <code>s = b</code> and <code>t = c</code>), so if <code>c</code> is a Monoid, we're good.</p> <p>So what is <code>c</code>? The expression you had was</p> <pre><code>PQ.singleton `mempty` [start] </code></pre> <p>i.e.</p> <pre><code>mempty PQ.singleton [start] </code></pre> <p>The instance declaration for <code>Monoid (a -&gt; b)</code> defines <code>mempty _ = mempty</code>, i.e. it's a function that ignores its argument and returns the empty element of the <code>b</code> Monoid. In other words, we can expand the call above to</p> <pre><code>mempty [start] </code></pre> <p>i.e. we ignore the argument and use mempty of the inner Monoid (which is <code>b -&gt; c</code>). Then we repeat, ignoring the argument again:</p> <pre><code>mempty </code></pre> <p>So the expression you had is just equivalent to a single <code>mempty</code>, which has the type <code>Monoid c =&gt; c</code>, i.e. it can be any Monoid whatsoever.</p> <p>In your case, the larger expression deduces <code>c</code> to be a <code>PQ.MinPQueue</code>. And <code>MinPQueue</code> is a Monoid instance with <code>mempty</code> being the empty queue.</p> <p>This is how you end up with the result you're seeing.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload