Physlib

Learn

Getting Started

Set up Lean and Physlib, then write and check your first Lean results. Start here if you’re new to Lean or to the project. If you’re still deciding how to help, see Ways to Contribute.

Make Your First Contribution

If you know the physics and are new to Lean, you can write an API map explaining a topic and linking to its Lean declarations. The API map walkthrough shows how to draft and check one.

Install Physlib

Physlib is written in Lean. The easiest way to use it is through VS Code and the official Lean 4 extension. Tick each step off as you go.

0 / 5 done

  1. Step 1

    Install VS Code

    VS Code is a lightweight code editor, and the recommended way to work with Lean.

  2. Step 2

    Install the Lean 4 extension

    The official Lean 4 extension opens directly in VS Code. You can also get it from the VS Code Marketplace.

  3. Step 3

    Install Lean

    Open the extension's setup guide and click its Install Lean button. This installs elan, Lean's version manager, together with Git (and curl on Linux). You can skip the guide's other steps, which are about creating new projects.

    If VS Code has not opened automatically
    1. Open VS Code.
    2. Create a new empty file with File › New Text File (Ctrl+N / Cmd+N).
    3. Click the ∀ symbol at the top right of the window.
    4. Select Documentation › Docs: Show Setup Guide…

    You don't need to pick a Lean version. Physlib pins its own, and elan downloads it the first time you build.

  4. Step 4

    Get a copy of Physlib

    Contributions reach Physlib through a fork, your own copy of the repository on GitHub. Sign in to GitHub, fork Physlib, then clone your fork. Replace YOUR-USERNAME with your GitHub username.

    Terminal
    git clone https://github.com/YOUR-USERNAME/physlib.git
    cd physlib
    git remote add upstream https://github.com/leanprover-community/physlib.git
    lake exe get_cache
    lake build

    The upstream remote lets you pull in the latest changes from Physlib. See the GitHub Guide for branches and pull requests.

    Run these in a terminal (VS Code has one under Terminal › New Terminal). get_cache downloads pre-built files, so the build takes minutes instead of much longer. If it fails, lake build still works, just slowly.

    "git: command not found"?

    Git is installed in Step 3, so first restart your terminal and check with git --version. If it is still missing, install it from git-scm.com.

  5. Step 5

    Open Physlib in VS Code

    Use File › Open Folder… and choose the physlib folder. Open the whole folder, not a single file, so Lean can find Physlib and Mathlib.

Something went wrong?

Sometimes things go wrong due to no fault of your own. Physlib or one of its dependencies may have been updated to a new version of Lean. In these cases, run the following in the top-level Physlib directory:

Terminal
rm -rf .lake
lake update
lake exe get_cache
lake build

Or as a single command:

Terminal
rm -rf .lake; lake update; lake exe get_cache; lake build

About Lean

A theorem prover that guarantees correctness.

Lean is an interactive theorem prover where you write mathematical definitions, theorems, and proofs — and the system verifies correctness using type theory, with no gaps or hand-waving.

Increasingly used by AI labs and mathematicians, Lean is now making its way into physics through Physlib.

WicksTheorem.lean
theorem wicks_theorem : (φs : List 𝓕.FieldOp) → 𝓣(ofFieldOpList φs) =
  ∑ (φsΛ : WickContraction φs.length), φsΛ.wickTerm
| [] => by
  rw [timeOrder_ofFieldOpList_nil]
  simp only [map_one, List.length_nil, Algebra.smul_mul_assoc]
  rw [sum_WickContraction_nil]
  simp only [wickTerm_empty_nil]
| φ :: φs => by
Wick's theorem — formally verified in Physlib.

Useful Resources

Writing Results

Things to bear in mind:

  • —It helps if results sit next to results about the same physics.
  • —Informal results and semi-formal results are easier to write if you're new to Lean.
  • —If you make substantial changes to a file, feel free to add yourself to the author list, in alphabetical order by last name.

Definitions

Definitions are used to introduce new concepts or objects in a precise and formal way. In Physlib we give all definitions docstrings.

Lean
/-- For a harmonic oscillator, the Schrodinger Operator corresponding to it
is defined as the function from `ℝ → ℂ` to `ℝ → ℂ` taking

`ψ ↦ - ℏ^2 / (2 * m) * ψ'' + 1/2 * m * ω^2 * x^2 * ψ`. -/
noncomputable def schrodingerOperator (ψ : ℝ → ℂ) : ℝ → ℂ :=
    fun x => - Q.ℏ ^ 2 / (2 * Q.m) * (deriv (deriv ψ) x) + 1/2 * Q.m * Q.ω^2 * x^2 * ψ x

structure, inductive and abbrev also introduce new concepts in a similar way to def.

Lemmas

In Physlib we use lemmas for every theorem which is not a main one. For every lemma you must provide a proof.

