Commit graph

139 commits

Author SHA1 Message Date
Joachim Breitner
c857d08be6
fix: remove derive_functional_induction (#3788)
this follows up on #3776 and the subsequent stage0 update, now relying
on the reserved name for the induction principles.
2024-03-27 10:08:13 +00:00
Julien Michel
4c0106d757
refactor: simplify Array.findIdx? code (#3648)
This shortens `Array.findIdx?` code, by using termination_by (and
well-founded recursion) instead of a structural recursion trick, with
the intent to make it more proof friendly.

One motivation is that it makes it easier to write a proof that
`Array.findIdx?` and `List.findIdx?` are equivalent. Furthermore, this
will be useful to prove that more complex functions are equivalent.

Closes #3646
2024-03-26 05:11:59 +00:00
Timo Carlin-Burns
8e96d7ba1d
refactor: clean up public API around Array.eraseIdx (#3676)
- Removes the public definitions `Array.eraseIdxAux` and
`Array.eraseIdxSzAux` which were implementation details.
- Motivation: `Array.eraseIdxAux` and `Array.eraseIdxSzAux` were clearly
not intended to remain public, but simply making them private would make
it inconvenient to unfold them when writing proofs in Std.
- Adds documentation comments to the public `Array.eraseIdx`-related
definitions which remain.
- Removes `Array.eraseIdx'` which was just `Array.feraseIdx` wrapped in
a subtype and adds `Array.size_feraseIdx` to prove the subtype property
as a standalone theorem.

Co-Authored-By: Daniel Windham <daniel@atlascomputing.org>
2024-03-17 06:25:10 +00:00
Kyle Miller
45fccc5906
feat: custom eliminators for induction and cases tactics, and beautiful eliminators for Nat (#3629)
Replaces `@[eliminator]` with two attributes `@[induction_eliminator]`
and `@[cases_eliminator]` for defining custom eliminators for the
`induction` and `cases` tactics, respectively.

Adds `Nat.recAux` and `Nat.casesAuxOn`, which are eliminators that are
defeq to `Nat.rec` and `Nat.casesOn`, but these use `0` and `n + 1`
rather than `Nat.zero` and `Nat.succ n`.

For example, using `induction` to prove that the factorial function is
positive now has the following goal states (thanks also to #3616 for the
goal state after unfolding).
```lean
example : 0 < fact x := by
  induction x with
  | zero => decide
  | succ x ih =>
    /-
    x : Nat
    ih : 0 < fact x
    ⊢ 0 < fact (x + 1)
    -/
    unfold fact
    /-
    ...
    ⊢ 0 < (x + 1) * fact x
    -/
    simpa using ih
```

Thanks to @adamtopaz for initial work on splitting the `@[eliminator]`
attribute.
2024-03-09 15:31:51 +00:00
Joe Hendrix
3e313d38f4
chore: upstream Std.Data.Array.Init.Basic (#3282)
This migrates the handful of array operations in
[Std.Data.Array.Init.Basic](https://github.com/leanprover/std4/blob/main/Std/Data/Array/Init/Basic.lean).
2024-02-08 19:30:47 +00:00
Joachim Breitner
c0f264ffe0
fix: reducing out-of-bounds swap! should return a, not default (#3197)
`Array.set!` and `Array.swap!` are fairly similar operations, both
modify an array, both take an index that it out of bounds.

But they behave different; all of these return `true`
```
#eval #[1,2].set! 2 42 == #[1,2]    -- with panic
#reduce #[1,2].set! 2 42 == #[1,2]  -- no panic

#eval #[1,2].swap! 0 2 == #[1,2]    -- with panic
#reduce #[1,2].swap! 0 2 == default -- no panic
```

The implementations are
```
@[extern "lean_array_set"]
def Array.set! (a : Array α) (i : @& Nat) (v : α) : Array α :=
  Array.setD a i v
```
but
```
@[extern "lean_array_swap"]
def swap! (a : Array α) (i j : @& Nat) : Array α :=
  if h₁ : i < a.size then
  if h₂ : j < a.size then swap a ⟨i, h₁⟩ ⟨j, h₂⟩
  else panic! "index out of bounds"
  else panic! "index out of bounds"
```

It seems to be more consistent to unify the behaviors, and define
```
@[extern "lean_array_swap"]
def swap! (a : Array α) (i j : @& Nat) : Array α :=
  if h₁ : i < a.size then
  if h₂ : j < a.size then swap a ⟨i, h₁⟩ ⟨j, h₂⟩
  else a
  else a
```

Also adds docstrings.

Fixes #3196
2024-01-19 18:29:18 +00:00
Joachim Breitner
368ead54b2 refactor: termination_by changes in stdlib 2024-01-10 17:27:35 +01:00
Joachim Breitner
34264a4b1d
doc: Improve docstrings around Array.mk,.data,.toList (#2771)
following a discussion at

<https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/Understanding.20the.20docstring.20for.20docs.23Array.2Edata/near/398705430>

---------

Co-authored-by: Mario Carneiro <di.gama@gmail.com>
2023-11-29 08:49:13 +00:00
Scott Morrison
57e23917b6 fix: implementation of Array.anyMUnsafe
move test
2023-10-11 11:20:45 +02:00
Mario Carneiro
3104c223d8 fix: reference implementation for Array.mapM 2023-06-27 14:21:22 -07:00
Henrik Böving
24cc6eae6d feat: log2 for Fin and UInts 2022-11-29 01:05:06 +01:00
Leonardo de Moura
9dbd9ec554 chore: fix build 2022-11-28 07:53:43 -08:00
Leonardo de Moura
a4acf084c8 chore: fix build 2022-11-15 16:51:40 -08:00
Mario Carneiro
d7d61bfb55 feat: use withoutPosition consistently (part 2) 2022-10-24 12:51:32 -07:00
Mario Carneiro
583e023314 chore: snake-case attributes (part 2) 2022-10-19 09:28:08 -07:00
Mario Carneiro
356db4e1df fix: simplify termination_by clause 2022-09-19 13:49:20 -07:00
Mario Carneiro
bb23fc0c86 chore: extract termination lemma for reverse 2022-09-19 13:49:20 -07:00
Mario Carneiro
ed6a5bba88 chore: rename insertAt to insertAt! 2022-09-19 13:49:20 -07:00
Mario Carneiro
f8c6306469 feat: remove bounds checks in Array.{reverse, insertAt} 2022-09-19 13:49:20 -07:00
Mario Carneiro
f6211b1a74
chore: convert doc/mod comments from /- to /--//-! (#1354) 2022-07-22 12:05:31 -07:00
Leonardo de Moura
2fcd406f99 chore: remove sorry 2022-07-10 20:04:06 -07:00
Leonardo de Moura
475c7e18cd chore: missing GetElem instances 2022-07-10 14:53:22 -07:00
Leonardo de Moura
2f1b80721e chore: avoid a[i]' h notation 2022-07-10 07:20:07 -07:00
Leonardo de Moura
aa52eebcdc feat: add instance GetElem (Array α) USize α fun xs i => LT.lt i.toNat xs.size where 2022-07-09 16:18:29 -07:00
Leonardo de Moura
1caff852fb chore: remove getOp functions 2022-07-09 16:09:28 -07:00
Leonardo de Moura
e4b358a01e refactor: prepare to elaborate a[i] notation using typeclasses 2022-07-09 15:24:22 -07:00
Leonardo de Moura
5e3a3a6c21 chore: remove notation a[i,h] for a[⟨i, h⟩] 2022-07-03 06:24:26 -07:00
Leonardo de Moura
a2456c3a0f feat: add notation a[i, h] for a[⟨i, h⟩] 2022-07-02 15:50:49 -07:00
Leonardo de Moura
3f3cd22366 feat: add Array.getOp! and Array.getOp?
Add warning when `Array.getOp` is used. TODO: replace `Array.getOp`
with safe version
2022-07-02 10:06:05 -07:00
E.W.Ayers
a7c33a963f doc: docstrings for List.isPrefixOf 2022-06-17 17:47:51 -07:00
Leonardo de Moura
041827bed5 chore: unused variables 2022-06-07 17:54:10 -07:00
Sebastian Ullrich
ae7b895f7a refactor: unname some unused variables 2022-06-07 16:37:45 -07:00
Leonardo de Moura
de2e2447d2 chore: style 2022-04-07 17:35:05 -07:00
Leonardo de Moura
cfb4e306f7 refactor: replace length_dropLast theorem 2022-04-01 16:44:24 -07:00
zygi
ac793ce196 fix: forward start and stop in Array.allM 2022-03-17 10:08:40 +01:00
Sebastian Ullrich
82c682d385 chore: remove redundant proof 2022-03-12 11:12:56 +01:00
Leonardo de Moura
5845002e8c feat: use WF at anyM 2022-03-03 16:43:05 -08:00
Leonardo de Moura
02d7cedba8 chore: remove unnecessary termination_by annotations 2022-03-03 11:24:06 -08:00
Leonardo de Moura
19bcb5fb31 feat: add Array.popWhile and Array.takeWhile 2022-02-19 07:04:52 -08:00
Sebastian Ullrich
54522006f4 refactor: List.get: take Fin to align with Array.get
/cc @leodemoura
2022-02-15 18:41:22 +01:00
Leonardo de Moura
1d5da63482 chore: simplify Array.isEqvAux 2022-02-10 16:51:47 -08:00
Leonardo de Moura
12e2a79170 chore: fix codebase after removing auto pure 2022-02-03 18:08:14 -08:00
Gabriel Ebner
561a869e49 fix: provide reference implementation for Array.modify 2022-01-17 12:41:12 -08:00
Leonardo de Moura
bac91b9b5b chore: remove arbitrary 2022-01-15 12:14:27 -08:00
Leonardo de Moura
acd482c5f2 feat: define Array.modify without using Inhabited 2022-01-14 19:47:42 -08:00
Leonardo de Moura
7fe6881c42 feat: use new termination_by syntax 2022-01-12 16:23:25 -08:00
Leonardo de Moura
381f66428a chore: use termination_by'
We are going to define a higher level syntax for `termination_by`.
2022-01-11 15:00:53 -08:00
Leonardo de Moura
dae3489fe2 feat: remove partials from Init/Data/Array/Basic.lean 2022-01-10 16:05:33 -08:00
Leonardo de Moura
68bd55af32 chore: fix codebase 2021-12-10 13:12:09 -08:00
Leonardo de Moura
2fd024c26f feat: add support for foldlM, foldl, ForIn instances for byte/float arrays 2021-10-18 16:54:56 -07:00