In particular, this expression compiles:
x = 4 :: Product Int
Why does the literal 4
can be Product Int
, even though it's type is Num a => a
?
CodePudding user response:
4
is an expression with a type annotation. The unannotated expression has type Num a => a
, but the annotation forces the type to be Product Int
.
The annotation is legal because Product a
has a Num
instance whenever a
has a Num
instance (which Int
has):
> :info Product
newtype Product a = Product {getProduct :: a}
[...]
instance Num a => Num (Product a)
[...]
> Product 3 Product 5
Product {getProduct = 8}
(which is to say, Product Int
is a subtype of Num a => a
).