Eileen Head <[EMAIL PROTECTED]> wrote
> Is there an easy way to define a new class, X, of types which is a
> class of some range of types in an existing class Y.
> [..]
> For example can you define a class PosNum in which the type in the
> class are positive Ints, positive Integers, positive Floats and
> positive Doubles?
If I am not misleading the direction, it is NOT possible.
Because such a class presumes to have the _instances_ which are the
proper subsets. For example, one should be able at least to define
the type of positive integers, with the appropriate instances.
Positive Integer is a subdomain of Integer. And the mathematical
constructors to create a new domain are much more powerful than the
type constructors of Haskell.
In Haskell a new type is created only by applying constructor to
the parameter types. Say,
C Integer, (Integer, [Integer]).
But it takes to a new type only the _whole_ set of values of the
parameter types, one cannot make it a proper subset.
For example, the compiler would recognize in the above example of
C Integer that ` C 'b' ' does not denote a correct value, and
that `C 2' is correct. But it considers also `C (-2)'
as correct, and (-2) is not of Positive integer.
Hence, to define a subdomain is possible, maybe, only in some much
weaker sense.
For example, one may introduce data PosInt = PosI Integer,
write an application which tests n > 0 each time it sees any
PosI n. One could define +,-,negate ... instances for it, so that
negate (PolI n) --> error ...
This models positive integers to some extent, but this domain check
is not supported by the Language, by compiler.
------------------
Sergey Mechveliani
[EMAIL PROTECTED]