Text.Printf replacement?

Edward Kmett ekmett at gmail.com
Sun Sep 8 08:01:53 CEST 2013


If you are using TypeFamilies then

instance (a ~ ()) => PrintfType (IO a)

would be the way to go.

No silliness with defaulting, no strange classes, no inference woes and it has exactly the right connotation. Once the compiler figures out the outside is IO, then it can assume the argument is ().

On Sep 7, 2013, at 11:01 PM, Bart Massey <bart at cs.pdx.edu> wrote:

> I already tried FlexibleInstances and the IsUnit typeclass trick and
> could get neither to work. AFAICT, both require cooperation from the
> caller. I may have just done it wrong, though. I have absolutely no
> problem with an IsUnit typeclass if that's a clean solution to the
> problem somehow. --Bart
> 
> On Sat, Sep 7, 2013 at 5:12 PM, Ben Millwood <haskell at benmachine.co.uk> wrote:
>> 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 applications).
>> 
>> 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
>>> http://www.haskell.org/mailman/listinfo/libraries
> 
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries




More information about the Libraries mailing list