| @@ -6,48 +6,49 @@ A statically typed language built on algebraic effects, with actors, reference c |
| 6 | 6 | import std/io |
| 7 | 7 | import std/list |
| 8 | 8 | |
| 9 | | −type Shape { |
| 10 | | − Circle(radius: Float) |
| 11 | | −, Rect(width: Float, height: Float) |
| 9 | +/// Search as an effect: `choose` returns once per candidate. |
| 10 | +effect search { |
| 11 | + multi ctl choose(from: Int, to: Int) -> Int |
| 12 | + final ctl fail() -> Never |
| 12 | 13 | } |
| 13 | 14 | |
| 14 | | −effect log { |
| 15 | | − fn log(msg: String) -> () |
| 15 | +/// Every answer the search reaches. |
| 16 | +fn every(action: fn() -> a / <search | e>) -> List<a> / <e> { |
| 17 | + handle { [action()] } with { |
| 18 | + choose(from, to) -> list.range(from, to) |> list.flat_map(resume) |
| 19 | + , fail() -> [] |
| 20 | + } |
| 16 | 21 | } |
| 17 | 22 | |
| 18 | | −fn area(shape: Shape) -> Float / <log> { |
| 19 | | − match shape { |
| 20 | | − Circle(r) -> 3.14159 * r * r |
| 21 | | − , Rect(w, h) -> { |
| 22 | | − log("a {w}×{h} rectangle") |
| 23 | | − w * h |
| 24 | | − } |
| 25 | | − } |
| 23 | +/// Right triangles with integer sides up to `n`, written as if there |
| 24 | +/// were only one. |
| 25 | +fn triangles(n: Int) -> List<(Int, Int, Int)> { |
| 26 | + use <- every |
| 27 | + let a = choose(1, n) |
| 28 | + let b = choose(a, n) |
| 29 | + let c = choose(b, n) |
| 30 | + if a * a + b * b != c * c { fail() } |
| 31 | + (a, b, c) |
| 26 | 32 | } |
| 27 | 33 | |
| 28 | 34 | pub fn main() -> () / <io> { |
| 29 | | − handle { |
| 30 | | − let total = [Circle(1.0), Rect(2.0, 3.0)] |> list.map(area) |> list.sum |
| 31 | | − io.println("total area: {total}") |
| 32 | | − } with { |
| 33 | | − log(msg) -> io.println("[log] {msg}") |
| 34 | | − } |
| 35 | + io.println("{triangles(20)}") |
| 35 | 36 | } |
| 36 | 37 | ``` |
| 37 | 38 | |
| 38 | | −`area` declares the `log` effect in its type; `main` decides what logging means. |
| 39 | +`triangles` reads as if `choose` returned one number. The `every` handler resumes it once per candidate and collects every answer; `use <- every` installs it for the rest of the function. |
| 39 | 40 | |
| 40 | 41 | ## Features |
| 41 | 42 | |
| 42 | | −- **Typed effects:** functions declare what they do (`/ <io, log>`) and handlers give it meaning: exceptions, generators, dependency injection and test mocks, without monads. |
| 43 | | −- **Actors:** typed processes with links, monitors and supervisors in the style of Erlang/OTP, scheduled preemptively on every core. |
| 44 | | −- **No garbage collector:** Perceus reference counting frees memory as soon as it is unused and updates unshared values in place. |
| 45 | | −- **Familiar ergonomics:** type inference, traits with `@derive`, exhaustive pattern matching, pipes, Gleam's `use`, `Result`/`Option` for errors. |
| 46 | | −- **HTML literals and templates:** type-checked markup in expressions, escaped when rendered. |
| 43 | +- **Typed effects:** functions declare what they do (`/ <io, sql>`) and handlers decide what that means: exceptions, generators, search with multi-shot `resume`, dependency injection, test doubles. No monads. |
| 44 | +- **Actors:** typed processes with links, monitors and OTP-style supervisors, scheduled preemptively on every core. |
| 45 | +- **No garbage collector:** Perceus reference counting frees memory as soon as it is unused and updates unshared values in place; `defer` runs cleanups. |
| 46 | +- **Types:** inference, traits with `@derive` and user-defined derives, associated and higher-kinded types, GADTs, opaque types, exhaustive pattern matching. |
| 47 | +- **Ergonomics:** pipes with placeholders (`x |> f(a, _)`), string interpolation, early `return`, `Result` and `Option` chained with `use`. |
| 48 | +- **For the web:** type-checked HTML literals and `pug`/`html` templates, an HTTP server with streaming and server-sent events, a TLS client, and packages for Postgres, argon2 and UUIDs. |
| 49 | +- **Native code:** compiled through C, with list pipelines fused and `Int`/`Float` arithmetic unboxed. Small benchmarks run in the range of GHC and Go, and a release hello world is a 387 KB static executable ([performance](docs/performance.md)). |
| 47 | 50 | - **C interop:** call C and Rust libraries, declared in `louise.toml`, with bindings generated from headers. |
| 48 | | −- **Tooling:** a formatter, a test runner, a language server (VS Code) and a tree-sitter grammar (Neovim, Helix). |
| 49 | | − |
| 50 | | −On small benchmarks it runs in the range of GHC and Go and well ahead of Node and Java, in a few megabytes of memory, with static executables around 400 KB ([performance](docs/performance.md)). |
| 51 | +- **Tooling:** dependencies from paths or git with a lockfile, a formatter, a test runner, a language server (VS Code), a tree-sitter grammar (Neovim, Helix), and `louise self update`. |
| 51 | 52 | |
| 52 | 53 | Louise is **pre-alpha**: the language and its standard library still change. |
| 53 | 54 | |