feat: expand standard library with Math module and new functions
- Add Math module: abs, min, max, sqrt, pow, floor, ceil, round - Add List functions: isEmpty, find, any, all, take, drop - Add String functions: startsWith, endsWith, toUpper, toLower, substring - Add 12 tests for new standard library functions Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
105
src/types.rs
105
src/types.rs
@@ -974,6 +974,54 @@ impl TypeEnv {
|
||||
"range".to_string(),
|
||||
Type::function(vec![Type::Int, Type::Int], Type::List(Box::new(Type::Int))),
|
||||
),
|
||||
(
|
||||
"isEmpty".to_string(),
|
||||
Type::function(vec![Type::List(Box::new(Type::var()))], Type::Bool),
|
||||
),
|
||||
(
|
||||
"find".to_string(),
|
||||
Type::function(
|
||||
vec![
|
||||
Type::List(Box::new(Type::var())),
|
||||
Type::function(vec![Type::var()], Type::Bool),
|
||||
],
|
||||
Type::Option(Box::new(Type::var())),
|
||||
),
|
||||
),
|
||||
(
|
||||
"any".to_string(),
|
||||
Type::function(
|
||||
vec![
|
||||
Type::List(Box::new(Type::var())),
|
||||
Type::function(vec![Type::var()], Type::Bool),
|
||||
],
|
||||
Type::Bool,
|
||||
),
|
||||
),
|
||||
(
|
||||
"all".to_string(),
|
||||
Type::function(
|
||||
vec![
|
||||
Type::List(Box::new(Type::var())),
|
||||
Type::function(vec![Type::var()], Type::Bool),
|
||||
],
|
||||
Type::Bool,
|
||||
),
|
||||
),
|
||||
(
|
||||
"take".to_string(),
|
||||
Type::function(
|
||||
vec![Type::List(Box::new(Type::var())), Type::Int],
|
||||
Type::List(Box::new(Type::var())),
|
||||
),
|
||||
),
|
||||
(
|
||||
"drop".to_string(),
|
||||
Type::function(
|
||||
vec![Type::List(Box::new(Type::var())), Type::Int],
|
||||
Type::List(Box::new(Type::var())),
|
||||
),
|
||||
),
|
||||
]);
|
||||
env.bind("List", TypeScheme::mono(list_module_type));
|
||||
|
||||
@@ -1017,6 +1065,26 @@ impl TypeEnv {
|
||||
"lines".to_string(),
|
||||
Type::function(vec![Type::String], Type::List(Box::new(Type::String))),
|
||||
),
|
||||
(
|
||||
"startsWith".to_string(),
|
||||
Type::function(vec![Type::String, Type::String], Type::Bool),
|
||||
),
|
||||
(
|
||||
"endsWith".to_string(),
|
||||
Type::function(vec![Type::String, Type::String], Type::Bool),
|
||||
),
|
||||
(
|
||||
"toUpper".to_string(),
|
||||
Type::function(vec![Type::String], Type::String),
|
||||
),
|
||||
(
|
||||
"toLower".to_string(),
|
||||
Type::function(vec![Type::String], Type::String),
|
||||
),
|
||||
(
|
||||
"substring".to_string(),
|
||||
Type::function(vec![Type::String, Type::Int, Type::Int], Type::String),
|
||||
),
|
||||
]);
|
||||
env.bind("String", TypeScheme::mono(string_module_type));
|
||||
|
||||
@@ -1138,6 +1206,43 @@ impl TypeEnv {
|
||||
]);
|
||||
env.bind("Schema", TypeScheme::mono(schema_module_type));
|
||||
|
||||
// Math module
|
||||
let math_module_type = Type::Record(vec![
|
||||
(
|
||||
"abs".to_string(),
|
||||
Type::function(vec![Type::var()], Type::var()), // Works on Int or Float
|
||||
),
|
||||
(
|
||||
"min".to_string(),
|
||||
Type::function(vec![Type::var(), Type::var()], Type::var()),
|
||||
),
|
||||
(
|
||||
"max".to_string(),
|
||||
Type::function(vec![Type::var(), Type::var()], Type::var()),
|
||||
),
|
||||
(
|
||||
"sqrt".to_string(),
|
||||
Type::function(vec![Type::var()], Type::Float),
|
||||
),
|
||||
(
|
||||
"pow".to_string(),
|
||||
Type::function(vec![Type::var(), Type::var()], Type::var()),
|
||||
),
|
||||
(
|
||||
"floor".to_string(),
|
||||
Type::function(vec![Type::var()], Type::Int),
|
||||
),
|
||||
(
|
||||
"ceil".to_string(),
|
||||
Type::function(vec![Type::var()], Type::Int),
|
||||
),
|
||||
(
|
||||
"round".to_string(),
|
||||
Type::function(vec![Type::var()], Type::Int),
|
||||
),
|
||||
]);
|
||||
env.bind("Math", TypeScheme::mono(math_module_type));
|
||||
|
||||
env
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user