Some library use unsafeCoerce
to temporarily satisfy constraint:
class Given a where given :: a
newtype Gift a r = Gift (Given a => r)
give :: forall a r. a -> (Given a => r) -> r
give a k = unsafeCoerce (Gift k :: Gift a r) a
(This example is from reflection package. singletons package also uses this trick.)
Why is this unsafeCoerce
safe?
Is there any official document which guarantee that Given a => r
and a -> r
have the same runtime representation in GHC?
No official document guarantees it. Ed Kmett is relying on what he knows about the inner workings of GHC. What he knows:
->
and =>
actually mean the same thing.I've actually written a proposal to try to do this legitimately, but it's tricky to accommodate all the use cases properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With