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 4091968
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:25:58+00:00 2026-05-20T19:25:58+00:00

Is there a way to programmatically get a list of instances of a type

  • 0

Is there a way to programmatically get a list of instances of a type class?

It strikes me that the compiler must know this information in order to type check and compile the code, so is there some way to tell the compiler: hey, you know those instances of that class, please put a list of them right here (as strings or whatever some representation of them).

  • 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-20T19:25:59+00:00Added an answer on May 20, 2026 at 7:25 pm

    You can generate the instances in scope for a given type class using Template Haskell.

    import Language.Haskell.TH
    
    -- get a list of instances
    getInstances :: Name -> Q [ClassInstance]
    getInstances typ = do
      ClassI _ instances <- reify typ
      return instances
    
    -- convert the list of instances into an Exp so they can be displayed in GHCi
    showInstances :: Name -> Q Exp
    showInstances typ = do
      ins <- getInstances typ
      return . LitE . stringL $ show ins
    

    Running this in GHCi:

    *Main> $(showInstances ''Num)
    "[ClassInstance {ci_dfun = GHC.Num.$fNumInteger, ci_tvs = [], ci_cxt = [], ci_cls = GHC.Num.Num, ci_tys = [ConT GHC.Integer.Type.Integer]},ClassInstance {ci_dfun = GHC.Num.$fNumInt, ci_tvs = [], ci_cxt = [], ci_cls = GHC.Num.Num, ci_tys = [ConT GHC.Types.Int]},ClassInstance {ci_dfun = GHC.Float.$fNumFloat, ci_tvs = [], ci_cxt = [], ci_cls = GHC.Num.Num, ci_tys = [ConT GHC.Types.Float]},ClassInstance {ci_dfun = GHC.Float.$fNumDouble, ci_tvs = [], ci_cxt = [], ci_cls = GHC.Num.Num, ci_tys = [ConT GHC.Types.Double]}]"
    

    Another useful technique is showing all instances in scope for a given type class using GHCi.

    Prelude> :info Num
    class (Eq a, Show a) => Num a where
      (+) :: a -> a -> a
      (*) :: a -> a -> a
      (-) :: a -> a -> a
      negate :: a -> a
      abs :: a -> a
      signum :: a -> a
      fromInteger :: Integer -> a
        -- Defined in GHC.Num
    instance Num Integer -- Defined in GHC.Num
    instance Num Int -- Defined in GHC.Num
    instance Num Float -- Defined in GHC.Float
    instance Num Double -- Defined in GHC.Float
    

    Edit: The important thing to know is that the compiler is only aware of type classes in scope in any given module (or at the ghci prompt, etc.). So if you call the showInstances TH function with no imports, you’ll only get instances from the Prelude. If you have other modules in scope, e.g. Data.Word, then you’ll see all those instances too.

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

Sidebar

Related Questions

On OSX is there any way to programmatically get the list of all iOS
Is there anyway to programmatically get a list of iOS apps from iTunes? AppleScript
I am wondering if there is any way for me to get list versioning
Is there a way to programmatically get to the details statistics of a youtube
is there a way to programatically get a list of all feeds and there
Is there a way to get a list of available sound cards on the
There's a way to get programmatically the dimensions of a photo taken with an
Is there a way to get programmatically latest changeset version in general. It's fairly
Is there any way to programmatically find the frame that a function was defined
I have some VirtualBox VMs running. Is there any way to programatically get the

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.