← Back to Pull requests

feat(TimeM): relate timed costs to big-O #1083

Open
cdbattags opened this pull request with 35 lines diff across 2 files
View on GitHub
cdbattags cdbattags commented on Oct 05, 2026 20:51 UTC
Author

Mathlib's Asymptotics.IsBigO and IsTheta compare two functions along a filter. TimeM.time is one scalar, so a size-indexed computation does not fit that API by itself.

This adds TimeM.isBigO and TimeM.isTheta in Cslib/Algorithms/Lean/TimeM/Asymptotics.lean. They coerce (cost n).time to ℝ and ask Mathlib whether that function of n is big-O or big-Theta of g at Filter.atTop. The module is separate from TimeM.lean so existing TimeM clients do not import analysis.

lake build Cslib.Algorithms.Lean.TimeM.Asymptotics succeeded, and lake exe mk_all left the new import in Cslib.lean.

Drafted with Grok 4.7, the 500k-context high-effort setting, in Cursor. I used it to read TimeM and Mathlib's Asymptotics.IsBigO / IsTheta and to write these two definitions. This repository has no LLM-generated label; the disclosure above is the step CONTRIBUTING.md asks for, via the Mathlib policy.

Diff: 35 lines (+35 / -0) across 2 files Reviewer: arademaker Review duration: 76352m
All Vouch review confidence checks passed
Residual risk 0.05 = Change Risk (0.38) × (1 − Review Confidence (0.88)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.38 · 35 lines diff across 2 files
0.38
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.91 · 4 substantive comment(s)
0.91
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
87%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.214
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
7 items
arademaker arademaker requested changes on Oct 15, 2026 16:41 UTC
Changes Requested Reviewer

My comments assume this PR was motivated by cslib-community/fad#83. I see two problems:

Problem 1

[Coe T ℝ] doesn't work with TimeM Nat α. So far, FAD has used TimeM Nat all over the place, but Mathlib has no instance of Coe ℕ ℝ. The coercion ℕ → ℝ is given by NatCast. So the proposed definition does not work for any definition in FAD.

Two possible solutions:

  • specialize the definition to Nat and use NatCast. This would be easier, but I don't think it is the right direction from a library point of view.

  • demand the conversion function as an extra parameter (toReal : T → ℝ).
  • Problem 2

    You are fixing the filter atTop and the index Nat.

    At cslib-community/fad#83 the cost function is given by a pair ℕ × ℕ, and so we would need to pass to Mathlib IsBigO the atTop ×ˢ atTop filter. But your definition is fixing IsBigO atTop .... In that way, this PR is not flexible enough. One possible suggestion would be to generalize your definitions to something like

    def IsTheta {ι α T : Type*} (toReal : T → ℝ) (l : Filter ι) 
       (cost : ι → TimeM T α) (g : ι → ℝ) : Prop :=
      Asymptotics.IsTheta l (fun i => toReal (cost i).time) g
    

    For the case of one variable, we could still use atTop.

    arademaker arademaker commented on code on Oct 15, 2026 16:43 UTC
    Code Review
    Cslib/Algorithms/Lean/TimeM/Asymptotics.lean:1
    @@ -0,0 +1,34 @@
    +/-
    +Copyright (c) 2026 Christian Battaglia. All rights reserved.
    +Released under Apache 2.0 license as described in the file LICENSE.
    +Authors: Christian Battaglia
    +-/
    +module
    +
    +public import Cslib.Init
    +public import Cslib.Algorithms.Lean.TimeM
    +public import Mathlib.Analysis.Asymptotics.Defs
    +public import Mathlib.Order.Filter.AtTopBot.Defs
    +
    +/-!
    +# Asymptotic bounds for `TimeM` costs
    +
    +Mathlib's `Asymptotics.IsBigO` and `Asymptotics.IsTheta` compare two functions along a filter.
    +`TimeM.time` is a single cost. These definitions lift a size-indexed computation to that API.
    +-/
    +
    +@[expose] public section
    +
    +open Asymptotics Filter
    +
    +namespace Cslib.Algorithms.Lean.TimeM
    +
    +/-- The cost of `cost`, as a function of input size, is big-O of `g` at infinity. -/
    +def isBigO {α T : Type*} [Coe T ℝ] (cost : ℕ → TimeM T α) (g : ℕ → ℝ) : Prop :=

    Coe T Real if T gets Nat will not work. The IsBigO atTop is too specific. The name isBigO should be IsBigO by our conventions since it returns Prop.

    arademaker arademaker commented on code on Oct 15, 2026 16:43 UTC
    Code Review
    Cslib/Algorithms/Lean/TimeM/Asymptotics.lean:1
    @@ -0,0 +1,34 @@
    +/-
    +Copyright (c) 2026 Christian Battaglia. All rights reserved.
    +Released under Apache 2.0 license as described in the file LICENSE.
    +Authors: Christian Battaglia
    +-/
    +module
    +
    +public import Cslib.Init
    +public import Cslib.Algorithms.Lean.TimeM
    +public import Mathlib.Analysis.Asymptotics.Defs
    +public import Mathlib.Order.Filter.AtTopBot.Defs
    +
    +/-!
    +# Asymptotic bounds for `TimeM` costs
    +
    +Mathlib's `Asymptotics.IsBigO` and `Asymptotics.IsTheta` compare two functions along a filter.
    +`TimeM.time` is a single cost. These definitions lift a size-indexed computation to that API.
    +-/
    +
    +@[expose] public section
    +
    +open Asymptotics Filter
    +
    +namespace Cslib.Algorithms.Lean.TimeM
    +
    +/-- The cost of `cost`, as a function of input size, is big-O of `g` at infinity. -/
    +def isBigO {α T : Type*} [Coe T ℝ] (cost : ℕ → TimeM T α) (g : ℕ → ℝ) : Prop :=
    +  IsBigO atTop (fun n => ((cost n).time : ℝ)) g
    +
    +/-- The cost of `cost`, as a function of input size, is big-Theta of `g` at infinity. -/
    +def isTheta {α T : Type*} [Coe T ℝ] (cost : ℕ → TimeM T α) (g : ℕ → ℝ) : Prop :=

    same as above.

    arademaker arademaker requested changes on Oct 15, 2026 16:45 UTC
    Changes Requested Reviewer

    Requested changes.

    arademaker arademaker commented on code on Oct 15, 2026 16:54 UTC
    Code Review
    Cslib/Algorithms/Lean/TimeM/Asymptotics.lean:1
    @@ -0,0 +1,34 @@
    +/-
    +Copyright (c) 2026 Christian Battaglia. All rights reserved.
    +Released under Apache 2.0 license as described in the file LICENSE.
    +Authors: Christian Battaglia
    +-/
    +module
    +
    +public import Cslib.Init
    +public import Cslib.Algorithms.Lean.TimeM
    +public import Mathlib.Analysis.Asymptotics.Defs
    +public import Mathlib.Order.Filter.AtTopBot.Defs
    +
    +/-!
    +# Asymptotic bounds for `TimeM` costs
    +
    +Mathlib's `Asymptotics.IsBigO` and `Asymptotics.IsTheta` compare two functions along a filter.
    +`TimeM.time` is a single cost. These definitions lift a size-indexed computation to that API.
    +-/
    +
    +@[expose] public section
    +
    +open Asymptotics Filter
    +
    +namespace Cslib.Algorithms.Lean.TimeM
    +
    +/-- The cost of `cost`, as a function of input size, is big-O of `g` at infinity. -/
    +def isBigO {α T : Type*} [Coe T ℝ] (cost : ℕ → TimeM T α) (g : ℕ → ℝ) : Prop :=

    Inspecting this issue more closely, the only common cost type for which Coe T ℝ resolves seems to be ℝ ≥ 0. Even T := ℝ fails, since there is no reflexive Coe instance.

    arademaker arademaker commented on Oct 16, 2026 00:48 UTC
    Contributor

    It may be worth to explore the connection of this PR with #468

    Reviewers
    Assignees
    cdbattags cdbattags
    Labels
    risk: low confidence: 87.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 37.9% (MEDIUM) · Higher = larger blast radius
    37.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 87.8% · Lower = rushed or shallow review
    87.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.6% · Safe to merge
    4.6%