2015 007 Addition of Ref module

John Reppy edited this page Aug 22, 2015 · 4 revisions

Proposal 2015-007

Addition of Ref module

Author: Andreas Rossberg
Last revised: August 16, 2015
Status: proposed
Discussion: issue #8


Synopsis

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

Description

  • datatype 'a ref
    The type of mutable references (same as the global type ref).

  • ! r
    Returns the value referred to by r. This function is the same as the global ! operator and is also part of the General structure.

  • r := a
    Makes the reference r refer to value a. This function is the same as the global := operator and is also part of the General structure.

  • exchange (r, a)
    Makes the reference r refer to value a and returns the previously referenced value in a single operation.

  • swap (r1, r2)
    Swaps the values referred to by the references r1 and r2.

  • app f r
    Applies the function f to the value referred to by the reference r. Equivalent to f (!r).

  • map f r
    Creates a new reference that refers to the value f a, where a is the value referred to to by r. Equivalent to ref (f (!r)).

  • modify f r
    Makes the reference r refer to the value f a, where a is the value previously referred to. This expression is equivalent to r := f (!r).

Rationale

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.

Impact

The addition of this module does not affect existing programs.


History

  • [2015-08-22] Added some text about the operators that are also in the General structure. [JHR].

  • [2015-08-16] Proposed


You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.