Module Sarek_types

type prim_type =
  1. | TUnit
  2. | TBool
  3. | TInt32

Primitive types supported in GPU kernels (core language only). Numeric types like float32, float64, int64 are library-defined.

type registered_type =
  1. | Int
    (*

    OCaml int - alias for int32 on GPU

    *)
  2. | Int64
    (*

    64-bit integer

    *)
  3. | Float32
    (*

    32-bit float

    *)
  4. | Float64
    (*

    64-bit float (double)

    *)
  5. | Char
    (*

    8-bit character

    *)
  6. | Custom of string
    (*

    User-registered types via @@sarek.type

    *)

Registered type name - for library-defined types like float32, float64, int64. These are not built-in but are registered by libraries via @@sarek.type.

type memspace =
  1. | Local
    (*

    Thread-private memory

    *)
  2. | Shared
    (*

    Block-shared memory

    *)
  3. | Global
    (*

    Global device memory

    *)

Memory spaces

type typ =
  1. | TPrim of prim_type
    (*

    Primitive types (core language)

    *)
  2. | TReg of registered_type
    (*

    Registered types (library-defined: float32, float64, int64, etc.)

    *)
  3. | TVar of tvar Stdlib.ref
    (*

    Unification variable

    *)
  4. | TVec of typ
    (*

    Vector type (GPU array parameter)

    *)
  5. | TArr of typ * memspace
    (*

    Local array with memory space

    *)
  6. | TFun of typ list * typ
    (*

    Function type

    *)
  7. | TRecord of string * (string * typ) list
    (*

    Record type: name, fields

    *)
  8. | TVariant of string * (string * typ option) list
    (*

    Variant type: name, constructors

    *)
  9. | TTuple of typ list
    (*

    Tuple type

    *)

Types

and tvar =
  1. | Unbound of int * int
    (*

    id, level for generalization

    *)
val tvar_counter : int Stdlib.Atomic.t

Generate fresh type variable IDs (thread-safe)

val fresh_tvar_id : unit -> int
val fresh_tvar : ?level:int -> unit -> typ

Create a fresh unbound type variable at given level

val reset_tvar_counter : unit -> unit

Reset the type variable counter (for testing)

val repr : typ -> typ

Follow links to get the actual type

val occurs : int -> typ -> bool

Check if a type variable occurs in a type (for occurs check)

type unify_error =
  1. | Cannot_unify of typ * typ
  2. | Occurs_check of int * typ

Unification error

val unify : typ -> typ -> (unit, unify_error) Stdlib.result

Unify two types

val pp_prim : Stdlib.Format.formatter -> prim_type -> unit

Pretty printing

val pp_registered : Stdlib.Format.formatter -> registered_type -> unit
val pp_memspace : Stdlib.Format.formatter -> memspace -> unit
val pp_typ : Stdlib.Format.formatter -> typ -> unit
val typ_to_string : typ -> string

Type Constructors and Constants

Helper functions to construct common types.

val t_unit : typ

Primitive type constructors

val t_bool : typ
val t_int32 : typ
val t_vec : typ -> typ

Composite type constructors

val t_arr : typ -> memspace -> typ
val t_fun : typ list -> typ -> typ
val t_float32 : typ

Registered numeric types (library-defined).

These are not built-in primitives but use TReg for type-checking. They must be registered via @@sarek.type attributes.

val t_float64 : typ
val t_int64 : typ
val t_int : typ
val t_char : typ

Type Predicates

Boolean-returning functions to check type properties.

For Result-returning validators with error messages, see Sarek_typer:

val is_numeric : typ -> bool

Check if type is numeric (includes both core int32 and registered float/int types).

val is_integer : typ -> bool

Check if type is integer (core int32 or registered int64)

val is_float : typ -> bool

Check if type is floating point (registered types)

val is_boolean : typ -> bool

Check if type is boolean

val is_tvar : typ -> bool

Check if type is an unbound type variable

Type Conversions

Functions to convert between different type representations.

val type_of_type_expr : Sarek_ast.type_expr -> typ

Convert AST type expression to type (with fresh type variables). Core types (unit, bool, int32) are handled directly. Other types (float32, float64, int64, etc.) are looked up in the type registry.

val memspace_of_ast : Sarek_ast.memspace -> memspace

Convert memspace from AST to types