How to define gunfold on List?
Hi, Does some one know how to define gunfold on recursive data struction like List? The ghc doc only give the example for non-recursive data like below. data T a b = C1 a b | C2 deriving (Typeable, Data) GHC will generate an instance that is equivalent to instance (Data a, Data b) => Data (T a b) where gfoldl k z (C1 a b) = z C1 `k` a `k` b gfoldl k z C2 = z C2 gunfold k z c = case constrIndex c of 1 -> k (k (z C1)) 2 -> z C2 toConstr (C1 _ _) = con_C1 toConstr C2 = con_C2 dataTypeOf _ = ty_T con_C1 = mkConstr ty_T "C1" [] Prefix con_C2 = mkConstr ty_T "C2" [] Prefix ty_T = mkDataType "Module.T" [con_C1, con_C2]
Hi there, There's no difference. See http://hackage.haskell.org/packages/archive/base/4.0.0.0/doc/html/src/Data-D... You can also see many other instances there. Cheers, Pedro On Sun, Jan 18, 2009 at 10:50, haihualin <haihualin@163.com> wrote:
Hi,
Does some one know how to define gunfold on recursive data struction like List?
The ghc doc only give the example for non-recursive data like below.
data T a b = C1 a b | C2 deriving (Typeable, Data)
GHC will generate an instance that is equivalent to
instance (Data a, Data b) => Data (T a b) where gfoldl k z (C1 a b) = z C1 `k` a `k` b gfoldl k z C2 = z C2
gunfold k z c = case constrIndex c of 1 -> k (k (z C1)) 2 -> z C2
toConstr (C1 _ _) = con_C1 toConstr C2 = con_C2
dataTypeOf _ = ty_T
con_C1 = mkConstr ty_T "C1" [] Prefix con_C2 = mkConstr ty_T "C2" [] Prefix ty_T = mkDataType "Module.T" [con_C1, con_C2]
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
participants (2)
-
haihualin -
José Pedro Magalhães