Skip to content
Merged
6 changes: 4 additions & 2 deletions docs/design-substrate-carrier-port-program.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,13 @@ This is the same shape as the core port program at a different layer: **per-meth
0. **Metadata stays in lens-local lookup tables.** Nothing moves. `*_templates()` (or its successor) stays as the authoritative per-method metadata surface; lenses consume it directly. No substrate change, no std/ change. Valid if the E-I evidence shows the metadata is cleanly separable from the cost/descent carriers — then co-locating isn't a modeling win.
1. **Extend type declarations with method-metadata annotations.** Needs substrate support for field-level refinements (currently partial per DB-11). Largest substrate change; smallest std/ surface change.
2. **Separate metadata carrier per algebra.** Declare `OrderedRingMetadata`, `PartialFunctionMetadata`, etc. paired with the type declarations. No substrate change; additional std/ surface (one carrier per algebra).
3. **Unified `MethodContract` carrier.** One generic carrier indexed by `(algebra_id, method_id)` that lenses query by id. No substrate change; minimal std/ surface growth (one carrier total). Closest in shape to a `TemplateArgumentBinding`-style lookup.
3. **Unified `MethodContract` carrier.** One generic carrier indexed by `(algebra_id: DeclarationId, method_id: DeclarationId)` that lenses query by structural declaration identity. No substrate change; minimal std/ surface growth (one carrier total). Closest in shape to a `TemplateArgumentBinding`-style lookup.

**Current consumers:** `ordered_ring_templates()`, `partial_function_templates()` and their siblings in `dsl/std/algebra.dag:447-569`; the complexity / cost analyses that read `size_effect` / `cost_shape` / `callback_element_position` off the returned templates.

**Recommendation (deferred):** this scoping doc does not pick a direction. The call interacts with family I (`SubValueRelation` + cost) — a lens that consumes both per-method metadata and `CostBound` may want them co-located, which favors option 3. But if E-I pre-flight evidence shows the metadata is cleanly separable from cost/descent, option 0 ("keep lens-local") is also live. Defer the pick until **E-P consumer wiring** (and optional cementing receipts) narrow the coupling question.
**Decision:** pick **Option 3, unified `MethodContract` carrier**. The E-I/E-P evidence did not show `size_effect`, `cost_shape`, and `callback_element_position` are cleanly separable from the cost/descent path. Instead, `cost.dag` and `complexity.dag` both still name live call-site consumption of the E-C/E-I/E-P facts as their remaining behavioral gap, while the existing `AlgebraFieldTemplate` rows carry `size_effect`, `cost_shape`, and callback element position together for the same method. Splitting those facts back into lens-local tables (Option 0) would preserve the current duplicate lookup story at exactly the point the lenses need a shared method contract. Type-declaration annotations (Option 1) require substrate field-level refinement work, which is out of scope for this design-call lane. Per-algebra carriers (Option 2) avoid substrate work but multiply the same contract shape across `OrderedRingMetadata`, `PartialFunctionMetadata`, and siblings. A single `MethodContract { algebra_id: DeclarationId, method_id: DeclarationId, size_effect, cost_shape, callback_element_position }` keeps one std-level authority keyed by structural declaration identity and matches the existing template-argument lookup style without widening substrate.

**Live receipt:** `src/v3/std/algebra.dag` declares `MethodContract`; `src/v3/lenses/cost.dag` imports it and includes `method_contract_cost_shape` as the minimal demo consumer. Bulk migration of `cost.dag` / `complexity.dag` to live call-site `MethodContract` lookup remains follow-up work after the §6a shape is locked.

**Cross-reference:** see PR #654's investigation receipt for the full Lane G findings that surfaced this question.

Expand Down
1 change: 0 additions & 1 deletion dsl/std/algebra.dag
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,3 @@ fn algebra_type_param_names(profile: AlgebraProfile) -> List<String> {
PartialFunctionProfile => ["K", "V"]
}
}

24 changes: 24 additions & 0 deletions scripts/regen_runtime_mirrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,30 @@


