hydra.graph module
The extension to graphs of Hydra’s core type system (hydra.core).
- class hydra.graph.Graph(bound_terms: Annotated[Mapping[Name, Term], 'The terms bound by all term variables in scope'], bound_types: Annotated[Mapping[Name, TypeScheme], 'The type schemes of all term variables in scope'], class_constraints: Annotated[Mapping[Name, TypeVariableConstraints], 'A mutable map from type variable names to their accumulated class constraints. This is populated during type inference when operations requiring Eq or Ord are encountered.'], lambda_variables: Annotated[Set[Name], 'The set of term variables introduced by specifically by lambdas'], metadata: Annotated[Mapping[Name, Term], 'Any additional metadata bound to term variables in scope'], primitives: Annotated[Mapping[Name, Primitive], 'All primitive functions and constants by name'], schema_types: Annotated[Mapping[Name, TypeScheme], 'All schema types (type schemes) in scope'], type_variables: Annotated[Set[Name], 'The set of type variables introduced specifically by type lambdas'])
Bases:
objectA graph, or lexical environment which binds names to terms, types, primitives, and metadata.
- BOUND_TERMS = Name(value='boundTerms')
- BOUND_TYPES = Name(value='boundTypes')
- class Builder(_bound_terms: 'Mapping[hydra.core.Name, hydra.core.Term]' = None, _bound_types: 'Mapping[hydra.core.Name, hydra.core.TypeScheme]' = None, _class_constraints: 'Mapping[hydra.core.Name, hydra.core.TypeVariableConstraints]' = None, _lambda_variables: 'Set[hydra.core.Name]' = None, _metadata: 'Mapping[hydra.core.Name, hydra.core.Term]' = None, _primitives: 'Mapping[hydra.core.Name, Primitive]' = None, _schema_types: 'Mapping[hydra.core.Name, hydra.core.TypeScheme]' = None, _type_variables: 'Set[hydra.core.Name]' = None)
Bases:
object- bound_terms(bound_terms)
- bound_types(bound_types)
- build()
- class_constraints(class_constraints)
- lambda_variables(lambda_variables)
- metadata(metadata)
- primitives(primitives)
- schema_types(schema_types)
- type_variables(type_variables)
- CLASS_CONSTRAINTS = Name(value='classConstraints')
- LAMBDA_VARIABLES = Name(value='lambdaVariables')
- METADATA = Name(value='metadata')
- PRIMITIVES = Name(value='primitives')
- SCHEMA_TYPES = Name(value='schemaTypes')
- TYPE_ = Name(value='hydra.graph.Graph')
- TYPE_VARIABLES = Name(value='typeVariables')
- bound_types: Annotated[Mapping[Name, TypeScheme], 'The type schemes of all term variables in scope']
- static builder()
- class_constraints: Annotated[Mapping[Name, TypeVariableConstraints], 'A mutable map from type variable names to their accumulated class constraints. This is populated during type inference when operations requiring Eq or Ord are encountered.']
- lambda_variables: Annotated[Set[Name], 'The set of term variables introduced by specifically by lambdas']
- metadata: Annotated[Mapping[Name, Term], 'Any additional metadata bound to term variables in scope']
- schema_types: Annotated[Mapping[Name, TypeScheme], 'All schema types (type schemes) in scope']
- type_variables: Annotated[Set[Name], 'The set of type variables introduced specifically by type lambdas']
- with_bound_terms(bound_terms)
- with_bound_types(bound_types)
- with_class_constraints(class_constraints)
- with_lambda_variables(lambda_variables)
- with_metadata(metadata)
- with_primitives(primitives)
- with_schema_types(schema_types)
- with_type_variables(type_variables)
- class hydra.graph.Library(name: Annotated[ModuleName, 'A common prefix for all primitive function names in the library'], prefix: Annotated[str, 'A preferred namespace prefix for function names in the library'], primitives: Annotated[Sequence[Primitive], 'The primitives defined in this library'])
Bases:
objectA library of primitive functions.
- class Builder(_name: 'hydra.packaging.ModuleName' = None, _prefix: 'str' = None, _primitives: 'Sequence[Primitive]' = None)
Bases:
object- build()
- name(name)
- prefix(prefix)
- primitives(primitives)
- NAME = Name(value='name')
- PREFIX = Name(value='prefix')
- PRIMITIVES = Name(value='primitives')
- TYPE_ = Name(value='hydra.graph.Library')
- static builder()
- name: Annotated[ModuleName, 'A common prefix for all primitive function names in the library']
- prefix: Annotated[str, 'A preferred namespace prefix for function names in the library']
- with_name(name)
- with_prefix(prefix)
- with_primitives(primitives)
- class hydra.graph.Primitive(definition: Annotated[PrimitiveDefinition, 'The host-independent declarative metadata for the primitive: name, description, signature, totality and purity flags, and an optional reference implementation.'], implementation: Annotated[Callable[[Graph, Sequence[Term]], object], "A concrete, host-specific implementation of the primitive function: given the current graph, a mapping from a list of argument terms to a result term, or an error. Arguments are reduced and stripped of annotations by the interpreter before the implementation is invoked, so the implementation can pattern-match the argument terms directly; a higher-order primitive whose result shape is fixed by its data argument can return an unreduced applicative term and let the outer reducer fold it (no graph needed). The graph is retained for the higher-order primitives that must evaluate a function argument mid-computation (e.g. lists.filter): evaluating that function resolves primitive names against the graph's primitive table. The legacy `InferenceContext` parameter was vestigial and has been removed (https://github.com/CategoricalData/hydra/issues/446)."])
Bases:
objectA built-in function or constant, consisting of the host-independent PrimitiveDefinition (name, signature, metadata) plus a host-specific implementation.
- class Builder(_definition: 'hydra.packaging.PrimitiveDefinition' = None, _implementation: 'Callable[[Graph, Sequence[hydra.core.Term]], Either[hydra.errors.Error, hydra.core.Term]]' = None)
Bases:
object- build()
- definition(definition)
- implementation(implementation)
- DEFINITION = Name(value='definition')
- IMPLEMENTATION = Name(value='implementation')
- TYPE_ = Name(value='hydra.graph.Primitive')
- static builder()
- definition: Annotated[PrimitiveDefinition, 'The host-independent declarative metadata for the primitive: name, description, signature, totality and purity flags, and an optional reference implementation.']
- implementation: Annotated[Callable[[Graph, Sequence[Term]], object], "A concrete, host-specific implementation of the primitive function: given the current graph, a mapping from a list of argument terms to a result term, or an error. Arguments are reduced and stripped of annotations by the interpreter before the implementation is invoked, so the implementation can pattern-match the argument terms directly; a higher-order primitive whose result shape is fixed by its data argument can return an unreduced applicative term and let the outer reducer fold it (no graph needed). The graph is retained for the higher-order primitives that must evaluate a function argument mid-computation (e.g. lists.filter): evaluating that function resolves primitive names against the graph's primitive table. The legacy `InferenceContext` parameter was vestigial and has been removed (https://github.com/CategoricalData/hydra/issues/446)."]
- with_definition(definition)
- with_implementation(implementation)
- class hydra.graph.TermCoder(type: Annotated[Type, 'The Hydra type of encoded terms'], encode: Annotated[Callable[[InferenceContext, Graph, Term], object], 'An encode function from terms to native values'], decode: Annotated[Callable[[InferenceContext, A], object], 'A decode function from native values to terms'])
Bases:
Generic[A]A type together with a coder for mapping terms into arguments for primitive functions, and mapping computed results into terms.
- class Builder(_type: 'hydra.core.Type' = None, _encode: 'Callable[[hydra.typing.InferenceContext, Graph, hydra.core.Term], Either[hydra.errors.Error, A]]' = None, _decode: 'Callable[[hydra.typing.InferenceContext, A], Either[hydra.errors.Error, hydra.core.Term]]' = None)
Bases:
Generic[A]- build()
- decode(decode)
- encode(encode)
- type(type)
- DECODE = Name(value='decode')
- ENCODE = Name(value='encode')
- TYPE = Name(value='type')
- TYPE_ = Name(value='hydra.graph.TermCoder')
- static builder()
- decode: Annotated[Callable[[InferenceContext, A], object], 'A decode function from native values to terms']
- encode: Annotated[Callable[[InferenceContext, Graph, Term], object], 'An encode function from terms to native values']
- with_decode(decode)
- with_encode(encode)
- with_type(type)