简体   繁体   中英

How do GHC.Generics representations work?

I am really confused by the 'Generic representation type' in GHC.Generics. I don't understand how this datatype works. For example, Meta-information, M1 :

newtype M1 i c f p
M1
  unM1 :: f p

Why edoes the type M1 have four type variables: i, c, f and p. By that I mean, why 4 . Does it have some special meaning?

Also, any books or papers good for learning about GHC Generics for beginners would be appreciated.

There are not a lot of resources out there for GHC.Generics. Your best bet is the documentation in hackage, blog posts and reading source code.

Here is a simplified reference list of type synonyms I keep in order to remember what all the types from GHC.Generics mean.

type TypeName = D1 ('MetaSelector name moduleName packageName isNewType) rest
-- Company from data Company = MkCompany { name :: String, address :: String }

type Constructor = C1 ('MetaCons constructor prefixOrInfix isRecord) rest
-- MkCompany from data Company = MkCompany { name :: String, address :: String }

type NamedSelector = S1 ('MetaSel ('Just selector) unpackednesss sourceStrictness decidedStrictness)
-- age from data Person = Person { age :: Int }

type UnnamedSelector = S1 ('MetaSel Nothing unpackednesss sourceStrictness decidedStrictness)
-- Int from data Wrapper = Wrapper Int

type ConcatSelectors = (:*:)

type ConcatConstructors = (:+:)

type TypeParameter a = Rec0 a
-- a from the right side of data Wrapper a = Wrapper {getA :: a}

type ConstructorEmptyValuePlaceholder = U1
-- data Empty = MkEmpty, MkEmpty is a Constructor but takes no values
-- ConstructorEmptyValuePlaceholder fills the type requirement of Constructor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM