View Source cure_typechecker (cure v0.1.0)

Cure Programming Language - Type Checker

The type checker module provides high-level type checking functionality for Cure programs. It works with parsed AST nodes and implements comprehensive static type analysis including dependent type verification, constraint solving, and type inference.

Features

Comprehensive Type Checking

  • Program-Level Analysis: Full program type checking with module support
  • Function Type Checking: Parameter and return type verification
  • Expression Type Inference: Bottom-up type inference for all expressions
  • Dependent Type Support: Verification of dependent type constraints

Module System Support

  • Module Scoping: Proper scoping of types and functions within modules
  • Export Verification: Ensures exported functions exist and have correct types
  • Import Resolution: Type-safe import of functions and types from other modules
  • Two-Pass Processing: Collects signatures before checking bodies

Advanced Type Features

  • Generic Functions: Full support for parametric polymorphism
  • Constraint Solving: Integration with SMT-based constraint solving
  • FSM Type Checking: Verification of finite state machine definitions
  • Erlang Interop: Type checking for Erlang function interfaces

Error Reporting

  • Detailed Error Messages: Precise error locations with helpful descriptions
  • Warning System: Non-fatal issues that may indicate problems
  • Error Recovery: Continues checking after errors to find more issues
  • Structured Results: Machine-readable error and warning information

Type Checking Process

1. Environment Setup

Env = cure_typechecker:builtin_env(),  % Built-in types and functions
R esult = cure_typechecker:check_program(AST).

2. Module Processing

  • Signature Collection: First pass collects all function signatures
  • Body Checking: Second pass verifies function bodies against signatures
  • Export Validation: Ensures all exported items are properly typed

3. Function Analysis

  • Parameter Processing: Converts parameter types to environment bindings
  • Constraint Checking: Verifies function constraints are boolean expressions
  • Body Inference: Infers body type and checks against declared return type
  • Generic Resolution: Resolves type parameters and constraints

Usage Examples

Program Type Checking

AST = cure_parser:parse_file("example.cure"),
Result = cure_typechecker:check_program(AST),
case Result#typecheck_result.success of
  true ->
    cure_utils:debug("Type checking successful~n");
 
  false -> 
    Errors = Result#typecheck_result.errors,
    cure_utils:debug("Type errors: ~p~n", [Errors])
end.

Function Type Checking

FuncAST = #function_def{name = add, params = Params, body = Body, ...},
{ok, Env, Result} = cure_typechecker:check_function(FuncAST).

Expression Type Inference

{ok, Type} = cure_typechecker:check_expression(ExprAST, Environment).

Type Checking Results

Returns structured results with:

  • Success Flag: Overall type checking success/failure
  • Inferred Types: Types inferred for expressions and functions
  • Error List: Detailed error information with locations
  • Warnings: Non-fatal issues found during checking

Built-in Environment

Provides built-in types and functions:

  • Primitive Types: Int, Float, String, Bool, Atom
  • Type Constructors: List, Tuple, Map, Vector
  • Standard Functions: Arithmetic, logical, and utility functions
  • FSM Operations: Built-in FSM manipulati on functions

Integration

This module integrates with:

  • cure_types: Core type system operations
  • cure_parser: Processes parsed AST nodes
  • cure_smt_solver: Constraint solving for dependent types
  • cure_type_optimizer: Provides type information for optimizations

Error Categories

  • Type Mismatches: Incompatible type assignments or operations
  • Undefined Variables: References to unbound variables
  • Constraint Violations: Failed dependent type constraints
  • Export Errors: Missing or incorrectly typed exported functions
  • Import Errors: Invalid module imports or type mismatches

Performance

  • Two-Pass Efficiency: Minimizes redundant type checking
  • Incremental Checking: Supports incremental compilation scenarios
  • Constraint Caching: Reuses constraint solving results where possible
  • Environment Sharing: Efficient environment management

Thread Safety

The type checker is stateless and thread-safe. Multiple type checking operations can run concurrently on different ASTs.

Summary

Functions

builtin_env()

check_dependent_constraint(Constraint, Value, Type)

check_expression(Expr, Env)

check_expression(Expr, Env, ExpectedType)

check_fsm/2

check_function/2

check_module/2

check_program(AST)

Type checks an entire Cure program.

Performs comprehensive type checking of all top-level items in the program including modules, functions, FSMs, and type definitions.

Arguments

  • AST - List of top-level AST items from the parser

Returns

  • typecheck_result() - Complete type checking results including:
  • success - Boolean indicating overall success/failure
  • type - Program type (usually undefined for programs)
  • errors - List of type checking errors found
  • warnings
  • List of warnings found

Example

AST = cure_parser:parse_file("example.cure"),
Result = cure_typechecker:check_program(AST),
case Result#typecheck_result.success of
  true -> 
    cure_utils:debug("Program type checks successfully~n");
 
  false -> 
    lists:foreach(fun(Error) ->
      cure_utils:debug("Error: ~s~n", [Error#typecheck_error.message])
    end, Result#typecheck_result.errors)
end.

Features

  • Built-in Environment: Uses standard built-in types and functions
  • Error Recovery: Continues checking after errors to find more issues
  • Two-Pass Processing: Collects signatures before checking implementations".

convert_and_resolve_record_field_tuple/2

convert_record_field_def/1

convert_type_to_tuple/1

create_function_type_from_signature(Params, ReturnType)

create_function_type_from_signature_records(Params, ReturnType)

extract_and_add_type_params(TypeExpr, Env)

extract_module_functions(AST)

extract_type_param_value/2

extract_type_params_helper/2

get_stdlib_function_type(Module, Name, Arity)

-spec get_stdlib_function_type(atom(), atom(), integer()) -> {ok, term()} | not_found.

infer_dependent_type(Expr, Env)

infer_type(Expr, Env)

load_stdlib_modules()