Thu, 3 Feb 2000 14:24:28 +1300 (NZD), Tom Pledger <[EMAIL PROTECTED]> pisze:

>     instance (Bounded a, Enum a) => Random a where ...

> But, every type in (Bounded, Enum) has a potential instance of Random,
> using no properties of the type except its Bounded and Enum functions.

Potential. But this does not need to be *the* implementation for
all types (e.g. Doubles).

So you want overlapping instances, with some rules of disambiguation?
They cause troubles.

E.g. the context (Bounded a, Enum a, Random a) could be reduced do
(Bounded a, Enum a) (similarly as (Eq a, Eq [a]) can be reduced to
Eq a), but then if such overloaded function is instantiated to Double,
the wrong Random instance is chosen.

And when different instances are visible in various modules, the same
type can use different Random instances in those modules - without
violation of the single instance rule. All instances themselves are
legal, and the fact that one was not visible didn't cause compile
time error in any module but is still a not so obvious error.

GHC has a switch -fallow-overlapping-instances. But they are not
very safe.

If this was the implementation for all types, there would be no Random
class, only normal functions with the context (Bounded a, Enum a).

The best that can be done is a default implementation as external
functions:
    defaultRandom  :: (Bounded a, Enum a, RandonGen g) => g -> (a,g)
    defaultRandomR :: (Bounded a, Enum a, RandonGen g) => (a,a) -> g -> (a,g)
so it's easy to write instances that use them. Such thing is used in
the Edison library.

------------------------------------------------------------------------

Generally it is dangerous to allow anything to depend on *absence*
of something, except when the presence would be an error (e.g. there
would be some detected collision).

Another instance of this problem are SML constructor and variable
names, which are not syntactically distinguished as in Haskell.
To write a variable in a pattern, you must know that the name was
not defined as a constructor elsewhere, but you can't ensure that
and violation will not necessarily cause error.

-- 
 __("<    Marcin Kowalczyk * [EMAIL PROTECTED] http://qrczak.ids.net.pl/
 \__/              GCS/M d- s+:-- a22 C+++$ UL++>++++$ P+++ L++>++++$ E-
  ^^                  W++ N+++ o? K? w(---) O? M- V? PS-- PE++ Y? PGP+ t
QRCZAK                  5? X- R tv-- b+>++ DI D- G+ e>++++ h! r--%>++ y-

Reply via email to