Changes between Version 48 and Version 49 of Commentary/Compiler/GenericDeriving


Ignore:
Timestamp:
Dec 20, 2011 1:25:02 PM (2 years ago)
Author:
dreixel
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Commentary/Compiler/GenericDeriving

    v48 v49  
    4545= Kind polymorphic overhaul = 
    4646 
    47 With the new `-XPolyKinds` functionality we can make the support for generic programming better typed. The basic idea is to define the universe codes (`M1`, `:+:`, etc.) as constructors of a datatype. Promotion then lifts these constructors to types, which we can use as before, only that now we have them all classified under a new kind: 
    48  
    49 {{{ 
    50 {-# LANGUAGE TypeOperators              #-} 
    51 {-# LANGUAGE GADTs                      #-} 
    52 {-# LANGUAGE TypeFamilies               #-} 
    53 {-# LANGUAGE ScopedTypeVariables        #-} 
    54 {-# LANGUAGE FlexibleContexts           #-} 
    55 {-# LANGUAGE FlexibleInstances          #-} 
    56 {-# LANGUAGE PolyKinds                  #-} 
    57 {-# LANGUAGE DefaultSignatures          #-} 
    58 {-# LANGUAGE NoImplicitPrelude          #-} 
    59  
    60 module GHC.NewGenerics where 
    61  
    62 import Prelude hiding (Functor(..), Show(..)) -- we'll redefine them 
    63 import qualified Prelude as P (Show(..)) 
    64 }}} 
     47With the new `-XPolyKinds` functionality we can make the support for generic programming better typed. The basic idea is to define the universe codes (`M1`, `:+:`, etc.) as constructors of a datatype. Promotion then lifts these constructors to types, which we can use as before, only that now we have them all classified under a new kind. The overhaul of the main module is explained below; for easier comparison with the current approach, names are kept the same whenever possible. 
    6548 
    6649== Generic representation universe == 
     
    175158 
    176159-- Meta data classes 
    177 class Datatype d where 
     160class Datatype d where -- kind polymorphic 
    178161  -- The name of the datatype, fully qualified 
    179162  datatypeName :: Proxy d -> String 
     
    183166 
    184167== Conversion between user datatypes and generic representation == 
     168 
     169We now get a more precise kind for `Rep`: 
     170 
    185171{{{ 
    186172-- Representable types of kind * 
     
    237223  gfmap f (Comp1 x) = Comp1 (fmap (gfmap f) x) 
    238224}}} 
     225 
     226Note that previously `Functor` and `GFunctor` had exactly the same types. 
     227Now we can make clear what the difference between them is. 
    239228   
    240229== Example generic function: `show` (kind `*`, uses metadata) ==