leanprover / leanprover/lean4

RFC: Add Array.mapA

Open
#13,714 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

P-low RFC
Dominant language
Lean
Stars
9.2k
Forks
990
Avg merge
1d 17h
Merged PRs (30d)
175

Description

I have noticed List has mapA but Array doesnt. Same in batteries - List has traverse but Array doesnt.

I propose adding the following implementation:

def mapA {m : Type u → Type v} [Applicative m] {α : Type w} {β : Type u} (f : α → m β) (as : Array α) : m (Array β) :=
  as.foldl (fun macc x => (fun acc y => acc.push y) <$> macc <*> f x) (pure (Array.emptyWithCapacity as.size))

I suppose it was not added bc of performance worries. But maybe can use @[csimp] to optimize for known monads? Or is there other way?

  • User Experience: This provides a standard way to map Applicative effects over Arrays, consistent with the existing List API.
  • Beneficiaries: Users working with Array who currently have to convert to List and back to perform effectful operations.
  • Maintainability: This reduces boilerplate for users and aligns Array with other traversable collections in the standard library.
Community Feedback

https://leanprover.zulipchat.com/#narrow/channel/270676-lean4/topic/RFC.3A.20Add.20Array.2EmapA

Impact

This would allow for more ergonomic and performant code when working with Array and Applicative functors. If others benefit from the changes in this proposal being added, please ask them to add 👍 to it.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the linked List.mapA implementation in Init/Data/List/Control.lean and the Array/List traversal context in Batteries/Data/List/Basic.lean. Review the linked Zulip discussion and performance question before proposing changes. Done means the API and optimization approach are agreed and the resulting Array.mapA work is accepted.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.