Skip to content

Feat : Query complexity model for algorithms#275

Draft
Shreyas4991 wants to merge 78 commits intoleanprover:mainfrom
Shreyas4991:query-complexity-freeM-shreyas
Draft

Feat : Query complexity model for algorithms#275
Shreyas4991 wants to merge 78 commits intoleanprover:mainfrom
Shreyas4991:query-complexity-freeM-shreyas

Conversation

@Shreyas4991
Copy link
Contributor

@Shreyas4991 Shreyas4991 commented Jan 21, 2026

Generalizing @tannerduve 's query complexity model. This model of algorithmic complexity provides a lightweight approach to complexity verification of algorithms, similar to #165

However it offers several improvements over #165 :

  1. No explicit ticks needed, so the chances of making mistakes like forgetting to count specific calls is removed.
  2. The operations which are counted are made explicit as an inductive type and a model of this type.
  3. The model of a query type accepts custom cost structures so long as you can define addition and a 0 operation on them (basically additive monoids suffice).
  4. With a notion of reductions between models, this approach is more modular for the algorithm specifier, allowing high level specs of operations which can be translated into simpler query models later.

Update to the below drawback : The model requires users to choose a cost for each pure operation upfront by a typeclass instance for a given type of Cost. This is a departure from TimeMs model. But with custom Cost types with a specific field for pure, these costs can be separated from that of calls to queries.

One drawback : It is still possible to sneak in free operations inside pure. However this is unavoidable in any lightweight monadic approach. A deeply embedded DSL is the only foolproof way to avoid this. Nevertheless this approach removes annotation and review burden and ensures that any actual call to a query will be counted. Thus it is easy to notice when a monadic operation is not being called.

Zulip Discussion Thread: https://leanprover.zulipchat.com/#narrow/channel/513188-CSLib/topic/Query.20model.20for.20Algorithms.20complexity.20.3A.20updates/near/569606456

@Shreyas4991 Shreyas4991 changed the title Query complexity free m shreyas Query complexity formalisation for describing algorithmic models Jan 22, 2026
Comment on lines +60 to +65
/-- Equivalence between `TimeM α` and `WriterT Nat Id α` as plain types. -/
def equivWriter (α : Type) : TimeM α ≃ WriterNat α where
toFun m := (m.ret, m.time)
invFun w := ⟨w.1, w.2⟩
left_inv m := by cases m; rfl
right_inv w := by cases w; rfl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is misleading; sure, the types have the same cardinality, but this isomorphism is not canonical; it does not preserve bind or tick.

Copy link
Contributor Author

@Shreyas4991 Shreyas4991 Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comes from upstream. I am not touching TimeM. I am of the opinion that we can delete the whole file. This model already subsumes everything TimeM does. And TimeM is too thin a wrapper to serve any purpose.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, does "upstream" mean #201? I don't believe this is an existing definition in the main branch.

Copy link
Contributor Author

@Shreyas4991 Shreyas4991 Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible. I don't recall touching TimeM at all. Tbh if we wanted to put this equivalence into CSLib at all, I would just change TimeM to an abbrev.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you merge master and delete this file? master already defines TimeM in a different file, let's not repeat it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do. I think this is a relic of #201

Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
@Shreyas4991
Copy link
Contributor Author

I plan to remove the circuits stuff and add it to a separate PR.

(TimeM.bind m f).ret = (f m.ret).ret := rfl

-- this allow us to simplify the chain of compositions
attribute [simp] Bind.bind Pure.pure TimeM.pure
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is cursed; I've removed it in #321

@Shreyas4991 Shreyas4991 force-pushed the query-complexity-freeM-shreyas branch from c965ec7 to 0a8316d Compare February 14, 2026 18:54
@eric-wieser
Copy link
Contributor

Please merge master.

@Shreyas4991
Copy link
Contributor Author

Please merge master.

Just did that a few hours ago

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file already exists with a different name, please fix the merge conflict by removing one of the duplicates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Comments