Skip to content

Section 4: Control Flow

TECHSPEC-04 — Section 4 · Version 0.1 · Status: Initial Draft

Status: draft. This section is an Initial Draft (Section 57 is a Working Register). It describes intended or proposed behavior, not a finalized, implemented language rule. Do not treat examples here as normative.

1. Control Flow Philosophy

Control flow determines the order in which program instructions execute.

The language must provide explicit, predictable control flow with compiler-enforced safety rules.

2. Conditional Statements

The language supports:

  • IF
  • ELSE IF
  • ELSE

Example:

IF account.balance > 0:
    RETURN SUCCESS(account)
ELSE:
    RETURN FAILURE(InsufficientFunds)
END IF

Rules: - Conditions must evaluate to Boolean. - Every branch must have a clearly defined boundary. - Nested conditions must be explicitly structured. - The compiler must detect unreachable branches where possible.

3. Pattern Matching

The language supports MATCH for handling multiple possible values.

Example:

MATCH result:
    SUCCESS(value):
        RETURN value

    FAILURE(error):
        RETURN HANDLE(error)
END MATCH

Rules: - Every possible variant must be handled or explicitly covered by a default branch. - Missing cases must produce compiler errors. - Pattern matching must respect the underlying type definitions. - Unreachable patterns must be rejected.

4. Loops

The initial version supports:

4.1 FOR

Iterates over a collection.

FOR user IN users:
    PROCESS(user)
END FOR

4.2 WHILE

Repeats while a Boolean condition remains true.

WHILE attempts < 3:
    attempts = attempts + 1
END WHILE

Rules: - Loop variables must have known types. - Collection iteration must respect collection types. - Loop conditions must evaluate to Boolean. - Infinite loops should be identifiable where possible. - Loops must have explicit boundaries.

5. Loop Safety

The compiler should support optional loop invariants and termination conditions.

Example:

WHILE balance > 0:
    INVARIANT balance >= 0
    DECREASES balance

    balance = balance - 1
END WHILE

Rules: - Loop invariants must remain true throughout iteration. - DECREASES identifies a measure intended to demonstrate termination. - Formal termination claims require actual verification. - The compiler must not assume that a loop terminates merely because a DECREASES declaration exists.

6. Function Exit

The language supports:

  • RETURN: Exit a function with its declared return value.
  • BREAK: Exit the nearest enclosing loop.
  • CONTINUE: Begin the next loop iteration.

Rules: - Every reachable function exit must satisfy applicable return and contract requirements. - Functions returning a value must not have reachable paths without a valid return. - BREAK and CONTINUE must only appear in valid loop contexts. - Unreachable statements should be reported.

7. Execution Path Analysis

The compiler must: - Analyze reachable execution paths. - Detect missing return paths. - Detect unreachable code. - Check exhaustive pattern matching. - Evaluate applicable contracts across all relevant paths. - Identify paths whose behavior cannot be established.

8. Design Goal

AI agents must not be able to accidentally omit important execution paths, ignore possible outcomes, or introduce ambiguous control flow without compiler detection.


Source file: TECHSPEC-4.md (preserved in source-original/LANG/). See the specification index, the glossary, and the cross-reference index.