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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:46:30+00:00 2026-05-26T06:46:30+00:00

Haskell’s type safety is second to none only to dependently-typed languages. But there is

  • 0

Haskell’s type safety is second to none only to dependently-typed languages. But there is some deep magic going on with Text.Printf that seems rather type-wonky.

> printf "%d\n" 3
3
> printf "%s %f %d" "foo" 3.3 3
foo 3.3 3

What is the deep magic behind this? How can the Text.Printf.printf function take variadic arguments like this?

What is the general technique used to allow for variadic arguments in Haskell, and how does it work?

(Side note: some type safety is apparently lost when using this technique.)

> :t printf "%d\n" "foo"
printf "%d\n" "foo" :: (PrintfType ([Char] -> t)) => t
  • 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-26T06:46:30+00:00Added an answer on May 26, 2026 at 6:46 am

    The trick is to use type classes. In the case of printf, the key is the PrintfType type class. It does not expose any methods, but the important part is in the types anyway.

    class PrintfType r
    printf :: PrintfType r => String -> r
    

    So printf has an overloaded return type. In the trivial case, we have no extra arguments, so we need to be able to instantiate r to IO (). For this, we have the instance

    instance PrintfType (IO ())
    

    Next, in order to support a variable number of arguments, we need to use recursion at the instance level. In particular we need an instance so that if r is a PrintfType, a function type x -> r is also a PrintfType.

    -- instance PrintfType r => PrintfType (x -> r)
    

    Of course, we only want to support arguments which can actually be formatted. That’s where the second type class PrintfArg comes in. So the actual instance is

    instance (PrintfArg x, PrintfType r) => PrintfType (x -> r)
    

    Here’s a simplified version which takes any number of arguments in the Show class and just prints them:

    {-# LANGUAGE FlexibleInstances #-}
    
    foo :: FooType a => a
    foo = bar (return ())
    
    class FooType a where
        bar :: IO () -> a
    
    instance FooType (IO ()) where
        bar = id
    
    instance (Show x, FooType r) => FooType (x -> r) where
        bar s x = bar (s >> print x)
    

    Here, bar takes an IO action which is built up recursively until there are no more arguments, at which point we simply execute it.

    *Main> foo 3 :: IO ()
    3
    *Main> foo 3 "hello" :: IO ()
    3
    "hello"
    *Main> foo 3 "hello" True :: IO ()
    3
    "hello"
    True
    

    QuickCheck also uses the same technique, where the Testable class has an instance for the base case Bool, and a recursive one for functions which take arguments in the Arbitrary class.

    class Testable a
    instance Testable Bool
    instance (Arbitrary x, Testable r) => Testable (x -> r) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

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
since Haskell has such expressive type system, is there something supported directly that we
With Haskell's type classes it almost seems that it enables ad hoc polymorphism, but
In Haskell, there is a function take n list which returns the first n
In Haskell function type ( -> ) is given, it's not an algebraic data
Haskell beginner here. I want to pass a type parameter with JSON, and have
In Haskell, why would you define a function with a type constraint: ghci> :t
In Haskell, I use the Data.Map module, and its principal type of the same
In Haskell, Is there a standard library/package for generating Random / Arbitrary enums? I

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.