這裏有一個例子使用syb
從here
{-# LANGUAGE DeriveDataTypeable, ScopedTypeVariables, FlexibleContexts #-}
import Data.Generics
import Unsafe.Coerce
{- | C tags the type that is actually parameterized, so to avoid touching the
Int when a ~ Int:
> data T a = T Int a
by changing the type (not representation) to:
> x :: T Int (C Int)
-}
newtype C a = C a deriving (Data,Typeable)
fmapData :: forall t a b. (Typeable a, Data (t (C a)), Data (t a)) =>
(a -> b) -> t a -> t b
fmapData f input = uc . everywhere (mkT $ \(x::C a) -> uc (f (uc x)))
$ (uc input :: t (C a))
where uc = unsafeCoerce
有使用例如'syb'給出底部是[here](http://www.haskell.org/haskellwiki/Scrap_your_boilerplate)。它使用'unsafeCoerce';不知道是否有一個不那麼噁心的做法。 – jberryman
@jberryman:你應該發佈這個答案。 – sclv