[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Two problems
Original-Via: uk.ac.ukc; Sat, 26 Jan 91 17:58:41 GMT
From: Lennart Augustsson <augustss>
Date: Sat, 26 Jan 91 18:52:49 +0100
To: haskell@cs.glasgow.ac.uk
Subject: Two problems
Original-Sender: augustss%se.chalmers.cs@sunic.sunet.se
Sender: haskell-request@cs.glasgow.ac.uk
Two problems:
-------------
I wrote the following little program the other day
instance (Num a) => Num (a,a) where
(a,b) * (c,d) = (a*c+a*d+b*c, a*c+b*d)
fib n = snd ((1,0)^n)
and immediately ran into two problems, one was anticipated
and the other was not.
1. It's not allowed to make an instance declaration in a module
that does not contain the type and/or class declaration of the
involved type and class. Why is this so? Why can't I define
how pairs should be multiplied locally in a module?
This restriction forces me to write
type TPair a b = Pair a b
instance (Num a) => Num (Pair a a) where
(Pair a b) * (Pair c d) = Pair (a*c+a*d+b*c) (a*c+b*d)
psnd (Pair x y) = y
fib n = psnd ((Pair 1 0)^n)
This problem was anticipated (and the restriction has in fact been
removed in Haskell B).
2. My second problem came as a surprise and I don't know what the answer
is. This is the real reason for this posting and I hope that the
knowledgable Haskell commitee can give me the answer.
The typing of "(1,0)" (I will use the builtin pairs for readablility) is
"(1,0) :: (Num a, Num b)=>(a,b)". The instance declaration only says
that "Num a => (a,a)" is an instance. Does this mean that the program has
a type error or should the compiler conclude that the 0 and 1 must have
the same type? The Haskell B compiler complains about a type error.
The Glasgow Haskell compiler (version 0.402) does not complain, but the
executable file it produces causes a core dump.
What's the right behaviour?
-- Lennart Augustsson