Skip to content

Commit

Permalink
[ base ] Add modifyRef into the Ref interface
Browse files Browse the repository at this point in the history
  • Loading branch information
buzden authored and gallais committed Dec 8, 2023
1 parent 8557b5b commit eddcbcd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@

* Adds `Data.Vect.foldrImplGoLemma`.

* `Ref` interface from `Data.Ref` inherits `Monad` and was extended by a function
for value modification implemented through reading and writing by default.

#### System

* Changes `getNProcessors` to return the number of online processors rather than
Expand Down
12 changes: 11 additions & 1 deletion libs/base/Data/Ref.idr
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ import Control.Monad.State.Interface
%default total

public export
interface Ref m r | m where
interface Monad m => Ref m r | m where
newRef : {0 a : Type} -> a -> m (r a)
readRef : {0 a : Type} -> r a -> m a
writeRef : r a -> a -> m ()

||| Updates a value and returns the previous value
modifyRef : (a -> a) -> r a -> m a
modifyRef f ref = do
old <- readRef ref
writeRef ref (f old) $> old

public export
modifyRef_ : Ref m r => (a -> a) -> r a -> m ()
modifyRef_ = ignore .: modifyRef

export
HasIO io => Ref io IORef where
newRef = newIORef
Expand Down

0 comments on commit eddcbcd

Please sign in to comment.