Created
June 17, 2013 01:41
-
-
Save mjdominus/5794190 to your computer and use it in GitHub Desktop.
Scale class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I want to define a type class that supports a scaling operation. This | |
includes polynomials, and various decorated sorts of polynomials. | |
I tried | |
class Num s => Scale s a where | |
scale :: s -> a -> a | |
A (Scale s a) constraint means that type a can be scaled using a | |
scalar of type s. So for example: | |
instance Num a => Scale a a where | |
scale = (*) | |
This doesn't work: | |
*Scale> scale 3 4 | |
<interactive>:56:1: | |
Ambiguous type variables `s0', `a0' in the constraint: | |
(Scale s0 a0) arising from a use of `scale' | |
Probable fix: add a type signature that fixes these type variable(s) | |
In the expression: scale 3 4 | |
In an equation for `it': it = scale 3 4 | |
<interactive>:56:7: | |
Ambiguous type variable `s0' in the constraint: | |
(Num s0) arising from the literal `3' | |
Probable fix: add a type signature that fixes these type variable(s) | |
In the first argument of `scale', namely `3' | |
In the expression: scale 3 4 | |
In an equation for `it': it = scale 3 4 | |
<interactive>:56:9: | |
Ambiguous type variable `a0' in the constraint: | |
(Num a0) arising from the literal `4' | |
Probable fix: add a type signature that fixes these type variable(s) | |
In the second argument of `scale', namely `4' | |
In the expression: scale 3 4 | |
In an equation for `it': it = scale 3 4 | |
It didn't work for my original purpose either: | |
-- Multiply a polynomial by c | |
instance Num c => Scale c (Poly c) where | |
scale c = fmap (* c) | |
poly_sub a b = poly_add a (scale (-1) b) | |
/home/mjd/src/haskell/min-poly/Polynomial.hs:42:28: | |
Could not deduce (Scale s0 (Poly a)) arising from a use of `scale' | |
from the context (Eq a, Num a, Scale a (Poly a)) | |
bound by the type signature for | |
poly_sub :: (Eq a, Num a, Scale a (Poly a)) => | |
Poly a -> Poly a -> Poly a | |
at /home/mjd/src/haskell/min-poly/Polynomial.hs:42:1-40 | |
Possible fix: | |
add (Scale s0 (Poly a)) to the context of | |
the type signature for | |
poly_sub :: (Eq a, Num a, Scale a (Poly a)) => | |
Poly a -> Poly a -> Poly a | |
or add an instance declaration for (Scale s0 (Poly a)) | |
In the second argument of `poly_add', namely `(scale (- 1) b)' | |
In the expression: poly_add a (scale (- 1) b) | |
In an equation for `poly_sub': | |
poly_sub a b = poly_add a (scale (- 1) b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment