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.
Ben writes: : | (@@) :: (Num a) => Polynomial a -> a -> a : | 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? Your definition of (@@) looks OK. The only problem is that you've applied it to two arguments of the same type (f, g :: Polynomial Integer), which don't unify with the parameter types (Polynomial a and a). Hugs has compared the type of the second parameter (a) with the type of the second argument (Polynomial Integer), and applied the Most General Unifier (the substitution of Polynomial Integer for a). The details of the error message refer to what happened when it compared the types of the first argument and the first parameter. It's worth checking whether [ f @@ g | f <- poly, g <- poly, f /= g ] really means what you intended.
The following appears to work, (@\@) :: (Num a) => Polynomial a -> Polynomial a -> Polynomial a f @\@ g = applyrec pp g 0 where pp = map (\x -> toPolynomial [x]) (fromPolynomial f) applyrec (f:fs) g m = f * g^m + applyrec fs g (m+1) applyrec [] _ _ = 0 Sorry for the original email, although I would be interested in a better solution if there is any commentary. Ben. On Sun, May 13, 2001 at 09:43:31PM -0500, Ben wrote:
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. ...etc.
participants (2)
-
Ben -
Tom Pledger