Skip to content

Commit 3b78b5f

Browse files
hyperpolymathclaude
andcommitted
Sec 18-19 in Pest + AST + parser: token economics + Kategoria types
Pest grammar: - budget_annotation, branch_modifier (speculative|cached), focus_annotation - purity_annotation extended with @neural, neural_dispatch + neural_target - choreo_batch, model-routing locale params (remote_params, edge_params, cluster_params) - type_expr split into type_expr (with effect_annotation?) + type_expr_base - dependent_type, refinement_type, path_type, graded_type, effect_annotation - GADT syntax: upper_ident<type_expr, ...> before bare upper_ident AST types: - BudgetAnnotation, BranchModifier, NeuralTarget, FocusAnnotation - DepArg, RefinementType, GradedType, Grade, EffectType, Effect - Updated: Branch (modifier), GivenClause (focus), AgentDecl (budget) - Updated: FunctionDecl (neural_dispatch), Purity (+Neural) - Updated: ChoreoStep (+Batch), LocaleConstructor (model routing params) Parser: all new rules wired to AST builders. Eval: new match arms (stubs — reference evaluator ignores modifiers). Tests: all 105 pass, zero clippy warnings. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 7deb170 commit 3b78b5f

7 files changed

Lines changed: 472 additions & 55 deletions

File tree

