hydra.adapt module

Simple, one-way adapters for types and terms.

hydra.adapt.adapt_data_graph(constraints: LanguageConstraints, do_expand: bool, els0: Sequence[Binding], cx: T0, graph0: Graph) object

Adapt a graph and its schema to the given language constraints. The doExpand flag controls eta expansion of partial applications. Adaptation is type-preserving: binding-level TypeSchemes are adapted (not stripped). Note: case statement hoisting is done separately, prior to adaptation. The els0 parameter provides the original ordered bindings. Returns both the adapted graph and the ordered adapted bindings.

hydra.adapt.adapt_float_type(constraints: LanguageConstraints, ft: FloatType) object

Attempt to adapt a floating-point type using the given language constraints.

hydra.adapt.adapt_graph_schema(constraints: LanguageConstraints, litmap: Mapping[LiteralType, LiteralType], types0: Mapping[T0, Type]) object

Adapt a schema graph to the given language constraints.

hydra.adapt.adapt_integer_type(constraints: LanguageConstraints, it: IntegerType) object

Attempt to adapt an integer type using the given language constraints.

hydra.adapt.adapt_lambda_domains(constraints: LanguageConstraints, litmap: Mapping[LiteralType, LiteralType], recurse: Callable[[T0], object], term: T0) object

Rewrite callback for adapting lambda domain types in a term.

hydra.adapt.adapt_literal(lt: LiteralType, l: Literal) Literal

Convert a literal to a different type.

hydra.adapt.adapt_literal_type(constraints: LanguageConstraints, lt: LiteralType) object

Attempt to adapt a literal type using the given language constraints.

hydra.adapt.adapt_literal_types_map(constraints: LanguageConstraints) Mapping[LiteralType, LiteralType]

Derive a map of adapted literal types for the given language constraints.

hydra.adapt.adapt_literal_value(litmap: Mapping[T0, LiteralType], lt: T0, l: Literal) Literal

Adapt a literal value using the given language constraints.

hydra.adapt.adapt_nested_types(constraints: LanguageConstraints, litmap: Mapping[LiteralType, LiteralType], recurse: Callable[[T0], object], term: T0) object

Rewrite callback for adapting nested let binding TypeSchemes in a term.

hydra.adapt.adapt_primitive(constraints: LanguageConstraints, litmap: Mapping[LiteralType, LiteralType], prim0: Primitive) object

Adapt a primitive to the given language constraints, prior to inference.

hydra.adapt.adapt_term(constraints: LanguageConstraints, litmap: Mapping[LiteralType, LiteralType], cx: T0, graph: Graph, term0: Term) object

Adapt a term using the given language constraints.

hydra.adapt.adapt_term_for_language(lang: Language, cx: T0, g: Graph, term: Term) object

Adapt a term using the constraints of a given language.

hydra.adapt.adapt_term_signature(constraints: LanguageConstraints, litmap: Mapping[LiteralType, LiteralType], sig0: TermSignature) object

Adapt the types within a term signature to the given language constraints, in place. Parameter names, descriptions, and per-parameter isLazy flags, as well as type parameters, are preserved; only the parameter and result types are adapted. Unlike routing through TypeScheme (the type-only view), this retains the full TermSignature metadata, including primitive laziness flags.

hydra.adapt.adapt_type(constraints: LanguageConstraints, litmap: Mapping[LiteralType, LiteralType], type0: Type) object

Adapt a type using the given language constraints.

hydra.adapt.adapt_type_for_language(lang: Language, typ: Type) object

Adapt a type using the constraints of a given language.

hydra.adapt.adapt_type_scheme(constraints: LanguageConstraints, litmap: Mapping[LiteralType, LiteralType], ts0: TypeScheme) object

Adapt a type scheme to the given language constraints, prior to inference.

hydra.adapt.compose_coders(c1: Coder[T0, T1, T2], c2: Coder[T1, T3, T2]) Coder[T0, T3, T2]

Compose two coders into a single coder.

hydra.adapt.data_graph_to_definitions(constraints: LanguageConstraints, do_infer: bool, do_expand: bool, do_hoist_case_statements: bool, do_hoist_polymorphic_let_bindings: bool, original_bindings: Sequence[Binding], graph0: Graph, namespaces: Sequence[ModuleName], cx: InferenceContext) object

Given a data graph along with language constraints, original ordered bindings, and a designated list of namespaces, adapt the graph to the language constraints, then return the processed graph along with term definitions grouped by namespace (in the order of the input namespaces). Inference is performed before adaptation if bindings lack type annotations. Hoisting must preserve type schemes; if any binding loses its type scheme after hoisting, the pipeline fails. Adaptation preserves type application/lambda wrappers and adapts embedded types. Post-adaptation inference is performed to ensure binding TypeSchemes are fully consistent. The doExpand flag controls eta expansion. The doHoistCaseStatements flag controls case statement hoisting (needed for Python). The doHoistPolymorphicLetBindings flag controls polymorphic let binding hoisting (needed for Java). The originalBindings parameter provides the original ordered bindings (from module elements).

hydra.adapt.literal_type_supported(constraints: LanguageConstraints, lt: LiteralType) bool

Check if a literal type is supported by the given language constraints.

hydra.adapt.prepare_float_type(ft: FloatType) tuple[FloatType, tuple[Callable[[FloatValue], FloatValue], Set[T0]]]

Prepare a float type, substituting unsupported types.

hydra.adapt.prepare_integer_type(it: IntegerType) tuple[IntegerType, tuple[Callable[[IntegerValue], IntegerValue], Set[str]]]

Prepare an integer type, substituting unsupported types.

hydra.adapt.prepare_literal_type(at: LiteralType) tuple[LiteralType, tuple[Callable[[Literal], Literal], Set[str]]]

Prepare a literal type, substituting unsupported types.

hydra.adapt.prepare_same(x: T0) tuple[T0, tuple[Callable[[T1], T1], Set[T2]]]

Return a value unchanged with identity transform and no messages.

hydra.adapt.prepare_type(cx: T0, typ: Type) tuple[Type, tuple[Callable[[Term], Term], Set[str]]]

Prepare a type, substituting unsupported literal types.

hydra.adapt.push_type_apps_inward(term: Term) Term

Normalize a term by pushing TermTypeApplication inward past TermApplication and TermLambda. This corrects structures produced by poly-let hoisting and eta expansion, where type applications from inference end up wrapping term applications or lambda abstractions instead of being directly on the polymorphic variable.

hydra.adapt.schema_graph_to_definitions(constraints: LanguageConstraints, graph: Graph, name_lists: Sequence[Sequence[Name]], cx: T0) object

Given a schema graph along with language constraints and a designated list of element names, adapt the graph to the language constraints, then return a corresponding type definition for each element name.

hydra.adapt.simple_language_adapter(lang: Language, cx: T0, g: Graph, typ: Type) object

Given a target language and a source type, produce an adapter which rewrites the type and its terms according to the language’s constraints. The encode direction adapts terms; the decode direction is identity.

hydra.adapt.term_alternatives(cx: T0, graph: Graph, term: Term) object

Find a list of alternatives for a given term, if any.

hydra.adapt.type_alternatives(type: Type) Sequence[Type]

Find a list of alternatives for a given type, if any.