Define fmap in terms of the Data data class

Is it possible to define fmap in terms of a Data typeclass from a Data.Data ?

It seems that with gfoldl it was not possible to change types. Are there other combinators that can do this?

I suppose this cannot be done in the general case, since no one can influence only Right 's on Either aa , but maybe it can be done for some cases like Maybe ?

(I know fmap easy to output, but I'm still wondering if this is possible with Data )

+4
source share
1 answer

Here, for example, using syb from 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 ta b. (Typeable a, Data (t (C a)), Data (ta)) => (a -> b) -> ta -> tb fmapData f input = uc . everywhere (mkT $ \(x::C a) -> uc (f (uc x))) $ (uc input :: t (C a)) where uc = unsafeCoerce 
+1
source

All Articles