[Haskell-cafe] more generic class instances?
Christopher Howard
christopher.howard at frigidcode.com
Sat Nov 2 05:40:43 UTC 2013
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:
class PartialSum f where
-- params: term function, end index
partialSum :: Integral b => f -> b -> a
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:
instance PartialSum (a -> b) where
partialSum f n = foldl (\u v -> u + f v) 0 [1..n]
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.
More information about the Haskell-Cafe
mailing list