From e9d4735b8b26f2c53e49ffd12bfa2a68003e43d5 Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Wed, 20 May 2026 19:19:39 -0400 Subject: [PATCH 01/14] =?UTF-8?q?WIP:=20T-12=20=E2=80=94=20lens/cost.dag?= =?UTF-8?q?=20+=20lens/complexity.dag=20(one=20mechanism,=20two=20projecti?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/v4/lens/complexity.dag | 52 +++++ src/v4/lens/cost.dag | 467 ++++++++++++++++++++++++++++++++++++- 2 files changed, 516 insertions(+), 3 deletions(-) diff --git a/src/v4/lens/complexity.dag b/src/v4/lens/complexity.dag index ce8402a5f57..2d4c7cedc55 100644 --- a/src/v4/lens/complexity.dag +++ b/src/v4/lens/complexity.dag @@ -30,3 +30,55 @@ module v4.lens.complexity + +import v4.lens.cost { + AsymptoticClass, + ClassConstant, + ClassUnknown, + SymbolicCost, + asymptotic_class_dominates, + asymptotic_class_of_cost, + symbolic_cost_of +} +import v4.std.diagnostic { + AmbiguousIntent, + Diagnostic, + NodeLocus, + Unavailable +} +import v4.std.node { Node, Symbol } +import v4.std.witness { Holds, Violates, Witness } + + +data complexity_reason_unmodeled_cost_projection: Symbol = complexity_reason_unmodeled_cost_projection + +type ComplexityBound { + class: AsymptoticClass +} + +fn complexity_bound_from_cost(cost: SymbolicCost) -> ComplexityBound { + ComplexityBound { + class: asymptotic_class_of_cost(cost: cost) + } +} + +fn complexity_projection_diagnostic(n: Node) -> Diagnostic { + Diagnostic { + reason: complexity_reason_unmodeled_cost_projection, + at: NodeLocus { node: n }, + correction: Unavailable { reason: AmbiguousIntent } + } +} + +fn complexity_of(n: Node) -> Witness { + match symbolic_cost_of(n: n) { + Holds { value: cost } => + Holds { value: complexity_bound_from_cost(cost: cost) } + Violates { diagnostic: d } => + Violates { diagnostic: d } + } +} + +fn complexity_bound_dominates(a: ComplexityBound, b: ComplexityBound) -> Bool { + asymptotic_class_dominates(a: a.class, b: b.class) +} diff --git a/src/v4/lens/cost.dag b/src/v4/lens/cost.dag index f9a5e44de92..0a02f315b3f 100644 --- a/src/v4/lens/cost.dag +++ b/src/v4/lens/cost.dag @@ -34,15 +34,476 @@ module v4.lens.cost import v4.extdeps.languages.llvm_ir { LlvmInstruction } +import v4.std.algebra { + BoundedLattice, + Lattice, + Magma, + Monoid, + Semigroup +} +import v4.std.collection { List } +import v4.std.diagnostic { + AmbiguousIntent, + Correction, + Diagnostic, + Locus, + NodeLocus, + Unavailable +} +import v4.std.node { + Arrow, + Atom, + Behavior, + Bind, + Branch, + Cardinality, + ComputationNode, + Conj, + Connective, + Disj, + Edge, + Instantiation, + Loop, + Node, + NodeFold, + Symbol, + Transform, + TypeNode, + Value, + fold_node +} +import v4.std.witness { Holds, Violates, Witness } + +data cost_reason_unmodeled_symbolic_cost: Symbol = cost_reason_unmodeled_symbolic_cost -// RULING-1: needs-more-work — minimal carrier until T-12 cost lattice/fold lands. -// 🟡 coproduct dissolution — T22-INFER-SYMBOLIC-COST-BRIDGE. +// U2/T-12: this is the single cost carrier. Complexity imports the +// `asymptotic_class_of_cost` projection below and does not re-walk `Node`. +// 🟢 coproduct dissolution — T-12-COST-CARRIER. +type SizeVariable { + source: Node +} + +// 🟢 coproduct dissolution — T-12-COST-CARRIER. type SymbolicCost -// 🟡 coproduct dissolution — T22-INFER-SYMBOLIC-COST-BRIDGE = ConstantCost { value: Int } + | LinearCost { variable: SizeVariable } + | LogCost { variable: SizeVariable } + | PolynomialCost { variable: SizeVariable, degree: Int } + | PolyLogCost { variable: SizeVariable, exponent: Int } + | ExponentialCost { variable: SizeVariable, base: Int } + | FactorialCost { variable: SizeVariable } + | SumCost { terms: List } + | ProductCost { factors: List } | UnknownCost { reason: String } +// 🟢 coproduct dissolution — T-12-COMPLEXITY-PROJECTION. +type AsymptoticClass + = ClassConstant + | ClassLog + | ClassLinear + | ClassLinearithmic + | ClassPolynomial { degree: Int } + | ClassPolyLog { exponent: Int } + | ClassExponential + | ClassFactorial + | ClassUnknown + +// Private fold state: the parent node chooses how child costs compose. +// This is lens-over-substrate: the only input is the existing `Node` +// tree plus the fold algebra, never a parallel authored cost table. +// 🟢 coproduct dissolution — T-12-FOLD-MODE. +type CostCompositionMode + = SequentialCost + | AlternativeCost + | IterativeCost + +type SymbolicCostFoldState { + mode: CostCompositionMode + cost: SymbolicCost +} + +fn constant_cost(value: Int) -> SymbolicCost { + ConstantCost { value: value } +} + +fn zero_cost() -> SymbolicCost { + constant_cost(value: 0) +} + +fn unit_cost() -> SymbolicCost { + constant_cost(value: 1) +} + +fn size_of_node(n: Node) -> SizeVariable { + SizeVariable { source: n } +} + +fn linear_in_node(n: Node) -> SymbolicCost { + LinearCost { variable: size_of_node(n: n) } +} + +fn unknown_cost(reason: String) -> SymbolicCost { + UnknownCost { reason: reason } +} + +fn symbolic_cost_diagnostic(n: Node) -> Diagnostic { + Diagnostic { + reason: cost_reason_unmodeled_symbolic_cost, + at: NodeLocus { node: n }, + correction: Unavailable { reason: AmbiguousIntent } + } +} + +fn symbolic_cost_witness(c: SymbolicCost, n: Node) -> Witness { + match c { + UnknownCost { reason: _ } => + Violates { diagnostic: symbolic_cost_diagnostic(n: n) } + _ => + Holds { value: c } + } +} + +fn base_cost_for_connective(c: Connective) -> SymbolicCost { + match c { + Atom { identity: _ } => zero_cost() + Conj => zero_cost() + Disj => zero_cost() + Arrow => unit_cost() + Cardinality => unit_cost() + Instantiation => unit_cost() + } +} + +fn base_cost_for_behavior(b: Behavior) -> SymbolicCost { + match b { + Value => zero_cost() + Transform => unit_cost() + Branch => unit_cost() + Loop => unit_cost() + Bind => zero_cost() + } +} + +fn cost_mode_for_connective(c: Connective) -> CostCompositionMode { + match c { + Atom { identity: _ } => SequentialCost + Conj => SequentialCost + Disj => AlternativeCost + Arrow => SequentialCost + Cardinality => IterativeCost + Instantiation => SequentialCost + } +} + +fn cost_mode_for_behavior(b: Behavior) -> CostCompositionMode { + match b { + Value => SequentialCost + Transform => SequentialCost + Branch => AlternativeCost + Loop => IterativeCost + Bind => SequentialCost + } +} + +fn cost_fold_init(n: Node) -> SymbolicCostFoldState { + match n.kind { + TypeNode { connective: c } => + SymbolicCostFoldState { + mode: cost_mode_for_connective(c: c), + cost: base_cost_for_connective(c: c) + } + ComputationNode { behavior: b } => + SymbolicCostFoldState { + mode: cost_mode_for_behavior(b: b), + cost: base_cost_for_behavior(b: b) + } + } +} + +fn symbolic_sequential(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { + match a { + ConstantCost { value: 0 } => b + UnknownCost { reason: _ } => a + _ => + match b { + ConstantCost { value: 0 } => a + UnknownCost { reason: _ } => b + _ => SumCost { terms: [a, b] } + } + } +} + +fn symbolic_product(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { + match a { + ConstantCost { value: 0 } => zero_cost() + ConstantCost { value: 1 } => b + UnknownCost { reason: _ } => a + _ => + match b { + ConstantCost { value: 0 } => zero_cost() + ConstantCost { value: 1 } => a + UnknownCost { reason: _ } => b + _ => ProductCost { factors: [a, b] } + } + } +} + +fn symbolic_max(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { + if symbolic_cost_dominates(a: a, b: b) { + a + } else { + b + } +} + +fn compose_child_cost( + mode: CostCompositionMode, + acc: SymbolicCost, + child: SymbolicCost +) -> SymbolicCost { + match mode { + SequentialCost => symbolic_sequential(a: acc, b: child) + AlternativeCost => symbolic_max(a: acc, b: child) + IterativeCost => symbolic_product(a: acc, b: child) + } +} + +fn cost_fold_step( + acc: SymbolicCostFoldState, + _: Edge, + child: SymbolicCostFoldState +) -> SymbolicCostFoldState { + SymbolicCostFoldState { + mode: acc.mode, + cost: compose_child_cost(mode: acc.mode, acc: acc.cost, child: child.cost) + } +} + +fn symbolic_cost_fold() -> NodeFold { + NodeFold { + init: cost_fold_init, + step: cost_fold_step + } +} + +fn symbolic_cost_of_node(n: Node) -> SymbolicCost { + fold_node(n: n, algebra: symbolic_cost_fold()).cost +} + +fn symbolic_cost_of(n: Node) -> Witness { + symbolic_cost_witness(c: symbolic_cost_of_node(n: n), n: n) +} + +fn asymptotic_class_join(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { + if asymptotic_class_dominates(a: a, b: b) { + a + } else { + b + } +} + +fn asymptotic_class_meet(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { + if asymptotic_class_dominates(a: a, b: b) { + b + } else { + a + } +} + +fn asymptotic_class_dominates(a: AsymptoticClass, b: AsymptoticClass) -> Bool { + match a { + ClassUnknown => true + ClassFactorial => + match b { + ClassUnknown => false + _ => true + } + ClassExponential => + match b { + ClassUnknown => false + ClassFactorial => false + _ => true + } + ClassPolynomial { degree: ad } => + match b { + ClassConstant => true + ClassLog => true + ClassLinear => true + ClassLinearithmic => true + ClassPolynomial { degree: bd } => ad >= bd + ClassPolyLog { exponent: _ } => true + _ => false + } + ClassLinearithmic => + match b { + ClassConstant => true + ClassLog => true + ClassLinear => true + ClassPolyLog { exponent: _ } => true + _ => false + } + ClassLinear => + match b { + ClassConstant => true + ClassLog => true + ClassPolyLog { exponent: _ } => true + ClassLinear => true + _ => false + } + ClassPolyLog { exponent: ae } => + match b { + ClassConstant => true + ClassLog => true + ClassPolyLog { exponent: be } => ae >= be + _ => false + } + ClassLog => + match b { + ClassConstant => true + ClassLog => true + _ => false + } + ClassConstant => + match b { + ClassConstant => true + _ => false + } + } +} + +fn symbolic_cost_dominates(a: SymbolicCost, b: SymbolicCost) -> Bool { + asymptotic_class_dominates( + a: asymptotic_class_of_cost(cost: a), + b: asymptotic_class_of_cost(cost: b) + ) +} + +fn fold_join_cost_classes(terms: List) -> AsymptoticClass { + fold(terms, init: ClassConstant, f: fn(acc, term) { + asymptotic_class_join(a: acc, b: asymptotic_class_of_cost(cost: term)) + }) +} + +fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { + match a { + ClassConstant => b + ClassUnknown => ClassUnknown + ClassFactorial => ClassFactorial + ClassExponential => match b { + ClassFactorial => ClassFactorial + ClassUnknown => ClassUnknown + _ => ClassExponential + } + ClassPolynomial { degree: ad } => + match b { + ClassConstant => a + ClassLog => ClassLinearithmic + ClassLinear => ClassPolynomial { degree: ad + 1 } + ClassLinearithmic => ClassPolynomial { degree: ad + 1 } + ClassPolynomial { degree: bd } => ClassPolynomial { degree: ad + bd } + ClassPolyLog { exponent: _ } => a + ClassExponential => ClassExponential + ClassFactorial => ClassFactorial + ClassUnknown => ClassUnknown + } + ClassLinear => + match b { + ClassConstant => ClassLinear + ClassLog => ClassLinearithmic + ClassLinear => ClassPolynomial { degree: 2 } + ClassLinearithmic => ClassPolynomial { degree: 2 } + ClassPolynomial { degree: bd } => ClassPolynomial { degree: bd + 1 } + ClassPolyLog { exponent: _ } => ClassLinear + ClassExponential => ClassExponential + ClassFactorial => ClassFactorial + ClassUnknown => ClassUnknown + } + ClassLinearithmic => + match b { + ClassConstant => ClassLinearithmic + ClassLog => ClassLinearithmic + ClassLinear => ClassPolynomial { degree: 2 } + ClassLinearithmic => ClassPolynomial { degree: 2 } + ClassPolynomial { degree: bd } => ClassPolynomial { degree: bd + 1 } + ClassPolyLog { exponent: _ } => ClassLinearithmic + ClassExponential => ClassExponential + ClassFactorial => ClassFactorial + ClassUnknown => ClassUnknown + } + ClassPolyLog { exponent: _ } => + match b { + ClassConstant => a + ClassLog => a + ClassPolyLog { exponent: _ } => a + _ => multiply_classes(a: b, b: a) + } + ClassLog => + match b { + ClassConstant => ClassLog + ClassLog => ClassPolyLog { exponent: 2 } + ClassPolyLog { exponent: e } => ClassPolyLog { exponent: e + 1 } + _ => multiply_classes(a: b, b: a) + } + } +} + +fn fold_product_cost_classes(factors: List) -> AsymptoticClass { + fold(factors, init: ClassConstant, f: fn(acc, factor) { + multiply_classes(a: acc, b: asymptotic_class_of_cost(cost: factor)) + }) +} + +fn asymptotic_class_of_cost(cost: SymbolicCost) -> AsymptoticClass { + match cost { + ConstantCost { value: _ } => ClassConstant + LinearCost { variable: _ } => ClassLinear + LogCost { variable: _ } => ClassLog + PolynomialCost { variable: _, degree: d } => ClassPolynomial { degree: d } + PolyLogCost { variable: _, exponent: e } => ClassPolyLog { exponent: e } + ExponentialCost { variable: _, base: _ } => ClassExponential + FactorialCost { variable: _ } => ClassFactorial + SumCost { terms: terms } => fold_join_cost_classes(terms: terms) + ProductCost { factors: factors } => fold_product_cost_classes(factors: factors) + UnknownCost { reason: _ } => ClassUnknown + } +} + +fn cost_lattice_meet(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { + if symbolic_cost_dominates(a: a, b: b) { + b + } else { + a + } +} + +fn cost_lattice_join(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { + symbolic_max(a: a, b: b) +} + +data symbolic_cost_sequential_monoid: Monoid = Monoid { + semigroup: Semigroup { magma: Magma { op: symbolic_sequential } }, + identity: ConstantCost { value: 0 } +} + +data symbolic_cost_lattice: BoundedLattice = BoundedLattice { + lattice: Lattice { + meet: cost_lattice_meet, + join: cost_lattice_join + }, + top: UnknownCost { reason: [] }, + bottom: ConstantCost { value: 0 } +} + +data asymptotic_class_lattice: BoundedLattice = BoundedLattice { + lattice: Lattice { + meet: asymptotic_class_meet, + join: asymptotic_class_join + }, + top: ClassUnknown, + bottom: ClassConstant +} + fn llvm_instruction_cost(i: LlvmInstruction) -> Int { match i { From 969a91dd3f338afcadf56fb8b54def5cead6ef7d Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Wed, 20 May 2026 19:22:36 -0400 Subject: [PATCH 02/14] =?UTF-8?q?WIP:=20T-12=20=E2=80=94=20lens/cost.dag?= =?UTF-8?q?=20+=20lens/complexity.dag=20(one=20mechanism,=20two=20projecti?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/v4/lens/complexity.dag | 134 +++++++++++++++++++++++++++++++++---- src/v4/lens/cost.dag | 56 +++++++++------- 2 files changed, 152 insertions(+), 38 deletions(-) diff --git a/src/v4/lens/complexity.dag b/src/v4/lens/complexity.dag index 2d4c7cedc55..52504a3665d 100644 --- a/src/v4/lens/complexity.dag +++ b/src/v4/lens/complexity.dag @@ -33,17 +33,25 @@ module v4.lens.complexity import v4.lens.cost { AsymptoticClass, - ClassConstant, - ClassUnknown, + ClassExponential, + ClassFactorial, + ClassLinear, + ClassLinearithmic, + ClassLog, + ClassPolynomial, + ClassPolyLog, SymbolicCost, + SizeVariable, asymptotic_class_dominates, asymptotic_class_of_cost, - symbolic_cost_of + cost_lens } import v4.std.diagnostic { AmbiguousIntent, Diagnostic, NodeLocus, + Textual, + WholeFile, Unavailable } import v4.std.node { Node, Symbol } @@ -51,27 +59,66 @@ import v4.std.witness { Holds, Violates, Witness } data complexity_reason_unmodeled_cost_projection: Symbol = complexity_reason_unmodeled_cost_projection +data complexity_projection_virtual_file: Symbol = complexity_projection_virtual_file -type ComplexityBound { - class: AsymptoticClass +// ComplexityBound is the asymptotic projection of `SymbolicCost`. The +// `class` relation remains owned by `cost.dag`; this carrier packages the +// projected user-facing bound without adding another cost mechanism. +// 🟢 coproduct dissolution — T-12-COMPLEXITY-PROJECTION. +type ComplexityBound + = Constant + | Logarithmic { size_var: SizeVariable } + | Linear { size_var: SizeVariable } + | Polynomial { size_var: SizeVariable, degree: Int } + | Exponential { size_var: SizeVariable } + | Factorial { size_var: SizeVariable } + | Unknown { diagnostic: Diagnostic } + +fn unknown_complexity() -> ComplexityBound { + Unknown { diagnostic: complexity_projection_diagnostic() } } fn complexity_bound_from_cost(cost: SymbolicCost) -> ComplexityBound { - ComplexityBound { - class: asymptotic_class_of_cost(cost: cost) + match cost { + ConstantCost { value: _ } => Constant + LogCost { variable: v } => Logarithmic { size_var: v } + LinearCost { variable: v } => Linear { size_var: v } + PolynomialCost { variable: v, degree: d } => Polynomial { size_var: v, degree: d } + PolyLogCost { variable: _, exponent: _ } => unknown_complexity() + ExponentialCost { variable: v, base: _ } => Exponential { size_var: v } + FactorialCost { variable: v } => Factorial { size_var: v } + SumCost { lhs: lhs, rhs: rhs } => + dominant_complexity_bound( + a: complexity_bound_from_cost(cost: lhs), + b: complexity_bound_from_cost(cost: rhs) + ) + ProductCost { lhs: lhs, rhs: rhs } => + multiply_complexity_bounds( + a: complexity_bound_from_cost(cost: lhs), + b: complexity_bound_from_cost(cost: rhs) + ) + UnknownCost { reason: _ } => unknown_complexity() + } +} + +fn dominant_complexity_bound(a: ComplexityBound, b: ComplexityBound) -> ComplexityBound { + if complexity_bound_dominates(a: a, b: b) { + a + } else { + b } } -fn complexity_projection_diagnostic(n: Node) -> Diagnostic { +fn complexity_projection_diagnostic() -> Diagnostic { Diagnostic { reason: complexity_reason_unmodeled_cost_projection, - at: NodeLocus { node: n }, + at: Textual { file: complexity_projection_virtual_file, extent: WholeFile }, correction: Unavailable { reason: AmbiguousIntent } } } -fn complexity_of(n: Node) -> Witness { - match symbolic_cost_of(n: n) { +fn asymptotic_projection(cost_witness: Witness) -> Witness { + match cost_witness { Holds { value: cost } => Holds { value: complexity_bound_from_cost(cost: cost) } Violates { diagnostic: d } => @@ -79,6 +126,69 @@ fn complexity_of(n: Node) -> Witness { } } +fn complexity_lens(n: Node) -> Witness { + asymptotic_projection(cost_witness: cost_lens(n: n)) +} + +fn complexity_bound_class(b: ComplexityBound) -> AsymptoticClass { + match b { + Constant => ClassConstant + Logarithmic { size_var: _ } => ClassLog + Linear { size_var: _ } => ClassLinear + Polynomial { size_var: _, degree: d } => ClassPolynomial { degree: d } + Exponential { size_var: _ } => ClassExponential + Factorial { size_var: _ } => ClassFactorial + Unknown { diagnostic: _ } => ClassUnknown + } +} + fn complexity_bound_dominates(a: ComplexityBound, b: ComplexityBound) -> Bool { - asymptotic_class_dominates(a: a.class, b: b.class) + asymptotic_class_dominates( + a: complexity_bound_class(b: a), + b: complexity_bound_class(b: b) + ) +} + +fn multiply_complexity_bounds(a: ComplexityBound, b: ComplexityBound) -> ComplexityBound { + match a { + Constant => b + Unknown { diagnostic: _ } => a + Factorial { size_var: _ } => a + Exponential { size_var: _ } => + match b { + Factorial { size_var: _ } => b + Unknown { diagnostic: _ } => b + _ => a + } + Polynomial { size_var: v, degree: ad } => + match b { + Constant => a + Logarithmic { size_var: _ } => a + Linear { size_var: _ } => Polynomial { size_var: v, degree: ad + 1 } + Polynomial { size_var: _, degree: bd } => Polynomial { size_var: v, degree: ad + bd } + Exponential { size_var: _ } => b + Factorial { size_var: _ } => b + Unknown { diagnostic: _ } => b + } + Linear { size_var: v } => + match b { + Constant => a + Logarithmic { size_var: _ } => Polynomial { size_var: v, degree: 2 } + Linear { size_var: _ } => Polynomial { size_var: v, degree: 2 } + Polynomial { size_var: _, degree: bd } => Polynomial { size_var: v, degree: bd + 1 } + Exponential { size_var: _ } => b + Factorial { size_var: _ } => b + Unknown { diagnostic: _ } => b + } + Logarithmic { size_var: v } => + match b { + Constant => a + Logarithmic { size_var: _ } => Polynomial { size_var: v, degree: 1 } + Linear { size_var: bv } => Polynomial { size_var: bv, degree: 2 } + Polynomial { size_var: _, degree: _ } => b + Exponential { size_var: _ } => b + Factorial { size_var: _ } => b + Unknown { diagnostic: _ } => b + } + } } diff --git a/src/v4/lens/cost.dag b/src/v4/lens/cost.dag index 0a02f315b3f..eeeb52f993c 100644 --- a/src/v4/lens/cost.dag +++ b/src/v4/lens/cost.dag @@ -41,12 +41,9 @@ import v4.std.algebra { Monoid, Semigroup } -import v4.std.collection { List } import v4.std.diagnostic { AmbiguousIntent, - Correction, Diagnostic, - Locus, NodeLocus, Unavailable } @@ -93,8 +90,8 @@ type SymbolicCost | PolyLogCost { variable: SizeVariable, exponent: Int } | ExponentialCost { variable: SizeVariable, base: Int } | FactorialCost { variable: SizeVariable } - | SumCost { terms: List } - | ProductCost { factors: List } + | SumCost { lhs: SymbolicCost, rhs: SymbolicCost } + | ProductCost { lhs: SymbolicCost, rhs: SymbolicCost } | UnknownCost { reason: String } // 🟢 coproduct dissolution — T-12-COMPLEXITY-PROJECTION. @@ -229,7 +226,7 @@ fn symbolic_sequential(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { match b { ConstantCost { value: 0 } => a UnknownCost { reason: _ } => b - _ => SumCost { terms: [a, b] } + _ => SumCost { lhs: a, rhs: b } } } } @@ -244,7 +241,7 @@ fn symbolic_product(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { ConstantCost { value: 0 } => zero_cost() ConstantCost { value: 1 } => a UnknownCost { reason: _ } => b - _ => ProductCost { factors: [a, b] } + _ => ProductCost { lhs: a, rhs: b } } } } @@ -288,10 +285,11 @@ fn symbolic_cost_fold() -> NodeFold { } fn symbolic_cost_of_node(n: Node) -> SymbolicCost { - fold_node(n: n, algebra: symbolic_cost_fold()).cost + let folded = fold_node(n: n, algebra: symbolic_cost_fold()) + folded.cost } -fn symbolic_cost_of(n: Node) -> Witness { +fn cost_lens(n: Node) -> Witness { symbolic_cost_witness(c: symbolic_cost_of_node(n: n), n: n) } @@ -379,12 +377,6 @@ fn symbolic_cost_dominates(a: SymbolicCost, b: SymbolicCost) -> Bool { ) } -fn fold_join_cost_classes(terms: List) -> AsymptoticClass { - fold(terms, init: ClassConstant, f: fn(acc, term) { - asymptotic_class_join(a: acc, b: asymptotic_class_of_cost(cost: term)) - }) -} - fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { match a { ClassConstant => b @@ -436,24 +428,28 @@ fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { ClassConstant => a ClassLog => a ClassPolyLog { exponent: _ } => a - _ => multiply_classes(a: b, b: a) + ClassLinear => ClassLinear + ClassLinearithmic => ClassLinearithmic + ClassPolynomial { degree: _ } => b + ClassExponential => ClassExponential + ClassFactorial => ClassFactorial + ClassUnknown => ClassUnknown } ClassLog => match b { ClassConstant => ClassLog ClassLog => ClassPolyLog { exponent: 2 } ClassPolyLog { exponent: e } => ClassPolyLog { exponent: e + 1 } - _ => multiply_classes(a: b, b: a) + ClassLinear => ClassLinearithmic + ClassLinearithmic => ClassLinearithmic + ClassPolynomial { degree: _ } => b + ClassExponential => ClassExponential + ClassFactorial => ClassFactorial + ClassUnknown => ClassUnknown } } } -fn fold_product_cost_classes(factors: List) -> AsymptoticClass { - fold(factors, init: ClassConstant, f: fn(acc, factor) { - multiply_classes(a: acc, b: asymptotic_class_of_cost(cost: factor)) - }) -} - fn asymptotic_class_of_cost(cost: SymbolicCost) -> AsymptoticClass { match cost { ConstantCost { value: _ } => ClassConstant @@ -463,8 +459,16 @@ fn asymptotic_class_of_cost(cost: SymbolicCost) -> AsymptoticClass { PolyLogCost { variable: _, exponent: e } => ClassPolyLog { exponent: e } ExponentialCost { variable: _, base: _ } => ClassExponential FactorialCost { variable: _ } => ClassFactorial - SumCost { terms: terms } => fold_join_cost_classes(terms: terms) - ProductCost { factors: factors } => fold_product_cost_classes(factors: factors) + SumCost { lhs: lhs, rhs: rhs } => + asymptotic_class_join( + a: asymptotic_class_of_cost(cost: lhs), + b: asymptotic_class_of_cost(cost: rhs) + ) + ProductCost { lhs: lhs, rhs: rhs } => + multiply_classes( + a: asymptotic_class_of_cost(cost: lhs), + b: asymptotic_class_of_cost(cost: rhs) + ) UnknownCost { reason: _ } => ClassUnknown } } @@ -491,7 +495,7 @@ data symbolic_cost_lattice: BoundedLattice = BoundedLattice { meet: cost_lattice_meet, join: cost_lattice_join }, - top: UnknownCost { reason: [] }, + top: UnknownCost { reason: "" }, bottom: ConstantCost { value: 0 } } From 96078dcd703ee13364cd41a8b6297c0ddb37f9de Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Wed, 20 May 2026 19:24:03 -0400 Subject: [PATCH 03/14] =?UTF-8?q?WIP:=20T-12=20=E2=80=94=20lens/cost.dag?= =?UTF-8?q?=20+=20lens/complexity.dag=20(one=20mechanism,=20two=20projecti?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/v4/lens/cost.dag | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/v4/lens/cost.dag b/src/v4/lens/cost.dag index eeeb52f993c..3f34519677f 100644 --- a/src/v4/lens/cost.dag +++ b/src/v4/lens/cost.dag @@ -161,13 +161,13 @@ fn symbolic_cost_witness(c: SymbolicCost, n: Node) -> Witness { } } -fn base_cost_for_connective(c: Connective) -> SymbolicCost { +fn base_cost_for_connective(c: Connective, n: Node) -> SymbolicCost { match c { Atom { identity: _ } => zero_cost() Conj => zero_cost() Disj => zero_cost() Arrow => unit_cost() - Cardinality => unit_cost() + Cardinality => linear_in_node(n: n) Instantiation => unit_cost() } } @@ -177,7 +177,7 @@ fn base_cost_for_behavior(b: Behavior) -> SymbolicCost { Value => zero_cost() Transform => unit_cost() Branch => unit_cost() - Loop => unit_cost() + Loop => unknown_cost(reason: "T-9-not-landed: loop bound") Bind => zero_cost() } } @@ -208,7 +208,7 @@ fn cost_fold_init(n: Node) -> SymbolicCostFoldState { TypeNode { connective: c } => SymbolicCostFoldState { mode: cost_mode_for_connective(c: c), - cost: base_cost_for_connective(c: c) + cost: base_cost_for_connective(c: c, n: n) } ComputationNode { behavior: b } => SymbolicCostFoldState { @@ -284,9 +284,12 @@ fn symbolic_cost_fold() -> NodeFold { } } +fn symbolic_cost_fold_state(n: Node) -> SymbolicCostFoldState { + fold_node(n: n, algebra: symbolic_cost_fold()) +} + fn symbolic_cost_of_node(n: Node) -> SymbolicCost { - let folded = fold_node(n: n, algebra: symbolic_cost_fold()) - folded.cost + symbolic_cost_fold_state(n: n).cost } fn cost_lens(n: Node) -> Witness { From 49ae28d50bc47110d35cd4de20a4b693e9ee2057 Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Wed, 20 May 2026 23:25:16 +0000 Subject: [PATCH 04/14] Add T-12 lens complexity worked claims --- src/v4/test/claim/lens_complexity/map_id.dag | 69 ++++++++++++++++ src/v4/test/claim/lens_complexity/nested.dag | 82 +++++++++++++++++++ .../while_external_condition.dag | 60 ++++++++++++++ 3 files changed, 211 insertions(+) create mode 100644 src/v4/test/claim/lens_complexity/map_id.dag create mode 100644 src/v4/test/claim/lens_complexity/nested.dag create mode 100644 src/v4/test/claim/lens_complexity/while_external_condition.dag diff --git a/src/v4/test/claim/lens_complexity/map_id.dag b/src/v4/test/claim/lens_complexity/map_id.dag new file mode 100644 index 00000000000..e41a9e15938 --- /dev/null +++ b/src/v4/test/claim/lens_complexity/map_id.dag @@ -0,0 +1,69 @@ +// src/v4/test/claim/lens_complexity/map_id.dag +// Scope: T-12 worked claim — structural cardinality projects to linear complexity. +// Owns: map_id_claim. +// Consumes: lens cost, lens complexity, std diagnostic, std node. +// Status: scaffold — compile-only until T-19/T-22 generated claim execution. + +module v4.test.claim.lens_complexity.map_id + +import v4.lens.complexity { ComplexityBound, Linear, complexity_lens } +import v4.lens.cost { LinearCost, SizeVariable, SymbolicCost, cost_lens } +import v4.std.diagnostic { + AmbiguousIntent, + Diagnostic, + Textual, + Unavailable, + WholeFile +} +import v4.std.node { + Cardinality, + ComputationNode, + Edge, + Node, + Positional, + Symbol, + Transform, + TypeNode +} +import v4.std.witness { Witness } + +data map_id_claim_file: Symbol = map_id_claim_file +data map_id_claim_reason: Symbol = map_id_claim_reason + +type LensComplexityClaim { + input: Node + expected_cost: SymbolicCost + expected_complexity: ComplexityBound + falsification: Diagnostic +} + +data map_id_body: Node = Node { + kind: ComputationNode { behavior: Transform }, + children: [] +} + +data map_id_input: Node = Node { + kind: TypeNode { connective: Cardinality }, + children: [ + Edge { label: Positional, target: map_id_body } + ] +} + +data map_id_claim: LensComplexityClaim = LensComplexityClaim { + input: map_id_input, + expected_cost: LinearCost { variable: SizeVariable { source: map_id_input } }, + expected_complexity: Linear { size_var: SizeVariable { source: map_id_input } }, + falsification: Diagnostic { + reason: map_id_claim_reason, + at: Textual { file: map_id_claim_file, extent: WholeFile }, + correction: Unavailable { reason: AmbiguousIntent } + } +} + +fn map_id_actual_cost() -> Witness { + cost_lens(n: map_id_input) +} + +fn map_id_actual_complexity() -> Witness { + complexity_lens(n: map_id_input) +} diff --git a/src/v4/test/claim/lens_complexity/nested.dag b/src/v4/test/claim/lens_complexity/nested.dag new file mode 100644 index 00000000000..9496fd3f050 --- /dev/null +++ b/src/v4/test/claim/lens_complexity/nested.dag @@ -0,0 +1,82 @@ +// src/v4/test/claim/lens_complexity/nested.dag +// Scope: T-12 worked claim — nested structural cardinalities project to polynomial complexity. +// Owns: nested_claim. +// Consumes: lens cost, lens complexity, std diagnostic, std node. +// Status: scaffold — compile-only until T-19/T-22 generated claim execution. + +module v4.test.claim.lens_complexity.nested + +import v4.lens.complexity { ComplexityBound, Polynomial, complexity_lens } +import v4.lens.cost { LinearCost, ProductCost, SizeVariable, SymbolicCost, cost_lens } +import v4.std.diagnostic { + AmbiguousIntent, + Diagnostic, + Textual, + Unavailable, + WholeFile +} +import v4.std.node { + Cardinality, + ComputationNode, + Edge, + Node, + Positional, + Symbol, + Transform, + TypeNode +} +import v4.std.witness { Witness } + +data nested_claim_file: Symbol = nested_claim_file +data nested_claim_reason: Symbol = nested_claim_reason + +type LensComplexityClaim { + input: Node + expected_cost: SymbolicCost + expected_complexity: ComplexityBound + falsification: Diagnostic +} + +data nested_body: Node = Node { + kind: ComputationNode { behavior: Transform }, + children: [] +} + +data nested_inner: Node = Node { + kind: TypeNode { connective: Cardinality }, + children: [ + Edge { label: Positional, target: nested_body } + ] +} + +data nested_input: Node = Node { + kind: TypeNode { connective: Cardinality }, + children: [ + Edge { label: Positional, target: nested_inner } + ] +} + +data nested_claim: LensComplexityClaim = LensComplexityClaim { + input: nested_input, + expected_cost: ProductCost { + lhs: LinearCost { variable: SizeVariable { source: nested_input } }, + rhs: LinearCost { variable: SizeVariable { source: nested_inner } } + }, + expected_complexity: Polynomial { + size_var: SizeVariable { source: nested_input }, + degree: 2 + }, + falsification: Diagnostic { + reason: nested_claim_reason, + at: Textual { file: nested_claim_file, extent: WholeFile }, + correction: Unavailable { reason: AmbiguousIntent } + } +} + +fn nested_actual_cost() -> Witness { + cost_lens(n: nested_input) +} + +fn nested_actual_complexity() -> Witness { + complexity_lens(n: nested_input) +} diff --git a/src/v4/test/claim/lens_complexity/while_external_condition.dag b/src/v4/test/claim/lens_complexity/while_external_condition.dag new file mode 100644 index 00000000000..249e105ee9e --- /dev/null +++ b/src/v4/test/claim/lens_complexity/while_external_condition.dag @@ -0,0 +1,60 @@ +// src/v4/test/claim/lens_complexity/while_external_condition.dag +// Scope: T-12 worked claim — non-structural loop bounds fail closed. +// Owns: while_external_condition_claim. +// Consumes: lens cost, lens complexity, std diagnostic, std node. +// Status: scaffold — compile-only until T-19/T-22 generated claim execution. + +module v4.test.claim.lens_complexity.while_external_condition + +import v4.lens.complexity { ComplexityBound, Unknown, complexity_lens } +import v4.lens.cost { SymbolicCost, UnknownCost, cost_lens } +import v4.std.diagnostic { + AmbiguousIntent, + Diagnostic, + Textual, + Unavailable, + WholeFile +} +import v4.std.node { + ComputationNode, + Loop, + Node, + Symbol +} +import v4.std.witness { Witness } + +data while_external_condition_file: Symbol = while_external_condition_file +data while_external_condition_reason: Symbol = while_external_condition_reason + +type LensComplexityClaim { + input: Node + expected_cost: SymbolicCost + expected_complexity: ComplexityBound + falsification: Diagnostic +} + +data while_external_condition_input: Node = Node { + kind: ComputationNode { behavior: Loop }, + children: [] +} + +data while_external_condition_diagnostic: Diagnostic = Diagnostic { + reason: while_external_condition_reason, + at: Textual { file: while_external_condition_file, extent: WholeFile }, + correction: Unavailable { reason: AmbiguousIntent } +} + +data while_external_condition_claim: LensComplexityClaim = LensComplexityClaim { + input: while_external_condition_input, + expected_cost: UnknownCost { reason: "T-9-not-landed: loop bound" }, + expected_complexity: Unknown { diagnostic: while_external_condition_diagnostic }, + falsification: while_external_condition_diagnostic +} + +fn while_external_condition_actual_cost() -> Witness { + cost_lens(n: while_external_condition_input) +} + +fn while_external_condition_actual_complexity() -> Witness { + complexity_lens(n: while_external_condition_input) +} From 3b1ec32e3229962c098dc38ca229743ff0356bdf Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Wed, 20 May 2026 19:30:06 -0400 Subject: [PATCH 05/14] =?UTF-8?q?WIP:=20T-12=20=E2=80=94=20lens/cost.dag?= =?UTF-8?q?=20+=20lens/complexity.dag=20(one=20mechanism,=20two=20projecti?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/v4/lens/cost.dag | 193 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 166 insertions(+), 27 deletions(-) diff --git a/src/v4/lens/cost.dag b/src/v4/lens/cost.dag index 3f34519677f..8cd5db12c5e 100644 --- a/src/v4/lens/cost.dag +++ b/src/v4/lens/cost.dag @@ -41,6 +41,7 @@ import v4.std.algebra { Monoid, Semigroup } +import v4.std.cardinality { NonZeroNat } import v4.std.diagnostic { AmbiguousIntent, Diagnostic, @@ -69,10 +70,12 @@ import v4.std.node { Value, fold_node } +import v4.std.nat { Nat, Succ, Zero } import v4.std.witness { Holds, Violates, Witness } data cost_reason_unmodeled_symbolic_cost: Symbol = cost_reason_unmodeled_symbolic_cost +data cost_lattice_top_reason: String = "lattice-top-unknown-cost" // U2/T-12: this is the single cost carrier. Complexity imports the // `asymptotic_class_of_cost` projection below and does not re-walk `Node`. @@ -81,14 +84,26 @@ type SizeVariable { source: Node } +type PolynomialDegree { + predecessor_after_linear: NonZeroNat +} + +type PolyLogExponent { + predecessor_after_log: NonZeroNat +} + +type ExponentialBase { + predecessor_after_one: NonZeroNat +} + // 🟢 coproduct dissolution — T-12-COST-CARRIER. type SymbolicCost = ConstantCost { value: Int } | LinearCost { variable: SizeVariable } | LogCost { variable: SizeVariable } - | PolynomialCost { variable: SizeVariable, degree: Int } - | PolyLogCost { variable: SizeVariable, exponent: Int } - | ExponentialCost { variable: SizeVariable, base: Int } + | PolynomialCost { variable: SizeVariable, degree: PolynomialDegree } + | PolyLogCost { variable: SizeVariable, exponent: PolyLogExponent } + | ExponentialCost { variable: SizeVariable, base: ExponentialBase } | FactorialCost { variable: SizeVariable } | SumCost { lhs: SymbolicCost, rhs: SymbolicCost } | ProductCost { lhs: SymbolicCost, rhs: SymbolicCost } @@ -100,8 +115,8 @@ type AsymptoticClass | ClassLog | ClassLinear | ClassLinearithmic - | ClassPolynomial { degree: Int } - | ClassPolyLog { exponent: Int } + | ClassPolynomial { degree: PolynomialDegree } + | ClassPolyLog { exponent: PolyLogExponent } | ClassExponential | ClassFactorial | ClassUnknown @@ -132,6 +147,18 @@ fn unit_cost() -> SymbolicCost { constant_cost(value: 1) } +fn nonzero_one() -> NonZeroNat { + NonZeroNat { prev: Zero } +} + +fn polynomial_degree_two() -> PolynomialDegree { + PolynomialDegree { predecessor_after_linear: nonzero_one() } +} + +fn polylog_exponent_two() -> PolyLogExponent { + PolyLogExponent { predecessor_after_log: nonzero_one() } +} + fn size_of_node(n: Node) -> SizeVariable { SizeVariable { source: n } } @@ -247,7 +274,98 @@ fn symbolic_product(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { } fn symbolic_max(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { - if symbolic_cost_dominates(a: a, b: b) { + match a { + ConstantCost { value: 0 } => b + _ => + match b { + ConstantCost { value: 0 } => a + _ => + if symbolic_cost_dominates(a: a, b: b) { + a + } else { + b + } + } + } +} + +fn polynomial_degree_dominates(a: PolynomialDegree, b: PolynomialDegree) -> Bool { + nonzero_nat_dominates(a: a.predecessor_after_linear, b: b.predecessor_after_linear) +} + +fn polylog_exponent_dominates(a: PolyLogExponent, b: PolyLogExponent) -> Bool { + nonzero_nat_dominates(a: a.predecessor_after_log, b: b.predecessor_after_log) +} + +fn nonzero_nat_dominates(a: NonZeroNat, b: NonZeroNat) -> Bool { + nat_dominates(a: a.prev, b: b.prev) +} + +fn nat_dominates(a: Nat, b: Nat) -> Bool { + match a { + Zero => + match b { + Zero => true + Succ { prev: _ } => false + } + Succ { prev: ap } => + match b { + Zero => true + Succ { prev: bp } => nat_dominates(a: ap, b: bp) + } + } +} + +fn add_nonzero_nat(a: NonZeroNat, b: NonZeroNat) -> NonZeroNat { + NonZeroNat { prev: Succ { prev: add_nat(a: a.prev, b: b.prev) } } +} + +fn add_nat(a: Nat, b: Nat) -> Nat { + match a { + Zero => b + Succ { prev: ap } => Succ { prev: add_nat(a: ap, b: b) } + } +} + +fn add_polynomial_degree(a: PolynomialDegree, b: PolynomialDegree) -> PolynomialDegree { + PolynomialDegree { + predecessor_after_linear: add_nonzero_nat( + a: a.predecessor_after_linear, + b: b.predecessor_after_linear + ) + } +} + +fn increment_polynomial_degree(d: PolynomialDegree) -> PolynomialDegree { + PolynomialDegree { + predecessor_after_linear: NonZeroNat { prev: Succ { prev: d.predecessor_after_linear.prev } } + } +} + +fn increment_polylog_exponent(e: PolyLogExponent) -> PolyLogExponent { + PolyLogExponent { + predecessor_after_log: NonZeroNat { prev: Succ { prev: e.predecessor_after_log.prev } } + } +} + +fn polynomial_degree_to_polylog_exponent(d: PolynomialDegree) -> PolyLogExponent { + PolyLogExponent { predecessor_after_log: d.predecessor_after_linear } +} + +fn polylog_exponent_to_polynomial_degree(e: PolyLogExponent) -> PolynomialDegree { + PolynomialDegree { predecessor_after_linear: e.predecessor_after_log } +} + +fn max_polynomial_degree(a: PolynomialDegree, b: PolynomialDegree) -> PolynomialDegree { + if polynomial_degree_dominates(a: a, b: b) { + a + } else { + b + } +} + +fn max_polylog_exponent(a: PolyLogExponent, b: PolyLogExponent) -> PolyLogExponent { + if polylog_exponent_dominates(a: a, b: b) { a } else { b @@ -332,7 +450,7 @@ fn asymptotic_class_dominates(a: AsymptoticClass, b: AsymptoticClass) -> Bool { ClassLog => true ClassLinear => true ClassLinearithmic => true - ClassPolynomial { degree: bd } => ad >= bd + ClassPolynomial { degree: bd } => polynomial_degree_dominates(a: ad, b: bd) ClassPolyLog { exponent: _ } => true _ => false } @@ -356,7 +474,7 @@ fn asymptotic_class_dominates(a: AsymptoticClass, b: AsymptoticClass) -> Bool { match b { ClassConstant => true ClassLog => true - ClassPolyLog { exponent: be } => ae >= be + ClassPolyLog { exponent: be } => polylog_exponent_dominates(a: ae, b: be) _ => false } ClassLog => @@ -374,10 +492,22 @@ fn asymptotic_class_dominates(a: AsymptoticClass, b: AsymptoticClass) -> Bool { } fn symbolic_cost_dominates(a: SymbolicCost, b: SymbolicCost) -> Bool { - asymptotic_class_dominates( - a: asymptotic_class_of_cost(cost: a), - b: asymptotic_class_of_cost(cost: b) - ) + match a { + ConstantCost { value: av } => + match b { + ConstantCost { value: bv } => av >= bv + _ => false + } + _ => + match b { + ConstantCost { value: 0 } => true + _ => + asymptotic_class_dominates( + a: asymptotic_class_of_cost(cost: a), + b: asymptotic_class_of_cost(cost: b) + ) + } + } } fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { @@ -393,11 +523,13 @@ fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { ClassPolynomial { degree: ad } => match b { ClassConstant => a - ClassLog => ClassLinearithmic - ClassLinear => ClassPolynomial { degree: ad + 1 } - ClassLinearithmic => ClassPolynomial { degree: ad + 1 } - ClassPolynomial { degree: bd } => ClassPolynomial { degree: ad + bd } - ClassPolyLog { exponent: _ } => a + ClassLog => ClassPolynomial { degree: increment_polynomial_degree(d: ad) } + ClassLinear => ClassPolynomial { degree: increment_polynomial_degree(d: ad) } + ClassLinearithmic => ClassPolynomial { degree: increment_polynomial_degree(d: ad) } + ClassPolynomial { degree: bd } => + ClassPolynomial { degree: add_polynomial_degree(a: ad, b: bd) } + ClassPolyLog { exponent: _ } => + ClassPolynomial { degree: increment_polynomial_degree(d: ad) } ClassExponential => ClassExponential ClassFactorial => ClassFactorial ClassUnknown => ClassUnknown @@ -406,9 +538,10 @@ fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { match b { ClassConstant => ClassLinear ClassLog => ClassLinearithmic - ClassLinear => ClassPolynomial { degree: 2 } - ClassLinearithmic => ClassPolynomial { degree: 2 } - ClassPolynomial { degree: bd } => ClassPolynomial { degree: bd + 1 } + ClassLinear => ClassPolynomial { degree: polynomial_degree_two() } + ClassLinearithmic => ClassPolynomial { degree: polynomial_degree_two() } + ClassPolynomial { degree: bd } => + ClassPolynomial { degree: increment_polynomial_degree(d: bd) } ClassPolyLog { exponent: _ } => ClassLinear ClassExponential => ClassExponential ClassFactorial => ClassFactorial @@ -418,9 +551,10 @@ fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { match b { ClassConstant => ClassLinearithmic ClassLog => ClassLinearithmic - ClassLinear => ClassPolynomial { degree: 2 } - ClassLinearithmic => ClassPolynomial { degree: 2 } - ClassPolynomial { degree: bd } => ClassPolynomial { degree: bd + 1 } + ClassLinear => ClassPolynomial { degree: polynomial_degree_two() } + ClassLinearithmic => ClassPolynomial { degree: polynomial_degree_two() } + ClassPolynomial { degree: bd } => + ClassPolynomial { degree: increment_polynomial_degree(d: bd) } ClassPolyLog { exponent: _ } => ClassLinearithmic ClassExponential => ClassExponential ClassFactorial => ClassFactorial @@ -433,7 +567,11 @@ fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { ClassPolyLog { exponent: _ } => a ClassLinear => ClassLinear ClassLinearithmic => ClassLinearithmic - ClassPolynomial { degree: _ } => b + ClassPolynomial { degree: pd } => + ClassPolynomial { degree: max_polynomial_degree( + a: pd, + b: polylog_exponent_to_polynomial_degree(e: e) + ) } ClassExponential => ClassExponential ClassFactorial => ClassFactorial ClassUnknown => ClassUnknown @@ -441,8 +579,9 @@ fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { ClassLog => match b { ClassConstant => ClassLog - ClassLog => ClassPolyLog { exponent: 2 } - ClassPolyLog { exponent: e } => ClassPolyLog { exponent: e + 1 } + ClassLog => ClassPolyLog { exponent: polylog_exponent_two() } + ClassPolyLog { exponent: e } => + ClassPolyLog { exponent: increment_polylog_exponent(e: e) } ClassLinear => ClassLinearithmic ClassLinearithmic => ClassLinearithmic ClassPolynomial { degree: _ } => b @@ -498,7 +637,7 @@ data symbolic_cost_lattice: BoundedLattice = BoundedLattice { meet: cost_lattice_meet, join: cost_lattice_join }, - top: UnknownCost { reason: "" }, + top: UnknownCost { reason: cost_lattice_top_reason }, bottom: ConstantCost { value: 0 } } From e3ea5854564d6c05fbb27cef84a0bc9d46db63f3 Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Wed, 20 May 2026 19:31:36 -0400 Subject: [PATCH 06/14] =?UTF-8?q?WIP:=20T-12=20=E2=80=94=20lens/cost.dag?= =?UTF-8?q?=20+=20lens/complexity.dag=20(one=20mechanism,=20two=20projecti?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/v4/lens/complexity.dag | 40 +++++++++++++++----- src/v4/lens/cost.dag | 25 ++++++++---- src/v4/test/claim/lens_complexity/nested.dag | 13 ++++++- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/src/v4/lens/complexity.dag b/src/v4/lens/complexity.dag index 52504a3665d..2f61c24f8fb 100644 --- a/src/v4/lens/complexity.dag +++ b/src/v4/lens/complexity.dag @@ -35,16 +35,32 @@ import v4.lens.cost { AsymptoticClass, ClassExponential, ClassFactorial, + ClassConstant, + ClassUnknown, ClassLinear, ClassLinearithmic, ClassLog, ClassPolynomial, ClassPolyLog, + ConstantCost, + ExponentialCost, + FactorialCost, + LinearCost, + LogCost, + PolynomialCost, + PolyLogCost, + ProductCost, + SumCost, + UnknownCost, + PolynomialDegree, SymbolicCost, SizeVariable, + add_polynomial_degree, asymptotic_class_dominates, asymptotic_class_of_cost, - cost_lens + cost_lens, + increment_polynomial_degree, + polynomial_degree_two } import v4.std.diagnostic { AmbiguousIntent, @@ -69,7 +85,7 @@ type ComplexityBound = Constant | Logarithmic { size_var: SizeVariable } | Linear { size_var: SizeVariable } - | Polynomial { size_var: SizeVariable, degree: Int } + | Polynomial { size_var: SizeVariable, degree: PolynomialDegree } | Exponential { size_var: SizeVariable } | Factorial { size_var: SizeVariable } | Unknown { diagnostic: Diagnostic } @@ -163,9 +179,12 @@ fn multiply_complexity_bounds(a: ComplexityBound, b: ComplexityBound) -> Complex Polynomial { size_var: v, degree: ad } => match b { Constant => a - Logarithmic { size_var: _ } => a - Linear { size_var: _ } => Polynomial { size_var: v, degree: ad + 1 } - Polynomial { size_var: _, degree: bd } => Polynomial { size_var: v, degree: ad + bd } + Logarithmic { size_var: _ } => + Polynomial { size_var: v, degree: increment_polynomial_degree(d: ad) } + Linear { size_var: _ } => + Polynomial { size_var: v, degree: increment_polynomial_degree(d: ad) } + Polynomial { size_var: _, degree: bd } => + Polynomial { size_var: v, degree: add_polynomial_degree(a: ad, b: bd) } Exponential { size_var: _ } => b Factorial { size_var: _ } => b Unknown { diagnostic: _ } => b @@ -173,9 +192,10 @@ fn multiply_complexity_bounds(a: ComplexityBound, b: ComplexityBound) -> Complex Linear { size_var: v } => match b { Constant => a - Logarithmic { size_var: _ } => Polynomial { size_var: v, degree: 2 } - Linear { size_var: _ } => Polynomial { size_var: v, degree: 2 } - Polynomial { size_var: _, degree: bd } => Polynomial { size_var: v, degree: bd + 1 } + Logarithmic { size_var: _ } => Polynomial { size_var: v, degree: polynomial_degree_two() } + Linear { size_var: _ } => Polynomial { size_var: v, degree: polynomial_degree_two() } + Polynomial { size_var: _, degree: bd } => + Polynomial { size_var: v, degree: increment_polynomial_degree(d: bd) } Exponential { size_var: _ } => b Factorial { size_var: _ } => b Unknown { diagnostic: _ } => b @@ -183,8 +203,8 @@ fn multiply_complexity_bounds(a: ComplexityBound, b: ComplexityBound) -> Complex Logarithmic { size_var: v } => match b { Constant => a - Logarithmic { size_var: _ } => Polynomial { size_var: v, degree: 1 } - Linear { size_var: bv } => Polynomial { size_var: bv, degree: 2 } + Logarithmic { size_var: _ } => unknown_complexity() + Linear { size_var: bv } => Polynomial { size_var: bv, degree: polynomial_degree_two() } Polynomial { size_var: _, degree: _ } => b Exponential { size_var: _ } => b Factorial { size_var: _ } => b diff --git a/src/v4/lens/cost.dag b/src/v4/lens/cost.dag index 8cd5db12c5e..c921d074339 100644 --- a/src/v4/lens/cost.dag +++ b/src/v4/lens/cost.dag @@ -280,10 +280,18 @@ fn symbolic_max(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { match b { ConstantCost { value: 0 } => a _ => - if symbolic_cost_dominates(a: a, b: b) { + if a == b { a } else { - b + if symbolic_cost_dominates(a: a, b: b) { + if symbolic_cost_dominates(a: b, b: a) { + SumCost { lhs: a, rhs: b } + } else { + a + } + } else { + b + } } } } @@ -542,7 +550,7 @@ fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { ClassLinearithmic => ClassPolynomial { degree: polynomial_degree_two() } ClassPolynomial { degree: bd } => ClassPolynomial { degree: increment_polynomial_degree(d: bd) } - ClassPolyLog { exponent: _ } => ClassLinear + ClassPolyLog { exponent: _ } => ClassPolynomial { degree: polynomial_degree_two() } ClassExponential => ClassExponential ClassFactorial => ClassFactorial ClassUnknown => ClassUnknown @@ -550,23 +558,23 @@ fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { ClassLinearithmic => match b { ClassConstant => ClassLinearithmic - ClassLog => ClassLinearithmic + ClassLog => ClassPolynomial { degree: polynomial_degree_two() } ClassLinear => ClassPolynomial { degree: polynomial_degree_two() } ClassLinearithmic => ClassPolynomial { degree: polynomial_degree_two() } ClassPolynomial { degree: bd } => ClassPolynomial { degree: increment_polynomial_degree(d: bd) } - ClassPolyLog { exponent: _ } => ClassLinearithmic + ClassPolyLog { exponent: _ } => ClassPolynomial { degree: polynomial_degree_two() } ClassExponential => ClassExponential ClassFactorial => ClassFactorial ClassUnknown => ClassUnknown } - ClassPolyLog { exponent: _ } => + ClassPolyLog { exponent: e } => match b { ClassConstant => a ClassLog => a ClassPolyLog { exponent: _ } => a ClassLinear => ClassLinear - ClassLinearithmic => ClassLinearithmic + ClassLinearithmic => ClassPolynomial { degree: polynomial_degree_two() } ClassPolynomial { degree: pd } => ClassPolynomial { degree: max_polynomial_degree( a: pd, @@ -584,7 +592,8 @@ fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { ClassPolyLog { exponent: increment_polylog_exponent(e: e) } ClassLinear => ClassLinearithmic ClassLinearithmic => ClassLinearithmic - ClassPolynomial { degree: _ } => b + ClassPolynomial { degree: bd } => + ClassPolynomial { degree: increment_polynomial_degree(d: bd) } ClassExponential => ClassExponential ClassFactorial => ClassFactorial ClassUnknown => ClassUnknown diff --git a/src/v4/test/claim/lens_complexity/nested.dag b/src/v4/test/claim/lens_complexity/nested.dag index 9496fd3f050..0908487f370 100644 --- a/src/v4/test/claim/lens_complexity/nested.dag +++ b/src/v4/test/claim/lens_complexity/nested.dag @@ -7,7 +7,15 @@ module v4.test.claim.lens_complexity.nested import v4.lens.complexity { ComplexityBound, Polynomial, complexity_lens } -import v4.lens.cost { LinearCost, ProductCost, SizeVariable, SymbolicCost, cost_lens } +import v4.lens.cost { + LinearCost, + PolynomialDegree, + ProductCost, + SizeVariable, + SymbolicCost, + cost_lens +} +import v4.std.cardinality { NonZeroNat } import v4.std.diagnostic { AmbiguousIntent, Diagnostic, @@ -25,6 +33,7 @@ import v4.std.node { Transform, TypeNode } +import v4.std.nat { Zero } import v4.std.witness { Witness } data nested_claim_file: Symbol = nested_claim_file @@ -64,7 +73,7 @@ data nested_claim: LensComplexityClaim = LensComplexityClaim { }, expected_complexity: Polynomial { size_var: SizeVariable { source: nested_input }, - degree: 2 + degree: PolynomialDegree { predecessor_after_linear: NonZeroNat { prev: Zero } } }, falsification: Diagnostic { reason: nested_claim_reason, From 1e07ef3e3370bdbb89a13c16d89f8d73d4553561 Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Wed, 20 May 2026 23:44:29 +0000 Subject: [PATCH 07/14] Project complexity through cost classes --- src/v4/lens/complexity.dag | 148 ++++++++++++++++++------------------- 1 file changed, 74 insertions(+), 74 deletions(-) diff --git a/src/v4/lens/complexity.dag b/src/v4/lens/complexity.dag index 2f61c24f8fb..6c079a5b055 100644 --- a/src/v4/lens/complexity.dag +++ b/src/v4/lens/complexity.dag @@ -52,15 +52,13 @@ import v4.lens.cost { ProductCost, SumCost, UnknownCost, + PolyLogExponent, PolynomialDegree, SymbolicCost, SizeVariable, - add_polynomial_degree, asymptotic_class_dominates, asymptotic_class_of_cost, - cost_lens, - increment_polynomial_degree, - polynomial_degree_two + cost_lens } import v4.std.diagnostic { AmbiguousIntent, @@ -85,6 +83,8 @@ type ComplexityBound = Constant | Logarithmic { size_var: SizeVariable } | Linear { size_var: SizeVariable } + | Linearithmic { size_var: SizeVariable } + | PolyLogarithmic { size_var: SizeVariable, exponent: PolyLogExponent } | Polynomial { size_var: SizeVariable, degree: PolynomialDegree } | Exponential { size_var: SizeVariable } | Factorial { size_var: SizeVariable } @@ -94,37 +94,83 @@ fn unknown_complexity() -> ComplexityBound { Unknown { diagnostic: complexity_projection_diagnostic() } } -fn complexity_bound_from_cost(cost: SymbolicCost) -> ComplexityBound { +fn representative_size_variable(cost: SymbolicCost) -> Witness { match cost { - ConstantCost { value: _ } => Constant - LogCost { variable: v } => Logarithmic { size_var: v } - LinearCost { variable: v } => Linear { size_var: v } - PolynomialCost { variable: v, degree: d } => Polynomial { size_var: v, degree: d } - PolyLogCost { variable: _, exponent: _ } => unknown_complexity() - ExponentialCost { variable: v, base: _ } => Exponential { size_var: v } - FactorialCost { variable: v } => Factorial { size_var: v } + ConstantCost { value: _ } => + Violates { diagnostic: complexity_projection_diagnostic() } + LogCost { variable: v } => Holds { value: v } + LinearCost { variable: v } => Holds { value: v } + PolynomialCost { variable: v, degree: _ } => Holds { value: v } + PolyLogCost { variable: v, exponent: _ } => Holds { value: v } + ExponentialCost { variable: v, base: _ } => Holds { value: v } + FactorialCost { variable: v } => Holds { value: v } SumCost { lhs: lhs, rhs: rhs } => - dominant_complexity_bound( - a: complexity_bound_from_cost(cost: lhs), - b: complexity_bound_from_cost(cost: rhs) - ) + match representative_size_variable(cost: lhs) { + Holds { value: v } => Holds { value: v } + Violates { diagnostic: _ } => representative_size_variable(cost: rhs) + } ProductCost { lhs: lhs, rhs: rhs } => - multiply_complexity_bounds( - a: complexity_bound_from_cost(cost: lhs), - b: complexity_bound_from_cost(cost: rhs) - ) - UnknownCost { reason: _ } => unknown_complexity() + match representative_size_variable(cost: lhs) { + Holds { value: v } => Holds { value: v } + Violates { diagnostic: _ } => representative_size_variable(cost: rhs) + } + UnknownCost { reason: _ } => + Violates { diagnostic: complexity_projection_diagnostic() } } } -fn dominant_complexity_bound(a: ComplexityBound, b: ComplexityBound) -> ComplexityBound { - if complexity_bound_dominates(a: a, b: b) { - a - } else { - b +fn complexity_bound_from_class( + class: AsymptoticClass, + size_var: Witness +) -> ComplexityBound { + match class { + ClassConstant => Constant + ClassLog => + match size_var { + Holds { value: v } => Logarithmic { size_var: v } + Violates { diagnostic: _ } => unknown_complexity() + } + ClassLinear => + match size_var { + Holds { value: v } => Linear { size_var: v } + Violates { diagnostic: _ } => unknown_complexity() + } + ClassLinearithmic => + match size_var { + Holds { value: v } => Linearithmic { size_var: v } + Violates { diagnostic: _ } => unknown_complexity() + } + ClassPolyLog { exponent: e } => + match size_var { + Holds { value: v } => PolyLogarithmic { size_var: v, exponent: e } + Violates { diagnostic: _ } => unknown_complexity() + } + ClassPolynomial { degree: d } => + match size_var { + Holds { value: v } => Polynomial { size_var: v, degree: d } + Violates { diagnostic: _ } => unknown_complexity() + } + ClassExponential => + match size_var { + Holds { value: v } => Exponential { size_var: v } + Violates { diagnostic: _ } => unknown_complexity() + } + ClassFactorial => + match size_var { + Holds { value: v } => Factorial { size_var: v } + Violates { diagnostic: _ } => unknown_complexity() + } + ClassUnknown => unknown_complexity() } } +fn complexity_bound_from_cost(cost: SymbolicCost) -> ComplexityBound { + complexity_bound_from_class( + class: asymptotic_class_of_cost(cost: cost), + size_var: representative_size_variable(cost: cost) + ) +} + fn complexity_projection_diagnostic() -> Diagnostic { Diagnostic { reason: complexity_reason_unmodeled_cost_projection, @@ -151,6 +197,8 @@ fn complexity_bound_class(b: ComplexityBound) -> AsymptoticClass { Constant => ClassConstant Logarithmic { size_var: _ } => ClassLog Linear { size_var: _ } => ClassLinear + Linearithmic { size_var: _ } => ClassLinearithmic + PolyLogarithmic { size_var: _, exponent: e } => ClassPolyLog { exponent: e } Polynomial { size_var: _, degree: d } => ClassPolynomial { degree: d } Exponential { size_var: _ } => ClassExponential Factorial { size_var: _ } => ClassFactorial @@ -164,51 +212,3 @@ fn complexity_bound_dominates(a: ComplexityBound, b: ComplexityBound) -> Bool { b: complexity_bound_class(b: b) ) } - -fn multiply_complexity_bounds(a: ComplexityBound, b: ComplexityBound) -> ComplexityBound { - match a { - Constant => b - Unknown { diagnostic: _ } => a - Factorial { size_var: _ } => a - Exponential { size_var: _ } => - match b { - Factorial { size_var: _ } => b - Unknown { diagnostic: _ } => b - _ => a - } - Polynomial { size_var: v, degree: ad } => - match b { - Constant => a - Logarithmic { size_var: _ } => - Polynomial { size_var: v, degree: increment_polynomial_degree(d: ad) } - Linear { size_var: _ } => - Polynomial { size_var: v, degree: increment_polynomial_degree(d: ad) } - Polynomial { size_var: _, degree: bd } => - Polynomial { size_var: v, degree: add_polynomial_degree(a: ad, b: bd) } - Exponential { size_var: _ } => b - Factorial { size_var: _ } => b - Unknown { diagnostic: _ } => b - } - Linear { size_var: v } => - match b { - Constant => a - Logarithmic { size_var: _ } => Polynomial { size_var: v, degree: polynomial_degree_two() } - Linear { size_var: _ } => Polynomial { size_var: v, degree: polynomial_degree_two() } - Polynomial { size_var: _, degree: bd } => - Polynomial { size_var: v, degree: increment_polynomial_degree(d: bd) } - Exponential { size_var: _ } => b - Factorial { size_var: _ } => b - Unknown { diagnostic: _ } => b - } - Logarithmic { size_var: v } => - match b { - Constant => a - Logarithmic { size_var: _ } => unknown_complexity() - Linear { size_var: bv } => Polynomial { size_var: bv, degree: polynomial_degree_two() } - Polynomial { size_var: _, degree: _ } => b - Exponential { size_var: _ } => b - Factorial { size_var: _ } => b - Unknown { diagnostic: _ } => b - } - } -} From 3c12ebf8ad049f3ffd1150e042913cd60ea15769 Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Wed, 20 May 2026 20:00:07 -0400 Subject: [PATCH 08/14] =?UTF-8?q?WIP:=20T-12=20=E2=80=94=20lens/cost.dag?= =?UTF-8?q?=20+=20lens/complexity.dag=20(one=20mechanism,=20two=20projecti?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/v4/lens/complexity.dag | 88 +++++++------------ src/v4/test/claim/lens_complexity/map_id.dag | 4 +- src/v4/test/claim/lens_complexity/nested.dag | 6 +- .../while_external_condition.dag | 10 ++- 4 files changed, 43 insertions(+), 65 deletions(-) diff --git a/src/v4/lens/complexity.dag b/src/v4/lens/complexity.dag index 6c079a5b055..51779f93437 100644 --- a/src/v4/lens/complexity.dag +++ b/src/v4/lens/complexity.dag @@ -33,15 +33,10 @@ module v4.lens.complexity import v4.lens.cost { AsymptoticClass, - ClassExponential, - ClassFactorial, ClassConstant, ClassUnknown, ClassLinear, - ClassLinearithmic, - ClassLog, ClassPolynomial, - ClassPolyLog, ConstantCost, ExponentialCost, FactorialCost, @@ -80,18 +75,31 @@ data complexity_projection_virtual_file: Symbol = complexity_projection_virtual_ // projected user-facing bound without adding another cost mechanism. // 🟢 coproduct dissolution — T-12-COMPLEXITY-PROJECTION. type ComplexityBound - = Constant - | Logarithmic { size_var: SizeVariable } - | Linear { size_var: SizeVariable } - | Linearithmic { size_var: SizeVariable } - | PolyLogarithmic { size_var: SizeVariable, exponent: PolyLogExponent } - | Polynomial { size_var: SizeVariable, degree: PolynomialDegree } - | Exponential { size_var: SizeVariable } - | Factorial { size_var: SizeVariable } - | Unknown { diagnostic: Diagnostic } + = ConstantComplexity + | SizedComplexity { class: AsymptoticClass, size_var: SizeVariable } + | UnknownComplexity { diagnostic: Diagnostic } fn unknown_complexity() -> ComplexityBound { - Unknown { diagnostic: complexity_projection_diagnostic() } + UnknownComplexity { diagnostic: complexity_projection_diagnostic() } +} + +fn sized_complexity(class: AsymptoticClass, size_var: SizeVariable) -> ComplexityBound { + SizedComplexity { class: class, size_var: size_var } +} + +fn linear_complexity(size_var: SizeVariable) -> ComplexityBound { + sized_complexity(class: ClassLinear, size_var: size_var) +} + +fn polynomial_complexity( + size_var: SizeVariable, + degree: PolynomialDegree +) -> ComplexityBound { + sized_complexity(class: ClassPolynomial { degree: degree }, size_var: size_var) +} + +fn unknown_complexity_with_diagnostic(diagnostic: Diagnostic) -> ComplexityBound { + UnknownComplexity { diagnostic: diagnostic } } fn representative_size_variable(cost: SymbolicCost) -> Witness { @@ -124,43 +132,13 @@ fn complexity_bound_from_class( size_var: Witness ) -> ComplexityBound { match class { - ClassConstant => Constant - ClassLog => - match size_var { - Holds { value: v } => Logarithmic { size_var: v } - Violates { diagnostic: _ } => unknown_complexity() - } - ClassLinear => - match size_var { - Holds { value: v } => Linear { size_var: v } - Violates { diagnostic: _ } => unknown_complexity() - } - ClassLinearithmic => - match size_var { - Holds { value: v } => Linearithmic { size_var: v } - Violates { diagnostic: _ } => unknown_complexity() - } - ClassPolyLog { exponent: e } => - match size_var { - Holds { value: v } => PolyLogarithmic { size_var: v, exponent: e } - Violates { diagnostic: _ } => unknown_complexity() - } - ClassPolynomial { degree: d } => - match size_var { - Holds { value: v } => Polynomial { size_var: v, degree: d } - Violates { diagnostic: _ } => unknown_complexity() - } - ClassExponential => - match size_var { - Holds { value: v } => Exponential { size_var: v } - Violates { diagnostic: _ } => unknown_complexity() - } - ClassFactorial => + ClassConstant => ConstantComplexity + ClassUnknown => unknown_complexity() + _ => match size_var { - Holds { value: v } => Factorial { size_var: v } + Holds { value: v } => SizedComplexity { class: class, size_var: v } Violates { diagnostic: _ } => unknown_complexity() } - ClassUnknown => unknown_complexity() } } @@ -194,15 +172,9 @@ fn complexity_lens(n: Node) -> Witness { fn complexity_bound_class(b: ComplexityBound) -> AsymptoticClass { match b { - Constant => ClassConstant - Logarithmic { size_var: _ } => ClassLog - Linear { size_var: _ } => ClassLinear - Linearithmic { size_var: _ } => ClassLinearithmic - PolyLogarithmic { size_var: _, exponent: e } => ClassPolyLog { exponent: e } - Polynomial { size_var: _, degree: d } => ClassPolynomial { degree: d } - Exponential { size_var: _ } => ClassExponential - Factorial { size_var: _ } => ClassFactorial - Unknown { diagnostic: _ } => ClassUnknown + ConstantComplexity => ClassConstant + SizedComplexity { class: c, size_var: _ } => c + UnknownComplexity { diagnostic: _ } => ClassUnknown } } diff --git a/src/v4/test/claim/lens_complexity/map_id.dag b/src/v4/test/claim/lens_complexity/map_id.dag index e41a9e15938..79bd03c0cda 100644 --- a/src/v4/test/claim/lens_complexity/map_id.dag +++ b/src/v4/test/claim/lens_complexity/map_id.dag @@ -6,7 +6,7 @@ module v4.test.claim.lens_complexity.map_id -import v4.lens.complexity { ComplexityBound, Linear, complexity_lens } +import v4.lens.complexity { ComplexityBound, complexity_lens, linear_complexity } import v4.lens.cost { LinearCost, SizeVariable, SymbolicCost, cost_lens } import v4.std.diagnostic { AmbiguousIntent, @@ -52,7 +52,7 @@ data map_id_input: Node = Node { data map_id_claim: LensComplexityClaim = LensComplexityClaim { input: map_id_input, expected_cost: LinearCost { variable: SizeVariable { source: map_id_input } }, - expected_complexity: Linear { size_var: SizeVariable { source: map_id_input } }, + expected_complexity: linear_complexity(size_var: SizeVariable { source: map_id_input }), falsification: Diagnostic { reason: map_id_claim_reason, at: Textual { file: map_id_claim_file, extent: WholeFile }, diff --git a/src/v4/test/claim/lens_complexity/nested.dag b/src/v4/test/claim/lens_complexity/nested.dag index 0908487f370..ecc81f16ee5 100644 --- a/src/v4/test/claim/lens_complexity/nested.dag +++ b/src/v4/test/claim/lens_complexity/nested.dag @@ -6,7 +6,7 @@ module v4.test.claim.lens_complexity.nested -import v4.lens.complexity { ComplexityBound, Polynomial, complexity_lens } +import v4.lens.complexity { ComplexityBound, complexity_lens, polynomial_complexity } import v4.lens.cost { LinearCost, PolynomialDegree, @@ -71,10 +71,10 @@ data nested_claim: LensComplexityClaim = LensComplexityClaim { lhs: LinearCost { variable: SizeVariable { source: nested_input } }, rhs: LinearCost { variable: SizeVariable { source: nested_inner } } }, - expected_complexity: Polynomial { + expected_complexity: polynomial_complexity( size_var: SizeVariable { source: nested_input }, degree: PolynomialDegree { predecessor_after_linear: NonZeroNat { prev: Zero } } - }, + ), falsification: Diagnostic { reason: nested_claim_reason, at: Textual { file: nested_claim_file, extent: WholeFile }, diff --git a/src/v4/test/claim/lens_complexity/while_external_condition.dag b/src/v4/test/claim/lens_complexity/while_external_condition.dag index 249e105ee9e..938a8164bbd 100644 --- a/src/v4/test/claim/lens_complexity/while_external_condition.dag +++ b/src/v4/test/claim/lens_complexity/while_external_condition.dag @@ -6,7 +6,11 @@ module v4.test.claim.lens_complexity.while_external_condition -import v4.lens.complexity { ComplexityBound, Unknown, complexity_lens } +import v4.lens.complexity { + ComplexityBound, + complexity_lens, + unknown_complexity_with_diagnostic +} import v4.lens.cost { SymbolicCost, UnknownCost, cost_lens } import v4.std.diagnostic { AmbiguousIntent, @@ -47,7 +51,9 @@ data while_external_condition_diagnostic: Diagnostic = Diagnostic { data while_external_condition_claim: LensComplexityClaim = LensComplexityClaim { input: while_external_condition_input, expected_cost: UnknownCost { reason: "T-9-not-landed: loop bound" }, - expected_complexity: Unknown { diagnostic: while_external_condition_diagnostic }, + expected_complexity: unknown_complexity_with_diagnostic( + diagnostic: while_external_condition_diagnostic + ), falsification: while_external_condition_diagnostic } From 39b54c1819584390477cd2b2d973cc867f854253 Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Thu, 21 May 2026 00:03:48 +0000 Subject: [PATCH 09/14] Remove unused complexity diagnostic import --- src/v4/lens/complexity.dag | 1 - 1 file changed, 1 deletion(-) diff --git a/src/v4/lens/complexity.dag b/src/v4/lens/complexity.dag index 51779f93437..bb7d83ccbdf 100644 --- a/src/v4/lens/complexity.dag +++ b/src/v4/lens/complexity.dag @@ -58,7 +58,6 @@ import v4.lens.cost { import v4.std.diagnostic { AmbiguousIntent, Diagnostic, - NodeLocus, Textual, WholeFile, Unavailable From ec589fae94cf721ed85c79ec66bd1fc0ae0ca75c Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Wed, 20 May 2026 20:19:41 -0400 Subject: [PATCH 10/14] =?UTF-8?q?WIP:=20T-12=20=E2=80=94=20lens/cost.dag?= =?UTF-8?q?=20+=20lens/complexity.dag=20(one=20mechanism,=20two=20projecti?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/v4/lens/cost.dag | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/src/v4/lens/cost.dag b/src/v4/lens/cost.dag index c921d074339..31aee684d70 100644 --- a/src/v4/lens/cost.dag +++ b/src/v4/lens/cost.dag @@ -531,13 +531,12 @@ fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { ClassPolynomial { degree: ad } => match b { ClassConstant => a - ClassLog => ClassPolynomial { degree: increment_polynomial_degree(d: ad) } + ClassLog => ClassUnknown ClassLinear => ClassPolynomial { degree: increment_polynomial_degree(d: ad) } - ClassLinearithmic => ClassPolynomial { degree: increment_polynomial_degree(d: ad) } + ClassLinearithmic => ClassUnknown ClassPolynomial { degree: bd } => ClassPolynomial { degree: add_polynomial_degree(a: ad, b: bd) } - ClassPolyLog { exponent: _ } => - ClassPolynomial { degree: increment_polynomial_degree(d: ad) } + ClassPolyLog { exponent: _ } => ClassUnknown ClassExponential => ClassExponential ClassFactorial => ClassFactorial ClassUnknown => ClassUnknown @@ -547,10 +546,10 @@ fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { ClassConstant => ClassLinear ClassLog => ClassLinearithmic ClassLinear => ClassPolynomial { degree: polynomial_degree_two() } - ClassLinearithmic => ClassPolynomial { degree: polynomial_degree_two() } + ClassLinearithmic => ClassUnknown ClassPolynomial { degree: bd } => ClassPolynomial { degree: increment_polynomial_degree(d: bd) } - ClassPolyLog { exponent: _ } => ClassPolynomial { degree: polynomial_degree_two() } + ClassPolyLog { exponent: _ } => ClassUnknown ClassExponential => ClassExponential ClassFactorial => ClassFactorial ClassUnknown => ClassUnknown @@ -558,12 +557,11 @@ fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { ClassLinearithmic => match b { ClassConstant => ClassLinearithmic - ClassLog => ClassPolynomial { degree: polynomial_degree_two() } - ClassLinear => ClassPolynomial { degree: polynomial_degree_two() } - ClassLinearithmic => ClassPolynomial { degree: polynomial_degree_two() } - ClassPolynomial { degree: bd } => - ClassPolynomial { degree: increment_polynomial_degree(d: bd) } - ClassPolyLog { exponent: _ } => ClassPolynomial { degree: polynomial_degree_two() } + ClassLog => ClassUnknown + ClassLinear => ClassUnknown + ClassLinearithmic => ClassUnknown + ClassPolynomial { degree: _ } => ClassUnknown + ClassPolyLog { exponent: _ } => ClassUnknown ClassExponential => ClassExponential ClassFactorial => ClassFactorial ClassUnknown => ClassUnknown @@ -571,15 +569,12 @@ fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { ClassPolyLog { exponent: e } => match b { ClassConstant => a - ClassLog => a - ClassPolyLog { exponent: _ } => a - ClassLinear => ClassLinear - ClassLinearithmic => ClassPolynomial { degree: polynomial_degree_two() } - ClassPolynomial { degree: pd } => - ClassPolynomial { degree: max_polynomial_degree( - a: pd, - b: polylog_exponent_to_polynomial_degree(e: e) - ) } + ClassLog => ClassPolyLog { exponent: increment_polylog_exponent(e: e) } + ClassPolyLog { exponent: be } => + ClassPolyLog { exponent: add_polylog_exponent(a: e, b: be) } + ClassLinear => ClassUnknown + ClassLinearithmic => ClassUnknown + ClassPolynomial { degree: _ } => ClassUnknown ClassExponential => ClassExponential ClassFactorial => ClassFactorial ClassUnknown => ClassUnknown @@ -591,9 +586,8 @@ fn multiply_classes(a: AsymptoticClass, b: AsymptoticClass) -> AsymptoticClass { ClassPolyLog { exponent: e } => ClassPolyLog { exponent: increment_polylog_exponent(e: e) } ClassLinear => ClassLinearithmic - ClassLinearithmic => ClassLinearithmic - ClassPolynomial { degree: bd } => - ClassPolynomial { degree: increment_polynomial_degree(d: bd) } + ClassLinearithmic => ClassUnknown + ClassPolynomial { degree: _ } => ClassUnknown ClassExponential => ClassExponential ClassFactorial => ClassFactorial ClassUnknown => ClassUnknown From e96e2bf77ed3383da8c15b0fdb97314363067e30 Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Thu, 21 May 2026 00:20:59 +0000 Subject: [PATCH 11/14] Fix complexity projection information loss --- src/v4/lens/complexity.dag | 98 ++++++++++++++------ src/v4/lens/cost.dag | 9 ++ src/v4/test/claim/lens_complexity/nested.dag | 15 ++- 3 files changed, 93 insertions(+), 29 deletions(-) diff --git a/src/v4/lens/complexity.dag b/src/v4/lens/complexity.dag index bb7d83ccbdf..764d818a647 100644 --- a/src/v4/lens/complexity.dag +++ b/src/v4/lens/complexity.dag @@ -47,7 +47,6 @@ import v4.lens.cost { ProductCost, SumCost, UnknownCost, - PolyLogExponent, PolynomialDegree, SymbolicCost, SizeVariable, @@ -69,58 +68,105 @@ import v4.std.witness { Holds, Violates, Witness } data complexity_reason_unmodeled_cost_projection: Symbol = complexity_reason_unmodeled_cost_projection data complexity_projection_virtual_file: Symbol = complexity_projection_virtual_file +// 🟢 coproduct dissolution — T-12-COMPLEXITY-PROJECTION. +type ComplexityVariables + = SingleComplexityVariable { size_var: SizeVariable } + | CompositeComplexityVariables { lhs: ComplexityVariables, rhs: ComplexityVariables } + // ComplexityBound is the asymptotic projection of `SymbolicCost`. The // `class` relation remains owned by `cost.dag`; this carrier packages the // projected user-facing bound without adding another cost mechanism. // 🟢 coproduct dissolution — T-12-COMPLEXITY-PROJECTION. type ComplexityBound = ConstantComplexity - | SizedComplexity { class: AsymptoticClass, size_var: SizeVariable } + | SizedComplexity { class: AsymptoticClass, variables: ComplexityVariables } | UnknownComplexity { diagnostic: Diagnostic } fn unknown_complexity() -> ComplexityBound { UnknownComplexity { diagnostic: complexity_projection_diagnostic() } } -fn sized_complexity(class: AsymptoticClass, size_var: SizeVariable) -> ComplexityBound { - SizedComplexity { class: class, size_var: size_var } +fn single_complexity_variable(size_var: SizeVariable) -> ComplexityVariables { + SingleComplexityVariable { size_var: size_var } +} + +fn composite_complexity_variables( + lhs: ComplexityVariables, + rhs: ComplexityVariables +) -> ComplexityVariables { + CompositeComplexityVariables { lhs: lhs, rhs: rhs } +} + +fn sized_complexity(class: AsymptoticClass, variables: ComplexityVariables) -> ComplexityBound { + SizedComplexity { class: class, variables: variables } } fn linear_complexity(size_var: SizeVariable) -> ComplexityBound { - sized_complexity(class: ClassLinear, size_var: size_var) + sized_complexity(class: ClassLinear, variables: single_complexity_variable(size_var: size_var)) } fn polynomial_complexity( size_var: SizeVariable, degree: PolynomialDegree ) -> ComplexityBound { - sized_complexity(class: ClassPolynomial { degree: degree }, size_var: size_var) + sized_complexity( + class: ClassPolynomial { degree: degree }, + variables: single_complexity_variable(size_var: size_var) + ) +} + +fn multidimensional_polynomial_complexity( + variables: ComplexityVariables, + degree: PolynomialDegree +) -> ComplexityBound { + sized_complexity(class: ClassPolynomial { degree: degree }, variables: variables) } fn unknown_complexity_with_diagnostic(diagnostic: Diagnostic) -> ComplexityBound { UnknownComplexity { diagnostic: diagnostic } } -fn representative_size_variable(cost: SymbolicCost) -> Witness { +fn combine_complexity_variables( + lhs: Witness, + rhs: Witness +) -> Witness { + match lhs { + Holds { value: lv } => + match rhs { + Holds { value: rv } => + Holds { value: composite_complexity_variables(lhs: lv, rhs: rv) } + Violates { diagnostic: _ } => Holds { value: lv } + } + Violates { diagnostic: _ } => rhs + } +} + +fn complexity_variables_from_cost(cost: SymbolicCost) -> Witness { match cost { ConstantCost { value: _ } => Violates { diagnostic: complexity_projection_diagnostic() } - LogCost { variable: v } => Holds { value: v } - LinearCost { variable: v } => Holds { value: v } - PolynomialCost { variable: v, degree: _ } => Holds { value: v } - PolyLogCost { variable: v, exponent: _ } => Holds { value: v } - ExponentialCost { variable: v, base: _ } => Holds { value: v } - FactorialCost { variable: v } => Holds { value: v } + LogCost { variable: v } => + Holds { value: single_complexity_variable(size_var: v) } + LinearCost { variable: v } => + Holds { value: single_complexity_variable(size_var: v) } + PolynomialCost { variable: v, degree: _ } => + Holds { value: single_complexity_variable(size_var: v) } + PolyLogCost { variable: v, exponent: _ } => + Holds { value: single_complexity_variable(size_var: v) } + ExponentialCost { variable: v, base: _ } => + Holds { value: single_complexity_variable(size_var: v) } + FactorialCost { variable: v } => + Holds { value: single_complexity_variable(size_var: v) } SumCost { lhs: lhs, rhs: rhs } => - match representative_size_variable(cost: lhs) { - Holds { value: v } => Holds { value: v } - Violates { diagnostic: _ } => representative_size_variable(cost: rhs) - } + combine_complexity_variables( + lhs: complexity_variables_from_cost(cost: lhs), + rhs: complexity_variables_from_cost(cost: rhs) + ) ProductCost { lhs: lhs, rhs: rhs } => - match representative_size_variable(cost: lhs) { - Holds { value: v } => Holds { value: v } - Violates { diagnostic: _ } => representative_size_variable(cost: rhs) - } + combine_complexity_variables( + lhs: complexity_variables_from_cost(cost: lhs), + rhs: complexity_variables_from_cost(cost: rhs) + ) UnknownCost { reason: _ } => Violates { diagnostic: complexity_projection_diagnostic() } } @@ -128,14 +174,14 @@ fn representative_size_variable(cost: SymbolicCost) -> Witness { fn complexity_bound_from_class( class: AsymptoticClass, - size_var: Witness + variables: Witness ) -> ComplexityBound { match class { ClassConstant => ConstantComplexity ClassUnknown => unknown_complexity() _ => - match size_var { - Holds { value: v } => SizedComplexity { class: class, size_var: v } + match variables { + Holds { value: v } => SizedComplexity { class: class, variables: v } Violates { diagnostic: _ } => unknown_complexity() } } @@ -144,7 +190,7 @@ fn complexity_bound_from_class( fn complexity_bound_from_cost(cost: SymbolicCost) -> ComplexityBound { complexity_bound_from_class( class: asymptotic_class_of_cost(cost: cost), - size_var: representative_size_variable(cost: cost) + variables: complexity_variables_from_cost(cost: cost) ) } @@ -172,7 +218,7 @@ fn complexity_lens(n: Node) -> Witness { fn complexity_bound_class(b: ComplexityBound) -> AsymptoticClass { match b { ConstantComplexity => ClassConstant - SizedComplexity { class: c, size_var: _ } => c + SizedComplexity { class: c, variables: _ } => c UnknownComplexity { diagnostic: _ } => ClassUnknown } } diff --git a/src/v4/lens/cost.dag b/src/v4/lens/cost.dag index 31aee684d70..112a16cce69 100644 --- a/src/v4/lens/cost.dag +++ b/src/v4/lens/cost.dag @@ -356,6 +356,15 @@ fn increment_polylog_exponent(e: PolyLogExponent) -> PolyLogExponent { } } +fn add_polylog_exponent(a: PolyLogExponent, b: PolyLogExponent) -> PolyLogExponent { + PolyLogExponent { + predecessor_after_log: add_nonzero_nat( + a: a.predecessor_after_log, + b: b.predecessor_after_log + ) + } +} + fn polynomial_degree_to_polylog_exponent(d: PolynomialDegree) -> PolyLogExponent { PolyLogExponent { predecessor_after_log: d.predecessor_after_linear } } diff --git a/src/v4/test/claim/lens_complexity/nested.dag b/src/v4/test/claim/lens_complexity/nested.dag index ecc81f16ee5..d9bc0254263 100644 --- a/src/v4/test/claim/lens_complexity/nested.dag +++ b/src/v4/test/claim/lens_complexity/nested.dag @@ -6,7 +6,13 @@ module v4.test.claim.lens_complexity.nested -import v4.lens.complexity { ComplexityBound, complexity_lens, polynomial_complexity } +import v4.lens.complexity { + ComplexityBound, + composite_complexity_variables, + complexity_lens, + multidimensional_polynomial_complexity, + single_complexity_variable +} import v4.lens.cost { LinearCost, PolynomialDegree, @@ -71,8 +77,11 @@ data nested_claim: LensComplexityClaim = LensComplexityClaim { lhs: LinearCost { variable: SizeVariable { source: nested_input } }, rhs: LinearCost { variable: SizeVariable { source: nested_inner } } }, - expected_complexity: polynomial_complexity( - size_var: SizeVariable { source: nested_input }, + expected_complexity: multidimensional_polynomial_complexity( + variables: composite_complexity_variables( + lhs: single_complexity_variable(size_var: SizeVariable { source: nested_input }), + rhs: single_complexity_variable(size_var: SizeVariable { source: nested_inner }) + ), degree: PolynomialDegree { predecessor_after_linear: NonZeroNat { prev: Zero } } ), falsification: Diagnostic { From 2c699245a5442ebf01c7c283fd3dbc6b6ac3b485 Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Wed, 20 May 2026 20:34:39 -0400 Subject: [PATCH 12/14] =?UTF-8?q?WIP:=20T-12=20=E2=80=94=20lens/cost.dag?= =?UTF-8?q?=20+=20lens/complexity.dag=20(one=20mechanism,=20two=20projecti?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/v4/lens/complexity.dag | 78 +++++++++++++++++++++++++++++++------- src/v4/lens/cost.dag | 32 ++++++++-------- 2 files changed, 81 insertions(+), 29 deletions(-) diff --git a/src/v4/lens/complexity.dag b/src/v4/lens/complexity.dag index 764d818a647..6390acc4a0b 100644 --- a/src/v4/lens/complexity.dag +++ b/src/v4/lens/complexity.dag @@ -34,9 +34,14 @@ module v4.lens.complexity import v4.lens.cost { AsymptoticClass, ClassConstant, - ClassUnknown, + ClassExponential, + ClassFactorial, + ClassLinearithmic, ClassLinear, + ClassLog, + ClassPolyLog, ClassPolynomial, + ClassUnknown, ConstantCost, ExponentialCost, FactorialCost, @@ -47,6 +52,7 @@ import v4.lens.cost { ProductCost, SumCost, UnknownCost, + PolyLogExponent, PolynomialDegree, SymbolicCost, SizeVariable, @@ -54,6 +60,7 @@ import v4.lens.cost { asymptotic_class_of_cost, cost_lens } +import v4.std.collection { List } import v4.std.diagnostic { AmbiguousIntent, Diagnostic, @@ -69,9 +76,17 @@ data complexity_reason_unmodeled_cost_projection: Symbol = complexity_reason_unm data complexity_projection_virtual_file: Symbol = complexity_projection_virtual_file // 🟢 coproduct dissolution — T-12-COMPLEXITY-PROJECTION. -type ComplexityVariables - = SingleComplexityVariable { size_var: SizeVariable } - | CompositeComplexityVariables { lhs: ComplexityVariables, rhs: ComplexityVariables } +type ComplexityVariables = List + +// 🟢 coproduct dissolution — T-12-COMPLEXITY-PROJECTION. +type SizeIndexedAsymptoticClass + = SizedLogClass + | SizedLinearClass + | SizedLinearithmicClass + | SizedPolynomialClass { degree: PolynomialDegree } + | SizedPolyLogClass { exponent: PolyLogExponent } + | SizedExponentialClass + | SizedFactorialClass // ComplexityBound is the asymptotic projection of `SymbolicCost`. The // `class` relation remains owned by `cost.dag`; this carrier packages the @@ -79,7 +94,7 @@ type ComplexityVariables // 🟢 coproduct dissolution — T-12-COMPLEXITY-PROJECTION. type ComplexityBound = ConstantComplexity - | SizedComplexity { class: AsymptoticClass, variables: ComplexityVariables } + | SizedComplexity { class: SizeIndexedAsymptoticClass, variables: ComplexityVariables } | UnknownComplexity { diagnostic: Diagnostic } fn unknown_complexity() -> ComplexityBound { @@ -87,22 +102,28 @@ fn unknown_complexity() -> ComplexityBound { } fn single_complexity_variable(size_var: SizeVariable) -> ComplexityVariables { - SingleComplexityVariable { size_var: size_var } + [size_var] } fn composite_complexity_variables( lhs: ComplexityVariables, rhs: ComplexityVariables ) -> ComplexityVariables { - CompositeComplexityVariables { lhs: lhs, rhs: rhs } + concat(lhs, rhs) } -fn sized_complexity(class: AsymptoticClass, variables: ComplexityVariables) -> ComplexityBound { +fn sized_complexity( + class: SizeIndexedAsymptoticClass, + variables: ComplexityVariables +) -> ComplexityBound { SizedComplexity { class: class, variables: variables } } fn linear_complexity(size_var: SizeVariable) -> ComplexityBound { - sized_complexity(class: ClassLinear, variables: single_complexity_variable(size_var: size_var)) + sized_complexity( + class: SizedLinearClass, + variables: single_complexity_variable(size_var: size_var) + ) } fn polynomial_complexity( @@ -110,7 +131,7 @@ fn polynomial_complexity( degree: PolynomialDegree ) -> ComplexityBound { sized_complexity( - class: ClassPolynomial { degree: degree }, + class: SizedPolynomialClass { degree: degree }, variables: single_complexity_variable(size_var: size_var) ) } @@ -119,7 +140,7 @@ fn multidimensional_polynomial_complexity( variables: ComplexityVariables, degree: PolynomialDegree ) -> ComplexityBound { - sized_complexity(class: ClassPolynomial { degree: degree }, variables: variables) + sized_complexity(class: SizedPolynomialClass { degree: degree }, variables: variables) } fn unknown_complexity_with_diagnostic(diagnostic: Diagnostic) -> ComplexityBound { @@ -181,12 +202,31 @@ fn complexity_bound_from_class( ClassUnknown => unknown_complexity() _ => match variables { - Holds { value: v } => SizedComplexity { class: class, variables: v } + Holds { value: v } => complexity_bound_from_sized_class(class: class, variables: v) Violates { diagnostic: _ } => unknown_complexity() } } } +fn complexity_bound_from_sized_class( + class: AsymptoticClass, + variables: ComplexityVariables +) -> ComplexityBound { + match class { + ClassConstant => ConstantComplexity + ClassLog => SizedComplexity { class: SizedLogClass, variables: variables } + ClassLinear => SizedComplexity { class: SizedLinearClass, variables: variables } + ClassLinearithmic => SizedComplexity { class: SizedLinearithmicClass, variables: variables } + ClassPolynomial { degree: d } => + SizedComplexity { class: SizedPolynomialClass { degree: d }, variables: variables } + ClassPolyLog { exponent: e } => + SizedComplexity { class: SizedPolyLogClass { exponent: e }, variables: variables } + ClassExponential => SizedComplexity { class: SizedExponentialClass, variables: variables } + ClassFactorial => SizedComplexity { class: SizedFactorialClass, variables: variables } + ClassUnknown => unknown_complexity() + } +} + fn complexity_bound_from_cost(cost: SymbolicCost) -> ComplexityBound { complexity_bound_from_class( class: asymptotic_class_of_cost(cost: cost), @@ -218,11 +258,23 @@ fn complexity_lens(n: Node) -> Witness { fn complexity_bound_class(b: ComplexityBound) -> AsymptoticClass { match b { ConstantComplexity => ClassConstant - SizedComplexity { class: c, variables: _ } => c + SizedComplexity { class: c, variables: _ } => asymptotic_class_from_sized_class(class: c) UnknownComplexity { diagnostic: _ } => ClassUnknown } } +fn asymptotic_class_from_sized_class(class: SizeIndexedAsymptoticClass) -> AsymptoticClass { + match class { + SizedLogClass => ClassLog + SizedLinearClass => ClassLinear + SizedLinearithmicClass => ClassLinearithmic + SizedPolynomialClass { degree: d } => ClassPolynomial { degree: d } + SizedPolyLogClass { exponent: e } => ClassPolyLog { exponent: e } + SizedExponentialClass => ClassExponential + SizedFactorialClass => ClassFactorial + } +} + fn complexity_bound_dominates(a: ComplexityBound, b: ComplexityBound) -> Bool { asymptotic_class_dominates( a: complexity_bound_class(b: a), diff --git a/src/v4/lens/cost.dag b/src/v4/lens/cost.dag index 112a16cce69..339d30c1ece 100644 --- a/src/v4/lens/cost.dag +++ b/src/v4/lens/cost.dag @@ -98,7 +98,7 @@ type ExponentialBase { // 🟢 coproduct dissolution — T-12-COST-CARRIER. type SymbolicCost - = ConstantCost { value: Int } + = ConstantCost { value: Nat } | LinearCost { variable: SizeVariable } | LogCost { variable: SizeVariable } | PolynomialCost { variable: SizeVariable, degree: PolynomialDegree } @@ -135,16 +135,16 @@ type SymbolicCostFoldState { cost: SymbolicCost } -fn constant_cost(value: Int) -> SymbolicCost { +fn constant_cost(value: Nat) -> SymbolicCost { ConstantCost { value: value } } fn zero_cost() -> SymbolicCost { - constant_cost(value: 0) + constant_cost(value: Zero) } fn unit_cost() -> SymbolicCost { - constant_cost(value: 1) + constant_cost(value: Succ { prev: Zero }) } fn nonzero_one() -> NonZeroNat { @@ -247,11 +247,11 @@ fn cost_fold_init(n: Node) -> SymbolicCostFoldState { fn symbolic_sequential(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { match a { - ConstantCost { value: 0 } => b + ConstantCost { value: Zero } => b UnknownCost { reason: _ } => a _ => match b { - ConstantCost { value: 0 } => a + ConstantCost { value: Zero } => a UnknownCost { reason: _ } => b _ => SumCost { lhs: a, rhs: b } } @@ -260,13 +260,13 @@ fn symbolic_sequential(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { fn symbolic_product(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { match a { - ConstantCost { value: 0 } => zero_cost() - ConstantCost { value: 1 } => b + ConstantCost { value: Zero } => zero_cost() + ConstantCost { value: Succ { prev: Zero } } => b UnknownCost { reason: _ } => a _ => match b { - ConstantCost { value: 0 } => zero_cost() - ConstantCost { value: 1 } => a + ConstantCost { value: Zero } => zero_cost() + ConstantCost { value: Succ { prev: Zero } } => a UnknownCost { reason: _ } => b _ => ProductCost { lhs: a, rhs: b } } @@ -275,10 +275,10 @@ fn symbolic_product(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { fn symbolic_max(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { match a { - ConstantCost { value: 0 } => b + ConstantCost { value: Zero } => b _ => match b { - ConstantCost { value: 0 } => a + ConstantCost { value: Zero } => a _ => if a == b { a @@ -512,12 +512,12 @@ fn symbolic_cost_dominates(a: SymbolicCost, b: SymbolicCost) -> Bool { match a { ConstantCost { value: av } => match b { - ConstantCost { value: bv } => av >= bv + ConstantCost { value: bv } => nat_dominates(a: av, b: bv) _ => false } _ => match b { - ConstantCost { value: 0 } => true + ConstantCost { value: Zero } => true _ => asymptotic_class_dominates( a: asymptotic_class_of_cost(cost: a), @@ -641,7 +641,7 @@ fn cost_lattice_join(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { data symbolic_cost_sequential_monoid: Monoid = Monoid { semigroup: Semigroup { magma: Magma { op: symbolic_sequential } }, - identity: ConstantCost { value: 0 } + identity: ConstantCost { value: Zero } } data symbolic_cost_lattice: BoundedLattice = BoundedLattice { @@ -650,7 +650,7 @@ data symbolic_cost_lattice: BoundedLattice = BoundedLattice { join: cost_lattice_join }, top: UnknownCost { reason: cost_lattice_top_reason }, - bottom: ConstantCost { value: 0 } + bottom: ConstantCost { value: Zero } } data asymptotic_class_lattice: BoundedLattice = BoundedLattice { From 96bdce73e26c87c8693a3319827d716bfa8e9d0e Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Wed, 20 May 2026 20:36:10 -0400 Subject: [PATCH 13/14] =?UTF-8?q?WIP:=20T-12=20=E2=80=94=20lens/cost.dag?= =?UTF-8?q?=20+=20lens/complexity.dag=20(one=20mechanism,=20two=20projecti?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/v4/lens/cost.dag | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/v4/lens/cost.dag b/src/v4/lens/cost.dag index 339d30c1ece..d731f8784d1 100644 --- a/src/v4/lens/cost.dag +++ b/src/v4/lens/cost.dag @@ -36,6 +36,8 @@ module v4.lens.cost import v4.extdeps.languages.llvm_ir { LlvmInstruction } import v4.std.algebra { BoundedLattice, + Equal, + Greater, Lattice, Magma, Monoid, @@ -70,7 +72,7 @@ import v4.std.node { Value, fold_node } -import v4.std.nat { Nat, Succ, Zero } +import v4.std.nat { Nat, Succ, Zero, nat_add, nat_compare } import v4.std.witness { Holds, Violates, Witness } @@ -310,29 +312,15 @@ fn nonzero_nat_dominates(a: NonZeroNat, b: NonZeroNat) -> Bool { } fn nat_dominates(a: Nat, b: Nat) -> Bool { - match a { - Zero => - match b { - Zero => true - Succ { prev: _ } => false - } - Succ { prev: ap } => - match b { - Zero => true - Succ { prev: bp } => nat_dominates(a: ap, b: bp) - } + match nat_compare(a: a, b: b) { + Greater => true + Equal => true + _ => false } } fn add_nonzero_nat(a: NonZeroNat, b: NonZeroNat) -> NonZeroNat { - NonZeroNat { prev: Succ { prev: add_nat(a: a.prev, b: b.prev) } } -} - -fn add_nat(a: Nat, b: Nat) -> Nat { - match a { - Zero => b - Succ { prev: ap } => Succ { prev: add_nat(a: ap, b: b) } - } + NonZeroNat { prev: Succ { prev: nat_add(a: a.prev, b: b.prev) } } } fn add_polynomial_degree(a: PolynomialDegree, b: PolynomialDegree) -> PolynomialDegree { From f243c183dd318402bbfd2a4d5fc19602c38c7b72 Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Wed, 20 May 2026 21:36:05 -0400 Subject: [PATCH 14/14] =?UTF-8?q?WIP:=20T-12=20=E2=80=94=20lens/cost.dag?= =?UTF-8?q?=20+=20lens/complexity.dag=20(one=20mechanism,=20two=20projecti?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/v4/lens/complexity.dag | 33 ++++- src/v4/lens/cost.dag | 26 ++-- .../lens_complexity/algebra_receipts.dag | 120 ++++++++++++++++++ 3 files changed, 160 insertions(+), 19 deletions(-) create mode 100644 src/v4/test/claim/lens_complexity/algebra_receipts.dag diff --git a/src/v4/lens/complexity.dag b/src/v4/lens/complexity.dag index 6390acc4a0b..9039b35780f 100644 --- a/src/v4/lens/complexity.dag +++ b/src/v4/lens/complexity.dag @@ -69,6 +69,7 @@ import v4.std.diagnostic { Unavailable } import v4.std.node { Node, Symbol } +import v4.std.refinement { Refined, refined_base } import v4.std.witness { Holds, Violates, Witness } @@ -76,7 +77,7 @@ data complexity_reason_unmodeled_cost_projection: Symbol = complexity_reason_unm data complexity_projection_virtual_file: Symbol = complexity_projection_virtual_file // 🟢 coproduct dissolution — T-12-COMPLEXITY-PROJECTION. -type ComplexityVariables = List +type ComplexityVariables = Refined> // 🟢 coproduct dissolution — T-12-COMPLEXITY-PROJECTION. type SizeIndexedAsymptoticClass @@ -102,14 +103,14 @@ fn unknown_complexity() -> ComplexityBound { } fn single_complexity_variable(size_var: SizeVariable) -> ComplexityVariables { - [size_var] + Refined { base: [size_var] } } fn composite_complexity_variables( lhs: ComplexityVariables, rhs: ComplexityVariables ) -> ComplexityVariables { - concat(lhs, rhs) + Refined { base: concat(refined_base(r: lhs), refined_base(r: rhs)) } } fn sized_complexity( @@ -276,8 +277,26 @@ fn asymptotic_class_from_sized_class(class: SizeIndexedAsymptoticClass) -> Asymp } fn complexity_bound_dominates(a: ComplexityBound, b: ComplexityBound) -> Bool { - asymptotic_class_dominates( - a: complexity_bound_class(b: a), - b: complexity_bound_class(b: b) - ) + match a { + UnknownComplexity { diagnostic: _ } => true + ConstantComplexity => + match b { + ConstantComplexity => true + _ => false + } + SizedComplexity { class: ac, variables: av } => + match b { + UnknownComplexity { diagnostic: _ } => false + ConstantComplexity => true + SizedComplexity { class: bc, variables: bv } => + if refined_base(r: av) == refined_base(r: bv) { + asymptotic_class_dominates( + a: asymptotic_class_from_sized_class(class: ac), + b: asymptotic_class_from_sized_class(class: bc) + ) + } else { + false + } + } + } } diff --git a/src/v4/lens/cost.dag b/src/v4/lens/cost.dag index d731f8784d1..c63171569c3 100644 --- a/src/v4/lens/cost.dag +++ b/src/v4/lens/cost.dag @@ -319,16 +319,15 @@ fn nat_dominates(a: Nat, b: Nat) -> Bool { } } -fn add_nonzero_nat(a: NonZeroNat, b: NonZeroNat) -> NonZeroNat { - NonZeroNat { prev: Succ { prev: nat_add(a: a.prev, b: b.prev) } } -} - fn add_polynomial_degree(a: PolynomialDegree, b: PolynomialDegree) -> PolynomialDegree { PolynomialDegree { - predecessor_after_linear: add_nonzero_nat( - a: a.predecessor_after_linear, - b: b.predecessor_after_linear - ) + predecessor_after_linear: NonZeroNat { + prev: Succ { + prev: Succ { + prev: nat_add(a: a.predecessor_after_linear.prev, b: b.predecessor_after_linear.prev) + } + } + } } } @@ -346,10 +345,13 @@ fn increment_polylog_exponent(e: PolyLogExponent) -> PolyLogExponent { fn add_polylog_exponent(a: PolyLogExponent, b: PolyLogExponent) -> PolyLogExponent { PolyLogExponent { - predecessor_after_log: add_nonzero_nat( - a: a.predecessor_after_log, - b: b.predecessor_after_log - ) + predecessor_after_log: NonZeroNat { + prev: Succ { + prev: Succ { + prev: nat_add(a: a.predecessor_after_log.prev, b: b.predecessor_after_log.prev) + } + } + } } } diff --git a/src/v4/test/claim/lens_complexity/algebra_receipts.dag b/src/v4/test/claim/lens_complexity/algebra_receipts.dag new file mode 100644 index 00000000000..6211008fc74 --- /dev/null +++ b/src/v4/test/claim/lens_complexity/algebra_receipts.dag @@ -0,0 +1,120 @@ +// src/v4/test/claim/lens_complexity/algebra_receipts.dag +// Scope: T-12 worked claim — algebra edge cases surfaced by review. +// Owns: polynomial_product_degree_claim, variable_sensitive_dominance_claim. +// Consumes: lens cost, lens complexity, std diagnostic, std node. +// Status: scaffold — compile-only until T-19/T-22 generated claim execution. + +module v4.test.claim.lens_complexity.algebra_receipts + +import v4.lens.complexity { + ComplexityBound, + complexity_bound_dominates, + complexity_bound_from_cost, + linear_complexity, + multidimensional_polynomial_complexity, + single_complexity_variable, + composite_complexity_variables +} +import v4.lens.cost { + PolynomialCost, + PolynomialDegree, + ProductCost, + SizeVariable, + SymbolicCost +} +import v4.std.cardinality { NonZeroNat } +import v4.std.diagnostic { + AmbiguousIntent, + Diagnostic, + Textual, + Unavailable, + WholeFile +} +import v4.std.nat { Succ, Zero } +import v4.std.node { + Atom, + Node, + Symbol, + TypeNode +} + +data algebra_receipts_file: Symbol = algebra_receipts_file +data algebra_receipts_reason: Symbol = algebra_receipts_reason +data algebra_receipts_x_symbol: Symbol = algebra_receipts_x_symbol +data algebra_receipts_y_symbol: Symbol = algebra_receipts_y_symbol + +type ComplexityProjectionClaim { + input_cost: SymbolicCost + expected_complexity: ComplexityBound + falsification: Diagnostic +} + +type ComplexityDominanceClaim { + lhs: ComplexityBound + rhs: ComplexityBound + expected_dominates: Bool + falsification: Diagnostic +} + +data algebra_receipts_x: Node = Node { + kind: TypeNode { connective: Atom { identity: algebra_receipts_x_symbol } }, + children: [] +} + +data algebra_receipts_y: Node = Node { + kind: TypeNode { connective: Atom { identity: algebra_receipts_y_symbol } }, + children: [] +} + +data polynomial_degree_two_receipt: PolynomialDegree = PolynomialDegree { + predecessor_after_linear: NonZeroNat { prev: Zero } +} + +data polynomial_degree_four_receipt: PolynomialDegree = PolynomialDegree { + predecessor_after_linear: NonZeroNat { prev: Succ { prev: Succ { prev: Zero } } } +} + +data algebra_receipts_diagnostic: Diagnostic = Diagnostic { + reason: algebra_receipts_reason, + at: Textual { file: algebra_receipts_file, extent: WholeFile }, + correction: Unavailable { reason: AmbiguousIntent } +} + +data polynomial_product_degree_claim: ComplexityProjectionClaim = ComplexityProjectionClaim { + input_cost: ProductCost { + lhs: PolynomialCost { + variable: SizeVariable { source: algebra_receipts_x }, + degree: polynomial_degree_two_receipt + }, + rhs: PolynomialCost { + variable: SizeVariable { source: algebra_receipts_y }, + degree: polynomial_degree_two_receipt + } + }, + expected_complexity: multidimensional_polynomial_complexity( + variables: composite_complexity_variables( + lhs: single_complexity_variable(size_var: SizeVariable { source: algebra_receipts_x }), + rhs: single_complexity_variable(size_var: SizeVariable { source: algebra_receipts_y }) + ), + degree: polynomial_degree_four_receipt + ), + falsification: algebra_receipts_diagnostic +} + +data variable_sensitive_dominance_claim: ComplexityDominanceClaim = ComplexityDominanceClaim { + lhs: linear_complexity(size_var: SizeVariable { source: algebra_receipts_x }), + rhs: linear_complexity(size_var: SizeVariable { source: algebra_receipts_y }), + expected_dominates: false, + falsification: algebra_receipts_diagnostic +} + +fn polynomial_product_projected_complexity() -> ComplexityBound { + complexity_bound_from_cost(cost: polynomial_product_degree_claim.input_cost) +} + +fn variable_sensitive_dominance_actual() -> Bool { + complexity_bound_dominates( + a: variable_sensitive_dominance_claim.lhs, + b: variable_sensitive_dominance_claim.rhs + ) +}