docs: add demos and update documentation for new features

Documentation:
- Update IMPLEMENTATION_PLAN.md with current status (222 tests)
- Update feature comparison table (Schema Evolution, Behavioral Types: )
- Add HttpServer to built-in effects list
- Update OVERVIEW.md with working behavioral types examples

Demo Programs:
- examples/schema_evolution.lux - Version annotations, constraints, runtime ops
- examples/behavioral_types.lux - pure, deterministic, commutative, idempotent, total

Sample Project:
- projects/rest-api/ - Full REST API demo with:
  - Task CRUD endpoints
  - Pattern matching router
  - JSON serialization
  - Effect-tracked request handling

Tests:
- Add behavioral type tests (pure, deterministic, commutative, idempotent, total)
- 227 tests passing

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-13 22:14:55 -05:00
parent 086552b7a4
commit 705bd57e81
7 changed files with 699 additions and 43 deletions

View File

@@ -0,0 +1,121 @@
// Behavioral Types Demo
// Demonstrates compile-time verification of function properties
// ============================================================
// PART 1: Pure Functions
// ============================================================
// Pure functions cannot have any effects
fn add(a: Int, b: Int): Int is pure = a + b
fn multiply(a: Int, b: Int): Int is pure = a * b
fn square(x: Int): Int is pure = x * x
// Composition of pure functions is pure
fn sumOfSquares(a: Int, b: Int): Int is pure =
add(square(a), square(b))
// ============================================================
// PART 2: Deterministic Functions
// ============================================================
// Deterministic functions always return the same output for the same input
// They cannot use Random or Time effects
fn factorial(n: Int): Int is deterministic =
if n <= 1 then 1 else n * factorial(n - 1)
fn fibonacci(n: Int): Int is deterministic =
if n <= 1 then n else fibonacci(n - 1) + fibonacci(n - 2)
// ============================================================
// PART 3: Commutative Functions
// ============================================================
// Commutative functions: f(a, b) = f(b, a)
fn max(a: Int, b: Int): Int is commutative =
if a > b then a else b
fn min(a: Int, b: Int): Int is commutative =
if a < b then a else b
fn gcd(a: Int, b: Int): Int is commutative =
if b == 0 then a else gcd(b, a - (a / b) * b)
// ============================================================
// PART 4: Idempotent Functions
// ============================================================
// Idempotent functions: f(f(x)) = f(x)
fn clamp(x: Int, minVal: Int, maxVal: Int): Int is idempotent =
if x < minVal then minVal
else if x > maxVal then maxVal
else x
fn absolute(x: Int): Int is idempotent =
if x < 0 then 0 - x else x
fn normalize(x: Int): Int is idempotent =
if x < 0 then 0 else if x > 100 then 100 else x
// ============================================================
// PART 5: Total Functions
// ============================================================
// Total functions always terminate (no infinite loops)
// Uses structural recursion on decreasing arguments
fn sumTo(n: Int): Int is total =
if n <= 0 then 0 else n + sumTo(n - 1)
fn countDown(n: Int): Int is total =
if n <= 0 then 0 else countDown(n - 1)
fn power(base: Int, exp: Int): Int is total =
if exp <= 0 then 1 else base * power(base, exp - 1)
// ============================================================
// PART 6: Combining Properties
// ============================================================
// Functions can have multiple behavioral properties
fn safeDivide(a: Int, b: Int, default: Int): Int is pure is deterministic =
if b == 0 then default else a / b
// ============================================================
// RESULTS
// ============================================================
fn main(): Unit with {Console} = {
Console.print("=== Behavioral Types Demo ===")
Console.print("")
Console.print("Part 1: Pure functions")
Console.print(" add(3, 4) = " + toString(add(3, 4)))
Console.print(" sumOfSquares(3, 4) = " + toString(sumOfSquares(3, 4)))
Console.print("")
Console.print("Part 2: Deterministic functions")
Console.print(" factorial(5) = " + toString(factorial(5)))
Console.print(" fibonacci(10) = " + toString(fibonacci(10)))
Console.print("")
Console.print("Part 3: Commutative functions")
Console.print(" max(10, 20) = " + toString(max(10, 20)))
Console.print(" gcd(48, 18) = " + toString(gcd(48, 18)))
Console.print("")
Console.print("Part 4: Idempotent functions")
Console.print(" clamp(150, 0, 100) = " + toString(clamp(150, 0, 100)))
Console.print(" absolute(-42) = " + toString(absolute(-42)))
Console.print(" normalize(normalize(75)) = " + toString(normalize(normalize(75))))
Console.print("")
Console.print("Part 5: Total functions")
Console.print(" sumTo(10) = " + toString(sumTo(10)))
Console.print(" power(2, 8) = " + toString(power(2, 8)))
Console.print("")
Console.print("Part 6: Combined properties")
Console.print(" safeDivide(10, 3, 0) = " + toString(safeDivide(10, 3, 0)))
Console.print(" safeDivide(10, 0, -1) = " + toString(safeDivide(10, 0, -1)))
}
main()

