Skip to content

2020 001 Addition of Universal module

John Reppy edited this page Apr 23, 2020 · 3 revisions

Proposal 2020-001

Addition of the Universal module

Author: John Reppy
Last revised: April 22, 2020
Status: proposed
Discussion: issue #27


This proposal recommends adding the Universal structure provided by Poly/ML to the SML Basis Library. The one significant change is to include the definition of the UNIVERSAL signature, which is not part of the Poly/ML design.

Synopsis

signature UNIVERSAL
structure Universal : UNIVERSAL

Interface

type universal

type 'a tag

val tag : unit -> 'a tag
val tagInject : 'a tag -> 'a -> universal
val tagIs : 'a tag -> universal -> bool
val tagProject : 'a tag -> universal -> 'a

Description

  • type universal
    The universal tagged-union type.

  • type 'a tag
    is the type of a tag for values with type 'a. Note that it is possible to define multiple tags with the same argument type; these will be distinct.

  • tag ()
    returns a new tag value.

  • tagInject tag x
    returns a universal value that is x tagged by tag.

  • tagIs tag un
    returns true if the universal value univ was formed by the expression tagInject tag x for some x and false otherwise.

  • tagProject tag un
    returns x if the universal value univ was formed by the expression tagInject tag x and raises the Match exception otherwise.

Discussion

The interface is taken directly from the PolyML documentation. It might make sense to either change the return type of tagProject to return an option, instead of raising the Match exception, or to add an additional projection function with an option return type.

Impact

This addition should not affect existing code.

Rationale

There are a number of examples where having a universal union type can be useful (Stephan Weeks's property lists is a good example). While such needs can be addressed using the exn type, the Universal structure provides a cleaner interface to such a mechanism. Furthermore, while exceptions are one way to implement this facility, implementations may provide more efficient implementations with native compiler support.


History

  • [2020-04-22] proposed

Clone this wiki locally