A mutable reference in IO, wrapping Lean's IO.Ref.
$$\text{IORef}(\alpha) \cong \text{IO.Ref}(\alpha)$$
Equations
- Data.IORef α = IO.Ref α
Instances For
Create a new IORef with an initial value.
$$\text{newIORef} : \alpha \to \text{IO}(\text{IORef}\ \alpha)$$
Equations
- Data.newIORef a = liftM (IO.mkRef a)
Instances For
Read the current value of an IORef.
$$\text{readIORef} : \text{IORef}\ \alpha \to \text{IO}\ \alpha$$
Equations
- Data.readIORef ref = ST.Ref.get ref
Instances For
Write a new value to an IORef, replacing the old.
$$\text{writeIORef} : \text{IORef}\ \alpha \to \alpha \to \text{IO}\ ()$$
Equations
- Data.writeIORef ref a = ST.Ref.set ref a
Instances For
Apply a function to the value in an IORef.
$$\text{modifyIORef}(\text{ref}, f) \equiv \text{ref} \gets f(\text{ref})$$
Equations
- Data.modifyIORef ref f = ST.Ref.modify ref f
Instances For
Strict version of modifyIORef. In Lean (strict evaluation) this is identical to modifyIORef.
$$\text{modifyIORef'} = \text{modifyIORef}$$
Equations
- Data.modifyIORef' ref f = ST.Ref.modify ref f
Instances For
Atomically modify an IORef, returning a derived value.
Applies $f$ to the current value, stores the first component, and returns the second.
$$\text{atomicModifyIORef}(\text{ref}, f) : \text{IO}\ \beta$$
where $f : \alpha \to (\alpha \times \beta)$
Note: not truly atomic w.r.t. concurrent access (use MVar or Mutex for that).
Equations
- Data.atomicModifyIORef ref f = do let a ← ST.Ref.get ref match f a with | (a', b) => do ST.Ref.set ref a' pure b
Instances For
Atomically modify an IORef without returning a value.
$$\text{atomicModifyIORef\_}(\text{ref}, f) \equiv \text{ref} \gets \pi_1(f(\text{ref}))$$
Equations
- Data.atomicModifyIORef_ ref f = do let __discr ← Data.atomicModifyIORef ref f have x : β := __discr pure PUnit.unit
Instances For
Atomically write a new value, returning the old.
$$\text{atomicWriteIORef}(\text{ref}, a) \equiv \text{writeIORef}(\text{ref}, a)$$
In Lean this is just writeIORef (strict evaluation makes it equivalent).
Equations
- Data.atomicWriteIORef ref a = ST.Ref.set ref a