Haskell learns title and post types

Based on a recent exchange , I was convinced of using Template Haskell to generate some code for compilation type security.

I need to enter the names and types of record fields. I understand that getting field names using constrFields . toConstr :: Data a => a -> [String] constrFields . toConstr :: Data a => a -> [String] . But I need more than field names, I need to know their type. For example, I need to know the names of fields that are of type Bool .

How to create a function f :: a -> [(String, xx)] , where a is the record, String is the name of the field, and xx is the type of field?

+8
haskell template-haskell
source share
1 answer

The type must be available along with everything else in the Info value provided by reify . In particular, you should get a TyConI that contains a Dec value , from which you can get a list of Con values ​​defining the constructors . Then the record type should use RecC , which will give you the list of fields described by the tuple containing the field name, strict field and type .

From there, from there you will do whatever you want.


Edit : for the sake of actually demonstrating the above, here is a really awful quick and dirty function that finds entries:

 import Language.Haskell.TH test :: Name -> Q Exp test n = do rfs <- fmap getRecordFields $ reify n litE . stringL $ show rfs getRecordFields :: Info -> [(String, [(String, String)])] getRecordFields (TyConI (DataD _ _ _ cons _)) = concatMap getRF' cons getRecordFields _ = [] getRF' :: Con -> [(String, [(String, String)])] getRF' (RecC name fields) = [(nameBase name, map getFieldInfo fields)] getRF' _ = [] getFieldInfo :: (Name, Strict, Type) -> (String, String) getFieldInfo (name, _, ty) = (nameBase name, show ty) 

By importing this into another module, we can use it like this:

 data Foo = Foo { foo1 :: Int, foo2 :: Bool } foo = $(test ''Foo) 

Loading in GHCi, the value in foo is [("Foo",[("foo1","ConT GHC.Types.Int"),("foo2","ConT GHC.Types.Bool")])] .

Does this give you an idea?

+8
source share

All Articles