Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7002845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:58:54+00:00 2026-05-27T20:58:54+00:00

Based on a recent exchange , I’ve been convinced to use Template Haskell to

  • 0

Based on a recent exchange, I’ve been convinced to use Template Haskell to generate some code to ensure compile-time type safety.

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

How do I construct a function f :: a -> [(String, xx)] where a is the record, String is the field name and xx is the field type?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:58:55+00:00Added an answer on May 27, 2026 at 8:58 pm

    The type should be available, along with everything else, in the Info value provided by reify. Specifically, you should get a TyConI, which contains a Dec value, from which you can get the list of Con values specifying the constructors. A record type should then use RecC, which will give you a list of fields described by a tuple containing the field name, whether the field is strict, and the type.

    Where you go from there depends on what you want to do with all this.


    Edit: For the sake of actually demonstrating the above, here’s a really terrible quick and dirty function that finds record fields:

    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)
    

    Importing that in another module, we can use it like so:

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

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

    Does that give you the rough idea?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On a recent Java project, we needed a free Java based real-time data plotting
I've written some simple php code designed to retrieve data from a database based
Some of my recent web projects that I worked on, use a flow engine
Based on this question it appears that the default template for CheckStyle will allow
I'm writing an Oracle query that needs to get the most recent event based
I'm developing a browser-based mobile app built on top of jQuery Mobile, and some
I have 2 tables. I Want to list the records based on the recent
I am having difficulty updating records within a database based on the most recent
I wrote my own PageStatePersister class based on SessionPageStatePersister which writes the most recent
I am trying to select the most recent record between 2 users based on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.