<div dir="ltr"><pre><font face="georgia, serif">The Indexable class is declared as </font></pre><pre><font face="&#39;courier new&#39;, monospace"><span class="hs-keyword" style="color:blue">class</span> <span class="hs-conid">Lookup</span> <span class="hs-varid">f</span> <span class="hs-keyglyph" style="color:red">=&gt;</span> <span class="hs-conid">Indexable</span> <span class="hs-varid">f</span> <span class="hs-keyword" style="color:blue">where</span>
<a name="line-161"></a>  <span class="hs-varid">index</span> <span class="hs-keyglyph" style="color:red">::</span> <span class="hs-varid">f</span> <span class="hs-varid">a</span> <span class="hs-keyglyph" style="color:red">-&gt;</span> <span class="hs-conid">Key</span> <span class="hs-varid">f</span> <span class="hs-keyglyph" style="color:red">-&gt;</span> <span class="hs-varid">a</span></font></pre>

<pre><font face="georgia, serif">Why f must be instance of Lookup?</font></pre><br><div class="gmail_quote">2012/4/12 Michael Sloan <span dir="ltr">&lt;<a href="mailto:mgsloan@gmail.com">mgsloan@gmail.com</a>&gt;</span><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello!<br>
<br>
Yes, classes of that variety exist in a few packages.  This is a<br>
<div class="im">particularly good treatment of it:<br>
<br>
<a href="http://hackage.haskell.org/package/keys" target="_blank">http://hackage.haskell.org/package/keys</a><br>
<br>
<br>
</div>Here are some classes from a very WIP implementation of a few<br>
<div class="im">&quot;Commutative Replicated Data Types&quot;:<br>
<br>
<a href="https://github.com/mgsloan/crdt/blob/master/src/Data/CRDT/Classes.hs" target="_blank">https://github.com/mgsloan/crdt/blob/master/src/Data/CRDT/Classes.hs</a><br>
<br>
</div>&quot;Function&quot; is identical to your addressable, without (#).  There&#39;re<br>
also classes for &quot;Update&quot;-able, &quot;Zero&quot;-able, and &quot;Size&quot;-able things.<br>
Zero has a strange definition because CRDT sets need to communicate<br>
what has been deleted, clearing a set results in a value that is not<br>
the same as &quot;zero&quot;.  I suppose that &quot;clear&quot; aught to be in a separate<br>
class.<br>
<div class="im HOEnZb"><br>
-Michael Sloan<br>
<br>
On Wed, Apr 11, 2012 at 10:47 PM, 陈文龙 &lt;<a href="mailto:qzchenwl@gmail.com">qzchenwl@gmail.com</a>&gt; wrote:<br>
&gt;<br>
</div><div class="HOEnZb"><div class="h5">&gt; To get element in List,Map... in python&#39;s way.<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; Python:<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;&gt; strMap[&quot;apple&quot;]<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; Haskell:<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;&gt; strMap # &quot;apple&quot;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; <a href="https://gist.github.com/2364395" target="_blank">https://gist.github.com/2364395</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; {-# LANGUAGE TypeFamilies #-}<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; module Addressable where<br>
&gt;<br>
&gt; import qualified Data.Map as M<br>
&gt;<br>
&gt; import Prelude<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; class Addressable a where<br>
&gt;<br>
&gt;     type Key a<br>
&gt;<br>
&gt;     type Value a<br>
&gt;<br>
&gt;     (#!) :: a -&gt; Key a -&gt; Value a<br>
&gt;<br>
&gt;     (#)  :: a -&gt; Key a -&gt; Maybe (Value a)<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; instance Addressable [a] where<br>
&gt;<br>
&gt;     type Key [a] = Int<br>
&gt;<br>
&gt;     type Value [a] = a<br>
&gt;<br>
&gt;     (#!) = (!!)<br>
&gt;<br>
&gt;     xs     # i | i &lt; 0 = Nothing<br>
&gt;<br>
&gt;     []     # _         = Nothing<br>
&gt;<br>
&gt;     (x:_)  # 0         = Just x<br>
&gt;<br>
&gt;     (_:xs) # n         = xs # (n-1)<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; instance (Ord k) =&gt; Addressable (M.Map k v) where<br>
&gt;<br>
&gt;     type Key (M.Map k v) = k<br>
&gt;<br>
&gt;     type Value (M.Map k v) = v<br>
&gt;<br>
&gt;     a #! i = a M.! i<br>
&gt;<br>
&gt;     a #  i = M.lookup i a<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; main :: IO ()<br>
&gt;<br>
&gt; main = do<br>
&gt;<br>
&gt;     let strMap = M.fromList [(&quot;one&quot;,&quot;1&quot;),(&quot;two&quot;,&quot;2&quot;),(&quot;three&quot;,&quot;3&quot;)]<br>
&gt;<br>
&gt;     let strList = [&quot;1&quot;,&quot;2&quot;,&quot;3&quot;]<br>
&gt;<br>
&gt;     print $ strMap  #  &quot;two&quot;      -- Just &quot;2&quot;<br>
&gt;<br>
&gt;     print $ strMap  #! &quot;two&quot;      -- &quot;2&quot;<br>
&gt;<br>
&gt;     print $ strList #  0          -- Just &quot;1&quot;<br>
&gt;<br>
&gt;     print $ strList #! 0          -- &quot;1&quot;<br>
&gt;<br>
&gt;     print $ strMap  #  &quot;no-exist&quot; -- Nothing<br>
&gt;<br>
&gt;     print $ strList #  100        -- Nothing<br>
&gt;<br>
&gt;     print $ strMap  #! &quot;no-exist&quot; -- error<br>
&gt;<br>
&gt;     print $ strList #! 100        -- error<br>
&gt;<br>
&gt;<br>
</div></div><div class="HOEnZb"><div class="h5">&gt; _______________________________________________<br>
&gt; Haskell-Cafe mailing list<br>
&gt; <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
&gt; <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
&gt;<br>
</div></div></blockquote></div><br></div>