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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:21:08+00:00 2026-06-12T01:21:08+00:00

In Haskell to define an instance of a type class you need to supply

  • 0

In Haskell to define an instance of a type class you need to supply a dictionary of functions required by the type class. I.e. to define an instance of Bounded, you need to supply a definition for minBound and maxBound.

For the purpose of this question, let’s call this dictionary the vtbl for the type class instance. Let me know if this is poor analogy.

My question centers around what kind of code generation can I expect from GHC when I call a type class function. In such cases I see three possibilities:

  1. the vtbl lookup to find the implementation function is down at run time
  2. the vtbl lookup is done at compile time and a direct call to the implementation function is emitted in the generated code
  3. the vtbl lookup is done at compile time and the implementation function is inlined at the call site

I’d like to understand when each of these occur – or if there are other possibilities.

Also, does it matter if the type class was defined in a separately compiled module as opposed to being part of the “main” compilation unit?

In a runnable program it seems that Haskell knows the types of all the functions and expressions in the program. Therefore, when I call a type class function the compiler should know what the vtbl is and exactly which implementation function to call. I would expect the compiler to at least generate a direct call to implementation function. Is this true?

(I say “runnable program” here to distinguish it from compiling a module which you don’t run.)

  • 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-06-12T01:21:09+00:00Added an answer on June 12, 2026 at 1:21 am

    As with all good questions, the answer is “it depends”. The rule of thumb is that there’s a runtime cost to any typeclass-polymorphic code. However, library authors have a lot of flexibility in eliminating this cost with GHC’s rewrite rules, and in particular there is a {-# SPECIALIZE #-} pragma that can automatically create monomorphic versions of polymorphic functions and use them whenever the polymorphic function can be inferred to be used at the monomorphic type. (The price for doing this is library and executable size, I think.)

    You can answer your question for any particular code segment using ghc’s -ddump-simpl flag. For example, here’s a short Haskell file:

    vDouble :: Double
    vDouble = 3
    vInt = length [2..5]
    main = print (vDouble + realToFrac vInt)
    

    Without optimizations, you can see that GHC does the dictionary lookup at runtime:

    Main.main :: GHC.Types.IO ()
    [GblId]
    Main.main =
      System.IO.print
        @ GHC.Types.Double
        GHC.Float.$fShowDouble
        (GHC.Num.+
           @ GHC.Types.Double
           GHC.Float.$fNumDouble
           (GHC.Types.D# 3.0)
           (GHC.Real.realToFrac
              @ GHC.Types.Int
              @ GHC.Types.Double
              GHC.Real.$fRealInt
              GHC.Float.$fFractionalDouble
              (GHC.List.length
                 @ GHC.Integer.Type.Integer
                 (GHC.Enum.enumFromTo
                    @ GHC.Integer.Type.Integer
                    GHC.Enum.$fEnumInteger
                    (__integer 2)
                    (__integer 5)))))
    

    …the relevant bit being realToFrac @Int @Double. At -O2, on the other hand, you can see it did the dictionary lookup statically and inlined the implementation, the result being a single call to int2Double#:

    Main.main2 =
      case GHC.List.$wlen @ GHC.Integer.Type.Integer Main.main3 0
      of ww_a1Oq { __DEFAULT ->
      GHC.Float.$w$sshowSignedFloat
        GHC.Float.$fShowDouble_$sshowFloat
        GHC.Show.shows26
        (GHC.Prim.+## 3.0 (GHC.Prim.int2Double# ww_a1Oq))
        (GHC.Types.[] @ GHC.Types.Char)
      }
    

    It’s also possible for a library author to choose to rewrite the polymorphic function to a call to a monomorphic one but not inline the implementation of the monomorphic one; this means that all of the possibilities you proposed (and more) are possible.

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

Sidebar

Related Questions

In chapter 15 of Real World Haskell, a type class is defined: class (Monad
In Haskell, why would you define a function with a type constraint: ghci> :t
I want to define what seems to require an infinite type. Required : a
The Haskell prelude and Standard Library define a number of useful type classes. Is
Total newbie here, struggling. I'm trying to define a type class for sets. For
I am trying to define a Vector3 data type in Haskell and allow the
Classic way to define Haskell functions is f1 :: String -> Int f1 ('-'
In Haskell I can define following data type: data Tree = Empty | Leaf
In Haskell, lifted type products mean that there's a semantic difference between (a,b,c) and
Does haskell-mode or some alternative package offer something akin to the wonderful inferior-haskell-type inside

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.