<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta>
      <journal-title-group>
        <journal-title>N. Cavalleri)
 https://sites.google.com/site/anthonybordg/ (A. Bordg)</journal-title>
      </journal-title-group>
    </journal-meta>
    <article-meta>
      <title-group>
        <article-title>Elements of Diferential Geometry in Lean</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>A Report for Mathematicians</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Anthony Bordg</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Nicolò Cavalleri</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>University College London, Dept. of Mathematics</institution>
          ,
          <addr-line>Gower Street, London WC1E 6BT</addr-line>
          ,
          <country country="UK">UK</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>University of Cambridge, Dept. of Computer Science and Technology</institution>
          ,
          <addr-line>15 JJ Thomson Avenue, Cambridge, CB3 0FD</addr-line>
          ,
          <country country="UK">UK</country>
        </aff>
      </contrib-group>
      <pub-date>
        <year>2022</year>
      </pub-date>
      <volume>000</volume>
      <fpage>0</fpage>
      <lpage>0003</lpage>
      <abstract>
        <p>We report on our experience formalizing diferential geometry with mathlib, the Lean mathematical library. Our account is geared towards geometers with no knowledge of type theory, but eager to learn more about the formalization of mathematics and maybe curious enough to give Lean a try in the future. To this efect, we stress the possibly surprising diference between the formalization and its pen-and-paper counterpart arising from Lean's treatment of equality. Our three case studies are Lie groups, vector bundles and the Lie algebra of a Lie group.</p>
      </abstract>
      <kwd-group>
        <kwd>eol&gt;formal mathematics</kwd>
        <kwd>dependent type theory</kwd>
        <kwd>Lean</kwd>
        <kwd>diferential geometry</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
      <p>
        The logical system underlying the Lean theorem prover is known as an intensional dependent
type theory [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]. We will focus on one of the most subtle notions in such a system: equality. To
learn more on Lean’s dependent type theory, the reader should refer to [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ].
      </p>
      <sec id="sec-1-1">
        <title>1.1. Equality in Lean</title>
        <p>The singular “equality” in the title actually hides a plural. Indeed, there are multiple notions of
equality in Lean, most notably the so-called definitional equality and the so-called propositional
equality. Some traces of this distinction can be found in traditional mathematics with the
“defined to be equal to” symbol := and with the non-trivial equalities stated as propositions
which have to be proved. Unsurprisingly, two terms that are definitionally equal in Lean
can be used interchangeably. In order to be able to refer to the equality between two terms
within Lean’s syntax, there exists a second kind of equality, namely the propositional equality.
However, if in pen-and-paper mathematics two expressions which have been proved equal
can now be used interchangeably, blurring the above distinction, in Lean these two notions of
equality are really distinct. Indeed, in Lean two terms that are propositionally equal may not be
definitionally equal. For instance, the equality  + 0 =  is true by definition in Lean, while
0 +  =  is not and as a proposition requires a proof. Of course, definitional equality depends
on implementation, something that mathematicians may not be used to take into account. After
some work, implementation details can to some extent be abstracted away from the user, but
they clearly afect the experience of the developer building an interface for a given mathematical
structure. In pen-and-paper mathematics, mathematical structures are ethereal specifications
without implementations and in the example above natural numbers form a totally ordered
commutative semiring in which the definition of addition as such does not matter. We will see
what kind of unexpected challenges these distinctions pose for the translation of pen-and-paper
mathematics into Lean.</p>
      </sec>
      <sec id="sec-1-2">
        <title>1.2. Diferential Geometry in Mathlib</title>
        <p>Manifolds in Lean are smooth manifolds locally modeled on a model with corners over a
nondiscrete normed field k, i.e. a field equipped with a valuation, namely a real-valued map 
satisfying  () = 0 if and only if  = 0 and</p>
        <p>() =  () (),  ( + ) ≤  () +  ()
