惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
博客园 - Franky
V
V2EX
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
J
Java Code Geeks
T
The Blog of Author Tim Ferriss
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Jina AI
Jina AI
博客园 - 叶小钗
F
Fortinet All Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
D
Docker
博客园 - 【当耐特】
阮一峰的网络日志
阮一峰的网络日志

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Lean 4 for Programmers: Building a Todo List with Proof
Shrijith Ven · 2026-05-24 · via DEV Community

Hello, I'm Shrijith Venkatramana. I'm building git-lrc, an AI code reviewer that runs on every commit. Star Us to help devs discover the project. Do give it a try and share your feedback for improving the product.


Most programmers already write specifications.

They just don't call them that.

Whenever you write:

assert user.id is not None

Enter fullscreen mode Exit fullscreen mode

or:

if (!task.completedAt) throw Error(...)

Enter fullscreen mode Exit fullscreen mode

you are encoding invariants informally.

The problem is:
the compiler does not know them.

So your system eventually drifts into illegal states.

That is where Lean 4 becomes interesting.

Lean is not merely a theorem prover. It is a programming language where:

  • types can encode invariants,
  • proofs are first-class objects,
  • illegal states can become unrepresentable.

In this article, we will build a tiny todo system in Lean 4 and prove some properties about it.

Not because todo apps matter.

But because mundane software hides surprisingly rich semantics.

Installing Lean 4

The easiest path is:

curl https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh -sSf | sh

Enter fullscreen mode Exit fullscreen mode

Then:

lake new todo-proof
cd todo-proof

Enter fullscreen mode Exit fullscreen mode

Open it in VSCode with the Lean extension.

Your First Lean Program

Let's start with a simple structure.

structure Task where
  id : Nat
  title : String
  completed : Bool
deriving Repr

Enter fullscreen mode Exit fullscreen mode

This is similar to:

@dataclass
class Task:
    id: int
    title: "str"
    completed: bool

Enter fullscreen mode Exit fullscreen mode

Now create a value:

def task1 : Task :=
  { id := 1
    title := "Learn Lean"
    completed := false }

Enter fullscreen mode Exit fullscreen mode

And evaluate it:

#eval task1.title

Enter fullscreen mode Exit fullscreen mode


Functions are Pure

Let's write a function to complete a task.

def completeTask (t : Task) : Task :=
  { t with completed := true }

Enter fullscreen mode Exit fullscreen mode

Test it:

#eval (completeTask task1).completed

Enter fullscreen mode Exit fullscreen mode

Unlike mutable application code, Lean encourages modeling transitions explicitly.

That becomes important once proofs enter the picture.


Lists of Tasks

Now define a todo list.

abbrev TodoList := List Task

Enter fullscreen mode Exit fullscreen mode

Add tasks:

def addTask (todos : TodoList) (task : Task) : TodoList :=
  task :: todos

Enter fullscreen mode Exit fullscreen mode

Example:

def todos :=
  addTask [] task1

Enter fullscreen mode Exit fullscreen mode


The First Invariant

Real systems care about invariants.

Suppose:

No two tasks should share the same ID.

In JavaScript this is convention.

In Lean we can state it formally.


Extracting IDs

def ids (todos : TodoList) : List Nat :=
  todos.map (fun t => t.id)

Enter fullscreen mode Exit fullscreen mode


Unique IDs Predicate

Lean already has List.Nodup.

It means:

this list contains no duplicates.

Now define:

def UniqueIds (todos : TodoList) : Prop :=
  List.Nodup (ids todos)

Enter fullscreen mode Exit fullscreen mode

This is our first specification.

Notice something subtle:

Prop

Enter fullscreen mode Exit fullscreen mode

means:

a logical proposition.

Not a runtime boolean.


Proving Something

Let's prove:

an empty todo list has unique IDs.

theorem empty_has_unique_ids :
  UniqueIds [] := by
  unfold UniqueIds ids
  simp

Enter fullscreen mode Exit fullscreen mode

That tiny proof already demonstrates the core idea.

We encoded:

  • a system property,
  • mathematically,
  • and mechanically verified it.

