<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Passing Curiosity: Posts tagged data structures</title>
    <link href="https://passingcuriosity.com/tags/data-structures/data-structures.xml" rel="self" />
    <link href="https://passingcuriosity.com" />
    <id>https://passingcuriosity.com/tags/data-structures/data-structures.xml</id>
    <author>
        <name>Thomas Sutton</name>
        
        <email>me@thomas-sutton.id.au</email>
        
    </author>
    <updated>2005-08-25T00:00:00Z</updated>
    <entry>
    <title>Annoyances of the Feature Kind</title>
    <link href="https://passingcuriosity.com/2005/annoyances-of-the-feature-kind/" />
    <id>https://passingcuriosity.com/2005/annoyances-of-the-feature-kind/</id>
    <published>2005-08-25T00:00:00Z</published>
    <updated>2005-08-25T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>Yesterday, I finally sat down and wrote a priority queue (a <a href="http://en.wikipedia.org/wiki/Heap">heap</a> to those
in the know) to store the formulae on a branch that still need to be processed.
Chris Okasaki (in his book <a href="http://www.amazon.com/dp/0521663504/">Purely Functional Data Structures</a>) defines the
interface of a heap as follows:</p>
<p><code><span class="keyword">class</span> Heap h <span class="keyword">where</span><code style="border: none;"> empty <span class="keyword">::</span> (<span class="keyword">Ord</span> a) <span class="keyword">=&gt;</span> h a
isEmpty <span class="keyword">::</span> (<span class="keyword">Ord</span> a) <span class="keyword">=&gt;</span> h a <span class="keyword">-&gt;</span> Bool</p>
<p>insert <span class="keyword">::</span> (<span class="keyword">Ord</span> a) <span class="keyword">=&gt;</span> a <span class="keyword">-&gt;</span> h a <span class="keyword">-&gt;</span> h a
merge <span class="keyword">::</span> (<span class="keyword">Ord</span> a) <span class="keyword">=&gt;</span> h a <span class="keyword">-&gt;</span> h a <span class="keyword">-&gt;</span> h a</p>
<p>findMin <span class="keyword">::</span> (<span class="keyword">Ord</span> a) <span class="keyword">=&gt;</span> h a <span class="keyword">-&gt;</span> a
deleteMin <span class="keyword">::</span> (<span class="keyword">Ord</span> a) <span class="keyword">=&gt;</span> h a <span class="keyword">-&gt;</span> h a
</code></code></p>
<p>This is a problem (in my opinion) in that it requires that heaps (all heaps) be
over ordered data-types. The entire <em>reason</em> that I am attempting to extend the
heap is to remove the ordering from the contained type, and use a priority type
instead. Thus we are no longer saying that, for example, <code>? &lt; ?</code> but that
<code>(priority ?) &lt; (priority ?)</code>.</p>
<p>I’ll be the first to admit that this isn’t much of a distinction to make
(especially from a pragmatic “programmer” point of view), but it is a nicer
approach.</p>
<p>I got around this problem, such as it is, by modifying my Prioritised class to
have a function to inject values into my Priority type and one to extract them.
The heap then is a type synonym:</p>
<p><code><span class="keyword">type</span> PriorityHeap t <span class="keyword">=</span> LeftistHeap (Priority t)</code></p>
<p>with the restriction that you need to explicitly wrap values you insert into a
heap:</p>
<p><code style="text-align: center;"><span class="keyword">let</span> h’ <span class="keyword">=</span> insert (priority v) h <span class="keyword">in</span>
…</code></p>
<p>and unwrap those you get out:</p>
<p><code style="text-align: center;"><span class="keyword">let</span> v <span class="keyword">=</span> value <span class="keyword">$</span> findMin h’ <span class="keyword">in</span> …</code></p>
<p>Not terribly inconvenient, but not ideal. I’m fairly sure that this could be
solved with some functional dependencies magic, but the current state of
affairs is good enough to be getting on with.</p>]]></summary>
</entry>

</feed>
