5cf004b282c246e420c136e20ca0d659f951af92
[packages/base.git] / Unsafe / Coerce.hs
1 {-# LANGUAGE Unsafe #-}
2 {-# LANGUAGE CPP, NoImplicitPrelude, MagicHash #-}
3
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module : Unsafe.Coerce
7 -- Copyright : Malcolm Wallace 2006
8 -- License : BSD-style (see the LICENSE file in the distribution)
9 --
10 -- Maintainer : libraries@haskell.org
11 -- Stability : experimental
12 -- Portability : portable
13 --
14 -- The highly unsafe primitive 'unsafeCoerce' converts a value from any
15 -- type to any other type. Needless to say, if you use this function,
16 -- it is your responsibility to ensure that the old and new types have
17 -- identical internal representations, in order to prevent runtime corruption.
18 --
19 -- The types for which 'unsafeCoerce' is representation-safe may differ
20 -- from compiler to compiler (and version to version).
21 --
22 -- * Documentation for correct usage in GHC will be found under
23 -- 'unsafeCoerce#' in GHC.Base (around which 'unsafeCoerce' is just a
24 -- trivial wrapper).
25 --
26 -- * In nhc98, the only representation-safe coercions are between Enum
27 -- types with the same range (e.g. Int, Int32, Char, Word32),
28 -- or between a newtype and the type that it wraps.
29
30 module Unsafe.Coerce (unsafeCoerce) where
31
32 #if defined(__GLASGOW_HASKELL__)
33 import GHC.Prim (unsafeCoerce#)
34 unsafeCoerce :: a -> b
35 unsafeCoerce = unsafeCoerce#
36 #endif
37
38 #if defined(__NHC__)
39 import NonStdUnsafeCoerce (unsafeCoerce)
40 #endif
41
42 #if defined(__HUGS__)
43 import Hugs.IOExts (unsafeCoerce)
44 #endif