[clean-list] translation of Haskell newtype?

John van Groningen johnvg@cs.kun.nl
Wed, 31 Jul 2002 12:34:29 +0200


Brian Rogoff wrote:
>    What's the recommended way to translate code using Haskell's newtype
>into Clean? For those who don't know Haskell, newtype is a performance hack 
>in which a type with a single strict unary constructor specially named by 
>"newtype" is isomorphic to some other type. This could allow us to work around
>the restriction that you can't define instances on type synonyms. Is it
>necessary to worry about this at all, or does the Clean compiler optimize
>this special case? 

The Clean compiler does not optimise this special case (a type with a single strict unary constructor). However the compiler attempts to optimise a record
with a single (strict) field, by unboxing such a record if it appears as a
strict field in (another) record, or as a strict argument of a constructor.
Strict arguments and results of functions are also unboxed for records.

So it is better to translate newtype into a record with a strict field,
instead of a constructor.

If you prefer to use a constructor like syntax, you could use a macro:

NewType a :== {new_type=a}

Regards,

John van Groningen