# Title

Minimal Unique Types

# Author

John Cowan, Marc Nieper-Wißkirchen

# Status

This SRFI is currently in final status. Here is an explanation of each status that a SRFI can hold. To provide input on this SRFI, please send email to `srfi-137@nospamsrfi.schemers.org`. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.

- Received: 2016/6/27
- 60-day deadline: 2016/8/26
- Draft #1 published: 2016/6/27
- Draft #2 published: 2016/10/4
- Finalized: 2016/10/4
- Revised to fix errata:
  - 2016/10/5 (Fixed text under `make-reia-subtype`.)
  - 2016/11/7 (Fixed text under `make-reia-subtype` again.)

# Abstract

This SRFI is intended to standardize a primitive run-time mechanism to create disjoint types.

# Rationale

This SRFI provides a simple hook to create new data types at run time that are disjoint from all existing types. allowing portable libraries to implement SRFI 9, SRFI 99, SRFI 131, SRFI 135, R6RS records, Chicken records, CLOS, persistent databases, remote access to data on servers, and the like on top of it. It is also portably implementable and usable entirely separately from any of these.

Note that there is no concept of a type object here: a type is simply a name for a group of closely linked procedures that allow the creation and manipulation of type instances (which are objects) and subtypes. This SRFI exposes no ambient authority, and relies entirely on module exports for access control. It is based on a less radical proposal by Alaric Snell-Pym, UniqueTypesSnellPym.

John Cowan is the author and shepherd of this SRFI. Marc Nieper-Wißkirchen is the author of the sample implementation.

# Specification

### Make-type

`(make-type` type-payload`)` → type-accessor constructor predicate accessor make-subtype

Calling `make-type` on type-payload, which can be any Scheme object, returns five values, all of which are procedures. They are distinct (in the sense of `eqv?`) from each other and from any other procedures returned by other calls to `make-type`. In brief, the five functions:

- return type-payload

- return newly allocated objects of a disjoint type known as instances, each associated with an instance payload

- return `#t` iff an object is an instance of this type

- return the instance payload

- return five more procedures associated with a subtype of this type

Details are given for a sample type in the next section. The type payload might contain metadata (such as field names or class variables) associated with the type as a whole.

## Sample procedures for a type

For the purposes of this section, we will suppose that

    (define-values (reia-metadata make-reia reia? reia-ref make-reia-subtype) (make-type 'reia))

has been evaluated, and document each of the five variables that it binds. "Reia" is an acronym for "remarkably 'evil' in appearance", and has no particular significance. Fnord!

`(reia-metadata)` → object

Returns the symbol `reia`.

`(make-reia` instance-payload`)`→ reia

Returns a newly allocated instance associated with instance-payload. This association is single and immutable, but it is possible to make use of an appropriate container payload in order to effectively associate the instance with more than one value. To make the association effectively mutable, use a mutable payload such as a box, list or vector. Instances belong to a type that is disjoint from any existing Scheme type, including types created by other calls to `make-type`.

`(reia?` object`)`→ boolean

Returns `#t` iff object was returned by a call to `make-reia` or any constructor created as part of a direct or indirect subtype of the `reia` type.

`(reia-ref` reia`)`→ object

Returns the instance payload of reia. It is an error if reia does not satisfy `reia?`.

`(make-reia-subtype` type-payload`)`→ type-accessor constructor predicate accessor make-subtype

Returns five new procedures with the same semantics as `make-type`, such that the objects returned by constructor satisfy `reia?` and their payload can be accessed using `reia-ref`.

# Implementation

The sample implementation of this SRFI is in the associated repository. Note that the predicate functions are O(k) where k is the depth of the type tree. An O(1) implementation is possible at the expense of making the creation of subtypes O(k).

# Copyright

Copyright (C) John Cowan, Marc Nieper-Wißkirchen (2016). All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.