fix: fix examples to use working patterns
- behavioral.lux: use verifiable behavioral patterns (abs for idempotent) - behavioral_types.lux: use simpler verified patterns, proper main invocation - schema_evolution.lux: simplify to runtime schema ops, fix record access - jit_test.lux: add proper main function with console output All examples now parse and run correctly. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,20 +3,14 @@
|
||||
//
|
||||
// Expected output:
|
||||
// add(5, 3) = 8
|
||||
// clamp(150, 0, 100) = 100
|
||||
// factorial(5) = 120
|
||||
// multiply(7, 6) = 42
|
||||
// abs(-5) = 5
|
||||
|
||||
// A pure function - no side effects, same input always gives same output
|
||||
fn add(a: Int, b: Int): Int is pure =
|
||||
a + b
|
||||
|
||||
// An idempotent function - applying twice gives same result as once
|
||||
fn clamp(value: Int, min: Int, max: Int): Int is idempotent =
|
||||
if value < min then min
|
||||
else if value > max then max
|
||||
else value
|
||||
|
||||
// A deterministic function - same input always gives same output
|
||||
fn factorial(n: Int): Int is deterministic =
|
||||
if n <= 1 then 1
|
||||
@@ -26,18 +20,22 @@ fn factorial(n: Int): Int is deterministic =
|
||||
fn multiply(a: Int, b: Int): Int is commutative =
|
||||
a * b
|
||||
|
||||
// An idempotent function - absolute value
|
||||
fn abs(x: Int): Int is idempotent =
|
||||
if x < 0 then 0 - x else x
|
||||
|
||||
// Test the functions
|
||||
let sumResult = add(5, 3)
|
||||
let clampedResult = clamp(150, 0, 100)
|
||||
let factResult = factorial(5)
|
||||
let productResult = multiply(7, 6)
|
||||
let absResult = abs(0 - 5)
|
||||
|
||||
// Print results
|
||||
fn printResults(): Unit with {Console} = {
|
||||
Console.print("add(5, 3) = " + toString(sumResult))
|
||||
Console.print("clamp(150, 0, 100) = " + toString(clampedResult))
|
||||
Console.print("factorial(5) = " + toString(factResult))
|
||||
Console.print("multiply(7, 6) = " + toString(productResult))
|
||||
Console.print("abs(-5) = " + toString(absResult))
|
||||
}
|
||||
|
||||
let output = run printResults() with {}
|
||||
|
||||
@@ -5,80 +5,50 @@
|
||||
// PART 1: Pure Functions
|
||||
// ============================================================
|
||||
|
||||
// Pure functions cannot have any effects
|
||||
// Pure functions have no side 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))
|
||||
fn subtract(a: Int, b: Int): Int is pure = a - 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
|
||||
// PART 2: 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)
|
||||
fn multiply(a: Int, b: Int): Int is commutative = a * b
|
||||
fn sum(a: Int, b: Int): Int is commutative = a + b
|
||||
|
||||
// ============================================================
|
||||
// PART 4: Idempotent Functions
|
||||
// PART 3: 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 =
|
||||
fn abs(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
|
||||
fn identity(x: Int): Int is idempotent = x
|
||||
|
||||
// ============================================================
|
||||
// PART 4: Deterministic Functions
|
||||
// ============================================================
|
||||
|
||||
// Deterministic functions always produce the same output for the same input
|
||||
fn factorial(n: Int): Int is deterministic =
|
||||
if n <= 1 then 1 else n * factorial(n - 1)
|
||||
|
||||
fn fib(n: Int): Int is deterministic =
|
||||
if n <= 1 then n else fib(n - 1) + fib(n - 2)
|
||||
|
||||
// ============================================================
|
||||
// PART 5: Total Functions
|
||||
// ============================================================
|
||||
|
||||
// Total functions always terminate (no infinite loops)
|
||||
// Uses structural recursion on decreasing arguments
|
||||
// Total functions are defined for all inputs (no infinite loops, no exceptions)
|
||||
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
|
||||
// ============================================================
|
||||
@@ -88,34 +58,28 @@ fn main(): Unit with {Console} = {
|
||||
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(" add(5, 3) = " + toString(add(5, 3)))
|
||||
Console.print(" subtract(10, 4) = " + toString(subtract(10, 4)))
|
||||
Console.print("")
|
||||
|
||||
Console.print("Part 2: Deterministic functions")
|
||||
Console.print("Part 2: Commutative functions")
|
||||
Console.print(" multiply(7, 6) = " + toString(multiply(7, 6)))
|
||||
Console.print(" sum(10, 20) = " + toString(sum(10, 20)))
|
||||
Console.print("")
|
||||
|
||||
Console.print("Part 3: Idempotent functions")
|
||||
Console.print(" abs(-42) = " + toString(abs(0 - 42)))
|
||||
Console.print(" identity(100) = " + toString(identity(100)))
|
||||
Console.print("")
|
||||
|
||||
Console.print("Part 4: 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(" fib(10) = " + toString(fib(10)))
|
||||
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()
|
||||
let output = run main() with {}
|
||||
|
||||
@@ -9,8 +9,12 @@ fn factorial(n: Int): Int =
|
||||
if n <= 1 then 1
|
||||
else n * factorial(n - 1)
|
||||
|
||||
fn main(): Int = {
|
||||
let a = fib(30)
|
||||
let b = factorial(10)
|
||||
a + b
|
||||
fn main(): Unit with {Console} = {
|
||||
let fibResult = fib(30)
|
||||
let factResult = factorial(10)
|
||||
Console.print("fib(30) = " + toString(fibResult))
|
||||
Console.print("factorial(10) = " + toString(factResult))
|
||||
Console.print("Total = " + toString(fibResult + factResult))
|
||||
}
|
||||
|
||||
let output = run main() with {}
|
||||
|
||||
@@ -1,45 +1,13 @@
|
||||
// Schema Evolution Demo
|
||||
// Demonstrates version tracking in the type system
|
||||
// Demonstrates version tracking using runtime Schema operations
|
||||
|
||||
// ============================================================
|
||||
// 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
|
||||
// PART 1: 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 })
|
||||
let user1 = Schema.versioned("User", 1, "Alice")
|
||||
let user2 = Schema.versioned("User", 2, "Bob")
|
||||
|
||||
// Check versions
|
||||
let v1 = Schema.getVersion(user1) // 1
|
||||
@@ -50,7 +18,7 @@ let upgraded = Schema.migrate(user1, 2)
|
||||
let upgradedVersion = Schema.getVersion(upgraded) // 2
|
||||
|
||||
// ============================================================
|
||||
// PART 4: Practical Example - API Versioning
|
||||
// PART 2: Practical Example - API Versioning
|
||||
// ============================================================
|
||||
|
||||
// Simulate different API response versions
|
||||
@@ -68,7 +36,7 @@ let resp1 = createResponseV1("Hello")
|
||||
let resp2 = createResponseV2("World", 1234567890)
|
||||
|
||||
let payload1 = getPayload(resp1)
|
||||
let payload2 = getPayload(resp2)
|
||||
let payload2 = resp2.payload
|
||||
|
||||
// ============================================================
|
||||
// RESULTS
|
||||
@@ -77,22 +45,14 @@ let payload2 = getPayload(resp2)
|
||||
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("Part 1: 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("Part 2: API versioning")
|
||||
Console.print(" Response v1 payload: " + payload1)
|
||||
Console.print(" Response v2 payload: " + payload2)
|
||||
}
|
||||
|
||||
main()
|
||||
let output = run main() with {}
|
||||
|
||||
Reference in New Issue
Block a user