Skip to content

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.

thinklang
let greeting = think<string>("Say hello to the world in a creative way")
print greeting

Source: examples/01-hello-think.tl


Classification

Uses a structured type to classify an email into a category with a confidence score.

thinklang
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 result

Source: examples/02-classification.tl


Data Extraction

Extracts structured entities from unstructured text.

thinklang
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 extracted

Source: examples/03-extraction.tl


Sentiment Analysis

Analyzes sentiment with a Confident wrapper that tracks the model's self-assessed confidence.

thinklang
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 sentiment

Source: examples/05-sentiment.tl


Multi-Step Processing

Chains multiple think calls, feeding the output of one into the context of the next.

thinklang
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 analysis

Source: examples/10-multi-step.tl


Pipeline

Chains expressions using the |> operator for left-to-right data flow.

thinklang
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.

thinklang
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 language

Source: examples/06-infer-basic.tl


Guards

Validates AI output with guard constraints. Retries automatically on failure.

thinklang
let result = think<Translation>("Translate to Spanish")
  with context: sourceText
  guard {
    length: 5..500
  }
  on_fail: retry(3)

print result

Source: examples/13-guards.tl


Reason Blocks

Multi-step reasoning with explicit numbered steps.

thinklang
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 analysis

Source: examples/12-reason-block.tl


Match Patterns

Pattern matching on AI results based on confidence or field values.

thinklang
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 response

Source: examples/14-match-expression.tl


Try/Catch

Error handling for AI operations. Catches specific runtime error types.

thinklang
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.

thinklang
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.

thinklang
let classified = think<Confident<Category>>("Classify this product")
  with context: item

print classified

The uncertain modifier enforces safe access at compile time:

thinklang
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.

thinklang
let recommendation = think<Recommendation>("Suggest products based on interests")
  with context: {
    profile,
    sensitiveData,
  }
  without context: sensitiveData

print recommendation

Source: examples/16-without-context.tl


Testing

ThinkLang includes a built-in test framework with value assertions and semantic assertions.

thinklang
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:

bash
npx tsx src/cli/index.ts test
npx tsx src/cli/index.ts test --update-snapshots
npx tsx src/cli/index.ts test --replay

Modules (Imports)

Split code across files using import. Types and functions are automatically importable from other .tl files.

thinklang
// 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
}
thinklang
// main.tl
import { Sentiment, analyzeSentiment } from "./types.tl"

let result = analyzeSentiment("Great product!")
print result

No export keyword needed. Paths are relative. Circular imports are detected.


Example Index

#FileFeature
0101-hello-think.tlBasic think call
0202-classification.tlStructured types, with context
0303-extraction.tlNested types, arrays, @maxItems
0404-summarization.tl@description annotations
0505-sentiment.tlConfident<T>
0606-infer-basic.tlinfer expression
0707-with-context.tlContext blocks with multiple variables
0808-confident-values.tlConfident<T> methods
0909-pipeline.tlPipeline |> operator
1010-multi-step.tlChaining multiple think calls
1111-uncertain.tluncertain modifier, .unwrap(), .expect()
1212-reason-block.tlreason blocks with goals and steps
1313-guards.tlGuard constraints, on_fail: retry(n)
1414-match-expression.tlmatch expression with patterns
1515-try-catch.tltry/catch error handling
1616-without-context.tlwithout context for privacy
1717-cache-demo.tlExact-match caching