Module Sarek_typer

Type Unification with Error Reporting

val unify_or_error : Sarek_types.typ -> Sarek_types.typ -> Sarek_ast.loc -> (unit, Sarek_error.error list) Stdlib.result

Unify two types and convert unification errors to typed errors with location

Type Validators

Result-returning validators for type checking with error messages.

These functions validate types and return detailed error messages on failure. For simple boolean checks without errors, use predicates from Sarek_types:

val check_numeric : Sarek_types.typ -> Sarek_ast.loc -> (unit, Sarek_error.error list) Stdlib.result

Check that a type is numeric (int32, int64, float32, float64).

  • parameter t

    The type to check

  • parameter loc

    Source location for error reporting

  • returns

    Ok () if numeric, Error with type mismatch otherwise

See also: Sarek_types.is_numeric for bool predicate version.

val check_integer : Sarek_types.typ -> Sarek_ast.loc -> (unit, Sarek_error.error list) Stdlib.result

Check that a type is integer (int32, int64, int).

  • parameter t

    The type to check

  • parameter loc

    Source location for error reporting

  • returns

    Ok () if integer, Error with type mismatch otherwise

See also: Sarek_types.is_integer for bool predicate version.

val check_boolean : Sarek_types.typ -> Sarek_ast.loc -> (unit, Sarek_error.error list) Stdlib.result

Check that a type is boolean.

  • parameter t

    The type to check

  • parameter loc

    Source location for error reporting

  • returns

    Ok () if boolean, Error with type mismatch otherwise

See also: Sarek_types.is_boolean for bool predicate version.

module TvarCtx : sig ... end

Type variable context for tracking named type variables like 'a, 'b

type tvar_ctx = {
  1. tvars : Sarek_types.typ TvarCtx.t Stdlib.ref;
  2. level : int;
    (*

    Level at which to create new type variables

    *)
}
val fresh_tvar_ctx : ?level:int -> unit -> tvar_ctx

Create a fresh type variable context at a given level

val type_of_type_expr_ctx : Sarek_env.t -> tvar_ctx -> Sarek_ast.type_expr -> Sarek_types.typ

Convert a parsed type expression using the current typing environment and a type variable context for polymorphic type variables

val type_of_type_expr_env : Sarek_env.t -> Sarek_ast.type_expr -> Sarek_types.typ

Convert a parsed type expression using the current typing environment. Creates fresh type variables for each TEVar - use type_of_type_expr_ctx when type variable names need to be preserved across multiple types.

Infer type of a binary operation

val infer_unop : Sarek_ast.unop -> Sarek_types.typ -> Sarek_ast.loc -> (Sarek_types.typ, Sarek_error.error list) Stdlib.result

Infer type of a unary operation

Type Inference Helpers

These helpers partition the main infer function by expression category.

Infer type of literal expressions (unit, bool, int, float constants).

Infer type of binary and unary operations (arithmetic, logical, bitwise).

Infer type of memory access operations (vectors, arrays, record fields). Handles field resolution for both known and external record types.

Infer type of control flow expressions (if, for, while, sequence). Ensures loop bounds are int32 and conditions are boolean.

val infer_data_structure : infer: (Sarek_env.t -> Sarek_ast.expr -> (Sarek_typed_ast.texpr * Sarek_env.t, Sarek_error.error list) Stdlib.result) -> infer_record_fields: (Sarek_env.t -> (string * Sarek_ast.expr) list -> ((string * Sarek_typed_ast.texpr) list * Sarek_env.t, Sarek_error.error list) Stdlib.result) -> infer_list: (Sarek_env.t -> Sarek_ast.expr list -> (Sarek_typed_ast.texpr list * Sarek_env.t, Sarek_error.error list) Stdlib.result) -> Sarek_env.t -> Sarek_ast.loc -> Sarek_ast.expr_desc -> (Sarek_typed_ast.texpr * Sarek_env.t) Sarek_error.result

Infer type of data structures (records, variants, tuples, arrays). Uses mutual recursion helpers for complex nested structures.

Infer type of special expressions (global refs, native blocks, pragmas, type annotations, open). These handle meta-level constructs and environment manipulation.

Infer type of let bindings (assign, let, let mut, let rec). Handles mutability constraints and recursive binding generalization.

Main type inference function

val infer_list : Sarek_env.t -> Sarek_ast.expr list -> (Sarek_typed_ast.texpr list * Sarek_env.t, Sarek_error.error list) Stdlib.result
val unify_args : Sarek_types.typ list -> Sarek_typed_ast.texpr list -> Sarek_ast.loc -> (unit, Sarek_error.error list) Stdlib.result
val infer_record_fields : Sarek_env.t -> (string * Sarek_ast.expr) list -> ((string * Sarek_typed_ast.texpr) list * Sarek_env.t, Sarek_error.error list) Stdlib.result
val infer_patterns : Sarek_env.t -> Sarek_types.typ list -> Sarek_ast.pattern list -> (Sarek_typed_ast.tpattern list * Sarek_env.t, Sarek_error.error list) Stdlib.result
val is_intrinsic_fun : Sarek_env.StringMap.key -> Sarek_env.t -> bool

Check if a name is an intrinsic function in the environment

Type a complete kernel