{"slug": "lean-4-101-for-python-programmers-a-gentle-introduction-to-theorem-proving", "title": "Lean 4 101 for Python Programmers: A Gentle Introduction to Theorem Proving", "summary": "This article provides a beginner-friendly introduction to Lean 4 for Python programmers, highlighting how Lean prioritizes formal correctness and explicit type definitions over Python's flexible, runtime-dependent approach. It explains key differences such as Lean's use of pattern matching, immutability, and theorem proving, contrasting these with Python's mutable state and reliance on tests that only sample behavior rather than proving correctness.", "body_md": "*Before we begin — if you're interested in improving engineering quality and developer workflows, check out git-lrc on GitHub — a free micro AI code review tool that runs on every commit. If the idea resonates with you, consider giving the project a ⭐ on GitHub.*\n\nPython developers are used to expressive code.\n\nYou can automate workflows in minutes, prototype products quickly, manipulate huge datasets, and build entire backends with surprisingly little code.\n\nBut eventually many developers run into a frustrating problem:\n\n“How do I know my program is actually correct?”\n\nYou can write tests.\n\nYou can add type hints.\n\nYou can run linters.\n\nBut tests only sample behavior.\n\nThey do not prove correctness.\n\nThat’s where Lean becomes fascinating.\n\nLean 4 is not just a programming language.\n\nIt’s also:\n\n- a theorem prover\n- a proof assistant\n- a functional programming language\n- and a system for mathematically verifying software\n\nIf you’re coming from Python, Lean can feel strange at first.\n\nBut once the mental model starts clicking, it genuinely changes how you think about programming.\n\nThis article is a beginner-friendly introduction to Lean 4 specifically for Python programmers.\n\n# The Biggest Difference: Lean Wants Precision\n\nPython is flexible by design.\n\nFor example:\n\n``` python\ndef add(a, b):\n    return a + b\n```\n\nPython is happy with this.\n\nBut think carefully:\n\n- What types are\n`a`\n\nand`b`\n\n? - Integers?\n- Strings?\n- Lists?\n- Custom objects?\n\nPython delays those decisions until runtime.\n\nLean does the opposite.\n\nIn Lean:\n\n```\ndef add (a : Nat) (b : Nat) : Nat :=\n  a + b\n```\n\nThis explicitly says:\n\n-\n`a`\n\nis a natural number -\n`b`\n\nis a natural number - result is a natural number\n\nAt first this can feel verbose to Python developers.\n\nBut Lean’s philosophy is interesting:\n\nAmbiguity is usually hidden complexity.\n\nLean tries to eliminate hidden assumptions.\n\n# Your First Lean Functions\n\nLet’s compare Python and Lean side by side.\n\nPython:\n\n``` python\ndef square(x):\n    return x * x\n```\n\nLean:\n\n```\ndef square (x : Int) : Int :=\n  x * x\n```\n\nVery similar.\n\nNow let’s try something slightly more interesting.\n\nPython factorial:\n\n``` python\ndef factorial(n):\n    if n == 0:\n        return 1\n    return n * factorial(n - 1)\n```\n\nLean factorial:\n\n``` js\ndef factorial : Nat → Nat\n| 0 => 1\n| n + 1 => (n + 1) * factorial n\n```\n\nThis looks unusual initially.\n\nWhat’s happening here?\n\nLean uses *pattern matching*.\n\nExplanation:\n\n- if input is\n`0`\n\n, return`1`\n\n- otherwise treat the number as\n`n + 1`\n\n- recursively compute factorial\n\nThis style is extremely common in functional programming.\n\nAnd importantly:\n\nLean can reason formally about recursive definitions.\n\n# Lean Cares Deeply About Correctness\n\nHere’s a subtle Python issue:\n\n``` python\ndef divide(a, b):\n    return a / b\n```\n\nLooks harmless.\n\nBut:\n\n```\ndivide(10, 0)\n```\n\nRuntime error.\n\nPython lets dangerous states exist until execution.\n\nLean pushes you to think earlier.\n\nFor example, Lean separates different numeric systems carefully:\n\n- natural numbers\n- integers\n- rational numbers\n- finite values\n\nThat strictness is not accidental.\n\nLean assumes:\n\nif something matters logically, it should be represented explicitly.\n\nThis is one reason theorem provers are powerful.\n\n# Immutable Thinking Feels Different\n\nPython programmers often mutate state:\n\n``` python\ncount = 0\n\ndef increment():\n    global count\n    count += 1\n```\n\nLean strongly prefers immutable transformations:\n\n```\ndef increment (n : Nat) : Nat :=\n  n + 1\n```\n\nInstead of changing existing state:\n\n- input goes in\n- new value comes out\n\nWhy is this important?\n\nBecause mutable state becomes difficult to reason about formally.\n\nImagine trying to mathematically prove correctness of a large system where:\n\n- variables change everywhere\n- functions have hidden side effects\n- execution order matters constantly\n\nFunctional programming reduces this chaos.\n\nThat’s one reason languages like Lean heavily embrace immutability.\n\n# Types in Lean Are Much More Powerful Than Python Type Hints\n\nPython type hints help developers.\n\nLean types help prove things.\n\nThat’s a huge distinction.\n\nPython:\n\n``` php\ndef greet(name: str) -> str:\n    return \"Hello \" + name\n```\n\nHelpful, but not enforced rigorously.\n\nLean types can encode logical guarantees.\n\nFor example, you can express:\n\n- “this list cannot be empty”\n- “this number is positive”\n- “this function always terminates”\n\nThat’s where Lean starts feeling less like programming and more like formal reasoning.\n\nA tiny theorem:\n\n```\ntheorem add_zero (n : Nat) : n + 0 = n := by\n  rfl\n```\n\nThis proves:\n\nadding zero changes nothing\n\nBreaking it down:\n\n-\n`theorem`\n\ndeclares a proposition -\n`(n : Nat)`\n\nmeans for any natural number`n`\n\n-\n`: n + 0 = n`\n\nis the claim -\n`by`\n\nstarts proof mode -\n`rfl`\n\nmeans “this is true by direct simplification”\n\nThis may look trivial.\n\nBut Lean scales this exact mechanism to verify extremely sophisticated systems.\n\n# Lean Proofs Feel Like Interactive Debugging\n\nOne surprising thing about Lean:\n\nWriting proofs often feels similar to debugging.\n\nSuppose we define list length.\n\n``` js\ndef length : List α → Nat\n| [] => 0\n| _ :: xs => 1 + length xs\n```\n\nExplanation:\n\n- empty list has length 0\n-\notherwise:\n\n- ignore first element (\n`_`\n\n) - recursively compute remaining length\n\n- ignore first element (\n\nNow imagine proving something about lists.\n\nLean gives continuous feedback:\n\n- current assumptions\n- proof goals\n- missing logical steps\n\nThis creates a very interactive workflow.\n\nPython developers often enjoy REPL-driven development.\n\nLean has a similar “conversation with the system” feeling.\n\nExcept instead of debugging runtime behavior, you debug logical reasoning.\n\n# Tactics: Step-by-Step Proof Construction\n\nLean proofs are often built using tactics.\n\nExample:\n\n```\ntheorem reverse_reverse (xs : List Nat) :\n  xs.reverse.reverse = xs := by\n  simp\n```\n\nWhat does this mean?\n\nIt proves:\n\nreversing a list twice returns the original list\n\nAnd `simp`\n\ntells Lean:\n\nsimplify using known rules\n\nSome useful beginner tactics:\n\n-\n`simp`\n\n→ simplify expressions -\n`rw`\n\n→ rewrite using known equalities -\n`intro`\n\n→ introduce assumptions -\n`exact`\n\n→ directly provide proof -\n`apply`\n\n→ use another theorem\n\nThink of tactics like:\n\n- debugging tools\n- proof-building commands\n- interactive reasoning helpers\n\nOver time they become surprisingly intuitive.\n\n# Lean Quietly Improves Your Software Engineering\n\nEven if you never become a theorem proving expert, Lean changes how you think.\n\nYou start asking:\n\n- What assumptions exist here?\n- What invariants should always hold?\n- Can invalid states exist?\n- Is this behavior guaranteed?\n\nThis mindset transfers back into ordinary engineering.\n\nYou often begin writing:\n\n- cleaner APIs\n- safer abstractions\n- more predictable systems\n- more explicit contracts\n\nIronically, learning Lean can improve your Python.\n\n# How Python Developers Should Learn Lean\n\nA good beginner path:\n\n### 1. Learn basic functional programming ideas\n\nFocus on:\n\n- recursion\n- immutability\n- pattern matching\n\n### 2. Install Lean + VS Code\n\nUsing Visual Studio Code with the Lean extension gives excellent feedback.\n\n### 3. Ignore advanced math initially\n\nMany beginners get overwhelmed because online discussions quickly become abstract.\n\nStart simple:\n\n- functions\n- lists\n- recursion\n- tiny proofs\n\n### 4. Build tiny programs\n\nGood practice ideas:\n\n- reverse a list\n- binary trees\n- arithmetic functions\n- list transformations\n\n### 5. Expect gradual progress\n\nLean is closer to learning chess or mathematics than learning a typical framework.\n\nThe intuition develops slowly.\n\n# Conclusion\n\nLean 4 represents a very different philosophy of programming.\n\nPython asks:\n\n“Can we make development faster and more flexible?”\n\nLean asks:\n\n“Can we make reasoning precise and verifiable?”\n\nBoth philosophies matter.\n\nBut as software systems become increasingly complex — especially in:\n\n- AI\n- infrastructure\n- cryptography\n- finance\n- distributed systems\n\n— the ability to formally reason about correctness may become far more important.\n\nAnd perhaps that’s the most interesting thing about Lean:\n\nIt doesn’t just teach syntax.\n\nIt teaches disciplined thinking.\n\nSo here’s a question to leave you with:\n\nIf AI increasingly generates code automatically, do developers become more valuable not for writing code — but for verifying whether systems are actually correct?\n\nThat future may arrive sooner than we think.\n\nAnd if it does, theorem provers like Lean may eventually move from research circles into mainstream software engineering.\n\nAnd again — if you're interested in developer tooling around engineering quality, check out [git-lrc on GitHub](https://github.com/HexmosTech/git-lrc) — a free micro AI code review tool that runs on commits — and consider giving the project a ⭐ if you'd like to support it.", "url": "https://wpnews.pro/news/lean-4-101-for-python-programmers-a-gentle-introduction-to-theorem-proving", "canonical_source": "https://dev.to/shrsv/lean-4-101-for-python-programmers-a-gentle-introduction-to-theorem-proving-1f3e", "published_at": "2026-05-22 19:46:26+00:00", "updated_at": "2026-05-22 20:02:34.544232+00:00", "lang": "en", "topics": ["developer-tools", "open-source", "research"], "entities": ["Lean 4", "Python", "git-lrc", "GitHub"], "alternates": {"html": "https://wpnews.pro/news/lean-4-101-for-python-programmers-a-gentle-introduction-to-theorem-proving", "markdown": "https://wpnews.pro/news/lean-4-101-for-python-programmers-a-gentle-introduction-to-theorem-proving.md", "text": "https://wpnews.pro/news/lean-4-101-for-python-programmers-a-gentle-introduction-to-theorem-proving.txt", "jsonld": "https://wpnews.pro/news/lean-4-101-for-python-programmers-a-gentle-introduction-to-theorem-proving.jsonld"}}