Skip to content

3 4 discourse scope and program synthesis

TransCompilator edited this page Nov 26, 2026 · 1 revision

3.4 Discourse Scope and Program Synthesis

Source: https://deepwiki.com/PonvasanthPon/TransCompilator/3-4-discourse-scope-and-program-synthesis

Discourse Scope and Program Synthesis

Relevant source files

The following files were used as context for generating this wiki page:

Purpose and Scope

The discourse_scope module in tc-semantics manages paragraph-level semantic constructs, structural range introductions, referential loop continuations, post-loop closers, and existential check whether validations. Rather than relying on machine learning, tc-r1 uses deterministic text pattern matching and syntactic boundary analysis to map narrative English discourse patterns into structured AST nodes and Core-7 control flow operations.

Sources: crates/tc-semantics/src/discourse_scope.rs:1-11


Paragraph-Level State and Range Declarations

Narrative English programs frequently initialize complex states using imaginative or hypothetical openings such as Imagine a red box starting with 5 and a blue box starting with 16. [crates/tc-semantics/src/discourse_scope.rs:107-108]. The engine parses these structures via parse_state_declaration [crates/tc-semantics/src/discourse_scope.rs:109].

Implementation Details

  1. Trigger Recognition: The sentence must begin with words like imagine, suppose, assume, or consider [crates/tc-semantics/src/discourse_scope.rs:111-117].
  2. Clause Splitting: The payload after the introductory verb is split across top-level and conjunctions [crates/tc-semantics/src/discourse_scope.rs:118-122].
  3. Marker Resolution: Each clause is scanned for initialization markers such as starting with, holding, containing, or with [crates/tc-semantics/src/discourse_scope.rs:126-128].
  4. Normalizer: Noun phrases are normalized to singular snake_case identifiers (e.g., red boxes becomes red_box) [crates/tc-semantics/src/discourse_scope.rs:89-105, 134-153].
graph TD
    A["Text Input"] --> B["parse_state_declaration"]
    B --> C{"Starts with Imagine/Suppose/Assume?"}
    C -->|Yes| D["Split Top-Level 'and' Clauses"]
    C -->|No| E["Return None"]
    D --> F["Extract Assignment Markers starting_with/holding"]
    F --> G["Normalize Noun Phrase to snake_case"]
    G --> H["Emit Vector of (Name, Value) Pairs"]

    sub:Sources: crates/tc-semantics/src/discourse_scope.rs:109-156
Loading

Sources: crates/tc-semantics/src/discourse_scope.rs:89-156


Global and Local Quantifiers

Quantifiers govern loops and boolean assertions across collections or ranges. The quantifier module distinguishes between global statement quantifiers (which desugar into Core-7 Loop statements) and local boolean quantifiers (which desugar into short-circuiting prelude loops with boolean temporaries) [crates/tc-semantics/src/quantifier.rs:1-6].

Quantifier Types and Domains

  • QuantDomainSpec: Represents either an inclusive integer range (from <start> to <end> [by <step>]) or a fixed-size array variable (Array { arr }) [crates/tc-semantics/src/quantifier.rs:10-26].
  • GlobalQuantifier: Represents statements like For every <var> from ... [crates/tc-semantics/src/quantifier.rs:64-74, 163-166].
  • LocalQuantSpec: Captures universal (Every), existential (Some), and negative existential (No) checks inside boolean condition contexts [crates/tc-semantics/src/quantifier.rs:28-61].
graph TD
    A["Natural Language Statement"] --> B["quantifier::parse_global"]
    B --> C{"Starts with 'For every/each'?"}
    C -->|Yes| D["Parse Domain QuantDomainSpec"]
    D --> E["Extract Optional Inline Body after comma"]
    E --> F["Emit GlobalQuantifier AST node"]
    C -->|No| G["Pass to Local Quantifier / Cond Parser"]

    sub:Sources: crates/tc-semantics/src/quantifier.rs:163-204
Loading

Sources: crates/tc-semantics/src/quantifier.rs:1-204


Pass Filters, Move Guards, and Stop Conditions

Loop behaviors are constrained via conditional guards and interruption triggers embedded within the discourse.

