typing question

Ben Ben <neb@one.net>
Sun, 13 May 2001 21:43:31 -0500


Hello, I hope that a question here wont be too much of a bother.

Yesterday I wrote a library for polynomial manipulation. Everything is fine
except for the composition operator. Which I having problems with, probably
trivial, but as I don't program in Haskell everyday a solution isn't clear.

I have my polynomials defined as:
newtype Polynomial a = Polynomial [a] deriving (Eq)

with
toPolynomial                  :: (Num a) => [a] -> Polynomial a
toPolynomial x                = Polynomial x
fromPolynomial                :: Polynomial a -> [a]
fromPolynomial (Polynomial x) = x

instance Show a => Show (Polynomial a) where
instance Num a => Num (Polynomial a) where... and defined appropriately

I also have a function, degree :: Polynomial a -> Int but to be more pointed
my question is related to,

(@@)   :: (Num a) => Polynomial a -> a -> a
f @@ g = applyrec (fromPolynomial f) g 0
   where
	applyrec (f:fs) g m = f * g^m + applyrec fs g (m+1)
	applyrec  []    _ _ = 0

This was originally written to evaluate f at value, ie n \mapsto f(n).

But I realized that this should also work for overloading, as my Polynomials
are Nums. In hugs the following works fine,

t = Polynomial [1,2,3] @@ Polynomial [3,2,1]

but I suspect that there is a type conversion happening for me which I should
be aware of as something like the following doesn't work,
[ f @@ g | f <- poly, g <- poly, f /= g ]
where poly is a list of polynomials gives me,

*** Expression     : f @@ g
*** Term           : f
*** Type           : Polynomial Integer
*** Does not match : Polynomial (Polynomial Integer)

so my question boils down to, how do I tell hugs/Haskell that Polynomials
over Polynomials of Integers are just Polynomials of Integers or at least
that this kind of composition is ok?

I am not on this mailing list so please CC me any responses. Thank you.

Ben.