AILANG Examples
Learn AILANG through interactive examples. Each example demonstrates a key feature with real working code from the repository.
Quick Start Examples
Hello World
The simplest AILANG program with I/O effects:
-- hello.ail - Simple hello world program
module examples/runnable/hello
-- Entry module with exported main function
-- Gets print prelude automatically (no import needed)
export func main() -> () ! {IO} =
print("Hello, AILANG!")
Run it:
ailang run --caps IO --entry main examples/runnable/hello.ail
Key concepts:
-
!: IOdeclares the function performs I/O -
printlnis imported fromstd/io -
--caps IOgrants I/O capability at runtime
Recursion & Pure Functions
AILANG uses recursion instead of loops for deterministic control flow:
-- recursion_factorial.ail
-- Demonstrates simple recursive function with LetRec
-- Status: ✅ Working (M-R4 recursion support)
module examples/runnable/recursion_factorial
import std/io (println)
-- Classic factorial using recursion
pure func factorial(n: int) -> int
tests [
(0, 1),
(1, 1),
(5, 120),
(10, 3628800),
(12, 479001600)
]
{
if n <= 1 then 1 else n * factorial(n - 1)
}
-- Tail-recursive factorial with accumulator
pure func factorialTail(n: int, acc: int) -> int
tests [
((0, 1), 1),
((1, 1), 1),
((5, 1), 120),
((10, 1), 3628800),
((12, 1), 479001600)
]
{
if n <= 1 then acc else factorialTail(n - 1, n * acc)
}
export func main() -> () ! {IO} {
let result1 = factorial(5);
let result2 = factorialTail(10, 1);
println("factorial(5) = " ++ show(result1));
println("factorialTail(10, 1) = " ++ show(result2))
}
Run it:
ailang run --entry factorial examples/runnable/recursion_factorial.ail
Test it:
# Test factorial(5)
echo "factorial(5)" | ailang repl --load examples/runnable/recursion_factorial.ail
# Output: 120
Key concepts:
-
factorialis pure (no effects declared) - Recursion replaces loops for deterministic reasoning
-
show()converts int to string for printing
Pattern Matching
Lists & Destructuring
Pattern matching is AILANG's primary control flow mechanism:
import std/io (println)
-- Sum list using pattern matching
fn sum(list: [int]) -> int =
match list {
[] -> 0,
[head, ...tail] -> head + sum(tail)
}
fn main() !: IO -> () =
let numbers = [1, 2, 3, 4, 5];
println("Sum: " ++ show(sum(numbers)))
Output:
Sum: 15
Key concepts:
-
[]matches empty list -
[head, ...tail]destructures list - Exhaustive matching required (compiler enforces)
Algebraic Data Types (ADTs)
Define custom types with multiple constructors:
import std/io (println)
type Tree =
| Leaf(int)
| Node(Tree, int, Tree)
-- Sum all values in tree
fn sumTree(tree: Tree) -> int =
match tree {
Leaf(n) -> n,
Node(left, value, right) ->
sumTree(left) + value + sumTree(right)
}
fn main() !: IO -> () =
let tree = Node(Leaf(1), 2, Node(Leaf(3), 4, Leaf(5)));
println("Tree sum: " ++ show(sumTree(tree)))
Output:
Tree sum: 15
Key concepts:
-
type Tree =defines an ADT with 2 constructors - Pattern matching handles all cases
- Compiler ensures exhaustiveness
Effect System
Multiple Effects
Functions can declare multiple effects they perform:
import std/io (println)
import std/fs (readFile)
import std/json (decode)
fn loadConfig() !: IO,FS -> Result[string, string] =
do {
content <- readFile("config.json");
println("Loaded config file");
return Ok(content)
}
fn main() !: IO,FS -> () =
match loadConfig() {
Ok(content) -> println("Config: " ++ content),
Err(e) -> println("Error: " ++ e)
}
Run it:
ailang run --caps IO,FS --entry main examples/config.ail
Key concepts:
-
!: IO,FSdeclares both I/O and filesystem effects -
doblocks sequence effectful operations -
Result[T,E]type for error handling
Effect Polymorphism
Functions can be generic over effects:
-- Works with any effect row
fn log[e](message: string) !: {IO | e} -> () =
println("[LOG] " ++ message)
-- Calls log while also reading files
fn processFile(path: string) !: IO,FS -> () =
do {
log("Reading file: " ++ path);
content <- readFile(path);
log("File loaded")
}
Key concepts:
-
!: {IO | e}means "IO plus whatever effects the caller has" - Effect polymorphism enables reusable functions
- Effect rows compose naturally
Type System
Type Inference
AILANG infers types automatically using Hindley-Milner:
import std/io (println)
-- No type annotations needed!
fn double(x) = x * 2
fn main() !: IO -> () =
let result = double(21);
println(show(result)) -- Prints: 42
Key concepts:
- Type inference works across function boundaries
- Explicit annotations optional but recommended
-
:typecommand in REPL shows inferred types
Records & Subsumption
Records are structural types with width subtyping:
import std/io (println)
type Person = { name: string, age: int }
fn greet(person: Person) !: IO -> () =
println("Hello, " ++ person.name)
fn main() !: IO -> () =
let employee = { name: "Alice", age: 30, role: "Engineer" };
greet(employee) -- OK! employee has all required fields
Key concepts:
- Records support structural typing
- Width subtyping:
{a, b, c}⊆{a, b} - Field access:
record.field
JSON & HTTP
JSON Parsing
Parse and work with JSON data:
import std/io (println)
import std/json (decode)
fn main() !: IO -> () =
let jsonString = "{\"name\": \"AILANG\", \"version\": \"0.3.14\"}";
match decode(jsonString) {
Ok(json) ->
match json {
Object(fields) ->
match fields["name"] {
Some(String(name)) -> println("Name: " ++ name),
_ -> println("Name not found")
},
_ -> println("Not an object")
},
Err(e) -> println("Parse error: " ++ e)
}
Key concepts:
-
decodereturnsResult[Json, string] - Pattern match on JSON structure
- Safe access to nested fields
HTTP Requests
Make HTTP requests with the Net effect:
import std/io (println)
import std/net (httpGet)
fn main() !: IO,Net -> () =
match httpGet("https://api.github.com/repos/sunholo-data/ailang") {
Ok(response) ->
println("Status: " ++ show(response.status)),
Err(e) ->
println("Request failed: " ++ e)
}
Run it:
ailang run --caps IO,Net --entry main examples/http_get.ail
Key concepts:
-
!: IO,Netdeclares network capability -
httpGetreturnsResult[Response, string] - DNS rebinding protection built-in
Advanced Examples
Higher-Order Functions
Functions are first-class values:
import std/io (println)
fn map(f, list) =
match list {
[] -> [],
[x, ...xs] -> [f(x), ...map(f, xs)]
}
fn main() !: IO -> () =
let numbers = [1, 2, 3, 4, 5];
let doubled = map(\x -> x * 2, numbers);
println(show(doubled)) -- [2, 4, 6, 8, 10]
Key concepts:
-
\x -> x * 2is a lambda function - Functions can be passed as arguments
- Closures capture environment
Mutual Recursion
Multiple functions can call each other:
import std/io (println)
fn isEven(n: int) -> bool =
if n == 0 then true
else isOdd(n - 1)
fn isOdd(n: int) -> bool =
if n == 0 then false
else isEven(n - 1)
fn main() !: IO -> () =
println("5 is odd: " ++ show(isOdd(5))) -- true
Key concepts:
- Functions can reference each other
- Stack overflow protection included
- Deterministic evaluation order
Try It Yourself
In the Playground
Visit the Playground to run code interactively in your browser.
With CLI
Install AILANG and run examples locally:
# Install (macOS Apple Silicon)
curl -L https://github.com/sunholo-data/ailang/releases/latest/download/ailang-darwin-arm64.tar.gz | tar -xz
sudo mv ailang /usr/local/bin/
# Or build from source
git clone https://github.com/sunholo-data/ailang && cd ailang && make install
# Run an example
ailang run --caps IO --entry main examples/runnable/hello.ail
# Interactive REPL
ailang repl
From GitHub
Clone the repository for all 66+ examples:
git clone https://github.com/sunholo-data/ailang
cd ailang/examples
# Run specific examples
ailang run --caps IO --entry main v3_3/factorial.ail
Implementation Status
This section tracks AILANG's implementation progress and is automatically updated by CI/CD.
The example verification status shows what percentage of AILANG features are fully implemented and working. Each example exercises specific language features, so the pass rate directly reflects implementation completeness.
Status
Example Verification Status
Last updated: 2025-12-05 (Auto-updated by CI)
Summary: 24 passed, 26 failed, 5 skipped (Total: 55)
Overall: 24/55 examples working (44%)
| Example File | Status | Notes |
|---|---|---|
runnable/adt_option.ail | ✅ Pass | |
runnable/adt_simple.ail | ✅ Pass | |
runnable/ai_effect.ail | ❌ Fail | Error: module loading error: failed to load std/ai (searc... |
runnable/arithmetic.ail | ✅ Pass | |
runnable/array_adt.ail | ✅ Pass | |
runnable/block_demo.ail | ⏭️ Skip | Test/demo file |
runnable/block_recursion.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/bug_float_comparison.ail | ✅ Pass | |
runnable/cli_args_demo.ail | ⏭️ Skip | Test/demo file |
runnable/closures.ail | ✅ Pass | |
runnable/conway_grid.ail | ❌ Fail | Error: module loading error: failed to load std/array (se... |
runnable/debug_effect.ail | ❌ Fail | Error: module loading error: failed to load std/debug (se... |
runnable/demos/adt_pipeline.ail | ❌ Fail | Error: module loading error: failed to load std/option (s... |
runnable/demos/hello_io.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/effects_basic.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/effects_pure.ail | ✅ Pass | |
runnable/func_expressions.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/guards_basic.ail | ✅ Pass | |
runnable/hello.ail | ✅ Pass | |
runnable/imported_adt_types.ail | ✅ Pass | |
runnable/imports.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/imports_basic.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/json_basic_decode.ail | ❌ Fail | Error: module loading error: failed to load std/json (sea... |
runnable/lambdas.ail | ✅ Pass | |
runnable/lambdas_advanced.ail | ✅ Pass | |
runnable/lambdas_closures.ail | ✅ Pass | |
runnable/lambdas_curried.ail | ✅ Pass | |
runnable/lambdas_higher_order.ail | ✅ Pass | |
runnable/letrec_recursion.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/list_patterns.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/micro_block_if.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/micro_block_seq.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/micro_io_echo.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/micro_option_map.ail | ❌ Fail | Error: module loading error: failed to load std/option (s... |
runnable/micro_record_person.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/option_demo.ail | ⏭️ Skip | Test/demo file |
runnable/patterns.ail | ✅ Pass | |
runnable/records.ail | ✅ Pass | |
runnable/recursion_factorial.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/recursion_fibonacci.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/recursion_match.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/recursion_mutual.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/recursion_quicksort.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/simple.ail | ✅ Pass | |
runnable/stdlib_demo.ail | ⏭️ Skip | Test/demo file |
runnable/stdlib_demo_simple.ail | ⏭️ Skip | Test/demo file |
runnable/test_cli_io.ail | ✅ Pass | |
runnable/test_fizzbuzz.ail | ❌ Fail | Error: module loading error: failed to load std/io (searc... |
runnable/test_guard_bool.ail | ✅ Pass | |
runnable/test_import_func.ail | ❌ Fail | Error: module loading error: failed to load std/option (s... |
runnable/test_io_builtins.ail | ❌ Fail | Error: effect checking failed in examples/runnable/test_i... |
runnable/test_module_minimal.ail | ✅ Pass | |
runnable/type_classes.ail | ✅ Pass | |
runnable/type_inference.ail | ✅ Pass | |
runnable/typeclasses.ail | ✅ Pass |
Known Limitations
Some examples are currently broken due to ongoing development:
- Typed Quasiquotes - Not yet implemented (v0.4.0+)
- Some JSON operations - Parser improvements in progress
- Complex effect polymorphism - Type inference improvements needed
See Implementation Status for details.
Contributing Examples
Found a bug? Have a cool example to share?
Next Steps
- Try the Playground - Run code in your browser
- Read the Language Reference - Complete syntax guide
- AI Integration Guide - Use AILANG with AI agents
- Browse all examples - 66+ example files
Note: Examples are automatically verified by CI/CD. See examples_status.md for the latest status.