Skip to content

Instantly share code, notes, and snippets.

@dmwit
Created July 9, 2019 15:00
Show Gist options
  • Save dmwit/eddd0ff330fdec670857657c70e2599e to your computer and use it in GitHub Desktop.
Save dmwit/eddd0ff330fdec670857657c70e2599e to your computer and use it in GitHub Desktop.
{-# 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