Generics and type classes

Stefan Reich doc at drjava.de
Fri Jan 30 22:18:15 EST 2004


Hi,

please consider the following module:

	module TypeTest where

	import Data.Generics

	class Data a => MyClass a where
	  special :: a -> a

	generic :: MyClass a => a -> a
	generic = everywhere (mkT special)

The general idea is to define traversals about data types not known in 
advance. The data types must implement a "special" transformation which 
is then automatically extended to a "generic" transformation.

The definition of "generic" is but an example. In reality, there might 
also be special2 and special3 beside "special", and "generic" might 
combine the three special transformations in some way.

The problem is this - the module doesn't compile.

	TypeTest.hs:1:
	    Ambiguous type variable `b' in the top-level constraint:
	      `MyClass b' arising from use of `special' at TypeTest.hs:9

I suppose the type of "mkT special" is ambiguous. But how can I change 
that? No type annotation or combination of type annotations seems to help.

Suggestions appreciated,
-Stefan



More information about the Glasgow-haskell-users mailing list