|
| 1 | +module Data.Record.ST |
| 2 | + ( STRecord |
| 3 | + , freezeSTRecord |
| 4 | + , thawSTRecord |
| 5 | + , peekSTRecord |
| 6 | + , pokeSTRecord |
| 7 | + , runSTRecord |
| 8 | + , pureSTRecord |
| 9 | + ) where |
| 10 | + |
| 11 | +import Prelude |
| 12 | + |
| 13 | +import Control.Monad.Eff (Eff, runPure) |
| 14 | +import Control.Monad.ST (ST) |
| 15 | +import Data.Symbol (class IsSymbol, SProxy, reflectSymbol) |
| 16 | + |
| 17 | +-- | A value of type `STRecord h r` represents a mutable record with fields `r`, |
| 18 | +-- | belonging to the state thread `h`. |
| 19 | +-- | |
| 20 | +-- | Create values of type `STRecord` using `thawSTRecord`. |
| 21 | +foreign import data STRecord :: Type -> # Type -> Type |
| 22 | + |
| 23 | +-- | Freeze a mutable record, creating a copy. |
| 24 | +foreign import freezeSTRecord :: forall h r eff. STRecord h r -> Eff (st :: ST h | eff) (Record r) |
| 25 | + |
| 26 | +-- | Thaw an immutable record, creating a copy. |
| 27 | +foreign import thawSTRecord :: forall h r eff. Record r -> Eff (st :: ST h | eff) (STRecord h r) |
| 28 | + |
| 29 | +-- | Run an ST computation safely, constructing a record. |
| 30 | +foreign import runSTRecord :: forall r eff. (forall h. Eff (st :: ST h | eff) (STRecord h r)) -> Eff eff (Record r) |
| 31 | + |
| 32 | +-- | Run an ST computation safely, constructing a record, assuming no other |
| 33 | +-- | types of effects. |
| 34 | +pureSTRecord :: forall r. (forall h eff. Eff (st :: ST h | eff) (STRecord h r)) -> Record r |
| 35 | +pureSTRecord st = runPure (runSTRecord st) |
| 36 | + |
| 37 | +foreign import unsafePeekSTRecord |
| 38 | + :: forall a r h eff |
| 39 | + . String |
| 40 | + -> STRecord h r |
| 41 | + -> Eff (st :: ST h | eff) a |
| 42 | + |
| 43 | +-- | Read the current value of a field in a mutable record, by providing a |
| 44 | +-- | type-level representative for the label which should be read. |
| 45 | +peekSTRecord |
| 46 | + :: forall l h a r r1 eff |
| 47 | + . RowCons l a r1 r |
| 48 | + => IsSymbol l |
| 49 | + => SProxy l |
| 50 | + -> STRecord h r |
| 51 | + -> Eff (st :: ST h | eff) a |
| 52 | +peekSTRecord l = unsafePeekSTRecord (reflectSymbol l) |
| 53 | + |
| 54 | +foreign import unsafePokeSTRecord |
| 55 | + :: forall a r h eff |
| 56 | + . String |
| 57 | + -> a |
| 58 | + -> STRecord h r |
| 59 | + -> Eff (st :: ST h | eff) Unit |
| 60 | + |
| 61 | +-- | Modify a record in place, by providing a type-level representative for the label |
| 62 | +-- | which should be updated. |
| 63 | +pokeSTRecord |
| 64 | + :: forall l h a r r1 eff |
| 65 | + . RowCons l a r1 r |
| 66 | + => IsSymbol l |
| 67 | + => SProxy l |
| 68 | + -> a |
| 69 | + -> STRecord h r |
| 70 | + -> Eff (st :: ST h | eff) Unit |
| 71 | +pokeSTRecord l = unsafePokeSTRecord (reflectSymbol l) |
0 commit comments