[Haskell-cafe] Question about classes from a Haskell newbie.
Jeff.Harper at handheld.com
Jeff.Harper at handheld.com
Tue May 24 15:52:00 EDT 2005
Thank you Pierre and Lemmih for answering my question about how to
implement the toComplex function. Both of your answers were insightful.
I understood Pierre's solution.
Pierre wrote:
> I think you misunderstand the meaning of the constraint !
>
> toComplex :: (RealFloat b) => a -> Complex b
>
> means the type of "b" needs to be a "RealFloat" and that it will be
> inferred from the context where the function "toComplex" is called !
"Inferring the type of a function based on the context where the function
is called." is an idea that I'm going to have to wrap my mind around.
Lemmih's solution is interesting too. I tried it. It seems to work well
under Hugs. I'm under the impression that the return type of toComplex is
inferred based on the argument passed to toComplex. However, I didn't
understand the syntax of the class declaration.
Lemmih wrote:
> import Complex
>
> class ConvertibleToComplex a b | a -> b where
> toComplex :: RealFloat b => a -> Complex b
I've looked at several sources to try to understand this declaration. I
can't find any examples where a class declaration takes two type variables
or uses the pipe symbol. One of the sources I used was _Haskell 98
Language and Libraries The Revised Report_. I'm not an expert at BNF
notation, but the definitions for topdecl, tycls, and tyvar in the grammar
seems to exclude Lemmih's declaration.
If someone could point me to some sources that explain this notation, I'd
be very grateful.
Thanks,
Jeff
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org//pipermail/haskell-cafe/attachments/20050524/7c138072/attachment-0001.htm
More information about the Haskell-Cafe
mailing list