use crate::env::ty_from_canon;
use crate::types::{EffectSet, Ty};
use indexmap::IndexMap;
use std::collections::HashMap;
use std::sync::OnceLock;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IndexConvention {
None,
Byte,
Codepoint,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BuiltinKind {
Pure,
VmNative,
Effect,
}
#[derive(Debug, Clone, Copy)]
pub struct BuiltinDef {
pub module: &'static str,
pub name: &'static str,
pub ty: &'static str,
pub kind: BuiltinKind,
pub index: IndexConvention,
pub doc: &'static str,
pub complexity: Option<&'static str>,
}
const fn pure(
module: &'static str,
name: &'static str,
ty: &'static str,
index: IndexConvention,
doc: &'static str,
complexity: Option<&'static str>,
) -> BuiltinDef {
BuiltinDef { module, name, ty, kind: BuiltinKind::Pure, index, doc, complexity }
}
const fn native(
module: &'static str,
name: &'static str,
ty: &'static str,
doc: &'static str,
complexity: Option<&'static str>,
) -> BuiltinDef {
BuiltinDef {
module,
name,
ty,
kind: BuiltinKind::VmNative,
index: IndexConvention::None,
doc,
complexity,
}
}
use IndexConvention::{Byte, Codepoint, None as NoIndex};
pub const BUILTINS: &[BuiltinDef] = &[
pure("str", "is_empty", "(Str) -> Bool", NoIndex,
"`true` when the string has no bytes.", None),
pure("str", "to_int", "(Str) -> Option[Int]", NoIndex,
"Parse a decimal integer (optional leading `-`); `None` on any other input.", None),
pure("str", "to_float", "(Str) -> Option[Float]", NoIndex,
"Parse a float literal; `None` on any other input.", None),
pure("str", "concat", "(Str, Str) -> Str", NoIndex,
"Concatenate two strings; `a + b` is the same operation.", None),
pure("str", "len", "(Str) -> Int", Byte,
"Length in UTF-8 bytes, not characters: `str.len(\"é\")` is 2.", Some("O(1)")),
pure("str", "char_at", "(Str, Int) -> Str", Byte,
"The byte at a byte index as a one-character string for ASCII bytes; `\"\"` for a non-ASCII byte or an index out of range. Never fails.",
Some("O(1)")),
pure("str", "split", "(Str, Str) -> List[Str]", NoIndex,
"Split on a separator; an empty separator splits into characters.", None),
pure("str", "join", "(List[Str], Str) -> Str", NoIndex,
"Join the elements with a separator; fails if an element is not a `Str`.", None),
pure("str", "starts_with", "(Str, Str) -> Bool", NoIndex,
"`true` when the first string begins with the second.", None),
pure("str", "ends_with", "(Str, Str) -> Bool", NoIndex,
"`true` when the first string ends with the second.", None),
pure("str", "contains", "(Str, Str) -> Bool", NoIndex,
"`true` when the second string occurs anywhere in the first.", None),
pure("str", "cmp", "(Str, Str) -> Int", NoIndex,
"Three-way byte-order comparison: `-1`, `0` or `1`. Use the comparison operators for a `Bool` (#440).", None),
pure("str", "replace", "(Str, Str, Str) -> Str", NoIndex,
"Replace every non-overlapping occurrence of the second string with the third.", None),
pure("str", "trim", "(Str) -> Str", NoIndex,
"Strip leading and trailing Unicode whitespace.", None),
pure("str", "to_upper", "(Str) -> Str", NoIndex,
"Unicode uppercase.", None),
pure("str", "to_lower", "(Str) -> Str", NoIndex,
"Unicode lowercase.", None),
pure("str", "strip_prefix", "(Str, Str) -> Option[Str]", NoIndex,
"The remainder after a prefix, or `None` when the prefix is absent.", None),
pure("str", "strip_suffix", "(Str, Str) -> Option[Str]", NoIndex,
"The remainder before a suffix, or `None` when the suffix is absent.", None),
pure("str", "slice", "(Str, Int, Int) -> Str", Codepoint,
"Half-open range of codepoint indices `[lo, hi)`; indices clamp to the codepoint count and a reversed range fails (#620).",
Some("O(distance from the previous slice or find on the same string) (#764)")),
pure("str", "is_ascii", "(Str) -> Bool", NoIndex,
"`true` when every byte is below 128; one native pass (#768).", Some("O(len)")),
pure("str", "find", "(Str, Str, Int) -> Option[Int]", Codepoint,
"Codepoint index of the first occurrence of the needle at or after `from`; `from` clamps to the string and an empty needle matches at `from` (#764).",
Some("O(distance scanned)")),
pure("str", "find_any", "(Str, Str, Int) -> Option[Int]", Codepoint,
"Codepoint index of the first character at or after `from` that occurs in the set string (#764).",
Some("O(distance scanned)")),
native("list", "map", "(List[a], (a) -> [| E] b) -> [| E] List[b]",
"Apply the closure to every element; the closure's effects flow to the call.", None),
native("list", "par_map", "(List[a], (a) -> [| E] b) -> [| E] List[b]",
"`map` on a worker pool capped by `LEX_PAR_MAX_CONCURRENCY` (#305).", None),
native("list", "sort_by", "(List[a], (a) -> [| E] b) -> [| E] List[a]",
"Stable sort by the key the closure derives; `Int`, `Float` and `Str` keys order natively, other shapes keep their input order (#338).",
Some("O(n log n)")),
native("list", "filter", "(List[a], (a) -> [| E] Bool) -> [| E] List[a]",
"Keep the elements the closure accepts.", None),
native("list", "fold", "(List[a], b, (b, a) -> [| E] b) -> [| E] b",
"Left fold from the initial accumulator.", None),
pure("list", "len", "(List[a]) -> Int", NoIndex,
"Number of elements.", Some("O(1)")),
pure("list", "is_empty", "(List[a]) -> Bool", NoIndex,
"`true` when the list has no elements.", Some("O(1)")),
pure("list", "range", "(Int, Int) -> List[Int]", NoIndex,
"Integers from `lo` up to but excluding `hi`; empty when `hi <= lo`.", None),
pure("list", "head", "(List[a]) -> Option[a]", NoIndex,
"The first element, or `None` for an empty list.", Some("O(1)")),
pure("list", "tail", "(List[a]) -> List[a]", NoIndex,
"Every element but the first; empty for an empty list.", Some("O(1) when the list is uniquely owned, otherwise O(n) (#774)")),
pure("list", "concat", "(List[a], List[a]) -> List[a]", NoIndex,
"The first list followed by the second.", None),
pure("list", "reverse", "(List[a]) -> List[a]", NoIndex,
"Elements in reverse order.", None),
pure("list", "cons", "(a, List[a]) -> List[a]", NoIndex,
"Prepend one element (#334).", Some("amortised O(1)")),
pure("list", "enumerate", "(List[a]) -> List[(Int, a)]", NoIndex,
"Pair every element with its zero-based index.", None),
];
pub fn declared_modules() -> Vec<&'static str> {
let mut out: Vec<&'static str> = Vec::new();
for d in BUILTINS {
if !out.contains(&d.module) {
out.push(d.module);
}
}
out
}
pub fn is_declared_module(module: &str) -> bool {
BUILTINS.iter().any(|d| d.module == module)
}
pub fn defs_for(module: &str) -> Vec<&'static BuiltinDef> {
BUILTINS.iter().filter(|d| d.module == module).collect()
}
pub fn lookup(module: &str, name: &str) -> Option<&'static BuiltinDef> {
BUILTINS.iter().find(|d| d.module == module && d.name == name)
}
const EFF_VAR_BASE: u32 = 1000;
pub fn parse_signature(def: &BuiltinDef, eff_var: u32) -> Result<Ty, String> {
let src = format!("type Sig = {}\n", def.ty);
let program = lex_syntax::parse_source(&src)
.map_err(|e| format!("{}.{}: cannot parse `{}`: {e:?}", def.module, def.name, def.ty))?;
let stages = lex_ast::canonicalize_program(&program);
let te = stages
.iter()
.find_map(|s| match s {
lex_ast::Stage::TypeDecl(td) => Some(&td.definition),
_ => None,
})
.ok_or_else(|| format!("{}.{}: no type in `{}`", def.module, def.name, def.ty))?;
if !matches!(te, lex_ast::TypeExpr::Function { .. }) {
return Err(format!("{}.{}: `{}` is not a function type", def.module, def.name, def.ty));
}
let mut params: Vec<String> = Vec::new();
collect_type_vars(te, &mut params);
let first_row = params.len();
collect_row_vars(te, &mut params);
let mut ty = ty_from_canon(te, ¶ms);
if params.len() > first_row + 1 {
return Err(format!(
"{}.{}: `{}` names more than one effect row variable",
def.module, def.name, def.ty
));
}
if params.len() == first_row + 1 {
remap_eff_var(&mut ty, first_row as u32, eff_var);
}
Ok(ty)
}
fn is_type_var(name: &str) -> bool {
let mut cs = name.chars();
matches!((cs.next(), cs.next()), (Some(c), None) if c.is_ascii_lowercase())
}
fn collect_type_vars(te: &lex_ast::TypeExpr, out: &mut Vec<String>) {
use lex_ast::TypeExpr as T;
match te {
T::Named { name, args } => {
if args.is_empty() && is_type_var(name) && !out.contains(name) {
out.push(name.clone());
}
for a in args {
collect_type_vars(a, out);
}
}
T::Function { params, ret, .. } => {
for p in params {
collect_type_vars(p, out);
}
collect_type_vars(ret, out);
}
T::Tuple { items } => {
for i in items {
collect_type_vars(i, out);
}
}
T::Record { fields } | T::RecordWithSpreads { fields, .. } => {
for f in fields {
collect_type_vars(&f.ty, out);
}
}
T::Union { variants } => {
for v in variants {
if let Some(p) = &v.payload {
collect_type_vars(p, out);
}
}
}
T::Refined { base, .. } => collect_type_vars(base, out),
}
}
fn collect_row_vars(te: &lex_ast::TypeExpr, out: &mut Vec<String>) {
use lex_ast::TypeExpr as T;
match te {
T::Function { params, effect_row_var, ret, .. } => {
if let Some(v) = effect_row_var {
if !out.contains(v) {
out.push(v.clone());
}
}
for p in params {
collect_row_vars(p, out);
}
collect_row_vars(ret, out);
}
T::Named { args, .. } => {
for a in args {
collect_row_vars(a, out);
}
}
T::Tuple { items } => {
for i in items {
collect_row_vars(i, out);
}
}
T::Record { fields } | T::RecordWithSpreads { fields, .. } => {
for f in fields {
collect_row_vars(&f.ty, out);
}
}
T::Union { variants } => {
for v in variants {
if let Some(p) = &v.payload {
collect_row_vars(p, out);
}
}
}
T::Refined { base, .. } => collect_row_vars(base, out),
}
}
fn remap_eff_var(ty: &mut Ty, from: u32, to: u32) {
match ty {
Ty::Function { params, effects, ret } => {
if effects.var == Some(from) {
*effects = EffectSet { concrete: effects.concrete.clone(), var: Some(to) };
}
for p in params {
remap_eff_var(p, from, to);
}
remap_eff_var(ret, from, to);
}
Ty::List(inner) => remap_eff_var(inner, from, to),
Ty::Tuple(items) => {
for i in items {
remap_eff_var(i, from, to);
}
}
Ty::Record(fields) => {
for v in fields.values_mut() {
remap_eff_var(v, from, to);
}
}
Ty::Con(_, args) => {
for a in args {
remap_eff_var(a, from, to);
}
}
Ty::Var(_) | Ty::Prim(_) | Ty::Unit | Ty::Never => {}
}
}
pub fn module_record(module: &str) -> Option<Ty> {
static CACHE: OnceLock<HashMap<&'static str, Ty>> = OnceLock::new();
let cache = CACHE.get_or_init(|| {
let mut out = HashMap::new();
for m in declared_modules() {
let mut fields = IndexMap::new();
for (i, def) in defs_for(m).into_iter().enumerate() {
let ty = parse_signature(def, EFF_VAR_BASE + i as u32)
.unwrap_or_else(|e| panic!("stdlib_spec: {e}"));
fields.insert(def.name.to_string(), ty);
}
out.insert(m, Ty::Record(fields));
}
out
});
cache.get(module).cloned()
}