Lean Proof Explanation: IsCycle.three_le_length

User:

could you explain the lean proof of this theorem

The following references were attached as context:
File path: Mathlib/Combinatorics/SimpleGraph/Path.lean
Source URL: GitHub Link
Repo: leanprover-community/mathlib4
CommitOID: 1a9b964b92509b9a757fd24bae6b6b9812443163
lemma IsCycle.three_le_length {v : V} {p : G.Walk v v} (hp : p.IsCycle) : 3 ≤ p.length := by
      
Copilot:
Absolutely! The lemma you are referring to is:
lemma IsCycle.three_le_length {v : V} {p : G.Walk v v} (hp : p.IsCycle) : 3 ≤ p.length := by
      

What does the lemma state?

Why is this true?

What does a Lean proof look like?

While I can't see the exact proof in the file, a typical Lean approach would be:

  1. Unpack IsCycle: Use the definition of IsCycle to get that p is a nontrivial closed walk with no repeated vertices except at the endpoints.
  2. No Repeated Vertices: The only way to have no repeats except start/end is if the walk visits at least 3 distinct vertices.
  3. Length Calculation:
    • The length of a walk is the number of edges.
    • For a cycle, this is at least 3 (since fewer would mean repeats or triviality).

A sketch of the proof in Lean:

lemma IsCycle.three_le_length {v : V} {p : G.Walk v v} (hp : p.IsCycle) : 3 ≤ p.length :=
begin
  -- Expand what it means for p to be a cycle
  rcases hp with ⟨hne, h_closed, h_norep⟩,
  -- hne: p is nontrivial (not nil or single edge)
  -- h_closed: p is closed (starts and ends at v)
  -- h_norep: no repeated vertices except endpoints
  -- Use the facts above to show p.length ≥ 3
  -- (Details would involve showing that the minimal nontrivial closed walk with no repeated vertices must have at least 3 edges)
end

Intuitive Explanation

Summary

If you want to see the full Lean code proof from the file, let me know and I can fetch and explain it line by line!

User:

Do please fetch the full Lean code proof and explain it line by line

Copilot:

Here is the full Lean proof for the lemma you asked about, followed by a detailed line-by-line explanation:

lemma IsCycle.three_le_length {v : V} {p : G.Walk v v} (hp : p.IsCycle) : 3 ≤ p.length := by
  have ⟨⟨hp, hp'⟩, _⟩ := hp
  match p with
  | .nil => simp at hp'
  | .cons h .nil => simp at h
  | .cons _ (.cons _ .nil) => simp at hp
  | .cons _ (.cons _ (.cons _ _)) => simp_rw [SimpleGraph.Walk.length_cons]; omega

Let's break this down:

1. Statement and Parameter Unpacking

lemma IsCycle.three_le_length {v : V} {p : G.Walk v v} (hp : p.IsCycle) : 3 ≤ p.length := by
Meaning: If p is a cycle (i.e., p.IsCycle), then the length of p is at least 3.

2. Extracting Structure from the Hypothesis

have ⟨⟨hp, hp'⟩, _⟩ := hp
Here, hp is unpacked using Lean's structure projection and pattern matching.
IsCycle extends IsCircuit, which in turn extends IsTrail, so hp contains:
  • The trail property (hp),
  • The non-nil (nonempty) property (hp'),
  • The property that the only repeated vertex is the start/end.

3. Case Analysis on the Walk Structure

match p with
| .nil => simp at hp'
| .cons h .nil => simp at h
| .cons _ (.cons _ .nil) => simp at hp
| .cons _ (.cons _ (.cons _ _)) => simp_rw [SimpleGraph.Walk.length_cons]; omega
  • The proof proceeds by splitting into cases depending on the structure of the walk p:
    • In Lean, .nil is the empty walk, .cons h p is a step (edge) followed by a subwalk.
  • Case 1: p is .nil (empty walk)
    | .nil => simp at hp'
    If p is empty, hp' (which is the non-nil property) is contradicted by simp, so this case cannot actually happen.
  • Case 2: p is a single edge: .cons h .nil
    | .cons h .nil => simp at h
    If p is just one edge, then h is an adjacency. But for a cycle, this is impossible in a simple graph (would require a loop), so simp at h derives a contradiction.
  • Case 3: p is two edges: .cons _ (.cons _ .nil)
    | .cons _ (.cons _ .nil) => simp at hp
    If p is two edges, this would mean a walk from v to some w and back to v. But for a cycle in a simple graph, this would require a repeated vertex or edge, which is not allowed. So simp at hp finds the contradiction.
  • Case 4: p is three or more edges: .cons _ (.cons _ (.cons _ _))
    | .cons _ (.cons _ (.cons _ _)) => simp_rw [SimpleGraph.Walk.length_cons]; omega
    This matches the case where p has at least three edges. simp_rw [SimpleGraph.Walk.length_cons] rewrites the length of the walk in terms of cons cells. omega is an automation tactic that solves linear arithmetic goals, so it checks that the length is at least 3.

Summary

  • The proof eliminates the cases where the walk is of length 0, 1, or 2, showing that a cycle must have at least length 3.
  • For length ≥ 3, it is automatically true by the definition of length.

Why is this correct?

In a simple graph, a cycle cannot have fewer than three edges because that would mean a loop or a repeat, both forbidden by the definition of a cycle in this context.
If you’d like clarification on any specific line, or want to know how the supporting definitions (like IsCycle) work, let me know!
User:

Could you export the above conversation into an html file?

Copilot:
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Lean Proof Explanation: IsCycle.three_le_length</title>
  <style>
    body { font-family: Arial, sans-serif; margin: 2em; background: #f9f9f9; }
    pre, code { background: #f4f4f4; padding: 0.5em; border-radius: 4px; }
    .user { color: #0a3069; margin-bottom: 1em; }
    .assistant { color: #125512; margin-bottom: 2em; }
    .block { margin-bottom: 2em; }
    .code-block { background: #f4f4f4; border: 1px solid #e1e1e8; padding: 1em; border-radius: 5px; }
    .header { font-weight: bold; }
    h1 { color: #22223b; }
    h2 { color: #2d6a4f; }
    h3 { color: #4f772d; }
  </style>
</head>
<body>
  <h1>Lean Proof Explanation: <code>IsCycle.three_le_length</code></h1>
  <div class="block user">
    <span class="header">User:</span>
    <p>could you explain the lean proof of this theorem</p>
    <pre>
lemma IsCycle.three_le_length {v : V} {p : G.Walk v v} (hp : p.IsCycle) : 3 ≤ p.length := by
    </pre>
  </div>...
        
(Partial. The content above was a generated HTML export of part of the chat. For the full transcript, see below.)
User:

Please check again. You have excluded parts of the text and some code snippets. Please provide the entire chat transcript accurately in html