Safe Task Addition

Now we define safer insertion.

We only allow adding a task if its ID is fresh.

First:

def idExists (todos : TodoList) (id : Nat) : Bool :=
  todos.any (fun t => t.id == id)

Enter fullscreen mode Exit fullscreen mode

Then:

def addTaskSafe (todos : TodoList) (task : Task) : Option TodoList :=
  if idExists todos task.id then
    none
  else
    some (task :: todos)

Enter fullscreen mode Exit fullscreen mode

This resembles production code more closely.


Proving Correctness of Safe Addition

Now the interesting part.

We want:

If the original list had unique IDs,
and insertion succeeds,
the resulting list also has unique IDs.

In theorem-prover land, this is the real API contract.

A simplified proof sketch:

theorem addTaskSafe_preserves_unique
    (todos : TodoList)
    (task : Task)
    (h : UniqueIds todos)
    (hadd : addTaskSafe todos task = some (task :: todos)) :
    UniqueIds (task :: todos) := by
  unfold addTaskSafe at hadd
  unfold UniqueIds at *
  simp at hadd
  aesop

Enter fullscreen mode Exit fullscreen mode

Do not worry if this feels strange initially.

The important conceptual shift is:

We are proving:

  • state transitions preserve invariants.

That is the heart of formal methods.


Illegal States

Most application bugs reduce to:

invalid transitions between states.

For example:

{
  completed: true,
  completedAt: null
}

Enter fullscreen mode Exit fullscreen mode

This is an illegal state.

Traditional systems allow it accidentally.

Lean encourages modeling state explicitly.


Encoding State in Types

Instead of:

structure Task where
  completed : Bool

Enter fullscreen mode Exit fullscreen mode

we can do:

inductive Status where
  | active
  | completed

Enter fullscreen mode Exit fullscreen mode

Now:

structure Task where
  id : Nat
  title : String
  status : Status

Enter fullscreen mode Exit fullscreen mode

Already better.

But we can go further.


Dependent Thinking

Suppose completed tasks must contain a completion timestamp.

In mainstream languages:

completedAt?: Date

Enter fullscreen mode Exit fullscreen mode

In Lean:

inductive TaskState where
  | active
  | completed (finishedAt : Nat)

Enter fullscreen mode Exit fullscreen mode

Now the timestamp exists iff the task is completed.

The illegal state disappears entirely.

This is a recurring pattern in theorem proving:
move runtime checks into type structure.


Why This Matters

At first glance, this seems academic.

But think about real systems:

  • payment processors,
  • distributed databases,
  • auth systems,
  • CI pipelines,
  • infrastructure tooling.

Most failures come from:

  • invalid states,
  • forgotten invariants,
  • impossible transitions that became possible.

Lean lets you model systems where:

  • assumptions are explicit,
  • transitions are checked,
  • proofs accompany implementations.

A More Interesting Direction

A todo app becomes genuinely interesting once you add:

  • offline sync,
  • collaboration,
  • event sourcing,
  • permissions,
  • undo/redo,
  • conflict resolution.

Then you start proving:

  • merge convergence,
  • replay determinism,
  • permission safety,
  • index consistency.

At that point, you are doing miniature distributed systems research.


Final Thoughts

The important lesson is not:

“we proved a todo app correct”.

It is:

ordinary software contains hidden formal structure.

Most systems already behave like:

  • state machines,
  • transition systems,
  • protocol graphs,
  • logical constraint engines.

Lean simply forces those assumptions into the open.

And once they are explicit:
they become provable.


*AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.*

Any feedback or contributors are welcome! It's online, source-available, and ready for anyone to use.


AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

See It In Action

See git-lrc catch serious security issues such as leaked credentials, expensive cloud operations, and sensitive material in log statements

git-lrc-intro-60s.mp4

Why

  • 🤖 AI agents silently break things. Code removed. Logic changed. Edge cases gone. You won't notice until production.
  • 🔍 Catch it before it ships. AI-powered inline comments show you exactly what changed and what looks wrong.
  • 🔁 Build a