TimeM: Time Complexity Monad #
TimeM α represents a computation that produces a value of type α and tracks its time cost.
Design Principles #
- Pure inputs, timed outputs: Functions take plain values and return
TimeMresults - Time annotations are trusted: The
timefield is NOT verified against actual cost. You must manually ensure annotations match the algorithm's complexity in your cost model. - Separation of concerns: Prove correctness properties on
.ret, prove complexity on.time
Cost Model #
Document your cost model explicitly Decide and be consistent about:
- What costs 1 unit? (comparison, arithmetic operation, etc.)
- What is free? (variable lookup, pattern matching, etc.)
- Recursive calls: Do you charge for the call itself?
Notation #
✓: A tick of time, seetick.⟪tm⟫: Extract the pure value from aTimeMcomputation (notation fortm.ret)
References #
See [Danielsson2008] for the discussion.
Lifts a pure value into a TimeM computation with zero time cost.
Prefer to use pure instead of TimeM.pure.
Equations
- Cslib.Algorithms.Lean.TimeM.pure a = { ret := a, time := 0 }
Instances For
Equations
- One or more equations did not get rendered due to their size.
@[simp]
@[simp]
@[simp]
@[simp]
@[simp]
@[simp]
@[simp]
@[simp]
@[simp]
Creates a TimeM computation with a time cost.
The time cost defaults to 1 if not provided.
Equations
- Cslib.Algorithms.Lean.TimeM.tick c = { ret := PUnit.unit, time := c }
Instances For
✓[c] x adds c ticks, then executes x.
Equations
- One or more equations did not get rendered due to their size.
Instances For
✓ x is a shorthand for ✓[1] x, which adds one tick and executes x.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Notation for extracting the return value from a TimeM computation: ⟪tm⟫
Equations
- One or more equations did not get rendered due to their size.