Reputation: 2011
I am learning about phantom types in Haskell. I know that phantom types are parameterised types whose parameters do not appear on the right hand side of its definition.
However, I would like to know whether all types declared with newtype
are phantom types.
When I try to compile a program with the following code:
newtype SpecialInt Int = Special Int
I get an error stating:
Unexpected type ‘Int’
In the newtype declaration for ‘SpecialInt’
A newtype declaration should have form
newtype SpecialInt a = ...
Which leads me to believe that newtypes are phantom types if the type parameter on the left hand side doesn't appear on the right hand side. E.g.
newtype SpecialInt a = Special a
Would not be a phantom type, but
newtype SpecialInt a = Special Int
would be a phantom type.
Therefore not all types declared with newtype
are phantom types. However, I am not sure if my reasoning is correct.
Upvotes: 1
Views: 232
Reputation: 476574
Which leads me to believe that newtypes are phantom types if the type parameter on the left hand side doesn't appear on the right hand side. E.g.
Well this is a consequence of your quoted definitions. You say that:
I know that phantom types are parameterised types whose parameters do not appear on the right hand side of its definition.
Since newtype
is a way to define a type, and since for parameterized types with parameters that do not appear at the right side are phantom types, types originating from a newtype
clause with a type parameter that do not appear at the right side are phantom types as well.
I would like to know whether all types declared with newtype are phantom types.
No, since for example a newtype
without any type parameter, or a newtype
with type parameters where are type parameters are used at the right side are not phantom types.
When I try to compile a program with the following code:
newtype SpecialInt Int = Special Int
I get an error (..)
Type variables at the left side are, well, variables. Int
is not a variable (it starts with an uppercase), so the above does not make much sense. If you want to define a SpecialInt
, you should define this as:
newtype SpecialInt = Special Int
so without an Int
at the left side of the =
token.
Upvotes: 3