View Source cure_fsm_typesafety (cure v0.7.0)

FSM Type Safety

This module provides enhanced type checking for FSM definitions, ensuring:

  • State-dependent payload types
  • Event type validation
  • Guard type constraints
  • Action type inference

Type Safety Features

  1. Payload Type Safety: Each state can have its own payload type
  2. Event Typing: Events are type-checked against expected types
  3. Guard Constraints: Guards refine types through constraints
  4. Action Safety: Actions preserve or transform types correctly

Usage

% Check FSM definition for type safety
case cure_fsm_typesafety:check_fsm_types(FsmDef, Env) of
    {ok, TypedFsmDef} ->
        io:format(\"FSM is type-safe~n\");
    {error, TypeError} ->
        io:format(\"Type error: ~p~n\", [TypeError])
end.

Summary

Functions

check_action_types(FsmDef, FsmEnv)

-spec check_action_types(FsmDef :: term(),
                         FsmEnv ::
                             #fsm_type_env{state_payload_types :: #{atom() => term()},
                                           event_types :: #{atom() => term()},
                                           guard_constraints :: [term()],
                                           global_env :: term()}) ->
                            {ok,
                             #fsm_type_env{state_payload_types :: #{atom() => term()},
                                           event_types :: #{atom() => term()},
                                           guard_constraints :: [term()],
                                           global_env :: term()}} |
                            {error, term()}.

check_event_types(FsmDef, FsmEnv)

-spec check_event_types(FsmDef :: term(),
                        FsmEnv ::
                            #fsm_type_env{state_payload_types :: #{atom() => term()},
                                          event_types :: #{atom() => term()},
                                          guard_constraints :: [term()],
                                          global_env :: term()}) ->
                           {ok,
                            #fsm_type_env{state_payload_types :: #{atom() => term()},
                                          event_types :: #{atom() => term()},
                                          guard_constraints :: [term()],
                                          global_env :: term()}} |
                           {error, term()}.

check_fsm_types(FsmDef, Env)

-spec check_fsm_types(FsmDef :: term(), Env :: term()) -> {ok, term()} | {error, term()}.

check_guard_types(FsmDef, FsmEnv)

-spec check_guard_types(FsmDef :: term(),
                        FsmEnv ::
                            #fsm_type_env{state_payload_types :: #{atom() => term()},
                                          event_types :: #{atom() => term()},
                                          guard_constraints :: [term()],
                                          global_env :: term()}) ->
                           {ok,
                            #fsm_type_env{state_payload_types :: #{atom() => term()},
                                          event_types :: #{atom() => term()},
                                          guard_constraints :: [term()],
                                          global_env :: term()}} |
                           {error, term()}.

check_state_payload_types(FsmDef, FsmEnv)

-spec check_state_payload_types(FsmDef :: term(),
                                FsmEnv ::
                                    #fsm_type_env{state_payload_types :: #{atom() => term()},
                                                  event_types :: #{atom() => term()},
                                                  guard_constraints :: [term()],
                                                  global_env :: term()}) ->
                                   {ok,
                                    #fsm_type_env{state_payload_types :: #{atom() => term()},
                                                  event_types :: #{atom() => term()},
                                                  guard_constraints :: [term()],
                                                  global_env :: term()}} |
                                   {error, term()}.

format_type_error(Error)

-spec format_type_error(Error :: term()) -> binary().

infer_payload_type(StateDef, FsmEnv)

-spec infer_payload_type(StateDef :: term(),
                         FsmEnv ::
                             #fsm_type_env{state_payload_types :: #{atom() => term()},
                                           event_types :: #{atom() => term()},
                                           guard_constraints :: [term()],
                                           global_env :: term()}) ->
                            term().