synonym type not allowed

Arjan van IJzendoorn arjanij@cs.kun.nl
Wed, 17 Dec 1997 16:08:13 +0100


Hello again,

Stefan wrote:

>You may regard instances of type synonyms as an instance of the basic
>type, then you don't have the problem that the compiler cannot determine
>whether the expression (3, 4.0) is of type (Int, Real) or of your
>MyType. You always use the instances of type (Int, Real).

And then Erik wrote:

>That is simple. That brings down the word synonym to what the word actually
>means: a different word for exactly the same thing. I cannot immagine that
>language developers have never thought of this. A synonym type would be
>reduced to a syntactic issue. What do we lose that way?

You're right. Language developers did think of this. However,
the problems remain. Let me clarify my statement with an example:

///////////

:: Complex :== (Int, Int)
:: Ratio   :== (Int, Int)

instance == Complex where
  (==) (a, b) (c, d) = a == c && b == d

instance == Ratio where
  (==) (a, b) (c, d) = a * d == b * c

Start = (3, 4) == (6, 8)

///////////

Need I say more?
Probably not, but I will anyway.

There is yet another problem. Overlapping instances are not allowed
in Clean. If you import StdTuple (included in StdEnv),
an instance for tuple equality is already defined.
You can not overrule this general equality for
a two-tuple of Ints.

Finally, a question. Is a tuple the right "data structure" for the problem.
I don't know what the tuple of an integer and a real stands for, but
in the above cases a record is more appropriate:

:: Complex
   = { re :: Int  // the real part
     , im :: Int  // the imaginary part
     }

:: Ratio
   = { num :: Int // numerator
     , den :: Int // denominator
     }

Clean has very nice notations for performing pattern-matches on
records and for building them. And records can be made instance
of classes...

Tuples are not really structured and IMHO you should only use them
to return several function results. And thus not for describing
the structure of the data you're manipulating.

Greetings,
  Arjan


----
"..they hung in the sky in much the same way as bricks don't."
                                         -- Douglas Adams --