for all ,  ∈ k, and in which there exists an element  with  () &lt; 1. The field k can be R
together with the absolute value for instance and in this case in pen-and-paper mathematics
-dimensional manifolds with corners are locally modeled on [0, ∞) × R−  for some  with
0 ≤  ≤ . If smooth manifolds without boundary and manifolds with boundary are well
known, manifolds with corners are comparatively less known. Obviously, manifolds with
corners generalize both boundaryless manifolds (case  := 0) and manifolds with boundary
(case  := 1). While a nondiscrete normed field does not add much complication and allows to
treat both real and complex manifolds at the same time, the set-up of manifolds with corners
in Lean requires some formal machinery as we will briefly see below and using them may be
cumbersome for the casual user. Beyond some basic material, as of June 2021 the Lean library for
diferential geometry lacks more advanced material, like de Rham cohomology and Riemannian
geometry, that could stress-test the design choices made. It will be interesting to follow the
evolution of this library.</p>
      </sec>
    </sec>
    <sec id="sec-2">
      <title>2. Case Studies</title>
      <p>Smooth manifolds in Lean as formalized by Sébastien Gouëzel are modeled on a model with
corners, denoted I : model_with_corners k E H in Lean. Given a model vector space 
and a model space , a model with corners is defined as a map  :  →  embedding nicely 
into  with respect to which the transition functions, which are maps from  to , are smooth.
In the case where k := R, one example of a model with corners is the embedding of [0, ∞)
into R. In the trivial case where the map  is the identity denoted id, embedding  into itself,
there is in Lean a trivial model with corners denoted model_with_corners_self k E. In
the case of boundaryless manifolds we shall see the complications arising from Lean’s treatment
of equality. In this case one would expect to work only with the trivial model with corners, but
this is not the case. Indeed, assume that we have two model vector spaces  and  . There are
two natural models with corners on  ×  , namely the identity id and the product id × id,
namely the map ( p : E × F, (p.1, p.2)), where p.1 and p.2 denote the first and the
second projection, respectively. These two maps are not definitionally equal, hence we have to
indicate to Lean which one we want to use. More precisely, the map id arises from considering
 ×  itself as a boundaryless manifold, while the map id × id arises from considering
 ×  as the product of two (boundaryless) manifolds, this product being denoted model_prod
in the library. This exemplifies well the kind of subtleties involved with the equality in Lean.
As a result, for boundaryless manifolds a dedicated property1 had to be introduced by Gouëzel.
This property, simply ensuring that a manifold has no boundary, has an unspecified model with
corners to maximize the applicability of its theorems.</p>
      <sec id="sec-2-1">
        <title>2.1. Lie Groups</title>
        <p>In this subsection we will see in the case of Lie groups the consequences of the design choices
for smooth manifolds outlined above. First, given the set-up of manifolds in Lean, a Lie group
in Lean is a priori a manifold with corners. Second, in the case of Lie groups one would
expect to use the trivial model with corners given by the identity map id, namely the model
model_with_corners_self. However, the product of two manifolds  and  ′ in the library
requires to use model_prod H H’ as the model space of  ×  ′, where  and ′ are the
respective model spaces of  and  ′. As a consequence, using the trivial model with corners
for formalizing Lie groups would not make Lie groups stable under products, i.e. it would not be
possible to prove that the product of two Lie groups is again a Lie group. Indeed, assume that
 :=  and  ′ := ′ are two Lie groups, each endowed with the trivial model with corners,
hence  (resp. ′) is  (resp. ′) and both  and ′ are id, then the product  × ′ would
be endowed with the model with corners id × id, which is not definitionally equal to id as
explained previously, so  × ′ would not be a Lie group if Lie groups were defined using the
trivial model with corners. Thus, the definition of a Lie group has to be generalized, maybe
awkwardly, allowing a model with corners where the model space and the model vector space
can be distinct. We give below the code for our definition of Lie groups for the curious reader
who wants to know what Lie groups look like in Lean, but we do not expect them to be able to
make sense of the code.
@[ancestor has_smooth_mul, to_additive]
class lie_group {k : Type*} [nondiscrete_normed_field k]
{H : Type*} [topological_space H]
{E : Type*} [normed_group E] [normed_space k E] (I :</p>
        <p>model_with_corners k E H)
