Appearance
GhostScience DSL Syntax Guide
The GhostScience DSL parser normalizes mathematical input from four notations into executable SymPy-oriented Python. The parser detects syntax automatically and supports mixed notation within a single query.
Supported Syntaxes
LaTeX
Detected when the input contains LaTeX commands (\int, \frac, \sqrt, Greek letters like \alpha, operators like \cdot, \times, etc.).
| LaTeX Input | Parsed Output |
|---|---|
\int x^2 dx | integrate(x**2, x) |
\frac{a}{b} | ((a)/(b)) |
\sqrt{x + 1} | sqrt(x + 1) |
\alpha + \beta | alpha + beta |
\sum_{n=0}^{\infty} | Sum( |
\lim_{x \to 0} | Limit( |
The parser first attempts conversion via SymPy's parse_latex(). On failure, it falls back to a rule-based regex normalizer that handles:
- Greek letter commands (30+ symbols)
\cdotand\timesto*\inftytooo\partialtoDerivative\sqrt{},\frac{}{}(recursive nested braces)- Integral, sum, and limit notation
^to**(exponentiation)
MATLAB
Detected when the input contains element-wise operators (.*, ./, .^), MATLAB keywords (function, end, elseif), or MATLAB-specific functions (linspace, zeros, ones, eye, eps).
| MATLAB Input | Parsed Output |
|---|---|
A .* B | A * B |
A .^ 2 | A ** 2 |
linspace(0, 1, 100) | numpy.linspace(0, 1, int(100)) |
zeros(3, 3) | numpy.zeros(3, 3) |
eye(4) | numpy.eye(4) |
eps | numpy.finfo(float).eps |
pi | numpy.pi |
Inf / NaN | numpy.inf / numpy.nan |
% comment | # comment |
transpose(A) | numpy.transpose(A) |
Natural Language
Detected when the input contains English math phrases (integrate, differentiate, solve, simplify, factor, expand, limit).
| Natural Language Input | Parsed Output |
|---|---|
integrate x squared with respect to x | integrate(x**2, x) |
derivative of sin(x) w.r.t. x | diff(sin(x), x) |
solve x^2 - 4 for x | solve(x**2 - 4, x) |
simplify (x^2 - 1)/(x - 1) | simplify((x**2 - 1)/(x - 1)) |
factor x^2 + 2x + 1 | factor(x**2 + 2*x + 1) |
expand (x + 1)^3 | expand((x + 1)**3) |
limit of sin(x)/x as x approaches 0 | limit(sin(x)/x, x, 0) |
limit of e^x as x approaches infinity | limit(e**x, x, oo) |
The parser uses pattern matching on phrases like "with respect to", "w.r.t.", "over", "for", "as ... approaches". When no variable is specified, the parser guesses the most frequent single letter in the expression.
Python
Detected when the input contains import, from, def, class, lambda, sympy, sp., np., numpy, or math. statements. Python input is passed through directly with no transformation.
Confidence: 0.95 (highest, since no conversion is needed).
Syntax Detection
The parser checks patterns in this priority order:
- LaTeX -- backslash commands (
\int,\alpha, etc.) - Python -- import/def/class statements or library references
- MATLAB -- element-wise operators or MATLAB function names
- Natural language -- English math verbs
- Fallback -- treated as Python
Parse Result
Every parse returns a ParseResult with:
| Field | Type | Description |
|---|---|---|
python_code | string | The normalized SymPy-oriented Python fragment |
original_input | string | The user's original text |
detected_syntax | string | One of: latex, matlab, python, natural |
confidence | float | 0.0-1.0 confidence score |
warnings | list[string] | Any issues encountered during parsing |
Confidence Scores
| Syntax | Typical Confidence |
|---|---|
| Python (passthrough) | 0.95 |
| LaTeX | 0.85 |
| MATLAB | 0.85 |
| Natural language | 0.70 |
| Parse error | 0.0 |
Using the DSL with the API
The DSL parser is used internally by the scientific orchestrator when processing chat tool calls. For programmatic use, the syntax_hint parameter can override auto-detection:
python
from ghostscience import GhostScienceClient
client = GhostScienceClient(api_key="gst_...")
result = client.symbolic_math(
expression=r"\int_0^1 x^2 dx",
operation="integrate",
)When using the chat interface, simply type your expression in any supported notation. The LLM and DSL parser handle the rest.
Related
- GhostScience -- PhD computing engine overview
- Examples -- script 16 covers multi-syntax DSL input