hydra.subterms module

The term-graph view of a typed graph: nodes are subterms, sharing is explicit, children are inline (a tree with symbolic cross-links). graphToTermGraph builds it; schemaToTypeGraph is the type-side twin. Term graphs are Ariola-Klop’s nested systems of recursion equations.

hydra.subterms.expected_for_step(schema: Mapping[Name, Type], typ: Type, term: Term, step: SubtermStep) → object

The expected type to thread into an immediate child, given this node’s (synthesized) type, its term, and the step reaching the child. This is the term->type position map underlying the TermGraph:TypeGraph erasure: element positions get the container element type, mapEntry gets pair<K,V>, lambdaBody the codomain, and so on. none where the child’s type is not fixed top-down (e.g. a function-position subterm), which is fine for non-empty children.

hydra.subterms.field_type_from_schema(schema: Mapping[Name, Type], term: Term, fname: Name) → object

The expected type of a record field / injected union field, from the named type in the schema.

hydra.subterms.find_field_type_in(fname: Name, fields: Sequence[FieldType]) → object

The type of the named field within a list of field types, if present.

hydra.subterms.graph_to_term_graph(graph: Graph) → object

The term-graph view of a typed graph: one root node per binding. Sharing is explicit; children are inline (a tree); variable occurrences become symbolic references carrying their binding node’s id. Input must be a POST-INFERENCE (elaborated) graph: binding schemes present, lambda domains present, typeLambda/typeApplication explicit. Node types are synthesized in one O(n) bidirectional pass; a missing annotation (or an untyped empty leaf) is a precondition failure.

hydra.subterms.root_expected_type(schema: Mapping[T0, T1], name: T0) → object

The expected type for a root binding node: its declared type in the schema, if present.

hydra.subterms.schema_to_type_graph(schema: Mapping[Name, Type]) → object

The type-graph view of a schema: one root node per named type. A Type.variable referencing a schema-bound name is a nominal reference; a forall-bound variable in scope is a variable reference to its binding node; leaf types and non-type constituents are carried as links.

hydra.subterms.synthesize_type(graph: T0, schema: Mapping[Name, Type], env: Mapping[Name, Type], root: Name, expected: object, path: T1, term: Term) → object

Synthesize the type of an elaborated subterm, syntax-directed and without unification, threading expected top-down. Reads annotations (lambda domain, typeApplication type) and combines child types bottom-up; synthesis-incomplete leaves (empty list/set/map, optional none, either’s absent branch) CONSUME expected. expected = none at such a leaf is a precondition failure with path.

hydra.subterms.term_attributes_of(term: Term) → Sequence[TermAttributeLink]

The content attribute links contributed by a term constructor (a non-term constituent, with its value).

hydra.subterms.term_graph_node(graph: Graph, schema: Mapping[Name, Type], root: Name, env: Mapping[Name, tuple[TermNodeId, Type]], lambda_scope: Set[Name], expected: object, path: SubtermPath, term: Term) → object

Build a term-graph node (inline) for the subterm at the given path within a root binding. env maps each in-scope local variable to its (binder node id, type); lambdaScope marks the lambda-bound subset. expected is the type flowing top-down, consumed by synthesis-incomplete leaves. A variable child is not a node: the parent emits a reference link for it.

hydra.subterms.term_reference_for(graph: Graph, env: Mapping[Name, tuple[TermNodeId, Type]], lambda_scope: Set[Name], name: Name) → object

Classify a variable occurrence, carrying its binder’s node id: lambda-bound (node = the binding lambda), let-bound — locally or as a graph binding — (node = the bound term), or a primitive; a free variable is a precondition failure. lambdaScope marks which local names are lambda-bound.

hydra.subterms.type_attributes_of(typ: Type) → Sequence[TypeAttributeLink]

The content attribute links contributed by a type constructor: annotation, forall parameter, or literal type.

hydra.subterms.type_graph_node(schema_names: Set[Name], root: Name, forall_scope: Mapping[Name, TypeNodeId], path: SubtypePath, typ: Type) → object

Build a type-graph node (inline) for the subtype at the given path within a named root type.

The outgoing links of a type node: its content attributes, and one link per immediate subtype — a variable occurrence becomes a reference link on the parent (occurrences are not nodes), any other subtype an inline subtype link. forallScope maps each in-scope forall variable to its binding node id.

hydra.subterms.type_reference_for(schema_names: Set[Name], forall_scope: Mapping[Name, TypeNodeId], name: Name) → object

Classify a type variable occurrence: a schema-bound name is a nominal reference; a forall-bound variable in scope is a variable reference carrying its binding forall’s node id; anything else is a free-variable precondition failure.