{-# language BangPatterns #-}
{-# language KindSignatures #-}
{-# language MagicHash #-}
{-# language RankNTypes #-}
{-# language UnboxedTuples #-}

module Control.Monad.ST.Run
  ( -- * Arrays
    runArrayST
  , runSmallArrayST
  , runByteArrayST
  , runPrimArrayST
  , runUnliftedArrayST
    -- * Integral Types
  , runIntST
  , runInt8ST
  , runInt16ST
  , runInt32ST
  , runWordST
  , runWord8ST
  , runWord16ST
  , runWord32ST
    -- * Char
  , runCharST
    -- * Floating Point Types
  , runFloatST
  , runDoubleST
    -- * Tuples
  , runIntArrayST
  , runIntByteArrayST
  , runIntLiftedTypeST
  , runIntIntByteArrayST
  , runWordArrayST
  , runWordByteArrayST
    -- * Maybes
  , 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#),Int(I#),Word(W#),runRW#)
import GHC.Exts (Double(D#),Float(F#))
import GHC.Int (Int8(I8#),Int16(I16#),Int32(I32#))
import GHC.ST (ST(ST))
import GHC.Word (Word8(W8#),Word16(W16#),Word32(W32#))

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 = forall a. Array# a -> Array a
Array (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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 = forall a. SmallArray# a -> SmallArray a
SmallArray (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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 (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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 = forall a. ByteArray# -> PrimArray a
PrimArray (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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 a)) -> UnliftedArray a
{-# inline runUnliftedArrayST #-}
runUnliftedArrayST :: forall a. (forall s. ST s (UnliftedArray a)) -> UnliftedArray a
runUnliftedArrayST forall s. ST s (UnliftedArray a)
f = forall a. ArrayArray# -> UnliftedArray a
UnliftedArray (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case forall s. ST s (UnliftedArray a)
f of { ST STRep RealWorld (UnliftedArray a)
g -> case STRep RealWorld (UnliftedArray a)
g State# RealWorld
s0 of { (# State# RealWorld
_, UnliftedArray ArrayArray#
r #) -> ArrayArray#
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# (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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# (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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# (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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# (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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# (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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# (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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# (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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# (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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# (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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# (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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# (forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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 #) = forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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, 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 #) = forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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, 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 #) = forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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 #) = forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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 #) = forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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 #) = forall o. (State# RealWorld -> o) -> o
runRW# (\State# RealWorld
s0 -> case 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 = forall o. (State# RealWorld -> o) -> o
runRW#
        (\State# RealWorld
s0 -> case 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
        (# (# #) | #) -> forall a. Maybe a
Nothing
        (# | ByteArray#
y #) -> forall a. a -> Maybe a
Just (ByteArray# -> ByteArray
ByteArray ByteArray#
y)