Skip to content

Commit 49ea836

Browse files
authored
update instructions for external projects (#237)
1 parent afeb177 commit 49ea836

File tree

5 files changed

+197
-17
lines changed

5 files changed

+197
-17
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ MATHBIN_COMMIT=origin/master
4343
mathbin-source:
4444
mkdir -p sources
4545
if [ ! -d "sources/mathlib/.git" ]; then \
46-
cd sources && git clone https://github.com/leanprover-community/mathlib.git; \
46+
cd sources && git clone --depth 1 https://github.com/leanprover-community/mathlib.git; \
4747
fi
4848
cd sources/mathlib && git clean -xfd && git fetch && git checkout "$(MATHBIN_COMMIT)" --
4949
cd sources/mathlib && echo -n 'mathlib commit: ' && git rev-parse HEAD
@@ -56,7 +56,7 @@ mathbin-source:
5656
lean3-source: mathbin-source
5757
mkdir -p sources
5858
if [ ! -d "sources/lean/.git" ]; then \
59-
cd sources && git clone https://github.com/leanprover-community/lean.git; \
59+
cd sources && git clone --depth 1 https://github.com/leanprover-community/lean.git; \
6060
fi
6161
cd sources/lean && git clean -xfd && git checkout "`cd ../mathlib && lean --version | sed -e "s/.*commit \([0-9a-f]*\).*/\1/"`" --
6262
mkdir -p sources/lean/build/release

Oneshot/README.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ This is a template which you can use to quickly play with the translation of sni
88
```
99
2. get in the folder: `cd mathport`
1010
3. install dependencies `cmake`, `gmp`, `gmp-devel`, `jq` if needed (debian/ubuntu `sudo apt install cmake gmp gmp-devel jq`, mac `brew install cmake jq`, etc)
11-
4. Run `make build`
12-
5. Run `make lean3-source`
13-
6. Get a mathport release with `./download-release.sh $my_choice`, where $mychoice = `nightly-2022-12-13-04` works, **or just** `./download-release.sh` to get the latest one automatically.
14-
7. Put Lean 3 code in `Oneshot/lean3-in/main.lean`.
15-
8. Put extra `#align` in `Oneshot/lean4-in/Oneshot.lean`
16-
9. Run `make oneshot`
11+
4. Run `lake exe cache get`
12+
5. Run `make build` (go get some coffee!)
13+
6. Run `make lean3-source`
14+
7. Get a mathport release with `./download-release.sh $my_choice`, where $my_choice = `nightly-2022-12-13-04` works, **or just** `./download-release.sh` to get the latest one automatically.
15+
8. Put Lean 3 code in `Oneshot/lean3-in/main.lean`.
16+
9. Put extra `#align` in `Oneshot/lean4-in/Oneshot.lean`
17+
10. Run `make oneshot`
1718

18-
After the first run, you only have to repeat steps 7-9, unless you want to update mathlib (step 6) or mathport itself (`git pull` and then steps 4-9).
19+
After the first run, you only have to repeat steps 8-10, unless you want to update mathlib (step 7) or mathport itself (`git pull` and then steps 4-10).
20+
21+
If things work, at the end you should see
22+
`# output is in Outputs/src/oneshot/Oneshot/Main.lean`.
23+
You may need to add `import Mathlib.Mathport.Rename` to this file
24+
before the `#align` commands work.
25+
Also, all you mathlib imports will say `import Mathbin.XYZ`,
26+
and need to be changed to `import Mathlib.XYZ`.

README.md

+106-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,103 @@ Mathport is a tool for porting Lean3 projects to Lean4. It consists of two (loos
55
- "binport", which translates Lean3 `.lean` files to Lean4 `.olean` files
66
- "synport", which best-effort translates Lean3 `.lean` files to Lean4 `.lean` files
77

8+
## Running on mathlib
9+
10+
If you want to port a file from mathlib to mathlib4, you don't need anything here.
11+
Instead you should go to the mathlib4 repository, and use `scripts/start_port.sh`.
12+
13+
## Running on a single file
14+
15+
Mathport supports a "oneshot" mode, for porting individual files or quick tests.
16+
A template is set up in the `Oneshot/` directory. See [`Oneshot/README.md`](Oneshot/README.md).
17+
18+
## Running on a project other than mathlib
19+
20+
Install dependencies if needed:
21+
22+
- debian/ubuntu: `sudo apt install cmake gmp gmp-devel jq`
23+
- mac: `brew install cmake jq`
24+
25+
In the mathport folder:
26+
27+
- `lake exe cache get`
28+
- `make build`
29+
This step is somewhat expensive as we need to compile (not just build)
30+
all of the dependencies of tactics.
31+
- `make source`
32+
- `./download-release.sh`
33+
(Or `./download-release.sh [relevant-release]`
34+
if you need to run against an old mathlib;
35+
see [mathport releases](https://github.com/leanprover-community/mathport/releases),
36+
and find the nearest `nightly`.)
37+
38+
Make sure that your project is using the same version of Lean 3 as the latest
39+
mathlib3, if at all possible.
40+
Similarly bump your mathlib dependency to the lastest mathlib3 if possible.
41+
42+
If you really want to run against an older mathlib3 (good luck!):
43+
44+
- In `sources/mathlib` run `git --fetch unshallow`
45+
- `git checkout SHA` for the mathlib3 SHA you need.
46+
- `leanproject get-cache`
47+
48+
Next, you'll need to prepare your project.
49+
It is probably best to clone a fresh copy for this
50+
(outside of the `mathport` directory).
51+
52+
In your project, run:
53+
54+
- `leanproject mk-all`
55+
- make sure that `leanproject build` runs cleanly.
56+
- In `leanpkg.path`, change the line `path _target/deps/mathlib/src` to
57+
`path ../mathport/sources/mathlib/src`
58+
(this should be the relative path from your project
59+
to mathport's copy of `mathlib/src`.)
60+
- `leanproject clean`
61+
- `lean --make --recursive --ast --tlean src` (get coffee).
62+
63+
Now, in mathport, edit `config-project.json` as follows:
64+
65+
- Under `pathConfig/packages/`, change the string after the `"Project"` key
66+
to the relative path from the mathport directory to your
67+
external project's `src/` directory.
68+
69+
Then run
70+
71+
- `./build/bin/mathport --make config-project.json Project::all`
72+
73+
If it succeeds, you should find Lean4 lean files in `Outputs/src/project/`.
74+
(Note this may be hidden in the VS Code explorer,
75+
but you can open it in the terminal.)
76+
77+
If you need to re-run mathport, you will need to `rm -rf Outputs/olean/project`.
78+
79+
If the generated Lean files look plausible,
80+
you will want to move them into a new Lean 4 project.
81+
82+
Somewhere outside of the `mathport` folder, run
83+
`lake +leanprover/lean4:nightly-2023-05-22 init MyProject math`
84+
(probably updating `nightly-2023-05-22`
85+
to match the toolchain in current `mathlib4`).
86+
Then you can `mv Outputs/src/project/Project ../MyProject/MyProject`,
87+
and then inside `MyProject` run `lake update` and `lake exe cache get`.
88+
89+
You will need to edit the imports to change
90+
`import Mathbin.XYZ` to `import Mathlib.XYZ`.
91+
92+
Depending on the layout of your project, you may need to adjust imports
93+
or create secondary libraries in your `lakefile.lean`, e.g.
94+
```
95+
lean_lib ForMathlib where
96+
roots := #[`ForMathlib]
97+
```
98+
99+
After that you should be able to edit files in VS Code,
100+
and begin fixing remaining errors.
101+
102+
Please expect errors if any of your filenames require escaping via `«...»`.
103+
(In particular if your filenames start with numerals.)
104+
8105
## Running with artifacts from continuous integration
9106

10107
A full run of `mathport` (see below) on Lean 3 and mathlib3 takes several hours.
@@ -16,19 +113,22 @@ Please use the repositories
16113
https://github.com/leanprover-community/lean3port
17114
and
18115
https://github.com/leanprover-community/mathlib3port
19-
and run `lake build` to obtain the generated `.olean` files.
116+
and run `lake exe cache get` and then
117+
`lake build` to obtain the generated `.olean` files.
20118

21119
Using these repositories, you can open the synported `.lean` files in VS Code
22120
to see the current state of output.
23121

24122
Alternatively, you can import some or all of the binported `.olean` files
25123
using e.g.
26-
```
124+
125+
```lean
27126
import Mathbin.AlgebraicGeometry.Scheme
28127
29128
#lookup3 algebraic_geometry.Scheme
30129
#check AlgebraicGeometry.Scheme
31130
```
131+
32132
(Specifying the `mathlib3port` repository as a Lake dependency in your own
33133
project should work to enable `import Mathbin.All`.)
34134

@@ -48,7 +148,10 @@ where `<tag>` is a release from https://github.com/leanprover-community/mathport
48148
## Running mathport locally
49149

50150
See the `Makefile` for usage (it takes several hours to rebuild the mathlib3 port from scratch).
51-
Basic usage is `make build source predata port`.
151+
Basic usage is `lake exe cache get` and then `make build source`.
152+
153+
After that you can try just `make predata port`,
154+
but you probably should download artifacts first as described below.
52155

53156
We provide artifacts for various stages of the build on the releases page of the `mathport` repository.
54157
The script `./download-release.sh nightly-YYYY-MM-DD` downloads one of these,
@@ -68,5 +171,3 @@ To port a single file execute `mathport` as follows
68171

69172
The directory `Test` contains subdirectories `importLeanBin` and `importMathbin`,
70173
each containing a `lakefile.lean` that depends on `lean3port` and `mathlib3port`, resp.
71-
72-
Mathport also supports a "oneshot" mode, for quick tests. A template is set up in the `Oneshot/` directory. See [`Oneshot/README.md`](Oneshot/README.md).

config-project.json

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"pathConfig": {
3+
"outRoot": "Outputs",
4+
"packages": {
5+
"Leanbin": "sources/lean/library",
6+
"Mathbin": "sources/mathlib/src",
7+
"Oneshot": "Oneshot/lean3-in",
8+
"Project": "../lean-liquid/src"
9+
},
10+
"leanPath": [
11+
"./lake-packages/mathlib/build/lib",
12+
"./lake-packages/Qq/build/lib",
13+
"./lake-packages/std/build/lib",
14+
"./lake-packages/aesop/build/lib",
15+
"./Oneshot/lean4-in/build/lib",
16+
"./Outputs/oleans/leanbin",
17+
"./Outputs/oleans/mathbin",
18+
"./Outputs/oleans/oneshot",
19+
"./Outputs/oleans/project"
20+
]
21+
},
22+
"baseModules": ["Mathlib"],
23+
"extraModules": [],
24+
"stringsToKeep": [
25+
"_sunfold",
26+
"equations",
27+
"private",
28+
"below",
29+
"ibelow"
30+
],
31+
"forceAbbrevs": [
32+
"lift",
33+
"lift_t",
34+
"coe_b",
35+
"coe_t",
36+
"coe_fn_b"
37+
],
38+
"defEqConstructions": [],
39+
"defEqConstructions-disabled-because-sometimes-different-in-lean4": [
40+
"casesOn",
41+
"noConfusion",
42+
"ibelow",
43+
"brecOn",
44+
"bInductionOn"
45+
],
46+
"disabledInstances": [
47+
"bool.decidable_eq",
48+
"nat.decidable_eq",
49+
"and.decidable",
50+
"or.decidable",
51+
"iff.decidable",
52+
"not.decidable",
53+
"implies.decidable",
54+
"decidable.true",
55+
"decidable.false",
56+
"exists_prop_decidable",
57+
"forall_prop_decidable",
58+
"int.decidable_eq",
59+
"nat.decidable_lt",
60+
"nat.decidable_le"
61+
],
62+
"neverSorries": [
63+
"of_as_true"
64+
],
65+
"sorries": [],
66+
"skipProofs": false,
67+
"skipDefEq": true,
68+
"replacementStyle": "comment",
69+
"redundantAlign": false,
70+
"error2warning" : true
71+
}

download-predata.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/usr/bin/env bash
22
# Script to download artifacts from a release.
3-
# Usage: `./download-predata.sh nightly-predata-2021-11-30`
3+
# Usage: `./download-predata.sh nightly-2021-11-30`
44

5-
RELEASE=$1 # e.g. nightly-predata-2021-11-30
5+
RELEASE=$1 # e.g. nightly-2021-11-30
66

77
if [ -z "$RELEASE" ]; then
8-
echo "Usage: ./download-predata.sh nightly-predata-YYYY-MM-DD"
8+
echo "Usage: ./download-predata.sh predata-nightly-YYYY-MM-DD"
99
echo "See https://github.com/leanprover-community/mathport/releases for available releases"
1010
exit 1
1111
fi

0 commit comments

Comments
 (0)