GHC: Ticket #7019: Wrong error message when using forall in constraints
http://ghc.haskell.org/trac/ghc/ticket/7019
<p>
I tried this in GHC 7.4.1:
</p>
<pre class="wiki">{-# LANGUAGE ConstraintKinds, RankNTypes, UndecidableInstances #-}
newtype Free c a = Free { runFree :: forall r. c r => (a -> r) -> r }
type C c = forall a. c (Free c a)
instance C c => Monad (Free c) where
return a = Free ($ a)
Free f >>= g = f g
</pre><p>
Which gives this error:
</p>
<pre class="wiki"> Could not deduce (c (Free c b)) arising from a use of `f'
from the context (C c)
</pre><p>
So apparently the forall is silently dropped.
</p>
<p>
Without UndecidableInstances the error is:
</p>
<pre class="wiki"> Illegal irreducible constraint forall a. c (Free c a)
in superclass/instance head context (Use -XUndecidableInstances to permit this)
</pre><p>
giving the impression that it is allowed.
</p>
en-usGHChttp://ghc.haskell.org/trac/ghc/chrome/site/ghc_logo.png
http://ghc.haskell.org/trac/ghc/ticket/7019
Trac 1.0.1simonpjMon, 25 Jun 2012 06:21:27 GMTowner, difficulty set
http://ghc.haskell.org/trac/ghc/ticket/7019#comment:1
http://ghc.haskell.org/trac/ghc/ticket/7019#comment:1
<ul>
<li><strong>owner</strong>
set to <em>simonpj</em>
</li>
<li><strong>difficulty</strong>
set to <em>Unknown</em>
</li>
</ul>
Ticketsjoerd_visscherFri, 29 Jun 2012 12:31:15 GMT
http://ghc.haskell.org/trac/ghc/ticket/7019#comment:2
http://ghc.haskell.org/trac/ghc/ticket/7019#comment:2
<p>
Apparently it is allowed to use forall in constraints (I just saw that <a class="ext-link" href="http://stackoverflow.com/questions/11258536/how-to-put-constraints-on-the-associated-data"><span class="icon"></span>here</a>). I used the type synonym as a workaround, but that's not needed, so here's a simplified case:
</p>
<pre class="wiki">{-# LANGUAGE ConstraintKinds, RankNTypes, UndecidableInstances #-}
import Data.Monoid
newtype Free a = Free { runFree :: forall r. Monoid r => (a -> r) -> r }
instance (forall a. Monoid (Free a)) => Monad (Free) where
return a = Free ($ a)
(>>=) = runFree
</pre><p>
gives the error
</p>
<pre class="wiki"> Could not deduce (Monoid (Free b)) arising from a use of `runFree'
from the context (forall a. Monoid (Free a))
</pre><p>
I replaced the constaint variable with Monoid, to check that that's not the problem. So this new version of course has a simple workaround, but that workaround is not available in the constraint variable version.
</p>
TicketsimonpjFri, 29 Jun 2012 16:36:42 GMT
http://ghc.haskell.org/trac/ghc/ticket/7019#comment:3
http://ghc.haskell.org/trac/ghc/ticket/7019#comment:3
<p>
GHC does not support polymorphic constraints to the left of an arrow. The StackOverflow thread you point to contains this program:
</p>
<pre class="wiki">{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE UndecidableInstances #-}
module T7019a where
class Context c where
func1 :: c -> String
class (forall b. Context (Associated a b)) -- URK!
=> Class a where
data Associated a :: * -> *
newtype ClassTest = ClassTest { runClassTest :: String }
instance (forall b. Context (Associated ClassTest b)) -- URK!
=> Class ClassTest where
data Associated ClassTest b = ClassTestAssoc b (b -> ClassTest)
instance Context (Associated ClassTest b) where
func1 (ClassTestAssoc b strFunc) = runClassTest $ strFunc b
main = putStr . func1 $ ClassTestAssoc 37 (ClassTest . show)
</pre><p>
I'm astonished that this program typechecks. It certainly shouldn't. We have not begun to think about constraints like <tt>(forall b. (Associated ClassTest b))</tt> I'm afraid.
</p>
<p>
Actually, your Monoid example makes a lot more sense, but can't you just have a top-level instance
</p>
<pre class="wiki">instance Monoid (Free a) where ...(not sure)....
</pre><p>
in which case you are fine.
</p>
<p>
Wanting foralls on the left of an arrow is not stupid. See <a class="new ticket" href="http://ghc.haskell.org/trac/ghc/ticket/5927" title="feature request: A type-level "implies" constraint on Constraints (new)">#5927</a>, <a class="new ticket" href="http://ghc.haskell.org/trac/ghc/ticket/2893" title="feature request: Implement "Quantified contexts" proposal (new)">#2893</a>, <a class="closed ticket" href="http://ghc.haskell.org/trac/ghc/ticket/2456" title="feature request: For higher kinds, instance declarations need quantification in the context (closed: duplicate)">#2456</a>. But it's not going to happen soon, because I don't know how to specify or implement it. (Which is not to say that someone else may not be able to crack it.)
</p>
<p>
Meanwhile, these programs should be rejected, and I'll add the tests so that they are.
</p>
TicketsimonpjThu, 12 Jul 2012 16:42:36 GMTstatus changed; testcase, resolution set
http://ghc.haskell.org/trac/ghc/ticket/7019#comment:4
http://ghc.haskell.org/trac/ghc/ticket/7019#comment:4
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>closed</em>
</li>
<li><strong>testcase</strong>
set to <em>typecheck/should_fail/T7019, T7019a</em>
</li>
<li><strong>resolution</strong>
set to <em>fixed</em>
</li>
</ul>
<p>
Done, and tests added.
</p>
Ticket