Skip to content

Instantly share code, notes, and snippets.

@strager
Last active December 21, 2015 05:49
Show Gist options
  • Select an option

  • Save strager/6259516 to your computer and use it in GitHub Desktop.

Select an option

Save strager/6259516 to your computer and use it in GitHub Desktop.
{-# LANGUAGE ExistentialQuantification #-}
import Data.Typeable (Typeable, TypeRep, typeOf)
import Unsafe.Coerce (unsafeCoerce)
data Magic = forall a. Magic
(a -> a -> Bool)
TypeRep
a
instance Eq Magic where
Magic eq typeRepX x == Magic _ typeRepY y
= typeRepX == typeRepY && x `eq` unsafeCoerce y
magic :: (Eq a, Typeable a) => a -> Magic
magic x = Magic (==) (typeOf x) x
-- > magic (10::Int) == magic ("hello"::String)
-- False
-- > magic (10::Int) == magic (10::Int)
-- True
-- > magic (10::Int) == magic (10::Double)
-- False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment