[Haskell-cafe] Exporting a Type Class for Type Signatures

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Tue Nov 11 06:23:01 EST 2008


Dominic Steinitz <dominic.steinitz at blueyonder.co.uk> wrote:

> Is there a way of allowing someone to use
> AESKey in a type signature but not allow them to declare new
> instances?

I was going to suggest you export the class abstractly, that is, without
its methods, so no-one could externally create an instance.  But then I
noticed that AESKey has no methods - it is just an alias for a
combination of other classes - so that trick is insufficient.

So, the other thing you could do is to introduce an indirection.

  module This (AESKey) where
    class (Bits a, Integral a)  => AESKeyIndirection a	-- un-exported
    class (AESKeyIndirection a) => AESKey a		-- exported

    instance AESKeyIndirection Word128
    instance AESKeyIndirection Word192
    instance AESKeyIndirection Word256

    instance AESKey Word128
    instance AESKey Word192
    instance AESKey Word256

Creating an instance of AESKey requires that you also create an instance
of AESKeyIndirection.  If only one of the names is available externally
(exported), then instances can only be created internal to the module.

Regards,
    Malcolm


More information about the Haskell-Cafe mailing list