Polyglot¶
polyglot — mvp.polyglot
Cluster: Core Infrastructure | Type: component | MCP Tools: 26
Overview¶
Cross-language translation and semantic analysis engine supporting nine languages: Python, JavaScript, TypeScript, Rust, Go, C++, Prolog, Lean, and Julia. Parses source into a language-agnostic IR, translates between language pairs, analyses semantics and control-flow automata, verifies behavioural equivalence, and refactors code — enabling agents to reason about, migrate, or verify programs across language boundaries.
Production caveat: polyglot is best treated as a bounded translation, IR inspection, refactoring, and semantic-analysis helper. It can produce useful drafts and structured verification evidence, but it is not a guaranteed production code-migration engine. Before shipping translated or refactored output, run the target language's compiler/interpreter, unit tests, security checks, and human review. For launch and design-partner workflows, describe the value as "translation and analysis assistance with explicit verification signals," not as automatic production migration.
Operational envelope: The parse/translate/verify loop is deterministic zero LLM: no prompt tokens, no hidden model call, and the same inputs route through the same parser/IR/emitter path. Translation and from_ir outputs are qualified-draft with a warning_card until verify_equivalence succeeds. Regex fallback parsing and semantic partial failures are surfaced through completion_state, warning_card, evidence, degraded, and degradation_reason.
When to use:
- Drafting a Python implementation into Rust or Go for follow-up review and test hardening
- Verifying that a refactored function in a different language is behaviourally equivalent to the original
- Analysing control flow and semantic properties of untrusted code before execution
Example:
from mvp.polyglot import PolyglotBlock, PolyglotInput
block = PolyglotBlock(name="polyglot")
result = block.infer(PolyglotInput(
op="translate",
source="def add(a, b):\n return a + b",
source_lang="python",
target_lang="rust",
))
# result.value.target_code → "fn add(a: i64, b: i64) -> i64 { a + b }"
Works well with: meta_programming, formal_methods, cegis
Public API¶
ProgramAutomaton¶
Finite automaton modeling program control flow.
| Field | Type | Default |
|---|---|---|
states | frozenset[str] | required |
alphabet | frozenset[str] | required |
transitions | tuple[tuple[str, str, str], ...] | required |
initial_state | str | required |
accepting_states | frozenset[str] | required |
Methods:
to_dict() -> dict¶
SemanticDomain(str, Enum)¶
Environment¶
Maps identifiers to denotations (ρ in notation).
| Field | Type | Default |
|---|---|---|
bindings | tuple[tuple[str, Any], ...] | () |
Methods:
lookup(name: str) -> Any¶
extend(name: str, value: Any) -> Environment¶
extend_many(pairs: dict[str, Any]) -> Environment¶
to_dict() -> dict[str, Any]¶
Store¶
Maps locations to values (σ in notation, models heap/mutable state).
| Field | Type | Default |
|---|---|---|
locations | tuple[tuple[str, Any], ...] | () |
Methods:
read(loc: str) -> Any¶
write(loc: str, value: Any) -> Store¶
to_dict() -> dict[str, Any]¶
Continuation¶
Represents 'what happens next' (κ in notation).
| Field | Type | Default |
|---|---|---|
body | Any | None |
env | Any | None |
Closure¶
Function denotation capturing lexical scope.
| Field | Type | Default |
|---|---|---|
params | tuple[str, ...] | required |
body | Any | required |
env | Environment | field(default_factory=Environment) |
IRNode¶
Language-agnostic intermediate representation node.
| Field | Type | Default |
|---|---|---|
kind | str | required |
value | Any | None |
children | tuple[Any, ...] | () |
type_info | dict[str, Any] | field(default_factory=dict) |
metadata | dict[str, Any] | field(default_factory=dict) |
semantic_domain | str | 'VALUE' |
MultiValueInverse¶
Lossless inverse view of a (possibly non-injective) bidict.
Constructor:
| Parameter | Type | Default |
|---|---|---|
forward_items | Any | required |
Methods:
get(value: Any, default: Any = None) -> list¶
All forward keys mapping to value (empty list if none).
is_ambiguous(value: Any) -> bool¶
Whether value has more than one reverse candidate.
items()¶
PolyglotBlock(AIBlock[PolyglotInput, PolyglotOutput, dict])¶
Cross-language translation, analysis, and verification block.
| Field | Type | Default |
|---|---|---|
name | str | 'polyglot' |
resource_bounds | ResourceBounds \| None | None |
usage | ResourceUsage | field(default_factory=ResourceUsage) |
Methods:
infer(data: PolyglotInput) -> Result[PolyglotOutput]¶
PolyglotInput(BaseModel)¶
Input for PolyglotBlock.
| Field | Type | Default |
|---|---|---|
source | str | required |
source_lang | str | required |
op | Literal['parse', 'translate', 'analyze_semantics', 'refactor', 'verify_equivalence', 'model_behavior', 'to_ir', 'from_ir', 'list_patterns', 'capabilities'] | required |
target_lang | str \| None | None |
params | dict[str, Any] | Field(default_factory=dict) |
PolyglotOutput(BaseModel)¶
Output from PolyglotBlock.
| Field | Type | Default |
|---|---|---|
source | str | required |
ir | dict \| None | None |
target_code | str \| None | None |
semantic_analysis | dict \| None | None |
equivalence_proof | dict \| None | None |
automaton | dict \| None | None |
metadata | dict | Field(default_factory=dict) |
degraded | bool | False |
degradation_reason | str \| None | None |
completion_state | CompletionState | 'qualified-draft' |
warning_card | str \| None | None |
evidence | dict[str, Any] | Field(default_factory=dict) |
request_id | str \| None | None |
task_id | str \| None | None |
run_id | str \| None | None |
Functions¶
extract_automaton(ir: IRNode) -> ProgramAutomaton¶
Extract a control flow automaton from an IR tree.
compare_automata(a: ProgramAutomaton, b: ProgramAutomaton) -> dict¶
Compare two automata for behavioral equivalence.
minimize_automaton(automaton: ProgramAutomaton) -> ProgramAutomaton¶
Minimize an automaton by removing unreachable states.
parse_source(source: str, lang: str) -> Result[dict]¶
Parse source code, returning a CST dict. Tries lark → ast → regex.
cst_to_ir(cst: dict, lang: str) -> IRNode¶
Convert a CST dict to IRNode tree.
ir_to_dict(node: IRNode) -> dict¶
Serialize IRNode tree to dict for JSON/storage.
dict_to_ir(d: dict) -> IRNode¶
Deserialize dict to IRNode tree.
ir_equal(a: IRNode, b: IRNode) -> bool¶
Structural equality ignoring metadata.
ir_pretty(node: IRNode, indent: int = 0) -> str¶
Human-readable IR dump.
is_eval_unsupported(val: Any) -> bool¶
Check if val is an unsupported-evaluation sentinel.
collect_unsupported_features(ir: IRNode) -> list[str]¶
Return the sorted unique IR node kinds the evaluator cannot denote.
evaluate(node: IRNode, env: Environment, store: Store) -> tuple[Any, Store]¶
Valuation function ⟦node⟧(env, store) → (value, store').
denote_equal(ir_a: IRNode, ir_b: IRNode, test_envs: list[Environment] | None = None, timeout_sec: float = 2.0) -> bool¶
Sound-leaning denotational equivalence:
Trueonly when ⟦ir_a⟧ = ⟦ir_b⟧
invert_bidict(bd: Any) -> MultiValueInverse¶
Build a lossless :class:
MultiValueInversefrom a bidict/fallback map.
reverse_translation_ambiguity(value: str, source_lang: str) -> dict | None¶
Describe the lossy reverse translation of value in source_lang.
translate_operator(op: str, source_lang: str, target_lang: str) -> str¶
Translate an operator from source_lang to target_lang via IR canonical form.
translate_type(type_name: str, source_lang: str, target_lang: str) -> str¶
Translate a type name from source_lang to target_lang via IR canonical form.
get_construct_equivalent(construct: str, target_lang: str) -> str | None¶
Get the equivalent of a construct in target_lang, or None if no equivalent.
rename(ir: IRNode, old_name: str, new_name: str) -> IRNode¶
Rename all occurrences of old_name to new_name in the IR tree.
extract_function(ir: IRNode, node_indices: list[int], new_name: str, params: list[str]) -> IRNode¶
Extract statements at node_indices from a block into a new function.
inline_function(ir: IRNode, func_name: str) -> IRNode¶
Inline a function - replace calls with function body.
change_signature(ir: IRNode, func_name: str, new_params: list[str]) -> IRNode¶
Change function parameter names.
translate(source: str, source_lang: str, target_lang: str, params: dict | None = None) -> Result[dict]¶
Full translation pipeline: source → IR → target code.
emit_code(ir: IRNode, lang: str) -> str¶
Emit source code from IR for the given language.
ts_available(lang: str) -> bool¶
Check if tree-sitter parsing is available for lang.
ts_parse(source: str, lang: str) -> dict¶
Parse source using tree-sitter and return a CST dict.
ts_cst_to_ir(cst: dict, lang: str) -> IRNode¶
Convert a tree-sitter CST dict to IR.
MCP Tools¶
| Operation | Source |
|---|---|
parse | polyglot_mcp |
to_ir | polyglot_mcp |
from_ir | polyglot_mcp |
analyze_semantics | polyglot_mcp |
detect_language | polyglot_mcp |
capabilities | polyglot_mcp |
translate | polyglot_mcp |
translate_function | polyglot_mcp |
translate_type | polyglot_mcp |
translate_expression | polyglot_mcp |
batch_translate | polyglot_mcp |
refactor_rename | polyglot_mcp |
refactor_extract | polyglot_mcp |
refactor_inline | polyglot_mcp |
refactor_signature | polyglot_mcp |
verify_equivalence | polyglot_mcp |
verify_types | polyglot_mcp |
verify_roundtrip | polyglot_mcp |
verify_behavior | polyglot_mcp |
model_behavior | polyglot_mcp |
compare_automata | polyglot_mcp |
minimize_automaton | polyglot_mcp |
store_translation | polyglot_mcp |
retrieve_translation | polyglot_mcp |
list_translations | polyglot_mcp |
search | polyglot_mcp |