[Haskell-cafe] Fwd: Type families - how to resolve ambiguities?

Ryan Ingram ryani.spam at gmail.com
Mon Sep 13 14:34:34 EDT 2010


On Sun, Sep 12, 2010 at 9:24 AM, Dominique Devriese
<dominique.devriese at cs.kuleuven.be> wrote:
>> However, it would make more sense to have it be a type family, without
>> the overhead of data (both in space and in typing).
>
> You can make Tensor a data family and use "newtype instances". As I
> understand these, there should not be a space overhead. The only
> overhead I would expect this to introduce is the extra newtype
> constructor.

Which is only at programming time; newtype constructors do not exist
at runtime.  They get erased after typechecking.

This also means that pattern matching against newtype constructors
cannot fail.  For example:

> data family F a
> data instance F Bool = B ()
> newtype instance F Int = I ()
>
> fBool :: F Bool -> Int
> f1 (B _) = 3
>
> fInt :: F Int -> Int
> f2 (I _) = 4
>
> main = do
>    print (fInt undefined)
>    print (fBool undefined)

This program should print 4 and then exit with an error.

  -- ryan


More information about the Haskell-Cafe mailing list