compiler optimization - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

rednaZ

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

TheMatten

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)

rednaZ

It does not compile with active MonomorphismRestriction and no type signature.

TheMatten

Ah, ambiguous type?

TheMatten

Or what does it say?

rednaZ

Couldn't match type

rednaZ

Because the contexts expect different types for phantom.

rednaZ

So only one of them can get the type for phantom it wants.