/
ST.purs
71 lines (60 loc) · 2.09 KB
/
ST.purs
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
module Data.Record.ST
( STRecord
, freezeSTRecord
, thawSTRecord
, peekSTRecord
, pokeSTRecord
, runSTRecord
, pureSTRecord
) where
import Prelude
import Control.Monad.Eff (Eff, runPure)
import Control.Monad.ST (ST)
import Data.Symbol (class IsSymbol, SProxy, reflectSymbol)
-- | A value of type `STRecord h r` represents a mutable record with fields `r`,
-- | belonging to the state thread `h`.
-- |
-- | Create values of type `STRecord` using `thawSTRecord`.
foreign import data STRecord :: Type -> # Type -> Type
-- | Freeze a mutable record, creating a copy.
foreign import freezeSTRecord :: forall h r eff. STRecord h r -> Eff (st :: ST h | eff) (Record r)
-- | Thaw an immutable record, creating a copy.
foreign import thawSTRecord :: forall h r eff. Record r -> Eff (st :: ST h | eff) (STRecord h r)
-- | Run an ST computation safely, constructing a record.
foreign import runSTRecord :: forall r eff. (forall h. Eff (st :: ST h | eff) (STRecord h r)) -> Eff eff (Record r)
-- | Run an ST computation safely, constructing a record, assuming no other
-- | types of effects.
pureSTRecord :: forall r. (forall h eff. Eff (st :: ST h | eff) (STRecord h r)) -> Record r
pureSTRecord st = runPure (runSTRecord st)
foreign import unsafePeekSTRecord
:: forall a r h eff
. String
-> STRecord h r
-> Eff (st :: ST h | eff) a
-- | Read the current value of a field in a mutable record, by providing a
-- | type-level representative for the label which should be read.
peekSTRecord
:: forall l h a r r1 eff
. RowCons l a r1 r
=> IsSymbol l
=> SProxy l
-> STRecord h r
-> Eff (st :: ST h | eff) a
peekSTRecord l = unsafePeekSTRecord (reflectSymbol l)
foreign import unsafePokeSTRecord
:: forall a r h eff
. String
-> a
-> STRecord h r
-> Eff (st :: ST h | eff) Unit
-- | Modify a record in place, by providing a type-level representative for the label
-- | which should be updated.
pokeSTRecord
:: forall l h a r r1 eff
. RowCons l a r1 r
=> IsSymbol l
=> SProxy l
-> a
-> STRecord h r
-> Eff (st :: ST h | eff) Unit
pokeSTRecord l = unsafePokeSTRecord (reflectSymbol l)