Counting Constructors

Gertjan Kamsteeg gkamsteeg@freeler.nl
Wed, 18 Jul 2001 01:53:57 +0200


Unless I misunderstood the problem, the following code will 'almost' do the
(first) job (misusing the first argument of showsPrec):

instance Show a => Show (Bind a) where
  showsPrec n Zero     = showInt n
  showsPrec n (Succ x) = showsPrec (n+1) x

Works fine in Hugs 98 (with -98 as well as with +98). It has a few
shortcomings however:

- If you call show on a term of the form Succ (Succ (... (Succ Zero)...)),
you'll have to provide its type (e.g., Zero :: Bind Int, Succ Zero :: Bind
(Bind Char), etc.). This is a general issue however, similar to trying to
show [].

- If you call show on a term of the form Succ (Succ (... (Succ x)...)), with
x of some non-Bind type, showsPrec n x is called (for some n) rather than
showsPrec 0 x. This shouldn't be much of a problem if you are prepared to
introduce some general wrapper type for x, and define a showsPrec function
for it, independent of its first argument.

Gertjan

----- Original Message -----
From: "Marcin 'Qrczak' Kowalczyk" <qrczak@knm.org.pl>
To: <haskell@haskell.org>
Sent: Tuesday, July 17, 2001 9:13 PM
Subject: Re: Counting Constructors


> Tue, 17 Jul 2001 12:08:51 +0200 (MEST), Tobias Haeberlein
<T.Haeberlein@gmx.de> pisze:
>
> > show (Succ ( ... (Succ Zero)..))  =  n
> > (where n is the number of Succ's)
> > and
> >
> > show (Succ ( ... (Succ x)..)) = show x
> > (when x != Zero)
>
> This is not possible in Haskell 98 - you can't overload an operation on
> "any type except this one". There are no "negative assertions" or
> "exceptions".
>
> It might be possible in Hugs run with -98 option (define "instance
> Count a" and "instance Count a => Count (Bind a)" and "instance Count
> a => Show (Bind a)", where class Count has a function of type a->Int),
> but I would not be surprised if it didn't work correctly in all cases,
> e.g. if the answer was too small sometimes. Overlapped instances is
> not a well behaved concept.
>
> --
>  __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
>  \__/
>   ^^                      SYGNATURA ZASTĘPCZA
> QRCZAK
>
>
> _______________________________________________
> Haskell mailing list
> Haskell@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell
>