-
Notifications
You must be signed in to change notification settings - Fork 104
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: add List.commonPrefix
and its lemmas
#994
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -482,6 +482,56 @@ theorem Sublist.erase_diff_erase_sublist {a : α} : | |
|
||
end Diff | ||
|
||
/-! ### prefix, suffix, infix -/ | ||
|
||
section | ||
|
||
variable [DecidableEq α] | ||
|
||
theorem commonPrefix_comm (l₁ l₂ : List α) : commonPrefix l₁ l₂ = commonPrefix l₂ l₁ := by | ||
cases l₁ <;> cases l₂ <;> simp only [commonPrefix] | ||
next a₁ l₁ a₂ l₂ => | ||
split | ||
· subst a₁ | ||
simp only [↓reduceIte, cons.injEq, true_and] | ||
apply commonPrefix_comm | ||
· next h => | ||
simp [Ne.symm h] | ||
|
||
theorem commonPrefix_prefix_left (l₁ l₂ : List α) : commonPrefix l₁ l₂ <+: l₁ := by | ||
match l₁, l₂ with | ||
| [], _ => simp [commonPrefix] | ||
| _::_, [] => simp [commonPrefix] | ||
| a₁::l₁, a₂::l₂ => | ||
simp only [commonPrefix] | ||
split | ||
· next h => | ||
simp only [h, ↓reduceIte, cons_prefix_cons, true_and] | ||
apply commonPrefix_prefix_left | ||
· next h => | ||
simp [h] | ||
|
||
theorem commonPrefix_prefix_right (l₁ l₂ : List α) : commonPrefix l₁ l₂ <+: l₂ := by | ||
rw [commonPrefix_comm] | ||
apply commonPrefix_prefix_left | ||
|
||
theorem commonPrefix_append_append (p l₁ l₂ : List α) : | ||
commonPrefix (p ++ l₁) (p ++ l₂) = p ++ commonPrefix l₁ l₂ := by | ||
Comment on lines
+518
to
+519
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. Can you add 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.
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.
Since I'm going to redefine |
||
match p with | ||
| [] => rfl | ||
| a::p => simpa [commonPrefix] using commonPrefix_append_append p l₁ l₂ | ||
|
||
theorem not_prefix_and_not_prefix_symm_iff {l₁ l₂ : List α} (hp : commonPrefix l₁ l₂ = []) : | ||
¬l₁ <+: l₂ ∧ ¬l₂ <+: l₁ ↔ l₁ ≠ [] ∧ l₂ ≠ [] ∧ l₁.head? ≠ l₂.head? := by | ||
match l₁, l₂ with | ||
| [], _ => simp | ||
| _, [] => simp | ||
| a₁::l₁, a₂::l₂ => | ||
simp only [commonPrefix, ite_eq_right_iff, reduceCtorEq, imp_false] at hp | ||
simp [hp, Ne.symm hp] | ||
|
||
end | ||
|
||
/-! ### drop -/ | ||
|
||
theorem disjoint_take_drop : ∀ {l : List α}, l.Nodup → m ≤ n → Disjoint (l.take m) (l.drop n) | ||
|
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.
Does it makes sense to define this instead as
which gives you the extra data you want at the same time.
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.
Yesterday, I found
Substring.commonPrefix
and decided to create aList
version of it. Perhaps I should've followed @fgdorais's suggestion more strictly. I'll modify my code after I get some rest.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.
You can then add
commonPrefix
as an abbrev to grab just the first component.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.
For your convenience, here is the tail recursive version of Eric's
extractCommonPrefix
.Let me know if you run into issues proving the csimp theorem.
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.
How about having both of these definitions without making
commonPrefix
anabbrev
? That'd make it easier to add new lemmas aboutcommonPrefix
.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.
Try the abbrev first. In principle, the abbrev should cut down on the number of necessary lemmas. Let's see how it works out in practice. Take your time and ping me here or on Zulip if you run into issues.