Download orchestrator/gitbucket/skills/scripts/mathrosetta_axiom.py from Snapkitty/sov-kernel-monster: direct link, hf CLI and curl.
- Browser
- Download file 8.89 kB
-
https://huggingface.co/Snapkitty/sov-kernel-monster/resolve/main/orchestrator/gitbucket/skills/scripts/mathrosetta_axiom.py
- Command line
-
hf download hf://Snapkitty/sov-kernel-monster/orchestrator/gitbucket/skills/scripts/mathrosetta_axiom.py
-
curl -L -o mathrosetta_axiom.py https://huggingface.co/Snapkitty/sov-kernel-monster/resolve/main/orchestrator/gitbucket/skills/scripts/mathrosetta_axiom.py
8.89 kB
| #!/usr/bin/env python3 | |
| """ | |
| MathRosetta AXIOM Translator β LaTeX β AXIOM Proof Syntax | |
| Translates LaTeX mathematical notation into AXIOM proof assistant syntax. | |
| Supports theorem statements, quantifiers, and proof skeletons. | |
| """ | |
| import re | |
| import time | |
| from typing import Optional | |
| # ββ Domain Mapping ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| DOMAIN_MAP = { | |
| "N": "Nat", | |
| "Z": "Int", | |
| "Q": "Rat", | |
| "R": "Real", | |
| "C": "Complex", | |
| } | |
| # ββ LaTeX β AXIOM Translator βββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class AXIOMTranslator: | |
| """Translate LaTeX mathematical notation to AXIOM proof syntax.""" | |
| def translate_forall(self, var: str, domain: str, body: str) -> str: | |
| """Translate βx β D, body.""" | |
| axiom_domain = DOMAIN_MAP.get(domain, domain) | |
| return f"β ({var} : {axiom_domain}), {body}" | |
| def translate_exists(self, var: str, domain: str, body: str) -> str: | |
| """Translate βx β D, body.""" | |
| axiom_domain = DOMAIN_MAP.get(domain, domain) | |
| return f"β ({var} : {axiom_domain}), {body}" | |
| def translate_theorem(self, name: str, statement: str) -> str: | |
| """Translate theorem statement to AXIOM.""" | |
| return f"theorem {name} : {statement} := by\n sorry" | |
| def translate_lemma(self, name: str, statement: str) -> str: | |
| """Translate lemma statement to AXIOM.""" | |
| return f"lemma {name} : {statement} := by\n sorry" | |
| def translate_latex_expr(self, latex: str) -> str: | |
| """Translate a LaTeX math expression to AXIOM syntax.""" | |
| result = latex | |
| # Replace \mathbb{X} with domain names | |
| for tex_domain, axiom_domain in DOMAIN_MAP.items(): | |
| result = re.sub(rf'\\mathbb{{{tex_domain}}}', axiom_domain, result) | |
| # Replace \forall x \in D, with β (x : D), | |
| result = re.sub( | |
| r'\\forall\s+(\w+)\s*\\in\s*(\w+|\\mathbb\{\w+\})\s*,', | |
| lambda m: f"β ({m.group(1)} : {DOMAIN_MAP.get(m.group(2).replace('\\mathbb{', '').replace('}', ''), m.group(2))}),", | |
| result | |
| ) | |
| # Replace \exists x \in D, with β (x : D), | |
| result = re.sub( | |
| r'\\exists\s+(\w+)\s*\\in\s*(\w+|\\mathbb\{\w+\})\s*,', | |
| lambda m: f"β ({m.group(1)} : {DOMAIN_MAP.get(m.group(2).replace('\\mathbb{', '').replace('}', ''), m.group(2))}),", | |
| result | |
| ) | |
| # Replace \to and \rightarrow with β | |
| result = re.sub(r'\\(to|rightarrow|implies)', 'β', result) | |
| # Replace \land with β§ | |
| result = result.replace(r'\land', 'β§') | |
| # Replace \lor with β¨ | |
| result = result.replace(r'\lor', 'β¨') | |
| # Replace \lnot and \neg with Β¬ | |
| result = re.sub(r'\\(lnot|neg)', 'Β¬', result) | |
| # Replace \leq with β€ | |
| result = result.replace(r'\leq', 'β€') | |
| # Replace \geq with β₯ | |
| result = result.replace(r'\geq', 'β₯') | |
| # Replace \neq with β | |
| result = result.replace(r'\neq', 'β ') | |
| # Replace x^{n} with x^n (simplify braces) | |
| result = re.sub(r'(\w)\^{(\w+)}', r'\1^\2', result) | |
| # Replace \frac{a}{b} with a/b | |
| result = re.sub(r'\\frac\{([^}]+)\}\{([^}]+)\}', r'(\1)/(\2)', result) | |
| # Replace \sqrt{x} with βx | |
| result = re.sub(r'\\sqrt\{([^}]+)\}', r'β(\1)', result) | |
| # Replace \sum with Ξ£ | |
| result = result.replace(r'\sum', 'Ξ£') | |
| # Replace \prod with Ξ | |
| result = result.replace(r'\prod', 'Ξ ') | |
| # Replace \int with β« | |
| result = result.replace(r'\int', 'β«') | |
| # Replace Greek letters | |
| greek = { | |
| r'\alpha': 'Ξ±', r'\beta': 'Ξ²', r'\gamma': 'Ξ³', r'\delta': 'Ξ΄', | |
| r'\phi': 'Ο', r'\varphi': 'Ο', r'\pi': 'Ο', r'\sigma': 'Ο', | |
| r'\omega': 'Ο', r'\Omega': 'Ξ©', r'\varepsilon': 'Ξ΅', r'\epsilon': 'Ξ΅', | |
| r'\lambda': 'Ξ»', r'\mu': 'ΞΌ', r'\nu': 'Ξ½', r'\xi': 'ΞΎ', | |
| r'\rho': 'Ο', r'\tau': 'Ο', r'\chi': 'Ο', r'\psi': 'Ο', | |
| } | |
| for tex, axiom in greek.items(): | |
| result = result.replace(tex, axiom) | |
| # Clean up remaining backslashes from unknown commands | |
| result = re.sub(r'\\(\w+)', r'\1', result) | |
| # Remove remaining braces | |
| result = result.replace('{', '').replace('}', '') | |
| return result.strip() | |
| def translate(self, latex: str) -> str: | |
| """Full LaTeX β AXIOM translation.""" | |
| return self.translate_latex_expr(latex) | |
| # ββ Batch Import ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def import_from_latex(latex_content: str) -> str: | |
| """Import LaTeX theorems and convert to AXIOM syntax.""" | |
| translator = AXIOMTranslator() | |
| results = [] | |
| results.append("-- AXIOM proofs imported from LaTeX") | |
| results.append(f"-- Generated: {time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())}") | |
| results.append("") | |
| # Find theorem/lemma declarations with balanced braces | |
| pattern = r'\\(theorem|lemma)\{([^}]+)\}\{' | |
| for match in re.finditer(pattern, latex_content): | |
| kind = match.group(1) | |
| name = match.group(2) | |
| # Find the matching closing brace for the statement | |
| start = match.end() | |
| depth = 1 | |
| pos = start | |
| while pos < len(latex_content) and depth > 0: | |
| if latex_content[pos] == '{': | |
| depth += 1 | |
| elif latex_content[pos] == '}': | |
| depth -= 1 | |
| pos += 1 | |
| statement = latex_content[start:pos-1] | |
| axiom_statement = translator.translate(statement) | |
| if kind == "theorem": | |
| results.append(translator.translate_theorem(name, axiom_statement)) | |
| else: | |
| results.append(translator.translate_lemma(name, axiom_statement)) | |
| results.append("") | |
| if len(results) <= 3: | |
| results.append("-- No theorems found in input") | |
| return "\n".join(results) | |
| def import_from_file(filepath: str) -> str: | |
| """Import LaTeX file and convert to AXIOM syntax.""" | |
| with open(filepath, 'r', encoding='utf-8') as f: | |
| content = f.read() | |
| return import_from_latex(content) | |
| # ββ Main ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if __name__ == "__main__": | |
| print("ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ") | |
| print("β MATHROSETTA AXIOM TRANSLATOR β") | |
| print("β LaTeX β AXIOM Proof Syntax β") | |
| print("ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ") | |
| print() | |
| translator = AXIOMTranslator() | |
| # Test translations | |
| tests = [ | |
| (r"\forall n \in \mathbb{N}, n + 0 = n", "add_zero"), | |
| (r"\forall x \in \mathbb{R}, x^2 \geq 0", "square_nonneg"), | |
| (r"\forall n \in \mathbb{N}, n > 0 \to n + 0 = n", "add_zero_pos"), | |
| (r"\forall a b \in \mathbb{R}, a + b = b + a", "add_comm"), | |
| (r"\exists x \in \mathbb{R}, x^2 = 2", "sqrt2_exists"), | |
| ] | |
| print("βΆ Individual translations:") | |
| print() | |
| for latex, name in tests: | |
| axiom = translator.translate(latex) | |
| print(f" LaTeX: {latex}") | |
| print(f" AXIOM: {axiom}") | |
| print(f" Theorem: theorem {name} : {axiom} := by") | |
| print(f" sorry") | |
| print() | |
| # Batch import test | |
| print("βΆ Batch import:") | |
| print() | |
| sample_latex = r""" | |
| \theorem{phi_sq}{\forall x \in \mathbb{R}, x^2 = x + 1} | |
| \theorem{phi_inv}{\forall x \in \mathbb{R}, x = x - 1} | |
| \theorem{collatz_reaches_one}{\forall n \in \mathbb{N}, n > 0 \to collatz(n) = 1} | |
| """ | |
| print(import_from_latex(sample_latex)) | |