[Haskell-cafe] MultiParamClasses question
Daniel Fischer
daniel.is.fischer at web.de
Tue May 25 15:27:59 EDT 2010
On Tuesday 25 May 2010 20:51:06, Eugeny N Dzhurinsky wrote:
> Hello, all!
>
> I'm trying to create set of polymorphic functions for working with
> custom containers. I decided to try out typeclass and define generic
> function, which uses the methods from the typeclass. The quick and naive
> solution is listed below:
As Stephen said, FunctionalDependencies and TypeFamilies are two options.
The problem is that stList is unusable because there's no way to find out
which instance to use (determine k and t) from a use of stList.
Also, you can't determine a from a use of stExists, so that's unusable,
too.
You could make all class parameters depend on b (via FunDeps or
TypeFamilies), or
- make b a type constructor of kind (* -> *) and
- move stList to its own class.
class StList b where
stList :: b a -> [a]
class Storage k t b where
stExists :: k -> t -> b a -> Bool
stAdjust :: k -> t -> (a -> a) -> b a -> b a
stInsert :: k -> t -> a -> b a -> b a
>
> ========================================================================
Don't put language extensions in an OPTIONS_GHC pragma, use a LANGUAGE
pragma instead.
> {-# OPTIONS_GHC -XMultiParamTypeClasses -XTypeSynonymInstances #-}
> import Data.List as L
>
> class Storage k t a b where
> stExists :: k -> t -> b -> Bool
> stAdjust :: k -> t -> ( a -> a ) -> b -> b
> stInsert :: k -> t -> a -> b -> b
> stList :: b -> [a]
>
More information about the Haskell-Cafe
mailing list