topos.core.morphism¶
Morphism Module¶
In the category of Programs, we treat source code not as static text, but as a ‘Morphism’ (f: A → B) between computational states.
- Mathematical Inspiration:
A program is a map that transforms an input domain (Object A) into an output codomain (Object B). As code becomes a ‘commodity’ via LLMs, the identity of the morphism matters less than its structural invariants.
Two morphisms f, g: A → B are considered equivalent if they produce the same observable behavior—but in our Topos, we also care about their internal structure. A morphism that is ‘commodity code’ may compute correctly but lack the structural integrity of a ‘verified’ morphism.
The characteristic map χ: Morphism → Ω assigns each morphism an evaluation value in our Heyting Algebra, capturing this nuanced view of correctness.
- class topos.core.morphism.ProgramMorphism(source, language='python', parser_backend='hybrid', filepath=None, ast=None, representations=<factory>, _cfg=None, _pdg=None, _cpg=None)[source]
Bases:
objectA program viewed as a transformation between computational states.
The ProgramMorphism is the central abstraction of topos. It encapsulates source code along with its parsed AST representation, providing the foundation for evaluation by the Subobject Classifier.
- source
The raw source code as a string.
- Type:
- language
The programming language (default: ‘python’).
- Type:
- filepath
Optional path to the source file.
- Type:
Path | None
- ast
The ProgramObject (AST) representation.
- Type:
ProgramObject | None
- representations
Additional representations (depgraph, etc.) attached to this morphism for multi-axis evaluation.
- Type:
list[Representation]
- Categorical Interpretation:
In category theory, a morphism f: A → B is an arrow between objects. Here, the source code IS the morphism – it defines how to transform inputs (domain) into outputs (codomain). The AST captures the ‘internal structure’ of this transformation; additional representations capture inter-module structure.
- source
- language = 'python'
- parser_backend = 'hybrid'
- filepath = None
- ast = None
- representations
- classmethod from_file(filepath, language='python', parser_backend='hybrid')[source]
Create a ProgramMorphism from a source file.
- Parameters:
filepath – Path to the source file.
language – Programming language of the source.
- Returns:
A new ProgramMorphism instance.
- property is_valid
Check if the morphism represents syntactically valid code.
- build_cfg()[source]
Build (and cache) the Control Flow Graph representation.
- build_pdg()[source]
Build (and cache) the academic Program Dependence Graph.
- build_cpg()[source]
Build (and cache) the Code Property Graph.
- property name
A human-readable identifier for this morphism.
- classify()[source]
Evaluate this morphism using the Subobject Classifier.
If additional representations are attached they will be included in the evaluation.
- Returns:
An EvaluationValue from the Heyting Algebra representing the code’s position in the evaluation lattice.