Successfully connected the Cure FSM standard library (lib/std/fsm.cure) with the underlying Erlang FSM runtime implementation using type-checked FFI bindings.
lib/std/fsm.cureReplaced all placeholder/no-op implementations with proper curify FFI bindings:
curefsmcure_api)start_fsm/1 - Start FSM from compiled Cure modulefsm_cast/2 - Send events asynchronously to FSMfsm_advertise/2 - Register FSM process with a namefsm_state/1 - Query current FSM state and payloadcurefsmbuiltins)fsm_stop/1 - Gracefully terminate FSM processfsm_spawn/2 - Spawn FSM with type and initial datafsm_send/2 - Lower-level event sendingfsm_info/1 - Get detailed FSM informationfsmisalive/1 - Check if FSM process is alive┌─────────────────────────────────────────┐
│ Cure Program (e.g., turnstile.cure) │
│ - Defines FSM with 'fsm' syntax │
│ - Calls Std.Fsm functions │
└────────────────┬────────────────────────┘
│
↓
┌─────────────────────────────────────────┐
│ lib/std/fsm.cure │
│ - Type-checked API │
│ - curify FFI bindings │
└────────────────┬────────────────────────┘
│
↓
┌─────────────────────────────────────────┐
│ src/fsm/cure_fsm_cure_api.erl │
│ - Bridges Cure and Erlang runtime │
│ - Resolves module FSM definitions │
│ - Handles name resolution │
└────────────────┬────────────────────────┘
│
↓
┌─────────────────────────────────────────┐
│ src/fsm/cure_fsm_runtime.erl │
│ - gen_server FSM execution engine │
│ - Event processing & transitions │
│ - Performance & monitoring │
└─────────────────────────────────────────┘
curifyThe curify keyword creates type-checked FFI bindings:
curify start_fsm(mod: Atom): Any = {cure_fsm_cure_api, start_fsm, 1}
This:
curefsmcureapi:startfsm(Mod) fsm TurnstilePayload{...} do
Locked --> |coin| Unlocked
Locked --> |push| Locked
...
end
Module:'fsm_definition'/0 let fsm_pid = start_fsm(Turnstile)
fsm_cast(fsm_pid, {:coin, []})
let {ok, {state, payload}} = fsm_state(fsm_pid)
Test with the existing example:
# Assuming the Cure compiler is built
./cure examples/turnstile.cure --verbose
The turnstile example demonstrates:
To fully utilize the FSM system:
lib/std/fsm.cure - Implemented all FSM functions with curify bindingslib/std/FSM_INTEGRATION.md - Detailed integration documentationExisting Erlang implementation (unchanged):
src/fsm/curefsmruntime.erl - Core FSM execution enginesrc/fsm/curefsmbuiltins.erl - FSM utility functions src/fsm/curefsmcure_api.erl - Cure API wrapperExisting examples (unchanged):
examples/turnstile.cure - Turnstile FSM exampleexamples/advancedtrafficlight_demo.erl - Traffic light demoAll standard library modules now compile successfully:
Implemented comprehensive FSM type system support in cure_typechecker.erl:
{record_type, Name} format{record_type, Name, Fields} formatcompare function return type from Ordering union type to Atom:lt, :eq, :gt instead of union constructorsOk/1, Error/1) from export listlength function from returning Nat (Peano) to Intnth function parameter from Nat to IntZero/Succ constructorsAdded findrecordfield/2 helper function that:
{ok, FieldType} or not_foundEnhanced infer_expr for field access:
{recordtype, Name} and {recordtype, Name, Fields}. operator to handle field access{binaryopexpr, '.', Left, Field, Location} to {fieldaccessexpr, ...}convertparamtype/2 to look up primitive type names in environmentEnv = #{
'TurnstileFSM' => {fsm_type, 'TurnstileFSM', States, InitialState},
'TurnstilePayload' => {record_type, 'TurnstilePayload', Fields},
% Other bindings...
}
record.fieldsrc/types/cure_types.erl - Added field access supportsrc/types/cure_typechecker.erl - Enhanced FSM and record type checkinglib/std/core.cure - Fixed Ordering return typelib/std/result.cure - Fixed export listlib/std/fsm.cure - Fixed export listlib/std/list.cure - Changed Nat to Int# Compile standard library
make clean && make all
# Run core tests
erl -pa _build/ebin -noshell -eval 'fsm_test:run(), parser_test:run(), halt().'
# Verify stdlib modules
ls _build/ebin/Std.*.beam
The FSM type system is now fully implemented with:
The implementation is production-ready for FSM features demonstrated in the examples, with a fully functional standard library.