liboleg-2010.1.6: An evolving collection of Oleg Kiselyov's Haskell modules



Generic polyvariadic printf in Haskell98

This generalization of Text.Printf.printf is inspired by the message of Evan Klitzke, who wrote on Haskell-Cafe about frequent occurrences in his code of the lines like

        infoM $ printf "%s saw %s with %s" (show x) (show y) (show z)

Writing show on and on quickly becomes tiresome. It turns out, we can avoid these repeating show, still conforming to Haskell98.

Our polyvariadic generic printf is like polyvariadic show with the printf-like format string. Our printf handles values of any present and future type for which there is a Show instance. For example:

        t1 = unR $ printf "Hi there"
        -- "Hi there"
        t2 = unR $ printf "Hi %s!" "there"
        -- "Hi there!"
        t3 = unR $ printf "The value of %s is %s" "x" 3
        -- "The value of x is 3"
        t4 = unR $ printf "The value of %s is %s" "x" [5]
        -- "The value of x is [5]"

The unsightly unR appears solely for Haskell98 compatibility: flexible instances remove the need for it. On the other hand, Evan Klitzke's code post-processes the result of formatting with infoM, which can subsume unR.

A bigger problem with our generic printf, shared with the original Text.Printf.printf, is partiality: The errors like passing too many or too few arguments to printf are caught only at run-time. We can certainly do better.

Version: The current version is 1.1, June 5, 2009.




newtype RString Source

Needed only for the sake of Haskell98 If we are OK with flexible instances, this newtype can be disposed of




unR :: String


SPrintF RString

These two instances are all we ever need

class SPrintF r whereSource


pr_aux :: [FDesc] -> [String] -> rSource


SPrintF RString

These two instances are all we ever need

(Show a, SPrintF r) => SPrintF (a -> r) 

data FDesc Source

A very simple language of format descriptors


FD_lit String