feat: implement built-in State, Reader, and Fail effects

Add runtime support for the State, Reader, and Fail effects that
were already defined in the type system. These effects can now be
used in effectful code blocks.

Changes:
- Add builtin_state and builtin_reader fields to Interpreter
- Implement State.get and State.put in handle_builtin_effect
- Implement Reader.ask in handle_builtin_effect
- Add Reader effect definition to types.rs
- Add Reader to built-in effects list in typechecker
- Add set_state/get_state/set_reader/get_reader methods
- Add 6 new tests for built-in effects
- Add examples/builtin_effects.lux demonstrating usage

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-13 09:46:13 -05:00
parent 62be78ff99
commit c0ef71beb7
5 changed files with 198 additions and 2 deletions

View File

@@ -475,6 +475,10 @@ pub struct Interpreter {
start_time: std::time::Instant,
/// Migration registry: type_name -> (from_version -> to_version -> migration)
migrations: HashMap<String, HashMap<u32, StoredMigration>>,
/// Built-in State effect storage (uses RefCell for interior mutability)
builtin_state: RefCell<Value>,
/// Built-in Reader effect value (uses RefCell for interior mutability)
builtin_reader: RefCell<Value>,
}
impl Interpreter {
@@ -492,9 +496,31 @@ impl Interpreter {
effect_traces: Vec::new(),
start_time: std::time::Instant::now(),
migrations: HashMap::new(),
builtin_state: RefCell::new(Value::Unit),
builtin_reader: RefCell::new(Value::Unit),
}
}
/// Set the initial value for the built-in State effect
pub fn set_state(&self, value: Value) {
*self.builtin_state.borrow_mut() = value;
}
/// Get the current value of the built-in State effect
pub fn get_state(&self) -> Value {
self.builtin_state.borrow().clone()
}
/// Set the value for the built-in Reader effect
pub fn set_reader(&self, value: Value) {
*self.builtin_reader.borrow_mut() = value;
}
/// Get the current value of the built-in Reader effect
pub fn get_reader(&self) -> Value {
self.builtin_reader.borrow().clone()
}
/// Enable effect tracing for debugging
pub fn enable_tracing(&mut self) {
self.trace_effects = true;
@@ -2134,6 +2160,23 @@ impl Interpreter {
span: None,
})
}
("State", "get") => {
Ok(self.builtin_state.borrow().clone())
}
("State", "put") => {
if let Some(value) = request.args.first() {
*self.builtin_state.borrow_mut() = value.clone();
Ok(Value::Unit)
} else {
Err(RuntimeError {
message: "State.put requires a value argument".to_string(),
span: None,
})
}
}
("Reader", "ask") => {
Ok(self.builtin_reader.borrow().clone())
}
_ => Err(RuntimeError {
message: format!(
"Unhandled effect operation: {}.{}",