r/HaskellBook•
etatoby

Why does newtype exist?

I understand what newtype does: it's a way to add compile-time differences and/or restrictions in the way a datatype is treated, including what instances it has and how they behave, without adding any run-time penalty.

Therefore, as far as I can tell, newtype is just an optimization that you may choose to apply (instead of just using data) when defining a datatype with a single constructor that takes on a single (strict?) argument.

What I'm wondering is why this optimization is exposed in the language in the first place. Should it not be something that GHC decides to do on its own, whenever it sees a data declaration with a single constructor etc.?

What semantic purpose does it serve, to be able to choose between data and newtype?

What practical purpose does it serve, to have GHC not apply this optimization automatically whenever possible?

Posted Jun 17, 2017, 10:47 AM UTC

41
Loading comments…