Files
lux/examples/behavioral.lux
Brandon Lucas 15a820a467 fix: make all example programs work correctly
- Add string concatenation support to + operator in typechecker
- Register ADT constructors in both type environment and interpreter
- Bind handlers as values so they can be referenced in run...with
- Fix effect checking to use subset instead of exact match
- Add built-in effects (Console, Fail, State) to run block contexts
- Suppress dead code warnings in diagnostics, modules, parser

Update all example programs with:
- Expected output documented in comments
- Proper run...with statements to execute code

Add new example programs:
- behavioral.lux: pure, idempotent, deterministic, commutative functions
- pipelines.lux: pipe operator demonstrations
- statemachine.lux: ADT-based state machines
- tailcall.lux: tail call optimization examples
- traits.lux: type classes and pattern matching

Add documentation:
- docs/IMPLEMENTATION_PLAN.md: feature roadmap and status
- docs/PERFORMANCE_AND_TRADEOFFS.md: performance analysis

Add benchmarks for performance testing.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-13 09:05:06 -05:00

44 lines
1.3 KiB
Plaintext

// Demonstrating behavioral properties in Lux
// Behavioral properties are compile-time guarantees about function behavior
//
// Expected output:
// add(5, 3) = 8
// clamp(150, 0, 100) = 100
// factorial(5) = 120
// multiply(7, 6) = 42
// 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
else n * factorial(n - 1)
// A commutative function - order of arguments doesn't matter
fn multiply(a: Int, b: Int): Int is commutative =
a * b
// Test the functions
let sumResult = add(5, 3)
let clampedResult = clamp(150, 0, 100)
let factResult = factorial(5)
let productResult = multiply(7, 6)
// 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))
}
let output = run printResults() with {}