[Haskell-cafe] Constraining constraints

Clinton Mead clintonmead at gmail.com
Sun May 7 04:19:01 UTC 2017


I posted the following on stackoverflow
<http://stackoverflow.com/questions/43827374/what-is-a-can-be-derived-from-constraint>,
but it hasn't got too much attention so I thought I'd ask here:

I can write the following:

{-# LANGUAGE RankNTypes #-}{-# LANGUAGE FlexibleInstances #-}{-#
LANGUAGE UndecidableInstances #-}{-# LANGUAGE ConstraintKinds #-}

f :: Integral a => (forall b. Num b => b) -> a
f = id

And all is good. Presumably GHC can derive Integral from Num so all is well.

I can be a bit tricker, yet I'm still fine:

class Integral x => MyIntegral xinstance Integral x => MyIntegral x
class Num x => MyNum xinstance Num x => MyNum x

f' :: MyIntegral a => (forall b. MyNum b => b) -> a
f' = id

So lets say I want to generalise this, like so:

g :: c2 a => (forall b. c1 b => b) -> a
g = id

Now obviously this will spit the dummy, because GHC can not derive c2 from
c1, as c2 is not constrained.

What do I need to add to the type signature of g to say that "you can
derive c2 from c1"?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170507/e89b9db9/attachment.html>


More information about the Haskell-Cafe mailing list