-
Notifications
You must be signed in to change notification settings - Fork 4
2015 007 Addition of Ref module
Author: Andreas Rossberg
Last revised: August 16, 2015
Status: proposed
Discussion: issue #8
signature REF =
sig
datatype ref = datatype ref
val ! : 'a ref -> 'a
val := : 'a ref * 'a -> unit
val exchange : 'a ref * 'a -> 'a
val swap : 'a ref * 'a ref -> unit
val app : ('a -> unit) -> 'a ref -> unit
val map : ('a -> 'b) -> 'a ref -> 'b ref
val modify : ('a -> 'a) -> 'a ref -> unit
end
structure Ref : REF
-
datatype 'a ref
The type of mutable references (same as the global typeref
). -
! r
Returns the value referred to byr
. This function is the same as the global!
operator and is also part of theGeneral
structure. -
r := a
Makes the referencer
refer to valuea
. This function is the same as the global:=
operator and is also part of theGeneral
structure. -
exchange (r, a)
Makes the referencer
refer to valuea
and returns the previously referenced value in a single operation. -
swap (r1, r2)
Swaps the values referred to by the referencesr1
andr2
. -
app f r
Applies the functionf
to the value referred to by the referencer
. Equivalent tof (!r)
. -
map f r
Creates a new reference that refers to the valuef a
, wherea
is the value referred to to byr
. Equivalent toref (f (!r))
. -
modify f r
Makes the referencer
refer to the valuef a
, wherea
is the value previously referred to. This expression is equivalent tor := f (!r)
.
This module provides a few combinators that are sometimes useful when applying higher-order functions to data structures containing references.
It also provides a natural home for the existing toplevel operators, but they would also remain in the General
structure for backward compatibility.
The addition of this module does not affect existing programs.
-
[2015-08-22] Added some text about the operators that are also in the
General
structure. [JHR]. -
[2015-08-16] Proposed