Key Parsing Functions

  • parse_move_guard: Recognizes conditional loop bypass rules, such as if a move would ever cause either box to drop below 5, skip that move [crates/tc-semantics/src/discourse_scope.rs:158-195]. It detects limit phrases like drop below, fall below, or go below and extracts the numeric floor value [crates/tc-semantics/src/discourse_scope.rs:178-194].
  • parse_stop_condition: Detects immediate loop termination instructions like Stop immediately as soon as <condition> [crates/tc-semantics/src/discourse_scope.rs:197-202].
  • parse_largest_tracker: Identifies extremum tracking requests (e.g., keep track of the largest value you printed or track the minimum) [crates/tc-semantics/src/discourse_scope.rs:49-87], returning a LargestTracker struct [crates/tc-semantics/src/discourse_scope.rs:50-54] that dictates whether maximum or minimum metrics are maintained [crates/tc-semantics/src/discourse_scope.rs:83-86].
graph TD
    A["Discourse Sentence"] --> B{"Sentence Pattern"}
    B -->|Move Guard| C["parse_move_guard"]
    B -->|Stop Condition| D["parse_stop_condition"]
    B -->|Extremum Tracker| E["parse_largest_tracker"]
    C --> F["Extract Numeric Floor & Skip Action"]
    D --> G["Extract Termination Condition"]
    E --> H["Emit LargestTracker maximum bool"]

    sub:Sources: crates/tc-semantics/src/discourse_scope.rs:49-202
Loading

Sources: crates/tc-semantics/src/discourse_scope.rs:49-202


Check-Whether Sentences and Existential Validation

Existential checks query data domains using phrasing such as check whether any prime divides n. These are modeled via CheckSpec, CheckKind, and CheckDomain structures [crates/tc-semantics/src/discourse_scope.rs:12-47].

Data Structures

  • CheckKind: Categorizes quantification into Any, Every, or No [crates/tc-semantics/src/discourse_scope.rs:13-21].
  • CheckDomain: Defines the scope of the inner check loop:
    • Range { start, end }: Inclusive numeric bounds [crates/tc-semantics/src/discourse_scope.rs:26-27].
    • Smaller { start }: Values smaller than the active loop referent [crates/tc-semantics/src/discourse_scope.rs:28-29].
    • Larger { end }: Values larger than the active loop referent [crates/tc-semantics/src/discourse_scope.rs:30-31].
    • List { name }: An existing list variable collection [crates/tc-semantics/src/discourse_scope.rs:32-34].
  • CheckSpec: Combines kind, noun, domain, and predicate text into a unified validation rule [crates/tc-semantics/src/discourse_scope.rs:36-47].
graph TD
    A["Natural Language 'Check Whether' Sentence"] --> B["discourse_scope::CheckSpec"]
    B --> C["Assign CheckKind Any/Every/No"]
    B --> D["Build CheckDomain Range/Smaller/Larger/List"]
    B --> E["Extract Predicate Text divides / conditions"]
    C --> F["Lower into Short-Circuiting Core-7 Logic"]
    D --> F
    E --> F

    sub:Sources: crates/tc-semantics/src/discourse_scope.rs:12-47
Loading

Sources: crates/tc-semantics/src/discourse_scope.rs:12-47


Condition Precedence and Boolean Logic Integration

The parsed discourse conditions interact with the boolean condition parser (cond.rs), which enforces ACE-inspired operator binding precedence [crates/tc-semantics/src/cond.rs:1-14]:

  1. , or (loosest) [crates/tc-semantics/src/cond.rs:3]
  2. , and [crates/tc-semantics/src/cond.rs:3]
  3. or [crates/tc-semantics/src/cond.rs:3]
  4. and [crates/tc-semantics/src/cond.rs:3]
  5. not [crates/tc-semantics/src/cond.rs:3]
  6. Atomic comparisons (==, <, >, etc.) (tightest) [crates/tc-semantics/src/cond.rs:3, 136]

Conditions are split recursively using split_condition [crates/tc-semantics/src/cond.rs:69-185] while respecting parenthesis depth and local quantifier boundaries (satisfies, where, such that) [crates/tc-semantics/src/cond.rs:73, 123-132], ensuring deterministic lowering into Core-7 boolean AST expressions (AstExpr) and prelude statements (AstStmt) [crates/tc-semantics/src/cond.rs:17-26].

Sources: crates/tc-semantics/src/cond.rs:1-185


Clone this wiki locally