-
Notifications
You must be signed in to change notification settings - Fork 5
2020 001 Addition of 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.
signature UNIVERSAL
structure Universal : UNIVERSALtype 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-
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 newtagvalue. -
tagInject tag x
returns a universal value that isxtagged bytag. -
tagIs tag un
returnstrueif the universal valueunivwas formed by the expressiontagInject tag xfor somexandfalseotherwise. -
tagProject tag un
returnsxif the universal valueunivwas formed by the expressiontagInject tag xand raises theMatchexception otherwise.
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.
This addition should not affect existing code.
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.
- [2020-04-22] proposed
