- Notifications
You must be signed in to change notification settings - Fork5
2015 007 Addition of Ref module
Author: Andreas Rossberg
Last revised: August 16, 2015
Status: proposed
Discussion:issue #8
signature REF =sigdatatyperef =datatype refval ! : 'a ref -> 'aval := : 'a ref * 'a -> unitval exchange : 'a ref * 'a -> 'aval swap : 'a ref * 'a ref -> unitval app : ('a -> unit) -> 'a ref -> unitval map : ('a -> 'b) -> 'a ref -> 'b refval modify : ('a -> 'a) -> 'a ref -> unitendstructure 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 theGeneral 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