DAG_COST_TEMPLATE = """#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CollectionSizeEffect {
ShrinkEffect,
ProjectionEffect,
IdentityEffect,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CostShape {
ShapeConstant,
ShapeLinearScan,
ShapeIterateBody,
ShapeSortBody,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MethodContract {
pub algebra_id: DeclarationId,
pub method_id: DeclarationId,
pub size_effect: Option<CollectionSizeEffect>,
pub cost_shape: Option<CostShape>,
pub callback_element_position: Option<i64>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DegreeAtLeastTwo {
DegreeTwo,
DegreeSuccessor { previous: Box<DegreeAtLeastTwo> },
Expand Down
2,719 changes: 1,366 additions & 1,353 deletions src/v3/compiler/src/bootstrap_generated.rs

Large diffs are not rendered by default.

2,559 changes: 1,286 additions & 1,273 deletions src/v3/compiler/src/bootstrap_generated_without_parse_surface.rs

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions src/v3/compiler/src/dag_cost_generated.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
// AUTO-GENERATED from `src/v3/std/algebra.dag`.
// Regenerate instead of hand-editing.

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CollectionSizeEffect {
ShrinkEffect,
ProjectionEffect,
IdentityEffect,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CostShape {
ShapeConstant,
ShapeLinearScan,
ShapeIterateBody,
ShapeSortBody,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MethodContract {
pub algebra_id: DeclarationId,
pub method_id: DeclarationId,
pub size_effect: Option<CollectionSizeEffect>,
pub cost_shape: Option<CostShape>,
pub callback_element_position: Option<i64>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DegreeAtLeastTwo {
DegreeTwo,
Expand Down
3 changes: 3 additions & 0 deletions src/v3/compiler/src/lens_cost_symbolic_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub struct SymbolicCostEntry {
pub fn symbolic_cost_of(p0: &Dag, p1: &PortId) -> Lookup<SymbolicCost> {
lookup_cost(&(compute_symbolic_costs(p0)), p1)
}
pub fn method_contract_cost_shape(p0: &MethodContract) -> Option<CostShape> {
((p0).cost_shape).clone()
}
pub fn compute_symbolic_costs(p0: &Dag) -> Vec<SymbolicCostEntry> {
((p0).nodes())
.iter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ src/v3/spec/go.dag 119 1057003 e21bbda50cdc82a9
src/v3/spec/python.dag 87 554606 d8aa76b9e7a30d49
src/v3/spec/rust.dag 135 1123734 4e900c6b6e7d9189
src/v3/spec/v3_l1.dag 8 2238 1f55d782265c3bfe
src/v3/std/algebra.dag 36 284938 29978b1b9f257877
src/v3/std/algebra.dag 39 291734 906e10f207933f84
src/v3/std/clean_emission.dag 15 23867 e7bc1e6145f4781e
src/v3/std/computation.dag 18 36754 0d38639c1f631609
src/v3/std/computation_model.dag 12 17291 30cd3a0f7cc08eae
Expand Down
8 changes: 8 additions & 0 deletions src/v3/lenses/cost.dag
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ module lenses.cost
import std.list { List, empty, fold, cons }
import std.substrate { Dag, Behavior, BranchPath, LoopNode, NodeId, PortId, node }
import std.algebra {
MethodContract,
CostShape,

This comment was marked as resolved.

SymbolicCost,
SizeVariable,
sequential,
Expand All @@ -51,6 +53,12 @@ type SymbolicCostEntry {
fn symbolic_cost_of(d: Dag, port_id: PortId) -> Lookup<SymbolicCost> =
lookup_cost(compute_symbolic_costs(d), port_id)

// §6a Option 3 demo consumer: cost analysis reads per-method metadata from
// the unified carrier keyed by `(algebra_id, method_id)`, keeping the
// cost/size/callback facts co-located for later live call-site wiring.
fn method_contract_cost_shape(contract: MethodContract) -> CostShape? =
contract.cost_shape

fn compute_symbolic_costs(d: Dag) -> List<SymbolicCostEntry> =
fold(d.nodes, seed_bind_params(d.nodes), |acc, behavior|
cons(entry_for(d, acc, behavior), acc)
Expand Down
48 changes: 47 additions & 1 deletion src/v3/std/algebra.dag
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,55 @@
module v3.std.algebra

import std.list { List, empty, fold, cons, singleton, filter }
import std.substrate { NonSingletonList, PortId }
import std.substrate { NonSingletonList, PortId, DeclarationId }
import v3.std.lookup { Lookup }

// 🟡 SCAFFOLD coproduct. These are v2 per-method output-size effect facts
// staged in v3 as the MethodContract metadata vocabulary. Pattern 1 fails
// for the §6a pick: cost/complexity need the facts co-located per method,
// not scattered into lens-local tables. Pattern 2 partially fits the unit
// variants but would erase the closed semantic partition that consumers
// dispatch on. Pattern 3 is the intended dissolution: once live call-site
// MethodContract producers read structural return-type / collection-shape
// facts, this carrier either narrows to those structural facts or remains as
// their named projection. Trigger: the bulk MethodContract producer/consumer
// migration replaces template-row seeding with structural method contracts.
type CollectionSizeEffect
= ShrinkEffect
| ProjectionEffect
| IdentityEffect

// Operation cost shape — structural contract on a method's algorithmic cost.
// Declared per method on `MethodContract`; cost/complexity lenses read this
// to compute cost expressions without string-dispatching on method names.
//
// 🟡 SCAFFOLD coproduct. Pattern 1 fails because cost shape belongs beside the
// same method identity as size/callback metadata. Pattern 2 fails because the
// variants name distinct closed asymptotic classes, not one labeled record
// shape with free data. Pattern 3 is the dissolution path: live cost analysis
// should derive these classes from `CostBound` / call-pattern structure when
// MethodContract lookup is wired at call sites. Trigger: the post-§6a bulk
// migration makes MethodContract construction consume E-C/E-I/E-P facts
// directly instead of copying template metadata.
type CostShape
= ShapeConstant
| ShapeLinearScan
| ShapeIterateBody
| ShapeSortBody

// Unified per-method metadata carrier for v3 lens consumers. This is the
// §6a Option 3 shape: one lookup key `(algebra_id, method_id)` using existing
// `DeclarationId` identity, with the method contract facts cost/complexity
// need co-located. It deliberately does not extend substrate declarations or
// create one carrier per algebra.
type MethodContract {
algebra_id: DeclarationId
method_id: DeclarationId
size_effect: CollectionSizeEffect?
cost_shape: CostShape?
callback_element_position: Int?
}

// 🟢 TERMINAL. `SizeVariable` names the runtime size of a DAG
// port's value. Two cost expressions correlate structurally via
// `source_port` equality — different folds over the same port
Expand Down
Loading