add constraint to the context of the RULE

Claude Heiland-Allen claude at mathr.co.uk
Mon Mar 20 21:54:16 UTC 2017


Hi all,

I have these functions:

    toDouble :: (Rounding r, Precision p) => Rounded r p -> Double
    fromDouble :: (Rounding r, Precision p) => Double -> Rounded r p

and I want to use RULES for optimization:

    {-# RULES "realToFrac/toDouble" realToFrac = toDouble #-}
    {-# RULES "realToFrac/fromDouble" realToFrac = fromDouble #-}

This doesn't work.  I found out (thanks to Stack Overflow via #haskell)
that I can modify the first rule to the following, which does compile
and also seems to fire in a simple test:

    {-# RULES "realToFrac/toDouble" forall (x :: (Rounding r, Precision
p) => Rounded r p) . realToFrac x = toDouble x #-}

But I'm at a loss with the fromDouble rule, the error message is:

    • Could not deduce (Rounding r) arising from a use of ‘fromDouble’
      from the context: (Real Double, Fractional (Rounded r p))
        bound by the RULE "realToFrac/fromDouble"
        at src/Numeric/Rounded.hs:359:11-57
      Possible fix:
        add (Rounding r) to the context of the RULE "realToFrac/fromDouble"
    • In the expression: fromDouble
      When checking the transformation rule "realToFrac/fromDouble"

How do I apply the "possible fix" to the fromDouble rule?

Is it even possible with the RULES syntax?

Note, there are these instances (among others):

    instance (Rounding r, Precision p) => Real (Rounded r p)
    instance (Rounding r, Precision p) => Fractional (Rounded r p)

Full code at https://code.mathr.co.uk/rounded (claude5 branch).

Thanks for any insight,


Claude
-- 
https://mathr.co.uk


More information about the Glasgow-haskell-users mailing list