[Haskell-cafe] Type constrain in instance?

Louis Zhuang louis.zhuang at acm.org
Fri Apr 9 11:22:09 EDT 2010


Hi, I got an issue when playing haskell's type system, partically parametic
class/instance have no way to specify constrains of their parameters. for
example, i want to data struct to chain functions and their derivatives so we
can have combined function and derivations, as following

import qualified Control.Category as Cat

data ChainableFunction a b = CF { cfF :: (a->b), cfDeriv :: (a->b) }

class Module a b wher4e
  (*>) :: a -> b -> b

instance Cat.Category CF where
  id :: (Num a) -- GHC dis-allow this
  id = CF id (const 1)
  (.) :: (Num a, Num b, Num c, Module a b, Module b c)
      => CF a b -> CF b c -> CF a c -- GHC disallow this either
  (.) (CF f f') (CF g g') = CF (g.f) (\a -> f' a *> g' (f a))



However GHC only has kinds for class/instance like (*->*->*) so we are forced to
allow all possible types in instance code. I'm not sure if I'm modelling things
correctly or is there another way to do the same thing?

Cheers, Louis



More information about the Haskell-Cafe mailing list