Generalize runSTArray and runSTUArray

Henning Thielemann lemming at henning-thielemann.de
Thu Aug 22 04:45:56 UTC 2019


On Wed, 21 Aug 2019, Zemyla wrote:

> The "runSTArray" and "runSTUArray" functions allow efficiently working 
> with Arrays in the ST monad before turning them immutable

I think instead of some new functions we should have a Kleisli Arrow data 
type that hides unsafeFreeze for us.

newtype Freezer s a b =
    -- private constructor and accessor
    Freezer {getFreezer :: a -> ST s b}

instance Category Freezer where
instance Arrow Freezer where

quickFreeze :: Freezer s (STArray s i e) (Array i e)
quickFreeze = Freezer unsafeFreeze

runFrozen :: (forall s. Freezer s a b) -> (forall s. ST s a) -> b
runFrozen freezer st = runST (getFreezer (freezer st))


For moving two arrays and an additional value out, you would do

runFrozen ((quickFreeze *** quickFreeze) *** id) st


More information about the Libraries mailing list