ThinkLang Examples
This page provides an overview of the example programs shipped with ThinkLang. Each example demonstrates a different language feature or pattern. Source files are in the examples/ directory.
Hello Think
The simplest AI call. Sends a prompt and prints the result.
let greeting = think<string>("Say hello to the world in a creative way")
print greetingSource: examples/01-hello-think.tl
Classification
Uses a structured type to classify an email into a category with a confidence score.
type Classification {
@description("The category of the email")
category: string
@description("Confidence score from 0 to 1")
confidence: float
@description("Brief explanation")
reason: string
}
let email = "Congratulations! You've won a FREE iPhone!"
let result = think<Classification>("Classify this email as spam, promotional, personal, or work")
with context: email
print resultSource: examples/02-classification.tl
Data Extraction
Extracts structured entities from unstructured text.
type Person {
name: string
role: string
company: string?
}
type ExtractionResult {
@maxItems(10)
people: Person[]
@description("Brief summary of the document")
summary: string
}
let document = "Meeting notes: John Smith (CEO of Acme Corp) met with Jane Doe..."
let extracted = think<ExtractionResult>("Extract all people and summarize")
with context: document
print extractedSource: examples/03-extraction.tl
Sentiment Analysis
Analyzes sentiment with a Confident wrapper that tracks the model's self-assessed confidence.
type Sentiment {
@description("positive, negative, or neutral")
label: string
@description("Intensity from 1-10")
intensity: int
}
let review = "This product is absolutely amazing!"
let sentiment = think<Confident<Sentiment>>("Analyze the sentiment of this review")
with context: review
print sentimentSource: examples/05-sentiment.tl
Multi-Step Processing
Chains multiple think calls, feeding the output of one into the context of the next.
let entities = think<Entities>("Extract technologies, applications, and challenges")
with context: report
let analysis = think<Analysis>("Analyze the landscape based on these entities")
with context: {
entities,
report,
}
print analysisSource: examples/10-multi-step.tl
Pipeline
Chains expressions using the |> operator for left-to-right data flow.
let result = inputData
|> think<Summary>("Summarize this")
|> think<Translation>("Translate to French")Source: examples/09-pipeline.tl
Infer
Lightweight classification and transformation without a full prompt.
let category = infer<string>("urgent: server is down!", "Classify priority as low, medium, high, or critical")
print category
let language = infer<string>("Bonjour le monde", "Detect the language")
print languageSource: examples/06-infer-basic.tl
Guards
Validates AI output with guard constraints. Retries automatically on failure.
let result = think<Translation>("Translate to Spanish")
with context: sourceText
guard {
length: 5..500
}
on_fail: retry(3)
print resultSource: examples/13-guards.tl
Reason Blocks
Multi-step reasoning with explicit numbered steps.
let analysis = reason<InvestmentAnalysis> {
goal: "Analyze this investment portfolio and provide recommendations"
steps:
1. "Evaluate the current asset allocation"
2. "Assess market conditions impact on each asset class"
3. "Identify risks and opportunities"
4. "Formulate a recommendation"
with context: {
portfolio,
marketConditions,
}
}
print analysisSource: examples/12-reason-block.tl
Match Patterns
Pattern matching on AI results based on confidence or field values.
let sentiment = think<Confident<Sentiment>>("Analyze the sentiment")
with context: review
let response = match sentiment {
{ confidence: >= 0.9 } => "High confidence result"
{ confidence: >= 0.5 } => "Moderate confidence result"
_ => "Low confidence -- manual review needed"
}
print responseSource: examples/14-match-expression.tl
Try/Catch
Error handling for AI operations. Catches specific runtime error types.
try {
let result = think<Summary>("Summarize this text in detail")
with context: text
print result
} catch SchemaViolation (e) {
print "Schema error occurred"
} catch ConfidenceTooLow (e) {
print "Confidence too low for reliable result"
}Source: examples/15-try-catch.tl
If/Else
Conditional logic based on AI results.
if sentiment.isConfident(0.8) {
print "High confidence result"
} else {
print "Low confidence -- needs review"
}Confident Types
Working with Confident<T> -- unwrapping, checking confidence, and using fallbacks.
let classified = think<Confident<Category>>("Classify this product")
with context: item
print classifiedThe uncertain modifier enforces safe access at compile time:
let uncertain category = think<Category>("Classify this product")
with context: text
let safeName = category.unwrap()
let highConf = category.expect(0.8)Sources: examples/08-confident-values.tl, examples/11-uncertain.tl
Context and Privacy
Using with context to provide data and without context to exclude sensitive fields.
let recommendation = think<Recommendation>("Suggest products based on interests")
with context: {
profile,
sensitiveData,
}
without context: sensitiveData
print recommendationSource: examples/16-without-context.tl
Testing
ThinkLang includes a built-in test framework with value assertions and semantic assertions.
test "classification produces a valid category" {
let result = think<Category>("Classify as food or drink")
with context: "orange juice"
assert result.name == "drink"
}
test "summary is relevant" {
let summary = think<Summary>("Summarize this article")
with context: article
assert.semantic(summary, "mentions key financial metrics")
}Run tests with:
npx tsx src/cli/index.ts test
npx tsx src/cli/index.ts test --update-snapshots
npx tsx src/cli/index.ts test --replayModules (Imports)
Split code across files using import. Types and functions are automatically importable from other .tl files.
// types.tl
type Sentiment {
label: string
score: float
}
fn analyzeSentiment(text: string): Sentiment {
let result = think<Sentiment>("Analyze the sentiment")
with context: text
print result
}// main.tl
import { Sentiment, analyzeSentiment } from "./types.tl"
let result = analyzeSentiment("Great product!")
print resultNo export keyword needed. Paths are relative. Circular imports are detected.
Example Index
| # | File | Feature |
|---|---|---|
| 01 | 01-hello-think.tl | Basic think call |
| 02 | 02-classification.tl | Structured types, with context |
| 03 | 03-extraction.tl | Nested types, arrays, @maxItems |
| 04 | 04-summarization.tl | @description annotations |
| 05 | 05-sentiment.tl | Confident<T> |
| 06 | 06-infer-basic.tl | infer expression |
| 07 | 07-with-context.tl | Context blocks with multiple variables |
| 08 | 08-confident-values.tl | Confident<T> methods |
| 09 | 09-pipeline.tl | Pipeline |> operator |
| 10 | 10-multi-step.tl | Chaining multiple think calls |
| 11 | 11-uncertain.tl | uncertain modifier, .unwrap(), .expect() |
| 12 | 12-reason-block.tl | reason blocks with goals and steps |
| 13 | 13-guards.tl | Guard constraints, on_fail: retry(n) |
| 14 | 14-match-expression.tl | match expression with patterns |
| 15 | 15-try-catch.tl | try/catch error handling |
| 16 | 16-without-context.tl | without context for privacy |
| 17 | 17-cache-demo.tl | Exact-match caching |