Lean
lemma schrodingerOperator_parity (ψ : ℝ → ℂ) :
    Q.schrodingerOperator (parity ψ) = parity (Q.schrodingerOperator ψ) := by
  funext x
  have (ψ : ℝ → ℂ) : (fun x => (deriv fun x => ψ (-x)) x) = fun x => - deriv ψ (-x) := by
    funext x
    rw [← deriv_comp_neg]
  simp [schrodingerOperator, parity, this]

Theorems

Theorems are significant results, but are otherwise exactly the same as lemmas.

Lean
theorem wicks_theorem_normal_order : (φs : List 𝓕.FieldOp) →
    𝓣(𝓝(ofFieldOpList φs)) =
    ∑ (φsΛ : {φsΛ : WickContraction φs.length // ¬ HaveEqTime φsΛ}), φsΛ.1.wickTerm := by
  ...

Informal Definitions

Informal definitions are definitions written in English rather than in Lean. They are given a tag (you can get a tag using lake exe make_tag), and a list of dependencies. The definition itself is written in the docstring. They cannot be used in further results.

Lean
/-- The hubble constant defined in terms of the scale factor
as `(dₜ a) / a`.

The notation `H` is used for the `hubbleConstant`. -/
informal_definition hubbleConstant where
  deps := []
  tag := "6Z2NB"

Informal Lemmas

Informal lemmas are lemmas written in English rather than in Lean. They are given a tag and a list of dependencies. The lemma and its proof are written in the docstring. They cannot be used in further results.

Lean
/-- The time evolution of the hubble parameter is equal to `dₜ H = - H^2 (1 + q)`. -/
informal_lemma time_evolution_hubbleConstant where
  deps := [``hubbleConstant]
  tag := "6Z3BS"

Semi-Formal Results

Semi-formal results are like informal definitions and lemmas except the type of the definition or statement of the lemma is made formal. They are half-way between informal and formal statements. They cannot be used in further results and are also given a tag.

Lean
/-- The force on the classical harmonic oscillator is `- k x`. -/
semiformal_result "6YB2U" force_is_linear (x : ℝ → ℝ) :
  force S x = - S.k • x

TODO Items

TODO items are used to indicate things which are to be done. They consist of a tag and a string statement of what is to be done.

Lean
TODO "6VZHC" "Create a new folder for the damped harmonic oscillator, initially as a place-holder."

Making a New File

Keep related results together. When a contribution needs its own module, put it beside files on the same physics topic.

Try a PhyslibAlpha Module

Create PhyslibAlpha/Particles/StandardModel/GaugeGroup/PhaseProduct.lean in your local copy of Physlib. Paste the example below and replace “Your Name” in the header. The lemma shows that composing two gauge transformations multiplies their U(1) phases.

PhaseProduct.lean
/-
Copyright (c) 2026 Your Name. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Your Name
-/
module

public import Physlib.Particles.StandardModel.GaugeGroup.Basic

/-!
# The U(1) phase of a product

Project a Standard Model gauge transformation onto its U(1) factor.

## i. Overview

An element of the unquotiented gauge group has three components,
in SU(3), SU(2) and U(1). The projection to U(1) is a homomorphism.

## ii. Key results

- `toU1_mul` gives the phase of a product of gauge transformations.

## iii. Table of contents

- A. Composition

## iv. References

* None.
-/

public section

namespace StandardModel.GaugeGroupI

/-!
## A. Composition
-/

/-- Composing two transformations multiplies their U(1) phases. -/
lemma toU1_mul (g h : StandardModel.GaugeGroupI) :
    toU1 (g * h) = toU1 g * toU1 h :=
  toU1.map_mul g h

end StandardModel.GaugeGroupI

Register the module in PhyslibAlpha.lean by adding this line in alphabetical order among the existing imports. Each dot in the module name corresponds to a folder separator in the file path.

PhyslibAlpha.lean
public import PhyslibAlpha.Particles.StandardModel.GaugeGroup.PhaseProduct

From the root of your Physlib checkout, build the new module. Lake checks the imports and the proof.

Terminal
lake build PhyslibAlpha.Particles.StandardModel.GaugeGroup.PhaseProduct

Use Physlib.lean to register modules under Physlib/. The Documentation Guide explains the section headings for a contribution, and the contribution handbook gives the full rules.

Linting

Linting is the process of checking your code for potential errors, stylistic issues, and adherence to coding standards. It helps ensure that your code is clean, consistent, and free from common mistakes.

On making a pull request to Physlib, GitHub workflows will lint your code automatically. You can also check locally:

Linting Locally

Terminal
lake exe lint_all
./scripts/lint/required/lint-style.sh

A pull request must pass both before it can be merged. Commit your changes before running lint-style.sh, and expect to run lint_all more than once. The scripts README describes every linter.