简体   繁体   中英

How do I replace Data.Generics with GHC.Generics?

So I've used syb for a long time, and often have functions like

friendlyNames :: Data a => a -> a
friendlyNames = everywhere (mkT (\(Name x _) -> Name x NameS))

What is the equivalent of this using GHC.Generics, assuming Generic a?

This might be the wrong problem to solve with GHC.Generics, but here's now you'd do it!

{-# Language TypeOperators #-}
{-# Language DeriveGeneric #-}
{-# Language DefaultSignatures #-}
{-# Language FlexibleContexts #-}
module Demo where

import GHC.Generics
import Language.Haskell.TH
import Language.Haskell.TH.Syntax

data Record = Record { field0 :: Int, field1 :: Maybe Record, field2 :: Name } deriving Generic

instance FriendlyNames Record -- body omitted and derived with GHC.Generics
instance FriendlyNames a => FriendlyNames (Maybe a)
instance FriendlyNames Int where friendlyNames = id -- no-op

------------------------------------------------------------------------

-- | Class for types that can be made friendly
class FriendlyNames a where
  friendlyNames :: a -> a
  default friendlyNames :: (GFriendlyNames (Rep a), Generic a) => a -> a
  friendlyNames = to . gfriendlyNames . from

-- | Replaces the second component of a name with 'NameS'
instance FriendlyNames Name where
  friendlyNames (Name x _) = Name x NameS

------------------------------------------------------------------------

-- | Class for generic structures that can have names made friendly
class GFriendlyNames f where
  gfriendlyNames :: f p -> f p

-- | Case for metadata (type constructor, data constructor, field selector)
instance GFriendlyNames f => GFriendlyNames (M1 i c f) where
  gfriendlyNames (M1 x) = M1 (gfriendlyNames x)

-- | Case for product types
instance (GFriendlyNames f, GFriendlyNames g) => GFriendlyNames (f :*: g) where
  gfriendlyNames (x :*: y) = gfriendlyNames x :*: gfriendlyNames y

-- | Case for sum types
instance (GFriendlyNames f, GFriendlyNames g) => GFriendlyNames (f :+: g) where
  gfriendlyNames (L1 x) = L1 (gfriendlyNames x)
  gfriendlyNames (R1 y) = R1 (gfriendlyNames y)

-- | Case for datatypes without any data constructors (why not?)
instance GFriendlyNames V1 where
  gfriendlyNames v1 = v1 `seq` error "gfriendlyNames.V1"

-- | Case for datatypes without any fields
instance GFriendlyNames U1 where
  gfriendlyNames U1 = U1

-- | Case for data constructor fields
instance FriendlyNames a => GFriendlyNames (K1 i a) where
  gfriendlyNames (K1 x) = K1 (friendlyNames x)

The GHC.Generics approach is more suited to situations where this kind of complexity can be written once and hidden away in a library. While the SYB approach relies on runtime checks, observe the GHC core that is generated for a friendlyNames that makes Record values friendly

-- RHS size: {terms: 14, types: 18, coercions: 0}
recordFriendlyNames
recordFriendlyNames =
  \ w_s63w ->
    case w_s63w of _ { Record ww1_s63z ww2_s63A ww3_s63B ->
    case $recordFriendlyNames ww1_s63z ww2_s63A ww3_s63B
    of _ { (# ww5_s63H, ww6_s63I, ww7_s63J #) ->
    Record ww5_s63H ww6_s63I ww7_s63J
    }
    }

-- RHS size: {terms: 19, types: 19, coercions: 0}
$recordFriendlyNames
$recordFriendlyNames =
  \ ww_s63z ww1_s63A ww2_s63B ->
    (# ww_s63z,
       case ww1_s63A of _ {
         Nothing -> Nothing;
         Just g1_a601 -> Just (recordFriendlyNames g1_a601)
       },
       case ww2_s63B of _ { Name x_a3Z3 ds_d5Z5 -> Name x_a3Z3 NameS } #)

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