Advertisement
Guest User

Generic construction of Rec fields

a guest
Jan 9th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE
  2.     DataKinds
  3.   , ExistentialQuantification
  4.   , PolyKinds
  5.   , TypeFamilies
  6.   , TypeOperators
  7.   #-}
  8.  
  9. module Main where
  10.  
  11. import Data.Vinyl
  12. import Data.Vinyl.TypeLevel
  13. import Data.Singletons
  14.  
  15. data Fields = Name | Age | Sleeping | Master deriving Show
  16. data instance Sing '(Fields, Name)     = SName
  17. data instance Sing '(Fields, Age)      = SAge
  18. data instance Sing '(Fields, Sleeping) = SSleeping
  19. data instance Sing '(Fields, Master)   = SMaster
  20.  
  21. type LifeForm = [Name, Age, Sleeping]
  22.  
  23. class Field f where
  24.    type ElF f (t :: k) :: *
  25.    data Attr f (t :: k) :: *
  26.    (=:) :: sing '(f,k) -> ElF f k -> Attr f k
  27.   _unAttr :: Attr f k -> ElF f k
  28.  
  29. instance Field Fields where
  30.   type ElF Fields Name     = String
  31.   type ElF Fields Age      = Int
  32.   type ElF Fields Sleeping = Bool
  33.   type ElF Fields Master   = Rec (Attr Fields) LifeForm
  34.   data Attr Fields k = Attr (ElF Fields k)
  35.   _ =: x = Attr x
  36.   _unAttr (Attr x) = x
  37.  
  38. --jon :: Rec (Attr Fields) LifeForm
  39. jon = (SName =: "jon") :& (SAge =: 2) :& (SSleeping =: False) :& RNil
  40.  
  41. --tucker :: Rec (Attr Fields) (LifeForm ++ '[Master])
  42. tucker = (SName =: "tucker") :& (SAge =: 9) :& (SSleeping =: True) :& (SMaster =: jon) :& RNil
  43.  
  44. data Other = Foo | Bar | Baz deriving Show
  45. data instance Sing '(Other, Foo) = SFoo
  46. data instance Sing '(Other, Bar) = SBar
  47. data instance Sing '(Other, Baz) = SBaz
  48.  
  49. instance Field Other where
  50.   type ElF Other Foo = Int
  51.   type ElF Other Bar = Double
  52.   type ElF Other Baz = String
  53.   data Attr Other k = Attr' (ElF Other k)
  54.    _ =: x = Attr' x
  55.   _unAttr (Attr' x) = x
  56.  
  57. foo = (SFoo =: 2) :& (SBar =: 3.0) :& (SBaz =: "baz") :& RNil
  58.  
  59. main = putStrLn "hello,world"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement