Text.Printf replacement?

Ben Millwood haskell at benmachine.co.uk
Sun Sep 8 02:12:19 CEST 2013

A no-extensions approach would be to duplicate the string trick, with an 
IsUnit class like the IsChar one. However, that's kind of incredibly 
silly, because fromUnit :: () -> a is pretty much the same as hasAValue 
:: a, so you get the least specific class ever.

Plus there already exist classes which have () as a member. How about:

    instance Monoid a => PrintfType (IO a) where ...

with 'return mempty' instead of 'return undefined'?

This is a little bit silly, but lightweight and extensions-free. If you 
think that mentioning Monoid here is a distraction, feel free to 
introduce a new class instead. It would end up being roughly speaking 
the Default class from data-default, which I have gone on record before 
as calling annoying and useless (with, admittedly, a few limited 

Here's another idea: just enable FlexibleInstances and use

    instance PrintfType (IO ()) where ...

FlexibleInstances is pretty stable and really the bottom of the 
hierarchy of class system extensions (perhaps just above 
FlexibleContexts, but certainly TypeFamilies and MPTCs tend to sort of 
assume you've got it enabled already). I don't know if there are any 
inference issues that I'm missing here.

On Fri, Sep 06, 2013 at 10:56:55AM -0700, Bart Massey wrote:
>TypeFamilies are kind of heavy machinery here :-), but since it would
>be confined to the library and seems to solve the problem, I'd be in
>favor of doing as you suggest. We can conditionally compile if we are
>worried about non-GHC implementations, I think. Do other folks have an
>opinion? --Bart
>On Fri, Sep 6, 2013 at 8:18 AM, Twan van Laarhoven <twanvl at gmail.com> wrote:
>> On 06/09/13 04:16, Bart Massey wrote:
>>> A final issue has to do with the return type of Text.Printf.printf,
>>> which is polymorphic between String and IO a. I'm sure this seemed
>>> like a good idea at the time, but it's not so ideal today: GHC gives a
>>> warning when printf is used at IO a unless you explicitly ignore the
>>> result.
>> You can probably force the `a` to be `()` by using:
>>     instance (a ~ ()) => PrintfType (IO a) where ...
>> But that is of course not standard Haskell.
>> Twan
>> _______________________________________________
>> Libraries mailing list
>> Libraries at haskell.org
>> http://www.haskell.org/mailman/listinfo/libraries
>Libraries mailing list
>Libraries at haskell.org

More information about the Libraries mailing list