Guest User

Untitled

a guest
Nov 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. {-# LANGUAGE GADTs #-}
  2. {-# LANGUAGE DataKinds #-}
  3. {-# LANGUAGE TypeFamilies #-}
  4.  
  5. import Data.Singletons
  6.  
  7. -- | inductive structure
  8. data Foo = Foo Foo | End
  9.  
  10. -- | the isomorphic type for 'Foo'
  11. data RealFoo = RFoo RealFoo | REnd
  12. deriving (Show)
  13.  
  14. data instance Sing (a :: Foo) where
  15. EndS :: Sing 'End
  16. FooS :: Sing a -> Sing ('Foo a)
  17.  
  18. instance SingI 'End where
  19. sing = EndS
  20. instance SingI a => SingI ('Foo a) where
  21. sing = FooS sing
  22.  
  23. instance SingKind Foo where
  24. type DemoteRep Foo = RealFoo
  25. fromSing EndS = REnd
  26. fromSing (FooS s) = RFoo (fromSing s)
  27. toSing REnd = SomeSing EndS
  28. toSing (RFoo r) = case toSing r of
  29. SomeSing s -> SomeSing (FooS s)
  30.  
  31. main :: IO ()
  32. main = do
  33. print $ fromSing (sing :: Sing 'End) -- => REnd
  34. print $ fromSing (sing :: Sing ('Foo 'End)) -- => RFoo REnd
Add Comment
Please, Sign In to add comment