[Haskell-cafe] Illegal type def

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Sat Dec 1 22:03:42 EST 2007

On Dec 1, 2007, at 21:43 , PR Stanley wrote:

> Hi
> > type assoc k v = [(k, v)]
> works beautifully and everything makes sense.
> > type Assoc v = (Ord k) => [(k, v)]
> This doesn't work. Is there any wayof defining k as an element of  
> type Ordinal. I could redefine k by putting Char or Int in its  
> place. Why can't I be more general?

Think of a type declaration as a macro which is expanded where it is  
used.  With parentheses around it and any unresolved references  
"forall"ed, because it has no idea what to do with them at  
declaration time.

So, if you use -fglasgow-exts, you could make the above type  
declaration.  But when you use it:

   foo :: Assoc Int -> Assoc Int

translates as

   foo :: (forall k. (Ord k) => [(k,Int)]) -> (forall k. (Ord k) =>  

This is almost certainly *not* what you want; the two "k"s are  

It could be argued that GHC should be smarter about it... but  
formalizing what that means is difficult (and subject to disagreements).

brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH

More information about the Haskell-Cafe mailing list