64. type always makes a new type: let length = type u32 is distinct from u32, converts only by applying a type to a value, and inherits only what it derives¶
Date: 2026-11-24
Status: Proposed. Dean asked for strong type aliases (2026-11-24), and chose the spelling in section 1, type always makes a new type, over a separate distinct word (2026-11-24).
Context¶
Reading the tour, Dean asked for strong type aliases: after let length = type u32, a length is a type of its own, never mixed up with a u32 or with another type over u32, and every conversion is an explicit function call. He suggested a metaprogram could make the conversion functions.
Today let byte = type u8 is another name for u8 (irgen.md): types are structural, so u64 and usize are one type and int{0..255} is u8. Records and variants are the exception. They are named, so two records with the same fields are different types. The language has no way to give a scalar, a range or an existing record a name that the type checker keeps apart, so a function area (w : u32) (h : u32) cannot stop a caller swapping its arguments, and a u32 of pixels can be added to a u32 of bytes.
The questions to settle are how it is spelled, whether the underlying type's bounds and contracts carry over, which operators and concepts the new type gets, how it meets types as values, concepts, error sets, @derive(json) and the ABI, and what it costs at run time and in proofs.
Decision¶
1. type makes a new type; a let of a type names one¶
let length = type u32 (* a new type, distinct from u32 *)
let percent = type int{0..100} (* a new type over a range *)
let byte = u8 (* another name for u8, as before *)
type already makes a new type when it is followed by a record or a variant. This ADR makes that the whole rule: type e is a new type whose values are the values of e, whatever e is. Since types are values (ADR 0002), naming an existing type needs no keyword at all: let byte = u8 binds the type u8 to a second name, as let y = x binds a value. That is how a transparent alias is spelled from now on.
No keyword is added. The existing aliases spelled with type (about ten of them in tests, docs/theories.md and ADR 0051's example, such as let blk = type int{0..63}) become let blk = int{0..63} in the same change that flips the meaning, so their programs do not change. max byte and min byte keep working for an alias (ADR 0059).
On the right of let, a type expression with no type is an alias: a type name, a range int{lo..hi} (written with no space before {, by the adjacency rule of ADR 0019), an applied generic such as option u8, or a type-level metaprogram's call (ADR 0035). Sema tells it apart from a value binding because it names a type.
2. It is built by a prelude metaprogram¶
The expander rewrites let n = type e, where e is not a record or variant literal, into a call of the prelude metaprogram distinct, which returns:
let length = type { raw#length : u32 } @distinct
let length#make (x : u32) : length ensures length#raw result = x = { raw#length = x }
let length#raw (x : length) : u32 ensures result = x.raw#length = x.raw#length
- A one-field record. Records are already nominal through Sema, IRGen, generics, interfaces and certificates, so a distinct type reuses all of it. The field's and the functions' names carry a
#, which no source name can, so no program can writel.raw; the only ways in and out are the conversions below. - A type is called to convert (Dean, 2026-11-24). Types are values, and applying one to a value converts it:
length 5makes alength, IRGen's call oflength#make, andu32 ltakes one apart, a call oflength#raw. Both are explicit, both read the same in code and in contracts, and no new names are made. Applying the underlying type unwraps exactly one layer: forlet width = type length,length wgives thelengthandu32 (length w)the number. The result ofu32 lwidens implicitly like anyu32, sou64 lisu32 lwidened; applying a narrower type,u8 l, is the narrowing goal it would be for theu32. Applying a type to a value that is neither of the type's underlying type nor of a distinct type over it is an error. A module may leave the conversions out of its interface (section 6), and a type is not a function value yet:map length xsis an error that suggestsmap (let (x) => length x) xs. - Names. Named functions,
to_lengthandfrom_lengthafter the library'sto_chars, were the first proposal. Dean chose calling the type (2026-11-24), which adds no names and reads the same both ways. @distinctmarks the record so that the rest of the compiler can tell a distinct type from a hand-written one-field record: diagnostics printlength (distinct u32), derives write the underlying value (section 4), and the metaprogram builtinunderlying treturnsefor a distinct type and unit for any other.
distinct is ordinary Pia in src/syntax/Prelude.cpp, like derive_eq and machine, and a program can call it itself.
3. Bounds and contracts carry over; conversions are explicit both ways¶
- Bounds. The field has the underlying type, so a
percentholds 0 to 100 and the prover knows it about everypercent, for free, as for the range itself.percent xhas the narrowing goalxfitsint{0..100}at its argument, as any call does, andint{0..100} p(oru8 p, widened) returns anint{0..100}, which widens implicitly to anything larger. - Contracts.
length#rawis a pure function of scalars, so in a contractu32 lmeans its body (tests/verify/pure_calls.pia), andrequires u32 w * u32 h <= max u32reads and proves as the scalar version did. - No implicit conversion, in either direction.
let l : length = 5is an error that suggestslength 5, au32where alengthis expected is an error that suggestslength x, and alengthwhere au32is expected suggestsu32 l. Two distinct types over one type never convert into each other except through the underlying type,width (u32 l). minandmax.max lengthis an error that suggestslength (max u32): the bounds are values of the underlying type, and folding them to a bare literal would be the implicit conversion this ADR rules out.@derive(bounds)(section 4) givesmin_lengthandmax_lengthas values oflength.
4. Nothing is inherited; operators come from derives¶
A distinct type starts with no operators, no equality in code, and meets no concept. This is the point of it: a length that could be added to itself could also be multiplied by itself, and length times length is an area. Each family is opted into with a derive, which writes ordinary overloads on the record:
| Derive | Gives | Meaning |
|---|---|---|
eq |
=, <> |
the existing derive_eq: compares the underlying values |
ord |
<, <=, >, >= |
compares the underlying values, each with a postcondition that says so |
add |
+, - between two values of the type |
results of the type; overflow is a goal as for the underlying type (ADR 0004) |
scale |
*, /, % by the underlying type, on the right |
length * u32 : length, never length * length |
bounds |
min_n, max_n |
the underlying type's bounds, as values of the type |
hash |
an instance of Pia.Core.Hash |
the underlying value's hash; needs eq |
json |
write_json_n, read_json_n and the Json instance |
the underlying value, not an object: a length is the JSON number 5, read with the underlying type's range check |
let length = type u32 @derive(eq, ord, add, scale)
A derive's postconditions are written in terms of the underlying values, u32 a, so callers prove facts about a + b the way they would about the underlying numbers. Concepts follow from the functions: a concept whose signature ord meets is met by any distinct type that derives it, and an adapter (ADR 0009) covers anything else. A program can write any operator itself, such as let mul (a : length) (b : length) : area, which is how units are built (theories.md, "Units").
@derive(add) on a distinct type over a type with no add, such as a record, is an error at the derive, which names the missing function.
5. Types as values, generics, error sets¶
- Types as values.
lengthis a type value like any record:option length,[length; 8],vec lengthand a generic's(t : type)all take it. A generic over(t : type where Ord t)acceptslengthwhen it derivesord. - Over records and variants.
let celsius = type readingis a distinct type over the recordreading. It does not copyreading's fields or constructors (constructors are one namespace, and copies would clash); it wraps onereading, reached asreading c. - Error sets. An error set's members are variants (ADR 0028), so a distinct type is not one, even over a variant. A handler matches the variant itself. Making a distinct type over a variant usable as an error set member is left until someone needs it.
- Distinct of distinct.
let width = type lengthwraps alength:width ltakes alength, not au32.
6. Interfaces, certificates and abstract types¶
- Interfaces. An interface shows a distinct type as it is defined,
let length = type u32, and exports the functions it chooses. The.piaicarries the@distinctrecord, so importers agree on its layout, and the interface hash covers the type's name and its underlying type: changingu32tou64changes the hash and the importers' witnesses (ADR 0024). - Abstract types. An interface that lists
let length : typeand exports one conversion but not the other, or a checked constructor in place oflength x, keeps the type's values under the module's control. This is what makes a distinct type with an invariant possible (asortedslice, anonzerodivisor). Abstract types are not importable yet (ADR 0039); this ADR does not change that, and invariant-carrying distinct types are a later ADR.
7. Zero cost¶
- Proofs.
transforms::scalarizeRecordsreplaces each record with its fields before the verifier runs (irgen.md), so a proof about alengthis the proof about au32, with the same goals and cost. Conversions are calls of pure scalar functions, which the verifier unfolds. - Code. After scalarization a
lengthis au32: a buffer oflengthis one buffer ofu32, and the conversions inline away. - The C ABI. A record parameter is already passed as its fields. A record result is written through an out-pointer today (codegen.md); this ADR makes a record of one scalar field return that scalar instead. That is what C does for a
struct { uint32_t v; }on arm64 and on x86_64 System V, so the entry point is the same as for the underlying type, and also what a C caller expects for a one-field struct.
Alternatives¶
- A
distinctword, keepingtype u32as an alias (let length = distinct u32). Additive, with nothing to migrate, but it adds a word for somethingtypealready means for records and variants, and leaves two spellings of "name a type" (type u8andu8). Dean chosetypeinstead (2026-11-24). - A core nominal type in Sema and the dialect (
!pia.distinct<"length", ui32>). Every pass that compares types would learn a new case, and the verifier and codegen would need to strip it, for the same result the record gives already. - One-constructor variants (
type | Length of u32). Nominal today, but a variant carries a tag, matching is the only way out, and a derive would have to treat one-constructor variants specially. - Inheriting everything the underlying type has. Rejected: it re-admits
length * length : length, which is the class of bug the feature exists to stop. Haskell'sGeneralizedNewtypeDerivingand Rust's newtype pattern both make derivation explicit for this reason. - Implicit conversion from literals.
let l : length = 5would be pleasant, but it is a conversion the reader cannot see, and it makeslength xoptional in exactly the places (constants, defaults) where units are most often wrong.
Consequences¶
Implementation, one PR each:
- Aliases spelled
let byte = u8. Sema accepts aletwhose right side is a type expression as an alias,type <scalar>keeps meaning an alias for now, and every existingtype-spelled alias in the repository moves to the new spelling. Tests:tests/verify/type_aliases.pia,tests/irgen/alias_errors.pia. type eis distinct. The prelude'sdistinct, the@distinctattribute, theunderlyingbuiltin, calls of a type as conversions, the diagnostics that suggest them, and one-scalar-field records returned as the scalar in the C ABI. Tests:tests/verify/distinct_types.pia,tests/irgen/distinct_errors.pia,tests/codegen/distinct_abi.pia,tests/meta/distinct.pia.- Derives.
ord,add,scaleandboundsin the prelude, andeqchecked on distinct types. Tests:tests/verify/distinct_derives.pia. jsonandhash.derive_jsonandPia.Core.Hashwrite and hash the underlying value. Tests intests/std.
Docs: the tour's Records section and docs/irgen.md (types), docs/meta.md (distinct, underlying, the new derives), docs/grammar.md (aliases), and the pia-language skill. ADR 0059's let byte = type u8 example becomes let byte = u8.
Open questions:
- Whether
@derive(add)should also give+|and-|(saturating) when the underlying type has them. Proposed: yes, as one family.