feat: implement Random and Time built-in effects

Add Random and Time effects for random number generation and
time-based operations. These effects can be used in any
effectful code block.

Random effect operations:
- Random.int(min, max) - random integer in range [min, max]
- Random.float() - random float in range [0.0, 1.0)
- Random.bool() - random boolean

Time effect operations:
- Time.now() - current Unix timestamp in milliseconds
- Time.sleep(ms) - sleep for specified milliseconds

Changes:
- Add rand crate dependency
- Add Random and Time effect definitions to types.rs
- Add effects to built-in effects list in typechecker
- Implement effect handlers in interpreter
- Add 4 new tests for Random and Time effects
- Add examples/random.lux demonstrating usage

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-13 09:49:09 -05:00
parent c0ef71beb7
commit 52ad5f8781
7 changed files with 258 additions and 3 deletions

View File

@@ -802,6 +802,56 @@ impl TypeEnv {
},
);
// Add Random effect
env.effects.insert(
"Random".to_string(),
EffectDef {
name: "Random".to_string(),
type_params: Vec::new(),
operations: vec![
EffectOpDef {
name: "int".to_string(),
params: vec![
("min".to_string(), Type::Int),
("max".to_string(), Type::Int),
],
return_type: Type::Int,
},
EffectOpDef {
name: "float".to_string(),
params: Vec::new(),
return_type: Type::Float,
},
EffectOpDef {
name: "bool".to_string(),
params: Vec::new(),
return_type: Type::Bool,
},
],
},
);
// Add Time effect
env.effects.insert(
"Time".to_string(),
EffectDef {
name: "Time".to_string(),
type_params: Vec::new(),
operations: vec![
EffectOpDef {
name: "now".to_string(),
params: Vec::new(),
return_type: Type::Int, // Unix timestamp in milliseconds
},
EffectOpDef {
name: "sleep".to_string(),
params: vec![("ms".to_string(), Type::Int)],
return_type: Type::Unit,
},
],
},
);
// Add Some and Ok, Err constructors
// Some : fn(a) -> Option<a>
let a = Type::var();