Formal Semantics

Audience: This section documents Calor's operational semantics for tooling developers, language contributors, and those building AI agents that generate Calor. End users typically don't need to reference this—the contracts you approve and the verification results you review are sufficient for normal workflows.

An agent-friendly language needs a spec that is tighter than "it emits C#."

Emitting C# can hide a lot of semantic gaps. When an agent writes Calor code, it needs to know exactly what that code will do—not what the C# compiler happens to do today, or what one version of the .NET runtime does differently than another.

If the semantics are not crisp, you get "works on this compiler version" behavior, and that kills trust.


The Problem with "It Compiles to X"

Consider a language that "compiles to JavaScript" or "emits C#". What does this code do?

Plain Text
result = f(a(), b()) + g(c())

The answer depends on:

  • Evaluation order: Is a() called before b()? Before c()? The backend language may not specify this.
  • Side effects: If a() modifies state that b() reads, the result depends on call order.
  • Overflow behavior: Does INT_MAX + 1 wrap, trap, or produce undefined behavior?
  • Null semantics: What happens when you dereference null?

If your spec says "emits C#", you've delegated these decisions to C#—but C# may not fully specify them, or may change them between versions.

For agents, this is catastrophic. An agent trained on one set of behaviors will generate incorrect code when those behaviors change or differ.


Calor's Solution: Crisp, Backend-Independent Semantics

Calor defines its semantics independently of any backend. The C# emitter must conform to Calor semantics, not define them.

What We Specify

ConcernCalor's DecisionWhy It Matters
Evaluation OrderStrictly left-to-rightAgents can reason about side-effect ordering
Short-Circuit&&/`
ScopingLexical with explicit shadowingNo surprises from dynamic lookup
Integer OverflowTRAP by defaultSafety-first; silent bugs are unacceptable
Type ConversionsExplicit for narrowingPrevents accidental data loss
NullabilityOption\<T\> for optional valuesNo null pointer exceptions
ExceptionsTyped, with semantic meaningContractViolationException carries context

Why This Matters for Agents

  1. Trainable Rules: Agents can be trained on precise semantics, not approximations.
  2. Testable Behavior: Every semantic decision has corresponding tests.
  3. Version Stability: Semantics are versioned; agents know which rules apply.
  4. Trust: Code behaves the same regardless of backend implementation details.

The Three Pillars

1. Precise Definitions

Every construct has a formal definition:

2. Precise Mapping to .NET

The backend specification defines exactly how Calor semantics map to .NET:

  • When direct emission is safe (C# matches Calor)
  • When additional code is required (checked arithmetic, temporaries)
  • Type mappings (Calor i32 to C# int)
  • Exception types and their contents

3. Stable Versioning

Agents will be trained and prompted against specific rules. The versioning specification ensures:

  • Semantic Versioning: MAJOR.MINOR.PATCH with clear upgrade rules
  • Module Declaration: Code declares which semantics version it targets
  • Compatibility Checking: Compiler warns/errors on version mismatches
  • Migration Path: Clear guidance for upgrading between versions

How It Works: The CNF Pipeline

Calor uses an intermediate representation called Calor Normal Form (CNF) to enforce semantics:

Plain Text
Source -> Parser -> AST -> TypeChecker -> Binder -> CNF Lowering -> CNF -> C# Emitter -> C#
                                                        ^
                                              Semantics enforced here

CNF makes semantics explicit:

  • Explicit temporaries: Evaluation order is baked in
  • Explicit control flow: No implicit fall-through
  • Explicit types: No implicit conversions
  • Explicit labels/branches: Short-circuit lowered to control flow

See Normal Form for the full CNF specification.


Test-Backed Semantics

Every semantic decision is backed by tests:

SemanticTestWhat It Verifies
S1FunctionArguments_EvaluatedLeftToRightArguments evaluated in order
S2BinaryOperators_EvaluatedLeftToRightOperators evaluated in order
S3LogicalAnd_ShortCircuits&& doesn't evaluate right if left is false
S4LogicalOr_ShortCircuits`
S5LexicalScoping_InnerReadsOuterInner scope accesses outer variables
S6ReturnFromNestedScopeReturn unwinds to function boundary
S7IntegerOverflow_TrapsOverflow throws OverflowException
S8NumericConversion_IntToFloatImplicit widening works
S9OptionNone_BehavesCorrectlyOption\<T\> semantics correct
S10RequiresFails_ThrowsContractViolationContracts throw with function ID
S11SemanticsVersionMismatch_EmitsDiagnosticVersion checking works

For Agent Developers

If you're building agents that generate Calor code:

  1. Train on the spec, not the output: Use the Core Semantics as the source of truth
  2. Version your prompts: Include §SEMVER{1.0.0} in generated modules
  3. Test against semantics tests: Your generated code should pass the same tests
  4. Don't assume C# behavior: If it's not in the Calor spec, don't rely on it

Current Version

Semantics Version: 1.0.0

This is the initial formal semantics specification.


Learn More