crates/oo7-cli/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ fn run_demo() {
189189
BranchArm {
190190
label: "accept".to_string(),
191191
given: Some(GivenClause {
192+
focus: None,
192193
items: vec![
193194
GivenItem::Named(
194195
"confidence".to_string(),
@@ -220,6 +221,7 @@ fn run_demo() {
220221
BranchArm {
221222
label: "revise".to_string(),
222223
given: Some(GivenClause {
224+
focus: None,
223225
items: vec![
224226
GivenItem::Named(
225227
"confidence".to_string(),
@@ -264,6 +266,7 @@ fn run_demo() {
264266
BranchArm {
265267
label: "reject".to_string(),
266268
given: Some(GivenClause {
269+
focus: None,
267270
items: vec![
268271
GivenItem::Named(
269272
"has_fatal_flaws".to_string(),

crates/oo7-core/src/ast.rs

Lines changed: 186 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
// The AST preserves the Harvard Architecture invariant:
77
// DataExpr and ControlExpr are separate enum variants.
88
// There is no way to construct a DataExpr containing control flow.
9+
//
10+
// Sections 18-19: Token Economics types (budget, branch modifiers,
11+
// neural dispatch, focus, batch) and Kategoria types (dependent,
12+
// refinement, path, graded, effect) are included.
913

1014
use serde::{Deserialize, Serialize};
1115

@@ -111,9 +115,11 @@ pub enum ControlStmt {
111115
scrutinee: ControlExpr,
112116
arms: Vec<(Pattern, ControlExprOrBlock)>,
113117
},
114-
/// `branch [traced "label"] { | arm -> body ... }`
118+
/// `branch [modifier] [traced "label"] { | arm -> body ... }`
115119
/// THE HERMENEUTIC MOMENT — the only nondeterministic construct
120+
/// Section 18.2: modifier can be `speculative` or `cached`
116121
Branch {
122+
modifier: Option<BranchModifier>,
117123
traced: Option<String>,
118124
arms: Vec<BranchArm>,
119125
},
@@ -134,6 +140,148 @@ pub enum ControlStmt {
134140
Expr(ControlExpr),
135141
}
136142

143+
// ============================================================
144+
// Section 18.1: Budget Annotation
145+
// ============================================================
146+
147+
/// Budget annotation: @budget(N) — token budget as linear resource.
148+
/// Eclexia's shadow pricing applied to tokens.
149+
#[derive(Debug, Clone, Serialize, Deserialize)]
150+
pub struct BudgetAnnotation {
151+
pub tokens: i64,
152+
}
153+
154+
// ============================================================
155+
// Section 18.2: Branch Modifier
156+
// ============================================================
157+
158+
/// Branch modifier — controls branch evaluation strategy.
159+
/// `speculative`: parallel evaluation of all arms (Chapel-style).
160+
/// `cached`: trace memoisation (Oblibeny reduction).
161+
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
162+
pub enum BranchModifier {
163+
/// Evaluate all arms in parallel, select best result.
164+
Speculative,
165+
/// Memoise via trace records — skip re-evaluation on cache hit.
166+
Cached,
167+
}
168+
169+
// ============================================================
170+
// Section 18.3: Neural Dispatch
171+
// ============================================================
172+
173+
/// Neural dispatch target — specifies which neurosymbolic backend
174+
/// to route a @neural function to.
175+
#[derive(Debug, Clone, Serialize, Deserialize)]
176+
pub enum NeuralTarget {
177+
/// Hypatia ESN/RBF system: `hypatia("model_name")`
178+
Hypatia(String),
179+
/// ONNX model: `onnx("model.onnx")`
180+
Onnx(String),
181+
/// Custom backend: `custom("backend_name")`
182+
Custom(String),
183+
}
184+
185+
// ============================================================
186+
// Section 18.4: Focus Annotation
187+
// ============================================================
188+
189+
/// Focus annotation on given clause: @focus(N).
190+
/// Allocates N tokens of context window to the evidence.
191+
/// Ephapax-style attention budgeting.
192+
#[derive(Debug, Clone, Serialize, Deserialize)]
193+
pub struct FocusAnnotation {
194+
pub tokens: i64,
195+
}
196+
197+
// ============================================================
198+
// Section 19: Kategoria Advanced Types
199+
// ============================================================
200+
201+
/// Dependent type argument — either a value (Data) or a type name.
202+
/// Value arguments are Harvard-separated: only Data expressions.
203+
#[derive(Debug, Clone, Serialize, Deserialize)]
204+
pub enum DepArg {
205+
/// A runtime value (Data expression) used as type index
206+
Value(DataExpr),
207+
/// A type argument (stored as raw string for now)
208+
Type(String),
209+
}
210+
211+
/// Refinement type: { x : T | P }
212+
/// The predicate P is a Data expression (Harvard-separated).
213+
#[derive(Debug, Clone, Serialize, Deserialize)]
214+
pub struct RefinementType {
215+
/// The bound variable name
216+
pub var: String,
217+
/// The base type (as raw string)
218+
pub base_type: String,
219+
/// Left side of the predicate
220+
pub predicate_left: DataExpr,
221+
/// Predicate operator
222+
pub predicate_op: PredOp,
223+
/// Right side of the predicate
224+
pub predicate_right: DataExpr,
225+
}
226+
227+
/// Graded type: @graded(grade) T — semiring-indexed resource tracking.
228+
/// In 007, grading tracks TOKEN COST.
229+
#[derive(Debug, Clone, Serialize, Deserialize)]
230+
pub struct GradedType {
231+
/// The grade (exact count, omega, zero, or symbolic)
232+
pub grade: Grade,
233+
/// The inner type (as raw string)
234+
pub inner_type: String,
235+
}
236+
237+
/// Grade in a graded type — how many times a resource may be used.
238+
#[derive(Debug, Clone, Serialize, Deserialize)]
239+
pub enum Grade {
240+
/// Exact usage count
241+
Exact(i64),
242+
/// Unrestricted usage (omega)
243+
Omega,
244+
/// Phantom/unused (zero)
245+
Zero,
246+
/// Symbolic grade from a semiring
247+
Symbolic(String),
248+
}
249+
250+
/// Effect type: T !{effects} — tracked side effects in the type system.
251+
/// Makes the Harvard boundary GRADUAL.
252+
#[derive(Debug, Clone, Serialize, Deserialize)]
253+
pub struct EffectType {
254+
/// The base type (as raw string)
255+
pub base_type: String,
256+
/// The set of effects
257+
pub effects: Vec<Effect>,
258+
}
259+
260+
/// Individual effect — one tracked side effect.
261+
#[derive(Debug, Clone, Serialize, Deserialize)]
262+
pub enum Effect {
263+
/// May send messages
264+
Send,
265+
/// May spawn agents
266+
Spawn,
267+
/// May move between locales
268+
Migrate,
269+
/// May mutate state
270+
State,
271+
/// Produces decision traces
272+
Trace,
273+
/// Invokes neurosymbolic system
274+
Neural,
275+
/// Costs N tokens
276+
Tokens(i64),
277+
/// User-defined effect
278+
Custom(String),
279+
}
280+
281+
// ============================================================
282+
// Branch Arms and Given Clauses
283+
// ============================================================
284+
137285
/// A branch arm — one option in a hermeneutic branch point.
138286
#[derive(Debug, Clone, Serialize, Deserialize)]
139287
pub struct BranchArm {
@@ -151,8 +299,12 @@ pub struct BranchArm {
151299
///
152300
/// INVARIANT: All items are DataExpr. Cannot contain control flow.
153301
/// This is enforced by the grammar AND the AST type.
302+
/// Section 18.4: optional focus annotation for attention budgeting.
154303
#[derive(Debug, Clone, Serialize, Deserialize)]
155304
pub struct GivenClause {
305+
/// Optional @focus(N) attention budget
306+
pub focus: Option<FocusAnnotation>,
307+
/// The evidence items
156308
pub items: Vec<GivenItem>,
157309
}
158310

@@ -270,10 +422,13 @@ pub enum Pattern {
270422
// ============================================================
271423

272424
/// Agent declaration.
425+
/// Section 18.1: includes optional budget annotation.
273426
#[derive(Debug, Clone, Serialize, Deserialize)]
274427
pub struct AgentDecl {
275428
pub name: String,
276429
pub capabilities: Vec<String>,
430+
/// Optional token budget (Section 18.1)
431+
pub budget: Option<BudgetAnnotation>,
277432
pub implements: Vec<(String, String)>,
278433
pub locale: Option<String>,
279434
pub data_blocks: Vec<DataBinding>,
@@ -361,16 +516,21 @@ pub struct BehaviourDecl {
361516
}
362517

363518
/// Function declaration with purity annotation.
519+
/// Section 18.3: purity includes Neural variant; body may contain
520+
/// neural_dispatch for @neural functions.
364521
#[derive(Debug, Clone, Serialize, Deserialize)]
365522
pub struct FunctionDecl {
366523
pub purity: Purity,
367524
pub name: String,
368525
pub params: Vec<(String, String)>,
369526
pub return_type: Option<String>,
370527
pub body: Vec<ControlStmt>,
528+
/// Neural dispatch target (only for @neural functions)
529+
pub neural_dispatch: Option<NeuralTarget>,
371530
}
372531

373532
/// Purity level — part of the effect type system (Layer 8).
533+
/// Section 18.3: Neural variant added for neurosymbolic dispatch.
374534
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
375535
pub enum Purity {
376536
/// Guaranteed termination, no side effects, addition-only
@@ -379,6 +539,8 @@ pub enum Purity {
379539
Pure,
380540
/// All effects permitted (default)
381541
Impure,
542+
/// Neurosymbolic dispatch — routes to trained model, ~zero tokens
543+
Neural,
382544
}
383545

384546
// ============================================================
@@ -400,6 +562,7 @@ pub struct ChoreographyDecl {
400562
}
401563

402564
/// A step in a choreography.
565+
/// Section 18.5: Batch variant added for amortised decisions.
403566
#[derive(Debug, Clone, Serialize, Deserialize)]
404567
pub enum ChoreoStep {
405568
/// Communication: `sender -> receiver : message_name(args...)`
@@ -432,6 +595,11 @@ pub enum ChoreoStep {
432595
},
433596
/// Goto: `goto label`
434597
Goto(String),
598+
/// Batch: `batch { steps... }` — amortised decisions (Section 18.5)
599+
/// Presents N decisions to the LLM as one structured prompt.
600+
Batch {
601+
steps: Vec<ChoreoStep>,
602+
},
435603
}
436604

437605
/// A branch arm in a choreography.
@@ -446,7 +614,7 @@ pub struct ChoreoBranchArm {
446614
}
447615

448616
// ============================================================
449-
// Locales
617+
// Locales (Section 18.6: model-routing extensions)
450618
// ============================================================
451619

452620
/// Locale declaration — names a compute location.
@@ -459,18 +627,28 @@ pub struct LocaleDecl {
459627
}
460628

461629
/// Locale constructor — where computation executes.
630+
/// Section 18.6: Remote, Edge, Cluster extended with model-routing params.
462631
#[derive(Debug, Clone, Serialize, Deserialize)]
463632
pub enum LocaleConstructor {
464633
/// Local execution
465634
Local,
466635
/// GPU device: `GPU(device: N)`
467636
Gpu { device: i64 },
468-
/// Remote URL: `Remote(url: "...")`
469-
Remote { url: String },
470-
/// Edge region: `Edge(region: "...")`
471-
Edge { region: String },
472-
/// Cluster of nodes: `Cluster(nodes: [...])`
473-
Cluster { nodes: Vec<String> },
637+
/// Remote with optional model routing: `Remote(url: "...", model: "...")`
638+
Remote {
639+
url: Option<String>,
640+
model: Option<String>,
641+
},
642+
/// Edge with optional model routing: `Edge(region: "...", model: "...")`
643+
Edge {
644+
region: Option<String>,
645+
model: Option<String>,
646+
},
647+
/// Cluster with nodes and optional models: `Cluster(nodes: [...], models: [...])`
648+
Cluster {
649+
nodes: Vec<String>,
650+
models: Vec<String>,
651+
},
474652
}
475653

476654
/// Locale expression — either a named locale or an inline constructor.

crates/oo7-core/src/eval.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,16 @@ impl Evaluator {
468468
}
469469
}
470470

471-
ControlStmt::Branch { traced, arms } => {
471+
ControlStmt::Branch {
472+
modifier: _modifier,
473+
traced,
474+
arms,
475+
} => {
476+
// Section 18.2: modifier (speculative/cached) is ignored in
477+
// the reference evaluator — it affects scheduling strategy,
478+
// not observable semantics. A production runtime would fork
479+
// evaluation streams for speculative and check trace cache
480+
// for cached.
472481
env.push_scope();
473482
let r = self.eval_branch(traced, arms, env);
474483
env.pop_scope();

0 commit comments

Comments
 (0)