Skip to content

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.Type

Adding first-class brand and where clauses to schema declarations (VIP-15) delivers:

  1. Zero-boilerplate nominal typing: schema NoteId = brand string and schema OrgId = brand string are mutually incompatible at compile time even though both are string at runtime.
  2. 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 4 Schema.check(...) / Schema.brand(...).
  3. Compile-time literal discharge & VIP-43 integration: Constant literals passed to branded/refined schema constructors (Port(8080) vs Port(99999)) can be checked statically at compile time, and where predicates lower cleanly into proof obligations for VIP-43 theorem / 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
  SimpleIdentifierComparison

Examples:

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
}
  • int inside a schema type position lowers to Schema.Int (a refined number subtype). Outside schema declarations, int remains an ordinary identifier. Inside a schema type position a bare int always means Schema.Int: a user schema or type that is itself named int cannot be referenced there and must be renamed or aliased. Whether int should instead yield to a user declaration of that name is an open decision for the VIP-77 owner.
  • brand without 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..b is closed (a <= x && x <= b); half-open a..<b is a <= 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.Type

Order 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) returns Email ! Schema.SchemaError, preserving full Effect typed error tracking.
  • Typed literal construction: Calling Port.make(8080) or Email.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 diagnostic EK7704.
    • Non-literal runtime expressions passed to .make(x) keep Effect's standard runtime check (and report EK0403 inside effect code because .make can throw on invalid runtime values; use Schema.decodeUnknownEffect(Port)(x) for untrusted input).
  • Nominal incompatibility: Because NoteId carries Brand.Brand<"NoteId"> and OrgId carries Brand.Brand<"OrgId">, assigning OrgId to NoteId or raw string to NoteId is a compile error (TS2322).

4. Integration with VIP-43 (theorem / spec) and VIP-44 (durable)

  • where range and pure predicate constraints automatically translate into SMT-LIB2 / Lean 4 preconditions when a schema type appears as a parameter of a VIP-43 theorem or spec.
  • Because every brand + where schema lowers to a standard Effect Schema, it is automatically serializable for VIP-44 durable values, VIP-57 schema service RPCs, VIP-78 api declarations, 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

MistakeCode
brand or where used outside a schema type positionEK7701
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 schemaEK7703
Constant literal passed to .make(...) provably violates a where range or regexEK7704
where predicate body performs try, await, or throw (refinements must be pure)EK7705
Duplicate brand modifier on the same schema typeEK7706

Testing & Verification Plan

  • Parser unit tests + FuzzSchemaBrandWhere verifying brand/where/int remain valid identifiers outside schema contexts.
  • Differential typechecker tests proving NoteId vs OrgId nominal rejection, range/regex constant-literal rejection (EK7704), and pure-predicate enforcement (EK7705).
  • End-to-end JSON round-trip and Schema.decodeUnknownEffect tests in typescript/vibelang/cases/vip77-*.
  • Z3/Lean 4 proof check confirming where constraints compose soundly with VIP-43 theorems.

Open Questions for Will

  1. Should brand generate a package-scoped unique symbol by default (so two packages defining schema Id = brand string never collide) or keep the plain string tag "Id" for ergonomic cross-file interoperability?
  2. For predicate syntax, do you prefer explicit lambda where (s) => s.length > 0, or also an implicit placeholder where %.length > 0 matching VIP-23 Hack pipe %?