Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: elaborate theorem bodies in parallel #5864

Draft
wants to merge 292 commits into
base: async-proofs-base
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
292 commits
Select commit Hold shift + click to select a range
348b652
update documentation
Kha Aug 27, 2024
fbc7e3e
make setBase private again
Kha Aug 27, 2024
bf2da5e
Merge branch 'kernel-env' into async-proofs
Kha Aug 27, 2024
45ff446
correct prefix restriction, Prelude passes
Kha Aug 27, 2024
efc0772
progess
Kha Aug 30, 2024
b5b9092
traces
Kha Sep 1, 2024
da63230
more
Kha Sep 3, 2024
1c33ad1
feat: IO.getTID
Kha Sep 10, 2024
651f043
feat: thread support for trace.profiler.output
Kha Sep 10, 2024
a4f5866
feat: trace.profiler: unconditionally show times of trace classes ena…
Kha Sep 10, 2024
cdd3265
profile async proofs
Kha Sep 10, 2024
556af07
Elab.block
Kha Sep 11, 2024
5ad9bb1
Merge remote-tracking branch 'upstream/master' into async-proofs
Kha Sep 11, 2024
e90f4de
disable Lake
Kha Sep 12, 2024
a5bcd27
Elab.block cont
Kha Sep 12, 2024
ec67cdd
Avoid some getConstInfos
Kha Sep 12, 2024
c658d6b
AsyncConstantInfo
Kha Sep 12, 2024
367540a
fix variable inclusion in async theorems
Kha Sep 13, 2024
5a52c3a
Merge branch 'upstream/master' into async-proofs
Kha Sep 15, 2024
47dd9d3
fix
Kha Sep 15, 2024
459d0f7
No async processing with late-triggering attrs
Kha Sep 17, 2024
af65397
Disable broken linter for now
Kha Sep 18, 2024
5825496
Global theorems
Kha Sep 18, 2024
d8bc2cb
fixes
Kha Sep 18, 2024
1c829ac
more fixes
Kha Sep 18, 2024
ac12529
more global theorems
Kha Sep 19, 2024
22d939c
even more global theorems
Kha Sep 20, 2024
cad2ff1
this way around is faster
Kha Sep 20, 2024
fc9b8a8
perf: optimize unfolding constant lookup
Kha Sep 21, 2024
17dce45
that's not unfoldable
Kha Sep 23, 2024
a1b2182
optimize Environment.findX
Kha Sep 23, 2024
fee8e84
dbg_trace Environment.setState
Kha Sep 23, 2024
b0c61fe
chore: remove `matcherExt` cache
Kha Oct 1, 2024
ab9d861
revamp async env API
Kha Sep 24, 2024
4aa1d1f
Merge branch 'upstream/nightly-with-mathlib' into async-proofs
Kha Oct 2, 2024
8615c7a
remove dbgStackTrace with panic
Kha Oct 4, 2024
bb9813d
merge push-xwrmupowysmt
Kha Oct 27, 2024
d095f81
async matcherinfo
Kha Oct 7, 2024
1afe0a7
async exportEntriesFn
Kha Oct 7, 2024
4ea6136
postponed compilation
Kha Oct 10, 2024
94d0973
fix async environment access
Kha Oct 11, 2024
4e17aa1
support asyncly added axioms
Kha Oct 11, 2024
9f79c9f
make all SimplePersistentEnvExts async-aware
Kha Oct 11, 2024
5fd0b75
fixes
Kha Oct 11, 2024
29d5c91
fix duplicate exports
Kha Oct 11, 2024
b2a37b2
enable realizations
Kha Oct 11, 2024
995555d
chore: remove dead code
Kha Oct 11, 2024
1faee26
fix section var filtering with async theorems
Kha Oct 11, 2024
2fa3615
fix findNoAsyncTheorem
Kha Oct 11, 2024
7bcef2f
reserved name action for preprocessed simp theorems
Kha Oct 11, 2024
b431895
fix: add realized consts to env once
Kha Oct 12, 2024
59bd509
allow realizations in aux decls ad-hoc
Kha Oct 12, 2024
45a06e3
consider private names in realization
Kha Oct 12, 2024
fe52f24
further fix realizations
Kha Oct 13, 2024
bfb8948
use nicer `Inhabited Expr` value
Kha Oct 14, 2024
28542b1
fix: realizing in async
Kha Oct 14, 2024
f24a14c
fix: exporting async
Kha Oct 14, 2024
053a863
allow async eqn cache accesses
Kha Oct 14, 2024
ed750dc
fix: async thms turned into axioms
Kha Oct 23, 2024
7447141
force compile in evalExpr
Kha Oct 23, 2024
bce25cb
async reducible
Kha Oct 23, 2024
409d54c
async header elab, avoids deadlock
Kha Oct 23, 2024
bc0cf19
enable realizations for inductive ctors
Kha Oct 24, 2024
3905683
fix async profile
Kha Oct 24, 2024
117ec10
improve async profile
Kha Oct 24, 2024
4111e49
unblock getEqnsFor on theorems
Kha Oct 24, 2024
48e2778
Back out "chore: remove `matcherExt` cache"
Kha Oct 24, 2024
f9be05b
allow `matcherExt` to work locally in async branches
Kha Oct 24, 2024
df39b5c
skip duplicate realized consts, will need to revisit
Kha Oct 25, 2024
c546f28
merge master
Kha Oct 25, 2024
2bcacaa
fix compiler setState
Kha Oct 25, 2024
1629424
fix trace.Elab.snapshotTree
Kha Oct 25, 2024
7374557
fix auto params in async theorems
Kha Oct 25, 2024
fd279b2
fix async env lookup of hygiened names
Kha Oct 25, 2024
6351b67
fix auto implicits in async thms
Kha Oct 25, 2024
ebe4214
fix `open` tactic in async theorems
Kha Oct 25, 2024
bd6a8e2
Environment.dbgFormatAsyncState
Kha Oct 25, 2024
ec159d8
isRflTheorem must realize missing constants
Kha Oct 25, 2024
26fdd06
comment findAsync? a bit
Kha Oct 27, 2024
27f8a83
the crucial comment
Kha Oct 27, 2024
518b9fb
chore: allow specifying LEAN_OPTS in `make` invocation
Kha Oct 27, 2024
371fe0c
fix nested realization
Kha Oct 27, 2024
6ae4c68
fix unresolved promise
Kha Oct 27, 2024
4d634bf
fix native_decide
Kha Oct 27, 2024
5882aaa
fix const realization deadlock
Kha Oct 27, 2024
f810459
remove obsolete test
Kha Oct 27, 2024
91d036e
restrict benchmarks for now due to missing -lLake
Kha Oct 27, 2024
0828604
realized constants should not report extensions
Kha Oct 28, 2024
3319266
fix `addDecl (skipExisting := true)`
Kha Oct 28, 2024
600b587
optimize `setImportedEntries`
Kha Oct 28, 2024
e298702
debugAsSorry doesn't seem necessary anymore
Kha Oct 28, 2024
c11567b
work around `gettid` not being available on glibc 2.27 and macOS
Kha Oct 28, 2024
7c6dfec
reactivate building Lake
Kha Oct 28, 2024
3b44cca
turn kernel blocking wait into continuation
Kha Oct 29, 2024
e00fabc
remove some more blockers
Kha Oct 29, 2024
902a3c4
async theorems with attributes
Kha Oct 29, 2024
f713e9a
remove @[simp] async blocker
Kha Oct 29, 2024
006d140
trace attribute application
Kha Oct 29, 2024
0786bc6
fix async tracing
Kha Nov 5, 2024
b98b409
fix postponing mutual defns
Kha Nov 6, 2024
0a3c666
fix default snapshot desc
Kha Nov 6, 2024
33e051d
delay all defs
Kha Nov 5, 2024
d34f83e
postponedCompiles should be an env ext
Kha Nov 6, 2024
09fb998
fix declarations getting lost in checkSubDecls*
Kha Nov 7, 2024
6f27a6a
runAsyncAsSnapshot desc
Kha Nov 7, 2024
d3bc0f4
chore: introduce UNSAFE_INCREMENTAL
Kha Nov 7, 2024
b32c6d9
fix afterCompilation attrs being applied too early
Kha Nov 7, 2024
f21f94a
cannot use addAsync with postponed decls
Kha Nov 7, 2024
6ca58e3
partial cleanup of postponed decls
Kha Nov 7, 2024
8639449
restrict postponement back to async elab for now
Kha Nov 7, 2024
56ea19c
reactivate a more kosher cmdlineSnapshots
Kha Nov 8, 2024
6b197a0
must force postponed compiles when eagerly compiling
Kha Nov 8, 2024
b98300d
fix toKernelNoAsync with postponed decls
Kha Nov 8, 2024
967861b
deprecate and avoid Environment.addAndCompile
Kha Nov 8, 2024
161fcec
fix runAsyncAsSnapshot
Kha Nov 8, 2024
8a1e3eb
resolveGlobalName trace
Kha Oct 29, 2024
82cf580
avoid more Environment.find?s
Kha Nov 9, 2024
bdd7b82
interpreter error message
Kha Nov 9, 2024
2654804
rebuild: async addDecl
Kha Nov 10, 2024
4340de8
better MutualDef snapshot desc
Kha Nov 10, 2024
a547b66
dubious async compileDecls?
Kha Nov 10, 2024
ec609d0
remove postponement
Kha Nov 12, 2024
0a6a419
fix addDecl
Kha Nov 13, 2024
ae2d8f6
sync all extension changes
Kha Nov 13, 2024
2e4afff
base -> kernel
Kha Nov 13, 2024
1572640
fix setMainModule, needs a more principled solution
Kha Nov 13, 2024
1d17e83
clean up checked env access
Kha Nov 13, 2024
4d8561c
fix updateBase
Kha Nov 13, 2024
979dd2b
remove kernel namespace remnants
Kha Nov 13, 2024
08ccd35
cleanup
Kha Nov 13, 2024
4ce251f
fix checkAndCommitEnv
Kha Nov 13, 2024
4d45908
revert async env ext changes
Kha Nov 13, 2024
d29d6d8
re-enable async proofs
Kha Nov 13, 2024
3dd829d
more environment asynchrony API
Kha Nov 9, 2024
1ddb9f6
re-activate findStateAsync
Kha Nov 9, 2024
3ad871f
make some env exts async
Kha Nov 9, 2024
e9d4080
more async env exts
Kha Nov 13, 2024
99d1794
more async env ext API
Kha Nov 9, 2024
f08b2ed
more async env exts
Kha Nov 9, 2024
a777e7e
match ext async
Kha Nov 9, 2024
d8c5e4c
env fixes
Kha Nov 9, 2024
f35a794
more async lookup
Kha Nov 9, 2024
4be3d83
avoid trivial task in addConstAsync
Kha Nov 14, 2024
cd0ec87
re-enable proof commitConst
Kha Nov 14, 2024
c2e90c2
async eqns
Kha Nov 14, 2024
0822d5a
testfile
Kha Nov 14, 2024
4b17c42
merge master
Kha Nov 14, 2024
8245a20
evalExprCore Elab.block
Kha Nov 14, 2024
e0e33b5
use gettid on Linux after all, via syscall()
Kha Nov 12, 2024
8d15779
refactor: split `Lean.Language.Basic`
Kha Nov 20, 2024
6694250
feat: snapshotTrees
Kha Nov 20, 2024
938e77d
use snapshotTrees
Kha Nov 20, 2024
3bc1f28
fix tests
Kha Nov 21, 2024
3c7dc4b
feat: creation and logging for asynchronous elaboration tasks
Kha Nov 20, 2024
85ca914
merge master
Kha Nov 22, 2024
d8db3c9
merge master
Kha Nov 26, 2024
f05032e
merge master
Kha Nov 29, 2024
2b9c4fa
merge master
Kha Dec 2, 2024
ae86dc9
minimize diff with master
Kha Dec 2, 2024
ef7ebe0
minimize further
Kha Dec 2, 2024
0289e43
respect Elab.async
Kha Dec 2, 2024
23fb458
make `isStructure` async-aware
Kha Nov 14, 2024
bcb0317
feat: parallel progress notifications
Kha Dec 3, 2024
8aa29d7
fix: do not double-report `snapshotTasks` after `wrapAsyncAsSnapshot`
Kha Nov 14, 2024
db22dfd
fix: missing task ranges in trace.Elab.snapshotTrees
Kha Nov 14, 2024
b64d206
async-aware declRangeExt
Kha Nov 14, 2024
da9b442
adjust progress range of MutualDef body task
Kha Nov 14, 2024
24d61ef
merge master
Kha Dec 6, 2024
6665837
feat: verify insertMany method for adding lists to HashMaps (#6211)
monsterkrampe Jan 15, 2025
b3f8fef
fix: improve E-matching pattern selection heuristics (#6653)
leodemoura Jan 15, 2025
54f06cc
feat: better support for partial applications in the E-matching proce…
leodemoura Jan 15, 2025
65175dc
feat: improve`grind` diagnostic information (#6656)
leodemoura Jan 15, 2025
127b3f9
feat: more grind tests (#6650)
kim-em Jan 15, 2025
64cf5e5
feat: improve `grind` search procedure (#6657)
leodemoura Jan 15, 2025
0050e93
refactor: lake: use `StateRefT` for `BuildStore` (#6290)
tydeu Jan 15, 2025
6259b47
feat: improve case-split heuristic used in `grind` (#6658)
leodemoura Jan 16, 2025
af4a7d7
fix: `grind` term preprocessor (#6659)
leodemoura Jan 16, 2025
3a6c5cf
feat: canonicalizer diagnostics (#6662)
leodemoura Jan 16, 2025
80ddbf4
feat: align List/Array/Vector.flatMap (#6660)
kim-em Jan 16, 2025
f4c9934
feat: Vector.getElem_flatMap (#6661)
kim-em Jan 16, 2025
f015271
feat: align List.replicate/Array.mkArray/Vector.mkVector lemmas (#6667)
kim-em Jan 16, 2025
906aa1b
feat: add `Nat.[shiftLeft_or_distrib`, shiftLeft_xor_distrib`, shiftL…
luisacicolini Jan 16, 2025
e42f7d9
feat: equality resolution for `grind` (#6663)
leodemoura Jan 16, 2025
17c0187
fix: add workaround for `MessageData` limitations (#6669)
leodemoura Jan 16, 2025
60142c9
chore: remove unneeded instance (#6671)
hargoniX Jan 16, 2025
35a4da2
feat: add `simp`-like parameters to `grind` (#6675)
leodemoura Jan 17, 2025
7f0ae22
fix: don't filter out local instances in LCNF toMono pass (#6664)
zwarich Jan 17, 2025
b7815b5
feat: add lcAny constant to Prelude (#6665)
zwarich Jan 17, 2025
e3fd954
chore: update stage0
Jan 17, 2025
f374ef1
refactor: move `ext` environment extension to `Lean.Meta.Tactic`
leodemoura Jan 17, 2025
9b629cc
chore: update stage0
leodemoura Jan 17, 2025
4d4c094
feat: extensionality theorems in `grind` (#6682)
leodemoura Jan 17, 2025
d4070d4
fix: `grind` parameter issues and configuration (#6686)
leodemoura Jan 18, 2025
98e3d6f
fix: make `#check_failure`'s output be info (#6685)
Seasawher Jan 18, 2025
5e63dd2
chore: fix typo in docstring of mkMVar (#6687)
goens Jan 18, 2025
3770808
feat: split `Lean.Kernel.Environment` from `Lean.Environment` (#5145)
Kha Jan 18, 2025
5fb2e89
chore: update stage0
Jan 18, 2025
50a0a97
refactor: move registration of namespaces on kernel add into elaborat…
Kha Jan 18, 2025
70050c3
chore: `init_grind_norm` command parser (#6689)
leodemoura Jan 18, 2025
2694179
chore: update stage0
Jan 18, 2025
8a8417f
refactor: `getUnfoldableConst*?` (#5997)
Kha Jan 18, 2025
5998ba5
feat: regression tests for `grind` adapted from `lean-egg` (#6688)
kim-em Jan 18, 2025
478d421
feat: `init_grind_norm` elaborator (#6690)
leodemoura Jan 19, 2025
7ee9382
chore: update stage0
Jan 19, 2025
4d8bc22
feat: Environment.addConstAsync (#6691)
Kha Jan 19, 2025
4213862
chore: remove `[grind_norm]` attribute (#6692)
leodemoura Jan 19, 2025
74bd40d
chore: update stage0
Jan 19, 2025
75c104c
feat: align List/Array/Vector.reverse lemmas (#6695)
kim-em Jan 19, 2025
b289b66
chore: remove deprecations from 2024-06 (#6696)
kim-em Jan 19, 2025
35bbb48
feat: refactor List/Array.mapFinIdx to unbundle the Fin argument (#6697)
kim-em Jan 19, 2025
645bdea
perf: optimize `setImportedEntries` (#6698)
Kha Jan 19, 2025
a062eea
feat: beta reduction in `grind` (#6700)
leodemoura Jan 19, 2025
9b7bd58
feat: add `[grind ←=]` attribute (#6702)
leodemoura Jan 20, 2025
1fcdd7a
feat: add `[grind cases]` and `[grind cases eager]` attributes (#6705)
leodemoura Jan 20, 2025
e40e089
chore: update stage0
Jan 20, 2025
57f0006
feat: align `{List/Array/Vector}.{foldl, foldr, foldlM, foldrM}` lemm…
kim-em Jan 20, 2025
ac6a29e
feat: complete alignment of `{List,Array,Vector}.{mapIdx,mapFinIdx}` …
kim-em Jan 20, 2025
1d03cd6
fix: negative timestamps and `PlainDateTime`s before 1970 (#6668)
algebraic-dev Jan 20, 2025
22117f2
feat: align List/Array/Vector.count theorems (#6712)
kim-em Jan 20, 2025
c07f64a
doc: Fix (and expand) docstrings for `bmod`/`bdiv` (#6713)
vlad902 Jan 20, 2025
189f5d4
feat: case splitting in `grind` (#6717)
leodemoura Jan 20, 2025
778333c
fix: match equality generation (#6719)
leodemoura Jan 21, 2025
4935829
feat: generalize `infoview.maxTraceChildren` to the cmdline (#6716)
Kha Jan 21, 2025
e3771e3
fix: don't generate code for decls with an implemented_by attribute (…
zwarich Jan 21, 2025
c54287f
feat: add proper erasure of type dependencies in LCNF (#6678)
zwarich Jan 21, 2025
16bd7ea
chore: deprecate List.iota (#6708)
kim-em Jan 21, 2025
8375d00
fix: allow ⱼ in identifiers (#6679)
hargoniX Jan 21, 2025
f9e904a
feat: add `BitVec.[getMsbD_mul, getElem_udiv, getLsbD_udiv, getMsbD_u…
luisacicolini Jan 21, 2025
91bae2e
feat: align {List/Array/Vector}.{attach,attachWith,pmap} lemmas (#6723)
kim-em Jan 21, 2025
edeae18
feat: add `Bitvec reverse` definition, `getLsbD_reverse, getMsbD_reve…
luisacicolini Jan 21, 2025
7b813d4
feat: partial_fixpoint: partial functions with equations (#6355)
nomeata Jan 21, 2025
3569797
feat: functional cases theorem for non-recursive functions (#6261)
nomeata Jan 21, 2025
31929c0
feat: lemmas for HashMap.alter and .modify (#6620)
datokrat Jan 21, 2025
eb30249
doc: make description of `pp.analyze` more precise (#6726)
madvorak Jan 21, 2025
0c2fb34
chore: remove useless `Nat.mul_one` from proof (#6728)
luisacicolini Jan 21, 2025
c9a03c7
feat: overlapping match patterns in `grind` (#6733)
leodemoura Jan 21, 2025
3881f21
fix: redundant information in the offset constraint module (#6734)
leodemoura Jan 21, 2025
de31faa
feat: case splitting `match`-expressions with overlapping patterns in…
leodemoura Jan 22, 2025
533af01
feat: improve `grind` canonicalizer (#6736)
leodemoura Jan 22, 2025
9b74c07
feat: lazy `ite` branch internalization in `grind` (#6737)
leodemoura Jan 22, 2025
7706b87
feat: bv_decide support for structures of supported types (#6724)
hargoniX Jan 22, 2025
b6db90a
doc: mention subscript j in the lexical structure (#6738)
hargoniX Jan 22, 2025
6ebce42
perf: fast path for multiplication with constants in bv_decide (#6739)
hargoniX Jan 22, 2025
91e261d
chore: disable Elab.async on the cmdline for now (#6722)
Kha Jan 22, 2025
6595ca8
feat: improve equation theorem support in `grind` (#6746)
leodemoura Jan 22, 2025
6befda8
feat: add twoPow multiplication lemmas (#6742)
bollu Jan 22, 2025
5f3c0da
feat: BitVec.ushiftRight in terms of extractLsb' (#6745)
bollu Jan 22, 2025
0925559
fix: do not double-report `snapshotTasks` after `wrapAsyncAsSnapshot`
Kha Nov 14, 2024
e04a5fb
feat: asynchronous kernel checking
Kha Dec 2, 2024
b2bb7c4
merge async-tc
Kha Dec 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion doc/lexical_structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Parts of atomic names can be escaped by enclosing them in pairs of French double
letterlike_symbols: [℀-⅏]
escaped_ident_part: "«" [^«»\r\n\t]* "»"
atomic_ident_rest: atomic_ident_start | [0-9'ⁿ] | subscript
subscript: [₀-₉ₐ-ₜᵢ-]
subscript: [₀-₉ₐ-ₜᵢ-ᵪⱼ]
```

String Literals
Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -699,12 +699,12 @@ else()
endif()

if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
add_custom_target(lake_lib ALL
add_custom_target(lake_lib
WORKING_DIRECTORY ${LEAN_SOURCE_DIR}
DEPENDS leanshared
COMMAND $(MAKE) -f ${CMAKE_BINARY_DIR}/stdlib.make Lake
VERBATIM)
add_custom_target(lake_shared ALL
add_custom_target(lake_shared
WORKING_DIRECTORY ${LEAN_SOURCE_DIR}
DEPENDS lake_lib
COMMAND $(MAKE) -f ${CMAKE_BINARY_DIR}/stdlib.make libLake_shared
Expand Down
115 changes: 98 additions & 17 deletions src/Init/Data/Array/Attach.lean
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Authors: Joachim Breitner, Mario Carneiro
prelude
import Init.Data.Array.Mem
import Init.Data.Array.Lemmas
import Init.Data.Array.Count
import Init.Data.List.Attach

namespace Array
Expand Down Expand Up @@ -142,10 +143,16 @@ theorem pmap_eq_map_attach {p : α → Prop} (f : ∀ a, p a → β) (l H) :
cases l
simp [List.pmap_eq_map_attach]

@[simp]
theorem pmap_eq_attachWith {p q : α → Prop} (f : ∀ a, p a → q a) (l H) :
pmap (fun a h => ⟨a, f a h⟩) l H = l.attachWith q (fun x h => f x (H x h)) := by
cases l
simp [List.pmap_eq_attachWith]

theorem attach_map_coe (l : Array α) (f : α → β) :
(l.attach.map fun (i : {i // i ∈ l}) => f i) = l.map f := by
cases l
simp [List.attach_map_coe]
simp

theorem attach_map_val (l : Array α) (f : α → β) : (l.attach.map fun i => f i.val) = l.map f :=
attach_map_coe _ _
Expand All @@ -172,6 +179,12 @@ theorem mem_attach (l : Array α) : ∀ x, x ∈ l.attach
rcases this with ⟨⟨_, _⟩, m, rfl⟩
exact m

@[simp]
theorem mem_attachWith (l : Array α) {q : α → Prop} (H) (x : {x // q x}) :
x ∈ l.attachWith q H ↔ x.1 ∈ l := by
cases l
simp

@[simp]
theorem mem_pmap {p : α → Prop} {f : ∀ a, p a → β} {l H b} :
b ∈ pmap f l H ↔ ∃ (a : _) (h : a ∈ l), f a (H a h) = b := by
Expand Down Expand Up @@ -223,16 +236,16 @@ theorem attachWith_ne_empty_iff {l : Array α} {P : α → Prop} {H : ∀ a ∈
cases l; simp

@[simp]
theorem getElem?_pmap {p : α → Prop} (f : ∀ a, p a → β) {l : Array α} (h : ∀ a ∈ l, p a) (n : Nat) :
(pmap f l h)[n]? = Option.pmap f l[n]? fun x H => h x (mem_of_getElem? H) := by
theorem getElem?_pmap {p : α → Prop} (f : ∀ a, p a → β) {l : Array α} (h : ∀ a ∈ l, p a) (i : Nat) :
(pmap f l h)[i]? = Option.pmap f l[i]? fun x H => h x (mem_of_getElem? H) := by
cases l; simp

@[simp]
theorem getElem_pmap {p : α → Prop} (f : ∀ a, p a → β) {l : Array α} (h : ∀ a ∈ l, p a) {n : Nat}
(hn : n < (pmap f l h).size) :
(pmap f l h)[n] =
f (l[n]'(@size_pmap _ _ p f l h ▸ hn))
(h _ (getElem_mem (@size_pmap _ _ p f l h ▸ hn))) := by
theorem getElem_pmap {p : α → Prop} (f : ∀ a, p a → β) {l : Array α} (h : ∀ a ∈ l, p a) {i : Nat}
(hi : i < (pmap f l h).size) :
(pmap f l h)[i] =
f (l[i]'(@size_pmap _ _ p f l h ▸ hi))
(h _ (getElem_mem (@size_pmap _ _ p f l h ▸ hi))) := by
cases l; simp

@[simp]
Expand All @@ -256,6 +269,18 @@ theorem getElem_attach {xs : Array α} {i : Nat} (h : i < xs.attach.size) :
xs.attach[i] = ⟨xs[i]'(by simpa using h), getElem_mem (by simpa using h)⟩ :=
getElem_attachWith h

@[simp] theorem pmap_attach (l : Array α) {p : {x // x ∈ l} → Prop} (f : ∀ a, p a → β) (H) :
pmap f l.attach H =
l.pmap (P := fun a => ∃ h : a ∈ l, p ⟨a, h⟩)
(fun a h => f ⟨a, h.1⟩ h.2) (fun a h => ⟨h, H ⟨a, h⟩ (by simp)⟩) := by
ext <;> simp

@[simp] theorem pmap_attachWith (l : Array α) {p : {x // q x} → Prop} (f : ∀ a, p a → β) (H₁ H₂) :
pmap f (l.attachWith q H₁) H₂ =
l.pmap (P := fun a => ∃ h : q a, p ⟨a, h⟩)
(fun a h => f ⟨a, h.1⟩ h.2) (fun a h => ⟨H₁ _ h, H₂ ⟨a, H₁ _ h⟩ (by simpa)⟩) := by
ext <;> simp

theorem foldl_pmap (l : Array α) {P : α → Prop} (f : (a : α) → P a → β)
(H : ∀ (a : α), a ∈ l → P a) (g : γ → β → γ) (x : γ) :
(l.pmap f H).foldl g x = l.attach.foldl (fun acc a => g acc (f a.1 (H _ a.2))) x := by
Expand Down Expand Up @@ -313,11 +338,7 @@ theorem attachWith_map {l : Array α} (f : α → β) {P : β → Prop} {H : ∀
(l.map f).attachWith P H = (l.attachWith (P ∘ f) (fun _ h => H _ (mem_map_of_mem f h))).map
fun ⟨x, h⟩ => ⟨f x, h⟩ := by
cases l
ext
· simp
· simp only [List.map_toArray, List.attachWith_toArray, List.getElem_toArray,
List.getElem_attachWith, List.getElem_map, Function.comp_apply]
erw [List.getElem_attachWith] -- Why is `erw` needed here?
simp [List.attachWith_map]

theorem map_attachWith {l : Array α} {P : α → Prop} {H : ∀ (a : α), a ∈ l → P a}
(f : { x // P x } → β) :
Expand Down Expand Up @@ -347,7 +368,23 @@ theorem attach_filter {l : Array α} (p : α → Bool) :
simp [List.attach_filter, List.map_filterMap, Function.comp_def]

-- We are still missing here `attachWith_filterMap` and `attachWith_filter`.
-- Also missing are `filterMap_attach`, `filter_attach`, `filterMap_attachWith` and `filter_attachWith`.

@[simp]
theorem filterMap_attachWith {q : α → Prop} {l : Array α} {f : {x // q x} → Option β} (H)
(w : stop = (l.attachWith q H).size) :
(l.attachWith q H).filterMap f 0 stop = l.attach.filterMap (fun ⟨x, h⟩ => f ⟨x, H _ h⟩) := by
subst w
cases l
simp [Function.comp_def]

@[simp]
theorem filter_attachWith {q : α → Prop} {l : Array α} {p : {x // q x} → Bool} (H)
(w : stop = (l.attachWith q H).size) :
(l.attachWith q H).filter p 0 stop =
(l.attach.filter (fun ⟨x, h⟩ => p ⟨x, H _ h⟩)).map (fun ⟨x, h⟩ => ⟨x, H _ h⟩) := by
subst w
cases l
simp [Function.comp_def, List.filter_map]

theorem pmap_pmap {p : α → Prop} {q : β → Prop} (g : ∀ a, p a → β) (f : ∀ b, q b → γ) (l H₁ H₂) :
pmap f (pmap g l H₁) H₂ =
Expand Down Expand Up @@ -427,16 +464,48 @@ theorem reverse_attach (xs : Array α) :

@[simp] theorem back?_attachWith {P : α → Prop} {xs : Array α}
{H : ∀ (a : α), a ∈ xs → P a} :
(xs.attachWith P H).back? = xs.back?.pbind (fun a h => some ⟨a, H _ (mem_of_back?_eq_some h)⟩) := by
(xs.attachWith P H).back? = xs.back?.pbind (fun a h => some ⟨a, H _ (mem_of_back? h)⟩) := by
cases xs
simp

@[simp]
theorem back?_attach {xs : Array α} :
xs.attach.back? = xs.back?.pbind fun a h => some ⟨a, mem_of_back?_eq_some h⟩ := by
xs.attach.back? = xs.back?.pbind fun a h => some ⟨a, mem_of_back? h⟩ := by
cases xs
simp

@[simp]
theorem countP_attach (l : Array α) (p : α → Bool) :
l.attach.countP (fun a : {x // x ∈ l} => p a) = l.countP p := by
cases l
simp [Function.comp_def]

@[simp]
theorem countP_attachWith {p : α → Prop} (l : Array α) (H : ∀ a ∈ l, p a) (q : α → Bool) :
(l.attachWith p H).countP (fun a : {x // p x} => q a) = l.countP q := by
cases l
simp

@[simp]
theorem count_attach [DecidableEq α] (l : Array α) (a : {x // x ∈ l}) :
l.attach.count a = l.count ↑a := by
rcases l with ⟨l⟩
simp only [List.attach_toArray, List.attachWith_mem_toArray, List.count_toArray]
rw [List.map_attach, List.count_eq_countP]
simp only [Subtype.beq_iff]
rw [List.countP_pmap, List.countP_attach (p := (fun x => x == a.1)), List.count]

@[simp]
theorem count_attachWith [DecidableEq α] {p : α → Prop} (l : Array α) (H : ∀ a ∈ l, p a) (a : {x // p x}) :
(l.attachWith p H).count a = l.count ↑a := by
cases l
simp

@[simp] theorem countP_pmap {p : α → Prop} (g : ∀ a, p a → β) (f : β → Bool) (l : Array α) (H₁) :
(l.pmap g H₁).countP f =
l.attach.countP (fun ⟨a, m⟩ => f (g a (H₁ a m))) := by
simp [pmap_eq_map_attach, countP_map, Function.comp_def]

/-! ## unattach

`Array.unattach` is the (one-sided) inverse of `Array.attach`. It is a synonym for `Array.map Subtype.val`.
Expand All @@ -455,7 +524,7 @@ and is ideally subsequently simplified away by `unattach_attach`.

If not, usually the right approach is `simp [Array.unattach, -Array.map_subtype]` to unfold.
-/
def unattach {α : Type _} {p : α → Prop} (l : Array { x // p x }) := l.map (·.val)
def unattach {α : Type _} {p : α → Prop} (l : Array { x // p x }) : Array α := l.map (·.val)

@[simp] theorem unattach_nil {p : α → Prop} : (#[] : Array { x // p x }).unattach = #[] := rfl
@[simp] theorem unattach_push {p : α → Prop} {a : { x // p x }} {l : Array { x // p x }} :
Expand Down Expand Up @@ -578,4 +647,16 @@ and simplifies these to the function directly taking the value.
cases l₂
simp

@[simp] theorem unattach_flatten {p : α → Prop} {l : Array (Array { x // p x })} :
l.flatten.unattach = (l.map unattach).flatten := by
unfold unattach
cases l using array₂_induction
simp only [flatten_toArray, List.map_map, Function.comp_def, List.map_id_fun', id_eq,
List.map_toArray, List.map_flatten, map_subtype, map_id_fun', List.unattach_toArray, mk.injEq]
simp only [List.unattach]

@[simp] theorem unattach_mkArray {p : α → Prop} {n : Nat} {x : { x // p x }} :
(Array.mkArray n x).unattach = Array.mkArray n x.1 := by
simp [unattach]

end Array
17 changes: 13 additions & 4 deletions src/Init/Data/Array/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ def mapM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α
/-- Variant of `mapIdxM` which receives the index as a `Fin as.size`. -/
@[inline]
def mapFinIdxM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m]
(as : Array α) (f : Fin as.size → α → m β) : m (Array β) :=
(as : Array α) (f : (i : Nat) → α → (h : i < as.size) → m β) : m (Array β) :=
let rec @[specialize] map (i : Nat) (j : Nat) (inv : i + j = as.size) (bs : Array β) : m (Array β) := do
match i, inv with
| 0, _ => pure bs
Expand All @@ -464,12 +464,12 @@ def mapFinIdxM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m]
rw [← inv, Nat.add_assoc, Nat.add_comm 1 j, Nat.add_comm]
apply Nat.le_add_right
have : i + (j + 1) = as.size := by rw [← inv, Nat.add_comm j 1, Nat.add_assoc]
map i (j+1) this (bs.push (← f ⟨j, j_lt⟩ (as.get j j_lt)))
map i (j+1) this (bs.push (← f j (as.get j j_lt) j_lt))
map as.size 0 rfl (mkEmpty as.size)

@[inline]
def mapIdxM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : Nat → α → m β) (as : Array α) : m (Array β) :=
as.mapFinIdxM fun i a => f i a
as.mapFinIdxM fun i a _ => f i a

@[inline]
def findSomeM? {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → m (Option β)) (as : Array α) : m (Option β) := do
Expand Down Expand Up @@ -579,16 +579,25 @@ def foldr {α : Type u} {β : Type v} (f : α → β → β) (init : β) (as : A
/-- Sum of an array.

`Array.sum #[a, b, c] = a + (b + (c + 0))` -/
@[inline]
def sum {α} [Add α] [Zero α] : Array α → α :=
foldr (· + ·) 0

@[inline]
def countP {α : Type u} (p : α → Bool) (as : Array α) : Nat :=
as.foldr (init := 0) fun a acc => bif p a then acc + 1 else acc

@[inline]
def count {α : Type u} [BEq α] (a : α) (as : Array α) : Nat :=
countP (· == a) as

@[inline]
def map {α : Type u} {β : Type v} (f : α → β) (as : Array α) : Array β :=
Id.run <| as.mapM f

/-- Variant of `mapIdx` which receives the index as a `Fin as.size`. -/
@[inline]
def mapFinIdx {α : Type u} {β : Type v} (as : Array α) (f : Fin as.size → α → β) : Array β :=
def mapFinIdx {α : Type u} {β : Type v} (as : Array α) (f : (i : Nat) → α → (h : i < as.size) → β) : Array β :=
Id.run <| as.mapFinIdxM f

@[inline]
Expand Down
Loading
Loading