public
Created

unsafeCoerce from TypeFamilies and GeneralizedNewtypeDeriving

  • Download Gist
unsafeCoerce.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
{-# LANGUAGE TypeFamilies, GeneralizedNewtypeDeriving #-}
 
type family Switch b a
type instance Switch b (A a) = a
type instance Switch b (B a) = b
 
newtype A a = A a
newtype B a = B (A a)
deriving (Switchable)
 
class Switchable a where
switch :: a -> Switch b a
 
instance Switchable (A a) where
switch (A x) = x
 
unsafeCoerce :: a -> b
unsafeCoerce x = switch (B (A x))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.