forked from leanprover-community/mathematics_in_lean
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreplace_examples.py
More file actions
72 lines (53 loc) · 2.35 KB
/
replace_examples.py
File metadata and controls
72 lines (53 loc) · 2.35 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import re
import os
def process_file(file_path):
# Read the import statements from the file
with open(file_path, "r") as f:
import_lines = [line.strip() for line in f.readlines() if line.strip()]
for import_line in import_lines:
# Parse the import statement to extract chapter and section information
match = re.match(
r"import MIL\.C(\d+)_(\w+)\.solutions\.Solutions_S(\d+)_(\w+)", import_line
)
if not match:
print(f"Skipping invalid import line: {import_line}")
continue
chapter_num, chapter_name, section_num, section_name = match.groups()
# Construct the path to the .lean file
lean_file_path = f"MIL/C{chapter_num}_{chapter_name}/solutions/Solutions_S{section_num}_{section_name}.lean"
# Check if the file exists
if not os.path.exists(lean_file_path):
print(f"File not found: {lean_file_path}")
continue
# Read the content of the .lean file
with open(lean_file_path, "r") as f:
content = f.read()
# Find all instances of "example [name]"
example_pattern = re.compile(r"^example\s", re.MULTILINE)
example_matches = example_pattern.finditer(content)
# Replace instances with "theorem C[xx]_S[yy]_[i]"
modified_content = content
offset = 0
for i, match in enumerate(example_matches):
start, end = match.span()
old_text = match.group()
new_text = f"theorem C{chapter_num}_S{section_num}_{i}"
# Adjust position based on previous replacements
adj_start = start + offset
adj_end = end + offset
# Replace the text
modified_content = (
modified_content[:adj_start] + new_text + modified_content[adj_end:]
)
# Update offset for next replacement
offset += len(new_text) - len(old_text)
# Write the modified content back to the file
with open(lean_file_path, "w") as f:
f.write(modified_content)
print(f"Processed {len(list(example_matches))} thms from {lean_file_path}")
def main():
input_file = input("Enter the path to the text file with import statements: ")
process_file(input_file)
print("Done processing all files.")
if __name__ == "__main__":
main()