Code for me simple geometry exercises in Lean4 so I can better understand the logic of…
- User
Code for me simple geometry exercises in Lean4 so I can better understand the logic of thinking.
- Assistant
Certainly! I'd be happy to help you understand basic geometry concepts using Lean 4. We'll start by defining some fundamental geometric objects and then work through simple exercises and proofs. I'll provide explanations along the way to clarify the logic.
Prerequisites
Before we begin, ensure you have Lean 4 installed. You can find installation instructions on the Lean 4 repository. Also, it's helpful if you have some familiarity with Lean's syntax and basic concepts.
Exercise 1: Defining Basic Geometric Concepts
Goal: Define the basic geometric entities: points, lines, and the incidence relation between them.
-- We start by declaring the types for Points and Lines. structure Point where name : String deriving DecidableEq, Repr structure Line where name : String deriving DecidableEq, Repr -- The incidence relation: when a point lies on a line. def Incidence (P : Point) (L : Line) : Prop := -- In a full geometry theory, we'd have more details. -- For illustration, we'll define this as a predicate. True -- Placeholder; we'll define specific incidents later.Explanation:
- We define
PointandLineas structures with anamefield for identification. - We derive
DecidableEqto allow comparison of points and lines, andReprfor pretty printing. Incidenceis a predicate representing the relation where a point lies on a line.
Exercise 2: Axiom - Two Points Determine a Line
Goal: Introduce an axiom stating that any two distinct points determine a unique line.
-- Axiom: Through any two distinct points, there exists exactly one line incident with both. axiom exists_unique_line_through_two_points (P Q : Point) (h : P ≠ Q) : ∃! L : Line, Incidence P L ∧ Incidence Q LExplanation:
- This axiom reflects a fundamental principle in Euclidean geometry.
∃!denotes "exists exactly one".h : P ≠ Qensures the points are distinct.
Exercise 3: Theorem - Two Points Determine a Unique Line
Goal: Use the axiom to prove that two distinct points determine a unique line.
-- Theorem: For any two distinct points, there exists exactly one line incident with both. theorem two_points_determine_unique_line (P Q : Point) (h : P ≠ Q) : ∃! L : Line, Incidence P L ∧ Incidence Q L := exists_unique_line_through_two_points P Q hExplanation:
- We restate the axiom as a theorem to demonstrate how to use axioms in proofs.
- This theorem directly invokes the axiom we've defined.
Exercise 4: Defining Collinearity
Goal: Define when three points are collinear.
-- Definition: Three points are collinear if there exists a line incident with all three. def Collinear (A B C : Point) : Prop := ∃ L : Line, Incidence A L ∧ Incidence B L ∧ Incidence C LExplanation:
Collinearis a predicate that holds if the three points lie on the same line.
Exercise 5: Theorem - Collinearity is Symmetric
Goal: Prove that the collinearity of three points is independent of the order of the points.
-- Theorem: Collinearity is symmetric with respect to its arguments. theorem collinear_symm (A B C : Point) : Collinear A B C ↔ Collinear C B A := by unfold Collinear apply Iff.intro { intro h rcases h with ⟨L, hA, hB, hC⟩ exact ⟨L, hC, hB, hA⟩ } { intro h rcases h with ⟨L, hC, hB, hA⟩ exact ⟨L, hA, hB, hC⟩ }Explanation:
- We use
unfoldto expand the definition ofCollinear. Iff.introis used to prove an 'if and only if' statement.rcasesdestructures the existential quantifier.- We swap the order of the points in the proof, demonstrating symmetry.
Exercise 6: Axiom - Line Uniqueness with Two Points
Goal: Introduce an axiom stating that if two lines share two distinct points, they are the same line.
-- Axiom: If two lines are incident with the same two distinct points, they are equal. axiom lines_equal_if_same_points (L1 L2 : Line) (P Q : Point) (hP : P ≠ Q) (hL1P : Incidence P L1) (hL1Q : Incidence Q L1) (hL2P : Incidence P L2) (hL2Q : Incidence Q L2) : L1 = L2Explanation:
- This axiom states that two lines sharing two distinct points must be the same line.
- It reinforces the uniqueness of the line determined by two points.
Exercise 7: Theorem - Lines with Two Common Points are Equal
Goal: Prove that if two lines share two distinct points, they are identical.
-- Theorem: Two lines incident with the same two distinct points are equal. theorem lines_with_same_two_points_equal (L1 L2 : Line) (P Q : Point) (hP : P ≠ Q) (hL1P : Incidence P L1) (hL1Q : Incidence Q L1) (hL2P : Incidence P L2) (hL2Q : Incidence Q L2) : L1 = L2 := lines_equal_if_same_points L1 L2 P Q hP hL1P hL1Q hL2P hL2QExplanation:
- We directly apply the axiom
lines_equal_if_same_pointsto prove the theorem. - This example demonstrates how axioms can be utilized in proofs.
Exercise 8: Defining and Proving Midpoints
Goal: Define what it means for a point to be the midpoint of a line segment and prove a simple property.
First, we need to introduce the concept of a line segment and distances. However, in a pure axiomatic geometry without coordinates or metrics, defining distances requires additional axioms. For simplicity, we'll assume the existence of a distance function.
Assuming: There exists a function
distancethat assigns a non-negative real number to any two points.-- We'll use Real numbers to represent distances. open Real -- Axiom: There exists a distance function satisfying the usual properties. axiom distance (A B : Point) : ℝ -- Axioms for the distance function: axiom distance_nonneg (A B : Point) : distance A B ≥ 0 axiom distance_symm (A B : Point) : distance A B = distance B A axiom distance_eq_zero_iff_eq (A B : Point) : distance A B = 0 ↔ A = BDefine Midpoint:
-- Definition: Point M is the midpoint of points A and B if it divides the segment into two equal lengths. def Midpoint (M A B : Point) : Prop := distance A M = distance M B ∧ distance A M + distance M B = distance A BTheorem:
-- Theorem: If M is the midpoint of A and B, then distance A M = (1/2) * distance A B theorem midpoint_distance (M A B : Point) (h : Midpoint M A B) : distance A M = (distance A B) / 2 := by rw [←h.2] -- Use the fact that distance A M + distance M B = distance A B have h_eq : 2 * distance A M = distance A B := by linarith -- Since distance A M = distance M B linarith -- Solve for distance A MExplanation:
- We define
Midpointbased on the distances between points. - The theorem shows a property of the midpoint in terms of distances.
- We use
rw(rewrite) to substitute using known equalities. linarithis a Lean tactic used to solve linear arithmetic goals.
Tips for Understanding the Logic
- Structure Definitions: In Lean,
structureis used to define new types with named fields. - Axioms: Fundamental truths assumed without proof. In Lean,
axiomintroduces such an assumption. - Theorems and Proofs: Theorems state propositions that can be proved using axioms and previously established theorems.
- Proof Tactics: Lean provides tactics like
by,intro,apply,rw,linarith, etc., to construct proofs. - Logical Connectives: Symbols like
∧(and),∨(or),¬(not),→(implies),↔(if and only if), and quantifiers like∀(for all),∃(exists),∃!(exists unique). - Pattern Matching:
rcasesis used to deconstruct existential quantifiers or other compound expressions.
Further Exercises
- Exercise: Define the concept of parallel lines and prove basic properties.
- Exercise: Introduce angles and prove that the sum of angles in a triangle equals 180 degrees (requires more advanced definitions).
- Exercise: Explore the properties of triangles, congruence, and similarity.
Feel free to modify the definitions and proofs to explore different aspects of geometry in Lean 4. Experimenting with the code and attempting to prove additional theorems will enhance your understanding of both geometry and formal proofs in Lean.
If you have any questions or need further clarification on any part of the code or logic, don't hesitate to ask!
- We define