|
| 1 | +--- |
| 2 | +name: physlibsearch |
| 3 | +description: Semantic search over Physlib, a formal Lean 4 library of physics theorems and definitions. Use when asked to find Lean 4 declarations related to physics or mathematics, look up formal proofs, retrieve theorem signatures, or browse the Physlib module hierarchy. |
| 4 | +--- |
| 5 | + |
| 6 | +# PhyslibSearch API Skill |
| 7 | + |
| 8 | +PhyslibSearch is a semantic search engine over **Physlib**, a Lean 4 formal library of physics and mathematics. It lets you find theorems, definitions, lemmas, and instances by describing them in natural language — no Lean syntax required. |
| 9 | + |
| 10 | +**Base URL**: `https://physlibsearch.net` |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## When to use this skill |
| 15 | + |
| 16 | +- The user asks to find a Lean 4 theorem, definition, or proof about a physics/math concept. |
| 17 | +- The user wants the formal statement (signature) of a known result (e.g. Newton's second law, Schrödinger equation). |
| 18 | +- The user wants to browse what Physlib covers in a specific area. |
| 19 | +- The user has a Lean 4 declaration name and wants its full record. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## Endpoints |
| 24 | + |
| 25 | +### 1. Search — `POST /search` |
| 26 | + |
| 27 | +The main endpoint. Submit one or more natural language queries, get ranked results. |
| 28 | + |
| 29 | +**Request** |
| 30 | +```json |
| 31 | +{ |
| 32 | + "query": ["Newton's second law"], |
| 33 | + "num_results": 10 |
| 34 | +} |
| 35 | +``` |
| 36 | +- `query`: array of strings (1–many queries); each runs independently and returns its own ranked list. |
| 37 | +- `num_results`: 1–150, default 10. |
| 38 | + |
| 39 | +**Response** — `list[list[QueryResult]]` (one list per query, ordered by relevance) |
| 40 | +```json |
| 41 | +[[ |
| 42 | + { |
| 43 | + "result": { |
| 44 | + "module_name": ["Physlib", "Mechanics", "Newton"], |
| 45 | + "kind": "theorem", |
| 46 | + "name": ["Physlib", "Mechanics", "Newton", "secondLaw"], |
| 47 | + "signature": "theorem Physlib.Mechanics.Newton.secondLaw : ∀ (m F : ℝ), m > 0 → acceleration m F = F / m", |
| 48 | + "type": "Prop", |
| 49 | + "value": null, |
| 50 | + "docstring": null, |
| 51 | + "informal_name": "Newton's Second Law", |
| 52 | + "informal_description": "For a body of mass $m > 0$ and applied force $F$, the acceleration satisfies $a = F/m$." |
| 53 | + }, |
| 54 | + "distance": 0.12 |
| 55 | + } |
| 56 | +]] |
| 57 | +``` |
| 58 | + |
| 59 | +**Key fields**: |
| 60 | +- `distance` — cosine distance (lower = more relevant; 0 is perfect). |
| 61 | +- `informal_name` / `informal_description` — human-readable explanation (LaTeX math). |
| 62 | +- `signature` — the formal Lean 4 statement. |
| 63 | +- `name` — fully-qualified name as a string array (use with `/fetch`). |
| 64 | +- `kind` — `theorem`, `definition`, `lemma`, `instance`, `axiom`, etc. |
| 65 | + |
| 66 | +**Example (curl)** |
| 67 | +```bash |
| 68 | +curl -s -X POST https://physlibsearch.net/search \ |
| 69 | + -H "Content-Type: application/json" \ |
| 70 | + -d '{"query": ["quantum harmonic oscillator energy levels"], "num_results": 5}' |
| 71 | +``` |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +### 2. Query Expansion — `POST /expand` |
| 76 | + |
| 77 | +Optionally call this **before** `/search` to improve results for technical or ambiguous queries. It uses HyDE (Hypothetical Document Embeddings): Gemini generates a plausible hypothetical Lean declaration, which is then used as the search vector. |
| 78 | + |
| 79 | +**Request** — raw JSON string (the query itself, not an object) |
| 80 | +```json |
| 81 | +"Schrödinger equation for a free particle" |
| 82 | +``` |
| 83 | + |
| 84 | +**Response** — JSON string (expanded query or original on failure) |
| 85 | +```json |
| 86 | +"theorem Physlib.QuantumMechanics.FreeParticle.schrodinger : ..." |
| 87 | +``` |
| 88 | + |
| 89 | +**When to use**: For precise or highly technical queries (specific equation names, less common concepts). For broad natural-language queries, go straight to `/search`. |
| 90 | + |
| 91 | +**Example flow** |
| 92 | +```bash |
| 93 | +# Step 1: expand |
| 94 | +EXPANDED=$(curl -s -X POST https://physlibsearch.net/expand \ |
| 95 | + -H "Content-Type: application/json" \ |
| 96 | + -d '"Schrödinger equation for a free particle"') |
| 97 | + |
| 98 | +# Step 2: search with expanded query |
| 99 | +curl -s -X POST https://physlibsearch.net/search \ |
| 100 | + -H "Content-Type: application/json" \ |
| 101 | + -d "{\"query\": [$EXPANDED], \"num_results\": 5}" |
| 102 | +``` |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +### 3. Fetch by Name — `POST /fetch` |
| 107 | + |
| 108 | +Retrieve full records for known Lean declaration names. Use this when you already have a name from a prior search or from the user. |
| 109 | + |
| 110 | +**Request** |
| 111 | +```json |
| 112 | +{ |
| 113 | + "query": [ |
| 114 | + ["Physlib", "Mechanics", "Newton", "secondLaw"], |
| 115 | + ["Physlib", "QuantumMechanics", "HarmonicOscillator", "energy"] |
| 116 | + ] |
| 117 | +} |
| 118 | +``` |
| 119 | +- Each entry is a `LeanName` — an array of strings forming the fully-qualified path. |
| 120 | + |
| 121 | +**Response** — `list[Record | null]` (null for names not found, order preserved) |
| 122 | + |
| 123 | +**Example (curl)** |
| 124 | +```bash |
| 125 | +curl -s -X POST https://physlibsearch.net/fetch \ |
| 126 | + -H "Content-Type: application/json" \ |
| 127 | + -d '{"query": [["Physlib", "Mechanics", "Newton", "secondLaw"]]}' |
| 128 | +``` |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +### 4. List Modules — `GET /modules` |
| 133 | + |
| 134 | +Returns all top-level Physlib modules with a count of searchable declarations. |
| 135 | + |
| 136 | +**Response** |
| 137 | +```json |
| 138 | +[ |
| 139 | + {"name": ["Physlib", "Mechanics"], "count": 42}, |
| 140 | + {"name": ["Physlib", "QuantumMechanics"], "count": 31} |
| 141 | +] |
| 142 | +``` |
| 143 | + |
| 144 | +**Example (curl)** |
| 145 | +```bash |
| 146 | +curl -s https://physlibsearch.net/modules |
| 147 | +``` |
| 148 | + |
| 149 | +--- |
| 150 | + |
| 151 | +### 5. Module Declarations — `POST /modules/declarations` |
| 152 | + |
| 153 | +Returns all declarations in a specific module, ordered by source position. |
| 154 | + |
| 155 | +**Request** — raw JSON array (the module name, not wrapped in an object) |
| 156 | +```json |
| 157 | +["Physlib", "Mechanics", "Newton"] |
| 158 | +``` |
| 159 | + |
| 160 | +**Response** — `list[Record]` (same shape as search results, without `distance`) |
| 161 | + |
| 162 | +**Example (curl)** |
| 163 | +```bash |
| 164 | +curl -s -X POST https://physlibsearch.net/modules/declarations \ |
| 165 | + -H "Content-Type: application/json" \ |
| 166 | + -d '["Physlib", "Mechanics", "Newton"]' |
| 167 | +``` |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## Best practices |
| 172 | + |
| 173 | +### Write queries like you'd explain the concept to a physicist, not like Lean code |
| 174 | + |
| 175 | +| Good | Avoid | |
| 176 | +|------|-------| |
| 177 | +| `"conservation of momentum"` | `"theorem momentum"` | |
| 178 | +| `"energy of a quantum harmonic oscillator"` | `"E_n = hbar omega (n + 1/2)"` | |
| 179 | +| `"Maxwell's equations in differential form"` | `"curl E = -dB/dt"` | |
| 180 | +| `"Euler-Lagrange equation"` | `"Lagrangian mechanics derivative"` | |
| 181 | + |
| 182 | +### Batch multiple queries in one request |
| 183 | + |
| 184 | +If you need to answer several related questions, send them together — each gets its own ranked list and it's a single round trip: |
| 185 | +```json |
| 186 | +{ |
| 187 | + "query": [ |
| 188 | + "kinetic energy theorem", |
| 189 | + "work-energy theorem", |
| 190 | + "conservation of mechanical energy" |
| 191 | + ], |
| 192 | + "num_results": 5 |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +### Use `/expand` for narrow technical queries |
| 197 | + |
| 198 | +Good candidates for expansion: named equations (Schrödinger, Navier-Stokes, Boltzmann), specific physical constants, or queries where the first search returns low-relevance results (distance > 0.4). |
| 199 | + |
| 200 | +### Interpret `distance` to judge relevance |
| 201 | + |
| 202 | +| Distance | Interpretation | |
| 203 | +|----------|---------------| |
| 204 | +| < 0.15 | Strong match — likely exactly what was asked for | |
| 205 | +| 0.15–0.30 | Good match — closely related concept | |
| 206 | +| 0.30–0.45 | Partial match — topically related but may not be the right theorem | |
| 207 | +| > 0.45 | Weak match — consider rephrasing or using `/expand` | |
| 208 | + |
| 209 | +### Show `informal_description` first, then `signature` |
| 210 | + |
| 211 | +For human-facing answers, lead with `informal_description` (natural language + LaTeX) and offer the formal `signature` as supporting detail. Most users want to understand the result before reading Lean 4 syntax. |
| 212 | + |
| 213 | +### Reconstruct the Lean import path from `name` |
| 214 | + |
| 215 | +The `name` array maps directly to the Lean import path: |
| 216 | +- `["Physlib", "Mechanics", "Newton", "secondLaw"]` → `Physlib.Mechanics.Newton.secondLaw` |
| 217 | +- Users can reference this in their Lean 4 files with `import Physlib.Mechanics.Newton` |
| 218 | + |
| 219 | +--- |
| 220 | + |
| 221 | +## Rate limits |
| 222 | + |
| 223 | +| Endpoint | Limit | |
| 224 | +|----------|-------| |
| 225 | +| `/search` | 1 request/second | |
| 226 | +| `/fetch` | 10 requests/second | |
| 227 | +| `/expand` | 15 requests/minute | |
| 228 | +| `/modules` | 30 requests/minute | |
| 229 | +| `/modules/declarations` | 30 requests/minute | |
| 230 | + |
| 231 | +For agents making multiple searches in a loop, add a short delay between `/search` calls or batch queries into a single request (preferred). |
| 232 | + |
| 233 | +--- |
| 234 | + |
| 235 | +## Declaration kinds |
| 236 | + |
| 237 | +Results can have these `kind` values: |
| 238 | +`theorem`, `definition`, `lemma`, `instance`, `axiom`, `structure`, `inductive`, `abbrev`, `opaque`, `example`, `proofWanted`, `classInductive` |
| 239 | + |
| 240 | +For most physics queries, `theorem` and `definition` are the most common and useful. |
| 241 | + |
| 242 | +--- |
| 243 | + |
| 244 | +## More |
| 245 | + |
| 246 | +- **Live search**: https://physlibsearch.net |
| 247 | +- **API docs**: https://physlibsearch.net/docs |
| 248 | +- **Skill repo**: https://github.com/Kernel-Science/physlibsearch-skill |
| 249 | +- **Source**: https://github.com/Kernel-Science/physlibsearch |
0 commit comments