hydra.type_script.coder module

TypeScript code generator: emits TypeScript type declarations from Hydra modules.

hydra.type_script.coder.analyze_type_script_function(cx: InferenceContext, g: Graph, term: Term) object

Analyze a term as a TypeScript function, using the Graph as the analysis environment.

hydra.type_script.coder.collect_forall_params(t: Type) Sequence[Name]

Collect the bound parameter names from a chain of nested foralls, in outer-to-inner order; stops at the first non-forall type.

hydra.type_script.coder.collect_imports(current_ns: ModuleName, t: Type) Set[Name]

Collect the names of every type referenced in a type tree that belongs to a different namespace than the current module, for computing the imports needed at the top of the emitted .ts file.

hydra.type_script.coder.collect_inner_type_imports(current_ns: ModuleName, term: Term) Set[Name]

Collect type imports referenced inside a term tree (lambda bodies, type applications, let-binding schemes), supplementing the top-level typeScheme walk.

hydra.type_script.coder.collect_term_imports(current_ns: ModuleName, t: Term) Set[Name]

Like collectImports but walks a Term, gathering free term-level variables that resolve to a different module.

hydra.type_script.coder.eager_free_variables_in_term(term: Term) Set[Name]

Find the free variables of a term that are referenced eagerly, i.e. outside of any nested lambda body; used to break false dependency cycles caused by DSL-level thunks such as hydra.parsers.lazy.

hydra.type_script.coder.encode_binding_as_statement(cx: InferenceContext, g: Graph, current_ns: ModuleName, b: Binding) Statement

Encode a let-binding as a TS statement inside an enclosing function body, hoisting lambda-valued bindings as nested function declarations.

hydra.type_script.coder.encode_lazy_call(cx: InferenceContext, g: Graph, current_ns: ModuleName, head_term: Term, args: Sequence[Term], lazy_flags: Sequence[bool]) Expression

Emit a fully-applied primitive call with selected arguments wrapped in thunks, per a parallel list of laziness flags.

hydra.type_script.coder.encode_literal(lit: Literal) Expression

Render a Hydra literal as a TypeScript expression.

hydra.type_script.coder.encode_literal_type(lt: LiteralType) TypeExpression

Map a Hydra literal type to a TypeScript type expression.

hydra.type_script.coder.encode_param(cx: T0, g: T1, current_ns: ModuleName, pname: Name, dom: Type) Pattern

Build a TS function parameter as a pattern, typed unless the domain is the analyze pass’s untyped-variable sentinel.

hydra.type_script.coder.encode_term(cx: InferenceContext, g: Graph, current_ns: ModuleName, term: Term) Expression

Render a Hydra term as a TypeScript expression.

hydra.type_script.coder.encode_term_definition(cx: InferenceContext, g: Graph, current_ns: ModuleName, td: TermDefinition) tuple[object, ModuleItem]

Render a Hydra term definition as a TypeScript module item.

hydra.type_script.coder.encode_type(cx: T0, g: T1, current_ns: ModuleName, t: Type) object

Map a Hydra type to a TypeScript type expression.

hydra.type_script.coder.encode_type_definition(cx: T0, g: Graph, current_ns: ModuleName, tdef: TypeDefinition) object

Encode a Hydra type definition as an interface declaration, a discriminated-union type alias, or a plain type alias.

hydra.type_script.coder.encode_type_or_any(cx: T0, g: T1, current_ns: ModuleName, typ: Type) TypeExpression

Try to encode a Hydra type as a TS type expression, falling back to any if encodeType fails.

hydra.type_script.coder.filter_non_local_names(current_ns: ModuleName, names: Set[Name]) Set[Name]

Keep only names whose module differs from the current module’s.

hydra.type_script.coder.flatten_application(t: Term) tuple[Term, Sequence[Term]]

Walk an application spine, returning the innermost head term and its arguments in application order.

hydra.type_script.coder.force_lazy_refs(targets0: Set[Name], term0: Term) Term

Rewrite free occurrences of the given target names from x to x(undefined), forcing a thunked binding created by thunkLazyLet.

hydra.type_script.coder.function_declaration_from_term(cx: InferenceContext, g: Graph, current_ns: ModuleName, lname: str, term: Term, _m_scheme: object) FunctionDeclaration

Build a TS function declaration from a Hydra term by peeling lambdas and let bindings into explicit parameters, statements, and a body.

hydra.type_script.coder.imports_to_text(overlay_subs: Set[str], kind: str, current_ns: ModuleName, names: Set[Name]) str

Render a set of qualified names as TypeScript import statements grouped by source module.

hydra.type_script.coder.lazy_flags_for_primitive(g: Graph, name: Name) Sequence[bool]

Look up a primitive by name and return its per-parameter laziness flags in parameter order.

hydra.type_script.coder.let_binding_is_thunk_candidate(b: Binding) bool

True when a let-binding’s value should be thunked to preserve Haskell’s lazy evaluation semantics.

hydra.type_script.coder.mk_doc_comment(mdesc: object) object

Build a documentation comment from an optional description string, or nothing if the description is missing or empty.

hydra.type_script.coder.module_to_type_script(overlay_subs: Set[str], mod: Module, defs: Sequence[Definition], cx: InferenceContext, g: Graph) object

Convert a Hydra module to a map from .ts file path to TypeScript source.

hydra.type_script.coder.print_interface_declaration(decl: InterfaceDeclaration) str

Render an interface declaration.

hydra.type_script.coder.print_literal(lit: Literal) str

Render a TypeScript literal as source text.

hydra.type_script.coder.print_module_item(mi: ModuleItem) str

Render a top-level module item.

hydra.type_script.coder.print_property_signature(ps: PropertySignature) str

