Repository navigation
Expand file tree
/
Copy pathport_lean435.py
More file actions
executable file
·53 lines (45 loc) · 2.53 KB
/
Copy pathport_lean435.py
File metadata and controls
executable file
·53 lines (45 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
"""Rename what Lean 4.35, Mathlib v4.35.0 and Batteries deprecate or move, in the proofs.
usage: scripts/port_lean435.py [file ...] (default: every tracked .lean file)
- `if_pos`, `if_neg`, `if_true`, `if_false`, `dif_pos`, `dif_neg` become `ite_eq_left`,
`ite_eq_right`, `ite_true`, `ite_false`, `dite_eq_left`, `dite_eq_right`;
- Batteries' `RBMap`, `RBSet` and `RBNode` move from namespace `Batteries` to `RBTree`
(`Batteries.Recycling.RBTree`), and `import Batteries.Data.RBMap` becomes that module;
- in a selector bridge `have hsig : transitionSigStr … := by simp […]` followed by a line
`decide +kernel`, the `decide` becomes `<;> decide +kernel`, as EquiVM's
`scripts/proof_skeleton.py` now emits it: 4.35's `simp` closes the string equality itself,
which leaves `decide` with no goal.
The exporter's output, between `-- BEGIN fe-lean-export` and `-- END fe-lean-export`, is left
alone: it changes only by `scripts/sync_exported.py`. Generated block summaries
(`*Blocks_NNN.lean`) are left alone too: they change only by regenerating them. Idempotent, so it
can be rerun on merged files.
"""
import pathlib
import re
import subprocess
import sys
root = pathlib.Path(__file__).resolve().parent.parent
ITE = {'if_pos': 'ite_eq_left', 'if_neg': 'ite_eq_right', 'if_true': 'ite_true',
'if_false': 'ite_false', 'dif_pos': 'dite_eq_left', 'dif_neg': 'dite_eq_right'}
ite = re.compile(r"(?<![\w.'])(" + '|'.join(ITE) + r")\b")
rb = re.compile(r'\bBatteries\.(RBMap|RBSet|RBNode)\b')
sig = re.compile(r'(have hsig : transitionSigStr [^\n]*:= by\n simp \[[^\]]*\]\n) decide \+kernel\n')
block = re.compile(r'(-- BEGIN fe-lean-export: [\w.]+\n.*?-- END fe-lean-export\n)', re.S)
def port(text):
text = ite.sub(lambda m: ITE[m[1]], text)
text = rb.sub(r'RBTree.\1', text)
text = text.replace('import Batteries.Data.RBMap.Lemmas', 'import Batteries.Recycling.RBTree.Lemmas')
text = text.replace('import Batteries.Data.RBMap', 'import Batteries.Recycling.RBTree')
text = re.sub(r'open Batteries \((RB[^)]*)\)', r'open RBTree (\1)', text)
return sig.sub(r'\1 <;> decide +kernel\n', text)
files = sys.argv[1:] or subprocess.check_output(['git', 'ls-files', '*.lean'], cwd=root, text=True).split()
for name in files:
path = root / name
if re.search(r'Blocks_\d+\.lean$', name):
continue
text = path.read_text()
parts = block.split(text)
new = ''.join(p if block.fullmatch(p) else port(p) for p in parts)
if new != text:
path.write_text(new)
print(f'ported {name}')