(G : Type*) [group G] [topological_space G] [charted_space H G]
extends has_smooth_mul I G : Prop :=
(smooth_inv : smooth I I ( a:G, a− 1))
We then proceed to prove without any dificulty that the product of two Lie groups is again a
Lie group.</p>
        <sec id="sec-2-1-1">
          <title>1Technically, something called a type class in Lean’s type theory.</title>
        </sec>
      </sec>
      <sec id="sec-2-2">
        <title>2.2. Vector Bundles</title>
        <p>One could define a vector bundle in Lean as a surjective continuous map  :  →  satisfying
standard conditions [3, chap. 3]. However, in case of a naive implementation of this definition
the fibers of the tangent bundle would not be definitionally equal to the tangent spaces, only
isomorphic to them. This is perfectly acceptable for the working mathematician, but now the
Lean developer would have two isomorphic copies of the tangent space at a given point. As
a consequence, our developer would have to transport theorems about these copies along an
isomorphism, something which is never carried out explicitly on paper, since mathematicians
treat isomorphic objects as equal. Thus, following a suggestion of Mario Carneiro we defined
instead a topological vector bundle as a map E : B → Type*, i.e. a family of types, the fibers,
indexed by a base space , such that each () is a topological vector space and around every
point there is a local trivialization which is linear in the fibers. This definition of vector bundles
has the merit of providing a more direct way of talking about fibers, avoiding to deal with fibers
only isomorphic to tangent spaces. Thus, topological vector bundles translate into Lean as
follows.
class topological_vector_bundle (R : Type*) {B : Type*} (F : Type*) (E
: B → Type*)
[semiring R] [∀ x, add_comm_monoid (E x)] [∀ x, module R (E x)]
[topological_space F] [add_comm_monoid F] [module R F]
[topological_space (total_space E)] [topological_space B] : Prop :=
(inducing [] : ∀ (b : B), inducing ( x : (E b), (id ⟨b, x⟩ :
total_space E)))
(locally_trivial [] : ∀ b : B, ∃ e : topological_vector_bundle.</p>
        <p>trivialization R F E, b ∈ e.base_set)</p>
      </sec>
      <sec id="sec-2-3">
        <title>2.3. The Lie Algebra of a Lie Group</title>
        <p>2.3.1. Derivations of the Algebra of Smooth Functions on a Manifold
We already touched on the isomorphism-as-identity problem in the previous section. We
will give one additional incarnation of this problem to exemplify how pervasive it is in the
formalization of mathematics with proof assistants. Given a real smooth manifold  , it is
known2 that Der(∞( )), the real vector space of derivations of the algebra ∞( ) of
smooth functions on  , is isomorphic to Γ ∞(,   ), the real vector space of smooth vector
ifelds on  .</p>
        <p>
          Γ ∞(,   ) ∼= Der(∞( ))
This isomorphism is an isomorphism in the category of Lie algebras over R. Since derivations of
the algebra of smooth functions is a more algebraic theory, it is particularly well suited for the
dependent type theory of Lean and we chose to follow this approach as shown in the snippet of
code below.
2Cf. https://ncatlab.org/nlab/show/derivations+of+smooth+functions+are+vector+fields
@[protect_proj]
structure derivation (R : Type*) (A : Type*) [comm_semiring R] [
comm_semiring A]
[algebra R A] (M : Type*) [add_cancel_comm_monoid M] [module A M] [
module R M]
[is_scalar_tower R A M]
extends A → [R] M :=
(leibniz’ (a b : A) : to_fun (a * b) = a · to_fun b + b · to_fun a)
However, in some situations one might prefer to work with vector fields and even once the
above isomorphism has been proved, it would remain to transfer along the isomorphism
properties from one side to the other and this would not be handled automatically by Lean.
This is a significant diference between Lean and the practice of pen-and-paper mathematics
where isomorphic objects are treated as equal. It is worth noting that in some dependent type
theories like Voevodsky’s Univalent Foundations [
          <xref ref-type="bibr" rid="ref4">4</xref>
          ] this problem has been eased3. Indeed, in
the Univalent Foundations, given two objects of some category of structured sets there is an
equivalence between the type of their equalities and the type of their isomorphisms [
          <xref ref-type="bibr" rid="ref5">5</xref>
          ], ensuring
that properties can always be transferred between isomorphic objects by invoking appropriate
theorems. Despite this advantage, the treatment of equality in the Univalent Foundations is
even more subtle than in Lean, since the equality between two objects is not a mere proposition
but it has the structure of an ∞-groupoid.
2.3.2. Left-Invariant Derivations and the Lie Algebra of a Lie Group
We formalized in Lean the property of being left-invariant for derivations which is analogous
to the property of being left-invariant for vector fields. In other words, this property of being
left-invariant commutes with the aforementioned isomorphism, i.e. a derivation is left-invariant
if and only if its image under this isomorphism is a left-invariant vector field. We encountered a
notable subtlety during the formalization of left-invariant derivations that we shall explain. Let 
be a Lie group and  be any element of . The symbol  will denote the left translation of  and
 its identity. Actually,  and  are not definitionally equal in Lean, only propositionally equal.
As a result, the tangent spaces  and  are not definitionally equal, only propositionally
equal. This mismatch is not only a problem for defining the property of being left-invariant,
but it could also be a problem in the future if one wants to prove for instance that every Lie
group is parallelizable. Indeed, to prove that every Lie group is parallelizable, one will have to
move around a basis of  using the left translations ’s to get a basis on each vector space
, hence one will also have to address the above mismatch in this case. At this point we have
to tell the reader a last surprising fact. There are not only two kinds of equalities in Lean as
presented in the introduction, but there is a third equality, the so-called heterogeneous equality
(heq), which can be used to handle the kind of mismatches described above, e.g. between 
and . However, heq, denoted == in Lean syntax, despite being an equivalence relation is
strictly weaker than propositional equality and has notorious limitations. For instance, given
two functions  and  and an element  in their domain, f == g does not necessarily imply
3Lean is not compatible with the Univalent Foundations since its third version, Lean 3.
f x == g x [6, section 3]. As a consequence, the use of heq often gives rise to some dificulties
informally referred to as “heq hell” in the Lean community. Thus, we decided not to use heq
and Oliver Nash suggested another way around our problem. Briefly, we introduced a new
diferential for derivations, an heterogeneous diferential, which takes as arguments not only
a smooth function  and a point  on the given manifold, but also a point  and a proof of
equality between  () and . This enables us to replace  with  everywhere and our
problem vanishes. This heterogeneous diferential has been used only for derivations so far and
a more standard, homogeneous, diferential is used throughout the library. Last, we eventually
proved that the left-invariant derivations form a Lie algebra. In particular, in the real case the
left-invariant derivations are an implementation of the Lie algebra of a Lie group.
instance : lie_algebra k (left_invariant_derivation I G) :=
{ lie_smul :=  r Y Z, by { ext1, simp only [commutator_apply,
map_smul, smul_sub, coe_smul,
        </p>
        <p>pi.smul_apply] } }</p>
      </sec>
    </sec>
    <sec id="sec-3">
      <title>3. Concluding Thoughts</title>
      <p>There are hints that the approach taken for formalizing manifolds in Lean is scalable, though
