I think, there is a simple optimization that GHC does not do yet.
If myValue :: forall phantom. MyType phantom -- phantom is a phantom variable is referred to from multiple contexts asking for different types for phantom, sharing should survive such that myValue is only evalutated once. I have not tried if GHC does that already.
If myValue :: forall phantom. MyClass phantom => MyType phantom -- phantom is a phantom variable is referred to from multiple contexts asking for different types for phantom, sharing should not survive because the value depends on the phantom's instance definition.
But if myValue :: forall phantom. MyTypeFamily phantom ~ MyOtherType => MyType phantom -- phantom is a phantom variable is referred to from multiple contexts asking for different types for phantom, am I right, that sharing would be safe again? I have tried and GHC does not do this optimization
From Core perspective, it sounds like a constant function - I guess it would reuse same value in case of local binding with inferred type (MonomorphismRestriction)
I think, there is a simple optimization that GHC does not do yet.
If
myValue :: forall phantom. MyType phantom -- phantom is a phantom variable
is referred to from multiple contexts asking for different types forphantom
, sharing should survive such thatmyValue
is only evalutated once. I have not tried if GHC does that already.If
myValue :: forall phantom. MyClass phantom => MyType phantom -- phantom is a phantom variable
is referred to from multiple contexts asking for different types forphantom
, sharing should not survive because the value depends on thephantom
's instance definition.But if
myValue :: forall phantom. MyTypeFamily phantom ~ MyOtherType => MyType phantom -- phantom is a phantom variable
is referred to from multiple contexts asking for different types forphantom
, am I right, that sharing would be safe again? I have tried and GHC does not do this optimizationFrom Core perspective, it sounds like a constant function - I guess it would reuse same value in case of local binding with inferred type (
MonomorphismRestriction
)It does not compile with active
MonomorphismRestriction
and no type signature.Ah, ambiguous type?
Or what does it say?
Couldn't match type
Because the contexts expect different types for
phantom
.Oh, I see
So only one of them can get the type for
phantom
it wants.