Lens, Iso, Mapping¶
didactic.api.Lens ¶
General bidirectional transform with a complement.
Subclass and override forward returning
(b, complement), and backward taking
(b, complement) and returning a. Round-trip laws (panproto's
check_get_put and check_put_get):
- GetPut:
backward(*forward(a)) == a - PutGet:
forward(backward(b, c)) == (b, c)
Notes
The pure-Python implementation does not enforce the laws; they are tested with hypothesis or verified through panproto once the runtime hookup lands.
See Also
didactic.Iso : the lossless special case (complement is unit). didactic.Mapping : one-way variant.
didactic.api.Iso ¶
Bases: Mapping[A, B]
Two-way bijection between A and B with no information loss.
Subclass and override forward and backward. Round-trip laws are:
backward(forward(a)) == aforward(backward(b)) == b
didactic verifies these in tests via didactic.api.testing.verify_iso; runtime verification on every forward call is opt-in.
See Also
didactic.Lens : the general lossy lens with a complement.
didactic.api.Mapping ¶
One-way transformation from A to B.
Subclass and override forward. Cannot be composed in reverse and has no inverse.
Notes
Mappings are pure Python in v0.0.2; the panproto-side Lens
representation lands later.
See Also
didactic.Iso : a Mapping with a verified inverse. didactic.Lens : a Mapping with a complement.
forward ¶
Map a forward to its target.
Subclasses must override. The default raises
NotImplementedError so misuse is loud.
didactic.api.DependentLens ¶
A schema-independent lens family.
Wraps a panproto.ProtolensChain. The wrapper carries the
underlying chain and exposes the operations that are useful from
didactic-shaped code:
- construction via auto_generate or auto_generate_with_hints
- composition (
>>or compose) - fusion of all steps into one (fuse)
- JSON round-trip (to_json and from_json)
- instantiation against a concrete schema (instantiate)
| PARAMETER | DESCRIPTION |
|---|---|
inner
|
An already-constructed
TYPE:
|
Notes
Equality compares the JSON-serialised form of the underlying chain; structurally identical chains are equal even if they were constructed by different code paths.
auto_generate
classmethod
¶
auto_generate(
src_schema: Schema,
tgt_schema: Schema,
protocol: Protocol,
*,
stringency: str | None = None,
) -> DependentLens
Auto-generate a chain between two schemas under protocol.
| PARAMETER | DESCRIPTION |
|---|---|
src_schema
|
The source schema.
TYPE:
|
tgt_schema
|
The target schema.
TYPE:
|
protocol
|
The panproto protocol that both schemas conform to.
TYPE:
|
stringency
|
Optional stringency hint passed through to panproto. Use
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DependentLens
|
A chain capturing the rewrites that take the source schema to the target. |
| RAISES | DESCRIPTION |
|---|---|
LensError
|
If panproto cannot derive a chain between the two schemas under the given protocol. |
auto_generate_with_hints
classmethod
¶
auto_generate_with_hints(
src_schema: Schema,
tgt_schema: Schema,
protocol: Protocol,
hints: dict[str, str],
*,
stringency: str | None = None,
) -> DependentLens
Auto-generate using vertex-correspondence hints.
| PARAMETER | DESCRIPTION |
|---|---|
src_schema
|
Source schema.
TYPE:
|
tgt_schema
|
Target schema.
TYPE:
|
protocol
|
The panproto protocol both schemas conform to.
TYPE:
|
hints
|
Vertex-correspondence hints mapping source-schema vertex
IDs to target-schema vertex IDs. A discovered
Correspondence supplies this
shape via its
TYPE:
|
stringency
|
Optional stringency hint.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DependentLens
|
A chain that respects the given hints. |
from_json
classmethod
¶
from_json(json_text: str) -> DependentLens
Reconstruct a chain from its JSON form.
| PARAMETER | DESCRIPTION |
|---|---|
json_text
|
A JSON string previously returned by to_json.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DependentLens
|
The reconstructed chain. |
| RAISES | DESCRIPTION |
|---|---|
LensError
|
If |
from_dsl_json
classmethod
¶
from_dsl_json(
source: str, body_vertex: str
) -> DependentLens
Compile a panproto-lens-dsl JSON document into a chain.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
The JSON surface of
TYPE:
|
body_vertex
|
Entry vertex of the source schema the chain is being authored against. The DSL compiler uses it to anchor the per-step protolens construction.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DependentLens
|
The compiled chain. |
| RAISES | DESCRIPTION |
|---|---|
LensError
|
If |
from_dsl_yaml
classmethod
¶
from_dsl_yaml(
source: str, body_vertex: str
) -> DependentLens
Compile a panproto-lens-dsl YAML document into a chain.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
The YAML surface of
TYPE:
|
body_vertex
|
Entry vertex of the source schema; see from_dsl_json.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DependentLens
|
The compiled chain. |
| RAISES | DESCRIPTION |
|---|---|
LensError
|
If |
from_dsl_nickel
classmethod
¶
from_dsl_nickel(
source: str, body_vertex: str
) -> DependentLens
Compile a panproto-lens-dsl Nickel document into a chain.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
The Nickel surface of
TYPE:
|
body_vertex
|
Entry vertex of the source schema; see from_dsl_json.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DependentLens
|
The compiled chain. |
| RAISES | DESCRIPTION |
|---|---|
LensError
|
If |
from_dsl_path
classmethod
¶
from_dsl_path(path: str, body_vertex: str) -> DependentLens
Compile a panproto-lens-dsl document loaded from path.
Dispatches on the file extension: .ncl is parsed as
Nickel, .json as JSON, .yaml / .yml as YAML.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Filesystem path to a lens-DSL document.
TYPE:
|
body_vertex
|
Entry vertex of the source schema; see from_dsl_json.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DependentLens
|
The compiled chain. |
| RAISES | DESCRIPTION |
|---|---|
LensError
|
If the file is missing or its contents are not a valid lens-DSL document. |
compose ¶
compose(other: DependentLens) -> DependentLens
Vertically compose this chain with other.
| PARAMETER | DESCRIPTION |
|---|---|
other
|
Another DependentLens whose source matches this chain's target.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DependentLens
|
The composed chain. |
Notes
Composition is associative; identity is the empty chain.
fuse ¶
Fuse all steps into a single protolens.
| RETURNS | DESCRIPTION |
|---|---|
object
|
A panproto-side fused protolens. The exact return type is panproto-defined; treat it as opaque. |
instantiate ¶
Instantiate against a concrete schema to produce a panproto.Lens.
| PARAMETER | DESCRIPTION |
|---|---|
schema
|
The concrete source schema to specialise against.
TYPE:
|
protocol
|
The panproto protocol the schema conforms to.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
object
|
A |
| RAISES | DESCRIPTION |
|---|---|
LensError
|
If the chain cannot be instantiated against |
didactic.api.Correspondence
dataclass
¶
One discovered schema morphism, scored.
| PARAMETER | DESCRIPTION |
|---|---|
vertex_map
|
Mapping from source-schema vertex IDs to target-schema vertex
IDs. Feed directly as the
TYPE:
|
quality
|
Alignment quality in
TYPE:
|
didactic.api.find_correspondences ¶
find_correspondences(
src_schema: Schema,
tgt_schema: Schema,
*,
anchors: dict[str, str] | None = None,
monic: bool = False,
epic: bool = False,
iso: bool = False,
max_results: int = 0,
relax_edge_name_pruning: bool = False,
) -> list[Correspondence]
Enumerate scored vertex correspondences between two schemas.
| PARAMETER | DESCRIPTION |
|---|---|
src_schema
|
Source schema.
TYPE:
|
tgt_schema
|
Target schema.
TYPE:
|
anchors
|
Vertex pairs (source ID to target ID) the search must respect. Use to pin known correspondences and let the search fill in the rest.
TYPE:
|
monic
|
Require the morphism to be injective on vertices (no two source vertices map to the same target vertex).
TYPE:
|
epic
|
Require the morphism to be surjective on vertices (every target vertex is hit).
TYPE:
|
iso
|
Require a bijection. Implies
TYPE:
|
max_results
|
Upper bound on the number of morphisms returned.
TYPE:
|
relax_edge_name_pruning
|
Keep kind-compatible candidate targets that share no outgoing edge name with the source vertex. By default the search prunes such candidates for object vertices with large candidate domains, which can discard a correct pairing when every child was renamed. Naturality is still enforced.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list of Correspondence
|
Discovered correspondences. Empty when no structure-preserving map exists under the given constraints. |
Notes
The schemas didactic builds from Model classes are single-vertex (the structure lives in the Theory), so searching between two Models degenerates to the root pairing. The search is informative on multi-vertex schemas: protocol schemas built by hand and schemas recovered by didactic.codegen.source.parse.
didactic.api.best_correspondence ¶
best_correspondence(
src_schema: Schema,
tgt_schema: Schema,
*,
anchors: dict[str, str] | None = None,
monic: bool = False,
epic: bool = False,
iso: bool = False,
relax_edge_name_pruning: bool = False,
) -> Correspondence | None
Return the highest-quality correspondence, or None.
| PARAMETER | DESCRIPTION |
|---|---|
src_schema
|
Source schema.
TYPE:
|
tgt_schema
|
Target schema.
TYPE:
|
anchors
|
Vertex pairs (source ID to target ID) the search must respect.
TYPE:
|
monic
|
Require injectivity on vertices.
TYPE:
|
epic
|
Require surjectivity on vertices.
TYPE:
|
iso
|
Require a bijection. Implies
TYPE:
|
relax_edge_name_pruning
|
Keep kind-compatible candidate targets that share no outgoing edge name with the source vertex. See find_correspondences.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Correspondence or None
|
The best-scoring correspondence, or |
See Also
find_correspondences : the full enumeration.