[Haskell-cafe] GHC optimizations and unsafePerformIO - Was: Data.Typeable TypeRep Ord instance.

Daniel Fischer daniel.is.fischer at googlemail.com
Sat Jan 1 15:42:48 CET 2011

On Saturday 01 January 2011 15:07:13, Thomas Davie wrote:
> On 1 Jan 2011, at 12:38, Andreas Baldeau wrote:
> >
> > So the question is, if ghc could transform this to simply compare the
> > keys throwing away unsafePerformIO and return.
> Wouldn't a much better plan simply be to take typeRepKey out of the IO
> monad?

I don't think so, it's in IO for a reason:

> -- | Returns a unique integer associated with a 'TypeRep'.  This can
> -- be used for making a mapping with TypeReps
> -- as the keys, for example.  It is guaranteed that @t1 == t2@
> -- if and only if @typeRepKey t1 == typeRepKey t2 at .
> --
> -- It is in the 'IO' monad because the actual value of the key may
> -- vary from run to run of the program.  You should only rely on
> -- the equality property, not any actual key value.  The relative
> -- ordering of keys has no meaning either.
> --
> typeRepKey :: TypeRep -> IO Int

That also means that an Ord instance based on the keys may change from run 
to run. It's probably not a problem for applications if it's only used for 
storing TypeReps in a Map and not for programme logic (if typeOf 'a' < 
typeOf True then this else that), but it's somewhat fishy nevertheless.

More information about the Haskell-Cafe mailing list