{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE UnboxedTuples #-}
module Control.Monad.ST.Run
(
runArrayST
, runSmallArrayST
, runByteArrayST
, runPrimArrayST
, runUnliftedArrayST
, runIntST
, runInt8ST
, runInt16ST
, runInt32ST
, runWordST
, runWord8ST
, runWord16ST
, runWord32ST
, runCharST
, runFloatST
, runDoubleST
, runIntArrayST
, runIntByteArrayST
, runIntLiftedTypeST
, runIntIntByteArrayST
, runWordArrayST
, runWordByteArrayST
, runMaybeByteArrayST
) where
import Data.Kind (Type)
import Data.Primitive.Array (Array (Array))
import Data.Primitive.ByteArray (ByteArray (ByteArray))
import Data.Primitive.PrimArray (PrimArray (PrimArray))
import Data.Primitive.SmallArray (SmallArray (SmallArray))
import Data.Primitive.Unlifted.Array (UnliftedArray_ (UnliftedArray))
import GHC.Exts (Char (C#), Double (D#), Float (F#), Int (I#), Word (W#), runRW#)
import GHC.Int (Int16 (I16#), Int32 (I32#), Int8 (I8#))
import GHC.ST (ST (ST))
import GHC.Word (Word16 (W16#), Word32 (W32#), Word8 (W8#))
runArrayST :: (forall s. ST s (Array a)) -> Array a
{-# INLINE runArrayST #-}
runArrayST :: forall a. (forall s. ST s (Array a)) -> Array a
runArrayST forall s. ST s (Array a)
f = Array# a -> Array a
forall a. Array# a -> Array a
Array ((State# RealWorld -> Array# a) -> Array# a
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld (Array a)
forall s. ST s (Array a)
f of ST STRep RealWorld (Array a)
g -> case STRep RealWorld (Array a)
g State# RealWorld
s0 of (# State# RealWorld
_, Array Array# a
r #) -> Array# a
r))
runSmallArrayST :: (forall s. ST s (SmallArray a)) -> SmallArray a
{-# INLINE runSmallArrayST #-}
runSmallArrayST :: forall a. (forall s. ST s (SmallArray a)) -> SmallArray a
runSmallArrayST forall s. ST s (SmallArray a)
f = SmallArray# a -> SmallArray a
forall a. SmallArray# a -> SmallArray a
SmallArray ((State# RealWorld -> SmallArray# a) -> SmallArray# a
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld (SmallArray a)
forall s. ST s (SmallArray a)
f of ST STRep RealWorld (SmallArray a)
g -> case STRep RealWorld (SmallArray a)
g State# RealWorld
s0 of (# State# RealWorld
_, SmallArray SmallArray# a
r #) -> SmallArray# a
r))
runByteArrayST :: (forall s. ST s ByteArray) -> ByteArray
{-# INLINE runByteArrayST #-}
runByteArrayST :: (forall s. ST s ByteArray) -> ByteArray
runByteArrayST forall s. ST s ByteArray
f = ByteArray# -> ByteArray
ByteArray ((State# RealWorld -> ByteArray#) -> ByteArray#
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld ByteArray
forall s. ST s ByteArray
f of ST STRep RealWorld ByteArray
g -> case STRep RealWorld ByteArray
g State# RealWorld
s0 of (# State# RealWorld
_, ByteArray ByteArray#
r #) -> ByteArray#
r))
runPrimArrayST :: (forall s. ST s (PrimArray a)) -> PrimArray a
{-# INLINE runPrimArrayST #-}
runPrimArrayST :: forall a. (forall s. ST s (PrimArray a)) -> PrimArray a
runPrimArrayST forall s. ST s (PrimArray a)
f = ByteArray# -> PrimArray a
forall a. ByteArray# -> PrimArray a
PrimArray ((State# RealWorld -> ByteArray#) -> ByteArray#
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld (PrimArray a)
forall s. ST s (PrimArray a)
f of ST STRep RealWorld (PrimArray a)
g -> case STRep RealWorld (PrimArray a)
g State# RealWorld
s0 of (# State# RealWorld
_, PrimArray ByteArray#
r #) -> ByteArray#
r))
runUnliftedArrayST :: (forall s. ST s (UnliftedArray_ unlifted_a a)) -> UnliftedArray_ unlifted_a a
{-# INLINE runUnliftedArrayST #-}
runUnliftedArrayST :: forall (unlifted_a :: UnliftedType) a.
(forall s. ST s (UnliftedArray_ unlifted_a a))
-> UnliftedArray_ unlifted_a a
runUnliftedArrayST forall s. ST s (UnliftedArray_ unlifted_a a)
f = UnliftedArray# unlifted_a -> UnliftedArray_ unlifted_a a
forall (unlifted_a :: UnliftedType) a.
UnliftedArray# unlifted_a -> UnliftedArray_ unlifted_a a
UnliftedArray ((State# RealWorld -> UnliftedArray# unlifted_a)
-> UnliftedArray# unlifted_a
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld (UnliftedArray_ unlifted_a a)
forall s. ST s (UnliftedArray_ unlifted_a a)
f of ST STRep RealWorld (UnliftedArray_ unlifted_a a)
g -> case STRep RealWorld (UnliftedArray_ unlifted_a a)
g State# RealWorld
s0 of (# State# RealWorld
_, UnliftedArray UnliftedArray# unlifted_a
r #) -> UnliftedArray# unlifted_a
r))
runCharST :: (forall s. ST s Char) -> Char
{-# INLINE runCharST #-}
runCharST :: (forall s. ST s Char) -> Char
runCharST forall s. ST s Char
f = Char# -> Char
C# ((State# RealWorld -> Char#) -> Char#
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld Char
forall s. ST s Char
f of ST STRep RealWorld Char
g -> case STRep RealWorld Char
g State# RealWorld
s0 of (# State# RealWorld
_, C# Char#
r #) -> Char#
r))
runFloatST :: (forall s. ST s Float) -> Float
{-# INLINE runFloatST #-}
runFloatST :: (forall s. ST s Float) -> Float
runFloatST forall s. ST s Float
f = Float# -> Float
F# ((State# RealWorld -> Float#) -> Float#
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld Float
forall s. ST s Float
f of ST STRep RealWorld Float
g -> case STRep RealWorld Float
g State# RealWorld
s0 of (# State# RealWorld
_, F# Float#
r #) -> Float#
r))
runDoubleST :: (forall s. ST s Double) -> Double
{-# INLINE runDoubleST #-}
runDoubleST :: (forall s. ST s Double) -> Double
runDoubleST forall s. ST s Double
f = Double# -> Double
D# ((State# RealWorld -> Double#) -> Double#
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld Double
forall s. ST s Double
f of ST STRep RealWorld Double
g -> case STRep RealWorld Double
g State# RealWorld
s0 of (# State# RealWorld
_, D# Double#
r #) -> Double#
r))
runIntST :: (forall s. ST s Int) -> Int
{-# INLINE runIntST #-}
runIntST :: (forall s. ST s Int) -> Int
runIntST forall s. ST s Int
f = Int# -> Int
I# ((State# RealWorld -> Int#) -> Int#
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld Int
forall s. ST s Int
f of ST STRep RealWorld Int
g -> case STRep RealWorld Int
g State# RealWorld
s0 of (# State# RealWorld
_, I# Int#
r #) -> Int#
r))
runWordST :: (forall s. ST s Word) -> Word
{-# INLINE runWordST #-}
runWordST :: (forall s. ST s Word) -> Word
runWordST forall s. ST s Word
f = Word# -> Word
W# ((State# RealWorld -> Word#) -> Word#
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld Word
forall s. ST s Word
f of ST STRep RealWorld Word
g -> case STRep RealWorld Word
g State# RealWorld
s0 of (# State# RealWorld
_, W# Word#
r #) -> Word#
r))
runWord8ST :: (forall s. ST s Word8) -> Word8
{-# INLINE runWord8ST #-}
runWord8ST :: (forall s. ST s Word8) -> Word8
runWord8ST forall s. ST s Word8
f = Word8# -> Word8
W8# ((State# RealWorld -> Word8#) -> Word8#
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld Word8
forall s. ST s Word8
f of ST STRep RealWorld Word8
g -> case STRep RealWorld Word8
g State# RealWorld
s0 of (# State# RealWorld
_, W8# Word8#
r #) -> Word8#
r))
runWord16ST :: (forall s. ST s Word16) -> Word16
{-# INLINE runWord16ST #-}
runWord16ST :: (forall s. ST s Word16) -> Word16
runWord16ST forall s. ST s Word16
f = Word16# -> Word16
W16# ((State# RealWorld -> Word16#) -> Word16#
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld Word16
forall s. ST s Word16
f of ST STRep RealWorld Word16
g -> case STRep RealWorld Word16
g State# RealWorld
s0 of (# State# RealWorld
_, W16# Word16#
r #) -> Word16#
r))
runWord32ST :: (forall s. ST s Word32) -> Word32
{-# INLINE runWord32ST #-}
runWord32ST :: (forall s. ST s Word32) -> Word32
runWord32ST forall s. ST s Word32
f = Word32# -> Word32
W32# ((State# RealWorld -> Word32#) -> Word32#
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld Word32
forall s. ST s Word32
f of ST STRep RealWorld Word32
g -> case STRep RealWorld Word32
g State# RealWorld
s0 of (# State# RealWorld
_, W32# Word32#
r #) -> Word32#
r))
runInt8ST :: (forall s. ST s Int8) -> Int8
{-# INLINE runInt8ST #-}
runInt8ST :: (forall s. ST s Int8) -> Int8
runInt8ST forall s. ST s Int8
f = Int8# -> Int8
I8# ((State# RealWorld -> Int8#) -> Int8#
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld Int8
forall s. ST s Int8
f of ST STRep RealWorld Int8
g -> case STRep RealWorld Int8
g State# RealWorld
s0 of (# State# RealWorld
_, I8# Int8#
r #) -> Int8#
r))
runInt16ST :: (forall s. ST s Int16) -> Int16
{-# INLINE runInt16ST #-}
runInt16ST :: (forall s. ST s Int16) -> Int16
runInt16ST forall s. ST s Int16
f = Int16# -> Int16
I16# ((State# RealWorld -> Int16#) -> Int16#
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld Int16
forall s. ST s Int16
f of ST STRep RealWorld Int16
g -> case STRep RealWorld Int16
g State# RealWorld
s0 of (# State# RealWorld
_, I16# Int16#
r #) -> Int16#
r))
runInt32ST :: (forall s. ST s Int32) -> Int32
{-# INLINE runInt32ST #-}
runInt32ST :: (forall s. ST s Int32) -> Int32
runInt32ST forall s. ST s Int32
f = Int32# -> Int32
I32# ((State# RealWorld -> Int32#) -> Int32#
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld Int32
forall s. ST s Int32
f of ST STRep RealWorld Int32
g -> case STRep RealWorld Int32
g State# RealWorld
s0 of (# State# RealWorld
_, I32# Int32#
r #) -> Int32#
r))
runIntArrayST :: (forall s. ST s (Int, Array a)) -> (Int, Array a)
{-# INLINE runIntArrayST #-}
runIntArrayST :: forall a. (forall s. ST s (Int, Array a)) -> (Int, Array a)
runIntArrayST forall s. ST s (Int, Array a)
f =
let !(# Int#
t0, Array# a
t1 #) = (State# RealWorld -> (# Int#, Array# a #)) -> (# Int#, Array# a #)
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld (Int, Array a)
forall s. ST s (Int, Array a)
f of ST STRep RealWorld (Int, Array a)
g -> case STRep RealWorld (Int, Array a)
g State# RealWorld
s0 of (# State# RealWorld
_, (I# Int#
r0, Array Array# a
r1) #) -> (# Int#
r0, Array# a
r1 #))
in (Int# -> Int
I# Int#
t0, Array# a -> Array a
forall a. Array# a -> Array a
Array Array# a
t1)
runWordArrayST :: (forall s. ST s (Word, Array a)) -> (Word, Array a)
{-# INLINE runWordArrayST #-}
runWordArrayST :: forall a. (forall s. ST s (Word, Array a)) -> (Word, Array a)
runWordArrayST forall s. ST s (Word, Array a)
f =
let !(# Word#
t0, Array# a
t1 #) = (State# RealWorld -> (# Word#, Array# a #))
-> (# Word#, Array# a #)
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld (Word, Array a)
forall s. ST s (Word, Array a)
f of ST STRep RealWorld (Word, Array a)
g -> case STRep RealWorld (Word, Array a)
g State# RealWorld
s0 of (# State# RealWorld
_, (W# Word#
r0, Array Array# a
r1) #) -> (# Word#
r0, Array# a
r1 #))
in (Word# -> Word
W# Word#
t0, Array# a -> Array a
forall a. Array# a -> Array a
Array Array# a
t1)
runIntLiftedTypeST :: forall (a :: Type). (forall s. ST s (Int, a)) -> (Int, a)
{-# INLINE runIntLiftedTypeST #-}
runIntLiftedTypeST :: forall a. (forall s. ST s (Int, a)) -> (Int, a)
runIntLiftedTypeST forall s. ST s (Int, a)
f =
let !(# Int#
t0, a
t1 #) = (State# RealWorld -> (# Int#, a #)) -> (# Int#, a #)
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld (Int, a)
forall s. ST s (Int, a)
f of ST STRep RealWorld (Int, a)
g -> case STRep RealWorld (Int, a)
g State# RealWorld
s0 of (# State# RealWorld
_, (I# Int#
r0, a
r1) #) -> (# Int#
r0, a
r1 #))
in (Int# -> Int
I# Int#
t0, a
t1)
runIntByteArrayST :: (forall s. ST s (Int, ByteArray)) -> (Int, ByteArray)
{-# INLINE runIntByteArrayST #-}
runIntByteArrayST :: (forall s. ST s (Int, ByteArray)) -> (Int, ByteArray)
runIntByteArrayST forall s. ST s (Int, ByteArray)
f =
let !(# Int#
t0, ByteArray#
t1 #) = (State# RealWorld -> (# Int#, ByteArray# #))
-> (# Int#, ByteArray# #)
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld (Int, ByteArray)
forall s. ST s (Int, ByteArray)
f of ST STRep RealWorld (Int, ByteArray)
g -> case STRep RealWorld (Int, ByteArray)
g State# RealWorld
s0 of (# State# RealWorld
_, (I# Int#
r0, ByteArray ByteArray#
r1) #) -> (# Int#
r0, ByteArray#
r1 #))
in (Int# -> Int
I# Int#
t0, ByteArray# -> ByteArray
ByteArray ByteArray#
t1)
runIntIntByteArrayST :: (forall s. ST s (Int, Int, ByteArray)) -> (Int, Int, ByteArray)
{-# INLINE runIntIntByteArrayST #-}
runIntIntByteArrayST :: (forall s. ST s (Int, Int, ByteArray)) -> (Int, Int, ByteArray)
runIntIntByteArrayST forall s. ST s (Int, Int, ByteArray)
f =
let !(# Int#
t0, Int#
t1, ByteArray#
t2 #) = (State# RealWorld -> (# Int#, Int#, ByteArray# #))
-> (# Int#, Int#, ByteArray# #)
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld (Int, Int, ByteArray)
forall s. ST s (Int, Int, ByteArray)
f of ST STRep RealWorld (Int, Int, ByteArray)
g -> case STRep RealWorld (Int, Int, ByteArray)
g State# RealWorld
s0 of (# State# RealWorld
_, (I# Int#
r0, I# Int#
r1, ByteArray ByteArray#
r2) #) -> (# Int#
r0, Int#
r1, ByteArray#
r2 #))
in (Int# -> Int
I# Int#
t0, Int# -> Int
I# Int#
t1, ByteArray# -> ByteArray
ByteArray ByteArray#
t2)
runWordByteArrayST :: (forall s. ST s (Word, ByteArray)) -> (Word, ByteArray)
{-# INLINE runWordByteArrayST #-}
runWordByteArrayST :: (forall s. ST s (Word, ByteArray)) -> (Word, ByteArray)
runWordByteArrayST forall s. ST s (Word, ByteArray)
f =
let !(# Word#
t0, ByteArray#
t1 #) = (State# RealWorld -> (# Word#, ByteArray# #))
-> (# Word#, ByteArray# #)
forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case ST RealWorld (Word, ByteArray)
forall s. ST s (Word, ByteArray)
f of ST STRep RealWorld (Word, ByteArray)
g -> case STRep RealWorld (Word, ByteArray)
g State# RealWorld
s0 of (# State# RealWorld
_, (W# Word#
r0, ByteArray ByteArray#
r1) #) -> (# Word#
r0, ByteArray#
r1 #))
in (Word# -> Word
W# Word#
t0, ByteArray# -> ByteArray
ByteArray ByteArray#
t1)
runMaybeByteArrayST :: (forall s. ST s (Maybe ByteArray)) -> Maybe ByteArray
{-# INLINE runMaybeByteArrayST #-}
runMaybeByteArrayST :: (forall s. ST s (Maybe ByteArray)) -> Maybe ByteArray
runMaybeByteArrayST forall s. ST s (Maybe ByteArray)
f =
let !x :: (# (# #) | ByteArray# #)
x =
(State# RealWorld -> (# (# #) | ByteArray# #))
-> (# (# #) | ByteArray# #)
forall o. (State# RealWorld -> o) -> o
runRW#
( \State# RealWorld
s0 -> case ST RealWorld (Maybe ByteArray)
forall s. ST s (Maybe ByteArray)
f of
ST STRep RealWorld (Maybe ByteArray)
g -> case STRep RealWorld (Maybe ByteArray)
g State# RealWorld
s0 of
(# State# RealWorld
_, Just (ByteArray ByteArray#
r2) #) -> (# | ByteArray#
r2 #)
(# State# RealWorld
_, Maybe ByteArray
Nothing #) -> (# (# #) | #)
)
in case (# (# #) | ByteArray# #)
x of
(# (# #) | #) -> Maybe ByteArray
forall a. Maybe a
Nothing
(# | ByteArray#
y #) -> ByteArray -> Maybe ByteArray
forall a. a -> Maybe a
Just (ByteArray# -> ByteArray
ByteArray ByteArray#
y)