Skip to content

Structured Data (Future)

Joe Curlee (w4ffl35) edited this page Jan 30, 2026 · 1 revision

Structured Data (Future): struct and enum

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 / impl blocks.
  • No inheritance / subtyping.
  • No structural typing (types are nominal).
  • No mutation syntax is introduced by this feature.

1) Surface syntax

1.1 struct declarations

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

1.2 enum declarations

Top-level only:

enum OptionInt {
  None;
  Some(Int);
}

Rules:

  • Variants are declared as either:
    • unit: VariantName;
    • single-payload tuple: VariantName(Type);
  • Variant names must be unique within the enum.
  • Multi-payload variants and struct-style variants are out-of-scope initially.

1.3 Construction

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

1.4 Field access

let x: Int = p.x;

Rules:

  • expr.field is valid only if expr has a struct type that declares field.
  • Accessing a missing field is an error.

1.5 Pattern matching (initially in-scope for enum)

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:

  • match is 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 _.
  • Duplicate patterns are rejected.

Out-of-scope initially:

  • Match guards (if ...).
  • Nested/destructuring patterns (e.g. Some(Point{...})).
  • Refutable patterns in let bindings.

2) Static semantics (type checking)

2.1 Type identity

  • struct and enum introduce nominal types.
  • Two types are equal iff they refer to the same declared name (after name resolution).

2.2 Struct literals

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.

2.3 Field access

If e has type S and S declares field f: T, then e.f has type T.

Otherwise:

  • If e is not a struct type: error.
  • If f does not exist: error.

2.4 Enum constructors

Given enum E { V0; V1(T); }:

  • E::V0 has type E.
  • If x has type T, then E::V1(x) has type E.

2.5 match

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:

  • match on a non-enum type is an error.
  • Using a pattern from a different enum is an error.

3) Verification interaction (initial stance)

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 > 0 is rejected until we explicitly define a solver encoding for field projection.

(If we later choose to support this, the encoding and constraints must be specified and reflected in Verification Scope.)


4) Error cases (examples)

Example A: struct literal missing/unknown fields

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.

Example B: enum match non-exhaustive and wrong pattern

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::None arm.
fn wrong_enum_pattern(opt: OptionInt) -> Int {
  match opt {
    Other::A => { return 0; };
    _ => { return 1; };
  }
}

Expected errors:

  • Pattern Other::A does not belong to matched enum OptionInt.

5) First implementation scope checklist

In-scope (first structured-data implementation):

  • Parsing and AST for top-level struct and enum declarations.
  • Name resolution for declared types and enum variants.
  • Type checking for struct literals, field access, enum constructors.
  • Type checking for match statements, 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.

Clone this wiki locally