-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(Data/Seq): modify and set operations for Seq
#20160
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
Closed
Closed
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
4a941eb
make Seq.recOn cases_eliminator
vasnesterov e7dd2f6
revert some
vasnesterov db575e9
general lemmas
vasnesterov 6c0d1eb
drop lemmas
vasnesterov de0847b
termination lemmas
vasnesterov d0c4d00
take lemmas
vasnesterov b4b2f7b
fold lemmas
vasnesterov 620f8ca
rename
vasnesterov d5c4cb6
fix
vasnesterov 6204386
golf
vasnesterov f47f463
suggestions
vasnesterov 53fdb26
Merge branch 'vasnesterov/Seq' into vasnesterov/Seq_refactor
vasnesterov 65a2c3f
rearrange
vasnesterov 30befcf
return lost lemmas
vasnesterov 9781fdd
Merge branch 'vasnesterov/Seq_refactor' into vasnesterov/Seq_operations
vasnesterov c43d4ad
zip lemmas
vasnesterov 57dda42
modify + set
vasnesterov 55b86cb
zip lemmas
vasnesterov 66a3e87
zip_map lemmas
vasnesterov 9688eb7
Merge branch 'vasnesterov/Seq_refactor' into vasnesterov/Seq_operations
vasnesterov 69496e6
remove import
vasnesterov f35a0e1
Merge branch 'vasnesterov/Seq_refactor' into vasnesterov/Seq_operations
vasnesterov 03f44db
Merge branch 'master' into vasnesterov/Seq_operations
vasnesterov 0e45935
Merge branch 'master' into vasnesterov/Seq_operations
vasnesterov 6603676
golf
vasnesterov b187964
golf
vasnesterov 2d46e13
docs
vasnesterov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -714,6 +714,21 @@ def fold (s : Seq α) (init : β) (f : β → α → β) : Seq β := | |
| | some (x, s) => .some (f acc x, f acc x, s) | ||
| cons init <| corec f (init, s) | ||
|
|
||
| /-- Applies `f` to the `n`th element of the sequence, if it exists, replacing that element | ||
| with the result. -/ | ||
| def modify (s : Seq α) (n : ℕ) (f : α → α) : Seq α where | ||
| val := Function.update s.val n ((s.val n).map f) | ||
| property := by | ||
| have (i : ℕ) : (Function.update s.val n ((s.get? n).map f)) i = none ↔ s.get? i = none := by | ||
| by_cases hi : i = n <;> simp [Function.update, hi] | ||
| simp [IsSeq, this] | ||
| exact @s.prop | ||
|
Comment on lines
+724
to
+725
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like a non-terminal simp, can we write it using |
||
|
|
||
| /-- Sets the value of sequence `s` at index `n` to `a`. If the `n`th element does not exist | ||
| (`s` terminates earlier), the sequence is left unchanged. -/ | ||
| def set (s : Seq α) (n : ℕ) (a : α) : Seq α := | ||
| modify s n (fun _ ↦ a) | ||
|
|
||
| section OfStream | ||
|
|
||
| @[simp] | ||
|
|
@@ -1243,6 +1258,60 @@ theorem fold_head (init : β) (f : β → α → β) (s : Seq α) : | |
|
|
||
| end Fold | ||
|
|
||
| section Modify | ||
|
|
||
| @[simp] | ||
| theorem modify_nil {f : α → α} {n} : modify nil n f = nil := by | ||
| ext1 m | ||
| simp [modify, Function.update] | ||
|
|
||
| @[simp] | ||
| theorem set_nil {n : ℕ} {x : α} : set nil n x = nil := modify_nil | ||
|
|
||
| @[simp] | ||
| theorem modify_cons_zero {f : α → α} {hd : α} {tl : Seq α} : | ||
| (cons hd tl).modify 0 f = cons (f hd) tl := by | ||
| ext1 n | ||
| cases n <;> simp [modify] | ||
| rfl | ||
|
|
||
| @[simp] | ||
| theorem set_cons_zero {hd hd' : α} {tl : Seq α} : | ||
| (cons hd tl).set 0 hd' = cons hd' tl := modify_cons_zero | ||
|
|
||
| @[simp] | ||
| theorem modify_cons_succ {hd : α} {f : α → α} {n : ℕ} {tl : Seq α} : | ||
| (cons hd tl).modify (n + 1) f = cons hd (tl.modify n f) := by | ||
| ext1 n | ||
| cases n <;> simp [modify, Function.update] <;> rfl | ||
|
|
||
| @[simp] | ||
| theorem set_cons_succ {hd x : α} {n : ℕ} {tl : Seq α} : | ||
| (cons hd tl).set (n + 1) x = cons hd (tl.set n x) := modify_cons_succ | ||
|
|
||
| theorem set_get_of_not_terminated {s : Seq α} {x : α} {n : ℕ} | ||
| (h_not_terminated : ¬ s.TerminatedAt n) : | ||
| (s.set n x).get? n = x := by | ||
| simpa [set, modify, ← Option.ne_none_iff_exists'] using h_not_terminated | ||
|
|
||
| theorem set_get_of_terminated {s : Seq α} {x : α} {n : ℕ} | ||
| (h_terminated : s.TerminatedAt n) : | ||
| (s.set n x).get? n = .none := by | ||
| simpa [set, modify] using h_terminated | ||
|
|
||
| theorem set_get_stable {s : Seq α} {x : α} {n m : ℕ} | ||
| (h : n ≠ m) : | ||
| (s.set m x).get? n = s.get? n := by | ||
| simp [set, modify, Function.update, h] | ||
|
|
||
| theorem set_dropn_stable_of_lt {s : Seq α} {m n : ℕ} {x : α} | ||
| (h : m < n) : | ||
| (s.set m x).drop n = s.drop n := by | ||
| ext1 i | ||
| simp [set_get_stable (show n + i ≠ m by omega)] | ||
|
|
||
| end Modify | ||
|
|
||
| instance : Functor Seq where map := @map | ||
|
|
||
| instance : LawfulFunctor Seq where | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't tested this, but it looks like it should work