Skip to main content

Module type_expr

Module type_expr 

Source
Expand description

Type expression utilities for Mangle’s structural type system.

Type expressions are represented as IR instructions (Inst::ApplyFn for compound types, Inst::Name for base types). This module provides:

  • Predicates (is_struct_type, is_union_type, etc.)
  • Accessors (struct_type_field, list_type_arg, etc.)
  • Wellformedness validation (wellformed_type)
  • Type conformance (set_conforms, type_conforms)
  • HasType runtime validation (has_type)
  • TaggedUnion expansion

Constants§

FN_EMPTY_TYPE
FN_FUN
FN_LIST
FN_MAP
FN_OPT
FN_OPTION
FN_PAIR
FN_REL
FN_SINGLETON
FN_STRUCT
FN_TAGGED_UNION
FN_TUPLE
FN_UNION

Functions§

apply_subst
Applies a substitution (type variable -> type) to a type expression. Returns a new InstId with all type variables replaced.
collect_vars
Collects all type variables from a type expression into a set.
empty_type
Creates or finds the empty type sentinel fn:EmptyType().
expand_tagged_union_for_bounds
Like expand_tagged_union_type but uses /name instead of fn:Singleton for the tag field type. This is less precise but compatible with the bounds checker’s type inference (which infers /name for name constants in facts).
expand_tagged_union_type
Expands fn:TaggedUnion(tag_field, tag1, struct1, tag2, struct2, ...) into fn:Union(fn:Struct(tag_field, fn:Singleton(tag1), ...fields1), ...).
find_or_create_name
Finds or creates a Name instruction in the IR.
get_type_context
Gets the type context (all type variables mapped to /any) from the type variables appearing in a type expression.
has_type
Checks if a concrete IR constant value matches a type expression.
intersect_type
Computes the intersection (greatest lower bound) of two type expressions.
is_any
True if this is /any.
is_base_type
True if this is a base type name constant (e.g. /number, /string, /any).
is_empty_type
True if this is the empty type sentinel fn:EmptyType().
is_fun_type
is_list_type
is_map_type
is_name_const
True if this is a Inst::Name (any name constant, including base types and user-defined name hierarchy members like /animal/dog).
is_opt_field
is_option_type
is_pair_type
is_rel_type
is_singleton_type
is_struct_type
is_tagged_union_type
is_tuple_type
is_type_var
True if this is a type variable (Inst::Var).
is_union_type
list_type_arg
Returns the element type of a fn:List type expression.
lower_bound
Computes the lower bound (most general common subtype) of a set of type expressions.
map_type_args
Returns (key_type, value_type) of a fn:Map type expression.
new_list_type
Creates a fn:List(elem_type) type expression.
new_map_type
Creates a fn:Map(key_type, val_type) type expression.
new_rel_type
Builds a fn:Rel(t1, t2, ...) type from a slice of argument types.
new_struct_type
Creates a fn:Struct(field1, type1, ...) type expression.
new_tuple_type
Creates a fn:Tuple(t1, t2, ...) type expression.
new_union_or_single
Builds fn:Union(alts...) or returns the single type if only one.
rel_type_alternatives
Extracts alternatives from a possibly-union-wrapped relation type.
rel_type_args
Extracts the argument types from a fn:Rel(t1, t2, ...).
rel_type_from_alternatives
Creates a single relation type or union from a list of alternatives.
rel_types_from_decl
Builds fn:Rel types from declaration bounds. Each BoundDecl becomes one fn:Rel.
remove_from_union_type
Removes types conforming to to_remove from a union type. Returns the remaining union, or EmptyType if nothing remains.
set_conforms
Checks if left set-conforms to right.
struct_type_field
Returns the type of a struct field by name (simple struct only).
struct_type_field_deep
Returns the type of a struct field by name. Handles Struct, Union, and TaggedUnion by projecting the field from each alternative and returning their upper bound.
struct_type_fields
Iterates struct type args, yielding (field_name_id, field_type_id, is_optional).
tagged_union_tag_field
Returns the tag field InstId (a Name) of a fn:TaggedUnion.
tagged_union_variants
Returns (tags, variant_struct_types) for a fn:TaggedUnion. Tags are at odd indices starting from 1, variant structs at even indices from 2.
type_conforms
Checks structural type conformance (non-union, non-tagged-union).
union_type_args
Returns the alternative type expressions of a fn:Union.
upper_bound
Computes the upper bound (least common supertype) of a set of type expressions.
wellformed_type
Validates that a type expression is well-formed.

Type Aliases§

TypeContext
Type variable context: maps variable NameId to its bound (an InstId type expr).