[GHC] #14195: Generalize makeStableName#

#14195: Generalize makeStableName# -------------------------------------+------------------------------------- Reporter: andrewthad | Owner: (none) Type: feature | Status: new request | Priority: low | Milestone: Component: Compiler | Version: 8.2.1 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- The function `makeStableName#` has the following type: {{{ makeStableName# :: a -> State# RealWorld -> (#State# RealWorld, StableName# a#) }}} I believe that it could safely be changed to: {{{ makeStableName# :: a -> State# s -> (#State# s, StableName# a#) }}} Currently, I have some code that looks like this: {{{ {-# LANGUAGE MagicHash #-} {-# LANGUAGE UnboxedTuples #-} module Example ( Stable , stabilize , destabilize ) where import GHC.Prim import GHC.ST data Stable s a = Stable (StableName# a) a stabilize :: a -> ST s (Stable s a) stabilize a = ST $ \ s -> case makeStableName# a (unsafeCoerce# s) of (# s', sn #) -> (# unsafeCoerce# s', Stable sn a #) destabilize :: Stable s a -> a destabilize (Stable _ a) = a instance Eq a => Eq (Stable s a) where Stable sn1 a1 == Stable sn2 a2 = case eqStableName# sn1 sn2 of 0# -> a1 == a2 _ -> True }}} I want to be working in `ST`, not in `IO`, and I believe that this use of `unsafeCoerce#` is safe. However, it would be nice to not have to use it at all. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14195 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14195: Generalize makeStableName# -------------------------------------+------------------------------------- Reporter: andrewthad | Owner: (none) Type: feature request | Status: patch Priority: low | Milestone: Component: Compiler | Version: 8.2.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D3928 Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * status: new => patch * differential: => Phab:D3928 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14195#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14195: Generalize makeStableName# -------------------------------------+------------------------------------- Reporter: andrewthad | Owner: (none) Type: feature request | Status: patch Priority: low | Milestone: Component: Compiler | Version: 8.2.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D3928 Wiki Page: | -------------------------------------+------------------------------------- Description changed by bgamari: Old description:
The function `makeStableName#` has the following type:
{{{ makeStableName# :: a -> State# RealWorld -> (#State# RealWorld, StableName# a#) }}}
I believe that it could safely be changed to:
{{{ makeStableName# :: a -> State# s -> (#State# s, StableName# a#) }}}
Currently, I have some code that looks like this:
{{{ {-# LANGUAGE MagicHash #-} {-# LANGUAGE UnboxedTuples #-} module Example ( Stable , stabilize , destabilize ) where
import GHC.Prim import GHC.ST
data Stable s a = Stable (StableName# a) a
stabilize :: a -> ST s (Stable s a) stabilize a = ST $ \ s -> case makeStableName# a (unsafeCoerce# s) of (# s', sn #) -> (# unsafeCoerce# s', Stable sn a #)
destabilize :: Stable s a -> a destabilize (Stable _ a) = a
instance Eq a => Eq (Stable s a) where Stable sn1 a1 == Stable sn2 a2 = case eqStableName# sn1 sn2 of 0# -> a1 == a2 _ -> True }}}
I want to be working in `ST`, not in `IO`, and I believe that this use of `unsafeCoerce#` is safe. However, it would be nice to not have to use it at all.
New description: The function `makeStableName#` has the following type: {{{#!hs makeStableName# :: a -> State# RealWorld -> (#State# RealWorld, StableName# a#) }}} I believe that it could safely be changed to: {{{#!hs makeStableName# :: a -> State# s -> (#State# s, StableName# a#) }}} Currently, I have some code that looks like this: {{{ {-# LANGUAGE MagicHash #-} {-# LANGUAGE UnboxedTuples #-} module Example ( Stable , stabilize , destabilize ) where import GHC.Prim import GHC.ST data Stable s a = Stable (StableName# a) a stabilize :: a -> ST s (Stable s a) stabilize a = ST $ \ s -> case makeStableName# a (unsafeCoerce# s) of (# s', sn #) -> (# unsafeCoerce# s', Stable sn a #) destabilize :: Stable s a -> a destabilize (Stable _ a) = a instance Eq a => Eq (Stable s a) where Stable sn1 a1 == Stable sn2 a2 = case eqStableName# sn1 sn2 of 0# -> a1 == a2 _ -> True }}} I want to be working in `ST`, not in `IO`, and I believe that this use of `unsafeCoerce#` is safe. However, it would be nice to not have to use it at all. -- -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14195#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14195: Generalize makeStableName# -------------------------------------+------------------------------------- Reporter: andrewthad | Owner: (none) Type: feature request | Status: patch Priority: low | Milestone: Component: Compiler | Version: 8.2.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D3928 Wiki Page: | -------------------------------------+------------------------------------- Changes (by simonpj): * cc: simonmar (added) Comment: Hang on. Making a new stable name has an I/O effect: it modifies a single, shared stable name table. When things have an I/O effect we put them in the IO monad. To put it more concretely, with the new signature I could say {{{ mks v :: a -> StableName# a mks v = runST (mkStableName# v) }}} But as the original paper points out, making a fresh stable name is not a pure operation. Copying Simon Marlow -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14195#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14195: Generalize makeStableName# -------------------------------------+------------------------------------- Reporter: andrewthad | Owner: (none) Type: feature request | Status: patch Priority: low | Milestone: Component: Compiler | Version: 8.2.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D3928 Wiki Page: | -------------------------------------+------------------------------------- Comment (by andrewthad): You're right. To actually make this be safe, we would have to redefine `StableName#` as well: {{{#!hs data StableName# s a makeStableName# :: a -> State# s -> (#State# s, StableName# s a#) }}} And now you can't use `runST` create a `StableName` at the top level like you did before. But, now this becomes a more annoying change to make. I'm going to go ahead and close this because, as you've pointed out, it's not as simple to do as I thought, and because I've realized that I don't actually need to use `StableName` to accomplish the thing I was originally planning on doing. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14195#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14195: Generalize makeStableName# -------------------------------------+------------------------------------- Reporter: andrewthad | Owner: (none) Type: feature request | Status: closed Priority: low | Milestone: Component: Compiler | Version: 8.2.1 Resolution: wontfix | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D3928 Wiki Page: | -------------------------------------+------------------------------------- Changes (by andrewthad): * status: patch => closed * resolution: => wontfix -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14195#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC