← Back to Documentation

FSM Implementation Integration Summary

What Was Done

Successfully connected the Cure FSM standard library (lib/std/fsm.cure) with the underlying Erlang FSM runtime implementation using type-checked FFI bindings.

Changes Made

1. Updated lib/std/fsm.cure

Replaced all placeholder/no-op implementations with proper curify FFI bindings:

Core Functions (using curefsmcure_api)

Additional Functions (using curefsmbuiltins)

Architecture

┌─────────────────────────────────────────┐
│  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            │
└─────────────────────────────────────────┘

How It Works

1. FFI with curify

The curify keyword creates type-checked FFI bindings:

curify start_fsm(mod: Atom): Any = {cure_fsm_cure_api, start_fsm, 1}

This:

2. FSM Lifecycle

  1. Define FSM in Cure module:
   fsm TurnstilePayload{...} do
     Locked --> |coin| Unlocked
     Locked --> |push| Locked
     ...
   end
   
  1. Compile - Generates Module:'fsm_definition'/0
  1. Start FSM:
   let fsm_pid = start_fsm(Turnstile)
   
  1. Send Events:
   fsm_cast(fsm_pid, {:coin, []})
   
  1. Query State:
   let {ok, {state, payload}} = fsm_state(fsm_pid)
   

3. Type Safety

Benefits

  1. Type Safety: Cure's type system validates FSM operations
  2. Performance: Direct Erlang calls with minimal overhead
  3. Integration: Seamless bridge between Cure and Erlang
  4. Completeness: Full FSM runtime capabilities exposed
  5. Error Handling: Proper error propagation with tagged tuples

Testing

Test with the existing example:

# Assuming the Cure compiler is built
./cure examples/turnstile.cure --verbose

The turnstile example demonstrates:

Next Steps

To fully utilize the FSM system:

  1. Register FSM Types: Optionally pre-register FSM types in runtime
  2. Add More Examples: Create more FSM examples (traffic light, protocol, etc.)
  3. Performance Testing: Benchmark FSM event processing
  4. Documentation: Add more inline documentation
  5. Error Messages: Improve error reporting for FSM operations

Files Modified

Files Created

Related Files

Existing Erlang implementation (unchanged):

Existing examples (unchanged):

FSM Type System Implementation Summary

Completed Tasks

1. ✅ Standard Library - Fully Compiled

All standard library modules now compile successfully:

2. ✅ FSM Type System Implementation

Implemented comprehensive FSM type system support in cure_typechecker.erl:

FSM Definition Type Checking

Record Type Support

Type Environment Management

3. ✅ Fixed Standard Library Issues

lib/std/core.cure

lib/std/result.cure

lib/std/fsm.cure

lib/std/list.cure

4. ✅ Field Access Implementation

Added findrecordfield/2 helper function that:

Enhanced infer_expr for field access:

5. ✅ Type System Enhancements

Binary Operator Handling

Type Conversion

Test Results

Passing Tests

Known Issues

Architecture Improvements

Type Environment Structure

Env = #{
  'TurnstileFSM' => {fsm_type, 'TurnstileFSM', States, InitialState},
  'TurnstilePayload' => {record_type, 'TurnstilePayload', Fields},
  % Other bindings...
}

Field Access Flow

  1. Parse field access as record.field
  2. Infer record expression type
  3. Look up record definition if needed
  4. Find field type in record fields
  5. Return field type

Record Type Handling

Files Modified

Core Type System

Standard Library

Verification Commands

# 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

Summary

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.