Render a property signature, with an optional JSDoc comment prepended.

hydra.type_script.coder.print_type_alias_declaration(decl: TypeAliasDeclaration) str

Render a type alias declaration.

hydra.type_script.coder.print_type_expression(t: TypeExpression) str

Render a TypeScript type expression as source text.

hydra.type_script.coder.print_type_parameter(tp: TypeParameter) str

Render one generic type parameter, with an optional extends constraint.

hydra.type_script.coder.print_type_parameter_list(tps: Sequence[TypeParameter]) str

Render a generic parameter list, or the empty string when there are no parameters.

hydra.type_script.coder.sanitize_param_name(n: Name) str

Sanitize a Hydra parameter name into a valid, non-reserved TS identifier.

hydra.type_script.coder.sort_bindings_topologically(bindings: Sequence[Binding]) Sequence[Binding]

Reorder let-bindings so each appears after the sibling bindings it depends on, avoiding JS temporal-dead-zone errors.

hydra.type_script.coder.sort_term_defs_topologically(current_ns: T0, tdefs: Sequence[TermDefinition]) Sequence[TermDefinition]

Reorder term definitions so each appears after the intra-module definitions it depends on.

hydra.type_script.coder.strip_foralls(t: Type) Type

Strip leading forall quantifiers from a type, returning the innermost body.

hydra.type_script.coder.term_head_variable(t: Term) object

If a term reduces, through annotations and type applications, to a variable reference, return its name.

hydra.type_script.coder.thunk_lazy_bindings(bindings: Sequence[Binding], body: Term) tuple[Sequence[Binding], Term]

The bindings-and-body core of thunkLazyLet, shared by the Term_let encoder and the lifted-binding path in functionDeclarationFromTerm.

hydra.type_script.coder.thunk_lazy_let(lt: Let) Let

Thunk every let-binding for which letBindingIsThunkCandidate holds, and rewrite forced references to it accordingly.

hydra.type_script.coder.ts_array(elems: Sequence[Expression]) Expression

An array expression: [e1, e2, …].

hydra.type_script.coder.ts_arrow(params: Sequence[str], body: Expression) Expression

An untyped arrow function with an expression body.

hydra.type_script.coder.ts_arrow_typed(patterns: Sequence[Pattern], body: Expression) Expression

A typed arrow function with typed parameter patterns.

hydra.type_script.coder.ts_as_any(e: Expression) Expression

Wrap an expression in a TypeScript ‘as any’ cast.

hydra.type_script.coder.ts_call(callee: Expression, args: Sequence[Expression]) Expression

A call expression, parenthesizing the callee when it’s an arrow function or other ambiguous call target.

hydra.type_script.coder.ts_cond(test: Expression, cons: Expression, alt: Expression) Expression

A conditional (ternary) expression.

hydra.type_script.coder.ts_doc_entity_ref(v1: EntityReference) str

Render a {@type hydra.packaging.EntityReference} as TSDoc link syntax.

hydra.type_script.coder.ts_env_get_graph(g: T0) T0

Identity getter for analyzeFunctionTerm when the environment is a Graph.

hydra.type_script.coder.ts_env_set_graph(new_g: T0, _old: T1) T0

Setter for analyzeFunctionTerm when the environment is a Graph.

hydra.type_script.coder.ts_escape_string(s: str) str

Escape a Hydra string for embedding as a double-quoted TypeScript string literal.

hydra.type_script.coder.ts_expr_ident(s: str) Expression

A bare identifier expression.

hydra.type_script.coder.ts_expr_str(s: str) Expression

A string-literal expression.

hydra.type_script.coder.ts_ident(s: str) Identifier

Wrap a string into a TypeScript identifier.

hydra.type_script.coder.ts_member(obj: Expression, prop: str) Expression

A member-access expression: obj.prop.

hydra.type_script.coder.ts_named_type(n: str) TypeExpression

A bare named type reference.

hydra.type_script.coder.ts_new(callee: Expression, args: Sequence[Expression]) Expression

A new-expression: new C(args).

hydra.type_script.coder.ts_object(props: Sequence[tuple[str, Expression]]) Expression

An object literal with identifier-style keys.

hydra.type_script.coder.ts_param(n: str) TypeParameter

A generic type parameter with no constraint.

hydra.type_script.coder.ts_param_app1(n: str, arg: TypeExpression) TypeExpression

A parameterized type with one type argument.

hydra.type_script.coder.ts_param_app2(n: str, a: TypeExpression, b: TypeExpression) TypeExpression

A parameterized type with two type arguments.

hydra.type_script.coder.ts_paren(e: Expression) Expression

Wrap an expression in parentheses when its serialized form needs grouping.

hydra.type_script.coder.ts_prop_sig(name: str, optional: bool, typ: TypeExpression) PropertySignature

A readonly property signature, with the name sanitized for TS reserved words.

hydra.type_script.coder.ts_prop_sig_with_doc(name: str, optional: bool, typ: TypeExpression, mcomments: object) PropertySignature

A readonly property signature with an optional JSDoc comment above the property line.

hydra.type_script.coder.ts_readonly_map(k: TypeExpression, v: TypeExpression) TypeExpression

A ReadonlyMap type with the given key and value types.

hydra.type_script.coder.ts_readonly_set(t: TypeExpression) TypeExpression

A ReadonlySet type with the given element type.

hydra.type_script.coder.ts_tuple(ts: Sequence[TypeExpression]) TypeExpression

A tuple type.

hydra.type_script.coder.ts_typed_ident(name: str, typ: TypeExpression) Pattern

A typed-identifier pattern for a function parameter with a known domain type.

hydra.type_script.coder.ts_undefined() Expression

The undefined value as an expression.