Modules
Turn treats code reuse as a first class runtime concern. You can import a local file, a standard library module, a remote URL, an external API schema, or a WebAssembly binary. In every case, use returns a native Turn value you call functions on immediately.
Local Modules
A module is a .tn file that evaluates to a value, typically a map of functions.
{
greet: turn(name: Str) -> Str {
return "Hello, " + name;
},
double: turn(n: Num) -> Num {
return n + n;
}
}Import it with the use keyword:
let utils = use "./my_utils";
let msg = utils.greet("Turn");Standard Library
Turn ships a standard library written entirely in Turn. Import any module with use "std/<name>". All modules are embedded in the binary, so no file system lookup is required.
| Module | Purpose | Identity Required |
|---|---|---|
std/net | HTTP GET and POST | grant identity::network(...) |
std/fs | File read and write | grant identity::filesystem(...) |
std/json | Parse and stringify JSON | none |
std/time | Timestamps and sleep | none |
std/env | Read and write environment variables | grant identity::environment(...) |
std/regex | Pattern matching and replacement | none |
std/math | max, min, abs | none |
See Standard Library for full signatures and examples.
URL-Native Modules
Turn can import any .tn file directly from an HTTPS URL. No package manager, no manifest file, no registry. The use keyword handles fetching, caching, and validation transparently.
let router = use "https://modules.turn-lang.dev/router.tn";
let result = router.match(request);How Caching Works
On the first import, the runtime fetches the file over HTTPS and stores it in .turn_cache/ast/ alongside a SHA-256 hash of the content. On every subsequent run, Turn reads from the cache without making a network request. If the remote file changes, the hash mismatch triggers a re-fetch and re-validation automatically.
Integrity and Security
URL imports are subject to the same sandboxing as local modules. A remote module cannot access the file system, spawn processes, or call tools unless the importing program explicitly passes capabilities to it. The fetched source is compiled through the same lexer, parser, and semantic analysis pipeline as local code, so malformed or malicious input is caught at compile time.
Versioning
To pin a specific version of a remote module, include the version in the URL path or query string. Turn does not impose a versioning scheme. The cache key is the full URL, so https://modules.turn-lang.dev/router@1.2.0.tn and https://modules.turn-lang.dev/router@1.3.0.tn are treated as separate modules.
TIP
For production deployments, host your shared modules on infrastructure you control. The URL-native model trades the complexity of a package registry for the simplicity of plain HTTPS, but you are responsible for the availability and integrity of the URLs you depend on.
OpenAPI Schema Absorption
For APIs that expose an OpenAPI (Swagger) schema, Turn can absorb the schema at compile time and generate pure Turn closures. The use schema::<protocol>("url") macro fetches the remote schema, parses it, and produces a module-like value you interact with exactly like a native Turn module.
let petstore = use schema::openapi("https://petstore.swagger.io/v2/swagger.json");
let pets = petstore.find_pets_by_status("available");This is a compile-time operation. The Turn compiler fetches the schema, generates struct definitions and typed closures from the API paths, and injects them into the program before execution begins. No HTTP boilerplate, no SDK dependencies. The openapi adapter is shipped by default, with graphql, fhir, and mcp adapters in active development.
See Ecosystem Bridges for the full story on how Turn connects to external APIs and services.
Wasm Component FFI
Turn can mount sandboxed WebAssembly components and expose their exported functions as native Turn closures.
let math = use "math.wasm";
let result = math.calculate_trajectory(15.0, 27.5);Wasm modules run in isolated linear memory and cannot access the host filesystem or network unless capabilities are explicitly granted. This is also how Turn's LLM inference providers work: each provider is a .wasm driver in .turn_modules/ that transforms requests and responses. See Inference Providers for details.
Module Resolution Order
When the compiler encounters a use declaration, it resolves the path in this order:
- Relative path: checked relative to the current source file.
std/prefix: resolved from the Turn standard library embedded in the binary.TURN_MODULE_PATH: environment variable with colon-separated directories.- URL: fetched over HTTPS and cached locally (for
use "https://..."paths).