View File

@@ -0,0 +1,98 @@
// Schema Evolution Demo
// Demonstrates version tracking in the type system
// ============================================================
// PART 1: Basic Version Annotations
// ============================================================
// Functions can require specific versions of data
fn processV1Data(value: Int @v1): Int = value * 2
fn processV2Data(value: Int @v2): Int = value * 3
// Version-annotated values
let dataV1: Int @v1 = 100
let dataV2: Int @v2 = 100
// These work - versions match
let resultV1 = processV1Data(dataV1) // 200
let resultV2 = processV2Data(dataV2) // 300
// ============================================================
// PART 2: Version Constraints
// ============================================================
// @v2+ means "version 2 or later"
fn processModernData(value: Int @v2+): Int = value + 1
// This works - v2 satisfies v2+
let modernResult = processModernData(dataV2)
// @latest means "compatible with any version"
fn processAnyVersion(value: Int @latest): Int = value
// This works - @latest accepts any version
let anyResult = processAnyVersion(dataV1)
// ============================================================
// PART 3: Runtime Schema Operations
// ============================================================
// Create versioned values at runtime
let user1 = Schema.versioned("User", 1, { name: "Alice", role: "admin" })
let user2 = Schema.versioned("User", 2, { name: "Bob", role: "user", active: true })
// Check versions
let v1 = Schema.getVersion(user1) // 1
let v2 = Schema.getVersion(user2) // 2
// Migrate to newer version (upgrade)
let upgraded = Schema.migrate(user1, 2)
let upgradedVersion = Schema.getVersion(upgraded) // 2
// ============================================================
// PART 4: Practical Example - API Versioning
// ============================================================
// Simulate different API response versions
fn createResponseV1(data: String): { version: Int, payload: String } =
{ version: 1, payload: data }
fn createResponseV2(data: String, timestamp: Int): { version: Int, payload: String, meta: { ts: Int } } =
{ version: 2, payload: data, meta: { ts: timestamp } }
// Version-aware processing
fn getPayload(response: { version: Int, payload: String }): String =
response.payload
let resp1 = createResponseV1("Hello")
let resp2 = createResponseV2("World", 1234567890)
let payload1 = getPayload(resp1)
let payload2 = getPayload(resp2)
// ============================================================
// RESULTS
// ============================================================
fn main(): Unit with {Console} = {
Console.print("=== Schema Evolution Demo ===")
Console.print("")
Console.print("Part 1: Version-specific processing")
Console.print(" processV1Data(100 @v1) = " + toString(resultV1))
Console.print(" processV2Data(100 @v2) = " + toString(resultV2))
Console.print("")
Console.print("Part 2: Version constraints")
Console.print(" processModernData(@v2+) = " + toString(modernResult))
Console.print(" processAnyVersion(@latest) = " + toString(anyResult))
Console.print("")
Console.print("Part 3: Runtime schema operations")
Console.print(" User v1 version: " + toString(v1))
Console.print(" User v2 version: " + toString(v2))
Console.print(" After upgrade: " + toString(upgradedVersion))
Console.print("")
Console.print("Part 4: API versioning")
Console.print(" Response v1 payload: " + payload1)
Console.print(" Response v2 payload: " + payload2)
}
main()