instance overaps with lib

Ross Paterson ross at soi.city.ac.uk
Tue Sep 5 07:11:20 EDT 2006


On Tue, Sep 05, 2006 at 12:50:39PM +0400, Serge D. Mechveliani wrote:
> More about instance overlaps with the GHC library:
> 
> I need to print in a special way the data of
> [Equation], (Term, Term), [(Term, Term)], (Equation, Equation).
> 
> The first can be by defining  showList  in  instance Show Equation.
> But  Show  has not a method of  showPair. So, I need to write the 
> function  showsTermPair  and to use it together with another home-made 
> function  showsListGeneric.

You seem to be using special instances to do two things: add spacing and
remove extraneous parentheses.  A neater way to do the latter would be
to use the precedence parameter of showsPrec for Term to control whether
the parentheses are added in the first place, e.g. (untested)

  showsPrec p t = 
    (case t 
     of
     VarT v         -> shows v
     App f []  []   -> showString (name f)
     App f []  [r]  -> showParen (p > app_prec) $
                           showString (name f) . showChar ' ' .
                           showsPrec (app_prec+1) r
     ...
    where app_prec = 10



More information about the Glasgow-haskell-users mailing list