Created
July 9, 2019 15:00
-
-
Save dmwit/eddd0ff330fdec670857657c70e2599e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TypeApplications #-} | |
{-# LANGUAGE TypeFamilies #-} | |
import Type.Reflection | |
data Foo = Int Int | Bool Bool | |
wrap :: Typeable b => b -> Maybe Foo | |
wrap b | |
| Just HRefl <- eqTypeRep t (typeRep @Int) = Just (Int b) | |
| Just HRefl <- eqTypeRep t (typeRep @Bool) = Just (Bool b) | |
| otherwise = Nothing | |
where | |
t = typeOf b | |
onFoo :: (Typeable a, Typeable b) => (a -> b) -> Foo -> Maybe Foo | |
onFoo f foo = case foo of | |
Int i | Just HRefl <- eqTypeRep t (typeRep @Int) -> wrap (f i) | |
Bool b | Just HRefl <- eqTypeRep t (typeRep @Bool) -> wrap (f b) | |
_ -> Nothing | |
where | |
Fun t _ = typeOf f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment