Autoformalize your Math

4. The library🔗

In this chapter we are going to set everything up to be able to contribute to an existing Lean project. In particular, we are going to make a contribution to the Banach lattices library. Open the Banach lattices library GitHub repository and keep reading...

4.1. Welcome to GitHub!🔗

When you open the link above, you will see something like this:

GitHub repo

This is the GitHub repository containing the code of the Banach lattices library.

On the upper left, you can see that the owner of the repository is davidmunozlahoz: this is the GitHub account under which the original Banach lattices library lives. Later we will make our own copy of the same repository. The central part of the page shows the folders and files of the repository. You can click on them and inspect their contents directly in the browser. GitHub provides much more than this: tools to discuss changes, report problems, review code, automate tests, and so on. We do not need all of that, and certainly not now. We will introduce the features as we need them.

Below the list of files, GitHub shows the contents of the file README.md. This is a plain text file, written in a simple format called markdown, whose purpose is to present the repository: what the project is, how to install or use it, what its current state is, and anything else that visitors should know before using the code.

4.2. Fork🔗

To be able to contribute to the shared project, you first need your own fork of it.

  1. Go to GitHub and use your email address to create an account (you can also sign up using your Google account).

  2. Go back to the Banach lattices library, and click the Fork button. Leave the default options and click Create fork.

Thus we have a remote copy of the Banach lattices library, but not a local copy yet. To create and manage a local copy, we will need to use git. Before doing that, however, it is convenient to set up another little thing. If you now go ahead, copy the repository, make some changes, and try to upload them to GitHub, how do GitHub's servers know that the person trying to upload the changes is the same as the owner of the repository? They cannot know, unless you sign your changes, and then somehow make GitHub aware that the signature is actually yours. In the next section we set this up, once and for all, so that this signing process is done automatically every time we work on a project.

4.3. Connecting to GitHub with SSH🔗

In this section, we are going to connect to GitHub's servers using the SSH protocol. The official guide on how to do this can be found here.

  1. We are going to create a public/private key pair. But before doing so, we need to check whether there is one already available. Open your terminal and run:

    $
    ls -al ~/.ssh
  2. If you get an error saying that the directory does not exist, or the directory is empty, then you don't have an SSH key and you can go to step 3. If there is a file named id_rsa.pub, id_ecdsa.pub, or id_ed25519.pub, then you already have an SSH key and you can go to step 4.

  3. Run the following command to generate a new SSH key (remember to replace the example email):

    $
    ssh-keygen -t ed25519 -C "your_email@example.com"
    Generating public/private ed25519 key pair.

    It will then prompt you for the file in which to save the key. Press Enter to use the default value. Finally, you will have to type a secure passphrase for the key (you can also leave it empty, but this is NOT a secure practice).

  4. Start the SSH agent:

    $
    eval "$(ssh-agent -s)"
    Agent pid 59566
  5. Add the new private key to the SSH agent:

    $
    ssh-add ~/.ssh/id_ed25519

    We have thus created our public/private key pair to communicate over the SSH protocol. Now it is time to let GitHub know our public key.

  6. Run:

    $
    cat ~/.ssh/id_ed25519.pub

    and copy the output (i.e., your public key).

  7. Go to GitHub, click your profile picture in the upper-right corner, then click "Settings".

  8. In the "Access" section of the sidebar, click "SSH and GPG keys".

  9. Click "New SSH key" or "Add SSH key".

  10. In the "Title" field, add a descriptive label for the new key. For example, if you are using a personal laptop, you might call this key "Personal laptop".

  11. In the "Key" field, paste your public key. Click "Add SSH key".

  12. Finally, we are going to test that you can indeed connect to GitHub's servers. Open your terminal and run:

    $
    ssh -T git@github.com
    The authenticity of host 'github.com (IP ADDRESS)' can't be established.
     ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
     Are you sure you want to continue connecting (yes/no)?

    Type yes, and we are done.

4.4. git enters the scene🔗

In simple terms, git is the command-line tool we use to manage the changes we make to a repository. It is already installed on your system, because it is a dependency of Lean (i.e., Lean needs it to work). We are going to use git to create a local copy of the Banach lattices library and set it up so that this copy is aware of your Banach lattices library fork, and also of the original Banach lattices library repository.

  1. First of all, open the terminal. You can either open your system terminal, as we did in tasting the terminal, or open a terminal inside VS Code (go to Terminal > New Terminal); do whatever feels more natural to you.

  2. Navigate, using cd, to the folder where you want to create a copy of the repository (mind you: when copying the repository, git will create its own folder, called banlat).

  3. Run:

    $
    git clone git@github.com:your-username/banlat.git

    where you have to replace your-username with your GitHub username. Now you have a local copy of the repository in the folder banlat.

  4. Now we are going to make this new repository aware of the original banlat repository. In order to do this, first run cd banlat to go to the repository, and then run:

    $
    git remote add upstream git@github.com:davidmunozlahoz/banlat.git
  5. Run the following:

    $
    git fetch upstream
    git branch --set-upstream-to=upstream/master master

    You should have some faith and skip the explanation of these commands for now.

  6. Same as above:

    $
    git config push.default current
    git config push.autoSetupRemote true

Congratulations! Now you have a local copy of the Banach lattices library, and you have everything configured to start making contributions. But before that, maybe we should do a quick tour of what's inside the library, so that you can navigate the folder more comfortably.

4.5. A quick tour of the library🔗

If you list the contents of the folder banlat using ls (or just open the folder in VS Code with File > Open folder...), you should see something like this:

$
ls
BanLat  AGENTS.md  BanLat.lean lakefile.toml  lake-manifest.json  lean-toolchain  README.md STYLE.md

Let me briefly explain what these files and folders are:

  • BanLat: This is the main folder, containing all the Lean code. We will explore it further below.

  • BanLat.lean: The main Lean file of the project, importing all the files inside the BanLat folder. When you create a new file inside BanLat, you have to import it in BanLat.lean for it to be compiled.

  • lakefile.toml, lake-manifest.json, and lean-toolchain: These files describe how the Lean project is built. The file lean-toolchain says which version of Lean should be used. lakefile.toml is the configuration file for Lake, the build tool used by Lean projects: it says what the project is called, what it depends on, and what should be built. Finally, lake-manifest.json records the exact versions of the dependencies.

  • README.md: We already covered this in the first section of this chapter.

  • AGENTS.md and STYLE.md: These are LLM-related files; we will cover them in the next chapter, when we set up our LLM assistant.

To prevent our computer from taking several hours to compile the whole mathlib, run in your terminal:

$
lake exe cache get

Then build the project with:

$
lake build

If you have the banlat folder already open in VS Code (if not, open it using File > Open folder...), expand the BanLat subfolder. There you will see a bunch of Lean files and further folders (with more and more subfolders and Lean files). Let's click, for instance, on Basic.lean. Wait for the Lean language server to start. When it starts, a Lean InfoView window will open, and you will see something like this:

VS Code Lean file

Navigate the Lean file. You will see how the information in the InfoView window changes, showing what we have in context.

VS Code Lean file infoview

We would now like to contribute to this project. However... we don't know how to write Lean code yet! In fact, we won't need to write almost any Lean, we will just need to read it. The writing will be done by our gracious LLM assistant. It is time to set it up.