[Haskell-cafe] Template Haskell, information about data constructor types

Neil Mitchell ndmitchell at gmail.com
Sun Jun 3 18:00:42 EDT 2007


Hi

I'm trying to write a function:

reaches :: Type -> Q [Type]

The intention is that reaches (Either Bool [Int]) would return [Either
Bool [Int], Bool, [Int], Int] - i.e. all types which are contained by
the initial type at any level.

I took a shot at this:

getTypes :: Type -> Q [Type]
getTypes t = do
        let (ConT c, cs) = typeApp t
        TyConI dat <- reify c
        return $ concatMap ctorTypes $ dataCtors dat


reaches :: Type -> Q [Type]
reaches t = f [] [t]
    where
        f done [] = return done
        f done (t:odo)
            | t `elem` done = f done odo
            | otherwise = do
                ts <- getTypes t
                f (t:done) (odo ++ ts)

Where typeApp splits a type to find its constructor, ctorTypes gets
the types of the fields, and dataCtors gets the constructors in a data
type. Unfortunately reify doesn't seem to work on types. Is it
possible to do what I am after?

Thanks

Neil


More information about the Haskell-Cafe mailing list