[Haskell-cafe] How to define Show [MyType] ?

Ryan Ingram ryani.spam at gmail.com
Tue Dec 9 06:09:56 EST 2008

Interesting.  But from a practical point of view (I know, irrelevant!
*grin*), it's very tempting to piggyback the entirety of the typeclass
feature on a simple technique like this.

It also gives you the benefit that functions like this:
> manyConstraints :: (Show a, Eq a, Data a) => a -> Bool
end up only taking a single "type" argument, instead of one per class.

In addition, when you use types directly you can remove arguments that
are determined by FDs:
> class Elem c e | c -> e where ...
> instance Elem [a] a where ...

> toList :: Elem c e => c -> [e]
> toList = ...

turns into something like

> toList :: Type c -> c -> [e] using e = fdElem c
> fdElem c = typecase c of
>    [e] -> e
>    ... other instances ...

(without the dependency, you have to pass both the type of c and e to toList.)

   -- ryan

On Tue, Dec 9, 2008 at 1:36 AM, Wouter Swierstra <wss at cs.nott.ac.uk> wrote:
>> The biggest wart is that "view" is not a total function; the compiler
>> needs to be extra careful to only call it on types that are instances
>> of "View".  I wonder if there is a good way to solve this problem?
> The usual way to solve this is to define a data type corresponding to all
> the types in your class. For example:
> data Data a where
>  | CHAR : Data Char
>  | STRING : Data String
>  | LIST : Data a -> Data [a]
>  ...
> With this representation you no longer need typecase (which is horrendous
> semantic hack) and your dispatch function can be made total. Hope this
> helps,
>  Wouter
> This message has been checked for viruses but the contents of an attachment
> may still contain software viruses, which could damage your computer system:
> you are advised to perform your own checks. Email communications with the
> University of Nottingham may be monitored as permitted by UK legislation.

More information about the Haskell-Cafe mailing list