much remains to be done to extend the library which currently lacks an array of nontrivial
examples for the notions already implemented. Regarding Lean itself we have to point out that
the level of automation is not on a par with what most mathematicians would expect. Moreover,
the fact that every term belongs to a type creates some dificulties that a mathematician will
hardly suspect. Indeed, if Lean type class inference mechanism allows to see automatically a
Lie group as a group or a Lie group as a smooth manifold, one needs the so-called coercions
to see for instance a smooth map as a map. These coercions clutter the code and render it
unnecessarily verbose from the mathematician’s perspective always equating an object with
its image under a forgetful functor or a canonical inclusion. A mathematician may also regret
the limited support for notations that makes the code verbose and hard to decipher as well
as the absence of LATEX support to comment extensively the code with formulas or diagrams.
Also, given its sophisticated type theory Lean has problably a steep learning curve for most
mathematicians and the cryptic messages given in the user interface whenever an error occurs
are not of much help to the beginner. Finally, if mathematicians think they know what equality
is, implementing their favorite mathematical structures in Lean may force them to revise this
position.</p>
    </sec>
    <sec id="sec-4">
      <title>Acknowledgments</title>
      <p>This work was supported by the ERC Advanced Grant ALEXANDRIA (Project GA 742178) and
the Cambridge Mathematics Placements Programme from the Faculty of Mathematics of the
University of Cambridge. We thank the maintainers of mathlib, as well as Oliver Nash, who
took an active part in reviewing our code after it was submitted to mathlib. Among the mathlib
maintainers we thank in particular Sébastien Gouëzel. We also thank one anonymous reviewer
for their relevant remarks.</p>
    </sec>
    <sec id="sec-5">
      <title>Supplement Material</title>
      <sec id="sec-5-1">
        <title>Our formal development is now part of mathlib, see our code on</title>
        <p>• Lie groups,
• Topological vector bundles,
• Lie algebra of a Lie group.</p>
      </sec>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>T.</given-names>
            <surname>Streicher</surname>
          </string-name>
          ,
          <article-title>Investigations into intensional type theory</article-title>
          , Habilitation, Ludwig Maximilian Universität,
          <year>1993</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>J.</given-names>
            <surname>Avigad</surname>
          </string-name>
          , L. de Moura, S. Kong, Theorem proving in Lean,
          <year>2021</year>
          . URL: https://leanprover. github.io/theorem_proving_in_lean/,
          <source>release 3.23.0.</source>
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [3]
          <string-name>
            <given-names>M.</given-names>
            <surname>Spivak</surname>
          </string-name>
          , A Comprehensive Introduction to Diferential Geometry, vol.
          <volume>1</volume>
          , third ed.,
          <source>Publish or Perish Inc</source>
          .,
          <year>1999</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <given-names>Univalent</given-names>
            <surname>Foundations</surname>
          </string-name>
          <string-name>
            <surname>Program</surname>
          </string-name>
          ,
          <source>Homotopy Type Theory: Univalent Foundations of Mathematics</source>
          , first ed., http://homotopytypetheory.org/book/,
          <year>2013</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <given-names>T.</given-names>
            <surname>Coquand</surname>
          </string-name>
          ,
          <string-name>
            <given-names>N. A.</given-names>
            <surname>Danielsson</surname>
          </string-name>
          , Isomorphism is equality,
          <source>Indagationes Mathematicae</source>
          <volume>24</volume>
          (
          <year>2013</year>
          )
          <fpage>1105</fpage>
          -
          <lpage>1120</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6]
          <string-name>
            <given-names>D.</given-names>
            <surname>Selsam</surname>
          </string-name>
          , L. d. Moura,
          <article-title>Congruence closure in intensional type theory</article-title>
          ,
          <source>in: International Joint Conference on Automated Reasoning</source>
          , Springer,
          <year>2016</year>
          , pp.
          <fpage>99</fpage>
          -
          <lpage>115</lpage>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>