{-# OPTIONS -fglasgow-exts #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE DeriveDataTypeable #-}
module NestedDatatypes () where
{-
We provide an illustrative ScrapYourBoilerplate example for a nested
datatype. For clarity, we do not derive the Typeable and Data
instances by the deriving mechanism but we show the intended
definitions. The overall conclusion is that nested datatypes do not
pose any challenge for the ScrapYourBoilerplate scheme. Well, this is
maybe not quite true because it seems like we need to allow
undecidable instances.
-}
import Data.Dynamic
import Data.Generics
-- A nested datatype
data Nesta = Boxa | Wrap (Nest [a]) deriving Typeable-- The Data instance for the nested datatype
instance (Data a, Data [a]) =>Data (Nesta)
where
gfoldlkz (Boxa) = zBox`k`agfoldlkz (Wrapw) = zWrap`k`wgmapTf (Boxa) = Box (fa)
gmapTf (Wrapw) = Wrap (fw)
toConstr (Box _) = boxConstrtoConstr (Wrap _) = wrapConstrgunfoldkzc = case constrIndexc of
1 -> k (zBox)
2 -> k (zWrap)
dataTypeOf _ = nestDataTypeboxConstr = mkConstrnestDataType"Box" [] PrefixwrapConstr = mkConstrnestDataType"Wrap" [] PrefixnestDataType = mkDataType"Main.Nest" [boxConstr,wrapConstr]