I've only recently started dabbling in Haskell, and functional
programming in general, and am having a few problems, one particular to
Haskell as a functional language, the other with Haskell's type classes. Just
in case it matters, I'm using "interactive Haskell B. version 0.999.7 SPARC
1994 Oct 17". Let's start with the simpler problem:
1) let fred x = x in fred fred;
Haskell complains:
[55] Ambiguously overloaded tyvar(s) in class(es) Text in show (let {
fred = \A1_fred -> A1_fred
} in fred fred)
while the equivalent ML statement (let fun fred x = x in fred fred end;)
returns a function of type ('a -> 'a) or simply (a -> a) in Haskell-speak.
Why can I not generate this definition?
2) I'm attempting to add another instance to the Num class as a test of
Haskell's OOP functionality. Thus, I create a new type which is a 3-tuple:
data ThreeSpace = TS (Integer, Integer, Integer);
Then, for each of the methods required by Num, a function is defined for the
new type. Warning, I don't know what 'signum' should do, so I return its
input.
let addTS (TS (x1, x2, x3)) (TS (y1, y2, y3)) = TS ((x1+y1), (x2+y2), (x3+y3));;
let subTS (TS (x1, x2, x3)) (TS (y1, y2, y3)) = TS ((x1-y1), (x2-y2), (x3-y3));;
let mulTS (TS (x1, x2, x3)) (TS (y1, y2, y3)) = TS ((x1*y1), (x2*y2), (x3*y3));;
let negTS (TS (x1, x2, x3)) = TS ((x1-x1-x1), (x2-x2-x2), (x3-x3-x3));;
let absTS (TS (x1, x2, x3)) = TS ((abs x1), (abs x2), (abs x3));;
let signumTS (TS (x1, x2, x3)) = TS (x1, x2, x3);;
let fIntegerTS x = TS ((fromInteger x), 0, 0);;
let fIntTS :: Int -> ThreeSpace; fIntTS x = TS ((fromInt x), 0, 0);;
Now, I need to add the ThreeSpace instance to Num:
instance Num ThreeSpace where
(+) = addTS
(-) = subTS
(*) = mulTS
negate = negTS
abs = absTS
signum = signumTS
fromInteger = fIntegerTS
fromInt = fIntTS
;
This is where things crap out. I get the following error:
[56] Not an instance Num ThreeSpace in ((DD.Num.$fromInteger{FARITY 1}))::(Integer,
Int, Double) -> ThreeSpace
in $fromInteger
I haven't the faintest clue what this error message means, particularly the
"(Integer, Int, Double) -> ThreeSpace" which looks like a function which takes
a 3-tuple and returns a 'ThreeSpace'. "whatis fromInteger" tells me:
method PreludeCore.fromInteger :: (Num a) => Integer -> a
so I know my definition typechecks. I've tried everything I could think of to
get this going, but to no avail. Help!
No light at the end of the tunnel,
Big Al the Devil's Pal!!
__ __
/ `-' / ,,
|[====|||||||||||[::}
\__.-._\ ``