-
Notifications
You must be signed in to change notification settings - Fork 5
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 theGeneralstructure. -
r := a
Makes the referencerrefer to valuea. This function is the same as the global:=operator and is also part of theGeneralstructure. -
exchange (r, a)
Makes the referencerrefer to valueaand returns the previously referenced value in a single operation. -
swap (r1, r2)
Swaps the values referred to by the referencesr1andr2. -
app f r
Applies the functionfto the value referred to by the referencer. Equivalent tof (!r). -
map f r
Creates a new reference that refers to the valuef a, whereais the value referred to to byr. Equivalent toref (f (!r)). -
modify f r
Makes the referencerrefer to the valuef a, whereais 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
Generalstructure. [JHR]. -
[2015-08-16] Proposed
