|
| 1 | +# Refactor Playbook |
| 2 | + |
| 3 | +Source of truth for OpenGALib refactor workflows. Sister to `scripts/` |
| 4 | +(reusable codemods) and the project's CLAUDE.md (architectural stance). |
| 5 | + |
| 6 | +The goal: **same operation never costs more than the first time**. |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## Decision tree |
| 11 | + |
| 12 | +``` |
| 13 | +What's the refactor about? |
| 14 | +├─ Rename a single identifier (def, theorem, structure field)? |
| 15 | +│ └─ VSCode F2 (Rename Symbol). |
| 16 | +│ Lean LSP scans the import graph semantically. |
| 17 | +│ Doesn't touch docstrings/comments. Safest possible. |
| 18 | +│ |
| 19 | +├─ Rewrite an import path prefix across many files? |
| 20 | +│ └─ scripts/rewrite-import.sh OLD_PREFIX NEW_PREFIX |
| 21 | +│ Idempotent, refuses dirty working tree, prefix-match. |
| 22 | +│ |
| 23 | +├─ Move file or directory? |
| 24 | +│ └─ git mv first, then scripts/rewrite-import.sh. |
| 25 | +│ git mv preserves rename detection in history. |
| 26 | +│ |
| 27 | +├─ Introduce or migrate notation? |
| 28 | +│ └─ Hand-write a 5-line example file FIRST. Verify parsing, |
| 29 | +│ typeclass inference, simp interaction — before any sweep. |
| 30 | +│ THEN bulk-migrate via perl/sed AND audit docstrings |
| 31 | +│ (text replace catches them too — see Pitfalls #2). |
| 32 | +│ |
| 33 | +├─ Change a typeclass cascade or generic signature? |
| 34 | +│ └─ Manual. No bulk tool helps. Build incrementally. |
| 35 | +│ Use git checkpoints between each step. |
| 36 | +│ |
| 37 | +├─ Need an AST-aware operation (rename only in code, not in |
| 38 | +│ docstrings; find all theorems referencing X; etc.)? |
| 39 | +│ └─ Lake script. See "Lake script vs bash" below. |
| 40 | +│ |
| 41 | +└─ Bulk delete dead content (e.g., a sub-package being removed)? |
| 42 | + └─ git rm -r + scripts/lean-grep.sh to find dangling refs + |
| 43 | + manual cleanup of those refs. |
| 44 | +``` |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## Pre-flight checklist (always) |
| 49 | + |
| 50 | +1. **`git status` clean.** No uncommitted changes. The bulk operation |
| 51 | + should be a single revertible step. |
| 52 | +2. **Snapshot commit** of the current good state if there's any pending |
| 53 | + work: `git commit -am "snapshot before X refactor"`. |
| 54 | +3. **One refactor concern per commit.** Don't bundle "rename + reorganize |
| 55 | + + add deprecation alias" into one diff. Three separate commits. |
| 56 | +4. **`lake build` after each commit.** Catches silent breakage early |
| 57 | + when revert is still cheap. |
| 58 | + |
| 59 | +If anything fails: `git reset --hard HEAD~1` and retry. The atomic-commit |
| 60 | +discipline makes rollback one command. |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +## Lake script vs bash — when to use which |
| 65 | + |
| 66 | +| Need | Tool | Reason | |
| 67 | +|------|------|--------| |
| 68 | +| File text replacement | bash + sed/perl | Milliseconds, well-understood | |
| 69 | +| Import path rewrite | `scripts/rewrite-import.sh` | Already written | |
| 70 | +| `grep` on Lean source only | `scripts/lean-grep.sh` | Excludes `.lake/`, `.git/` | |
| 71 | +| Rename identifier in code only (skip docstrings) | Lake script | Needs Lean syntax tree | |
| 72 | +| Find all theorems whose statement uses X | Lake script | Needs `Lean.Environment` API | |
| 73 | +| Audit `@[simp]` lemma RHS shapes | Lake script | Needs elaborator | |
| 74 | +| Generate a typeclass dependency graph | Lake script | Needs full elab info | |
| 75 | +| One-off file munging | Inline shell command | Don't bother formalizing | |
| 76 | + |
| 77 | +**Rule of thumb:** if the codemod would need to *understand* Lean |
| 78 | +syntax or semantics, write it in Lean (Lake script). Otherwise bash is |
| 79 | +faster to write and run. |
| 80 | + |
| 81 | +### Lake script template |
| 82 | + |
| 83 | +In `lakefile.lean`: |
| 84 | + |
| 85 | +```lean |
| 86 | +script myCodemod (args : List String) do |
| 87 | + match args with |
| 88 | + | [arg1, arg2] => |
| 89 | + -- ... do work using IO + Lean APIs ... |
| 90 | + return 0 |
| 91 | + | _ => |
| 92 | + IO.eprintln "Usage: lake script run myCodemod ARG1 ARG2" |
| 93 | + return 1 |
| 94 | +``` |
| 95 | + |
| 96 | +Invoke: `lake script run myCodemod foo bar`. |
| 97 | + |
| 98 | +For AST-level work, `import Lean` and use `Lean.Environment`, |
| 99 | +`Lean.Syntax`, `Lean.Elab.*`. Mathlib's `scripts/` directory has good |
| 100 | +examples. |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +## Pitfalls (encountered, in this lib's history) |
| 105 | + |
| 106 | +1. **Text-level sed corrupts docstrings.** `sed 's/X/Y/g'` on `.lean` |
| 107 | + files matches `X` inside `/-- ... -/` blocks too. After bulk |
| 108 | + migration, search docstrings with `scripts/lean-grep.sh '<old form>'` |
| 109 | + and clean residual mentions. Or use VSCode F2 (semantic) when |
| 110 | + available. |
| 111 | + |
| 112 | +2. **`open scoped X` requires the namespace to exist via imports.** |
| 113 | + Adding `open scoped X` to a file whose import graph doesn't reach a |
| 114 | + `namespace X` declaration produces "unknown namespace X" build |
| 115 | + error. After any sweep that adds scoped opens, verify with |
| 116 | + `scripts/lean-grep.sh 'open scoped'`. |
| 117 | + |
| 118 | +3. **Notation prefix conflicts with built-in syntax.** |
| 119 | + - `T[x]` clashes with Lean's array indexing (`term[term]`) |
| 120 | + - `T x` (where `T` is an identifier) is parsed as function |
| 121 | + application, beating a `notation:max "T " x:max` pattern |
| 122 | + - Solutions that work: paren form (`Tan(x)`, like `Ric(X, Y)`), |
| 123 | + bracket form with non-identifier prefix (`∇[X]`, since `∇` is in |
| 124 | + Unicode category `Sm`, not `Lu`). |
| 125 | + |
| 126 | +4. **Notation requires careful eta-reduction.** `fun x => f x` |
| 127 | + wrappers in notation RHS create lambdas that don't beta-reduce in |
| 128 | + simp's normal form, breaking pattern matches in subsequent rewrites. |
| 129 | + Always eta-reduce: `notation X => f` not `notation X => fun x => f x`. |
| 130 | + |
| 131 | +5. **Typeclass inference can fail through `_` in notation.** A |
| 132 | + `notation:max "Tan(" x ")" => TangentSpace _ x` gets stuck when |
| 133 | + Lean can't pin the implicit `I : ModelWithCorners` from |
| 134 | + surrounding context. If this happens repeatedly, either: |
| 135 | + (a) keep the original verbose form `TangentSpace I x`, or |
| 136 | + (b) make `I` explicit in the notation. |
| 137 | + `abbrev`-based shorthands hit similar issues. |
| 138 | + |
| 139 | +6. **Force-pushing to remove an oversight is partially effective.** |
| 140 | + `Co-Authored-By: ...` trailers, once pushed to a public repo, |
| 141 | + remain in GitHub's contributor cache even after the commit is |
| 142 | + force-pushed away. The orphan commit is still server-side. Lesson: |
| 143 | + **don't push to a public repo with a trailer you'd regret**. |
| 144 | + Memory `feedback_release_repo_attribution.md` enforces this for |
| 145 | + MathNetwork/OpenGA going forward. |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +## Adding to this playbook |
| 150 | + |
| 151 | +When a refactor pattern is performed **3+ times** with the same |
| 152 | +manual workaround, lift it into: |
| 153 | + |
| 154 | +* a `scripts/` entry (if shell-tool-shaped), or |
| 155 | +* a `lake script` entry (if AST-aware), and |
| 156 | +* a row in the decision tree above + a Pitfall note if it has a known |
| 157 | + failure mode. |
| 158 | + |
| 159 | +The playbook is dogfood. Trust accumulates over commits. |
0 commit comments