hydra.lexical module

A module for lexical operations over graphs.

hydra.lexical.build_graph(elements: Sequence[Binding], environment: Mapping[Name, object], primitives: Mapping[Name, Primitive]) Graph

Build a Graph from element bindings, environment, and primitives.

hydra.lexical.choose_unique_name(reserved: Set[Name], name: Name) Name

Pick a name that does not collide with a reserved set, by appending a numeric suffix to the requested name when necessary.

hydra.lexical.dereference_schema_type(name: Name, types: Mapping[Name, TypeScheme]) object

Resolve a schema type through a chain of zero or more typedefs.

hydra.lexical.dereference_variable(graph: Graph, name: Name) object

Look up a binding by name in a graph, returning Either an error or the binding.

hydra.lexical.elements_to_graph(parent: Graph, schema_types: Mapping[Name, TypeScheme], elements: Sequence[Binding]) Graph

Create a graph from a parent graph, schema types, and list of element bindings.

hydra.lexical.empty_graph() Graph

An empty graph; no elements, no primitives, no schema.

hydra.lexical.empty_inference_context() InferenceContext

An empty inference context; fresh-variable counter at zero and empty trace.

hydra.lexical.fields_of(t: Type) Sequence[FieldType]

Extract the fields of a record or union type.

hydra.lexical.get_field(m: Mapping[Name, T0], fname: Name, decode: Callable[[T0], object]) object

Look up a field by name in a record’s field map and decode its value, failing if the field is missing.

hydra.lexical.graph_to_bindings(g: Graph) Sequence[Binding]

Reconstruct a list of Bindings from a Graph’s boundTerms and boundTypes.

hydra.lexical.graph_with_primitives(built_in: Sequence[Primitive], user_provided: Sequence[Primitive]) Graph

Build a graph with primitives assembled from built-in and user-provided lists. User-provided primitives shadow built-in ones.

hydra.lexical.lookup_binding(graph: Graph, name: Name) object

Look up a binding in a graph by name.

hydra.lexical.lookup_primitive(graph: Graph, name: Name) object

Look up a primitive function in a graph by name.

hydra.lexical.lookup_term(graph: Graph, name: Name) object

Look up a term by name in a graph.

hydra.lexical.match_enum(graph: Graph, tname: Name, pairs: Sequence[tuple[Name, T0]], v1: Term) object

Match a term against an enum type, dispatching on the variant name to a value from the supplied list.

hydra.lexical.match_record(graph: T0, decode: Callable[[Mapping[Name, Term]], object], term: Term) object

Match a term against a record type and decode its fields, failing if the term is not a record.

hydra.lexical.match_union(graph: Graph, tname: Name, pairs: Sequence[tuple[Name, Callable[[Term], object]]], term: Term) object

Match a term against a union type, dispatching on the injected variant to the appropriate decoder. Variable terms are dereferenced through the graph before matching.

hydra.lexical.match_unit_field(fname: T0, x: T1) tuple[T0, Callable[[T2], object]]

Build a (fieldName, decoder) pair for a unit-valued union variant: the decoder ignores its argument and returns a fixed value.

hydra.lexical.require_binding(graph: Graph, name: Name) object

Look up a binding in a graph by name, failing with a list of available names if it is not found.

hydra.lexical.require_primitive(graph: Graph, name: Name) object

Look up a primitive in a graph by name, failing if it is not registered.

hydra.lexical.require_primitive_type(tx: Graph, name: Name) object

Look up a primitive’s type scheme in a graph by name, failing if the primitive is not registered.

hydra.lexical.require_term(graph: Graph, name: Name) object

Resolve a name to a term in the graph, following variable references, and fail if the name is not bound.

hydra.lexical.resolve_term(graph: Graph, name: Name) object

TODO: distinguish between lambda-bound and let-bound variables.

hydra.lexical.strip_and_dereference_term(graph: Graph, term: Term) object

Strip annotations and type lambdas/applications from a term, then follow variable references through the graph until a non-variable term is reached.

hydra.lexical.strip_and_dereference_term_either(graph: Graph, term: Term) object

Strip annotations and dereference variables, returning Either an error or the resolved term.