<div dir="ltr"><div class="gmail_default" style="font-family:'courier new',monospace"></div><div class="markdown-here-wrapper" id="markdown-here-wrapper-132138" style><p style="margin:1.2em 0px!important">Hi Christopher,</p>

<p style="margin:1.2em 0px!important">What you want is to make <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">b</code> (and <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">a</code>) depend on <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">f</code>. This can be done in several ways.</p>

<p style="margin:1.2em 0px!important">With functional dependencies:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="language-haskell" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline;white-space:pre;overflow:auto;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block;padding:0.5em;color:rgb(51,51,51);background-color:rgb(248,248,255);background-repeat:initial initial"><span class="class" style="color:rgb(68,85,136);font-weight:bold"><span class="keyword" style="color:rgb(51,51,51);font-weight:bold">class</span> (<span class="type">Integral</span> a, <span class="type">Num</span> b) => <span class="type">PartialSum</span> a b f | f -> a b <span class="keyword" style="color:rgb(51,51,51);font-weight:bold">where</span></span>
  partialSum :: f -> a -> b

<span class="class" style="color:rgb(68,85,136);font-weight:bold"><span class="keyword" style="color:rgb(51,51,51);font-weight:bold">instance</span> (<span class="type">Integral</span> a, <span class="type">Num</span> b) => <span class="type">PartialSum</span> a b (a -> b) <span class="keyword" style="color:rgb(51,51,51);font-weight:bold">where</span></span>
  partialSum f n = foldl (\u v -> u + f v) <span class="number" style="color:rgb(0,153,153)">0</span> [<span class="number" style="color:rgb(0,153,153)">1.</span>.n]</code></pre>
<p style="margin:1.2em 0px!important">With type families:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="language-haskell" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline;white-space:pre;overflow:auto;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block;padding:0.5em;color:rgb(51,51,51);background-color:rgb(248,248,255);background-repeat:initial initial"><span class="class" style="color:rgb(68,85,136);font-weight:bold"><span class="keyword" style="color:rgb(51,51,51);font-weight:bold">class</span> <span class="type">PartialSum</span> f <span class="keyword" style="color:rgb(51,51,51);font-weight:bold">where</span></span>
  <span class="typedef"><span class="keyword" style="color:rgb(51,51,51);font-weight:bold">type</span> <span class="type">End</span> f</span>
  <span class="typedef"><span class="keyword" style="color:rgb(51,51,51);font-weight:bold">type</span> <span class="type">Res</span> f</span>
  partialSum' :: f -> <span class="type">End</span> f -> <span class="type">Res</span> f

<span class="class" style="color:rgb(68,85,136);font-weight:bold"><span class="keyword" style="color:rgb(51,51,51);font-weight:bold">instance</span> (<span class="type">Integral</span> a, <span class="type">Num</span> b) => <span class="type">PartialSum</span> (a -> b) <span class="keyword" style="color:rgb(51,51,51);font-weight:bold">where</span></span>
  <span class="typedef"><span class="keyword" style="color:rgb(51,51,51);font-weight:bold">type</span> <span class="type">End</span> <span class="container">(<span class="title" style="color:rgb(153,0,0);font-weight:bold">a</span> -> <span class="title" style="color:rgb(153,0,0);font-weight:bold">b</span>)</span> = a</span>
  <span class="typedef"><span class="keyword" style="color:rgb(51,51,51);font-weight:bold">type</span> <span class="type">Res</span> <span class="container">(<span class="title" style="color:rgb(153,0,0);font-weight:bold">a</span> -> <span class="title" style="color:rgb(153,0,0);font-weight:bold">b</span>)</span> = b</span>
  partialSum f n = foldl (\u v -> u + f v) <span class="number" style="color:rgb(0,153,153)">0</span> [<span class="number" style="color:rgb(0,153,153)">1.</span>.n]</code></pre>
<p style="margin:1.2em 0px!important">I can’t see though what you’re trying to achieve. Could you provide some more use cases for that class?</p>
<p style="margin:1.2em 0px!important">Best,<br>Nick</p>
</div><div class="gmail_default" style="font-family:'courier new',monospace"></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/11/2 Christopher Howard <span dir="ltr"><<a href="mailto:christopher.howard@frigidcode.com" target="_blank">christopher.howard@frigidcode.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi. I am playing around with basic Haskell overloading. What I'm interested in is how to do specialization in Haskell -- i.e., functions that work generically on all (or many) types but work more efficiently on certain types. So, I am trying to make a class of functions which can be fed into a partial sum calculator. Maybe something like so:<br>

<br>
class PartialSum f where<br>
    -- params: term function, end index<br>
    partialSum :: Integral b => f -> b -> a<br>
<br>
The most generic instance would be any function that takes an integer and returns a number. The specialized instances would things like, say, a wrapped function which is guaranteed to be linear (through safe constructors or something). But I'm having trouble figuring out how even to make the generic version. I'm thinking something like this:<br>

<br>
instance PartialSum (a -> b) where<br>
    partialSum f n = foldl (\u v -> u + f v) 0 [1..n]<br>
<br>
But the compiler complains it can't prove that the input to the "f" function is the same type as the "n" parameter. That makes sense, but I'm not sure how to fix that.<br>
______________________________<u></u>_________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br></div>