[Haskell-cafe] Deduce problem.

Ben Franksen ben.franksen at online.de
Mon Nov 21 18:27:21 CET 2011


Magicloud Magiclouds wrote:
> So I think I got what you guys meant, I limited ClassB to only H.
> Then how to archive my requirement, that from and to only return items
> that instanced ClassB?

If you are willing to go beyond Haskell98 (or Haskell2010), you can use a 
multi-parameter class. Enable the extension:

{-# LANGUAGE MultiParamTypeClasses #-}

An then, instead of

class (ClassA a) => ClassC a where
  from :: (ClassB b) => a -> [b]
  to :: (ClassB c) => a -> [c]

you say

class (ClassA a, ClassB b) => ClassC a b c where
  from :: c -> [b]
  to :: c -> [a]

This means that for each triple of concrete types (a,b,c) that you wish to 
be an instance of ClassC, you must provide an instance declaration, e.g.

instance ClassC Test H H where
  from = ...whatever...
  to = ...whatever...

Now you have the fixed type H in the instance declaration and not a 
universally quantified type variable.

Cheers
Ben




More information about the Haskell-Cafe mailing list