[clean-list] Re: Synonym types as instances

Bernard James POPE bjpop@cs.mu.OZ.AU
Fri, 24 Jan 2003 23:08:02 +1100 (EST)


> Never mind the precise details here. Then an instance for a concrete
> parser-type should be made. But the only concrete types I can imagine here
> are synonym-types, because parsers are functions. For 'instance':
> 
> :: Parser s r :== [s] -> [(r,[s])]	// the traditional list-of-successes
> 
> My question is: is this a serious disadvantage of Clean compared to Haskell?

If you are talking about Haskell 98 then the language forbids the
instantiation of type classes with synonyms, see section 
4.3.2, of the Haskell 98 Language Report.

Though many implementations relax this restriction.

However, in Haskell 98 there is the "newtype" construct:

   newtype Parser s r = P ([s] -> [(r, [s])])

The constructor P is only present at compile time, it is not present in the
runtime representation of a value of type 'Parser s r'. 

So at runtime you can say that a value of type 'Parser s r' has the same
"representation" as a value of type '[s] -> [(r, [s])]'.

Now it is perfectly fine to instantiate a class with a type declared using
the newtype construct.

I don't remember whether Clean has a similar construct to Haskell 98's newtype
but I would be surprised if it didn't. Perhaps if it is not there it could
be simulated with strictness annotations on ordinary algebraic type
declarations.

Cheers,
Bernie.