Repository navigation
Structured Data (Future)
This document proposes surface syntax and core semantics for structured data in Curlee.
Status: future design (not part of MVP syntax).
Goals:
- Provide user-defined product types (
struct) and sum types (enum). - Keep the initial feature set small and unambiguous.
- Keep tooling and error recovery strong (explicit delimiters; minimal implicit rules).
- Avoid expanding the MVP verification fragment by accident.
Non-goals (initial implementation):
- No generics / type parameters.
- No methods /
implblocks. - No inheritance / subtyping.
- No structural typing (types are nominal).
- No mutation syntax is introduced by this feature.
Top-level only:
struct Point {
x: Int;
y: Int;
}
Rules:
- Fields are declared as
name: Type;. - Field names must be unique within the struct.
- For the first implementation, structs are nominal and closed (no dynamic field addition).
Top-level only:
enum OptionInt {
None;
Some(Int);
}
Rules:
- Variants are declared as either:
- unit:
VariantName; - single-payload tuple:
VariantName(Type);
- unit:
- Variant names must be unique within the enum.
- Multi-payload variants and struct-style variants are out-of-scope initially.
Struct literals:
let p: Point = Point{ x: 1, y: 2 };
Enum constructors:
let a: OptionInt = OptionInt::None;
let b: OptionInt = OptionInt::Some(123);
Rules:
- A struct literal starts with the struct name followed by
{ ... }. - A struct literal must provide each field exactly once.
- Missing field: error.
- Extra/unknown field: error.
- Duplicate field: error.
- Field order is not significant.
- Enum constructors use
EnumName::VariantName.- For unit variants:
Enum::V. - For payload variants:
Enum::V(expr).
- For unit variants:
let x: Int = p.x;
Rules:
-
expr.fieldis valid only ifexprhas a struct type that declaresfield. - Accessing a missing field is an error.
A match statement dispatches on an enum value and introduces variant payload bindings.
match opt {
OptionInt::None => { return 0; };
OptionInt::Some(v) => { return v; };
}
Rules:
-
matchis initially a statement (not an expression). Each arm body is a block. - Arms are separated by
;to aid parse recovery. - Patterns are restricted to:
-
Enum::Variant(unit variant) -
Enum::Variant(binding)(single payload) -
_(wildcard catch-all)
-
- Arms must be exhaustive for the matched enum.
- Exhaustiveness can be satisfied either by listing all variants or by including
_.
- Exhaustiveness can be satisfied either by listing all variants or by including
- Duplicate patterns are rejected.
Out-of-scope initially:
- Match guards (
if ...). - Nested/destructuring patterns (e.g.
Some(Point{...})). - Refutable patterns in
letbindings.
-
structandenumintroduce nominal types. - Two types are equal iff they refer to the same declared name (after name resolution).
Given struct S { f1: T1; ... fn: Tn; }, a literal S{ ... }:
- Each field initializer expression must type-check and be assignable to the declared field type.
- The literal’s type is
S.
If e has type S and S declares field f: T, then e.f has type T.
Otherwise:
- If
eis not a struct type: error. - If
fdoes not exist: error.
Given enum E { V0; V1(T); }:
-
E::V0has typeE. - If
xhas typeT, thenE::V1(x)has typeE.
If e has type E where E is an enum:
- Each pattern must refer to a variant of
E(or_). - For
E::V(binding)the binding is introduced in the arm’s block with the variant payload type.
Exhaustiveness:
- The set of patterns must cover all variants of
E, or include_.
Type errors:
-
matchon a non-enum type is an error. - Using a pattern from a different enum is an error.
To avoid scope drift relative to Verification Scope:
- Contracts/refinements (
requires/ensures/where) remain restricted to the MVP logic fragment. - For the first implementation, structured values are not permitted inside verification predicates.
- Example:
requires p.x > 0is rejected until we explicitly define a solver encoding for field projection.
- Example:
(If we later choose to support this, the encoding and constraints must be specified and reflected in Verification Scope.)
struct Point {
x: Int;
y: Int;
}
fn bad_point() -> Int {
let p: Point = Point{ x: 1 };
return p.y;
}
Expected errors:
- Struct literal missing required field
y.
fn bad_field() -> Int {
let p: Point = Point{ x: 1, y: 2, z: 3 };
return 0;
}
Expected errors:
- Struct literal contains unknown field
z.
enum OptionInt {
None;
Some(Int);
}
enum Other {
A;
}
fn bad_match(opt: OptionInt) -> Int {
match opt {
OptionInt::Some(v) => { return v; };
}
}
Expected errors:
- Non-exhaustive match: missing
OptionInt::Nonearm.
fn wrong_enum_pattern(opt: OptionInt) -> Int {
match opt {
Other::A => { return 0; };
_ => { return 1; };
}
}
Expected errors:
- Pattern
Other::Adoes not belong to matched enumOptionInt.
In-scope (first structured-data implementation):
- Parsing and AST for top-level
structandenumdeclarations. - Name resolution for declared types and enum variants.
- Type checking for struct literals, field access, enum constructors.
- Type checking for
matchstatements, including exhaustiveness and duplicate-arm detection. - Diagnostics with precise spans for:
- missing/extra/duplicate fields
- unknown field access
- non-exhaustive matches
- wrong-enum patterns
Out-of-scope (first implementation):
- Generics.
- Methods.
- Nested patterns.
- Match guards.
- Using field projection in verification predicates.