Created
July 31, 2019 08:12
-
-
Save sarkologist/7ba54f286bca9b349f8df315a79f7634 to your computer and use it in GitHub Desktop.
dynamic typing in haskell (from sandy maguire's thinking with types)
This file contains 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
data Dynamic where | |
Dynamic :: Typeable t => t -> Dynamic | |
elimDynamic :: (forall a. Typeable a => a -> r) -> Dynamic -> r | |
elimDynamic f (Dynamic a) = f a | |
fromDynamic :: Typeable a => Dynamic -> Maybe a | |
fromDynamic = elimDynamic cast | |
liftD2 :: forall a b r. ( Typeable a , Typeable b , Typeable r ) | |
=> Dynamic -> Dynamic -> (a -> b -> r) -> Maybe Dynamic | |
liftD2 d1 d2 f = fmap Dynamic . f <$> fromDynamic @a d1 <*> fromDynamic @b d2 | |
pyPlus :: Dynamic -> Dynamic -> Dynamic pyPlus a b = | |
fromMaybe (error "bad types for pyPlus") $ asum | |
[ liftD2 @String @String a b (++) | |
, liftD2 @Int @Int a b (+) | |
, liftD2 @String @Int a b $ \strA intB -> strA ++ show intB | |
, liftD2 @Int @String a b $ \intA strB -> show intA ++ strB | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment