UserPreferences

WrapperTypes


WrapperTypes are usually trivial wrappers (i.e. newtypes) that are designed to convey some information to the type system. NonTrivialTypeSynonyms and TypeclassWrapper are both instances of this. This idiom is also in a synergistic relation to PhantomTypes, TraitsTypeclass, and SimulatingDependentTypes.

http://www.haskell.org/pipermail/haskell/2004-August/014397.html gives a rather involved example that uses this idiom.

One use of WrapperTypes is to add PhantomTypes to a pre-existing (e.g. 3rd party) type.

Another example of WrapperTypes occurs in WxHaskell to handle interfacing to an OO library. The ?SubTyping relationship is represented as nested WrapperTypes, so that type Labrador a = CAnimal (CDog (CLabrador a)) would represent a Labrador or a subclass of it, and bark :: CAnimal (CDog a) -> IO () would be a function that works for any CDog or subclass of it.

TraitsTypeclass and WrapperTypes often give two different approaches to solving the same problem. For example, if you want to compare two Strings for equality in different ways (mainly case-sensitive and case-insensitive) you can either use a wrapper to adapt String to the Eq class,

newtype CIString = CIString String

instance Eq CIString where
    CIString a == CIString b = map toUpper a == map toUpper b
or you can make a TraitsTypeclass
class MyEq traits a where
    cmp :: traits -> a -> a -> Bool

data CaseSensitive
data CaseInsensitive

instance MyEq CaseSensitive String where cmp _ = (==)
instance MyEq CaseInsensitive String where cmp _ a b = map toUpper a == map toUpper b

As the example illustrates, the two approaches have different trade-offs, but we can also get some of the benefits of both with the following synergy between PhantomTypes and TraitsTypeclass (and perhaps also WrapperTypes). What we do is store the traits type variable in a phantom type variable (added in this case via a wrapper type) which avoids the need for a ReifiedType parameter or the construction of a custom class (when the class already exists).

newtype PString a = PString String

data CaseSensitive
data CaseInsensitive

instance Eq (PString CaseSensitive) where PString a = PString b = a == b
instance Eq (PString CaseInsensitive) where
    PString a == PString b = map toUpper a == map toUpper b
This gives us the benefit of only having one type that we can parameterize to different implementations and the benefit of working with pre-existing type classes, it does however require us to provide a phantom type argument.


CategoryIdiom