Haskell novice question - deriving from Show 
Author Message
 Haskell novice question - deriving from Show

I am trying to make my set data structure class inherit from Show, and
define the default showsPrec in terms of showsPrec on the elements.
Like this:

   showsPrec :: Show a => Int -> s a -> ShowS ;
   showsPrec n xs str = let {
     join [] sep str = ('}' : str);
     join [x] sep str = showsPrec (n+1) x ('}' : str);

       showsPrec (n+1) x1 (sep ++ join xs sep str)
   } in '{' : join (to_list xs) ", " str ;

but Hugs says

Prelude> Reading file "/home/itz/hds/Set.hs":
ERROR /home/itz/hds/Set.hs:125 - Inferred type is not general enough
*** Expression    : showsPrec
*** Expected type : (Set a b, Show b) => Int -> a b -> ShowS
*** Inferred type : (Set a (b c), Show (b c)) => Int -> a (b c) -> ShowS

Why?  Why is it inferring (b c) type for x in the declaration of join
(apparently)?

--
Ian Zimmerman, Oakland, California, U.S.A.
GPG: 433BA087  9C0F 194F 203A 63F7 B1B8  6E5A 8CA3 27DB 433B A087
EngSoc adopts market economy: cheap is wasteful, efficient is expensive.



Sat, 08 Jan 2005 14:17:44 GMT  
 Haskell novice question - deriving from Show

Quote:

>    showsPrec :: Show a => Int -> s a -> ShowS ;
>    showsPrec n xs str = let {
>      join [] sep str = ('}' : str);
>      join [x] sep str = showsPrec (n+1) x ('}' : str);

>        showsPrec (n+1) x1 (sep ++ join xs sep str)
>    } in '{' : join (to_list xs) ", " str ;

> but Hugs says

> Prelude> Reading file "/home/itz/hds/Set.hs":
> ERROR /home/itz/hds/Set.hs:125 - Inferred type is not general enough
> *** Expression    : showsPrec
> *** Expected type : (Set a b, Show b) => Int -> a b -> ShowS
> *** Inferred type : (Set a (b c), Show (b c)) => Int -> a (b c) -> ShowS

> Why?  Why is it inferring (b c) type for x in the declaration of join
> (apparently)?

Ok, so you wrote type (s a) for the parameter xs of showsPrec, but Hugs
infers the type (a (b c)).  The only criteria Hugs has to make this
decision is the usage of xs in the body of the definition of showsPrec.
It appears that xs is used only once, in the expression (to_list xs).
You have not given us the definition of the function to_list, but Hugs
is telling us that the type to_list expects for its argument is (a (b c)).
Perhaps that is not correct, and therefore the fault is in the
definition of to_list.  Or perhaps to_list is defined correctly, but you
have called it with the wrong argument value?

Regards,
    Malcolm



Sun, 09 Jan 2005 01:16:58 GMT  
 Haskell novice question - deriving from Show

Quote:

> > showsPrec :: Show a => Int -> s a -> ShowS ;
> > showsPrec n xs str = let {
> > join [] sep str = ('}' : str);
> > join [x] sep str = showsPrec (n+1) x ('}' : str);

> > showsPrec (n+1) x1 (sep ++ join xs sep str)
> > } in '{' : join (to_list xs) ", " str ;

> > but Hugs says

> > Prelude> Reading file "/home/itz/hds/Set.hs":
> > ERROR /home/itz/hds/Set.hs:125 - Inferred type is not general enough
> > *** Expression    : showsPrec
> > *** Expected type : (Set a b, Show b) => Int -> a b -> ShowS
> > *** Inferred type : (Set a (b c), Show (b c)) => Int -> a (b c) -> ShowS

> Here's all the module says about to_list:

>    to_list :: s a -> [a] ;

OK, thanks for the extra info.  So now we know that the first parameter
of the auxiliary function 'join' should be of type [a].  However, I see that
in the last clause of 'join', you have a recursive call to showsPrec, where
the parameter x1 (which must be of type a) is passed directly to showsPrec in
a position where it needs to be an (s a).  Hence the error.  This type is
propagated outwards, such that join must take a list of (s a), i.e. [s a],
and so to_list is expected to be instantiated with the signature
     to_list :: s (s a) -> [s a] ;
and so on.

Regards,
    Malcolm



Tue, 11 Jan 2005 20:32:49 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. novice Haskell question

2. Questions on derived Text class instances of Haskell

3. pop-up dialog box showing module sub/fun or derived type contents

4. Haskell: Why is Ord derived from Eq?

5. Haskell's deriving for Clean

6. Haskell: format of Show Real

7. Haskell - instance declaration for Show.

8. Novice RETRACT question (now SETOF/BAGOF question)

9. Derived types with derived types with allocatables

10. Derived-type subcomponents of derived type

11. Some threading and COM question from a Dolphin novice

12. Novice J Question - Names/Nouns

 

 
Powered by phpBB® Forum Software