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: add List.commonPrefix and its lemmas #994

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

chabulhwi
Copy link
Contributor

@chabulhwi chabulhwi commented Oct 16, 2024

The function List.commonPrefix returns the longest common prefix of
two lists.

Co-authored-by: Kyle Miller [email protected]


The changes in this pull request were initially included in #987.

The function `List.commonPrefix` returns the longest common prefix of
two lists.

Co-authored-by: Kyle Miller <[email protected]>
Comment on lines +518 to +519
theorem commonPrefix_append_append (p l₁ l₂ : List α) :
commonPrefix (p ++ l₁) (p ++ l₂) = p ++ commonPrefix l₁ l₂ := by
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add commonPrefix_cons_cons as a lemma too?

Copy link
Contributor Author

@chabulhwi chabulhwi Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do it after I have dinner!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do it after I have dinner!

Since I'm going to redefine commonPrefix, I'll do it after I get more rest.

Comment on lines +13 to +22
/-- Returns the longest common prefix of two lists. -/
def commonPrefix [DecidableEq α] : (l₁ l₂ : List α) → List α
| [], _ => []
| _, [] => []
| a₁::l₁, a₂::l₂ =>
if a₁ = a₂ then
a₁ :: (commonPrefix l₁ l₂)
else
[]

Copy link
Member

@eric-wieser eric-wieser Oct 16, 2024

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

Suggested change
/-- Returns the longest common prefix of two lists. -/
def commonPrefix [DecidableEq α] : (l₁ l₂ : List α) → List α
| [], _ => []
| _, [] => []
| a₁::l₁, a₂::l₂ =>
if a₁ = a₂ then
a₁ :: (commonPrefix l₁ l₂)
else
[]
/-- Extract the longest common prefix of two lists, and the remainder of each list. -/
def extractCommonPrefix [DecidableEq α] : (l₁ l₂ : List α) → List α × List α × List α
| [], l₂ => ([], [], l₂)
| l₁, [] => ([], l₁, [])
| a₁ :: l₁, a₂ :: l₂ =>
if a₁ = a₂ then
let (common, l₁, l₂) := extractCommonPrefix l₁ l₂
(a₁ :: common, l₁, l₂)
else
([], a₁ :: l₁, a₂ :: l₂)

which gives you the extra data you want at the same time.

Copy link
Contributor Author

@chabulhwi chabulhwi Oct 16, 2024

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 a List version of it. Perhaps I should've followed @fgdorais's suggestion more strictly. I'll modify my code after I get some rest.

Copy link
Collaborator

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.

Copy link
Collaborator

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.

def extractCommonPrefixTR [DecidableEq α] (l₁ l₂ : List α) : List α
  loop [] l₁ l₂
where
  loop (acc : List) : (l₁ l₂ : List α) → List α
  | [], l₂ => (acc.reverse, [], l₂)
  | l₁, [] => (acc.reverse, l₁, [])
  | a₁::l₁, a₂::l₂ =>
    if a₁ = a₂ then
      loop (a₁ :: acc) l₁ l₂
    else
      (acc.reverse, l₁, l₂)

@[csimp] theorem extractCommonPrefix_eq_extractCommonPrefixTR :
    @extractCommonPrefix = @extractCommonPrefixTR := sorry

Let me know if you run into issues proving the csimp theorem.

Copy link
Contributor Author

@chabulhwi chabulhwi Oct 16, 2024

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.

How about having both of these definitions without making commonPrefix an abbrev? That'd make it easier to add new lemmas about commonPrefix.

Copy link
Collaborator

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.

@fgdorais fgdorais added the awaiting-review This PR is ready for review; the author thinks it is ready to be merged. label Oct 16, 2024
chabulhwi added a commit to chabulhwi/batteries that referenced this pull request Oct 16, 2024
I moved the theorem to another pull request:
leanprover-community#994.
@fgdorais fgdorais added awaiting-author Waiting for PR author to address issues and removed awaiting-review This PR is ready for review; the author thinks it is ready to be merged. labels Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-author Waiting for PR author to address issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants