Struct Inference
The simplest demonstration of Turn's Cognitive Type Safety. Define a struct, then use infer to have the LLM populate it from a natural language prompt. Turn compiles the struct schema at build time, sends it to the LLM as a JSON Schema constraint, and validates the response before binding it to the typed variable.
If the LLM returns malformed JSON or the wrong field types, Turn's coercion engine retries automatically — the caller always receives a correctly typed value or an explicit Result::Err.
struct_infer.tn
// Turn Language: Struct Inference
// Demonstrates Cognitive Type Safety: inferring a structured type.
// 1. Define the shape of thought
struct Sentiment {
score: Num,
reasoning: Str
};
call("echo", "Analyzing sentiment...");
// 2. Infer — the LLM populates the struct; Turn validates the output
let result = infer Sentiment {
"I love this new language! It makes building agents so much easier.";
};
// 3. Type-safe access — no casting, no null checks
call("echo", "Score: " + result.score);
call("echo", "Reasoning: " + result.reasoning);
if result.score > 0.8 {
call("echo", "Verdict: POSITIVE");
} else {
call("echo", "Verdict: NEGATIVE/NEUTRAL");
}
return result;Run it:
export TURN_INFER_PROVIDER=~/.turn/providers/turn_provider_openai.wasm turn run impl/examples/struct_infer.tn
The full implementation is in impl/examples/struct_infer.tn.