Skip to content

Kinds

A kind says what sort of static argument a binder expects, such as a type or an index. A telescope is an ordered list of binders in which later ones may mention earlier ones (de Bruijn, 1991). For a vector indexed by length, these keep the element type and the length apart from the elements. The module gives kinds, finite enumerated index types, and declaration telescopes.

kinds

Kinds, closed index sorts, and declaration telescopes for QIEC.

TYPE module-attribute

TYPE = TypeKind()

The kind of types.

EFFECT module-attribute

EFFECT = EffectKind()

The kind of effect interface applications.

ROW module-attribute

ROW = RowKind()

The kind of effect rows.

DATA_ROW module-attribute

DATA_ROW = DataRowKind()

The kind of labelled rows of types, written Row in source.

NAT module-attribute

NAT = NatSort()

The sort of natural-number indices.

SHAPE module-attribute

SHAPE = ShapeSort()

The sort of static tensor shapes.

Kind

The kind of a static binder: a type, an effect, a row, or an arrow between kinds.

IndexSort

The sort of an index binder: naturals, shapes, contexts, or a declared sort.

TelescopeBinder

TelescopeBinder = TypeBinder | IndexBinder | EffectBinder

One binder of a declaration telescope: a type, an index, or an effect binder.

Telescope

Telescope = tuple[TelescopeBinder, ...]

The static binders a declaration takes, in order; later binders may mention earlier ones.

TypeKind didactic-model

TypeKind(*, tag: Literal['type'] = 'type')

Bases: QiecModel

The kind of types.

PARAMETER DESCRIPTION
tag

The serialization discriminator; always "type".

TYPE: Literal['type'] DEFAULT: 'type'

EffectKind didactic-model

EffectKind(*, tag: Literal['effect'] = 'effect')

Bases: QiecModel

The kind of effect interfaces.

PARAMETER DESCRIPTION
tag

The serialization discriminator; always "effect".

TYPE: Literal['effect'] DEFAULT: 'effect'

RowKind didactic-model

RowKind(*, tag: Literal['row'] = 'row')

Bases: QiecModel

The kind of effect rows.

PARAMETER DESCRIPTION
tag

The serialization discriminator; always "row".

TYPE: Literal['row'] DEFAULT: 'row'

DataRowKind didactic-model

DataRowKind(*, tag: Literal['data_row'] = 'data_row')

Bases: QiecModel

The kind of labelled rows of types.

A row of this kind maps distinct labels to types and is the shared argument of a record type and a variant type, so one row variable can stand for the fields of a record and the alternatives of a variant.

PARAMETER DESCRIPTION
tag

The serialization discriminator; always "data_row".

TYPE: Literal['data_row'] DEFAULT: 'data_row'

ArrowKind didactic-model

ArrowKind(*, domain: Kind = field(opaque=True, extras=OPAQUE_BY_VALUE), codomain: Kind = field(opaque=True, extras=OPAQUE_BY_VALUE), tag: Literal['arrow'] = 'arrow')

Bases: QiecModel

The kind of a constructor from one kind to another.

PARAMETER DESCRIPTION
domain

The kind of the argument.

TYPE: Kind DEFAULT: field(opaque=True, extras=OPAQUE_BY_VALUE)

codomain

The kind of the result.

TYPE: Kind DEFAULT: field(opaque=True, extras=OPAQUE_BY_VALUE)

tag

The serialization discriminator; always "arrow".

TYPE: Literal['arrow'] DEFAULT: 'arrow'

NatSort didactic-model

NatSort(*, tag: Literal['nat'] = 'nat')

Bases: QiecModel

The sort of natural-number indices.

PARAMETER DESCRIPTION
tag

The serialization discriminator; always "nat".

TYPE: Literal['nat'] DEFAULT: 'nat'

ShapeSort didactic-model

ShapeSort(*, rank: int | None = None, tag: Literal['shape'] = 'shape')

Bases: QiecModel

The sort of shapes, optionally restricted to a fixed rank.

PARAMETER DESCRIPTION
rank

The number of dimensions, or None for any rank. A binder of unspecified rank accepts a shape of any rank, which is what makes a rank-polymorphic signature expressible.

TYPE: int or None DEFAULT: None

tag

Discriminator for the index-sort union.

TYPE: Literal['shape'] DEFAULT: 'shape'

ContextSort didactic-model

ContextSort(*, signature: str, tag: Literal['context'] = 'context')

Bases: QiecModel

A logic-program context indexed by a stable signature name.

PARAMETER DESCRIPTION
signature

The name of the signature whose contexts inhabit this sort.

TYPE: str

tag

The serialization discriminator; always "context".

TYPE: Literal['context'] DEFAULT: 'context'

UserIndexSort didactic-model

UserIndexSort(*, name: str, constructors: tuple[str, ...] = field(opaque=True, extras=OPAQUE_BY_VALUE), arities: tuple[int, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE), tag: Literal['user'] = 'user')

Bases: QiecModel

A closed user-defined index datatype.

Constructor names are part of the sort identity. Open index sorts are deliberately absent from the first kernel because they make coverage unstable under separate compilation.

PARAMETER DESCRIPTION
name

The sort's name, part of its identity.

TYPE: str

constructors

Constructor names, unique and part of the identity. Two sorts with the same name and different constructors are different sorts, which is what keeps coverage decidable.

TYPE: tuple[str, ...] DEFAULT: field(opaque=True, extras=OPAQUE_BY_VALUE)

arities

Argument count per constructor, positionally aligned with constructors. Empty defaults to all-nullary.

TYPE: tuple[int, ...] DEFAULT: field(default=(), opaque=True, extras=OPAQUE_BY_VALUE)

tag

Discriminator for the index-sort union.

TYPE: Literal['user'] DEFAULT: 'user'

constructor_arity

constructor_arity(name: str) -> int

The number of arguments one constructor takes.

PARAMETER DESCRIPTION
name

The constructor name.

TYPE: str

RETURNS DESCRIPTION
int

Its arity, zero for a nullary constructor.

RAISES DESCRIPTION
ValueError

If the sort has no such constructor. The sort is closed, so an unknown name is an error rather than an open extension.

Source code in src/quivers/qiec/kinds.py
def constructor_arity(self, name: str) -> int:
    """The number of arguments one constructor takes.

    Parameters
    ----------
    name : str
        The constructor name.

    Returns
    -------
    int
        Its arity, zero for a nullary constructor.

    Raises
    ------
    ValueError
        If the sort has no such constructor. The sort is closed, so
        an unknown name is an error rather than an open extension.
    """
    try:
        position = self.constructors.index(name)
    except ValueError as error:
        raise ValueError(
            f"{name!r} is not a constructor of index sort {self.name!r}"
        ) from error
    return self.arities[position]

TypeBinder didactic-model

TypeBinder(*, name: str, kind: Kind = field(default=TYPE, opaque=True, extras=OPAQUE_BY_VALUE), refinable: bool = False, tag: Literal['type'] = 'type')

Bases: QiecModel

A telescope binder for a type or effect-kinded static argument.

PARAMETER DESCRIPTION
name

The binder's display name.

TYPE: str

kind

The kind of the bound argument.

TYPE: Kind DEFAULT: field(default=TYPE, opaque=True, extras=OPAQUE_BY_VALUE)

refinable

Whether case analysis may refine the bound argument; true for a family's indices and false for its uniform parameters.

TYPE: bool DEFAULT: False

tag

The serialization discriminator; always "type".

TYPE: Literal['type'] DEFAULT: 'type'

IndexBinder didactic-model

IndexBinder(*, name: str, sort: IndexSort = field(opaque=True, extras=OPAQUE_BY_VALUE), refinable: bool = False, tag: Literal['index'] = 'index')

Bases: QiecModel

A telescope binder for an index argument.

PARAMETER DESCRIPTION
name

The binder's display name.

TYPE: str

sort

The closed index sort of the bound argument.

TYPE: IndexSort DEFAULT: field(opaque=True, extras=OPAQUE_BY_VALUE)

refinable

Whether case analysis may refine the bound argument; true for a family's indices and false for its uniform parameters.

TYPE: bool DEFAULT: False

tag

The serialization discriminator; always "index".

TYPE: Literal['index'] DEFAULT: 'index'

EffectBinder didactic-model

EffectBinder(*, name: str, refinable: bool = False, tag: Literal['effect'] = 'effect')

Bases: QiecModel

A telescope binder for an effect argument.

PARAMETER DESCRIPTION
name

The binder's display name.

TYPE: str

refinable

Whether case analysis may refine the bound argument.

TYPE: bool DEFAULT: False

tag

The serialization discriminator; always "effect".

TYPE: Literal['effect'] DEFAULT: 'effect'

validate_telescope

validate_telescope(telescope: Telescope) -> None

Reject duplicate names in one dependent telescope.

PARAMETER DESCRIPTION
telescope

The binders to check, in order.

TYPE: Telescope

RAISES DESCRIPTION
ValueError

If a binder name is empty or repeats. Later binders may depend on earlier ones, so a repeated name would make a reference ambiguous.

Source code in src/quivers/qiec/kinds.py
def validate_telescope(telescope: Telescope) -> None:
    """Reject duplicate names in one dependent telescope.

    Parameters
    ----------
    telescope : Telescope
        The binders to check, in order.

    Raises
    ------
    ValueError
        If a binder name is empty or repeats. Later binders may depend on
        earlier ones, so a repeated name would make a reference ambiguous.
    """
    seen: set[str] = set()
    for binder in telescope:
        if not binder.name:
            raise ValueError("telescope binder name cannot be empty")
        if binder.name in seen:
            raise ValueError(f"duplicate telescope binder {binder.name!r}")
        seen.add(binder.name)

Cited sources

N. G. de Bruijn. Telescopic Mappings in Typed Lambda Calculus. Information and Computation, 91(2):189–204, 1991. doi:10.1016/0890-5401(91)90066-B.