A statically typed, expression-oriented programming language. The
compiler translates Thoxa to C, and a C compiler builds that into a
native executable.
Automatic memory, without a collector or a borrow checker.
Values are reference counted. Integers, floats, and small records are stored
inline; strings, arrays, and maps are counted and copied on write, so a value
never changes under another binding. There are no lifetimes and no collection
pauses. Files, sockets, and databases close when their last reference goes.
Errors are values.Trial<T, E> and May<T> are ordinary unions from the
standard library, and ? hands a failure back to the caller. There
are no exceptions. A panic (an index out of range, a division by zero) is for
bugs, and it ends only the task it happens in.
The type is the schema.Int64 where >= 1 is a type, and "small" | "large" is a
closed set of strings. From a type definition the compiler writes a JSON reader
that reports every bad field, a form reader, and an encoder. There are no derive
macros, schema objects, or runtime reflection.
Close to C.
The output is C. extern fn calls a C function directly, a type with
body fields has C layout, and integer behaviour is defined: arithmetic wraps,
narrowing is checked, and division by zero panics instead of being undefined.
What a small web service needs is in the language.
Tag literals build HTML as values, a type body of GET "/path" clauses
is a router, sql"…" quotes every value it interpolates, and
--> starts a task whose socket operations park instead of blocking:
one OS thread, no async, no await. The standard library has
an HTTP/1.1 server and SQLite. This site is a Thoxa program, and its manual page is
rendered from Markdown by Thoxa code.
Features
Types
Int64, Int32, UInt32, UInt64, Byte, Float64, Bool, String, Bytes; products and unions; literal types; refinements (where); generic types; function types.
Traits
trait and conform: a type conforms only by declaring it, and a trait writes the members its conforming types share. Getters and setters read like fields; self is the receiver.
Inference
Local inference for let. Return types are inferred for functions and methods without an annotation.
Matching
Constructor, literal, and or-patterns, with guards. A match on a union must cover every member.
Functions
Positional or named arguments, defaults, and multi-clause functions that dispatch on parameter types, literal types included.
Values
Declared Equatable, Hashable, and Ordered, field by field or written by hand: ==, map keys, and < on your own types. Array and Map copy on write; a Map keeps insertion order.
Errors
May, Trial, and ?. A panic ends the task it happens in, not the process.
Memory
Reference counting. A type that conforms to Resource owns something outside the program and releases it with its last reference.
Concurrency
--> starts a task. Socket operations park it until they can proceed. One OS thread.
Data
T.read for JSON text and T.readForm for forms; encode for output; Json literals; typed route body parameters.
Conversions
x as T converts, and its type says whether it can fail; x as? T converts or returns the failure. A type adds conversions into itself with from clauses.
Web
Tag literals (Node), route clauses, an HTTP/1.1 server with static files and server-sent events, and sql"…" with SQLite.
C
extern fn, C-layout types, Address<T>, and fixed-size arrays.
Tools
thoxa run, build, check (with --json), and lsp; a Zed extension.
Each sample is a complete program from
workloads/landing/samples.
cargo make test runs each one and compares its output with what is
shown here; the server, which never exits, is compiled but not run.
A product type lists its fields; a union lists its members. match takes a union apart, with guards, and says so at compile time when a member is missing. A type that declares Equatable compares field by field.
import logging, { info } from"std/logging"typeCircle(radius: Float64)
typeRect(width: Float64, height: Float64)
conformRecttoEquatabletypeTriangle(base: Float64, height: Float64)
typeShape = Circle | Rect | Trianglefnarea(shape!: Shape) -> Float64 {
match shape {
Circle(radius) => 3.14159 * radius * radius
Rect(width, height) => width * height
Triangle(base, height) => base * height / 2.0
}
}
fndescribe(shape!: Shape) -> String {
match shape {
Rect(width, height) if width == height => "a square"Rect => "a rectangle"Circle => "a circle"Triangle => "a triangle"
}
}
fnmain() {
logging.toStdout()
logging.setFormat(logging.formatPlain)
let shapes: Array<Shape> = [
Circle(radius: 1.0),
Rect(width: 2.0, height: 2.0),
Triangle(base: 3.0, height: 4.0),
]
for shape in shapes {
info("{describe(shape)}: {area(shape)}")
}
info("{Rect(width: 2.0, height: 2.0) == Rect(width: 2.0, height: 2.0)}")
0
}
Output
a circle: 3.14159
a square: 4.0
a triangle: 6.0
true
Trial<T, E> (Pass / Fail) and May<T> (Has / Not) are ordinary unions from std/fallible. ? unwraps a Pass or returns the Fail to the caller. There are no exceptions.
import { Has, Not, May, Pass, Fail, Trial } from"std/fallible"import { parseUInt } from"std/string"import logging, { info } from"std/logging"typeSetting(name: String, value: Int64)
fnparseLine(line!: String) -> Trial<Setting, String> {
let parts = match line.splitOnce("=") {
Has(found) => found
Not => returnFail("no `=` in `{line}`")
}
let value = matchparseUInt(parts.after.trim()) {
Has(number) => number
Not => returnFail("`{parts.after.trim()}` is not a number")
}
Pass(Setting(name: parts.before.trim(), value))
}
fnparseAll(lines!: Array<String>) -> Trial<Array<Setting>, String> {
mut settings: Array<Setting> = []
for line in lines {
settings.append(parseLine(line)?)
}
Pass(settings)
}
fnmain() -> Int64 {
logging.toStdout()
logging.setFormat(logging.formatPlain)
let files = [
["port = 8080", "workers = 4"],
["port = 8080", "workers four"],
["port = eighty"],
]
for lines in files {
matchparseAll(lines) {
Pass(found) => info("{found.count} settings, port {found[0].value}")
Fail(reason) => info("error: {reason}")
}
}
0
}
Output
2 settings, port 8080
error: no `=` in `workers four`
error: `eighty` is not a number
where narrows a type; a literal union names the values a string may take. json as T (or T.read for text) builds a T and reports every problem it finds; encode writes any value back out. Nothing is derived or registered: the compiler reads the type.
A tag is an expression of type Node. A type becomes a component by conforming to Component and writing its view. Text and attributes are escaped when the tree is written out; there is no template language and no string building.
A type whose body lists methods and paths is a router: the compiler writes its route function. serve runs it on one OS thread, a task per connection. The form is read into a type, the list is written as JSON.
--> starts a call and does not wait for it. A socket operation that would block parks the task and another one runs. One OS thread, no async, no await: the same function works in a task or out of one.
import net, { Socket, createSocket } from"std/net"import { Pass, Fail, Trial } from"std/fallible"import logging, { info } from"std/logging"fnlisten(listener!: Socket, clients!: Int64) -> Trial<Void, String> {
for served in0..clients {
let peer = listener.accept()?
-->answer(peer)
}
Pass(Void)
}
fnanswer(peer!: Socket) -> Trial<Void, String> {
let request = peer.receive().toString()
peer.sendAll(Bytes.fromString(request.toUpper()))?
peer.close()
Pass(Void)
}
fnask(port!: Int64, word!: String) -> Trial<String, String> {
let client = createSocket()?
client.connect(port)?
client.sendAll(Bytes.fromString(word))?
let reply = client.receive().toString()
client.close()
Pass(reply)
}
fnrun() -> Trial<Void, String> {
let listener = createSocket()?
listener.reuseAddr()?
listener.bind(0)?
listener.listen(16)?
let port = listener.localPort()?
let words = ["tasks", "park", "on", "sockets"]
-->listen(listener, words.count)
for word in words {
info(ask(port, word)?)
}
Pass(Void)
}
fnmain() -> Int64 {
logging.toStdout()
logging.setFormat(logging.formatPlain)
matchrun() {
Pass => 0Fail(error) {
info(error)
1
}
}
}
extern fn declares a C function and the compiler writes its prototype, so anything in libc (or another linked library) can be called. A type with body fields has C layout, and Address<T> passes it by pointer. This program asks the kernel its name with uname(2) and writes the answer with write(2).
Thoxa is pre-alpha. What works is what the test suite covers: each feature above
has programs in tests/conformance that compile, run, and print what
they should on every change, under LeakSanitizer. Not there yet:
Releases. Build from source; Linux x86-64 is the only platform.
Closures, generic functions, and interfaces or traits.
Threads. Tasks share one OS thread and switch when one parks on a socket.
Precise memory management. Reference counting is the simple, single-threaded kind: every copy counts.
A broad standard library. There are files, TCP, HTTP/1.1, JSON, SQLite, strings, paths, and logging.
Editor support beyond Zed. The language server's navigation is incomplete.
Stable syntax. Code written today will need changes.
Building
You need a Rust toolchain and a C compiler (GCC or Clang).
git clone https://github.com/moonlight-pm/thoxa
cd thoxa
cargo build -p thoxa # the compiler: target/rust/debug/thoxa
cargo make test # conformance suite, samples, unit tests
target/rust/debug/thoxa run workloads/landing/samples/types.thx
The manual covers the language, the standard library, and
the tools. Problems and questions go to the
issue tracker.