Leanpub Header

Skip to main content

Learning Lean 4 With Examples

A Practical Guide to Functional Programming, Classic AI, and Lightweight Verification in Lean 4

Learning Lean 4 With Examples
This book is 100% completeLast updated on 2026-11-16

Have fun with the Lean 4 language, no need to be scared!

Minimum price

Free!

$10.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
About

About

About the Book

While most Lean 4 guides dive straight into formal theorem proving and abstract mathematics, "Learning Lean 4 With Examples" treats Lean as a practical, expressive functional programming language. Through fourteen self-contained, single-file programs, you will build classic AI algorithms, symbolic polynomial calculus, automatic differentiation, and web/LLM integrations, along with native executables using Lake and C FFI. Along the way, the book introduces lightweight compile-time checks and proofs to verify properties of your code without requiring hardcore mathematical discovery. Written by veteran AI researcher Mark Watson with the assistance of DeepSeek 4.1 Flash, Gemini Argon, and Claude Opus 5.5, this concise volume offers a hands-on entry point for programmers curious about strongly typed functional software.

Share this book

Categories

Author

About the Author

Mark Watson

Mark Watson is a consultant specializing in LLMs, deep learning, machine learning, knowledge graphs, and general artificial intelligence software development. He uses Common Lisp, Clojure, Python, Java, Haskell, and Ruby for development.

He is the author of 20+ published books on Artificial Intelligence, Deep Learning, Java, Ruby, Machine Learning, Common LISP, Clojure, JavaScript, Semantic Web, NLP, C++, Linux, and Scheme. He has 55 US Patents.

Mark's consulting customer list includes: Google, Capital One, Olive AI, CompassLabs, Disney, Sitescout.com, Embed.ly, and Webmind Corporation.

Mark wrote ten traditional published books for McGraw Hill, Springer Verlag, J Wiley, and Morgan Kaufman publishers before adopting the LeanPub self-publishing platform.

The Leanpub Podcast

Podcast

Podcast Episode

Contents

Table of Contents

Little Bits of Lean 4

Preface

  1. What’s Up With Lean 4?

Introduction

  1. Installing Lean on macOS
  2. Running the examples
  3. What each chapter needs

A Short Tour of Lean 4

  1. Functions
  2. #eval
  3. Lists, arrays, and tuples
  4. String helpers
  5. Structures
  6. Type classes and instances
  7. abbrev and dot calls
  8. Coercions
  9. Inductive types and pattern matching
  10. Option and do
  11. More monads: Except and StateM
  12. Loops and local variables
  13. Typing the symbols
  14. Numbers
  15. Sorting and exact fractions
  16. Bool and Prop
  17. Proofs
  18. Programs
  19. A helper you will see again
  20. Cheat sheet

Bayesian Inference

  1. The Code
  2. How It Works
  3. Checking the Numbers
  4. Lean 4 Notes
  5. Running It
  6. Using It

A Tiny Triple Store

  1. The Code
  2. How It Works
  3. Lean 4 Notes
  4. Running It
  5. Using It

Ranking Documents with BM25

  1. The Code
  2. How It Works
  3. Checking a Score
  4. Lean 4 Notes
  5. Running It
  6. Using It

Extractive Summarization

  1. The Code
  2. How It Works
  3. Checking a Score
  4. Lean 4 Notes
  5. Running It
  6. Using It

A Tiny Vector Store

  1. The Code
  2. How It Works
  3. The Geometry
  4. Lean 4 Notes
  5. Running It
  6. Using It

Associative Memory with Hopfield Networks

  1. The Code
  2. How It Works
  3. Lean 4 Notes
  4. Running It
  5. Testing Recall
  6. Using It

Symbolic Polynomial Calculus

  1. The Code
  2. How It Works
  3. Multiplication
  4. Lean 4 Notes
  5. Running It
  6. Using It

Automatic Differentiation

  1. The Code
  2. How It Works
  3. Checking the Gradients
  4. Lean 4 Notes
  5. Running It
  6. Using It

On-Device AI with Apple Foundation Models

  1. The Code
  2. How It Works
  3. The fm Command
  4. Lean 4 Notes
  5. Running It
  6. Using It

JavaScript-Rendered Pages with Lightpanda

  1. Dump Formats
  2. The Code
  3. How It Works
  4. Lean 4 Notes
  5. Running It
  6. Using It

Local LLMs with Ollama

  1. The Code
  2. How It Works
  3. The Reply
  4. Multi-turn Chat
  5. Generation Options
  6. Lean 4 Notes
  7. Running It
  8. Using It

Calling Google Gemini

  1. The Code
  2. How It Works
  3. Keeping the Key Private
  4. The Response
  5. Generation Options
  6. Lean 4 Notes
  7. Running It
  8. Using It

Querying Knowledge Graphs with SPARQL

  1. The Code
  2. How It Works
  3. The Result Format
  4. Lean 4 Notes
  5. Running It
  6. Using It

Web Search with Exa and Brave

  1. The Two APIs
  2. The Code
  3. How It Works
  4. Lean 4 Notes
  5. Running It
  6. Using It

From One File to a Lake Project

  1. Modules and Files
  2. Adding an Executable
  3. A Project for This Book
  4. Troubleshooting

Making Stand Alone Programs With Lean 4

  1. Four Steps With Lake
  2. What the Build Leaves Behind
  3. Building One File by Hand
  4. Start-Up Time
  5. A Command-Line Tool That Calls C
  6. Linking a System Library
  7. Shipping the Binary
  8. Try This

Errors You May See

  1. A reserved word as a name
  2. A line that starts with a minus sign
  3. A field of an unknown type
  4. A missing default value
  5. An import after code
  6. An identifier from a missing import
  7. A Nat where a Float is expected
  8. A missing match case
  9. A subtraction that floors at zero
  10. Summary
  11. Reading an Error

Where to Go Next

  1. Properties to Prove
  2. A First Proof
  3. Tactics and Mathlib

Use Cases: Mathematical Discovery, Verification, and AI

  1. Why a Checked Proof Means Something
  2. Proving Things About Programs
  3. Verification in Production
  4. Mathlib
  5. Lean and AI
  6. What This Means for You

Also by the Author

Also by the Author

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub