Created
August 10, 2022 15:10
-
-
Save Swordlash/768db40979de8df2369e9e54ebd373a0 to your computer and use it in GitHub Desktop.
Patterns
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
{-# LANGUAGE PatternSynonyms | |
, GADTs | |
, TypeApplications | |
, RankNTypes | |
, ScopedTypeVariables | |
, ViewPatterns | |
#-} | |
-- GADT of possible types | |
data Type a where | |
TInt :: Type Int | |
class ToType a where | |
toType :: Type a | |
instance ToType Int where | |
toType = TInt | |
-- some opaque type | |
data Expr a where | |
EInt :: Expr Int | |
-- existential adding info of ToType dictionary | |
data SomeExpr where | |
Expr :: ToType a => Expr a -> SomeExpr | |
-- explicit use of dictionary | |
typeof :: forall a. ToType a => Expr a -> Type a | |
typeof _ = toType @a | |
-- here something wrong happens | |
{-# COMPLETE (:::) #-} | |
--pattern (:::) :: ToType a => Expr a -> Type a -> SomeExpr | |
pattern expr ::: tt <- Expr expr@(typeof -> tt) | |
where expr ::: _ = Expr expr | |
test :: (MonadFail m) => m String | |
test = do | |
_e ::: t <- pure $ Expr EInt | |
case t of | |
TInt -> pure "int" | |
main :: IO () | |
main = test >>= putStrLn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment