[Haskell-cafe] A type class puzzle

Greg Buchholz haskell at sleepingsquirrel.org
Tue Oct 31 12:02:53 EST 2006


Yitzchak Gale wrote:
> Tomasz Zielonka wrote:
> >If you insist that each index should be given as a separate
> >function argument, it may be possible to achieve it using the tricks
> >that allow to write the variadic composition operator.
> 
> I am not familiar with that. Do you have a reference?
> Is that the best way to do it? (Is that a way to do it at all?)

  You might find these articles somewhat related...

    Functions with the variable number of (variously typed) arguments
    http://okmij.org/ftp/Haskell/types.html#polyvar-fn

    Deepest functor [was: fmap for lists of lists of lists of ...]
    http://okmij.org/ftp/Haskell/deepest-functor.lhs

...That first article is the strangest.  I couldn't reconcile the fact
that if our type signature specifies two arguments, we can pattern
match on three arguments in the function definition.  Compare the number
of arguments in the first and second instances...

> class BuildList a r  | r-> a where
>     build' :: [a] -> a -> r
>
> instance BuildList a [a] where
>     build' l x = reverse$ x:l
>
> instance BuildList a r => BuildList a (a->r) where
>     build' l x y = build'(x:l) y

...if you try something like...

foo :: [a] -> a -> r
foo l x y = undefined

...you'll get an error message like...

    The equation(s) for `foo' have three arguments,
    but its type `[a] -> a -> r' has only two


YMMV,

Greg Buchholz



More information about the Haskell-Cafe mailing list