Skip to content

Instantly share code, notes, and snippets.

@luqui
Created March 4, 2013 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luqui/5080206 to your computer and use it in GitHub Desktop.
Save luqui/5080206 to your computer and use it in GitHub Desktop.
unsafeCoerce from TypeFamilies and GeneralizedNewtypeDeriving
{-# 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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment