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 [Dan08] for the discussion.
Lifts a pure value into a TimeM computation with zero time cost.
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.
Creates a TimeM computation with a specified value and time cost.
The time cost defaults to 1 if not provided.
Equations
- Cslib.Algorithms.Lean.TimeM.tick a c = { ret := a, time := c }
Instances For
Notation for tick with explicit time cost: ✓ a, c
Equations
- One or more equations did not get rendered due to their size.
Instances For
Notation for tick with default time cost of 1: ✓ a
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.
Instances For
A unit computation with time cost 1.