Functions and Actions
Dryft distinguishes between pure functions (fun) and effectful actions (act). This separation decouples logic from state, making code easier to understand, test, and optimize.
Why fun and act?
The distinction between pure and impure code is what matters, not just purity itself:- Pure functions (
fun): Logic code that can be easily understood, tested, and optimized with memoization and compile-time evaluation - Impure actions (
act): State and I/O code that is easily recognizable and portable
Functions (fun)
Functions are pure - they:- Have no side effects
- Always produce the same output for the same input
- Cannot call actions
- Can only manipulate the stack and compute values
Defining Functions
Simple Function Examples
Functions with Type Signatures
Function Composition
Functions naturally compose in concatenative style:Actions (act)
Actions can have side effects:- Perform I/O operations
- Call other actions
- Call pure functions
- Modify external state
Defining Actions
Simple Action Examples
The main Action
Every Dryft program starts with themain action:
Actions with Parameters
Actions can accept parameters via the stack:Purity Enforcement
Functions cannot call actions:Local Variables
Both functions and actions can use local variables:Calling Functions and Actions
Simply use the function or action name:Method Structure
Internally, both functions and actions are stored as methods with:- name: The function/action identifier
- code: The compiled body
- class: Either
FunctionorAction - itypes: Input types consumed from stack
- etypes: Output types produced to stack
Real-World Example
Benefits of Separation
Testability
Pure functions are trivial to test - just check input/output
Optimization
Functions can be memoized and evaluated at compile time
Reasoning
Know at a glance which code has side effects
Portability
Pure logic is easily portable across contexts
Summary
- Use
funfor pure, side-effect-free logic - Use
actfor I/O, state changes, and side effects - Functions cannot call actions (enforced at compile time)
- Actions can call both functions and actions
- This separation leads to cleaner, more maintainable code