Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

2015 007 Addition of Ref module

John Reppy edited this pageAug 22, 2015 ·4 revisions

Proposal 2015-007

Addition ofRef module

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


Synopsis

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

Description

  • 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).

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 theGeneral 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 theGeneral structure.[JHR].

  • [2015-08-16] Proposed


Creative Commons License
This document is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/).

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp