[GHC] #14967: Optimizer Casting Caf with nominal type

#14967: Optimizer Casting Caf with nominal type -------------------------------------+------------------------------------- Reporter: etn | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.4.1 Keywords: coerce, | Operating System: Unknown/Multiple nominal | Architecture: | Type of failure: Incorrect result Unknown/Multiple | at runtime Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- When you make a function with a constant result, the optimizer will turn it into a cast of a CAF, even if doing so coerces a nominal parameter into a different one. Minimal Example program: {{{#!hs {-# LANGUAGE RoleAnnotations #-} import Debug.Trace (trace) type role Nom nominal data Nom a = Nom Int deriving Show class Gen g where gen :: g instance Gen (Nom a) where gen = trace "genD" $ Nom 0 main = print (gen :: Nom Int) >> print (gen :: Nom ()) >> print (gen :: Nom Char) }}} This program shows that only one value of type Nom is created and shared, even though doing so requires coercing a nominal role I discovered this while checking core for sharing after creating a constraint result caching mechanism. An IOref ended up being shared for multiple different constraint result holders -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14967 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14967: Optimizer Casting Caf with nominal type -------------------------------------+------------------------------------- Reporter: etn | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.4.1 Resolution: | Keywords: coerce, | nominal Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by etn): After looking for an example where this breaks without unsafe operations, I realized that this is almost certainly a safe transformation under the assumption of referential transparency. What this now amounts to is that there needs to be a way to prevent this from occurring in the case that a value cannot be shared between types. This issue came up when implementing Applicative, and I have realized that it is probably impossible to avoid this unless we can disable thunk-rewriting for particular thunks. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14967#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14967: Optimizer Casting Caf with nominal type -------------------------------------+------------------------------------- Reporter: etn | Owner: (none) Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 8.4.1 Resolution: invalid | Keywords: coerce, | nominal Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by simonpj): * status: new => closed * resolution: => invalid Comment: Looks ok to me. We get {{{ T14967.$fGenNom_$cgen :: forall a. Nom a T14967.$fGenNom_$cgen = \ (@ a_a2zl) -> trace @ (Nom a_a2zl) T14967.$fGenNom3 (T14967.$fGenNom1 @ a_a2zl) }}} and we invoke this polymorphic function at three different types. But types are erased before code generation, so this turns into {{{ T14967.$fGenNom_$cgen = trace T14967.$fGenNom3 T14967.$fGenNom1 }}} and the three uses of this thunk are of course shared. There no casting involved. Only polymorphic functions. Does that make sense? I'll close as not-a-bug but re-open if you disagree. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14967#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC