> The ghc compiler complains about 2 type errors in the following code:
> 
> > data SearchResult a = Found a | Fail
> >
> > class (Eq key, Ord key) => Dictionary dict key dat where
> >      delete :: key -> dict -> dict
> >      search :: key -> dict -> (key,SearchResult dat,dict)
> >      searchList :: [key] -> dict -> ([(key,SearchResult dat)],dict)
> >
> >      searchList [] d = ([],d)
> >      searchList (x:xs) d = let (sresults,d') = searchList xs d
> >                                (x',sresult,d'') = search x d'
> >                                new_sres = (x',sresult):sresults
> >                            in (new_sres,d'')
> 
> the first error:
> 
>     Class type variable `dat' does not appear in method signature
>         delete :: key -> dict -> dict
> 
> Why does ghc expect that I use all of the type variables?
> Obviously I only need
> the key to remove an entry of a dictionary.

You're right.  The restriction is excessive.  Thanks for pointing
this out.  Probably we should only require that at least one
of the class variables is constrained.

Actually I think you would be better off with a class like 
this:

  class (Eq key, Ord key) => Dictionary dict key where
     delete :: key -> dict dat -> dict dat
     search :: key -> dict dat -> (key, SearchResult dat, dict dat)
     searchList :: [key] -> dict dat -> ([(key,SearchResult dat)],dict dat)

That is, you don't need the 'dat' type variable the class at all.

>     Ambiguous type variable(s)
>     `key', `dict'
>         in the constraint `Dictionary dict key a10v'
>         arising from use of `searchList' at Dtest2.hs:11
>     In an equation for function `searchList':
>         searchList (x : xs) d
>                    = let
>                        (sresults, d') = searchList xs d
>                        (x', sresult, d'') = search x d'
>                        new_sres = (x', (sresult)) : sresults
>                      in (new_sres, (d''))
>     In the definition for method `searchList'

You don't say which version of the compiler you are using,
but I think this a palpable bug in 3.01 that is fixed in 3.02.
So you are right to be confused by it!

Simon


Reply via email to