Singular Type Constructor
Tue, 31 Jul 2001 09:55:47 +0100
> data T a =3D T
> Is there anything in the Prelude or a standard library that=20
> looks like=20
> this? Should there be? What should it be called? 'Singleton'?=20
> I looked but couldn't find anything. Such a simple type=20
> constructor is in=20
> fact very useful when you need to pass types around to=20
> disambiguate class=20
> instances. For instance:
> class HasTypeName t where
> getTypeName :: T t -> String
> instance HasTypeName Int where
> getTypeName T =3D "Int"
> instance HasTypeName () where
> getTypeName T =3D "()"
> intName =3D getTypeName (T :: T Int)
The type T is undoubtedly useful, but I think the example you give isn't
a good one. It works perfectly well without the T data type:
class HasTypeName t where
getTypeName :: t -> String
instance HasTypeName Int where
getTypeName _ =3D "Int"
instance HasTypeName () where
getTypeName _ =3D "()"
intName =3D getTypeName (undefined :: Int)
(this is just like the Typeable class in the Dynamic library, BTW)