GHC: Ticket #4398: Panic with FlexibleContexts and rewrite rules
http://ghc.haskell.org/trac/ghc/ticket/4398
<p>
I wanted a rewrite rule that fired if one of the type variables was an instance of Ord. To make that work, I tried something like this:
</p>
<pre class="wiki">{-# LANGUAGE FlexibleContexts #-}
{-# RULES "suspicious" forall (x :: a) y. f (x :: Ord a => a) y = g x y #-}
{-# NOINLINE f #-}
f :: a -> a -> Bool
f x y = True
g :: Ord a => a -> a -> Bool
g = (<)
main = print $ f 2 1
</pre><p>
Which generates a rewrite rule like this:
</p>
<pre class="wiki">"suspicious" ALWAYS
forall {@ a $dOrd :: Ord a x :: a y :: a}
f @ a x y
= g @ a $dOrd x y
</pre><p>
Naturally, $dOrd isn't present on the left hand side so we get a panic.
</p>
<p>
Is there any way to write this kind of rule without inducing a panic? I suspect that there isn't, but would like to be proven wrong.. sometimes you can optimise better if you can e.g. see that a type is Orderable.
</p>
en-usGHChttp://ghc.haskell.org/trac/ghc/chrome/site/ghc_logo.png
http://ghc.haskell.org/trac/ghc/ticket/4398
Trac 1.2simonpjThu, 21 Oct 2010 10:16:41 GMTstatus changed; testcase set
http://ghc.haskell.org/trac/ghc/ticket/4398#comment:1
http://ghc.haskell.org/trac/ghc/ticket/4398#comment:1
<ul>
<li><strong>testcase</strong>
set to <em>simplCore/should_compile/T4398</em>
</li>
<li><strong>status</strong>
changed from <em>new</em> to <em>merge</em>
</li>
</ul>
<p>
Fundamentally you can't do this. Rewrite rules are run by the simplifier, and it knows nothing of instance declarations etc. So any evidence (dictionaries) needed on the RHS must be available on the LHS of the rule.
</p>
<p>
But there shouldn't be a panic. I've improved matters as far as giving a (still slightly opaque) warning, and discarding the rule, rather than panicing.
</p>
<pre class="wiki">Thu Oct 21 09:54:02 BST 2010 simonpj@microsoft.com
* Improve rule checking, to fix panic Trac #4398
Lots of comments with decomposeRuleLhs
M ./compiler/deSugar/Desugar.lhs -9 +4
M ./compiler/deSugar/DsBinds.lhs -75 +82
Thu Oct 21 09:50:30 BST 2010 simonpj@microsoft.com
* Improve the simple expression optimiser so it does simple beta reduction
M ./compiler/coreSyn/CoreSubst.lhs -31 +43
</pre><p>
I think this is worth merging.
</p>
<p>
Simon
</p>
TicketiglooSat, 23 Oct 2010 12:08:35 GMTstatus changed; resolution set
http://ghc.haskell.org/trac/ghc/ticket/4398#comment:2
http://ghc.haskell.org/trac/ghc/ticket/4398#comment:2
<ul>
<li><strong>status</strong>
changed from <em>merge</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>fixed</em>
</li>
</ul>
<p>
Both merged.
</p>
Ticket