+\begin{code}
+gen_Typeable_binds :: TyCon -> LHsBinds RdrName
+gen_Typeable_binds tycon
+ = unitBag $
+ mk_easy_FunBind tycon_loc
+ (mk_typeOf_RDR tycon) -- Name of appropriate type0f function
+ [nlWildPat]
+ (nlHsApps mkTypeRep_RDR [tycon_rep, nlList []])
+ where
+ tycon_loc = getSrcSpan tycon
+ tycon_rep = nlHsVar mkTyConRep_RDR `nlHsApp` nlHsLit (mkHsString (showSDoc (ppr tycon)))
+
+mk_typeOf_RDR :: TyCon -> RdrName
+-- Use the arity of the TyCon to make the right typeOfn function
+mk_typeOf_RDR tycon = varQual_RDR tYPEABLE (mkFastString ("typeOf" ++ suffix))
+ where
+ arity = tyConArity tycon
+ suffix | arity == 0 = ""
+ | otherwise = show arity
+\end{code}
+
+
+
+%************************************************************************
+%* *
+\subsection{Data}
+%* *
+%************************************************************************
+
+From the data type
+
+ data T a b = T1 a b | T2
+
+we generate
+
+ $cT1 = mkDataCon $dT "T1" Prefix
+ $cT2 = mkDataCon $dT "T2" Prefix
+ $dT = mkDataType "Module.T" [] [$con_T1, $con_T2]
+ -- the [] is for field labels.
+
+ instance (Data a, Data b) => Data (T a b) where
+ gfoldl k z (T1 a b) = z T `k` a `k` b
+ gfoldl k z T2 = z T2
+ -- ToDo: add gmapT,Q,M, gfoldr
+
+ gunfold k z c = case conIndex c of
+ I# 1# -> k (k (z T1))
+ I# 2# -> z T2
+
+ toConstr (T1 _ _) = $cT1
+ toConstr T2 = $cT2
+
+ dataTypeOf _ = $dT
+
+\begin{code}
+gen_Data_binds :: FixityEnv
+ -> TyCon
+ -> (LHsBinds RdrName, -- The method bindings
+ LHsBinds RdrName) -- Auxiliary bindings
+gen_Data_binds fix_env tycon
+ = (listToBag [gfoldl_bind, gunfold_bind, toCon_bind, dataTypeOf_bind],
+ -- Auxiliary definitions: the data type and constructors
+ datatype_bind `consBag` listToBag (map mk_con_bind data_cons))
+ where
+ tycon_loc = getSrcSpan tycon
+ tycon_name = tyConName tycon
+ data_cons = tyConDataCons tycon
+ n_cons = length data_cons
+ one_constr = n_cons == 1
+
+ ------------ gfoldl
+ gfoldl_bind = mk_FunBind tycon_loc gfoldl_RDR (map gfoldl_eqn data_cons)
+ gfoldl_eqn con = ([nlVarPat k_RDR, nlVarPat z_RDR, nlConVarPat con_name as_needed],
+ foldl mk_k_app (nlHsVar z_RDR `nlHsApp` nlHsVar con_name) as_needed)
+ where
+ con_name :: RdrName
+ con_name = getRdrName con
+ as_needed = take (dataConSourceArity con) as_RDRs
+ mk_k_app e v = nlHsPar (nlHsOpApp e k_RDR (nlHsVar v))
+
+ ------------ gunfold
+ gunfold_bind = mk_FunBind tycon_loc
+ gunfold_RDR
+ [([k_Pat, z_Pat, if one_constr then nlWildPat else c_Pat],
+ gunfold_rhs)]
+
+ gunfold_rhs
+ | one_constr = mk_unfold_rhs (head data_cons) -- No need for case
+ | otherwise = nlHsCase (nlHsVar conIndex_RDR `nlHsApp` c_Expr)
+ (map gunfold_alt data_cons)
+
+ gunfold_alt dc = mkSimpleHsAlt (mk_unfold_pat dc) (mk_unfold_rhs dc)
+ mk_unfold_rhs dc = foldr nlHsApp
+ (nlHsVar z_RDR `nlHsApp` nlHsVar (getRdrName dc))
+ (replicate (dataConSourceArity dc) (nlHsVar k_RDR))
+
+ mk_unfold_pat dc -- Last one is a wild-pat, to avoid
+ -- redundant test, and annoying warning
+ | tag-fIRST_TAG == n_cons-1 = nlWildPat -- Last constructor
+ | otherwise = nlConPat intDataCon_RDR [nlLitPat (HsIntPrim (toInteger tag))]
+ where
+ tag = dataConTag dc
+
+ ------------ toConstr
+ toCon_bind = mk_FunBind tycon_loc toConstr_RDR (map to_con_eqn data_cons)
+ to_con_eqn dc = ([nlWildConPat dc], nlHsVar (mk_constr_name dc))
+
+ ------------ dataTypeOf
+ dataTypeOf_bind = mk_easy_FunBind
+ tycon_loc
+ dataTypeOf_RDR
+ [nlWildPat]
+ (nlHsVar data_type_name)
+
+ ------------ $dT
+
+ data_type_name = mkDerivedRdrName tycon_name mkDataTOcc
+ datatype_bind = mkVarBind
+ tycon_loc
+ data_type_name
+ ( nlHsVar mkDataType_RDR
+ `nlHsApp` nlHsLit (mkHsString (showSDoc (ppr tycon)))
+ `nlHsApp` nlList constrs
+ )
+ constrs = [nlHsVar (mk_constr_name con) | con <- data_cons]
+
+
+ ------------ $cT1 etc
+ mk_constr_name con = mkDerivedRdrName (dataConName con) mkDataCOcc
+ mk_con_bind dc = mkVarBind
+ tycon_loc
+ (mk_constr_name dc)
+ (nlHsApps mkConstr_RDR (constr_args dc))
+ constr_args dc =
+ [ -- nlHsIntLit (toInteger (dataConTag dc)), -- Tag
+ nlHsVar data_type_name, -- DataType
+ nlHsLit (mkHsString (occNameString dc_occ)), -- String name
+ nlList labels, -- Field labels
+ nlHsVar fixity] -- Fixity
+ where
+ labels = map (nlHsLit . mkHsString . getOccString)
+ (dataConFieldLabels dc)
+ dc_occ = getOccName dc
+ is_infix = isDataSymOcc dc_occ
+ fixity | is_infix = infix_RDR
+ | otherwise = prefix_RDR
+
+gfoldl_RDR = varQual_RDR gENERICS FSLIT("gfoldl")
+gunfold_RDR = varQual_RDR gENERICS FSLIT("gunfold")
+toConstr_RDR = varQual_RDR gENERICS FSLIT("toConstr")
+dataTypeOf_RDR = varQual_RDR gENERICS FSLIT("dataTypeOf")
+mkConstr_RDR = varQual_RDR gENERICS FSLIT("mkConstr")
+mkDataType_RDR = varQual_RDR gENERICS FSLIT("mkDataType")
+conIndex_RDR = varQual_RDR gENERICS FSLIT("constrIndex")
+prefix_RDR = dataQual_RDR gENERICS FSLIT("Prefix")
+infix_RDR = dataQual_RDR gENERICS FSLIT("Infix")