Compilation time and time difference between type and new type

What is the difference at different stages of the pipeline with compilation readiness between the type declaration and the newtype declaration?

My assumption was that they were compiled with the same machine instructions and that the only difference was that the program was checked type, where, for example,

 type Name = String newtype Name_ = N String 

You can use Name anywhere a String , but typechecker will call you if you use Name_ , where String is expected, even if they encode the same information.

I ask a question because, if so, I see no reason why the following declarations should not be valid:

 type List a = Either () (a, List a) newtype List_ a = L (Either () (a, List_ a)) 

However, the type of check accepts the second, but rejects the first. Why is this?

+6
source share
1 answer

Luke's comment should be the answer. As a first approximation, a type synonym in Haskell is nothing more than macros. That is, they are expanded by type checking into fully evaluated types. The type checker cannot handle infinite types, so Haskell does not have equirecursive types.

newtypes provide you with isorecursive types, which in GHC essentially boil down to equirecursive types in the main language. Haskell is not the core of GHC, so you do not have access to these types. Equirecursive types are a little more difficult to work with both types and people, while isorecursive types have equivalent power.

+4
source

All Articles