dreamstack/examples/refined-types.ds
enzotar 9ef28bb53a feat: dependent types — refinement types, type aliases, type annotations
- Lexer: added 'type' and 'where' keywords
- AST: TypeExpr::Refined, Declaration::TypeAlias, LetDecl.type_annotation
- Parser: parse_type_alias_decl, parse_type_expr (Named, Generic, where)
- Type system: Type::Refined, Predicate/PredicateExpr with evaluate_static()
- Errors: RefinementViolation, TypeAliasCycle (Elm-style messages)
- Checker: type alias registry, resolve_type_expr, ast_to_predicate,
  static refinement checking for literal values
- Codegen: Phase 1b runtime guards, predicate_to_js helper

Syntax: type PositiveInt = Int where value > 0
        let count: PositiveInt = 5  -- static check passes
        let count: PositiveInt = -1 -- compile error

118 tests pass (16 in ds-types, 5 new for refinements)
2026-02-26 11:09:33 -08:00

27 lines
715 B
Text

-- DreamStack Dependent Types Example
-- Demonstrates refinement types, type aliases, and type annotations
-- Type aliases with refinement predicates
type PositiveInt = Int where value > 0
type Percentage = Float where value >= 0.0
-- Basic type annotations
let count: Int = 0
let name: String = "hello"
-- Annotated with refinement type alias
let priority: PositiveInt = 1
let progress: Percentage = 75.0
-- Derived values don't need annotation (inferred)
let doubled = count * 2
let greeting = "Welcome, {name}!"
view main = column [
text "Count: {count}"
text "Priority: {priority}"
text "Progress: {progress}%"
text greeting
button "+" { click: count += 1 }
button "-" { click: count -= 1 }
]