VIP-77: Nominal brand and Refinement where on schema
Status: accepted by Will, 2026-11-30. Will: "I accept 76 to 81". (Previously open, 2026-11-28.)
Will's words
"Let's make a VIP for the Nominal brand + Refinement where on schema that's great."
Motivation
Real Effect codebases rely heavily on branded primitives (NoteId, UserId, OrgId, Email, Port, NonEmptyString) so structurally identical primitives cannot be swapped by accident. Today in VibeLang (VIP-15), schema removes Schema.Struct boilerplate for records and unions, but branded and refined primitives still drop back to verbose Effect pipelines:
export const NoteId = Schema.String.pipe(Schema.brand("NoteId"))
export type NoteId = typeof NoteId.Type
export const Port = Schema.Int.pipe(Schema.between(1, 65535))
export type Port = typeof Port.Type
export const Email = Schema.String.pipe(Schema.pattern(/^[^@\s]+@[^@\s]+\.[^@\s]+$/))
export type Email = typeof Email.TypeAdding first-class brand and where clauses to schema declarations (VIP-15) delivers:
- Zero-boilerplate nominal typing:
schema NoteId = brand stringandschema OrgId = brand stringare mutually incompatible at compile time even though both arestringat runtime. - Concise, self-documenting refinements: Ranges (
1..65535), regex literals (/^.../), and typed predicate expressions (where (s) => s.length > 0) live right inside the schema declaration and compile directly to Effect 4Schema.check(...)/Schema.brand(...). - Compile-time literal discharge & VIP-43 integration: Constant literals passed to branded/refined schema constructors (
Port(8080)vsPort(99999)) can be checked statically at compile time, andwherepredicates lower cleanly into proof obligations for VIP-43theorem/spec.
Decision
1. Grammar
VIP-77 extends SchemaDeclaration (VIP-15) with contextual brand and where clauses. Neither brand nor where is reserved globally; both remain ordinary identifiers outside schema definitions.
SchemaAliasDeclaration :
export? schema BindingIdentifier = SchemaBrand? SchemaTypeExpr SchemaWhereClause?
SchemaField :
readonly? PropertyName ?opt : SchemaBrand? SchemaTypeExpr SchemaWhereClause?
SchemaBrand :
brand StringLiteral?
SchemaWhereClause :
where SchemaConstraint ( && SchemaConstraint )*
SchemaConstraint :
NumericLiteral .. NumericLiteral
NumericLiteral ..< NumericLiteral
RegularExpressionLiteral
( BindingIdentifier ) => Expression
SimpleIdentifierComparisonExamples:
export schema NoteId = brand string
export schema OrgId = brand string
export schema Port = int where 1..65535
export schema Ratio = number where 0..<1
export schema Email = brand string where /^[^@\s]+@[^@\s]+\.[^@\s]+$/
export schema Slug = string where /^[a-z0-9-]+$/ && (s) => s.length <= 64
export schema User {
readonly id: UserId
readonly email: Email
readonly age: int where 0..150
readonly bio?: string where (s) => s.length <= 500
}intinside aschematype position lowers toSchema.Int(a refinednumbersubtype). Outsideschemadeclarations,intremains an ordinary identifier. Inside a schema type position a bareintalways meansSchema.Int: a user schema or type that is itself namedintcannot be referenced there and must be renamed or aliased. Whetherintshould instead yield to a user declaration of that name is an open decision for the VIP-77 owner.brandwithout an explicit string literal uses the declared schema's name (or"TypeName.fieldName"for inline struct fields) as the brand tag.brand "app/NoteId"allows an explicit nominal brand key for cross-package identity.- Range
a..bis closed (a <= x && x <= b); half-opena..<bisa <= x && x < b.
2. Semantics & Lowering
Every brand and where form lowers strictly to standard Effect 4 Schema combinators with zero new runtime helpers:
export const NoteId = Schema.String.pipe(Schema.brand("NoteId"))
export type NoteId = typeof NoteId.Type
export const Port = Schema.Int.pipe(
Schema.check(Schema.isBetween({ minimum: 1, maximum: 65535 }))
)
export type Port = typeof Port.Type
export const Email = Schema.String.pipe(
Schema.check(Schema.isPattern(/^[^@\s]+@[^@\s]+\.[^@\s]+$/)),
Schema.brand("Email")
)
export type Email = typeof Email.Type
export const Slug = Schema.String.pipe(
Schema.check(Schema.isPattern(/^[a-z0-9-]+$/)),
Schema.check(Schema.makeFilter((s) => s.length <= 64))
)
export type Slug = typeof Slug.TypeOrder of application: where refinements always run before Schema.brand(...), so a branded value cannot be constructed without satisfying all where predicates.
3. Construction, Decoding & Compile-Time Literal Checking
- Runtime untrusted input: Standard
Schema.decodeUnknownEffect(Email)(raw)returnsEmail ! Schema.SchemaError, preserving full Effect typed error tracking. - Typed literal construction: Calling
Port.make(8080)orEmail.make("a@b.com")validates statically when the argument is a compile-time constant literal:- Constant literals that provably violate a range (
Port.make(99999)) or regex (Email.make("not-an-email")) report compile-time diagnosticEK7704. - Non-literal runtime expressions passed to
.make(x)keep Effect's standard runtime check (and reportEK0403insideeffectcode because.makecan throw on invalid runtime values; useSchema.decodeUnknownEffect(Port)(x)for untrusted input).
- Constant literals that provably violate a range (
- Nominal incompatibility: Because
NoteIdcarriesBrand.Brand<"NoteId">andOrgIdcarriesBrand.Brand<"OrgId">, assigningOrgIdtoNoteIdor rawstringtoNoteIdis a compile error (TS2322).
4. Integration with VIP-43 (theorem / spec) and VIP-44 (durable)
whererange and pure predicate constraints automatically translate into SMT-LIB2 / Lean 4 preconditions when aschematype appears as a parameter of a VIP-43theoremorspec.- Because every
brand+whereschema lowers to a standard EffectSchema, it is automatically serializable for VIP-44 durable values, VIP-57schema serviceRPCs, VIP-78apideclarations, and VIP-80 typed SQL columns.
5. Declaration Output
.d.ts and .d.vibe.ts emit the exact lowered Schema constant and typeof Name.Type alias, so TypeScript consumers see identical branded types (string & Brand<"NoteId">) with zero VibeLang-specific .d.ts extensions.
6. Static Rules
| Mistake | Code |
|---|---|
brand or where used outside a schema type position | EK7701 |
Invalid range bound (a..b where a > b, or non-numeric bound on non-numeric base) | EK7702 |
Regex where /.../ applied to a non-string base schema | EK7703 |
Constant literal passed to .make(...) provably violates a where range or regex | EK7704 |
where predicate body performs try, await, or throw (refinements must be pure) | EK7705 |
Duplicate brand modifier on the same schema type | EK7706 |
Testing & Verification Plan
- Parser unit tests +
FuzzSchemaBrandWhereverifyingbrand/where/intremain valid identifiers outsideschemacontexts. - Differential typechecker tests proving
NoteIdvsOrgIdnominal rejection, range/regex constant-literal rejection (EK7704), and pure-predicate enforcement (EK7705). - End-to-end JSON round-trip and
Schema.decodeUnknownEffecttests intypescript/vibelang/cases/vip77-*. - Z3/Lean 4 proof check confirming
whereconstraints compose soundly with VIP-43 theorems.
Open Questions for Will
- Should
brandgenerate a package-scoped unique symbol by default (so two packages definingschema Id = brand stringnever collide) or keep the plain string tag"Id"for ergonomic cross-file interoperability? - For predicate syntax, do you prefer explicit lambda
where (s) => s.length > 0, or also an implicit placeholderwhere %.length > 0matching VIP-23 Hack pipe%?