Skip to content

Latest commit

 

History

History
166 lines (126 loc) · 6.4 KB

File metadata and controls

166 lines (126 loc) · 6.4 KB

Lean 4 Workshop

https://github.com/aygp-dr/lean4-workshop/actions/workflows/ci.yml/badge.svg https://img.shields.io/badge/Lean-4.23.0-blue.svg https://img.shields.io/badge/license-MIT-green.svg https://img.shields.io/github/stars/aygp-dr/lean4-workshop.svg https://img.shields.io/badge/docs-org--mode-blueviolet.svg

Overview

A hands-on workshop for learning Lean 4, the functional programming language and interactive theorem prover.

This repository contains:

  • Learning resources and tutorials
  • Example proofs demonstrating Lean 4 concepts
  • Makefile for common operations

Quick Start

Prerequisites

  • Lean 4 (v4.23.0 or later)
  • Lake (comes with Lean 4)
  • Optional: elan (version manager)

Check Your Setup

gmake deps    # Verify Lean 4 toolchain
gmake check   # Run a quick hello world test

Repository Structure

lean4-workshop/
├── README.org           # This file
├── RESOURCES.org        # Learning resources and tutorials
├── Makefile             # Build and check commands
├── lean-toolchain       # Lean version pinning
├── examples/            # Runnable examples
│   ├── satirical/       # Why specifications matter (5 files)
│   ├── correct/         # Proper specifications
│   ├── fp/              # Functional programming
│   │   ├── 01_OptionMonad.lean  # Option type and safe operations
│   │   ├── 02_IOMonad.lean      # IO and side effects
│   │   ├── 03_StateMonad.lean   # Stateful computations
│   │   ├── 04_TypeClasses.lean  # Polymorphism and overloading
│   │   └── 05_Recursion.lean    # Recursion patterns
│   ├── proofs/          # Theorem proving
│   │   ├── 01_Logic.lean        # Propositional logic
│   │   ├── 02_Equality.lean     # Equality and rewriting
│   │   └── 03_Induction.lean    # Inductive proofs
│   └── data/            # Data structures
│       ├── 01_Structures.lean   # Records and products
│       ├── 02_Arrays.lean       # Efficient arrays
│       └── 03_Inductive.lean    # Custom inductive types
├── exercises/           # Workshop exercises
│   ├── Ex01_Basics.lean          # rfl, simp, exact
│   ├── Ex02_NaturalNumbers.lean  # induction, arithmetic
│   ├── Ex03_Lists.lean           # list operations
│   └── Solutions/                # Complete solutions
├── .github/workflows/   # CI configuration
├── AGENTS.md            # Issue tracking instructions
└── .beads/              # Issue tracking database

Learning Path

1. Interactive Games (Recommended Start)

GameDescriptionLink
Natural Number GameProve 2+2=4 and learn inductionPlay NNG4
Set Theory GameLearn set operationsLean Game Server
RoboStory-based math puzzlesLean Game Server

2. Books and Tutorials

ResourceAudienceLink
Functional Programming in LeanProgrammersFPIL
Theorem Proving in Lean 4Proof enthusiastsTPIL
Mathematics in LeanMathematiciansMIL
Learn X in Y MinutesQuick referenceLean4 in Y

See RESOURCES.org for the complete list.

3. Satirical Examples: Why Specifications Matter

The examples/satirical/ directory contains “O(1)” algorithms that satisfy weak specifications but don’t actually work:

ExampleThe TrickLesson
O1SortIdentity functionPermutation spec must check elements
O1SearchReturn first elementMust verify we found the TARGET
O1HashReturn lengthDeterminism alone is useless
O1CompressReturn empty listMust require decompression
O1EncryptIdentity functionReversibility without confidentiality
-- From examples/satirical/01_O1Sort.lean
def sort {α : Type} : List α → List α := id

-- WARNING: This is INTENTIONALLY WRONG!
def isPermutation : List α → List α → Bool
  | xs, ys => xs.length == ys.length  -- Only checks length!

The examples/correct/ directory shows how specifications should be written.

Moral: Formal verification proves your code matches your spec. It doesn’t prove your spec is what you actually wanted.

Editor Setup

VS Code (Recommended)

Install the lean4 extension from the marketplace.

Emacs

;; Using straight.el + use-package
(use-package lean4-mode
  :straight (lean4-mode
             :type git
             :host github
             :repo "leanprover-community/lean4-mode"
             :files ("*.el" "data")))

Key binding: C-c C-i toggles the proof state view.

Makefile Targets

TargetDescription
helpShow available targets
depsCheck Lean 4 toolchain
checkVerify installation with hello world
buildBuild project with Lake
cleanRemove build artifacts
verifyVerify all Lean files compile
verify-examplesCheck satirical and correct examples
verify-exercisesCheck exercises and solutions

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes (org-mode files only)
  4. Submit a pull request

Community

License

MIT License - See LICENSE file for details.

Acknowledgments