Reflection
Reflect, the typed descriptions of types that the elaborator derives, the fold that consumes them, and the operations on rows and the laws that come with them.
Reflect D S describes the structure of a type as an ordinary term, which
the kernel checks. A function folds over a description to compute a value
or a type from the structure, so that codecs, storage layouts and records
of handlers are derived in Omegyu, without a builtin for each.
Reflect D S a description, whose fold gives results in Type
Reflect @p D S the same, with results in Type pD is a record of custom type formers, { f : K -> Type, … }, and S is
a family over records with those formers. A leaf D.f k becomes F.f k
in S F, and keeps its descriptor k. {=} as D means no custom
leaves. S gives types of Type; @p chooses the universe of the fold's
results, not that of the types described.
Descriptions derived from types
Where a description is expected, a type gives it: the elaborator derives it
from the type. scalars folds over a description, and counts the scalar
values that a value of the type holds: a list counts as one value, a
function as none, and a union as its largest alternative.
let scalars =
\{S : {} -> Type} (description : Reflect {=} S) ->
description.fold
(\(_ : {} -> Type) -> Natural)
{ leaf = {=}
, bool = 1
, natural = 1
, integer = 1
, double = 1
, text = 1
, word = \(bits : Natural) -> 1
, list = \A (element : Natural) -> 1
, optional = \A (value : Natural) -> value
, pair = \A B (first second : Natural) -> first + second
, function = \A B (domain codomain : Natural) -> 0
, record =
\I
row
children
ops
(fields : forall (i : I) -> Natural) ->
std:List.fold
(\(i : I) (n : Natural) -> fields i + n)
0
ops.indices
, union =
\I
row
children
ops
(alternatives : forall (i : I) -> Natural) ->
std:List.fold
(\(i : I) (n : Natural) ->
std:Natural.max (alternatives i) n)
0
ops.indices
}
in scalars
{ owner : Word 160
, limits : { low : Natural, high : Natural }
, tags : List Text
, state : < Open | Closed : { at : Natural, by : Word 160 } >
}Normal form
6A description has formers, which identifies its custom leaves, and
fold. Given P : (Families -> Type) -> Type p and one case for each kind
of type, description.fold P cases gives P S:
| Case | What it receives |
|---|---|
leaf | one function for each custom former, which takes its descriptor |
bool, natural, integer, double, text | the result for the constant family |
word | the width |
list, optional | the family of the element, and its result |
pair, function | both families, and their results |
record, union | an index type, a row family, the families of the children, the operations on the row, and the results of the children |
Custom formers are matched before their representations are inspected.
Records, unions, lists, optional values, pairs and functions nest, and the
types of std:Vec of a known length reduce to pairs and the empty record.
Dependent functions and dependent pairs need an explicit interpretation,
such as a custom former. A row that ends with an unknown variable cannot be
enumerated: a generic function takes the description from a caller whose
row is known.
Operations on rows
Each record and union case receives an index type I, a row family
R : (I -> Type) -> Row Type, and the families of the children. The same
R gives a record type { ...R F } and a union type < ...R F > for any
F : I -> Type, so one fold can derive a record of handlers and a union of
messages with the same labels, and other types at each label. The
operations are:
| Operation | What it does |
|---|---|
indices | lists every index, in the canonical order of the labels |
field | gives the label of an index, and its written position if it has one |
ordinal | gives the position of an index in the canonical order |
project, build | select a field of a record, and build a record in any monad |
inject, match | build a value of the union, and take one apart |
editRecord | gets and sets a field, with the laws that relate the two |
mapRecord, mapUnion | change the type of each field or alternative, with the laws of identity, composition and congruence |
The elaborator builds the operations and the proofs of their laws as
ordinary terms, which the kernel checks: the laws are not axioms.
std:Reflection/Row defines their types, and
its mapIso proves that inverse mappings of the children lift to inverse
mappings of records and unions.
reflect-iso.omg changes
nested field types with one fold, and proves both round trips.
The fold goes through the labels in their canonical order. The written
position of a label is separate data, an Optional Natural, which
definitions, imports and applications keep when they can. A positional
encoder checks its requirements on the written order instead of using the
canonical one; reflect-structure.omg
shows both orders.
Values with their descriptions
{ structure : Reflect D S, value : S D } pairs a description with a value
of the type that it describes. Where this type is expected, an ordinary
value gives both fields: the elaborator derives structure from the value's
static type.
let describe =
\{S : {} -> Type}
(d : { structure : Reflect {=} S, value : S {=} }) ->
d
let described = describe { active = True, names = [ "Ada" ] }
in described.valueNormal form
{ active = True, names = [ "Ada" ] }A function is described by its type, and its body is not run. A value does
not give the written order of its fields: give
{ structure = SomeType, value = someValue } when the order matters.
reflect-formers-values.omg
keeps custom leaves inside nested containers.
Adapters in Omegyu
Flat lists of fields, interpreters of values and typed interfaces of handlers are folds over these descriptions, written in Omegyu. Their rules are Omegyu code too: an interface requires each field to be a function, and a positional ABI requires a known order of the parameters. Such an adapter computes its conditions and requires a proof of them at its boundary. A valid declaration gets the proof automatically, and an invalid one has no proof, so it is a type error.