<div dir="ltr">hrm, so youre wanting something even smarter than the MINIMAL pragma stuff, namely <br>"depending on which subset of the complementary methods are defined, define this method differently"?</div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Oct 4, 2014 at 11:44 AM, Andreas Abel <span dir="ltr"><<a href="mailto:abela@chalmers.se" target="_blank">abela@chalmers.se</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Consider the following class for an overloaded pretty printer.  For<br>
atomic data (numeric types, String etc.) one would implement<br>
prettyShow, for complex data either pretty or prettyPrec.<br>
<br>
> import Text.PrettyPrint<br>
><br>
> class Pretty a where<br>
>   pretty      :: a -> Doc<br>
>   prettyShow  :: a -> String<br>
>   prettyPrec  :: Int -> a -> Doc<br>
<br>
Implementing one of these methods should be enough, giving default<br>
implementations for the other two.<br>
<br>
It is easy to get prettyShow and prettyPrec from pretty.<br>
<br>
>   prettyShow = render . pretty<br>
>   prettyPrec = const . pretty<br>
<br>
However, to define pretty from one of the others, I need two default<br>
implementations.<br>
<br>
>   pretty = text . prettyShow<br>
>   pretty = prettyPrec 0<br>
<br>
Is there a way to get this to work?<br>
<br>
Workarounds (not entirely satisfactory): Technically, one could define<br>
a cycle of default implementations.  Alternative 1:<br>
<br>
>   pretty       = prettyPrec 0<br>
>   prettyShow   = render . pretty<br>
>   prettyPrec _ = text . prettyShow<br>
<br>
Problem: Here, if pretty is defined,<br>
<br>
>   prettyPrec _ = text . render . pretty<br>
<br>
instead of just<br>
<br>
>   prettyPrec _ = pretty<br>
<br>
and (text . render) is not the identity (destroys inner document structure).<br>
<br>
Alternative 2:<br>
<br>
>    pretty       = text . prettyShow<br>
>    prettyShow   = render . prettyPrec 0<br>
>    prettyPrec _ = pretty<br>
<br>
Problem: Here, if prettyPrec is defined,<br>
<br>
>    pretty = text . render . prettyPrec 0<br>
<br>
instead of just<br>
<br>
>    pretty = prettyPrec 0<br>
<br>
I guess alternative 2 is worse than alternative 1, as one would<br>
usually define prettyPrec to get pretty, and not the otherway round.<br>
But none of these two alternatives really does the job.<span class="HOEnZb"><font color="#888888"><br>
<br>
-- <br>
Andreas Abel  <><      Du bist der geliebte Mensch.<br>
<br>
Department of Computer Science and Engineering<br>
Chalmers and Gothenburg University, Sweden<br>
<br>
<a href="mailto:andreas.abel@gu.se" target="_blank">andreas.abel@gu.se</a><br>
<a href="http://www2.tcs.ifi.lmu.de/~abel/" target="_blank">http://www2.tcs.ifi.lmu.de/~<u></u>abel/</a><br>
______________________________<u></u>_________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/haskell-cafe</a><br>
</font></span></blockquote></div><br></div>