[Haskell-cafe] know a workaround for greedy context reduction?

Simon Peyton-Jones simonpj at microsoft.com
Sun Dec 7 15:34:40 EST 2008


This is perfectly reasonable behavior I'm afraid.  If you do ":info d" you'll get d's original type signature.  But ":type" takes an *arbitrary expression* (in this case a single variable 'd', and figures out its most general type.  You could have said ":t (3*3)" for example.

In this case, when inferring the most general type of the expression "d", GHC tries to simplify the context (D a), and uses the instance declaration to reduce it to (C a).  And then it can't simplify it further.  But you *might* have had
        instance C a
somewhere, in which case it'd have been able to simplify the (C a) away.  So GHC must try that route.  If it fails, you want it to "back up" to a notationally more convenient type, but GHC can't do that, I'm afraid

Simon

| -----Original Message-----
| From: haskell-cafe-bounces at haskell.org [mailto:haskell-cafe-
| bounces at haskell.org] On Behalf Of Nicolas Frisby
| Sent: 06 December 2008 03:23
| To: haskell Cafe
| Subject: [Haskell-cafe] know a workaround for greedy context reduction?
|
| With these three declarations
|
|   {-# LANGUAGE FlexibleInstances #-}
|   {-# LANGUAGE UndecidableInstances #-}
|
|   class C a where c :: a
|   class C a => D a where d :: a
|   instance C a => D a where d = c
|
| ghci exhibits this behavior:
|
|   *> :t d
|   d :: (C a) => a
|
| Where I would prefer "d :: (D a) => a". In my actual examples, the
| context is much larger and I can't involve overlapping instances. Is
| there a known workaround? I didn't find a related bug on the GHC trac,
| and I don't know if other compilers behave in the same way.
| _______________________________________________
| Haskell-Cafe mailing list
| Haskell-Cafe at haskell.org
| http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list