Fuzzy Logic

Blurring code and reasoning with is, as, pick, and vary

Overview

Most of Kaya behaves like an ordinary scripting language: deterministic, predictable, repeatable. But a handful of language features are backed by an LLM at runtime, letting you express fuzzy intent — "is this message angry?", "pick the best headline", "classify this into one of these buckets" — directly in code.

These are the fuzzy-logic features:

  • is — semantic matching: does a value mean a description?
  • as — LLM classification: coerce a value into a refined literal type
  • pick — LLM-powered selection: choose the best item from a list
  • vary — prompt variation: generate alternative phrasings of a string

Because each call invokes a model, these features are non-deterministic and asynchronous. Use them where judgement matters; reach for ordinary operators (==, includes, etc.) where exactness matters.

Model-backed examples require admitted model bindings and a configured default when no model is supplied; see native model setup. Supply the surrounding input bindings shown by each fragment. These examples are not provider-free first-run programs.

Semantic Matching (is)

The is operator asks an LLM whether a value semantically matches a natural-language description, returning a boolean.

Syntax: <value> is <description>

if userMessage is "an intent to leave the conversation" {
  "Goodbye! Thanks for chatting."
}

if response is "positive sentiment" {
  "Glad you liked it!"
} else if response is "negative sentiment" {
  "Sorry to hear that — let me help."
}

// Loop until the user signals they're done
while userInput is "a request to continue" {
  userInput = generate(message: "Anything else?")
}
Run in Browser Text model required

Matching Against Semantic Types

You can match against a type that carries a semantic constraint:

type PositiveMessage = string + { is: "positive sentiment" }

if message is PositiveMessage {
  "Matched the semantic type constraint"
}

Important Limitation

is supports a standalone binding (let result = value is "description") or a direct condition in if, else if or while. Combining it with && or || is a parse error; combinations with ?? or ternary ? : parse but are refused during execution. Keep the semantic judgment separate from ordinary logical expressions.

// ❌ ERROR: is cannot be composed
// if x == 3 && foo is "positive" { }
// let result = foo is "positive" ? "yes" : "no"

// ✅ Nest conditions instead
if x == 3 {
  if foo is "positive" {
    "Both conditions met"
  }
}

How it works: the value is converted to a string, the LLM is asked whether it semantically matches the description, and the result is converted to a boolean.

See also: Semantic Match operator reference and Semantic Constraints.

LLM Classification (as)

The as operator performs type coercion. For ordinary types the cast is deterministic (no model involved); for refined literal types it uses an LLM to classify the value into one of the allowed options.

Deterministic Casts (no LLM)

let f = "3"
let g = f as number        // → 3
let h = f as int           // → 3 (floors decimal)
let i = 42 as string       // → "42"
let j = "true" as boolean  // → true
Run in Browser No model needed

LLM-Based Classification (refined literals)

Define a literal union where each option carries an is description, then cast a value into it. The LLM picks the best-matching option.

type Sentiment = "positive" + { is: "happy or satisfied" }
               | "negative" + { is: "sad or dissatisfied" }
               | "neutral"  + { is: "factual or indifferent" }

let mood = userMessage as Sentiment          // Operator form
let mood = as(value: userMessage, type: Sentiment)  // Function form
let mood = userMessage.as(Sentiment)          // Method form
Run in Browser Text model required

All three forms are equivalent. Classification is ideal for routing, triage, and tagging:

type Priority = "urgent" + { is: "critical, needs immediate attention" }
              | "high"   + { is: "important but not critical" }
              | "low"    + { is: "nice-to-have" }

let p = ticketText as Priority
if p == "urgent" {
  "Paging the on-call engineer..."
}

LLM Selection (pick)

pick asks an LLM to choose the single best item from an array given a natural-language criterion. It supports method, function, and (via metadata) file-aware forms.

// Method syntax (recommended)
let best = items.pick(criteria: "highest rated")

// Function syntax
let best = pick(items: items, criteria: "best")

// Criteria can be dynamic
let myCriteria = "the healthiest option"
let healthy = foods.pick(criteria: myCriteria)

The result is a normal value, so it chains:

let bestName = users.pick(criteria: "most experienced").name
let firstLetter = foods.pick(criteria: "dessert")[0]

pick auto-detects file types from variable metadata, so it works for selecting among images and other files as well as plain values.

Prompt Variation (vary)

vary generates alternative phrasings of a prompt string — useful for A/B testing prompts, creative exploration, and generating diverse inputs.

// Generate 5 variations
let variations = "Write a haiku about nature".vary(i: 5)

// Include the original as the first item
let all = "Describe Mars".vary(i: 3, includeOriginal: true)
// Returns: ["Describe Mars", "variation1", "variation2", "variation3"]

// Guide the variance
let approaches = "Explain recursion".vary(
  i: 3,
  prompt: "Use different teaching approaches: analogy, example, and formal definition"
)

// Function syntax
let taglines = vary(
  value: "Generate a product tagline",
  i: 4,
  prompt: "Vary the tone from serious to playful"
)

Behavior:

  • includeOriginal: false (default) → returns exactly i LLM-generated variations
  • includeOriginal: true → returns i + 1 items (original first)

A common pattern is to fan a single idea out into variants and generate from each:

for variant in "Write a welcome message".vary(i: 3) {
  let output = generate(message: variant)
  "Variant '{{variant}}' → {{output}}"
}

When to Use Fuzzy vs. Exact

NeedUse
Exact string/number comparison==, ===, includes
"Does this mean X?"is
"Which bucket does this fall into?"as (refined literal)
"Which of these is best?"pick
"Give me other phrasings"vary

Because every fuzzy operation calls a model, prefer deterministic operators whenever the answer is well-defined — reserve fuzzy logic for genuinely subjective or natural-language judgement.

See Also