diff --git a/src/v4/lens/complexity.dag b/src/v4/lens/complexity.dag index ce8402a5f57..9039b35780f 100644 --- a/src/v4/lens/complexity.dag +++ b/src/v4/lens/complexity.dag @@ -30,3 +30,273 @@ module v4.lens.complexity + +import v4.lens.cost { + AsymptoticClass, + ClassConstant, + ClassExponential, + ClassFactorial, + ClassLinearithmic, + ClassLinear, + ClassLog, + ClassPolyLog, + ClassPolynomial, + ClassUnknown, + ConstantCost, + ExponentialCost, + FactorialCost, + LinearCost, + LogCost, + PolynomialCost, + PolyLogCost, + ProductCost, + SumCost, + UnknownCost, + PolyLogExponent, + PolynomialDegree, + SymbolicCost, + SizeVariable, + asymptotic_class_dominates, + asymptotic_class_of_cost, + cost_lens +} +import v4.std.collection { List } +import v4.std.diagnostic { + AmbiguousIntent, + Diagnostic, + Textual, + WholeFile, + Unavailable +} +import v4.std.node { Node, Symbol } +import v4.std.refinement { Refined, refined_base } +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 = Refined> + +// 🟢 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 +// projected user-facing bound without adding another cost mechanism. +// 🟢 coproduct dissolution — T-12-COMPLEXITY-PROJECTION. +type ComplexityBound + = ConstantComplexity + | SizedComplexity { class: SizeIndexedAsymptoticClass, variables: ComplexityVariables } + | UnknownComplexity { diagnostic: Diagnostic } + +fn unknown_complexity() -> ComplexityBound { + UnknownComplexity { diagnostic: complexity_projection_diagnostic() } +} + +fn single_complexity_variable(size_var: SizeVariable) -> ComplexityVariables { + Refined { base: [size_var] } +} + +fn composite_complexity_variables( + lhs: ComplexityVariables, + rhs: ComplexityVariables +) -> ComplexityVariables { + Refined { base: concat(refined_base(r: lhs), refined_base(r: rhs)) } +} + +fn sized_complexity( + class: SizeIndexedAsymptoticClass, + variables: ComplexityVariables +) -> ComplexityBound { + SizedComplexity { class: class, variables: variables } +} + +fn linear_complexity(size_var: SizeVariable) -> ComplexityBound { + sized_complexity( + class: SizedLinearClass, + variables: single_complexity_variable(size_var: size_var) + ) +} + +fn polynomial_complexity( + size_var: SizeVariable, + degree: PolynomialDegree +) -> ComplexityBound { + sized_complexity( + class: SizedPolynomialClass { degree: degree }, + variables: single_complexity_variable(size_var: size_var) + ) +} + +fn multidimensional_polynomial_complexity( + variables: ComplexityVariables, + degree: PolynomialDegree +) -> ComplexityBound { + sized_complexity(class: SizedPolynomialClass { degree: degree }, variables: variables) +} + +fn unknown_complexity_with_diagnostic(diagnostic: Diagnostic) -> ComplexityBound { + UnknownComplexity { diagnostic: diagnostic } +} + +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: 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 } => + combine_complexity_variables( + lhs: complexity_variables_from_cost(cost: lhs), + rhs: complexity_variables_from_cost(cost: rhs) + ) + ProductCost { lhs: lhs, rhs: 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() } + } +} + +fn complexity_bound_from_class( + class: AsymptoticClass, + variables: Witness +) -> ComplexityBound { + match class { + ClassConstant => ConstantComplexity + ClassUnknown => unknown_complexity() + _ => + match variables { + 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), + variables: complexity_variables_from_cost(cost: cost) + ) +} + +fn complexity_projection_diagnostic() -> Diagnostic { + Diagnostic { + reason: complexity_reason_unmodeled_cost_projection, + at: Textual { file: complexity_projection_virtual_file, extent: WholeFile }, + correction: Unavailable { reason: AmbiguousIntent } + } +} + +fn asymptotic_projection(cost_witness: Witness) -> Witness { + match cost_witness { + Holds { value: cost } => + Holds { value: complexity_bound_from_cost(cost: cost) } + Violates { diagnostic: d } => + Violates { diagnostic: d } + } +} + +fn complexity_lens(n: Node) -> Witness { + asymptotic_projection(cost_witness: cost_lens(n: n)) +} + +fn complexity_bound_class(b: ComplexityBound) -> AsymptoticClass { + match b { + ConstantComplexity => ClassConstant + 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 { + 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 f9a5e44de92..c63171569c3 100644 --- a/src/v4/lens/cost.dag +++ b/src/v4/lens/cost.dag @@ -34,15 +34,624 @@ module v4.lens.cost import v4.extdeps.languages.llvm_ir { LlvmInstruction } +import v4.std.algebra { + BoundedLattice, + Equal, + Greater, + Lattice, + Magma, + Monoid, + Semigroup +} +import v4.std.cardinality { NonZeroNat } +import v4.std.diagnostic { + AmbiguousIntent, + Diagnostic, + 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.nat { Nat, Succ, Zero, nat_add, nat_compare } +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`. +// 🟢 coproduct dissolution — T-12-COST-CARRIER. +type SizeVariable { + source: Node +} + +type PolynomialDegree { + predecessor_after_linear: NonZeroNat +} +type PolyLogExponent { + predecessor_after_log: NonZeroNat +} + +type ExponentialBase { + predecessor_after_one: NonZeroNat +} -// RULING-1: needs-more-work — minimal carrier until T-12 cost lattice/fold lands. -// 🟡 coproduct dissolution — T22-INFER-SYMBOLIC-COST-BRIDGE. +// 🟢 coproduct dissolution — T-12-COST-CARRIER. type SymbolicCost -// 🟡 coproduct dissolution — T22-INFER-SYMBOLIC-COST-BRIDGE - = ConstantCost { value: Int } + = ConstantCost { value: Nat } + | LinearCost { variable: SizeVariable } + | LogCost { variable: SizeVariable } + | 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 } | UnknownCost { reason: String } +// 🟢 coproduct dissolution — T-12-COMPLEXITY-PROJECTION. +type AsymptoticClass + = ClassConstant + | ClassLog + | ClassLinear + | ClassLinearithmic + | ClassPolynomial { degree: PolynomialDegree } + | ClassPolyLog { exponent: PolyLogExponent } + | 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: Nat) -> SymbolicCost { + ConstantCost { value: value } +} + +fn zero_cost() -> SymbolicCost { + constant_cost(value: Zero) +} + +fn unit_cost() -> SymbolicCost { + constant_cost(value: Succ { prev: Zero }) +} + +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 } +} + +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, n: Node) -> SymbolicCost { + match c { + Atom { identity: _ } => zero_cost() + Conj => zero_cost() + Disj => zero_cost() + Arrow => unit_cost() + Cardinality => linear_in_node(n: n) + Instantiation => unit_cost() + } +} + +fn base_cost_for_behavior(b: Behavior) -> SymbolicCost { + match b { + Value => zero_cost() + Transform => unit_cost() + Branch => unit_cost() + Loop => unknown_cost(reason: "T-9-not-landed: loop bound") + 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, n: n) + } + 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: Zero } => b + UnknownCost { reason: _ } => a + _ => + match b { + ConstantCost { value: Zero } => a + UnknownCost { reason: _ } => b + _ => SumCost { lhs: a, rhs: b } + } + } +} + +fn symbolic_product(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { + match a { + ConstantCost { value: Zero } => zero_cost() + ConstantCost { value: Succ { prev: Zero } } => b + UnknownCost { reason: _ } => a + _ => + match b { + ConstantCost { value: Zero } => zero_cost() + ConstantCost { value: Succ { prev: Zero } } => a + UnknownCost { reason: _ } => b + _ => ProductCost { lhs: a, rhs: b } + } + } +} + +fn symbolic_max(a: SymbolicCost, b: SymbolicCost) -> SymbolicCost { + match a { + ConstantCost { value: Zero } => b + _ => + match b { + ConstantCost { value: Zero } => a + _ => + if a == b { + a + } else { + 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 + } + } + } + } +} + +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 nat_compare(a: a, b: b) { + Greater => true + Equal => true + _ => false + } +} + +fn add_polynomial_degree(a: PolynomialDegree, b: PolynomialDegree) -> PolynomialDegree { + PolynomialDegree { + predecessor_after_linear: NonZeroNat { + prev: Succ { + prev: Succ { + prev: nat_add(a: a.predecessor_after_linear.prev, b: b.predecessor_after_linear.prev) + } + } + } + } +} + +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 add_polylog_exponent(a: PolyLogExponent, b: PolyLogExponent) -> PolyLogExponent { + PolyLogExponent { + predecessor_after_log: NonZeroNat { + prev: Succ { + prev: Succ { + prev: nat_add(a: a.predecessor_after_log.prev, b: b.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 + } +} + +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_fold_state(n: Node) -> SymbolicCostFoldState { + fold_node(n: n, algebra: symbolic_cost_fold()) +} + +fn symbolic_cost_of_node(n: Node) -> SymbolicCost { + symbolic_cost_fold_state(n: n).cost +} + +fn cost_lens(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 } => polynomial_degree_dominates(a: ad, b: 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 } => polylog_exponent_dominates(a: ae, b: 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 { + match a { + ConstantCost { value: av } => + match b { + ConstantCost { value: bv } => nat_dominates(a: av, b: bv) + _ => false + } + _ => + match b { + ConstantCost { value: Zero } => 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 { + match a { + ClassConstant => b + ClassUnknown => ClassUnknown + ClassFactorial => ClassFactorial + ClassExponential => match b { + ClassFactorial => ClassFactorial + ClassUnknown => ClassUnknown + _ => ClassExponential + } + ClassPolynomial { degree: ad } => + match b { + ClassConstant => a + ClassLog => ClassUnknown + ClassLinear => ClassPolynomial { degree: increment_polynomial_degree(d: ad) } + ClassLinearithmic => ClassUnknown + ClassPolynomial { degree: bd } => + ClassPolynomial { degree: add_polynomial_degree(a: ad, b: bd) } + ClassPolyLog { exponent: _ } => ClassUnknown + ClassExponential => ClassExponential + ClassFactorial => ClassFactorial + ClassUnknown => ClassUnknown + } + ClassLinear => + match b { + ClassConstant => ClassLinear + ClassLog => ClassLinearithmic + ClassLinear => ClassPolynomial { degree: polynomial_degree_two() } + ClassLinearithmic => ClassUnknown + ClassPolynomial { degree: bd } => + ClassPolynomial { degree: increment_polynomial_degree(d: bd) } + ClassPolyLog { exponent: _ } => ClassUnknown + ClassExponential => ClassExponential + ClassFactorial => ClassFactorial + ClassUnknown => ClassUnknown + } + ClassLinearithmic => + match b { + ClassConstant => ClassLinearithmic + ClassLog => ClassUnknown + ClassLinear => ClassUnknown + ClassLinearithmic => ClassUnknown + ClassPolynomial { degree: _ } => ClassUnknown + ClassPolyLog { exponent: _ } => ClassUnknown + ClassExponential => ClassExponential + ClassFactorial => ClassFactorial + ClassUnknown => ClassUnknown + } + ClassPolyLog { exponent: e } => + match b { + ClassConstant => a + 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 + } + ClassLog => + match b { + ClassConstant => ClassLog + ClassLog => ClassPolyLog { exponent: polylog_exponent_two() } + ClassPolyLog { exponent: e } => + ClassPolyLog { exponent: increment_polylog_exponent(e: e) } + ClassLinear => ClassLinearithmic + ClassLinearithmic => ClassUnknown + ClassPolynomial { degree: _ } => ClassUnknown + ClassExponential => ClassExponential + ClassFactorial => ClassFactorial + ClassUnknown => ClassUnknown + } + } +} + +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 { 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 + } +} + +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: Zero } +} + +data symbolic_cost_lattice: BoundedLattice = BoundedLattice { + lattice: Lattice { + meet: cost_lattice_meet, + join: cost_lattice_join + }, + top: UnknownCost { reason: cost_lattice_top_reason }, + bottom: ConstantCost { value: Zero } +} + +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 { 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 + ) +} 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..79bd03c0cda --- /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, complexity_lens, linear_complexity } +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_complexity(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..d9bc0254263 --- /dev/null +++ b/src/v4/test/claim/lens_complexity/nested.dag @@ -0,0 +1,100 @@ +// 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, + composite_complexity_variables, + complexity_lens, + multidimensional_polynomial_complexity, + single_complexity_variable +} +import v4.lens.cost { + LinearCost, + PolynomialDegree, + ProductCost, + SizeVariable, + SymbolicCost, + cost_lens +} +import v4.std.cardinality { NonZeroNat } +import v4.std.diagnostic { + AmbiguousIntent, + Diagnostic, + Textual, + Unavailable, + WholeFile +} +import v4.std.node { + Cardinality, + ComputationNode, + Edge, + Node, + Positional, + Symbol, + Transform, + TypeNode +} +import v4.std.nat { Zero } +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: 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 { + 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..938a8164bbd --- /dev/null +++ b/src/v4/test/claim/lens_complexity/while_external_condition.dag @@ -0,0 +1,66 @@ +// 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, + complexity_lens, + unknown_complexity_with_diagnostic +} +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_complexity_with_diagnostic( + 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) +}