Skip to content

feat: binary recursive implementation of List.mapA - #3877

Closed
digama0 wants to merge 1 commit into
leanprover:masterfrom
digama0:mapA_brec
Closed

feat: binary recursive implementation of List.mapA#3877
digama0 wants to merge 1 commit into
leanprover:masterfrom
digama0:mapA_brec

Conversation

@digama0

@digama0 digama0 commented Apr 11, 2024

Copy link
Copy Markdown
Collaborator

Inspired by #3867 (comment) . After playing with this function a bit more, I was able to confirm that it's not really possible to implement it tail recursively in most monads (note that tail-recursiveness depends on the monad itself), because seq is not tail recursive in most monads and you have to stack up at least n-1 of them to reduce a list of length n. However, we can do the next best thing which is to use a balanced tree of seq applications. I have confirmed that this will evaluate

#eval List.mapA (m := StateT Nat Id) pure (List.range 10000) |>.run 1

without stack overflow, unlike the original implementation, but it still deserves a !bench because the binary reduction has overhead and it may be worthwhile to switch over to naive recursion below some threshold.

@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Apr 11, 2024
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Apr 11, 2024
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Apr 11, 2024
@ghost ghost added the builds-mathlib CI has verified that Mathlib builds against this PR label Apr 11, 2024
@ghost

ghost commented Apr 11, 2024

Copy link
Copy Markdown

Mathlib CI status (docs):

ghost pushed a commit to leanprover-community/batteries that referenced this pull request Apr 11, 2024
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Apr 11, 2024
@david-christiansen

Copy link
Copy Markdown
Contributor

!bench

@leanprover-bot

Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit f405eb4.
There were no significant changes against commit d988849.

@digama0

digama0 commented Apr 12, 2024

Copy link
Copy Markdown
Collaborator Author

(note: the benchmark is useless because List.mapA is unused in core)

@kim-em

kim-em commented Apr 22, 2024

Copy link
Copy Markdown
Collaborator

Could you add a doc-string explaining at least that the complicated implementation is for performance, and ideally explaining why tail-recursiveness is impossible and that this is "the next best thing"?

@kim-em kim-em added the awaiting-author Waiting for PR author to address issues label Apr 22, 2024
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Apr 22, 2024
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Apr 22, 2024
@github-actions github-actions Bot added the stale label May 23, 2024
@kim-em
kim-em self-requested a review as a code owner May 29, 2024 16:23
@github-actions github-actions Bot removed the stale label May 30, 2024
@github-actions github-actions Bot added stale and removed stale labels Jun 29, 2024
@github-actions github-actions Bot added the stale label Aug 20, 2024
@github-actions github-actions Bot removed the stale label Sep 6, 2024
@github-actions github-actions Bot added the stale label Oct 30, 2024
@github-actions github-actions Bot removed the stale label Jun 6, 2025
@github-actions github-actions Bot added the stale label Jul 27, 2025
@github-actions github-actions Bot removed the stale label Aug 27, 2025
@github-actions github-actions Bot added the stale label Sep 27, 2025
@srghma

srghma commented May 11, 2026

Copy link
Copy Markdown

@digama0

not really possible to implement it tail recursively in most monads

what are these monads? maybe for these monads possible to create

class CanImplementTailRecursivenessFor m where
  proof_x : ...

and then write in docstring for List.mapA - "hey, if You want List.mapA to work faster for Your monad then proove/add class CanImplementTailRecursivenessFor for it"

and if monad has CanImplementTailRecursivenessFor then we enable optimization using @[csimp]

@srghma srghma mentioned this pull request May 11, 2026
@github-actions github-actions Bot removed the stale label May 12, 2026
@srghma srghma mentioned this pull request May 12, 2026
@github-actions github-actions Bot added the stale label Jun 11, 2026
@kim-em

kim-em commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

This hasn't been touched in over a year, and has a merge conflict. I'm closing now; please re-open if you think appropriate.

@kim-em kim-em closed this Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-author Waiting for PR author to address issues builds-mathlib CI has verified that Mathlib builds against this PR stale toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants