Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions src/v4/lens/complexity.dag
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,156 @@

module v4.lens.complexity


import v4.lens.cost {
AsymptoticClass,
ClassConstant,
ClassUnknown,
ClassLinear,
ClassPolynomial,
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.diagnostic {
AmbiguousIntent,
Diagnostic,
Textual,
WholeFile,
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
data complexity_projection_virtual_file: Symbol = complexity_projection_virtual_file

// ComplexityBound is the asymptotic projection of `SymbolicCost`. The
Comment thread
briansrls marked this conversation as resolved.
Resolved
// `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 }
| UnknownComplexity { diagnostic: Diagnostic }

fn unknown_complexity() -> ComplexityBound {
UnknownComplexity { diagnostic: complexity_projection_diagnostic() }
Comment thread
briansrls marked this conversation as resolved.
Resolved
}

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<SizeVariable> {
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 }
SumCost { lhs: lhs, rhs: 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 } =>
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 complexity_bound_from_class(
class: AsymptoticClass,
size_var: Witness<SizeVariable>
) -> ComplexityBound {
match class {
ClassConstant => ConstantComplexity
ClassUnknown => unknown_complexity()
_ =>
match size_var {
Holds { value: v } => SizedComplexity { class: class, size_var: v }
Violates { diagnostic: _ } => 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,
at: Textual { file: complexity_projection_virtual_file, extent: WholeFile },
correction: Unavailable { reason: AmbiguousIntent }
}
}

fn asymptotic_projection(cost_witness: Witness<SymbolicCost>) -> Witness<ComplexityBound> {
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<ComplexityBound> {
asymptotic_projection(cost_witness: cost_lens(n: n))
}

fn complexity_bound_class(b: ComplexityBound) -> AsymptoticClass {
Comment thread
briansrls marked this conversation as resolved.
Resolved
match b {
ConstantComplexity => ClassConstant
SizedComplexity { class: c, size_var: _ } => c
UnknownComplexity { diagnostic: _ } => ClassUnknown
}
}

fn complexity_bound_dominates(a: ComplexityBound, b: ComplexityBound) -> Bool {
asymptotic_class_dominates(
a: complexity_bound_class(b: a),
b: complexity_bound_class(b: b)
)
}
Loading
Loading