File: Trans.hs

package info (click to toggle)
haskell-stmonadtrans 0.4.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 96 kB
  • sloc: haskell: 589; makefile: 2
file content (222 lines) | stat: -rw-r--r-- 6,735 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
{-# LANGUAGE CPP, MagicHash, Rank2Types #-}
{- |
   Module      :  Control.Monad.ST.Trans
   Copyright   :  Josef Svenningsson 2008-2017
                  (c) The University of Glasgow, 1994-2000
   License     :  BSD

   Maintainer  :  josef.svenningsson@gmail.com, Andreas Abel
   Stability   :  stable
   Portability :  non-portable (GHC Extensions)

   This library provides a monad transformer version of the ST monad.

   Warning! This monad transformer should not be used with monads that
   can contain multiple answers, like the list monad. The reason is that
   the state token will be duplicated across the different answers and
   this causes Bad Things to happen (such as loss of referential
   transparency). Safe monads include the monads @'State'@, @'Reader'@, @'Writer'@,
   @'Maybe'@ and combinations of their corresponding monad transformers.

-}
module Control.Monad.ST.Trans(
      -- * The ST Monad Transformer
      STT,
      runST,
      runSTT,
      -- * Mutable references
      STRef,
      newSTRef,
      readSTRef,
      writeSTRef,
      -- * Mutable arrays
      STArray,
      newSTArray,
      readSTArray,
      writeSTArray,
      boundsSTArray,
      numElementsSTArray,
      freezeSTArray,
      thawSTArray,
      runSTArray,
      -- * Unsafe Operations
      unsafeReadSTArray,
      unsafeWriteSTArray,
      unsafeFreezeSTArray,
      unsafeThawSTArray,
      unsafeIOToSTT,
      unsafeSTToIO,
      unsafeSTTToIO,
      unsafeSTRefToIORef,
      unsafeIORefToSTRef
      ) where

import GHC.Base            (realWorld#)
import GHC.Arr             (Ix, Array(..))
import qualified GHC.Arr as STArray

#if __GLASGOW_HASKELL__ <= 708
import Control.Applicative (Applicative)
#endif

import Control.Monad.ST.Trans.Internal

import Data.Array.ST       (STArray, newArray, readArray, writeArray)
import Data.IORef          (IORef)
import Data.STRef          (STRef)
import qualified Data.STRef as STRef

import System.IO.Unsafe    (unsafePerformIO)
import Unsafe.Coerce       (unsafeCoerce)

{-# INLINE newSTRef #-}
-- | Create a new reference
newSTRef :: (Applicative m) => a -> STT s m (STRef s a)
newSTRef i = liftST (STRef.newSTRef i)

{-# INLINE readSTRef #-}
-- | Reads the value of a reference
readSTRef :: (Applicative m) => STRef s a -> STT s m a
readSTRef ref = liftST (STRef.readSTRef ref)

{-# INLINE writeSTRef #-}
-- | Modifies the value of a reference
writeSTRef :: (Applicative m) => STRef s a -> a -> STT s m ()
writeSTRef ref a = liftST (STRef.writeSTRef ref a)

{-# DEPRECATED runST "Use runSTT instead" #-}
{-# NOINLINE runST #-}
-- | Executes a computation in the 'STT' monad transformer
runST :: Monad m => (forall s. STT s m a) -> m a
runST m = let (STT f) = m
 -- the parenthesis is needed because of a bug in the parser of GHC
          in do (STTRet _st a) <- ( f realWorld# )
                return a

{-# NOINLINE runSTT #-}
-- | Executes a computation in the 'STT' monad transformer
runSTT :: Monad m => (forall s. STT s m a) -> m a
runSTT m = let (STT f) = m
           in do (STTRet _st a) <- ( f realWorld# )
                 return a

-- Mutable arrays.

{-# INLINE newSTArray #-}
-- | Creates a new mutable array
newSTArray :: (Ix i, Applicative m) =>
              (i,i) -> e -> STT s m (STArray s i e)
newSTArray bnds i = liftST (newArray bnds i)

{-# INLINE boundsSTArray #-}
-- | Returns the lowest and highest indices of the array
boundsSTArray :: STArray s i e -> (i,i)
boundsSTArray = STArray.boundsSTArray

{-# INLINE numElementsSTArray #-}
-- | Returns the number of elements in the array
numElementsSTArray :: STArray s i e -> Int
numElementsSTArray = STArray.numElementsSTArray

{-# INLINE readSTArray #-}
-- | Retrieves an element from the array
readSTArray :: (Ix i, Applicative m) =>
               STArray s i e -> i -> STT s m e
readSTArray arr i = liftST (readArray arr i)

{-# INLINE unsafeReadSTArray #-}
unsafeReadSTArray :: (
#if __GLASGOW_HASKELL__ <= 710
    Ix i,
#endif
    Applicative m) =>
                     STArray s i e -> Int -> STT s m e
unsafeReadSTArray arr i = liftST (STArray.unsafeReadSTArray arr i)

{-# INLINE writeSTArray #-}
-- | Modifies an element in the array
writeSTArray :: (Ix i, Applicative m) =>
                STArray s i e -> i -> e -> STT s m ()
writeSTArray arr i e = liftST (writeArray arr i e)

{-# INLINE unsafeWriteSTArray #-}
unsafeWriteSTArray :: (
#if __GLASGOW_HASKELL__ <= 710
    Ix i,
#endif
  Applicative m) =>
                      STArray s i e -> Int -> e -> STT s m ()
unsafeWriteSTArray arr i e = liftST (STArray.unsafeWriteSTArray arr i e)

{-# INLINE freezeSTArray #-}
-- | Copy a mutable array and turn it into an immutable array
freezeSTArray :: (
#if __GLASGOW_HASKELL__ <= 710
    Ix i,
#endif
  Applicative m) =>
                 STArray s i e -> STT s m (Array i e)
freezeSTArray arr = liftST (STArray.freezeSTArray arr)

{-# INLINE unsafeFreezeSTArray #-}
unsafeFreezeSTArray :: (
#if __GLASGOW_HASKELL__ <= 710
    Ix i,
#endif
  Applicative m) =>
                       STArray s i e -> STT s m (Array i e)
unsafeFreezeSTArray arr = liftST (STArray.unsafeFreezeSTArray arr)

{-# INLINE thawSTArray #-}
-- | Copy an immutable array and turn it into a mutable array
thawSTArray :: (
#if __GLASGOW_HASKELL__ <= 710
    Ix i,
#endif
  Applicative m) =>
               Array i e -> STT s m (STArray s i e)
thawSTArray arr = liftST (STArray.thawSTArray arr)

{-# INLINE unsafeThawSTArray #-}
unsafeThawSTArray :: (
#if __GLASGOW_HASKELL__ <= 710
    Ix i,
#endif
  Applicative m) =>
                     Array i e -> STT s m (STArray s i e)
unsafeThawSTArray arr = liftST (STArray.unsafeThawSTArray arr)

{-# INLINE runSTArray #-}
-- | A safe way to create and work with a mutable array before returning an
-- immutable array for later perusal.  This function avoids copying
-- the array before returning it.
runSTArray :: (
#if __GLASGOW_HASKELL__ <= 710
  Ix i,
#endif
#if __GLASGOW_HASKELL__ <= 708
  Applicative m,
#endif
  Monad m)
           => (forall s . STT s m (STArray s i e))
           -> m (Array i e)
runSTArray st = runSTT (st >>= unsafeFreezeSTArray)


{-# NOINLINE unsafeIOToSTT #-}
unsafeIOToSTT :: (Monad m, Functor m) => IO a -> STT s m a
unsafeIOToSTT m = return $! unsafePerformIO m

{-# DEPRECATED unsafeSTToIO "Use unsafeSTTToIO instead" #-}
unsafeSTToIO :: STT s IO a -> IO a
unsafeSTToIO m = runSTT $ unsafeCoerce m

unsafeSTTToIO :: STT s IO a -> IO a
unsafeSTTToIO m = runSTT $ unsafeCoerce m

-- This should work, as STRef and IORef should have identical internal representation
unsafeSTRefToIORef  :: STRef s a -> IORef a
unsafeSTRefToIORef ref = unsafeCoerce ref

unsafeIORefToSTRef :: IORef a -> STRef s a
unsafeIORefToSTRef ref = unsafeCoerce ref